2 // accountCirclesViewsPrint.c
5 // Created by Richard Murphy on 12/8/16.
9 #include "accountCirclesViewsPrint.h"
15 // Created by Richard Murphy on 1/27/16.
24 #include <sys/utsname.h>
30 #include <Security/SecItem.h>
32 #include <CoreFoundation/CFNumber.h>
33 #include <CoreFoundation/CFString.h>
35 #include <Security/SecureObjectSync/SOSCloudCircle.h>
36 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
37 #include <Security/SecureObjectSync/SOSPeerInfo.h>
38 #include <Security/SecureObjectSync/SOSPeerInfoPriv.h>
39 #include <Security/SecureObjectSync/SOSPeerInfoV2.h>
40 #include <Security/SecureObjectSync/SOSUserKeygen.h>
41 #include <Security/SecureObjectSync/SOSKVSKeys.h>
42 #include <securityd/SOSCloudCircleServer.h>
43 #include <Security/SecOTRSession.h>
44 #include <SOSCircle/CKBridge/SOSCloudKeychainClient.h>
46 #include <utilities/SecCFWrappers.h>
47 #include <utilities/debugging.h>
49 #include <SecurityTool/readline.h>
51 #include "keychain_log.h"
52 #include "secToolFileIO.h"
53 #include "secViewDisplay.h"
56 #include <Security/SecPasswordGenerate.h>
58 /* Copied from CFPriv.h */
59 // #include <CoreFoundation/CFPriv.h>
61 CF_EXPORT CFDictionaryRef _CFCopySystemVersionDictionary(void);
62 CF_EXPORT const CFStringRef _kCFSystemVersionProductNameKey;
63 CF_EXPORT const CFStringRef _kCFSystemVersionProductVersionKey;
64 CF_EXPORT const CFStringRef _kCFSystemVersionBuildVersionKey;
66 #define MAXKVSKEYTYPE kUnknownKey
67 #define DATE_LENGTH 18
69 #include <utilities/SecCFWrappers.h>
72 static const char *getSOSCCStatusDescription(SOSCCStatus ccstatus)
76 case kSOSCCInCircle: return "In Circle";
77 case kSOSCCNotInCircle: return "Not in Circle";
78 case kSOSCCRequestPending: return "Request pending";
79 case kSOSCCCircleAbsent: return "Circle absent";
80 case kSOSCCError: return "Circle error";
83 return "<unknown ccstatus>";
88 static void printPeerInfos(char *label, CFArrayRef (^getArray)(CFErrorRef *error)) {
89 CFErrorRef error = NULL;
90 CFArrayRef ppi = getArray(&error);
91 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(NULL);
92 CFStringRef mypeerID = SOSPeerInfoGetPeerID(me);
95 printmsg(CFSTR("%s count: %ld\n"), label, (long)CFArrayGetCount(ppi));
96 CFArrayForEach(ppi, ^(const void *value) {
98 SOSPeerInfoRef peer = (SOSPeerInfoRef)value;
99 CFIndex version = SOSPeerInfoGetVersion(peer);
100 CFStringRef peerName = SOSPeerInfoGetPeerName(peer);
101 CFStringRef devtype = SOSPeerInfoGetPeerDeviceType(peer);
102 CFStringRef peerID = SOSPeerInfoGetPeerID(peer);
103 CFStringRef transportType = CFSTR("KVS");
104 CFStringRef deviceID = CFSTR("");
105 CFDictionaryRef gestalt = SOSPeerInfoCopyPeerGestalt(peer);
106 CFStringRef osVersion = CFDictionaryGetValue(gestalt, CFSTR("OSVersion"));
107 CFReleaseNull(gestalt);
111 CFDictionaryRef v2Dictionary = peer->v2Dictionary;
112 transportType = CFDictionaryGetValue(v2Dictionary, sTransportType);
113 deviceID = CFDictionaryGetValue(v2Dictionary, sDeviceID);
115 char *pname = CFStringToCString(peerName);
116 char *dname = CFStringToCString(devtype);
117 char *tname = CFStringToCString(transportType);
118 char *iname = CFStringToCString(deviceID);
119 char *osname = CFStringToCString(osVersion);
120 const char *me = CFEqualSafe(mypeerID, peerID) ? "me>" : " ";
123 snprintf(buf, 160, "%s %s: %-16s %-16s %-16s %-16s", me, label, pname, dname, tname, iname);
127 CFStringRef pid = SOSPeerInfoGetPeerID(peer);
128 CFIndex vers = SOSPeerInfoGetVersion(peer);
129 printmsg(CFSTR("%s %@ V%d OS:%s\n"), buf, pid, vers, osname);
133 printmsg(CFSTR("No %s, error: %@\n"), label, error);
136 CFReleaseNull(error);
139 void SOSCCDumpCircleInformation()
141 CFErrorRef error = NULL;
142 CFArrayRef generations = NULL;
143 bool is_accountKeyIsTrusted = false;
144 __block int count = 0;
146 SOSCCStatus ccstatus = SOSCCThisDeviceIsInCircle(&error);
147 printmsg(CFSTR("ccstatus: %s (%d)\n"), getSOSCCStatusDescription(ccstatus), ccstatus);
149 is_accountKeyIsTrusted = SOSCCValidateUserPublic(&error);
150 if(is_accountKeyIsTrusted)
151 printmsg(CFSTR("Account user public is trusted%@"),CFSTR("\n"));
153 printmsg(CFSTR("Account user public is not trusted error:(%@)\n"), error);
154 CFReleaseNull(error);
156 generations = SOSCCCopyGenerationPeerInfo(&error);
158 CFArrayForEach(generations, ^(const void *value) {
161 printmsg(CFSTR("Circle name: %@, "),value);
164 CFStringRef genDesc = SOSGenerationCountCopyDescription(value);
165 printmsg(CFSTR("Generation Count: %@"), genDesc);
166 CFReleaseNull(genDesc);
168 printmsg(CFSTR("%s\n"), "");
171 printmsg(CFSTR("No generation count: %@\n"), error);
173 CFReleaseNull(generations);
174 CFReleaseNull(error);
176 printPeerInfos(" Peers", ^(CFErrorRef *error) { return SOSCCCopyValidPeerPeerInfo(error); });
177 printPeerInfos(" Invalid", ^(CFErrorRef *error) { return SOSCCCopyNotValidPeerPeerInfo(error); });
178 printPeerInfos(" Retired", ^(CFErrorRef *error) { return SOSCCCopyRetirementPeerInfo(error); });
179 printPeerInfos(" Concur", ^(CFErrorRef *error) { return SOSCCCopyConcurringPeerPeerInfo(error); });
180 printPeerInfos("Applicants", ^(CFErrorRef *error) { return SOSCCCopyApplicantPeerInfo(error); });
182 CFReleaseNull(error);
188 /* KVS Dumping Support for iCloud Keychain */
190 static CFTypeRef getObjectsFromCloud(CFArrayRef keysToGet, dispatch_queue_t processQueue, dispatch_group_t dgroup)
192 __block CFTypeRef object = NULL;
194 const uint64_t maxTimeToWaitInSeconds = 30ull * NSEC_PER_SEC;
195 dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);
196 dispatch_time_t finishTime = dispatch_time(DISPATCH_TIME_NOW, maxTimeToWaitInSeconds);
198 dispatch_group_enter(dgroup);
200 CloudKeychainReplyBlock replyBlock =
201 ^ (CFDictionaryRef returnedValues, CFErrorRef error)
203 secinfo("sync", "SOSCloudKeychainGetObjectsFromCloud returned: %@", returnedValues);
204 object = returnedValues;
209 secerror("SOSCloudKeychainGetObjectsFromCloud returned error: %@", error);
211 dispatch_group_leave(dgroup);
212 secinfo("sync", "SOSCloudKeychainGetObjectsFromCloud block exit: %@", object);
213 dispatch_semaphore_signal(waitSemaphore);
217 SOSCloudKeychainGetAllObjectsFromCloud(processQueue, replyBlock);
219 SOSCloudKeychainGetObjectsFromCloud(keysToGet, processQueue, replyBlock);
221 dispatch_semaphore_wait(waitSemaphore, finishTime);
222 if (object && (CFGetTypeID(object) == CFNullGetTypeID())) // return a NULL instead of a CFNull
227 secerror("returned: %@", object);
231 static CFStringRef printFullDataString(CFDataRef data){
232 __block CFStringRef fullData = NULL;
234 BufferPerformWithHexString(CFDataGetBytePtr(data), CFDataGetLength(data), ^(CFStringRef dataHex) {
235 fullData = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@"), dataHex);
241 static void displayLastKeyParameters(CFTypeRef key, CFTypeRef value)
243 CFDataRef valueAsData = asData(value, NULL);
245 CFDataRef dateData = CFDataCreateCopyFromRange(kCFAllocatorDefault, valueAsData, CFRangeMake(0, DATE_LENGTH));
246 CFDataRef keyParameterData = CFDataCreateCopyFromPositions(kCFAllocatorDefault, valueAsData, DATE_LENGTH, CFDataGetLength(valueAsData));
247 CFStringRef dateString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, dateData, kCFStringEncodingUTF8);
248 CFStringRef keyParameterDescription = UserParametersDescription(keyParameterData);
249 if(keyParameterDescription)
250 printmsg(CFSTR("%@: %@: %@\n"), key, dateString, keyParameterDescription);
252 printmsg(CFSTR("%@: %@\n"), key, printFullDataString(value));
253 CFReleaseNull(dateString);
254 CFReleaseNull(keyParameterData);
255 CFReleaseNull(dateData);
256 CFReleaseNull(keyParameterDescription);
259 printmsg(CFSTR("%@: %@\n"), key, value);
263 static void displayKeyParameters(CFTypeRef key, CFTypeRef value)
266 CFStringRef keyParameterDescription = UserParametersDescription((CFDataRef)value);
268 if(keyParameterDescription)
269 printmsg(CFSTR("%@: %@\n"), key, keyParameterDescription);
271 printmsg(CFSTR("%@: %@\n"), key, value);
273 CFReleaseNull(keyParameterDescription);
276 printmsg(CFSTR("%@: %@\n"), key, value);
280 static void displayLastCircle(CFTypeRef key, CFTypeRef value)
282 CFDataRef valueAsData = asData(value, NULL);
284 CFErrorRef localError = NULL;
286 CFDataRef dateData = CFDataCreateCopyFromRange(kCFAllocatorDefault, valueAsData, CFRangeMake(0, DATE_LENGTH));
287 CFDataRef circleData = CFDataCreateCopyFromPositions(kCFAllocatorDefault, valueAsData, DATE_LENGTH, CFDataGetLength(valueAsData));
288 CFStringRef dateString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, dateData, kCFStringEncodingUTF8);
289 SOSCircleRef circle = SOSCircleCreateFromData(NULL, (CFDataRef) circleData, &localError);
293 CFNumberRef idLength = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &size);
294 CFDictionaryRef format = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("SyncD"), CFSTR("SyncD"), CFSTR("idLength"), idLength, NULL);
295 printmsgWithFormatOptions(format, CFSTR("%@: %@: %@\n"), key, dateString, circle);
296 CFReleaseNull(idLength);
297 CFReleaseNull(format);
301 printmsg(CFSTR("%@: %@\n"), key, printFullDataString(circleData));
303 CFReleaseNull(dateString);
304 CFReleaseNull(circleData);
305 CFReleaseSafe(circle);
306 CFReleaseNull(dateData);
307 CFReleaseNull(localError);
310 printmsg(CFSTR("%@: %@\n"), key, value);
314 static void displayCircle(CFTypeRef key, CFTypeRef value)
316 CFDataRef circleData = (CFDataRef)value;
318 CFErrorRef localError = NULL;
319 if (isData(circleData))
322 CFNumberRef idLength = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &size);
323 CFDictionaryRef format = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("SyncD"), CFSTR("SyncD"), CFSTR("idLength"), idLength, NULL);
324 SOSCircleRef circle = SOSCircleCreateFromData(NULL, circleData, &localError);
325 printmsgWithFormatOptions(format, CFSTR("%@: %@\n"), key, circle);
326 CFReleaseSafe(circle);
327 CFReleaseNull(idLength);
328 CFReleaseNull(format);
332 printmsg(CFSTR("%@: %@\n"), key, value);
335 static void displayMessage(CFTypeRef key, CFTypeRef value)
337 CFDataRef message = (CFDataRef)value;
339 const char* messageType = SecOTRPacketTypeString(message);
340 printmsg(CFSTR("%@: %s: %ld\n"), key, messageType, CFDataGetLength(message));
343 printmsg(CFSTR("%@: %@\n"), key, value);
346 static void printEverything(CFTypeRef objects)
348 CFDictionaryForEach(objects, ^(const void *key, const void *value) {
351 printmsg(CFSTR("%@: %@\n\n"), key, printFullDataString(value));
354 printmsg(CFSTR("%@: %@\n"), key, value);
359 static void decodeForKeyType(CFTypeRef key, CFTypeRef value, SOSKVSKeyType type){
362 displayCircle(key, value);
366 displayMessage(key, value);
369 displayKeyParameters(key, value);
371 case kLastKeyParameterKey:
372 displayLastKeyParameters(key, value);
375 displayLastCircle(key, value);
377 case kInitialSyncKey:
378 case kAccountChangedKey:
382 printmsg(CFSTR("%@: %@\n"), key, value);
387 static void decodeAllTheValues(CFTypeRef objects){
388 SOSKVSKeyType keyType = 0;
389 __block bool didPrint = false;
391 for (keyType = 0; keyType <= MAXKVSKEYTYPE; keyType++){
392 CFDictionaryForEach(objects, ^(const void *key, const void *value) {
393 if(SOSKVSKeyGetKeyType(key) == keyType){
394 decodeForKeyType(key, value, keyType);
399 printmsg(CFSTR("%@\n"), CFSTR(""));
404 bool SOSCCDumpCircleKVSInformation(char *itemName) {
405 CFArrayRef keysToGet = NULL;
408 CFStringRef itemStr = CFStringCreateWithCString(kCFAllocatorDefault, itemName, kCFStringEncodingUTF8);
409 fprintf(outFile, "Retrieving %s from KVS\n", itemName);
410 keysToGet = CFArrayCreateForCFTypes(kCFAllocatorDefault, itemStr, NULL);
411 CFReleaseSafe(itemStr);
413 dispatch_queue_t generalq = dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL);
414 dispatch_group_t work_group = dispatch_group_create();
415 CFTypeRef objects = getObjectsFromCloud(keysToGet, generalq, work_group);
416 CFReleaseSafe(keysToGet);
419 fprintf(outFile, "All keys and values straight from KVS\n");
420 printEverything(objects);
421 fprintf(outFile, "\nAll values in decoded form...\n");
422 decodeAllTheValues(objects);
424 fprintf(outFile, "\n");