2 * Copyright (c) 2000-2001,2011,2014 Apple 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_utilities/globalizer.h>
28 #include <security_utilities/threading.h>
29 #include <security_utilities/alloc.h>
30 #include <security_cdsa_utilities/cssmerrors.h>
31 #include <Security/cssmapple.h>
32 #include <Security/cssmtype.h>
33 #include <Security/cssmapi.h>
39 mCspHand(CSSM_INVALID_HANDLE
),
40 mCspDlHand(CSSM_INVALID_HANDLE
)
43 CSSM_CSP_HANDLE
getCspHand(bool bareCsp
);
46 /* connection to CSP and CSPDL, evaluated lazily */
48 CSSM_HANDLE mCspDlHand
;
52 /* the single global thing */
53 static ModuleNexus
<CSPAttacher
> cspAttacher
;
55 static void *CL_malloc(
59 return Allocator::standard().malloc(size
);
66 Allocator::standard().free(memblock
);
69 static void *CL_realloc(
74 return Allocator::standard().realloc(memblock
, size
);
77 static void *CL_calloc(
82 return Allocator::standard().calloc(num
, size
);
85 static const CSSM_API_MEMORY_FUNCS CL_memFuncs
= {
95 * This only gets called when cspAttacher get deleted, i.e., when this code
96 * is actually unloaded from the process's address space.
98 CSPAttacher::~CSPAttacher()
100 StLock
<Mutex
> _(mLock
);
102 if(mCspHand
!= CSSM_INVALID_HANDLE
) {
103 CSSM_ModuleDetach(mCspHand
);
104 CSSM_ModuleUnload(&gGuidAppleCSP
, NULL
, NULL
);
106 if(mCspDlHand
!= CSSM_INVALID_HANDLE
) {
107 CSSM_ModuleDetach(mCspDlHand
);
108 CSSM_ModuleUnload(&gGuidAppleCSPDL
, NULL
, NULL
);
112 CSSM_CSP_HANDLE
CSPAttacher::getCspHand(bool bareCsp
)
116 const CSSM_GUID
*guid
;
117 CSSM_VERSION vers
= {2, 0};
118 StLock
<Mutex
> _(mLock
);
119 CSSM_CSP_HANDLE cspHand
;
122 if(mCspHand
!= CSSM_INVALID_HANDLE
) {
123 /* already connected */
126 guid
= &gGuidAppleCSP
;
127 modName
= "AppleCSP";
130 if(mCspDlHand
!= CSSM_INVALID_HANDLE
) {
131 /* already connected */
134 guid
= &gGuidAppleCSPDL
;
135 modName
= "AppleCSPDL";
137 crtn
= CSSM_ModuleLoad(guid
,
138 CSSM_KEY_HIERARCHY_NONE
,
139 NULL
, // eventHandler
140 NULL
); // AppNotifyCallbackCtx
142 clErrorLog("AppleX509CLSession::cspAttach: error (%d) loading %s",
144 CssmError::throwMe(crtn
);
146 crtn
= CSSM_ModuleAttach (guid
,
148 &CL_memFuncs
, // memFuncs
150 CSSM_SERVICE_CSP
, // SubserviceFlags
152 CSSM_KEY_HIERARCHY_NONE
,
153 NULL
, // FunctionTable
158 clErrorLog("AppleX509CLSession::cspAttach: error (%d) attaching to %s",
160 CssmError::throwMe(crtn
);
166 mCspDlHand
= cspHand
;
172 * Just one public function - "give me a CSP handle".
173 * bareCsp true: AppleCSP
174 * bareCsp false: AppleCSPDL
176 CSSM_CSP_HANDLE
getGlobalCspHand(bool bareCsp
)
178 return cspAttacher().getCspHand(bareCsp
);