SystemCertificates.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_OSX
  3. #include "os/SystemCertificates.h"
  4. #include <Security/SecTrust.h>
  5. #include <Security/SecCertificate.h>
  6. #include <Security/SecImportExport.h>
  7. namespace il2cpp
  8. {
  9. namespace os
  10. {
  11. void* SystemCertificates::OpenSystemRootStore()
  12. {
  13. CFArrayRef anchors = NULL;
  14. OSStatus s;
  15. s = SecTrustCopyAnchorCertificates(&anchors);
  16. IL2CPP_ASSERT(s == noErr);
  17. return (void*)anchors;
  18. }
  19. int SystemCertificates::EnumSystemCertificates(void* certStore, void** iter, int *format, int* size, void** data)
  20. {
  21. OSStatus s;
  22. CFDataRef certData;
  23. int numCerts = (int)CFArrayGetCount((CFArrayRef)certStore);
  24. *format = DATATYPE_STRING;
  25. // Order matters when it comes to certificates need to read in reverse
  26. int currentCert = numCerts;
  27. if (*iter != NULL)
  28. {
  29. currentCert = static_cast<int>(reinterpret_cast<intptr_t>(*iter));
  30. }
  31. SecCertificateRef cert = (SecCertificateRef)CFArrayGetValueAtIndex((CFArrayRef)certStore, (currentCert - 1));
  32. s = SecItemExport(cert, kSecFormatPEMSequence, kSecItemPemArmour, NULL, &certData);
  33. if (s == errSecSuccess)
  34. {
  35. char* certPEMStr = (char*)CFDataGetBytePtr(certData);
  36. *iter = reinterpret_cast<void*>(static_cast<intptr_t>((currentCert - 1)));
  37. *data = certPEMStr;
  38. *size = sizeof(certPEMStr);
  39. if ((currentCert - 1) > 0)
  40. {
  41. return TRUE;
  42. }
  43. }
  44. return FALSE;
  45. }
  46. void SystemCertificates::CloseSystemRootStore(void* cStore)
  47. {
  48. CFRelease((CFArrayRef)cStore);
  49. }
  50. }
  51. }
  52. #endif