]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/SecureObjectSync/SOSAccountGetSet.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / SecureObjectSync / SOSAccountGetSet.m
1 //
2 // SOSAccountGetSet.c
3 // Security
4 //
5 //
6
7 #include "SOSAccountPriv.h"
8
9 #include <utilities/SecCFWrappers.h>
10
11 #import <Security/SecureObjectSync/SOSAccountTrust.h>
12 #import <Security/SecureObjectSync/SOSAccountTrustClassic.h>
13
14 //
15 // MARK: Generic Value manipulation
16 //
17
18 static inline bool SOSAccountEnsureExpansion(SOSAccount* account, CFErrorRef *error) {
19
20 if (!account.trust.expansion) {
21 account.trust.expansion = [NSMutableDictionary dictionary];
22 }
23
24 return SecAllocationError(((__bridge CFDictionaryRef)account.trust.expansion), error, CFSTR("Can't Alloc Account Expansion dictionary"));
25 }
26
27 bool SOSAccountClearValue(SOSAccount* account, CFStringRef key, CFErrorRef *error) {
28 bool success = SOSAccountEnsureExpansion(account, error);
29 if(!success){
30 return success;
31 }
32
33 [account.trust.expansion removeObjectForKey: (__bridge NSString* _Nonnull)(key)];
34
35 return success;
36 }
37
38 bool SOSAccountSetValue(SOSAccount* account, CFStringRef key, CFTypeRef value, CFErrorRef *error) {
39 if (value == NULL) return SOSAccountClearValue(account, key, error);
40
41 bool success = SOSAccountEnsureExpansion(account, error);
42 if(!success)
43 return success;
44
45 [account.trust.expansion setObject:(__bridge id _Nonnull)(value) forKey:(__bridge NSString* _Nonnull)(key)];
46
47 return success;
48 }
49
50 //
51 // MARK: UUID
52 CFStringRef SOSAccountCopyUUID(SOSAccount* account) {
53 CFStringRef uuid = CFRetainSafe(asString(SOSAccountGetValue(account, kSOSAccountUUID, NULL), NULL));
54 if (uuid == NULL) {
55 CFUUIDRef newID = CFUUIDCreate(kCFAllocatorDefault);
56 uuid = CFUUIDCreateString(kCFAllocatorDefault, newID);
57
58 CFErrorRef setError = NULL;
59 if (!SOSAccountSetValue(account, kSOSAccountUUID, uuid, &setError)) {
60 secerror("Failed to set UUID: %@ (%@)", uuid, setError);
61 }
62 CFReleaseNull(setError);
63 CFReleaseNull(newID);
64 }
65 return uuid;
66 }
67
68 void SOSAccountEnsureUUID(SOSAccount* account) {
69 CFStringRef uuid = SOSAccountCopyUUID(account);
70 CFReleaseNull(uuid);
71 }
72