reporting_utils.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. reporting_utils.m
  3. TrustKit
  4. Copyright 2015 The TrustKit Project Authors
  5. Licensed under the MIT license, see associated LICENSE file for terms.
  6. See AUTHORS file for the list of project authors.
  7. */
  8. #import "reporting_utils.h"
  9. NSArray<NSString *> *convertTrustToPemArray(SecTrustRef serverTrust)
  10. {
  11. // Convert the trust object into an array of PEM certificates
  12. // Warning: SecTrustEvaluate() always needs to be called first on the serverTrust to be able to extract the certificates
  13. NSMutableArray *certificateChain = [NSMutableArray array];
  14. CFIndex chainLen = SecTrustGetCertificateCount(serverTrust);
  15. for (CFIndex i=0;i<chainLen;i++)
  16. {
  17. SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i);
  18. CFDataRef certificateData = SecCertificateCopyData(certificate);
  19. // Craft the PEM certificate
  20. NSString *certificatePem = [NSString
  21. stringWithFormat:@"-----BEGIN CERTIFICATE-----\n%@\n-----END CERTIFICATE-----",
  22. [(__bridge NSData *)certificateData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
  23. [certificateChain addObject:certificatePem];
  24. CFRelease(certificateData);
  25. }
  26. return certificateChain;
  27. }
  28. NSArray<NSString *> *convertPinsToHpkpPins(NSSet<NSData *> *knownPins)
  29. {
  30. // Convert the know pins from a set of data to an array of strings as described in the HPKP spec
  31. NSMutableArray *formattedPins = [NSMutableArray array];
  32. for (NSData *pin in knownPins)
  33. {
  34. [formattedPins addObject:[NSString stringWithFormat:@"pin-sha256=\"%@\"", [pin base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)0]]];
  35. }
  36. return formattedPins;
  37. }