scoped_capi_types.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2012 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 CRYPTO_SCOPED_CAPI_TYPES_H_
  5. #define CRYPTO_SCOPED_CAPI_TYPES_H_
  6. #include <windows.h>
  7. #include <memory>
  8. #include "base/check.h"
  9. #include "base/scoped_generic.h"
  10. #include "base/win/wincrypt_shim.h"
  11. namespace crypto {
  12. // Simple traits for the Free family of CryptoAPI functions, such as
  13. // CryptDestroyHash, which take only a single argument to release.
  14. template <typename CAPIHandle, BOOL(WINAPI* Destroyer)(CAPIHandle)>
  15. struct CAPITraits {
  16. static CAPIHandle InvalidValue() { return 0; }
  17. static void Free(CAPIHandle handle) {
  18. BOOL ok = Destroyer(handle);
  19. DCHECK(ok);
  20. }
  21. };
  22. // Traits for the Close/Release family of CryptoAPI functions, which take
  23. // a second DWORD parameter indicating flags to use when closing or releasing.
  24. // This includes functions like CertCloseStore or CryptReleaseContext.
  25. template <typename CAPIHandle,
  26. BOOL(WINAPI* Destroyer)(CAPIHandle, DWORD),
  27. DWORD flags>
  28. struct CAPITraitsWithFlags {
  29. static CAPIHandle InvalidValue() { return 0; }
  30. static void Free(CAPIHandle handle) {
  31. BOOL ok = Destroyer(handle, flags);
  32. DCHECK(ok);
  33. }
  34. };
  35. using ScopedHCERTSTORE =
  36. base::ScopedGeneric<HCERTSTORE,
  37. CAPITraitsWithFlags<HCERTSTORE, CertCloseStore, 0>>;
  38. using ScopedHCRYPTPROV = base::ScopedGeneric<
  39. HCRYPTPROV,
  40. CAPITraitsWithFlags<HCRYPTPROV, CryptReleaseContext, 0>>;
  41. using ScopedHCRYPTKEY =
  42. base::ScopedGeneric<HCRYPTKEY, CAPITraits<HCRYPTKEY, CryptDestroyKey>>;
  43. using ScopedHCRYPTHASH =
  44. base::ScopedGeneric<HCRYPTHASH, CAPITraits<HCRYPTHASH, CryptDestroyHash>>;
  45. using ScopedHCRYPTMSG =
  46. base::ScopedGeneric<HCRYPTMSG, CAPITraits<HCRYPTMSG, CryptMsgClose>>;
  47. struct ChainEngineTraits {
  48. static HCERTCHAINENGINE InvalidValue() { return nullptr; }
  49. static void Free(HCERTCHAINENGINE engine) {
  50. CertFreeCertificateChainEngine(engine);
  51. }
  52. };
  53. using ScopedHCERTCHAINENGINE =
  54. base::ScopedGeneric<HCERTCHAINENGINE, ChainEngineTraits>;
  55. struct FreeCertContextFunctor {
  56. void operator()(PCCERT_CONTEXT context) const {
  57. if (context)
  58. CertFreeCertificateContext(context);
  59. }
  60. };
  61. using ScopedPCCERT_CONTEXT =
  62. std::unique_ptr<const CERT_CONTEXT, FreeCertContextFunctor>;
  63. struct FreeCertChainContextFunctor {
  64. void operator()(PCCERT_CHAIN_CONTEXT chain_context) const {
  65. if (chain_context)
  66. CertFreeCertificateChain(chain_context);
  67. }
  68. };
  69. using ScopedPCCERT_CHAIN_CONTEXT =
  70. std::unique_ptr<const CERT_CHAIN_CONTEXT, FreeCertChainContextFunctor>;
  71. struct FreeCtlContextFunctor {
  72. void operator()(PCCTL_CONTEXT ctl_context) const {
  73. if (ctl_context)
  74. CertFreeCTLContext(ctl_context);
  75. }
  76. };
  77. using ScopedPCCTL_CONTEXT =
  78. std::unique_ptr<const CTL_CONTEXT, FreeCtlContextFunctor>;
  79. } // namespace crypto
  80. #endif // CRYPTO_SCOPED_CAPI_TYPES_H_