blob: 16a519200103e35687ea4b70c52ef877d1810d6b [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
5#ifndef CRYPTO_SCOPED_CAPI_TYPES_H_
6#define CRYPTO_SCOPED_CAPI_TYPES_H_
7
8#include <windows.h>
9#include <wincrypt.h>
10
11#include <algorithm>
12
13#include "base/logging.h"
14
15namespace crypto {
16
17// Simple destructor for the Free family of CryptoAPI functions, such as
18// CryptDestroyHash, which take only a single argument to release.
19template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle)>
20struct CAPIDestroyer {
21 void operator()(CAPIHandle handle) const {
22 if (handle) {
23 BOOL ok = Destroyer(handle);
24 DCHECK(ok);
25 }
26 }
27};
28
29// Destructor 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.
32template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle, DWORD),
33 DWORD flags>
34struct CAPIDestroyerWithFlags {
35 void operator()(CAPIHandle handle) const {
36 if (handle) {
37 BOOL ok = Destroyer(handle, flags);
38 DCHECK(ok);
39 }
40 }
41};
42
43// scoped_ptr-like class for the CryptoAPI cryptography and certificate
44// handles. Because these handles are defined as integer types, and not
45// pointers, the existing scoped classes, such as scoped_ptr_malloc, are
46// insufficient. The semantics are the same as scoped_ptr.
47template <class CAPIHandle, typename FreeProc>
48class ScopedCAPIHandle {
49 public:
50 explicit ScopedCAPIHandle(CAPIHandle handle = NULL) : handle_(handle) {}
51
52 ~ScopedCAPIHandle() {
53 reset();
54 }
55
56 void reset(CAPIHandle handle = NULL) {
57 if (handle_ != handle) {
58 FreeProc free_proc;
59 free_proc(handle_);
60 handle_ = handle;
61 }
62 }
63
64 operator CAPIHandle() const { return handle_; }
65 CAPIHandle get() const { return handle_; }
66
67 CAPIHandle* receive() {
68 CHECK(handle_ == NULL);
69 return &handle_;
70 }
71
72 bool operator==(CAPIHandle handle) const {
73 return handle_ == handle;
74 }
75
76 bool operator!=(CAPIHandle handle) const {
77 return handle_ != handle;
78 }
79
80 void swap(ScopedCAPIHandle& b) {
81 CAPIHandle tmp = b.handle_;
82 b.handle_ = handle_;
83 handle_ = tmp;
84 }
85
86 CAPIHandle release() {
87 CAPIHandle tmp = handle_;
88 handle_ = NULL;
89 return tmp;
90 }
91
92 private:
93 CAPIHandle handle_;
94
95 DISALLOW_COPY_AND_ASSIGN(ScopedCAPIHandle);
96};
97
98template<class CH, typename FP> inline
99bool operator==(CH h, const ScopedCAPIHandle<CH, FP>& b) {
100 return h == b.get();
101}
102
103template<class CH, typename FP> inline
104bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) {
105 return h != b.get();
106}
107
108typedef ScopedCAPIHandle<
109 HCRYPTPROV,
110 CAPIDestroyerWithFlags<HCRYPTPROV,
111 CryptReleaseContext, 0> > ScopedHCRYPTPROV;
112
113typedef ScopedCAPIHandle<
114 HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY;
115
116typedef ScopedCAPIHandle<
117 HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH;
118
119} // namespace crypto
120
121#endif // CRYPTO_SCOPED_CAPI_TYPES_H_