2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
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
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>
38 mCspHand(CSSM_INVALID_HANDLE
),
39 mCspDlHand(CSSM_INVALID_HANDLE
)
42 CSSM_CSP_HANDLE
getCspHand(bool bareCsp
);
45 /* connection to CSP and CSPDL, evaluated lazily */
47 CSSM_HANDLE mCspDlHand
;
51 /* the single global thing */
52 static ModuleNexus
<CSPAttacher
> cspAttacher
;
54 static void *CL_malloc(
58 return CssmAllocator::standard().malloc(size
);
65 CssmAllocator::standard().free(memblock
);
68 static void *CL_realloc(
73 return CssmAllocator::standard().realloc(memblock
, size
);
76 static void *CL_calloc(
81 return CssmAllocator::standard().calloc(num
, size
);
84 static const CSSM_API_MEMORY_FUNCS CL_memFuncs
= {
94 * This only gets called when cspAttacher get deleted, i.e., when this code
95 * is actually unloaded from the process's address space.
97 CSPAttacher::~CSPAttacher()
99 StLock
<Mutex
> _(mLock
);
101 if(mCspHand
!= CSSM_INVALID_HANDLE
) {
102 CSSM_ModuleDetach(mCspHand
);
103 CSSM_ModuleUnload(&gGuidAppleCSP
, NULL
, NULL
);
105 if(mCspDlHand
!= CSSM_INVALID_HANDLE
) {
106 CSSM_ModuleDetach(mCspDlHand
);
107 CSSM_ModuleUnload(&gGuidAppleCSPDL
, NULL
, NULL
);
111 CSSM_CSP_HANDLE
CSPAttacher::getCspHand(bool bareCsp
)
115 const CSSM_GUID
*guid
;
116 CSSM_VERSION vers
= {2, 0};
117 StLock
<Mutex
> _(mLock
);
118 CSSM_CSP_HANDLE cspHand
;
121 if(mCspHand
!= CSSM_INVALID_HANDLE
) {
122 /* already connected */
125 guid
= &gGuidAppleCSP
;
126 modName
= "AppleCSP";
129 if(mCspDlHand
!= CSSM_INVALID_HANDLE
) {
130 /* already connected */
133 guid
= &gGuidAppleCSPDL
;
134 modName
= "AppleCSPDL";
136 crtn
= CSSM_ModuleLoad(guid
,
137 CSSM_KEY_HIERARCHY_NONE
,
138 NULL
, // eventHandler
139 NULL
); // AppNotifyCallbackCtx
141 errorLog2("AppleX509CLSession::cspAttach: error (%d) loading %s\n",
143 CssmError::throwMe(crtn
);
145 crtn
= CSSM_ModuleAttach (guid
,
147 &CL_memFuncs
, // memFuncs
149 CSSM_SERVICE_CSP
, // SubserviceFlags
151 CSSM_KEY_HIERARCHY_NONE
,
152 NULL
, // FunctionTable
157 errorLog2("AppleX509CLSession::cspAttach: error (%d) attaching to %s\n",
159 CssmError::throwMe(crtn
);
165 mCspDlHand
= cspHand
;
171 * Just one public function - "give me a CSP handle".
172 * bareCsp true: AppleCSP
173 * bareCsp false: AppleCSPDL
175 CSSM_CSP_HANDLE
getGlobalCspHand(bool bareCsp
)
177 return cspAttacher().getCspHand(bareCsp
);