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>";
89 getSOSCCLastDepartureReasonDescription(enum DepartureReason reason)
92 #define CASE_REASON(x) case kSOS##x: return #x
93 CASE_REASON(DepartureReasonError);
94 CASE_REASON(NeverLeftCircle);
95 CASE_REASON(WithdrewMembership);
96 CASE_REASON(MembershipRevoked);
97 CASE_REASON(LeftUntrustedCircle);
98 CASE_REASON(NeverAppliedToCircle);
99 CASE_REASON(DiscoveredRetirement); // we should all be so lucky
100 CASE_REASON(LostPrivateKey);
101 CASE_REASON(PasswordChanged);
108 static void printPeerInfos(char *label, CFArrayRef (^getArray)(CFErrorRef *error)) {
109 CFErrorRef error = NULL;
110 CFArrayRef ppi = getArray(&error);
111 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(NULL);
112 CFStringRef mypeerID = SOSPeerInfoGetPeerID(me);
115 printmsg(CFSTR("%s count: %ld\n"), label, (long)CFArrayGetCount(ppi));
116 CFArrayForEach(ppi, ^(const void *value) {
118 SOSPeerInfoRef peer = (SOSPeerInfoRef)value;
119 CFIndex version = SOSPeerInfoGetVersion(peer);
120 CFStringRef peerName = SOSPeerInfoGetPeerName(peer);
121 CFStringRef devtype = SOSPeerInfoGetPeerDeviceType(peer);
122 CFStringRef peerID = SOSPeerInfoGetPeerID(peer);
123 CFStringRef transportType = CFSTR("KVS");
124 CFStringRef deviceID = CFSTR("");
125 CFDictionaryRef gestalt = SOSPeerInfoCopyPeerGestalt(peer);
126 CFStringRef osVersion = CFDictionaryGetValue(gestalt, CFSTR("OSVersion"));
130 CFDictionaryRef v2Dictionary = peer->v2Dictionary;
131 transportType = CFDictionaryGetValue(v2Dictionary, sTransportType);
132 deviceID = CFDictionaryGetValue(v2Dictionary, sDeviceID);
134 char *pname = CFStringToCString(peerName);
135 char *dname = CFStringToCString(devtype);
136 char *tname = CFStringToCString(transportType);
137 char *iname = CFStringToCString(deviceID);
138 const char *me = CFEqualSafe(mypeerID, peerID) ? "me>" : " ";
141 snprintf(buf, 160, "%s %s: %-16s %-16s %-16s %-36s", me, label, pname, dname, tname, iname);
148 // %s in (Core)Foundation format strings treats the string as MacRoman, need to do this to guarantee UTF8 handling
149 CFStringRef bufstr = CFStringCreateWithCString(NULL, buf, kCFStringEncodingUTF8);
150 CFStringRef pid = SOSPeerInfoGetPeerID(peer);
151 CFIndex vers = SOSPeerInfoGetVersion(peer);
152 printmsg(CFSTR("%@ %@ V%d OS:%@\n"), bufstr, pid, vers, osVersion ?: CFSTR(""));
155 CFReleaseNull(gestalt);
158 printmsg(CFSTR("No %s, error: %@\n"), label, error);
161 CFReleaseNull(error);
165 void SOSCCDumpCircleInformation()
167 CFErrorRef error = NULL;
168 CFArrayRef generations = NULL;
169 bool is_accountKeyIsTrusted = false;
170 __block int count = 0;
172 SOSCCStatus ccstatus = SOSCCThisDeviceIsInCircle(&error);
173 printmsg(CFSTR("ccstatus: %s (%d)\n"), getSOSCCStatusDescription(ccstatus), ccstatus);
175 printmsg(CFSTR("Error checking circle status: %@\n"), error);
177 CFReleaseNull(error);
179 enum DepartureReason departureReason = SOSCCGetLastDepartureReason(&error);
180 printmsg(CFSTR("LastDepartureReason: %s (%d)\n"), getSOSCCLastDepartureReasonDescription(departureReason), departureReason);
182 printmsg(CFSTR("Error checking last departure reason error: %@\n"), error);
184 CFReleaseNull(error);
186 is_accountKeyIsTrusted = SOSCCValidateUserPublic(&error);
187 if(is_accountKeyIsTrusted)
188 printmsg(CFSTR("Account user public is trusted\n"));
190 printmsg(CFSTR("Account user public is not trusted error:(%@)\n"), error);
191 CFReleaseNull(error);
193 generations = SOSCCCopyGenerationPeerInfo(&error);
195 CFArrayForEach(generations, ^(const void *value) {
198 printmsg(CFSTR("Circle name: %@, "),value);
201 CFStringRef genDesc = SOSGenerationCountCopyDescription(value);
202 printmsg(CFSTR("Generation Count: %@"), genDesc);
203 CFReleaseNull(genDesc);
205 printmsg(CFSTR("%s\n"), "");
208 printmsg(CFSTR("No generation count: %@\n"), error);
210 CFReleaseNull(generations);
211 CFReleaseNull(error);
213 printPeerInfos(" Peers", ^(CFErrorRef *error) { return SOSCCCopyValidPeerPeerInfo(error); });
214 printPeerInfos(" Invalid", ^(CFErrorRef *error) { return SOSCCCopyNotValidPeerPeerInfo(error); });
215 printPeerInfos(" Retired", ^(CFErrorRef *error) { return SOSCCCopyRetirementPeerInfo(error); });
216 printPeerInfos(" Concur", ^(CFErrorRef *error) { return SOSCCCopyConcurringPeerPeerInfo(error); });
217 printPeerInfos("Applicants", ^(CFErrorRef *error) { return SOSCCCopyApplicantPeerInfo(error); });
219 CFReleaseNull(error);
223 SOSCCDumpEngineInformation(void)
225 CFErrorRef error = NULL;
227 printmsg(CFSTR("Engine state:\n"));
228 if (!SOSCCForEachEngineStateAsString(&error, ^(CFStringRef oneStateString) {
229 printmsg(CFSTR("%@\n"), oneStateString);
231 printmsg(CFSTR("No engine state, got error: %@\n"), error);
237 SOSCCDumpViewUnwarePeers(void)
239 printPeerInfos("view-unaware", ^(CFErrorRef *error) { return SOSCCCopyViewUnawarePeerInfo(error); });
242 /* KVS Dumping Support for iCloud Keychain */
244 static CFTypeRef getObjectsFromCloud(CFArrayRef keysToGet, dispatch_queue_t processQueue, dispatch_group_t dgroup)
246 __block CFTypeRef object = NULL;
248 const uint64_t maxTimeToWaitInSeconds = 30ull * NSEC_PER_SEC;
249 dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);
250 dispatch_time_t finishTime = dispatch_time(DISPATCH_TIME_NOW, maxTimeToWaitInSeconds);
252 dispatch_group_enter(dgroup);
254 CloudKeychainReplyBlock replyBlock =
255 ^ (CFDictionaryRef returnedValues, CFErrorRef error)
257 secinfo("sync", "SOSCloudKeychainGetObjectsFromCloud returned: %@", returnedValues);
258 object = returnedValues;
263 secerror("SOSCloudKeychainGetObjectsFromCloud returned error: %@", error);
265 dispatch_group_leave(dgroup);
266 secinfo("sync", "SOSCloudKeychainGetObjectsFromCloud block exit: %@", object);
267 dispatch_semaphore_signal(waitSemaphore);
271 SOSCloudKeychainGetAllObjectsFromCloud(processQueue, replyBlock);
273 SOSCloudKeychainGetObjectsFromCloud(keysToGet, processQueue, replyBlock);
275 dispatch_semaphore_wait(waitSemaphore, finishTime);
276 if (object && (CFGetTypeID(object) == CFNullGetTypeID())) // return a NULL instead of a CFNull
281 secerror("returned: %@", object);
285 static CFStringRef printFullDataString(CFDataRef data){
286 __block CFStringRef fullData = NULL;
288 BufferPerformWithHexString(CFDataGetBytePtr(data), CFDataGetLength(data), ^(CFStringRef dataHex) {
289 fullData = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@"), dataHex);
295 static void displayLastKeyParameters(CFTypeRef key, CFTypeRef value)
297 CFDataRef valueAsData = asData(value, NULL);
299 CFDataRef dateData = CFDataCreateCopyFromRange(kCFAllocatorDefault, valueAsData, CFRangeMake(0, DATE_LENGTH));
300 CFDataRef keyParameterData = CFDataCreateCopyFromPositions(kCFAllocatorDefault, valueAsData, DATE_LENGTH, CFDataGetLength(valueAsData));
301 CFStringRef dateString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, dateData, kCFStringEncodingUTF8);
302 CFStringRef keyParameterDescription = UserParametersDescription(keyParameterData);
303 if(keyParameterDescription)
304 printmsg(CFSTR("%@: %@: %@\n"), key, dateString, keyParameterDescription);
306 printmsg(CFSTR("%@: %@\n"), key, printFullDataString(value));
307 CFReleaseNull(dateString);
308 CFReleaseNull(keyParameterData);
309 CFReleaseNull(dateData);
310 CFReleaseNull(keyParameterDescription);
313 printmsg(CFSTR("%@: %@\n"), key, value);
317 static void displayKeyParameters(CFTypeRef key, CFTypeRef value)
320 CFStringRef keyParameterDescription = UserParametersDescription((CFDataRef)value);
322 if(keyParameterDescription)
323 printmsg(CFSTR("%@: %@\n"), key, keyParameterDescription);
325 printmsg(CFSTR("%@: %@\n"), key, value);
327 CFReleaseNull(keyParameterDescription);
330 printmsg(CFSTR("%@: %@\n"), key, value);
334 static void displayLastCircle(CFTypeRef key, CFTypeRef value)
336 CFDataRef valueAsData = asData(value, NULL);
338 CFErrorRef localError = NULL;
340 CFDataRef dateData = CFDataCreateCopyFromRange(kCFAllocatorDefault, valueAsData, CFRangeMake(0, DATE_LENGTH));
341 CFDataRef circleData = CFDataCreateCopyFromPositions(kCFAllocatorDefault, valueAsData, DATE_LENGTH, CFDataGetLength(valueAsData));
342 CFStringRef dateString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, dateData, kCFStringEncodingUTF8);
343 SOSCircleRef circle = SOSCircleCreateFromData(NULL, (CFDataRef) circleData, &localError);
347 CFNumberRef idLength = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &size);
348 CFDictionaryRef format = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("SyncD"), CFSTR("SyncD"), CFSTR("idLength"), idLength, NULL);
349 printmsgWithFormatOptions(format, CFSTR("%@: %@: %@\n"), key, dateString, circle);
350 CFReleaseNull(idLength);
351 CFReleaseNull(format);
355 printmsg(CFSTR("%@: %@\n"), key, printFullDataString(circleData));
357 CFReleaseNull(dateString);
358 CFReleaseNull(circleData);
359 CFReleaseSafe(circle);
360 CFReleaseNull(dateData);
361 CFReleaseNull(localError);
364 printmsg(CFSTR("%@: %@\n"), key, value);
368 static void displayCircle(CFTypeRef key, CFTypeRef value)
370 CFDataRef circleData = (CFDataRef)value;
372 CFErrorRef localError = NULL;
373 if (isData(circleData))
376 CFNumberRef idLength = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &size);
377 CFDictionaryRef format = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("SyncD"), CFSTR("SyncD"), CFSTR("idLength"), idLength, NULL);
378 SOSCircleRef circle = SOSCircleCreateFromData(NULL, circleData, &localError);
379 printmsgWithFormatOptions(format, CFSTR("%@: %@\n"), key, circle);
380 CFReleaseSafe(circle);
381 CFReleaseNull(idLength);
382 CFReleaseNull(format);
386 printmsg(CFSTR("%@: %@\n"), key, value);
389 static void displayMessage(CFTypeRef key, CFTypeRef value)
391 CFDataRef message = (CFDataRef)value;
393 const char* messageType = SecOTRPacketTypeString(message);
394 printmsg(CFSTR("%@: %s: %ld\n"), key, messageType, CFDataGetLength(message));
397 printmsg(CFSTR("%@: %@\n"), key, value);
400 static void printEverything(CFTypeRef objects)
402 CFDictionaryForEach(objects, ^(const void *key, const void *value) {
405 printmsg(CFSTR("%@: %@\n\n"), key, printFullDataString(value));
408 printmsg(CFSTR("%@: %@\n"), key, value);
413 static void decodeForKeyType(CFTypeRef key, CFTypeRef value, SOSKVSKeyType type){
416 displayCircle(key, value);
420 displayMessage(key, value);
423 displayKeyParameters(key, value);
425 case kLastKeyParameterKey:
426 displayLastKeyParameters(key, value);
429 displayLastCircle(key, value);
431 case kInitialSyncKey:
432 case kAccountChangedKey:
436 printmsg(CFSTR("%@: %@\n"), key, value);
441 static void decodeAllTheValues(CFTypeRef objects){
442 SOSKVSKeyType keyType = 0;
443 __block bool didPrint = false;
445 for (keyType = 0; keyType <= MAXKVSKEYTYPE; keyType++){
446 CFDictionaryForEach(objects, ^(const void *key, const void *value) {
447 if(SOSKVSKeyGetKeyType(key) == keyType){
448 decodeForKeyType(key, value, keyType);
453 printmsg(CFSTR("%@\n"), CFSTR(""));
458 bool SOSCCDumpCircleKVSInformation(char *itemName) {
459 CFArrayRef keysToGet = NULL;
462 CFStringRef itemStr = CFStringCreateWithCString(kCFAllocatorDefault, itemName, kCFStringEncodingUTF8);
463 fprintf(outFile, "Retrieving %s from KVS\n", itemName);
464 keysToGet = CFArrayCreateForCFTypes(kCFAllocatorDefault, itemStr, NULL);
465 CFReleaseSafe(itemStr);
467 dispatch_queue_t generalq = dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL);
468 dispatch_group_t work_group = dispatch_group_create();
469 CFTypeRef objects = getObjectsFromCloud(keysToGet, generalq, work_group);
470 CFReleaseSafe(keysToGet);
473 fprintf(outFile, "All keys and values straight from KVS\n");
474 printEverything(objects);
475 fprintf(outFile, "\nAll values in decoded form...\n");
476 decodeAllTheValues(objects);
478 fprintf(outFile, "\n");