OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_CRYPTO_CSSM_INIT_H_ | 5 #ifndef BASE_CRYPTO_CSSM_INIT_H_ |
6 #define BASE_CRYPTO_CSSM_INIT_H_ | 6 #define BASE_CRYPTO_CSSM_INIT_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <Security/cssm.h> | 9 #include <Security/cssm.h> |
10 | 10 |
11 #include "base/basictypes.h" | |
11 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 | 15 |
15 class Lock; | 16 class Lock; |
16 | 17 |
17 // Initialize CSSM if it isn't already initialized. This must be called before | 18 // Initialize CSSM if it isn't already initialized. This must be called before |
18 // any other CSSM functions. This function is thread-safe, and CSSM will only | 19 // any other CSSM functions. This function is thread-safe, and CSSM will only |
19 // ever be initialized once. CSSM will be properly shut down on program exit. | 20 // ever be initialized once. CSSM will be properly shut down on program exit. |
20 void EnsureCSSMInit(); | 21 void EnsureCSSMInit(); |
21 | 22 |
22 // Returns the shared CSP handle used by CSSM functions. | 23 // Returns the shared CSP handle used by CSSM functions. |
23 CSSM_CSP_HANDLE GetSharedCSPHandle(); | 24 CSSM_CSP_HANDLE GetSharedCSPHandle(); |
24 | 25 |
26 // Returns the shared CL handle used by CSSM functions. | |
27 CSSM_CL_HANDLE GetSharedCLHandle(); | |
28 | |
29 // Returns the shared TP handle used by CSSM functions. | |
30 CSSM_TP_HANDLE GetSharedTPHandle(); | |
31 | |
25 // Set of pointers to memory function wrappers that are required for CSSM | 32 // Set of pointers to memory function wrappers that are required for CSSM |
26 extern const CSSM_API_MEMORY_FUNCS kCssmMemoryFunctions; | 33 extern const CSSM_API_MEMORY_FUNCS kCssmMemoryFunctions; |
27 | 34 |
28 // Utility function to log an error message including the error name. | 35 // Utility function to log an error message including the error name. |
29 void LogCSSMError(const char *function_name, CSSM_RETURN err); | 36 void LogCSSMError(const char *function_name, CSSM_RETURN err); |
30 | 37 |
38 // Utility function to release memory allocated by CSSM. | |
39 // Note the wrapper classes below (ScopedCSSMData & ScopedCSSMTPtr) | |
40 void CSSMFree(void* ptr); | |
41 | |
31 // The OS X certificate and key management wrappers over CSSM are not | 42 // The OS X certificate and key management wrappers over CSSM are not |
32 // thread-safe. In particular, code that accesses the CSSM database is | 43 // thread-safe. In particular, code that accesses the CSSM database is |
33 // problematic. | 44 // problematic. |
34 // | 45 // |
35 // http://developer.apple.com/mac/library/documentation/Security/Reference/certi fkeytrustservices/Reference/reference.html | 46 // http://developer.apple.com/mac/library/documentation/Security/Reference/certi fkeytrustservices/Reference/reference.html |
36 Lock& GetMacSecurityServicesLock(); | 47 Lock& GetMacSecurityServicesLock(); |
37 | 48 |
49 // Wrapper class for CSSM_DATA type. | |
50 // The constructor initializes data_ to zero and the destructor releases the | |
51 // data properly. | |
52 class ScopedCSSMData { | |
53 public: | |
54 ScopedCSSMData(); | |
55 ~ScopedCSSMData(); | |
56 operator CSSM_DATA*() { return &data_; } | |
57 CSSM_DATA* operator ->() { return &data_; } | |
58 | |
59 private: | |
60 CSSM_DATA data_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(ScopedCSSMData); | |
63 }; | |
64 | |
65 // Wrapper class for CSSM types. | |
66 // Destructor frees the memory properly. | |
Ryan Sleevi
2011/02/05 00:23:37
nit: Can you specify that this should only be used
dmac
2011/02/08 01:23:45
Done.
| |
67 template<typename T> | |
68 class ScopedCSSMTPtr { | |
69 public: | |
70 typedef T* TPtr; | |
71 | |
72 explicit ScopedCSSMTPtr(T* ptr = NULL) : ptr_(ptr) { } | |
73 ~ScopedCSSMTPtr() { | |
74 reset(); | |
75 } | |
76 | |
77 void reset(T* p = NULL) { | |
78 if (p != ptr_) { | |
79 if (ptr_) { | |
80 CSSMFree(ptr_); | |
81 } | |
82 ptr_ = p; | |
83 } | |
84 } | |
85 | |
86 T* get() const { return ptr_; } | |
87 T* release() WARN_UNUSED_RESULT { | |
88 void* retVal = ptr_; | |
89 ptr_ = NULL; | |
90 return retVal; | |
91 } | |
92 | |
93 operator T*() { return ptr_; } | |
94 T* operator ->() { return ptr_; } | |
95 | |
96 // Receive is used when you need to pass a CSSMType* into a system | |
97 // function and have this object take ownership of th result. | |
98 TPtr& receive() { | |
99 assert(ptr_ == NULL); | |
100 return ptr_; | |
101 } | |
102 private: | |
103 T* ptr_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(ScopedCSSMTPtr); | |
106 }; | |
107 | |
38 } // namespace base | 108 } // namespace base |
39 | 109 |
40 #endif // BASE_CRYPTO_CSSM_INIT_H_ | 110 #endif // BASE_CRYPTO_CSSM_INIT_H_ |
OLD | NEW |