| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/crypto/cssm_init.h" |
| 6 |
| 7 #include <Security/cssm.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/singleton.h" |
| 11 |
| 12 // When writing crypto code for Mac OS X, you may find the following |
| 13 // documentation useful: |
| 14 // - Common Security: CDSA and CSSM, Version 2 (with corrigenda) |
| 15 // http://www.opengroup.org/security/cdsa.htm |
| 16 // - Apple Cryptographic Service Provider Functional Specification |
| 17 // - CryptoSample: http://developer.apple.com/SampleCode/CryptoSample/ |
| 18 |
| 19 namespace { |
| 20 |
| 21 class CSSMInitSingleton { |
| 22 public: |
| 23 CSSMInitSingleton() : inited_(false), loaded_(false) { |
| 24 static CSSM_VERSION version = {2, 0}; |
| 25 // TODO(wtc): what should our caller GUID be? |
| 26 static const CSSM_GUID test_guid = { |
| 27 0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 } |
| 28 }; |
| 29 CSSM_RETURN crtn; |
| 30 CSSM_PVC_MODE pvc_policy = CSSM_PVC_NONE; |
| 31 crtn = CSSM_Init(&version, CSSM_PRIVILEGE_SCOPE_NONE, &test_guid, |
| 32 CSSM_KEY_HIERARCHY_NONE, &pvc_policy, NULL); |
| 33 if (crtn) { |
| 34 NOTREACHED(); |
| 35 return; |
| 36 } |
| 37 inited_ = true; |
| 38 |
| 39 crtn = CSSM_ModuleLoad(&gGuidAppleCSP, CSSM_KEY_HIERARCHY_NONE, NULL, NULL); |
| 40 if (crtn) { |
| 41 NOTREACHED(); |
| 42 return; |
| 43 } |
| 44 loaded_ = true; |
| 45 } |
| 46 |
| 47 ~CSSMInitSingleton() { |
| 48 CSSM_RETURN crtn; |
| 49 if (loaded_) { |
| 50 crtn = CSSM_ModuleUnload(&gGuidAppleCSP, NULL, NULL); |
| 51 DCHECK(crtn == CSSM_OK); |
| 52 } |
| 53 if (inited_) { |
| 54 crtn = CSSM_Terminate(); |
| 55 DCHECK(crtn == CSSM_OK); |
| 56 } |
| 57 } |
| 58 |
| 59 private: |
| 60 bool inited_; // True if CSSM_Init has been called successfully. |
| 61 bool loaded_; // True if CSSM_ModuleLoad has been called successfully. |
| 62 }; |
| 63 |
| 64 } // namespace |
| 65 |
| 66 namespace base { |
| 67 |
| 68 void EnsureCSSMInit() { |
| 69 Singleton<CSSMInitSingleton>::get(); |
| 70 } |
| 71 |
| 72 } // namespace base |
| OLD | NEW |