]> git.saurik.com Git - apple/security.git/blob - AppleX509CL/CSPAttacher.cpp
Security-28.tar.gz
[apple/security.git] / AppleX509CL / CSPAttacher.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 * CSPAttacher.cpp - process-wide class which loads and attaches to CSP at most
21 * once, and detaches and unloads the CSP when this code is
22 * unloaded.
23 */
24
25 #include "CSPAttacher.h"
26 #include "cldebugging.h"
27 #include <Security/globalizer.h>
28 #include <Security/threading.h>
29 #include <Security/cssmalloc.h>
30 #include <Security/cssmapple.h>
31 #include <Security/cssmtype.h>
32 #include <Security/cssmapi.h>
33
34 class CSPAttacher
35 {
36 public:
37 CSPAttacher() :
38 mCspHand(CSSM_INVALID_HANDLE),
39 mCspDlHand(CSSM_INVALID_HANDLE)
40 { }
41 ~CSPAttacher();
42 CSSM_CSP_HANDLE getCspHand(bool bareCsp);
43
44 private:
45 /* connection to CSP and CSPDL, evaluated lazily */
46 CSSM_HANDLE mCspHand;
47 CSSM_HANDLE mCspDlHand;
48 Mutex mLock;
49 };
50
51 /* the single global thing */
52 static ModuleNexus<CSPAttacher> cspAttacher;
53
54 static void *CL_malloc(
55 uint32 size,
56 void *allocref)
57 {
58 return CssmAllocator::standard().malloc(size);
59 }
60
61 static void CL_free(
62 void *memblock,
63 void *allocref)
64 {
65 CssmAllocator::standard().free(memblock);
66 }
67
68 static void *CL_realloc(
69 void *memblock,
70 uint32 size,
71 void *allocref)
72 {
73 return CssmAllocator::standard().realloc(memblock, size);
74 }
75
76 static void *CL_calloc(
77 uint32 num,
78 uint32 size,
79 void *allocref)
80 {
81 return CssmAllocator::standard().calloc(num, size);
82 }
83
84 static const CSSM_API_MEMORY_FUNCS CL_memFuncs = {
85 CL_malloc,
86 CL_free,
87 CL_realloc,
88 CL_calloc,
89 NULL
90 };
91
92
93 /*
94 * This only gets called when cspAttacher get deleted, i.e., when this code
95 * is actually unloaded from the process's address space.
96 */
97 CSPAttacher::~CSPAttacher()
98 {
99 StLock<Mutex> _(mLock);
100
101 if(mCspHand != CSSM_INVALID_HANDLE) {
102 CSSM_ModuleDetach(mCspHand);
103 CSSM_ModuleUnload(&gGuidAppleCSP, NULL, NULL);
104 }
105 if(mCspDlHand != CSSM_INVALID_HANDLE) {
106 CSSM_ModuleDetach(mCspDlHand);
107 CSSM_ModuleUnload(&gGuidAppleCSPDL, NULL, NULL);
108 }
109 }
110
111 CSSM_CSP_HANDLE CSPAttacher::getCspHand(bool bareCsp)
112 {
113 const char *modName;
114 CSSM_RETURN crtn;
115 const CSSM_GUID *guid;
116 CSSM_VERSION vers = {2, 0};
117 StLock<Mutex> _(mLock);
118 CSSM_CSP_HANDLE cspHand;
119
120 if(bareCsp) {
121 if(mCspHand != CSSM_INVALID_HANDLE) {
122 /* already connected */
123 return mCspHand;
124 }
125 guid = &gGuidAppleCSP;
126 modName = "AppleCSP";
127 }
128 else {
129 if(mCspDlHand != CSSM_INVALID_HANDLE) {
130 /* already connected */
131 return mCspDlHand;
132 }
133 guid = &gGuidAppleCSPDL;
134 modName = "AppleCSPDL";
135 }
136 crtn = CSSM_ModuleLoad(guid,
137 CSSM_KEY_HIERARCHY_NONE,
138 NULL, // eventHandler
139 NULL); // AppNotifyCallbackCtx
140 if(crtn) {
141 errorLog2("AppleX509CLSession::cspAttach: error (%d) loading %s\n",
142 (int)crtn, modName);
143 CssmError::throwMe(crtn);
144 }
145 crtn = CSSM_ModuleAttach (guid,
146 &vers,
147 &CL_memFuncs, // memFuncs
148 0, // SubserviceID
149 CSSM_SERVICE_CSP, // SubserviceFlags
150 0, // AttachFlags
151 CSSM_KEY_HIERARCHY_NONE,
152 NULL, // FunctionTable
153 0, // NumFuncTable
154 NULL, // reserved
155 &cspHand);
156 if(crtn) {
157 errorLog2("AppleX509CLSession::cspAttach: error (%d) attaching to %s\n",
158 (int)crtn, modName);
159 CssmError::throwMe(crtn);
160 }
161 if(bareCsp) {
162 mCspHand = cspHand;
163 }
164 else {
165 mCspDlHand = cspHand;
166 }
167 return cspHand;
168 }
169
170 /*
171 * Just one public function - "give me a CSP handle".
172 * bareCsp true: AppleCSP
173 * bareCsp false: AppleCSPDL
174 */
175 CSSM_CSP_HANDLE getGlobalCspHand(bool bareCsp)
176 {
177 return cspAttacher().getCspHand(bareCsp);
178 }
179