]> git.saurik.com Git - apple/security.git/blob - Keychain/KDSecItems.m
Security-55471.14.18.tar.gz
[apple/security.git] / Keychain / KDSecItems.m
1 //
2 // KDSecItems.m
3 // Security
4 //
5 // Created by J Osborne on 2/14/13.
6 //
7 //
8
9 #import "KDSecItems.h"
10 #include <Security/Security.h>
11 #include <Security/SecItemPriv.h>
12
13
14 NSString *kKDSecItemsUpdated = @"KDSecItemsUpdated";
15
16 @interface KDSecItems ()
17 @property NSMutableArray *items;
18 @end
19
20 @implementation KDSecItems
21
22 -(NSInteger)numberOfRowsInTableView:(NSTableView*)t
23 {
24 return [self.items count];
25 }
26
27 +(NSString*)nameOfItem:(NSDictionary*)item
28 {
29 id name = item[(id)kSecAttrService];
30 if (name) {
31 return name;
32 }
33
34 NSString *path = item[(id)kSecAttrPath];
35 if (!path) {
36 path = @"/";
37 }
38 NSString *port = item[(id)kSecAttrPort];
39 if ([@"0" isEqualToString:port] || [@0 isEqual:port]) {
40 port = @"";
41 } else {
42 port = [NSString stringWithFormat:@":%@", port];
43 }
44
45 return [NSString stringWithFormat:@"%@://%@%@%@", item[(id)kSecAttrProtocol], item[(id)kSecAttrServer], port, path];
46 }
47
48 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
49 {
50 NSString *identifier = [aTableColumn identifier];
51
52 if ([@"account" isEqualToString:identifier]) {
53 return self.items[rowIndex][(id)kSecAttrAccount];
54 }
55 if ([@"name" isEqualToString:identifier]) {
56 return [KDSecItems nameOfItem:self.items[rowIndex]];
57 }
58
59 return [NSString stringWithFormat:@"*** c=%@ r%ld", [aTableColumn identifier], (long)rowIndex];
60 }
61
62 -(NSArray*)fetchItemsMatching:(NSDictionary *)query
63 {
64 CFTypeRef raw_items = NULL;
65 OSStatus result = SecItemCopyMatching((__bridge CFDictionaryRef)(query), &raw_items);
66 if (result) {
67 // XXX: UI
68 NSLog(@"Error result %d - query: %@", result, query);
69 return nil;
70 }
71 if (CFArrayGetTypeID() == CFGetTypeID(raw_items)) {
72 return (__bridge NSArray*)raw_items;
73 }
74
75 NSLog(@"Unexpected result type from copyMatching: %@ (query=%@)", raw_items, query);
76 CFRelease(raw_items);
77
78 return nil;
79 }
80
81 -(void)loadItems
82 {
83 NSDictionary *query_genp = @{(id)kSecClass: (id)kSecClassGenericPassword,
84 (__bridge id)kSecAttrSynchronizable: @1,
85 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
86 (id)kSecReturnAttributes: (id)kCFBooleanTrue};
87 NSDictionary *query_inet = @{(id)kSecClass: (id)kSecClassInternetPassword,
88 (__bridge id)kSecAttrSynchronizable: @1,
89 (id)kSecMatchLimit: (id)kSecMatchLimitAll,
90 (id)kSecReturnAttributes: (id)kCFBooleanTrue};
91 NSArray *nextItems = [[self fetchItemsMatching:query_genp] arrayByAddingObjectsFromArray:[self fetchItemsMatching:query_inet]];
92 self.items = [[nextItems sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
93 NSDictionary *da = a, *db = b;
94 return [da[(id)kSecAttrService] caseInsensitiveCompare:db[(id)kSecAttrService]];
95 }] mutableCopy];
96
97 dispatch_async(dispatch_get_main_queue(), ^{
98 [[NSNotificationCenter defaultCenter] postNotificationName:kKDSecItemsUpdated object:self];
99 });
100 }
101
102 -(id)init
103 {
104 [self loadItems];
105 return self;
106 }
107
108 @end