Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(541)

Side by Side Diff: base/crypto/scoped_capi_types.h

Issue 1558018: Implements support for PBKDF2-based key derivation, random key generation, an... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Style fixup Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/crypto/rsa_private_key_win.cc ('k') | base/crypto/signature_creator.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ native
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 BASE_CRYPTO_SCOPED_CAPI_TYPES_H_
6 #define BASE_CRYPTO_SCOPED_CAPI_TYPES_H_
7
8 #include <windows.h>
9 #include <wincrypt.h>
10
11 #include "base/logging.h"
12
13 namespace base {
14
15 // Simple destructor for the Free family of CryptoAPI functions, such as
16 // CryptDestroyHash, which take only a single argument to release.
17 template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle)>
18 struct CAPIDestroyer {
19 void operator()(CAPIHandle handle) const {
20 if (handle) {
21 BOOL ok = Destroyer(handle);
22 DCHECK(ok);
23 }
24 }
25 };
26
27 // Destructor for the Close/Release family of CryptoAPI functions, which take
28 // a secondary DWORD parameter indicating flags to use when closing or
29 // releasing. This includes functions like CertCloseStore or
30 // CryptReleaseContext.
31 template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle, DWORD),
32 DWORD flags>
33 struct CAPIDestroyerWithFlags {
34 void operator()(CAPIHandle handle) const {
35 if (handle) {
36 BOOL ok = Destroyer(handle, flags);
37 DCHECK(ok);
38 }
39 }
40 };
41
42 // Scoped_ptr-like class for the CryptoAPI cryptography and certificate
43 // handles. Because these handles are defined as integer types, and not
44 // pointers, the existing scoped classes, such as scoped_ptr_malloc, are
45 // insufficient. The semantics are the same as scoped_ptr.
46 template <class CAPIHandle, typename FreeProc>
47 class ScopedCAPIHandle {
48 public:
49 ScopedCAPIHandle() : handle_(NULL) {}
50
51 explicit ScopedCAPIHandle(CAPIHandle handle) : handle_(handle) {}
52
53 ~ScopedCAPIHandle() {
54 free_(handle_);
55 }
56
57 void reset(CAPIHandle handle = NULL) {
58 if (handle_ != handle) {
59 free_(handle_);
60 handle_ = handle;
61 }
62 }
63
64 operator CAPIHandle() { return handle_; }
65 operator CAPIHandle() const { return handle_; }
66 CAPIHandle get() const { return handle_; }
67
68 CAPIHandle* receive() {
69 CHECK_EQ(NULL, handle_);
70 return &handle_;
71 }
72
73 bool operator==(CAPIHandle handle) {
74 return handle_ == handle;
75 }
76
77 bool operator!=(CAPIHandle handle) {
78 return handle_ != handle;
79 }
80
81 void swap(ScopedCAPIHandle& b) {
82 CAPIHandle tmp = b.handle_;
83 b.handle_ = handle_;
84 handle_ = tmp;
85 }
86
87 CAPIHandle release() {
88 CAPIHandle tmp = handle_;
89 handle_ = NULL;
90 return tmp;
91 }
92
93 private:
94 CAPIHandle handle_;
95 static FreeProc const free_;
96
97 DISALLOW_COPY_AND_ASSIGN(ScopedCAPIHandle);
98 };
99
100 template<class CH, typename FP>
101 FP const ScopedCAPIHandle<CH, FP>::free_ = FP();
102
103 template<class CH, typename FP> inline
104 bool operator==(CH h, const ScopedCAPIHandle<CH, FP>& b) {
105 return h == b.get();
106 }
107
108 template<class CH, typename FP> inline
109 bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) {
110 return h != b.get();
111 }
112
113 typedef ScopedCAPIHandle<
114 HCRYPTPROV,
115 CAPIDestroyerWithFlags<HCRYPTPROV,
116 CryptReleaseContext, 0> > ScopedHCRYPTPROV;
117
118 typedef ScopedCAPIHandle<
119 HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY;
120
121 typedef ScopedCAPIHandle<
122 HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH;
123
124 } // namespace base
125
126 #endif // BASE_CRYPTO_SCOPED_CAPI_TYPES_H_
OLDNEW
« no previous file with comments | « base/crypto/rsa_private_key_win.cc ('k') | base/crypto/signature_creator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698