x509_util_apple.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_CERT_X509_UTIL_APPLE_H_
  5. #define NET_CERT_X509_UTIL_APPLE_H_
  6. #include <CoreFoundation/CFArray.h>
  7. #include <Security/Security.h>
  8. #include "base/mac/scoped_cftyperef.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "net/base/hash_value.h"
  11. #include "net/base/net_export.h"
  12. #include "net/cert/x509_certificate.h"
  13. namespace net {
  14. namespace x509_util {
  15. // Creates a SecCertificate handle from the DER-encoded representation.
  16. // Returns NULL on failure.
  17. NET_EXPORT base::ScopedCFTypeRef<SecCertificateRef>
  18. CreateSecCertificateFromBytes(const uint8_t* data, size_t length);
  19. // Returns a SecCertificate representing |cert|, or NULL on failure.
  20. NET_EXPORT base::ScopedCFTypeRef<SecCertificateRef>
  21. CreateSecCertificateFromX509Certificate(const X509Certificate* cert);
  22. // Returns a new CFMutableArrayRef containing this certificate and its
  23. // intermediate certificates in the form expected by Security.framework
  24. // and Keychain Services, or NULL on failure.
  25. // The first item in the array will be this certificate, followed by its
  26. // intermediates, if any.
  27. NET_EXPORT base::ScopedCFTypeRef<CFMutableArrayRef>
  28. CreateSecCertificateArrayForX509Certificate(X509Certificate* cert);
  29. // Specify behavior if an intermediate certificate fails SecCertificate
  30. // parsing. kFail means the function should return a failure result
  31. // immediately. kIgnore means the invalid intermediate is not added to the
  32. // output container.
  33. enum class InvalidIntermediateBehavior { kFail, kIgnore };
  34. // Returns a new CFMutableArrayRef containing this certificate and its
  35. // intermediate certificates in the form expected by Security.framework
  36. // and Keychain Services. Returns NULL if the certificate could not be
  37. // converted. |invalid_intermediate_behavior| specifies behavior if
  38. // intermediates of |cert| could not be converted.
  39. NET_EXPORT base::ScopedCFTypeRef<CFMutableArrayRef>
  40. CreateSecCertificateArrayForX509Certificate(
  41. X509Certificate* cert,
  42. InvalidIntermediateBehavior invalid_intermediate_behavior);
  43. // Creates an X509Certificate representing |sec_cert| with intermediates
  44. // |sec_chain|.
  45. NET_EXPORT scoped_refptr<X509Certificate>
  46. CreateX509CertificateFromSecCertificate(
  47. base::ScopedCFTypeRef<SecCertificateRef> sec_cert,
  48. const std::vector<base::ScopedCFTypeRef<SecCertificateRef>>& sec_chain);
  49. // Creates an X509Certificate with non-standard parsing options.
  50. // Do not use without consulting //net owners.
  51. NET_EXPORT scoped_refptr<X509Certificate>
  52. CreateX509CertificateFromSecCertificate(
  53. base::ScopedCFTypeRef<SecCertificateRef> sec_cert,
  54. const std::vector<base::ScopedCFTypeRef<SecCertificateRef>>& sec_chain,
  55. X509Certificate::UnsafeCreateOptions options);
  56. // Calculates the SHA-256 fingerprint of the certificate. Returns an empty
  57. // (all zero) fingerprint on failure.
  58. NET_EXPORT SHA256HashValue CalculateFingerprint256(SecCertificateRef cert);
  59. // Returns a new CFArrayRef containing the certificate chain built in |trust|.
  60. base::ScopedCFTypeRef<CFArrayRef> CertificateChainFromSecTrust(
  61. SecTrustRef trust);
  62. } // namespace x509_util
  63. } // namespace net
  64. #endif // NET_CERT_X509_UTIL_APPLE_H_