1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CRYPTO_SCOPED_CAPI_TYPES_H_
6 #define CRYPTO_SCOPED_CAPI_TYPES_H_
12 #include "base/check.h"
13 #include "base/scoped_generic.h"
14 #include "base/win/wincrypt_shim.h"
18 // Simple traits for the Free family of CryptoAPI functions, such as
19 // CryptDestroyHash, which take only a single argument to release.
20 template <typename CAPIHandle, BOOL(WINAPI* Destroyer)(CAPIHandle)>
22 static CAPIHandle InvalidValue() { return 0; }
23 static void Free(CAPIHandle handle) {
24 BOOL ok = Destroyer(handle);
29 // Traits for the Close/Release family of CryptoAPI functions, which take
30 // a second DWORD parameter indicating flags to use when closing or releasing.
31 // This includes functions like CertCloseStore or CryptReleaseContext.
32 template <typename CAPIHandle,
33 BOOL(WINAPI* Destroyer)(CAPIHandle, DWORD),
35 struct CAPITraitsWithFlags {
36 static CAPIHandle InvalidValue() { return 0; }
37 static void Free(CAPIHandle handle) {
38 BOOL ok = Destroyer(handle, flags);
43 using ScopedHCERTSTORE =
44 base::ScopedGeneric<HCERTSTORE,
45 CAPITraitsWithFlags<HCERTSTORE, CertCloseStore, 0>>;
47 using ScopedHCRYPTPROV = base::ScopedGeneric<
49 CAPITraitsWithFlags<HCRYPTPROV, CryptReleaseContext, 0>>;
51 using ScopedHCRYPTKEY =
52 base::ScopedGeneric<HCRYPTKEY, CAPITraits<HCRYPTKEY, CryptDestroyKey>>;
54 using ScopedHCRYPTHASH =
55 base::ScopedGeneric<HCRYPTHASH, CAPITraits<HCRYPTHASH, CryptDestroyHash>>;
57 using ScopedHCRYPTMSG =
58 base::ScopedGeneric<HCRYPTMSG, CAPITraits<HCRYPTMSG, CryptMsgClose>>;
60 struct ChainEngineTraits {
61 static HCERTCHAINENGINE InvalidValue() { return nullptr; }
62 static void Free(HCERTCHAINENGINE engine) {
63 CertFreeCertificateChainEngine(engine);
67 using ScopedHCERTCHAINENGINE =
68 base::ScopedGeneric<HCERTCHAINENGINE, ChainEngineTraits>;
70 struct FreeCertContextFunctor {
71 void operator()(PCCERT_CONTEXT context) const {
73 CertFreeCertificateContext(context);
77 using ScopedPCCERT_CONTEXT =
78 std::unique_ptr<const CERT_CONTEXT, FreeCertContextFunctor>;
80 struct FreeCertChainContextFunctor {
81 void operator()(PCCERT_CHAIN_CONTEXT chain_context) const {
83 CertFreeCertificateChain(chain_context);
87 using ScopedPCCERT_CHAIN_CONTEXT =
88 std::unique_ptr<const CERT_CHAIN_CONTEXT, FreeCertChainContextFunctor>;
90 struct FreeCtlContextFunctor {
91 void operator()(PCCTL_CONTEXT ctl_context) const {
93 CertFreeCTLContext(ctl_context);
97 using ScopedPCCTL_CONTEXT =
98 std::unique_ptr<const CTL_CONTEXT, FreeCtlContextFunctor>;
100 } // namespace crypto
102 #endif // CRYPTO_SCOPED_CAPI_TYPES_H_