• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CRYPTO_SCOPED_CAPI_TYPES_H_
6 #define CRYPTO_SCOPED_CAPI_TYPES_H_
7 
8 #include <windows.h>
9 
10 #include <memory>
11 
12 #include "base/check.h"
13 #include "base/scoped_generic.h"
14 #include "base/win/wincrypt_shim.h"
15 
16 namespace crypto {
17 
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)>
21 struct CAPITraits {
InvalidValueCAPITraits22   static CAPIHandle InvalidValue() { return 0; }
FreeCAPITraits23   static void Free(CAPIHandle handle) {
24     BOOL ok = Destroyer(handle);
25     DCHECK(ok);
26   }
27 };
28 
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),
34           DWORD flags>
35 struct CAPITraitsWithFlags {
InvalidValueCAPITraitsWithFlags36   static CAPIHandle InvalidValue() { return 0; }
FreeCAPITraitsWithFlags37   static void Free(CAPIHandle handle) {
38     BOOL ok = Destroyer(handle, flags);
39     DCHECK(ok);
40   }
41 };
42 
43 using ScopedHCERTSTORE =
44     base::ScopedGeneric<HCERTSTORE,
45                         CAPITraitsWithFlags<HCERTSTORE, CertCloseStore, 0>>;
46 
47 using ScopedHCRYPTPROV = base::ScopedGeneric<
48     HCRYPTPROV,
49     CAPITraitsWithFlags<HCRYPTPROV, CryptReleaseContext, 0>>;
50 
51 using ScopedHCRYPTKEY =
52     base::ScopedGeneric<HCRYPTKEY, CAPITraits<HCRYPTKEY, CryptDestroyKey>>;
53 
54 using ScopedHCRYPTHASH =
55     base::ScopedGeneric<HCRYPTHASH, CAPITraits<HCRYPTHASH, CryptDestroyHash>>;
56 
57 using ScopedHCRYPTMSG =
58     base::ScopedGeneric<HCRYPTMSG, CAPITraits<HCRYPTMSG, CryptMsgClose>>;
59 
60 struct ChainEngineTraits {
InvalidValueChainEngineTraits61   static HCERTCHAINENGINE InvalidValue() { return nullptr; }
FreeChainEngineTraits62   static void Free(HCERTCHAINENGINE engine) {
63     CertFreeCertificateChainEngine(engine);
64   }
65 };
66 
67 using ScopedHCERTCHAINENGINE =
68     base::ScopedGeneric<HCERTCHAINENGINE, ChainEngineTraits>;
69 
70 struct FreeCertContextFunctor {
operatorFreeCertContextFunctor71   void operator()(PCCERT_CONTEXT context) const {
72     if (context)
73       CertFreeCertificateContext(context);
74   }
75 };
76 
77 using ScopedPCCERT_CONTEXT =
78     std::unique_ptr<const CERT_CONTEXT, FreeCertContextFunctor>;
79 
80 struct FreeCertChainContextFunctor {
operatorFreeCertChainContextFunctor81   void operator()(PCCERT_CHAIN_CONTEXT chain_context) const {
82     if (chain_context)
83       CertFreeCertificateChain(chain_context);
84   }
85 };
86 
87 using ScopedPCCERT_CHAIN_CONTEXT =
88     std::unique_ptr<const CERT_CHAIN_CONTEXT, FreeCertChainContextFunctor>;
89 
90 struct FreeCtlContextFunctor {
operatorFreeCtlContextFunctor91   void operator()(PCCTL_CONTEXT ctl_context) const {
92     if (ctl_context)
93       CertFreeCTLContext(ctl_context);
94   }
95 };
96 
97 using ScopedPCCTL_CONTEXT =
98     std::unique_ptr<const CTL_CONTEXT, FreeCtlContextFunctor>;
99 
100 }  // namespace crypto
101 
102 #endif  // CRYPTO_SCOPED_CAPI_TYPES_H_
103