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/CoreFoundation.h>
33 #include <CoreFoundation/CFPriv.h>
35 #include <Security/SecureObjectSync/SOSCloudCircle.h>
36 #include <Security/SecureObjectSync/SOSCloudCircleInternal.h>
37 #include <Security/SecureObjectSync/SOSPeerInfo.h>
38 #include "keychain/SecureObjectSync/SOSPeerInfoPriv.h"
39 #include "keychain/SecureObjectSync/SOSPeerInfoV2.h"
40 #include "keychain/SecureObjectSync/SOSUserKeygen.h"
41 #include "keychain/SecureObjectSync/SOSKVSKeys.h"
42 #include <securityd/SOSCloudCircleServer.h>
43 #include <Security/SecOTRSession.h>
44 #include "keychain/SecureObjectSync/CKBridge/SOSCloudKeychainClient.h"
46 #include <utilities/SecCFWrappers.h>
47 #include <utilities/debugging.h>
49 #include "SecurityTool/sharedTool/readline.h"
51 #include "keychain_log.h"
52 #include "secToolFileIO.h"
53 #include "secViewDisplay.h"
56 #include <Security/SecPasswordGenerate.h>
58 #define MAXKVSKEYTYPE kUnknownKey
59 #define DATE_LENGTH 18
61 #include <utilities/SecCFWrappers.h>
64 static const char *getSOSCCStatusDescription(SOSCCStatus ccstatus)
68 case kSOSCCInCircle: return "In Circle";
69 case kSOSCCNotInCircle: return "Not in Circle";
70 case kSOSCCRequestPending: return "Request pending";
71 case kSOSCCCircleAbsent: return "Circle absent";
72 case kSOSCCError: return "Circle error";
75 return "<unknown ccstatus>";
81 getSOSCCLastDepartureReasonDescription(enum DepartureReason reason)
84 #define CASE_REASON(x) case kSOS##x: return #x
85 CASE_REASON(DepartureReasonError);
86 CASE_REASON(NeverLeftCircle);
87 CASE_REASON(WithdrewMembership);
88 CASE_REASON(MembershipRevoked);
89 CASE_REASON(LeftUntrustedCircle);
90 CASE_REASON(NeverAppliedToCircle);
91 CASE_REASON(DiscoveredRetirement); // we should all be so lucky
92 CASE_REASON(LostPrivateKey);
93 CASE_REASON(PasswordChanged);
100 static void printPeerInfos(char *label, CFStringRef mypeerID, CFArrayRef (^copyPeers)(CFErrorRef *error)) {
101 CFErrorRef error = NULL;
102 CFArrayRef ppi = copyPeers(&error);
105 printmsg(CFSTR("%s count: %ld\n"), label, (long)CFArrayGetCount(ppi));
106 CFArrayForEach(ppi, ^(const void *value) {
108 SOSPeerInfoRef peer = (SOSPeerInfoRef)value;
109 if(!peer) { return; }
110 CFStringRef peerName = SOSPeerInfoGetPeerName(peer);
111 CFStringRef devtype = SOSPeerInfoGetPeerDeviceType(peer);
112 CFStringRef peerID = SOSPeerInfoGetPeerID(peer);
113 CFStringRef transportType = CFSTR("KVS");
114 CFStringRef deviceID = CFSTR("");
115 CFStringRef machineID = CFSTR("");
116 CFDictionaryRef gestalt = SOSPeerInfoCopyPeerGestalt(peer);
117 CFStringRef osVersion = NULL;
119 osVersion = CFDictionaryGetValue(gestalt, CFSTR("OSVersion"));
121 osVersion = CFSTR("Unknown");
124 if(SOSPeerInfoVersionHasV2Data(peer)){
125 CFDictionaryRef v2Dictionary = peer->v2Dictionary;
127 transportType = CFDictionaryGetValue(v2Dictionary, CFSTR("TransportType"));
128 deviceID = CFDictionaryGetValue(v2Dictionary, CFSTR("DeviceID"));
129 machineID = CFDictionaryGetValue(v2Dictionary, CFSTR("MachineIDKey"));
132 char *pname = CFStringToCString(peerName);
133 char *dname = CFStringToCString(devtype);
134 char *tname = CFStringToCString(transportType);
135 char *iname = CFStringToCString(deviceID);
136 char *mname = CFStringToCString(machineID);
137 const char *me = CFEqualSafe(mypeerID, peerID) ? "me>" : " ";
140 snprintf(buf, 160, "%s %s: %-16s dev:%-16s trn:%-16s devid:%-36s mid: %-36s", me, label, pname, dname, tname, iname, mname);
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("%@ pid:%@ V%d OS:%@\n"), bufstr, pid, vers, osVersion ?: CFSTR(""));
155 CFReleaseNull(gestalt);
158 printmsg(CFSTR("No %s, error: %@\n"), label, error);
161 CFReleaseNull(error);
164 void SOSCCDumpCircleInformation()
166 CFErrorRef error = NULL;
167 CFArrayRef generations = NULL;
168 bool is_accountKeyIsTrusted = false;
169 __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 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(NULL);
214 CFStringRef mypeerID = SOSPeerInfoGetPeerID(me);
216 printPeerInfos(" Peers", mypeerID, ^(CFErrorRef *error) { return SOSCCCopyValidPeerPeerInfo(error); });
217 printPeerInfos(" Invalid", mypeerID, ^(CFErrorRef *error) { return SOSCCCopyNotValidPeerPeerInfo(error); });
218 printPeerInfos(" Retired", mypeerID, ^(CFErrorRef *error) { return SOSCCCopyRetirementPeerInfo(error); });
219 printPeerInfos(" Concur", mypeerID, ^(CFErrorRef *error) { return SOSCCCopyConcurringPeerPeerInfo(error); });
220 printPeerInfos("Applicants", mypeerID, ^(CFErrorRef *error) { return SOSCCCopyApplicantPeerInfo(error); });
223 CFReleaseNull(error);
227 SOSCCDumpEngineInformation(void)
229 CFErrorRef error = NULL;
231 printmsg(CFSTR("Engine state:\n"));
232 if (!SOSCCForEachEngineStateAsString(&error, ^(CFStringRef oneStateString) {
233 printmsg(CFSTR("%@\n"), oneStateString);
235 printmsg(CFSTR("No engine state, got error: %@\n"), error);
241 SOSCCDumpViewUnwarePeers(void)
243 SOSPeerInfoRef me = SOSCCCopyMyPeerInfo(NULL);
244 CFStringRef mypeerID = SOSPeerInfoGetPeerID(me);
246 printPeerInfos("view-unaware", mypeerID, ^(CFErrorRef *error) { return SOSCCCopyViewUnawarePeerInfo(error); });
251 /* KVS Dumping Support for iCloud Keychain */
253 static CFTypeRef getObjectsFromCloud(CFArrayRef keysToGet, dispatch_queue_t processQueue, dispatch_group_t dgroup)
255 __block CFTypeRef object = NULL;
257 const uint64_t maxTimeToWaitInSeconds = 30ull * NSEC_PER_SEC;
258 dispatch_semaphore_t waitSemaphore = dispatch_semaphore_create(0);
259 dispatch_time_t finishTime = dispatch_time(DISPATCH_TIME_NOW, maxTimeToWaitInSeconds);
261 dispatch_group_enter(dgroup);
263 CloudKeychainReplyBlock replyBlock =
264 ^ (CFDictionaryRef returnedValues, CFErrorRef error)
266 secinfo("sync", "SOSCloudKeychainGetObjectsFromCloud returned: %@", returnedValues);
267 object = returnedValues;
272 secerror("SOSCloudKeychainGetObjectsFromCloud returned error: %@", error);
274 dispatch_group_leave(dgroup);
275 secinfo("sync", "SOSCloudKeychainGetObjectsFromCloud block exit: %@", object);
276 dispatch_semaphore_signal(waitSemaphore);
280 SOSCloudKeychainGetAllObjectsFromCloud(processQueue, replyBlock);
282 SOSCloudKeychainGetObjectsFromCloud(keysToGet, processQueue, replyBlock);
284 dispatch_semaphore_wait(waitSemaphore, finishTime);
285 if (object && (CFGetTypeID(object) == CFNullGetTypeID())) // return a NULL instead of a CFNull
290 secerror("returned: %@", object);
294 static CFStringRef printFullDataString(CFDataRef data){
295 __block CFStringRef fullData = NULL;
297 BufferPerformWithHexString(CFDataGetBytePtr(data), CFDataGetLength(data), ^(CFStringRef dataHex) {
298 fullData = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@"), dataHex);
304 static void displayLastKeyParameters(CFTypeRef key, CFTypeRef value)
306 CFDataRef valueAsData = asData(value, NULL);
308 CFDataRef dateData = CFDataCreateCopyFromRange(kCFAllocatorDefault, valueAsData, CFRangeMake(0, DATE_LENGTH));
309 CFDataRef keyParameterData = CFDataCreateCopyFromPositions(kCFAllocatorDefault, valueAsData, DATE_LENGTH, CFDataGetLength(valueAsData));
310 CFStringRef dateString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, dateData, kCFStringEncodingUTF8);
311 CFStringRef keyParameterDescription = UserParametersDescription(keyParameterData);
312 if(keyParameterDescription)
313 printmsg(CFSTR("%@: %@: %@\n"), key, dateString, keyParameterDescription);
315 printmsg(CFSTR("%@: %@\n"), key, printFullDataString(value));
316 CFReleaseNull(dateString);
317 CFReleaseNull(keyParameterData);
318 CFReleaseNull(dateData);
319 CFReleaseNull(keyParameterDescription);
322 printmsg(CFSTR("%@: %@\n"), key, value);
326 static void displayKeyParameters(CFTypeRef key, CFTypeRef value)
329 CFStringRef keyParameterDescription = UserParametersDescription((CFDataRef)value);
331 if(keyParameterDescription)
332 printmsg(CFSTR("%@: %@\n"), key, keyParameterDescription);
334 printmsg(CFSTR("%@: %@\n"), key, value);
336 CFReleaseNull(keyParameterDescription);
339 printmsg(CFSTR("%@: %@\n"), key, value);
343 static void displayLastCircle(CFTypeRef key, CFTypeRef value)
345 CFDataRef valueAsData = asData(value, NULL);
347 CFErrorRef localError = NULL;
349 CFDataRef dateData = CFDataCreateCopyFromRange(kCFAllocatorDefault, valueAsData, CFRangeMake(0, DATE_LENGTH));
350 CFDataRef circleData = CFDataCreateCopyFromPositions(kCFAllocatorDefault, valueAsData, DATE_LENGTH, CFDataGetLength(valueAsData));
351 CFStringRef dateString = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, dateData, kCFStringEncodingUTF8);
352 SOSCircleRef circle = SOSCircleCreateFromData(NULL, (CFDataRef) circleData, &localError);
356 CFNumberRef idLength = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &size);
357 CFDictionaryRef format = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("SyncD"), CFSTR("SyncD"), CFSTR("idLength"), idLength, NULL);
358 printmsgWithFormatOptions(format, CFSTR("%@: %@: %@\n"), key, dateString, circle);
359 CFReleaseNull(idLength);
360 CFReleaseNull(format);
364 printmsg(CFSTR("%@: %@\n"), key, printFullDataString(circleData));
366 CFReleaseNull(dateString);
367 CFReleaseNull(circleData);
368 CFReleaseSafe(circle);
369 CFReleaseNull(dateData);
370 CFReleaseNull(localError);
373 printmsg(CFSTR("%@: %@\n"), key, value);
377 static void displayCircle(CFTypeRef key, CFTypeRef value)
379 CFDataRef circleData = (CFDataRef)value;
381 CFErrorRef localError = NULL;
382 if (isData(circleData))
385 CFNumberRef idLength = CFNumberCreate(kCFAllocatorDefault, kCFNumberCFIndexType, &size);
386 CFDictionaryRef format = CFDictionaryCreateForCFTypes(kCFAllocatorDefault, CFSTR("SyncD"), CFSTR("SyncD"), CFSTR("idLength"), idLength, NULL);
387 SOSCircleRef circle = SOSCircleCreateFromData(NULL, circleData, &localError);
388 printmsgWithFormatOptions(format, CFSTR("%@: %@\n"), key, circle);
389 CFReleaseSafe(circle);
390 CFReleaseNull(idLength);
391 CFReleaseNull(format);
395 printmsg(CFSTR("%@: %@\n"), key, value);
398 static void displayMessage(CFTypeRef key, CFTypeRef value)
400 CFDataRef message = (CFDataRef)value;
402 const char* messageType = SecOTRPacketTypeString(message);
403 printmsg(CFSTR("%@: %s: %ld\n"), key, messageType, CFDataGetLength(message));
406 printmsg(CFSTR("%@: %@\n"), key, value);
409 static void printEverything(CFTypeRef objects)
411 CFDictionaryForEach(objects, ^(const void *key, const void *value) {
414 printmsg(CFSTR("%@: %@\n\n"), key, printFullDataString(value));
417 printmsg(CFSTR("%@: %@\n"), key, value);
422 static void decodeForKeyType(CFTypeRef key, CFTypeRef value, SOSKVSKeyType type){
425 displayCircle(key, value);
429 displayMessage(key, value);
432 displayKeyParameters(key, value);
434 case kLastKeyParameterKey:
435 displayLastKeyParameters(key, value);
438 displayLastCircle(key, value);
440 case kInitialSyncKey:
441 case kAccountChangedKey:
445 printmsg(CFSTR("%@: %@\n"), key, value);
450 static void decodeAllTheValues(CFTypeRef objects){
451 SOSKVSKeyType keyType = 0;
452 __block bool didPrint = false;
454 for (keyType = 0; keyType <= MAXKVSKEYTYPE; keyType++){
455 CFDictionaryForEach(objects, ^(const void *key, const void *value) {
456 if(SOSKVSKeyGetKeyType(key) == keyType){
457 decodeForKeyType(key, value, keyType);
462 printmsg(CFSTR("%@\n"), CFSTR(""));
467 bool SOSCCDumpCircleKVSInformation(char *itemName) {
468 CFArrayRef keysToGet = NULL;
471 CFStringRef itemStr = CFStringCreateWithCString(kCFAllocatorDefault, itemName, kCFStringEncodingUTF8);
472 fprintf(outFile, "Retrieving %s from KVS\n", itemName);
473 keysToGet = CFArrayCreateForCFTypes(kCFAllocatorDefault, itemStr, NULL);
474 CFReleaseSafe(itemStr);
476 dispatch_queue_t generalq = dispatch_queue_create("general", DISPATCH_QUEUE_SERIAL);
477 dispatch_group_t work_group = dispatch_group_create();
478 CFTypeRef objects = getObjectsFromCloud(keysToGet, generalq, work_group);
479 CFReleaseSafe(keysToGet);
482 fprintf(outFile, "All keys and values straight from KVS\n");
483 printEverything(objects);
484 fprintf(outFile, "\nAll values in decoded form...\n");
485 decodeAllTheValues(objects);
487 fprintf(outFile, "\n");