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.22 $ $Date: 2006/10/09 22:14:04 $";
43 #endif /* NSS_3_4_CODE */
64 NSS_IMPLEMENT nssSession
*
65 nssSession_ImportNSS3Session(NSSArena
*arenaOpt
,
66 CK_SESSION_HANDLE session
,
67 PZLock
*lock
, PRBool rw
)
69 nssSession
*rvSession
;
70 rvSession
= nss_ZNEW(arenaOpt
, nssSession
);
74 rvSession
->handle
= session
;
75 rvSession
->lock
= lock
;
76 rvSession
->ownLock
= PR_FALSE
;
81 NSS_IMPLEMENT nssSession
*
89 nssSession
*rvSession
;
90 rvSession
= nss_ZNEW(arenaOpt
, nssSession
);
92 return (nssSession
*)NULL
;
95 rvSession
->handle
= PK11_GetRWSession(slot
->pk11slot
);
96 if (rvSession
->handle
== CK_INVALID_HANDLE
) {
97 nss_ZFreeIf(rvSession
);
100 rvSession
->isRW
= PR_TRUE
;
101 rvSession
->slot
= slot
;
103 * The session doesn't need its own lock. Here's why.
104 * 1. If we are reusing the default RW session of the slot,
105 * the slot lock is already locked to protect the session.
106 * 2. If the module is not thread safe, the slot (or rather
107 * module) lock is already locked.
108 * 3. If the module is thread safe and we are using a new
109 * session, no higher-level lock has been locked and we
110 * would need a lock for the new session. However, the
111 * NSS_3_4_CODE usage of the session is that it is always
112 * used and destroyed within the same function and never
113 * shared with another thread.
114 * So the session is either already protected by another
115 * lock or only used by one thread.
117 rvSession
->lock
= NULL
;
118 rvSession
->ownLock
= PR_FALSE
;
125 NSS_IMPLEMENT PRStatus
134 PK11_RestoreROSession(s
->slot
->pk11slot
, s
->handle
);
138 return (ckrv
== CKR_OK
) ? PR_SUCCESS
: PR_FAILURE
;
142 nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain
*td
, PK11SlotInfo
*nss3slot
)
146 arena
= nssArena_Create();
150 rvSlot
= nss_ZNEW(arena
, NSSSlot
);
152 nssArena_Destroy(arena
);
155 rvSlot
->base
.refCount
= 1;
156 rvSlot
->base
.lock
= PZ_NewLock(nssILockOther
);
157 rvSlot
->base
.arena
= arena
;
158 rvSlot
->pk11slot
= nss3slot
;
159 rvSlot
->epv
= nss3slot
->functionList
;
160 rvSlot
->slotID
= nss3slot
->slotID
;
161 /* Grab the slot name from the PKCS#11 fixed-length buffer */
162 rvSlot
->base
.name
= nssUTF8_Duplicate(nss3slot
->slot_name
,td
->arena
);
163 rvSlot
->lock
= (nss3slot
->isThreadSafe
) ? NULL
: nss3slot
->sessionLock
;
167 NSS_IMPLEMENT NSSToken
*
168 nssToken_CreateFromPK11SlotInfo(NSSTrustDomain
*td
, PK11SlotInfo
*nss3slot
)
172 arena
= nssArena_Create();
176 rvToken
= nss_ZNEW(arena
, NSSToken
);
178 nssArena_Destroy(arena
);
181 rvToken
->base
.refCount
= 1;
182 rvToken
->base
.lock
= PZ_NewLock(nssILockOther
);
183 rvToken
->base
.arena
= arena
;
184 rvToken
->pk11slot
= nss3slot
;
185 rvToken
->epv
= nss3slot
->functionList
;
186 rvToken
->defaultSession
= nssSession_ImportNSS3Session(td
->arena
,
188 nss3slot
->sessionLock
,
189 nss3slot
->defRWSession
);
190 /* The above test was used in 3.4, for this cache have it always on */
191 if (!PK11_IsInternal(nss3slot
) && PK11_IsHW(nss3slot
)) {
192 rvToken
->cache
= nssTokenObjectCache_Create(rvToken
,
193 PR_TRUE
, PR_TRUE
, PR_TRUE
);
194 if (!rvToken
->cache
) {
195 nssArena_Destroy(arena
);
196 return (NSSToken
*)NULL
;
199 rvToken
->trustDomain
= td
;
200 /* Grab the token name from the PKCS#11 fixed-length buffer */
201 rvToken
->base
.name
= nssUTF8_Duplicate(nss3slot
->token_name
,td
->arena
);
202 rvToken
->slot
= nssSlot_CreateFromPK11SlotInfo(td
, nss3slot
);
203 if (!rvToken
->slot
) {
204 nssArena_Destroy(arena
);
205 return (NSSToken
*)NULL
;
207 rvToken
->slot
->token
= rvToken
;
208 rvToken
->defaultSession
->slot
= rvToken
->slot
;
213 nssToken_UpdateName(NSSToken
*token
)
218 token
->base
.name
= nssUTF8_Duplicate(token
->pk11slot
->token_name
,token
->base
.arena
);
227 return slot
->pk11slot
->isPerm
;
236 return PK11_IsFriendly(slot
->pk11slot
);
239 NSS_IMPLEMENT PRStatus
240 nssToken_Refresh(NSSToken
*token
)
242 PK11SlotInfo
*nss3slot
;
247 nss3slot
= token
->pk11slot
;
248 token
->defaultSession
= nssSession_ImportNSS3Session(token
->slot
->base
.arena
,
250 nss3slot
->sessionLock
,
251 nss3slot
->defRWSession
);
255 NSS_IMPLEMENT PRStatus
261 PK11SlotInfo
*nss3slot
= slot
->pk11slot
;
262 PRBool doit
= PR_FALSE
;
263 if (slot
->token
->base
.name
[0] == 0) {
266 if (PK11_InitToken(nss3slot
, PR_FALSE
) != SECSuccess
) {
270 nssTrustDomain_UpdateCachedTokenCerts(slot
->token
->trustDomain
,
273 return nssToken_Refresh(slot
->token
);
276 NSS_IMPLEMENT PRStatus
277 nssToken_GetTrustOrder
283 SECMODModule
*module
;
284 slot
= tok
->pk11slot
;
285 module
= PK11_GetModule(slot
);
286 return module
->trustOrder
;
295 if (!slot
->pk11slot
->needLogin
) {
298 return PK11_IsLoggedIn(slot
->pk11slot
, NULL
);
303 nssToken_GetTrustDomain(NSSToken
*token
)
305 return token
->trustDomain
;
309 nssTrustDomain_RemoveTokenCertsFromCache
315 NSS_IMPLEMENT PRStatus
316 nssToken_NotifyCertsNotVisible
321 return nssTrustDomain_RemoveTokenCertsFromCache(tok
->trustDomain
, tok
);