5 // Created by Richard Murphy on 1/26/15.
9 #include <AssertMacros.h>
10 #include "SOSPeerInfoV2.h"
11 #include <Security/SecureObjectSync/SOSInternal.h>
12 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
13 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
14 #include <Security/SecureObjectSync/SOSPeerInfoInternal.h>
15 #include <utilities/der_plist.h>
16 #include <utilities/der_plist_internal.h>
17 #include <corecrypto/ccder.h>
18 #include <utilities/der_date.h>
21 // Description Dictionary Entries Added for V2
22 CFStringRef sV2DictionaryKey = CFSTR("V2DictionaryData"); // CFData wrapper for V2 extensions
23 CFStringRef sViewsKey = CFSTR("Views"); // Array of View labels
24 CFStringRef sSerialNumberKey = CFSTR("SerialNumber");
25 CFStringRef sViewsPending = CFSTR("ViewsPending"); // Array of View labels (pending)
27 CFStringRef sSecurityPropertiesKey = CFSTR("SecurityProperties");
28 CFStringRef kSOSHsaCrKeyDictionary = CFSTR("HSADictionary");
29 CFStringRef sRingState = CFSTR("RingState");
30 CFStringRef sBackupKeyKey = CFSTR("BackupKey");
31 CFStringRef sEscrowRecord = CFSTR("EscrowRecord");
35 // need entitlement for this:
37 #include <MobileGestalt.h>
39 static CFStringRef SOSCopySerialNumberAsString(CFErrorRef *error) {
40 CFTypeRef iosAnswer = (CFStringRef) MGCopyAnswer(kMGQSerialNumber, NULL);
42 SOSCreateError(kSOSErrorAllocationFailure, CFSTR("No Memory"), NULL, error);
44 return (CFStringRef) iosAnswer;
49 #include <CoreFoundation/CoreFoundation.h>
50 #include <IOKit/IOKitLib.h>
52 static CFStringRef SOSCopySerialNumberAsString(CFErrorRef *error) {
53 CFStringRef serialNumber = NULL;
54 CFStringRef retval = NULL;
56 io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
57 require_action_quiet(platformExpert, errOut, SOSCreateError(kSOSErrorAllocationFailure, CFSTR("No Memory"), NULL, error));
58 serialNumber = IORegistryEntryCreateCFProperty(platformExpert, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0);
59 if(serialNumber) retval = CFStringCreateCopy(kCFAllocatorDefault, serialNumber);
60 IOObjectRelease(platformExpert);
61 CFReleaseNull(serialNumber);
68 bool SOSPeerInfoSerialNumberIsSet(SOSPeerInfoRef pi) {
69 CFStringRef serial = SOSPeerInfoV2DictionaryCopyString(pi, sSerialNumberKey);
70 bool retval = (serial != NULL);
71 CFReleaseNull(serial);
75 void SOSPeerInfoSetSerialNumber(SOSPeerInfoRef pi) {
76 CFStringRef serialNumber = SOSCopySerialNumberAsString(NULL);
77 if(serialNumber) SOSPeerInfoV2DictionarySetValue(pi, sSerialNumberKey, serialNumber);
78 CFReleaseNull(serialNumber);
81 const CFStringRef SOSSerialUnknown = CFSTR("Unknown");
83 void SOSPeerInfoSetTestSerialNumber(SOSPeerInfoRef pi, CFStringRef serialNumber) {
84 if(serialNumber) SOSPeerInfoV2DictionarySetValue(pi, sSerialNumberKey, serialNumber);
87 CFStringRef SOSPeerInfoCopySerialNumber(SOSPeerInfoRef pi) {
88 CFStringRef retval = SOSPeerInfoV2DictionaryCopyString(pi, sSerialNumberKey);
89 return (retval ? retval : CFRetain(SOSSerialUnknown));
92 CFStringRef SOSPeerInfoCopyOSVersion(SOSPeerInfoRef pi) {
93 return SOSPeerInfoV2DictionaryCopyString(pi, kPIOSVersionKey);
97 static bool SOSPeerInfoV2SanityCheck(SOSPeerInfoRef pi) {
101 if(!SOSPeerInfoVersionHasV2Data(pi)) {
107 static CFDataRef SOSPeerInfoGetV2Data(SOSPeerInfoRef pi) {
108 if(SOSPeerInfoV2SanityCheck(pi) == false) return NULL;
109 return asData(CFDictionaryGetValue(pi->description, sV2DictionaryKey), NULL);
112 static CFMutableDictionaryRef SOSCreateDictionaryFromDER(CFDataRef v2Data, CFErrorRef *error) {
113 CFPropertyListRef pl = NULL;
116 secerror("Creating raw dictionary instead of creating from DER");
117 return CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
120 if(CFGetTypeID(v2Data) != CFDataGetTypeID()) {
121 SOSCreateError(kSOSErrorBadFormat, CFSTR("Corrupted v2Data Item"), NULL, error);
125 const uint8_t *der_p = CFDataGetBytePtr(v2Data);
126 const uint8_t *der_end = CFDataGetLength(v2Data) + der_p;
128 der_p = der_decode_plist(NULL, kCFPropertyListImmutable, &pl, error, der_p, der_end);
130 if (der_p == NULL || der_p != der_end) {
131 SOSCreateError(kSOSErrorBadFormat, CFSTR("Bad Format of Dictionary DER"), NULL, error);
135 if (CFGetTypeID(pl) != CFDictionaryGetTypeID()) {
136 CFStringRef description = CFCopyTypeIDDescription(CFGetTypeID(pl));
137 SOSCreateErrorWithFormat(kSOSErrorUnexpectedType, NULL, error, NULL,
138 CFSTR("Expected dictionary got %@"), description);
139 CFReleaseNull(description);
144 return (CFMutableDictionaryRef) pl;
152 static CFDataRef SOSCreateDERFromDictionary(CFDictionaryRef di, CFErrorRef *error) {
153 size_t size = der_sizeof_plist(di, error);
157 uint8_t *der = malloc(size);
161 if (der_encode_plist(di, error, der, der+size) == NULL) {
165 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, der, size, kCFAllocatorMalloc);
169 bool SOSPeerInfoUpdateToV2(SOSPeerInfoRef pi, CFErrorRef *error) {
171 CFDataRef v2data = NULL;
172 if(!pi) return false;
174 SOSPeerInfoSetVersionNumber(pi, PEERINFO_CURRENT_VERSION);
175 CFMutableDictionaryRef v2Dictionary = CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
176 CFMutableSetRef views = SOSViewCopyViewSet(kViewSetDefault);
177 CFMutableSetRef secproperties = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
178 CFDictionaryAddValue(v2Dictionary, sViewsKey, views);
179 CFDictionaryAddValue(v2Dictionary, sSecurityPropertiesKey, secproperties);
181 CFDictionaryAddValue(v2Dictionary, sDeviceID, CFSTR(""));
182 CFDictionaryAddValue(v2Dictionary, sTransportType, SOSTransportMessageTypeKVS);
183 CFDictionaryAddValue(v2Dictionary, sPreferIDS, kCFBooleanFalse);
184 CFDictionaryAddValue(v2Dictionary, sPreferIDSFragmentation, kCFBooleanTrue);
185 CFDictionaryAddValue(v2Dictionary, sPreferIDSACKModel, kCFBooleanTrue);
187 require_action_quiet((v2data = SOSCreateDERFromDictionary(v2Dictionary, error)), out, SOSCreateError(kSOSErrorAllocationFailure, CFSTR("No Memory"), NULL, error));
188 CFDictionaryAddValue(pi->description, sV2DictionaryKey, v2data);
189 // To use the central serial number setting routine this must be done after the V2 dictionary is installed.
190 SOSPeerInfoSetSerialNumber(pi);
191 SOSPeerInfoExpandV2Data(pi, error);
194 CFReleaseNull(views);
195 CFReleaseNull(v2data);
196 CFReleaseNull(v2Dictionary);
197 CFReleaseNull(secproperties);
201 void SOSPeerInfoPackV2Data(SOSPeerInfoRef pi) {
202 require(SOSPeerInfoV2SanityCheck(pi), errOut);
203 require_quiet(pi->v2Dictionary, errOut);
204 CFDataRef v2der = SOSCreateDERFromDictionary(pi->v2Dictionary, NULL);
205 CFDictionarySetValue(pi->description, sV2DictionaryKey, v2der);
206 CFReleaseNull(v2der);
211 bool SOSPeerInfoExpandV2Data(SOSPeerInfoRef pi, CFErrorRef *error) {
212 CFDataRef v2data = NULL;
213 CFMutableDictionaryRef v2Dictionary = NULL;
215 require_action_quiet((v2data = SOSPeerInfoGetV2Data(pi)), out, SOSCreateError(kSOSErrorDecodeFailure, CFSTR("No V2 Data in description"), NULL, error));
216 require_action_quiet((v2Dictionary = SOSCreateDictionaryFromDER(v2data, error)), out, SOSCreateError(kSOSErrorDecodeFailure, CFSTR("Can't expand V2 Dictionary"), NULL, error));
217 CFReleaseNull(pi->v2Dictionary);
218 pi->v2Dictionary = v2Dictionary;
222 CFReleaseNull(v2Dictionary);
227 void SOSPeerInfoV2DictionarySetValue(SOSPeerInfoRef pi, const void *key, const void *value) {
228 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
230 CFDictionaryRemoveValue(pi->v2Dictionary, key);
232 CFDictionarySetValue(pi->v2Dictionary, key, value);
233 SOSPeerInfoPackV2Data(pi);
238 void SOSPeerInfoV2DictionaryRemoveValue(SOSPeerInfoRef pi, const void *key) {
239 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
240 CFDictionaryRemoveValue(pi->v2Dictionary, key);
241 SOSPeerInfoPackV2Data(pi);
246 bool SOSPeerInfoV2DictionaryHasBoolean(SOSPeerInfoRef pi, const void *key) {
247 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
248 CFTypeRef value = CFDictionaryGetValue(pi->v2Dictionary, key);
249 if(asBoolean(value,NULL) != NULL)
255 // Not API to prevent owenership confusion
256 static CFStringRef SOSPeerInfoV2DictionaryGetAsString(SOSPeerInfoRef pi, const void *key) {
257 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
258 return asString(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
264 bool SOSPeerInfoV2DictionaryHasStringValue(SOSPeerInfoRef pi, const void *key, CFStringRef value) {
265 return CFEqualSafe(SOSPeerInfoV2DictionaryGetAsString(pi, key), value);
268 bool SOSPeerInfoV2DictionaryHasString(SOSPeerInfoRef pi, const void *key) {
269 CFStringRef possibleID = SOSPeerInfoV2DictionaryGetAsString(pi, key);
270 return possibleID != NULL && CFStringGetLength(possibleID) > 0;
273 bool SOSPeerInfoV2DictionaryHasSet(SOSPeerInfoRef pi, const void *key) {
274 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
275 CFTypeRef value = CFDictionaryGetValue(pi->v2Dictionary, key);
276 if(asSet(value,NULL) != NULL)
282 bool SOSPeerInfoV2DictionaryHasData(SOSPeerInfoRef pi, const void *key) {
283 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
284 CFTypeRef value = CFDictionaryGetValue(pi->v2Dictionary, key);
285 if(asData(value,NULL) != NULL)
291 CFMutableStringRef SOSPeerInfoV2DictionaryCopyString(SOSPeerInfoRef pi, const void *key) {
292 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
293 CFStringRef value = asString(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
295 return CFStringCreateMutableCopy(kCFAllocatorDefault, CFStringGetLength(value), value);
300 static void SOSPeerInfoV2DictionaryWithValue(SOSPeerInfoRef pi, const void *key, void(^operation)(CFTypeRef value)) {
301 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
302 CFTypeRef value = CFRetainSafe(CFDictionaryGetValue(pi->v2Dictionary, key));
304 CFReleaseNull(value);
309 void SOSPeerInfoV2DictionaryWithSet(SOSPeerInfoRef pi, const void *key, void(^operation)(CFSetRef set)) {
310 SOSPeerInfoV2DictionaryWithValue(pi, key, ^(CFTypeRef value) {
311 CFSetRef set = asSet(value, NULL);
318 CFMutableSetRef SOSPeerInfoV2DictionaryCopySet(SOSPeerInfoRef pi, const void *key) {
319 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
320 CFSetRef value = asSet(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
322 return CFSetCreateMutableCopy(kCFAllocatorDefault, CFSetGetCount(value), value);
327 void SOSPeerInfoV2DictionaryForEachSetValue(SOSPeerInfoRef pi, const void *key, void (^action)(const void* value)) {
328 CFSetRef value = NULL;
329 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
330 value = asSet(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
334 CFSetForEach(value, action);
338 bool SOSPeerInfoV2DictionaryHasSetContaining(SOSPeerInfoRef pi, const void *key, const void* member) {
339 CFSetRef value = NULL;
340 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
341 value = asSet(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
343 return value && CFSetContainsValue(value, member);
346 CFMutableDataRef SOSPeerInfoV2DictionaryCopyData(SOSPeerInfoRef pi, const void *key) {
347 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
348 CFDataRef value = asData(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
350 return CFDataCreateMutableCopy(kCFAllocatorDefault, CFDataGetLength(value), value);
355 CFBooleanRef SOSPeerInfoV2DictionaryCopyBoolean(SOSPeerInfoRef pi, const void *key) {
356 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
357 CFBooleanRef value = asBoolean(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
359 return CFRetainSafe(value);
364 CFMutableDictionaryRef SOSPeerInfoV2DictionaryCopyDictionary(SOSPeerInfoRef pi, const void *key) {
365 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
366 CFDictionaryRef value = asDictionary(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
368 return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, CFDictionaryGetCount(value), value);
374 SOSPeerInfoRef SOSPeerInfoCopyWithV2DictionaryUpdate(CFAllocatorRef allocator, SOSPeerInfoRef toCopy, CFDictionaryRef newv2dict, SecKeyRef signingKey, CFErrorRef* error) {
375 SOSPeerInfoRef retval = SOSPeerInfoCreateCopy(kCFAllocatorDefault, toCopy, error);
376 require_quiet(retval, errOut);
377 require_action_quiet(SOSPeerInfoExpandV2Data(retval, error), errOut, CFReleaseNull(retval));
378 CFDictionaryForEach(newv2dict, ^(const void *key, const void *value) {
379 CFDictionarySetValue(retval->v2Dictionary, key, value);
381 SOSPeerInfoPackV2Data(retval);
382 require_action_quiet(SOSPeerInfoSign(signingKey, retval, error), errOut, CFReleaseNull(retval));