1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "crypto/cssm_init.h"
#include <Security/SecBase.h>
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/singleton.h"
#include "base/strings/sys_string_conversions.h"
// When writing crypto code for Mac OS X, you may find the following
// documentation useful:
// - Common Security: CDSA and CSSM, Version 2 (with corrigenda)
// http://www.opengroup.org/security/cdsa.htm
// - Apple Cryptographic Service Provider Functional Specification
// - CryptoSample: http://developer.apple.com/SampleCode/CryptoSample/
namespace {
void* CSSMMalloc(CSSM_SIZE size, void* alloc_ref) {
return malloc(size);
}
void CSSMFree(void* mem_ptr, void* alloc_ref) {
free(mem_ptr);
}
void* CSSMRealloc(void* ptr, CSSM_SIZE size, void* alloc_ref) {
return realloc(ptr, size);
}
void* CSSMCalloc(uint32 num, CSSM_SIZE size, void* alloc_ref) {
return calloc(num, size);
}
class CSSMInitSingleton {
public:
static CSSMInitSingleton* GetInstance() {
return Singleton<CSSMInitSingleton,
LeakySingletonTraits<CSSMInitSingleton> >::get();
}
CSSM_CSP_HANDLE csp_handle() const { return csp_handle_; }
CSSM_CL_HANDLE cl_handle() const { return cl_handle_; }
CSSM_TP_HANDLE tp_handle() const { return tp_handle_; }
private:
CSSMInitSingleton()
: inited_(false), csp_loaded_(false), cl_loaded_(false),
tp_loaded_(false), csp_handle_(CSSM_INVALID_HANDLE),
cl_handle_(CSSM_INVALID_HANDLE), tp_handle_(CSSM_INVALID_HANDLE) {
static CSSM_VERSION version = {2, 0};
// TODO(wtc): what should our caller GUID be?
static const CSSM_GUID test_guid = {
0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 }
};
CSSM_RETURN crtn;
CSSM_PVC_MODE pvc_policy = CSSM_PVC_NONE;
crtn = CSSM_Init(&version, CSSM_PRIVILEGE_SCOPE_NONE, &test_guid,
CSSM_KEY_HIERARCHY_NONE, &pvc_policy, NULL);
if (crtn) {
NOTREACHED();
return;
}
inited_ = true;
crtn = CSSM_ModuleLoad(&gGuidAppleCSP, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
if (crtn) {
NOTREACHED();
return;
}
csp_loaded_ = true;
crtn = CSSM_ModuleLoad(
&gGuidAppleX509CL, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
if (crtn) {
NOTREACHED();
return;
}
cl_loaded_ = true;
crtn = CSSM_ModuleLoad(
&gGuidAppleX509TP, CSSM_KEY_HIERARCHY_NONE, NULL, NULL);
if (crtn) {
NOTREACHED();
return;
}
tp_loaded_ = true;
const CSSM_API_MEMORY_FUNCS cssmMemoryFunctions = {
CSSMMalloc,
CSSMFree,
CSSMRealloc,
CSSMCalloc,
NULL
};
crtn = CSSM_ModuleAttach(&gGuidAppleCSP, &version, &cssmMemoryFunctions, 0,
CSSM_SERVICE_CSP, 0, CSSM_KEY_HIERARCHY_NONE,
NULL, 0, NULL, &csp_handle_);
DCHECK_EQ(CSSM_OK, crtn);
crtn = CSSM_ModuleAttach(&gGuidAppleX509CL, &version, &cssmMemoryFunctions,
0, CSSM_SERVICE_CL, 0, CSSM_KEY_HIERARCHY_NONE,
NULL, 0, NULL, &cl_handle_);
DCHECK_EQ(CSSM_OK, crtn);
crtn = CSSM_ModuleAttach(&gGuidAppleX509TP, &version, &cssmMemoryFunctions,
0, CSSM_SERVICE_TP, 0, CSSM_KEY_HIERARCHY_NONE,
NULL, 0, NULL, &tp_handle_);
DCHECK_EQ(CSSM_OK, crtn);
}
~CSSMInitSingleton() {
CSSM_RETURN crtn;
if (csp_handle_) {
CSSM_RETURN crtn = CSSM_ModuleDetach(csp_handle_);
DCHECK_EQ(CSSM_OK, crtn);
}
if (cl_handle_) {
CSSM_RETURN crtn = CSSM_ModuleDetach(cl_handle_);
DCHECK_EQ(CSSM_OK, crtn);
}
if (tp_handle_) {
CSSM_RETURN crtn = CSSM_ModuleDetach(tp_handle_);
DCHECK_EQ(CSSM_OK, crtn);
}
if (csp_loaded_) {
crtn = CSSM_ModuleUnload(&gGuidAppleCSP, NULL, NULL);
DCHECK_EQ(CSSM_OK, crtn);
}
if (cl_loaded_) {
crtn = CSSM_ModuleUnload(&gGuidAppleX509CL, NULL, NULL);
DCHECK_EQ(CSSM_OK, crtn);
}
if (tp_loaded_) {
crtn = CSSM_ModuleUnload(&gGuidAppleX509TP, NULL, NULL);
DCHECK_EQ(CSSM_OK, crtn);
}
if (inited_) {
crtn = CSSM_Terminate();
DCHECK_EQ(CSSM_OK, crtn);
}
}
bool inited_; // True if CSSM_Init has been called successfully.
bool csp_loaded_; // True if gGuidAppleCSP has been loaded
bool cl_loaded_; // True if gGuidAppleX509CL has been loaded.
bool tp_loaded_; // True if gGuidAppleX509TP has been loaded.
CSSM_CSP_HANDLE csp_handle_;
CSSM_CL_HANDLE cl_handle_;
CSSM_TP_HANDLE tp_handle_;
friend struct DefaultSingletonTraits<CSSMInitSingleton>;
};
} // namespace
namespace crypto {
void EnsureCSSMInit() {
CSSMInitSingleton::GetInstance();
}
CSSM_CSP_HANDLE GetSharedCSPHandle() {
return CSSMInitSingleton::GetInstance()->csp_handle();
}
CSSM_CL_HANDLE GetSharedCLHandle() {
return CSSMInitSingleton::GetInstance()->cl_handle();
}
CSSM_TP_HANDLE GetSharedTPHandle() {
return CSSMInitSingleton::GetInstance()->tp_handle();
}
void* CSSMMalloc(CSSM_SIZE size) {
return ::CSSMMalloc(size, NULL);
}
void CSSMFree(void* ptr) {
::CSSMFree(ptr, NULL);
}
void LogCSSMError(const char* fn_name, CSSM_RETURN err) {
if (!err)
return;
base::ScopedCFTypeRef<CFStringRef> cfstr(
SecCopyErrorMessageString(err, NULL));
LOG(ERROR) << fn_name << " returned " << err
<< " (" << base::SysCFStringRefToUTF8(cfstr) << ")";
}
ScopedCSSMData::ScopedCSSMData() {
memset(&data_, 0, sizeof(data_));
}
ScopedCSSMData::~ScopedCSSMData() {
if (data_.Data) {
CSSMFree(data_.Data);
data_.Data = NULL;
}
}
} // namespace crypto
|