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

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

Issue 1528021: Implement PBKDF2-based key derivation, random key generation,... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Make albertb's suggested changes. 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
+ LF
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 <algorithm>
12
13 #include "base/logging.h"
14
15 namespace base {
16
17 // Simple destructor for the Free family of CryptoAPI functions, such as
18 // CryptDestroyHash, which take only a single argument to release.
19 template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle)>
20 struct 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.
32 template <typename CAPIHandle, BOOL (WINAPI *Destroyer)(CAPIHandle, DWORD),
33 DWORD flags>
34 struct 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.
47 template <class CAPIHandle, typename FreeProc>
48 class ScopedCAPIHandle {
49 public:
50 explicit ScopedCAPIHandle(CAPIHandle handle = NULL) : handle_(handle) {}
51
52 ~ScopedCAPIHandle() {
53 free_(handle_);
54 }
55
56 void reset(CAPIHandle handle = NULL) {
57 if (handle_ != handle) {
58 free_(handle_);
59 handle_ = handle;
60 }
61 }
62
63 operator CAPIHandle() const { return handle_; }
64 CAPIHandle get() const { return handle_; }
65
66 CAPIHandle* receive() {
67 CHECK_EQ(NULL, handle_);
68 return &handle_;
69 }
70
71 bool operator==(CAPIHandle handle) const {
72 return handle_ == handle;
73 }
74
75 bool operator!=(CAPIHandle handle) const {
76 return handle_ != handle;
77 }
78
79 void swap(ScopedCAPIHandle& b) {
80 CAPIHandle tmp = b.handle_;
81 b.handle_ = handle_;
82 handle_ = tmp;
83 }
84
85 CAPIHandle release() {
86 CAPIHandle tmp = handle_;
87 handle_ = NULL;
88 return tmp;
89 }
90
91 private:
92 CAPIHandle handle_;
93 static const FreeProc free_;
94
95 DISALLOW_COPY_AND_ASSIGN(ScopedCAPIHandle);
96 };
97
98 template<class CH, typename FP>
99 const FP ScopedCAPIHandle<CH, FP>::free_ = FP();
100
101 template<class CH, typename FP> inline
102 bool operator==(CH h, const ScopedCAPIHandle<CH, FP>& b) {
103 return h == b.get();
104 }
105
106 template<class CH, typename FP> inline
107 bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) {
108 return h != b.get();
109 }
110
111 typedef ScopedCAPIHandle<
112 HCRYPTPROV,
113 CAPIDestroyerWithFlags<HCRYPTPROV,
114 CryptReleaseContext, 0> > ScopedHCRYPTPROV;
115
116 typedef ScopedCAPIHandle<
117 HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY;
118
119 typedef ScopedCAPIHandle<
120 HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH;
121
122 } // namespace base
123
124 #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