1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 static const char CVS_ID
[] = "@(#) $RCSfile: dev3hack.c,v $ $Revision: 1.23 $ $Date: 2007/11/16 05:29:26 $";
60 NSS_IMPLEMENT nssSession
*
61 nssSession_ImportNSS3Session(NSSArena
*arenaOpt
,
62 CK_SESSION_HANDLE session
,
63 PZLock
*lock
, PRBool rw
)
65 nssSession
*rvSession
;
66 rvSession
= nss_ZNEW(arenaOpt
, nssSession
);
70 rvSession
->handle
= session
;
71 rvSession
->lock
= lock
;
72 rvSession
->ownLock
= PR_FALSE
;
77 NSS_IMPLEMENT nssSession
*
85 nssSession
*rvSession
;
86 rvSession
= nss_ZNEW(arenaOpt
, nssSession
);
88 return (nssSession
*)NULL
;
91 rvSession
->handle
= PK11_GetRWSession(slot
->pk11slot
);
92 if (rvSession
->handle
== CK_INVALID_HANDLE
) {
93 nss_ZFreeIf(rvSession
);
96 rvSession
->isRW
= PR_TRUE
;
97 rvSession
->slot
= slot
;
99 * The session doesn't need its own lock. Here's why.
100 * 1. If we are reusing the default RW session of the slot,
101 * the slot lock is already locked to protect the session.
102 * 2. If the module is not thread safe, the slot (or rather
103 * module) lock is already locked.
104 * 3. If the module is thread safe and we are using a new
105 * session, no higher-level lock has been locked and we
106 * would need a lock for the new session. However, the
107 * current usage of the session is that it is always
108 * used and destroyed within the same function and never
109 * shared with another thread.
110 * So the session is either already protected by another
111 * lock or only used by one thread.
113 rvSession
->lock
= NULL
;
114 rvSession
->ownLock
= PR_FALSE
;
121 NSS_IMPLEMENT PRStatus
130 PK11_RestoreROSession(s
->slot
->pk11slot
, s
->handle
);
134 return (ckrv
== CKR_OK
) ? PR_SUCCESS
: PR_FAILURE
;
138 nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain
*td
, PK11SlotInfo
*nss3slot
)
142 arena
= nssArena_Create();
146 rvSlot
= nss_ZNEW(arena
, NSSSlot
);
148 nssArena_Destroy(arena
);
151 rvSlot
->base
.refCount
= 1;
152 rvSlot
->base
.lock
= PZ_NewLock(nssILockOther
);
153 rvSlot
->base
.arena
= arena
;
154 rvSlot
->pk11slot
= nss3slot
;
155 rvSlot
->epv
= nss3slot
->functionList
;
156 rvSlot
->slotID
= nss3slot
->slotID
;
157 /* Grab the slot name from the PKCS#11 fixed-length buffer */
158 rvSlot
->base
.name
= nssUTF8_Duplicate(nss3slot
->slot_name
,td
->arena
);
159 rvSlot
->lock
= (nss3slot
->isThreadSafe
) ? NULL
: nss3slot
->sessionLock
;
163 NSS_IMPLEMENT NSSToken
*
164 nssToken_CreateFromPK11SlotInfo(NSSTrustDomain
*td
, PK11SlotInfo
*nss3slot
)
168 arena
= nssArena_Create();
172 rvToken
= nss_ZNEW(arena
, NSSToken
);
174 nssArena_Destroy(arena
);
177 rvToken
->base
.refCount
= 1;
178 rvToken
->base
.lock
= PZ_NewLock(nssILockOther
);
179 rvToken
->base
.arena
= arena
;
180 rvToken
->pk11slot
= nss3slot
;
181 rvToken
->epv
= nss3slot
->functionList
;
182 rvToken
->defaultSession
= nssSession_ImportNSS3Session(td
->arena
,
184 nss3slot
->sessionLock
,
185 nss3slot
->defRWSession
);
186 /* The above test was used in 3.4, for this cache have it always on */
187 if (!PK11_IsInternal(nss3slot
) && PK11_IsHW(nss3slot
)) {
188 rvToken
->cache
= nssTokenObjectCache_Create(rvToken
,
189 PR_TRUE
, PR_TRUE
, PR_TRUE
);
190 if (!rvToken
->cache
) {
191 nssArena_Destroy(arena
);
192 return (NSSToken
*)NULL
;
195 rvToken
->trustDomain
= td
;
196 /* Grab the token name from the PKCS#11 fixed-length buffer */
197 rvToken
->base
.name
= nssUTF8_Duplicate(nss3slot
->token_name
,td
->arena
);
198 rvToken
->slot
= nssSlot_CreateFromPK11SlotInfo(td
, nss3slot
);
199 if (!rvToken
->slot
) {
200 nssArena_Destroy(arena
);
201 return (NSSToken
*)NULL
;
203 rvToken
->slot
->token
= rvToken
;
204 rvToken
->defaultSession
->slot
= rvToken
->slot
;
209 nssToken_UpdateName(NSSToken
*token
)
214 token
->base
.name
= nssUTF8_Duplicate(token
->pk11slot
->token_name
,token
->base
.arena
);
223 return slot
->pk11slot
->isPerm
;
232 return PK11_IsFriendly(slot
->pk11slot
);
235 NSS_IMPLEMENT PRStatus
236 nssToken_Refresh(NSSToken
*token
)
238 PK11SlotInfo
*nss3slot
;
243 nss3slot
= token
->pk11slot
;
244 token
->defaultSession
= nssSession_ImportNSS3Session(token
->slot
->base
.arena
,
246 nss3slot
->sessionLock
,
247 nss3slot
->defRWSession
);
251 NSS_IMPLEMENT PRStatus
257 PK11SlotInfo
*nss3slot
= slot
->pk11slot
;
258 PRBool doit
= PR_FALSE
;
259 if (slot
->token
->base
.name
[0] == 0) {
262 if (PK11_InitToken(nss3slot
, PR_FALSE
) != SECSuccess
) {
266 nssTrustDomain_UpdateCachedTokenCerts(slot
->token
->trustDomain
,
269 return nssToken_Refresh(slot
->token
);
272 NSS_IMPLEMENT PRStatus
273 nssToken_GetTrustOrder
279 SECMODModule
*module
;
280 slot
= tok
->pk11slot
;
281 module
= PK11_GetModule(slot
);
282 return module
->trustOrder
;
291 if (!slot
->pk11slot
->needLogin
) {
294 return PK11_IsLoggedIn(slot
->pk11slot
, NULL
);
299 nssToken_GetTrustDomain(NSSToken
*token
)
301 return token
->trustDomain
;
305 nssTrustDomain_RemoveTokenCertsFromCache
311 NSS_IMPLEMENT PRStatus
312 nssToken_NotifyCertsNotVisible
317 return nssTrustDomain_RemoveTokenCertsFromCache(tok
->trustDomain
, tok
);