Repository navigation
Expand file tree
/
Copy pathmain.m
More file actions
130 lines (113 loc) · 4.97 KB
/
Copy pathmain.m
File metadata and controls
130 lines (113 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// main.m
// Key
//
// Created by Ralph Plawetzki on 08.02.24.
//
#import <Cocoa/Cocoa.h>
// the kServiceName must be the same as in the project settings > Keychain Sharing > Keychain Groups
static NSString * const kServiceName = @"Cryptomator";
static NSString * const password = @"highly_secret";
static NSString * const vault = @"-BCrPdbeoc_y";
SecAccessControlRef createAccessControl(void) {
SecAccessControlCreateFlags flags = kSecAccessControlUserPresence;
SecAccessControlRef accessControl = SecAccessControlCreateWithFlags(
kCFAllocatorDefault,
kSecAttrAccessibleWhenUnlocked,
flags,
NULL // Ignore any error
);
return accessControl;
}
// Method to add an item to the keychain with access control
void addItemToKeychain(void) {
// Create a dictionary of keychain attributes
NSDictionary *keychainAttributes = @{
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: kServiceName,
(__bridge id)kSecAttrAccessControl: (__bridge_transfer id)createAccessControl(),
(__bridge id)kSecAttrAccount: vault,
(__bridge id)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding] // Convert password string to data
};
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)keychainAttributes, NULL);
if (status == errSecSuccess) {
NSLog(@"Item added to keychain successfully.");
} else {
NSLog(@"Error adding item to keychain. Status code: %d", (int)status);
}
}
// Method to retrieve and display an item from the keychain
void displayItemFromKeychain(void) {
// Create a dictionary of search attributes to find the item in the keychain
NSDictionary *searchAttributes = @{
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: kServiceName,
(__bridge id)kSecAttrAccount: vault,
(__bridge id)kSecReturnAttributes: @YES,
(__bridge id)kSecReturnData: @YES,
(__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne
};
CFDictionaryRef result = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)searchAttributes, (CFTypeRef *)&result);
if (status == errSecSuccess && result != NULL) {
NSDictionary *attributes = (__bridge_transfer NSDictionary *)result;
// Extract and display the password from the attributes
NSData *passwordData = attributes[(__bridge id)kSecValueData];
NSString *password = [[NSString alloc] initWithData:passwordData encoding:NSUTF8StringEncoding];
NSLog(@"Item found in keychain. Password: %@", password);
} else if (status == errSecItemNotFound) {
NSLog(@"No matching item found in the keychain.");
} else {
NSLog(@"Error retrieving item from keychain. Status code: %d", (int)status);
}
}
void updateItemFromKeychain(void) {
// Create a dictionary of search attributes to find the item in the keychain
NSDictionary *searchAttributes = @{
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: kServiceName,
(__bridge id)kSecAttrAccount: vault,
(__bridge id)kSecReturnAttributes: @YES,
(__bridge id)kSecReturnData: @YES,
(__bridge id)kSecMatchLimit: (__bridge id)kSecMatchLimitOne
};
CFDictionaryRef result = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)searchAttributes, (CFTypeRef *)&result);
if (status == errSecSuccess && result != NULL) {
NSLog(@"Item found in keychain, updating it.");
NSDictionary *changeAttributes = @{
(__bridge id)kSecValueData: [password dataUsingEncoding:NSUTF8StringEncoding]
};
status = SecItemUpdate((__bridge CFDictionaryRef)searchAttributes, (__bridge CFDictionaryRef)changeAttributes);
NSLog(@"Updated. Status code: %d", (int)status);
} else if (status == errSecItemNotFound) {
NSLog(@"No matching item found in the keychain.");
} else {
NSLog(@"Error updating item in keychain. Status code: %d", (int)status);
}
}
void deleteItemFromKeychain(void) {
NSDictionary *searchAttributes = @{
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService: kServiceName,
(__bridge id)kSecAttrAccount: vault
};
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)searchAttributes);
if (status == errSecSuccess) {
NSLog(@"Item deleted from keychain successfully.");
} else if (status == errSecItemNotFound) {
NSLog(@"No matching item found in the keychain.");
} else {
NSLog(@"Error deleting item from keychain. Status code: %d", (int)status);
}
}
// Main function
int main(int argc, const char * argv[]) {
@autoreleasepool {
//addItemToKeychain();
displayItemFromKeychain();
//updateItemFromKeychain();
//deleteItemFromKeychain();
}
return 0;
}