Guest User

Untitled

a guest
Jul 18th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2010 Stephen F. Booth <me@sbooth.org>
  3. * All Rights Reserved
  4. */
  5.  
  6. #include "CopyAppleRootCertificate.h"
  7. #import <Security/Security.h>
  8.  
  9. CFDataRef CopyAppleRootCertificate()
  10. {
  11. SecKeychainRef keychain = NULL;
  12. OSStatus status = SecKeychainOpen("/System/Library/Keychains/SystemRootCertificates.keychain", &keychain);
  13. if(noErr != status) {
  14. if(NULL != keychain)
  15. CFRelease(keychain), keychain = NULL;
  16. return NULL;
  17. }
  18.  
  19. CFArrayRef searchList = CFArrayCreate(kCFAllocatorDefault, (const void **)&keychain, 1, &kCFTypeArrayCallBacks);
  20.  
  21. CFRelease(keychain), keychain = NULL;
  22.  
  23. SecKeychainSearchRef searchRef = nil;
  24. status = SecKeychainSearchCreateFromAttributes(searchList, kSecCertificateItemClass, NULL, &searchRef);
  25. if(noErr != status) {
  26. if(NULL != searchRef)
  27. CFRelease(searchRef), searchRef = NULL;
  28. if(NULL != searchList)
  29. CFRelease(searchList), searchList = NULL;
  30. return NULL;
  31. }
  32.  
  33. SecKeychainItemRef itemRef = NULL;
  34. CFDataRef resultData = NULL;
  35.  
  36. while(noErr == SecKeychainSearchCopyNext(searchRef, &itemRef)) {
  37. // Grab the name of the certificate
  38. SecKeychainAttributeList list;
  39. SecKeychainAttribute attributes [1];
  40.  
  41. attributes[0].tag = kSecLabelItemAttr;
  42.  
  43. list.count = 1;
  44. list.attr = attributes;
  45.  
  46. SecKeychainItemCopyContent(itemRef, NULL, &list, NULL, NULL);
  47.  
  48. CFDataRef nameData = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, attributes[0].data, attributes[0].length, kCFAllocatorNull);
  49. CFStringRef name = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, nameData, kCFStringEncodingUTF8);
  50.  
  51. if(kCFCompareEqualTo == CFStringCompare(name, CFSTR("Apple Root CA"), 0)) {
  52. CSSM_DATA certData;
  53. status = SecCertificateGetData((SecCertificateRef)itemRef, &certData);
  54. if(noErr != status) {
  55. if(NULL != itemRef)
  56. CFRelease(itemRef), itemRef = NULL;
  57. }
  58.  
  59. resultData = CFDataCreate(kCFAllocatorDefault, certData.Data, certData.Length);
  60.  
  61. SecKeychainItemFreeContent(&list, NULL);
  62.  
  63. if(NULL != itemRef)
  64. CFRelease(itemRef), itemRef = NULL;
  65.  
  66. break;
  67. }
  68.  
  69. CFRelease(name), name = NULL;
  70. }
  71.  
  72. CFRelease(searchList), searchList = NULL;
  73. CFRelease(searchRef), searchRef = NULL;
  74.  
  75. return resultData;
  76. }
Add Comment
Please, Sign In to add comment