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 CFMutableDictionaryRef retval = NULL;
114 CFPropertyListRef pl = NULL;
117 secerror("Creating raw dictionary instead of creating from DER");
118 return CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
121 if(CFGetTypeID(v2Data) != CFDataGetTypeID()) {
122 SOSCreateError(kSOSErrorBadFormat, CFSTR("Corrupted v2Data Item"), NULL, error);
126 const uint8_t *der_p = CFDataGetBytePtr(v2Data);
127 const uint8_t *der_end = CFDataGetLength(v2Data) + der_p;
129 der_p = der_decode_plist(NULL, kCFPropertyListImmutable, &pl, error, der_p, der_end);
131 if (der_p == NULL || der_p != der_end) {
132 SOSCreateError(kSOSErrorBadFormat, CFSTR("Bad Format of Dictionary DER"), NULL, error);
136 if (CFGetTypeID(pl) != CFDictionaryGetTypeID()) {
137 CFStringRef description = CFCopyTypeIDDescription(CFGetTypeID(pl));
138 SOSCreateErrorWithFormat(kSOSErrorUnexpectedType, NULL, error, NULL,
139 CFSTR("Expected dictionary got %@"), description);
140 CFReleaseSafe(description);
145 retval = (CFMutableDictionaryRef) pl;
149 CFReleaseNull(retval);
154 static CFDataRef SOSCreateDERFromDictionary(CFDictionaryRef di, CFErrorRef *error) {
155 size_t size = der_sizeof_plist(di, error);
156 if (size == 0) return NULL;
158 der_encode_plist(di, error, der, der+size);
159 return CFDataCreate(kCFAllocatorDefault, der, size);
163 bool SOSPeerInfoUpdateToV2(SOSPeerInfoRef pi, CFErrorRef *error) {
165 CFDataRef v2data = NULL;
166 if(!pi) return false;
168 SOSPeerInfoSetVersionNumber(pi, PEERINFO_CURRENT_VERSION);
169 CFMutableDictionaryRef v2Dictionary = CFDictionaryCreateMutable(NULL, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
170 CFMutableSetRef views = SOSViewCopyViewSet(kViewSetDefault);
171 CFMutableSetRef secproperties = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);
172 CFDictionaryAddValue(v2Dictionary, sViewsKey, views);
173 CFDictionaryAddValue(v2Dictionary, sSecurityPropertiesKey, secproperties);
175 CFDictionaryAddValue(v2Dictionary, sDeviceID, CFSTR(""));
176 CFDictionaryAddValue(v2Dictionary, sTransportType, SOSTransportMessageTypeIDSV2);
177 CFDictionaryAddValue(v2Dictionary, sPreferIDS, kCFBooleanFalse);
178 CFDictionaryAddValue(v2Dictionary, sPreferIDSFragmentation, kCFBooleanTrue);
179 CFDictionaryAddValue(v2Dictionary, sPreferIDSACKModel, kCFBooleanTrue);
181 require_action_quiet((v2data = SOSCreateDERFromDictionary(v2Dictionary, error)), out, SOSCreateError(kSOSErrorAllocationFailure, CFSTR("No Memory"), NULL, error));
182 CFDictionaryAddValue(pi->description, sV2DictionaryKey, v2data);
183 // To use the central serial number setting routine this must be done after the V2 dictionary is installed.
184 SOSPeerInfoSetSerialNumber(pi);
185 SOSPeerInfoExpandV2Data(pi, error);
188 CFReleaseNull(views);
189 CFReleaseNull(v2data);
190 CFReleaseNull(v2Dictionary);
194 void SOSPeerInfoPackV2Data(SOSPeerInfoRef pi) {
195 require(SOSPeerInfoV2SanityCheck(pi), errOut);
196 require_quiet(pi->v2Dictionary, errOut);
197 CFDataRef v2der = SOSCreateDERFromDictionary(pi->v2Dictionary, NULL);
198 CFDictionarySetValue(pi->description, sV2DictionaryKey, v2der);
199 CFReleaseNull(v2der);
204 bool SOSPeerInfoExpandV2Data(SOSPeerInfoRef pi, CFErrorRef *error) {
205 CFDataRef v2data = NULL;
206 CFMutableDictionaryRef v2Dictionary = NULL;
208 require_action_quiet((v2data = SOSPeerInfoGetV2Data(pi)), out, SOSCreateError(kSOSErrorDecodeFailure, CFSTR("No V2 Data in description"), NULL, error));
209 require_action_quiet((v2Dictionary = SOSCreateDictionaryFromDER(v2data, error)), out, SOSCreateError(kSOSErrorDecodeFailure, CFSTR("Can't expand V2 Dictionary"), NULL, error));
210 CFReleaseNull(pi->v2Dictionary);
211 pi->v2Dictionary = v2Dictionary;
215 CFReleaseNull(v2Dictionary);
220 void SOSPeerInfoV2DictionarySetValue(SOSPeerInfoRef pi, const void *key, const void *value) {
221 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
223 CFDictionaryRemoveValue(pi->v2Dictionary, key);
225 CFDictionarySetValue(pi->v2Dictionary, key, value);
226 SOSPeerInfoPackV2Data(pi);
231 void SOSPeerInfoV2DictionaryRemoveValue(SOSPeerInfoRef pi, const void *key) {
232 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
233 CFDictionaryRemoveValue(pi->v2Dictionary, key);
234 SOSPeerInfoPackV2Data(pi);
239 bool SOSPeerInfoV2DictionaryHasBoolean(SOSPeerInfoRef pi, const void *key) {
240 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
241 CFTypeRef value = CFDictionaryGetValue(pi->v2Dictionary, key);
242 if(asBoolean(value,NULL) != NULL)
248 // Not API to prevent owenership confusion
249 static CFStringRef SOSPeerInfoV2DictionaryGetAsString(SOSPeerInfoRef pi, const void *key) {
250 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
251 return asString(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
257 bool SOSPeerInfoV2DictionaryHasStringValue(SOSPeerInfoRef pi, const void *key, CFStringRef value) {
258 return CFEqualSafe(SOSPeerInfoV2DictionaryGetAsString(pi, key), value);
261 bool SOSPeerInfoV2DictionaryHasString(SOSPeerInfoRef pi, const void *key) {
262 CFStringRef possibleID = SOSPeerInfoV2DictionaryGetAsString(pi, key);
263 return possibleID != NULL && CFStringGetLength(possibleID) > 0;
266 bool SOSPeerInfoV2DictionaryHasSet(SOSPeerInfoRef pi, const void *key) {
267 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
268 CFTypeRef value = CFDictionaryGetValue(pi->v2Dictionary, key);
269 if(asSet(value,NULL) != NULL)
275 bool SOSPeerInfoV2DictionaryHasData(SOSPeerInfoRef pi, const void *key) {
276 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
277 CFTypeRef value = CFDictionaryGetValue(pi->v2Dictionary, key);
278 if(asData(value,NULL) != NULL)
284 CFMutableStringRef SOSPeerInfoV2DictionaryCopyString(SOSPeerInfoRef pi, const void *key) {
285 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
286 CFStringRef value = asString(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
288 return CFStringCreateMutableCopy(kCFAllocatorDefault, CFStringGetLength(value), value);
293 static void SOSPeerInfoV2DictionaryWithValue(SOSPeerInfoRef pi, const void *key, void(^operation)(CFTypeRef value)) {
294 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
295 CFTypeRef value = CFRetainSafe(CFDictionaryGetValue(pi->v2Dictionary, key));
297 CFReleaseNull(value);
302 void SOSPeerInfoV2DictionaryWithSet(SOSPeerInfoRef pi, const void *key, void(^operation)(CFSetRef set)) {
303 SOSPeerInfoV2DictionaryWithValue(pi, key, ^(CFTypeRef value) {
304 CFSetRef set = asSet(value, NULL);
311 CFMutableSetRef SOSPeerInfoV2DictionaryCopySet(SOSPeerInfoRef pi, const void *key) {
312 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
313 CFSetRef value = asSet(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
315 return CFSetCreateMutableCopy(kCFAllocatorDefault, CFSetGetCount(value), value);
320 void SOSPeerInfoV2DictionaryForEachSetValue(SOSPeerInfoRef pi, const void *key, void (^action)(const void* value)) {
321 CFSetRef value = NULL;
322 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
323 value = asSet(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
327 CFSetForEach(value, action);
331 bool SOSPeerInfoV2DictionaryHasSetContaining(SOSPeerInfoRef pi, const void *key, const void* member) {
332 CFSetRef value = NULL;
333 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
334 value = asSet(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
336 return value && CFSetContainsValue(value, member);
339 CFMutableDataRef SOSPeerInfoV2DictionaryCopyData(SOSPeerInfoRef pi, const void *key) {
340 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
341 CFDataRef value = asData(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
343 return CFDataCreateMutableCopy(kCFAllocatorDefault, CFDataGetLength(value), value);
348 CFBooleanRef SOSPeerInfoV2DictionaryCopyBoolean(SOSPeerInfoRef pi, const void *key) {
349 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
350 CFBooleanRef value = asBoolean(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
352 return CFRetainSafe(value);
357 CFMutableDictionaryRef SOSPeerInfoV2DictionaryCopyDictionary(SOSPeerInfoRef pi, const void *key) {
358 require_quiet(SOSPeerInfoExpandV2Data(pi, NULL), errOut);
359 CFDictionaryRef value = asDictionary(CFDictionaryGetValue(pi->v2Dictionary, key), NULL);
361 return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, CFDictionaryGetCount(value), value);
367 SOSPeerInfoRef SOSPeerInfoCopyWithV2DictionaryUpdate(CFAllocatorRef allocator, SOSPeerInfoRef toCopy, CFDictionaryRef newv2dict, SecKeyRef signingKey, CFErrorRef* error) {
368 SOSPeerInfoRef retval = SOSPeerInfoCreateCopy(kCFAllocatorDefault, toCopy, error);
369 require_quiet(retval, errOut);
370 require_action_quiet(SOSPeerInfoExpandV2Data(retval, error), errOut, CFReleaseNull(retval));
371 CFDictionaryForEach(newv2dict, ^(const void *key, const void *value) {
372 CFDictionarySetValue(retval->v2Dictionary, key, value);
374 SOSPeerInfoPackV2Data(retval);
375 require_action_quiet(SOSPeerInfoSign(signingKey, retval, error), errOut, CFReleaseNull(retval));