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.21 $ $Date: 2005/01/20 02:25:48 $";
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);
71
rvSession->handle = session;
72
rvSession->lock = lock;
73
rvSession->ownLock = PR_FALSE;
78
NSS_IMPLEMENT nssSession *
86
nssSession *rvSession;
87
rvSession = nss_ZNEW(arenaOpt, nssSession);
89
return (nssSession *)NULL;
92
rvSession->handle = PK11_GetRWSession(slot->pk11slot);
93
if (rvSession->handle == CK_INVALID_HANDLE) {
94
nss_ZFreeIf(rvSession);
97
rvSession->isRW = PR_TRUE;
98
rvSession->slot = slot;
100
* The session doesn't need its own lock. Here's why.
101
* 1. If we are reusing the default RW session of the slot,
102
* the slot lock is already locked to protect the session.
103
* 2. If the module is not thread safe, the slot (or rather
104
* module) lock is already locked.
105
* 3. If the module is thread safe and we are using a new
106
* session, no higher-level lock has been locked and we
107
* would need a lock for the new session. However, the
108
* NSS_3_4_CODE usage of the session is that it is always
109
* used and destroyed within the same function and never
110
* shared with another thread.
111
* So the session is either already protected by another
112
* lock or only used by one thread.
114
rvSession->lock = NULL;
115
rvSession->ownLock = PR_FALSE;
122
NSS_IMPLEMENT PRStatus
131
PK11_RestoreROSession(s->slot->pk11slot, s->handle);
135
return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE;
139
nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
143
arena = nssArena_Create();
147
rvSlot = nss_ZNEW(arena, NSSSlot);
149
nssArena_Destroy(arena);
152
rvSlot->base.refCount = 1;
153
rvSlot->base.lock = PZ_NewLock(nssILockOther);
154
rvSlot->base.arena = arena;
155
rvSlot->pk11slot = nss3slot;
156
rvSlot->epv = nss3slot->functionList;
157
rvSlot->slotID = nss3slot->slotID;
158
/* Grab the slot name from the PKCS#11 fixed-length buffer */
159
rvSlot->base.name = nssUTF8_Duplicate(nss3slot->slot_name,td->arena);
160
rvSlot->lock = (nss3slot->isThreadSafe) ? NULL : nss3slot->sessionLock;
164
NSS_IMPLEMENT NSSToken *
165
nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
169
arena = nssArena_Create();
173
rvToken = nss_ZNEW(arena, NSSToken);
175
nssArena_Destroy(arena);
178
rvToken->base.refCount = 1;
179
rvToken->base.lock = PZ_NewLock(nssILockOther);
180
rvToken->base.arena = arena;
181
rvToken->pk11slot = nss3slot;
182
rvToken->epv = nss3slot->functionList;
183
rvToken->defaultSession = nssSession_ImportNSS3Session(td->arena,
185
nss3slot->sessionLock,
186
nss3slot->defRWSession);
187
/* The above test was used in 3.4, for this cache have it always on */
188
if (!PK11_IsInternal(nss3slot) && PK11_IsHW(nss3slot)) {
189
rvToken->cache = nssTokenObjectCache_Create(rvToken,
190
PR_TRUE, PR_TRUE, PR_TRUE);
191
if (!rvToken->cache) {
192
nssArena_Destroy(arena);
193
return (NSSToken *)NULL;
196
rvToken->trustDomain = td;
197
/* Grab the token name from the PKCS#11 fixed-length buffer */
198
rvToken->base.name = nssUTF8_Duplicate(nss3slot->token_name,td->arena);
199
rvToken->slot = nssSlot_CreateFromPK11SlotInfo(td, nss3slot);
200
rvToken->slot->token = rvToken;
201
rvToken->defaultSession->slot = rvToken->slot;
206
nssToken_UpdateName(NSSToken *token)
211
token->base.name = nssUTF8_Duplicate(token->pk11slot->token_name,token->base.arena);
220
return slot->pk11slot->isPerm;
229
return PK11_IsFriendly(slot->pk11slot);
232
NSS_IMPLEMENT PRStatus
233
nssToken_Refresh(NSSToken *token)
235
PK11SlotInfo *nss3slot;
240
nss3slot = token->pk11slot;
241
token->defaultSession = nssSession_ImportNSS3Session(token->slot->base.arena,
243
nss3slot->sessionLock,
244
nss3slot->defRWSession);
248
NSS_IMPLEMENT PRStatus
254
PK11SlotInfo *nss3slot = slot->pk11slot;
255
PRBool doit = PR_FALSE;
256
if (slot->token->base.name[0] == 0) {
259
if (PK11_InitToken(nss3slot, PR_FALSE) != SECSuccess) {
263
nssTrustDomain_UpdateCachedTokenCerts(slot->token->trustDomain,
266
return nssToken_Refresh(slot->token);
269
NSS_IMPLEMENT PRStatus
270
nssToken_GetTrustOrder
276
SECMODModule *module;
277
slot = tok->pk11slot;
278
module = PK11_GetModule(slot);
279
return module->trustOrder;
288
if (!slot->pk11slot->needLogin) {
291
return PK11_IsLoggedIn(slot->pk11slot, NULL);
296
nssToken_GetTrustDomain(NSSToken *token)
298
return token->trustDomain;
302
nssTrustDomain_RemoveTokenCertsFromCache
308
NSS_IMPLEMENT PRStatus
309
nssToken_NotifyCertsNotVisible
314
return nssTrustDomain_RemoveTokenCertsFromCache(tok->trustDomain, tok);