x509_util_win.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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_WIN_H_
  5. #define NET_CERT_X509_UTIL_WIN_H_
  6. #include <memory>
  7. #include <vector>
  8. #include <windows.h>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/win/wincrypt_shim.h"
  11. #include "crypto/scoped_capi_types.h"
  12. #include "net/base/hash_value.h"
  13. #include "net/base/net_export.h"
  14. #include "net/cert/x509_certificate.h"
  15. namespace net::x509_util {
  16. // Creates an X509Certificate representing |os_cert| with intermediates
  17. // |os_chain|.
  18. NET_EXPORT scoped_refptr<X509Certificate> CreateX509CertificateFromCertContexts(
  19. PCCERT_CONTEXT os_cert,
  20. const std::vector<PCCERT_CONTEXT>& os_chain);
  21. // Creates an X509Certificate with non-standard parsing options.
  22. // Do not use without consulting //net owners.
  23. NET_EXPORT scoped_refptr<X509Certificate> CreateX509CertificateFromCertContexts(
  24. PCCERT_CONTEXT os_cert,
  25. const std::vector<PCCERT_CONTEXT>& os_chain,
  26. X509Certificate::UnsafeCreateOptions options);
  27. // Returns a new PCCERT_CONTEXT containing the certificate and its
  28. // intermediate certificates, or NULL on failure. This function is only
  29. // necessary if the CERT_CONTEXT.hCertStore member will be accessed or
  30. // enumerated, which is generally true for any CryptoAPI functions involving
  31. // certificate chains, including validation or certificate display.
  32. //
  33. // While the returned PCCERT_CONTEXT and its HCERTSTORE can safely be used on
  34. // multiple threads if no further modifications happen, it is generally
  35. // preferable for each thread that needs such a context to obtain its own,
  36. // rather than risk thread-safety issues by sharing.
  37. NET_EXPORT crypto::ScopedPCCERT_CONTEXT CreateCertContextWithChain(
  38. const X509Certificate* cert);
  39. // Specify behavior if an intermediate certificate fails CERT_CONTEXT parsing.
  40. // kFail means the function should return a failure result immediately. kIgnore
  41. // means the invalid intermediate is not added to the output context.
  42. enum class InvalidIntermediateBehavior { kFail, kIgnore };
  43. // As CreateCertContextWithChain above, but |invalid_intermediate_behavior|
  44. // specifies behavior if intermediates of |cert| could not be converted.
  45. NET_EXPORT crypto::ScopedPCCERT_CONTEXT CreateCertContextWithChain(
  46. const X509Certificate* cert,
  47. InvalidIntermediateBehavior invalid_intermediate_behavior);
  48. // Calculates the SHA-256 fingerprint of the certificate. Returns an empty
  49. // (all zero) fingerprint on failure.
  50. NET_EXPORT SHA256HashValue CalculateFingerprint256(PCCERT_CONTEXT cert);
  51. // Returns true if the certificate is self-signed.
  52. NET_EXPORT bool IsSelfSigned(PCCERT_CONTEXT cert_handle);
  53. } // namespace net::x509_util
  54. #endif // NET_CERT_X509_UTIL_WIN_H_