| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "base/crypto/cssm_init.h" | 5 #include "base/crypto/cssm_init.h" |
| 6 | 6 |
| 7 #include <Security/SecBase.h> | 7 #include <Security/SecBase.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
| 11 #include "base/singleton.h" | 11 #include "base/singleton.h" |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
| 14 | 13 |
| 15 // When writing crypto code for Mac OS X, you may find the following | 14 // When writing crypto code for Mac OS X, you may find the following |
| 16 // documentation useful: | 15 // documentation useful: |
| 17 // - Common Security: CDSA and CSSM, Version 2 (with corrigenda) | 16 // - Common Security: CDSA and CSSM, Version 2 (with corrigenda) |
| 18 // http://www.opengroup.org/security/cdsa.htm | 17 // http://www.opengroup.org/security/cdsa.htm |
| 19 // - Apple Cryptographic Service Provider Functional Specification | 18 // - Apple Cryptographic Service Provider Functional Specification |
| 20 // - CryptoSample: http://developer.apple.com/SampleCode/CryptoSample/ | 19 // - CryptoSample: http://developer.apple.com/SampleCode/CryptoSample/ |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 bool csp_loaded_; // True if gGuidAppleCSP has been loaded | 146 bool csp_loaded_; // True if gGuidAppleCSP has been loaded |
| 148 bool cl_loaded_; // True if gGuidAppleX509CL has been loaded. | 147 bool cl_loaded_; // True if gGuidAppleX509CL has been loaded. |
| 149 bool tp_loaded_; // True if gGuidAppleX509TP has been loaded. | 148 bool tp_loaded_; // True if gGuidAppleX509TP has been loaded. |
| 150 CSSM_CSP_HANDLE csp_handle_; | 149 CSSM_CSP_HANDLE csp_handle_; |
| 151 CSSM_CL_HANDLE cl_handle_; | 150 CSSM_CL_HANDLE cl_handle_; |
| 152 CSSM_TP_HANDLE tp_handle_; | 151 CSSM_TP_HANDLE tp_handle_; |
| 153 | 152 |
| 154 friend struct DefaultSingletonTraits<CSSMInitSingleton>; | 153 friend struct DefaultSingletonTraits<CSSMInitSingleton>; |
| 155 }; | 154 }; |
| 156 | 155 |
| 157 // This singleton is separate as it pertains to Apple's wrappers over | |
| 158 // their own CSSM handles, as opposed to our own CSSM_CSP_HANDLE. | |
| 159 class SecurityServicesSingleton { | |
| 160 public: | |
| 161 static SecurityServicesSingleton* GetInstance() { | |
| 162 return Singleton<SecurityServicesSingleton, | |
| 163 LeakySingletonTraits<SecurityServicesSingleton> >::get(); | |
| 164 } | |
| 165 | |
| 166 base::Lock& lock() { return lock_; } | |
| 167 | |
| 168 private: | |
| 169 friend struct DefaultSingletonTraits<SecurityServicesSingleton>; | |
| 170 | |
| 171 SecurityServicesSingleton() {} | |
| 172 ~SecurityServicesSingleton() {} | |
| 173 | |
| 174 base::Lock lock_; | |
| 175 | |
| 176 DISALLOW_COPY_AND_ASSIGN(SecurityServicesSingleton); | |
| 177 }; | |
| 178 | |
| 179 } // namespace | 156 } // namespace |
| 180 | 157 |
| 181 namespace base { | 158 namespace base { |
| 182 | 159 |
| 183 void EnsureCSSMInit() { | 160 void EnsureCSSMInit() { |
| 184 CSSMInitSingleton::GetInstance(); | 161 CSSMInitSingleton::GetInstance(); |
| 185 } | 162 } |
| 186 | 163 |
| 187 CSSM_CSP_HANDLE GetSharedCSPHandle() { | 164 CSSM_CSP_HANDLE GetSharedCSPHandle() { |
| 188 return CSSMInitSingleton::GetInstance()->csp_handle(); | 165 return CSSMInitSingleton::GetInstance()->csp_handle(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 206 | 183 |
| 207 void LogCSSMError(const char* fn_name, CSSM_RETURN err) { | 184 void LogCSSMError(const char* fn_name, CSSM_RETURN err) { |
| 208 if (!err) | 185 if (!err) |
| 209 return; | 186 return; |
| 210 base::mac::ScopedCFTypeRef<CFStringRef> cfstr( | 187 base::mac::ScopedCFTypeRef<CFStringRef> cfstr( |
| 211 SecCopyErrorMessageString(err, NULL)); | 188 SecCopyErrorMessageString(err, NULL)); |
| 212 LOG(ERROR) << fn_name << " returned " << err | 189 LOG(ERROR) << fn_name << " returned " << err |
| 213 << " (" << SysCFStringRefToUTF8(cfstr) << ")"; | 190 << " (" << SysCFStringRefToUTF8(cfstr) << ")"; |
| 214 } | 191 } |
| 215 | 192 |
| 216 base::Lock& GetMacSecurityServicesLock() { | |
| 217 return SecurityServicesSingleton::GetInstance()->lock(); | |
| 218 } | |
| 219 | |
| 220 ScopedCSSMData::ScopedCSSMData() { | 193 ScopedCSSMData::ScopedCSSMData() { |
| 221 memset(&data_, 0, sizeof(data_)); | 194 memset(&data_, 0, sizeof(data_)); |
| 222 } | 195 } |
| 223 | 196 |
| 224 ScopedCSSMData::~ScopedCSSMData() { | 197 ScopedCSSMData::~ScopedCSSMData() { |
| 225 if (data_.Data) { | 198 if (data_.Data) { |
| 226 CSSMFree(data_.Data); | 199 CSSMFree(data_.Data); |
| 227 data_.Data = NULL; | 200 data_.Data = NULL; |
| 228 } | 201 } |
| 229 } | 202 } |
| 230 | 203 |
| 231 } // namespace base | 204 } // namespace base |
| OLD | NEW |