Import 1.9b4 NSS tag from cvs
[mozilla-nss.git] / security / nss / lib / pk11wrap / dev3hack.c
blob9ce547b5bf9bf46ad7b6c5662b93795a0d53b345
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
12 * License.
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.
21 * Contributor(s):
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 ***** */
37 #ifdef DEBUG
38 static const char CVS_ID[] = "@(#) $RCSfile: dev3hack.c,v $ $Revision: 1.23 $ $Date: 2007/11/16 05:29:26 $";
39 #endif /* DEBUG */
41 #ifndef PKIT_H
42 #include "pkit.h"
43 #endif /* PKIT_H */
45 #ifndef DEVM_H
46 #include "devm.h"
47 #endif /* DEVM_H */
49 #include "pki3hack.h"
50 #include "dev3hack.h"
51 #include "pkim.h"
53 #ifndef BASE_H
54 #include "base.h"
55 #endif /* BASE_H */
57 #include "pk11func.h"
58 #include "secmodti.h"
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);
67 if (!rvSession) {
68 return NULL;
70 rvSession->handle = session;
71 rvSession->lock = lock;
72 rvSession->ownLock = PR_FALSE;
73 rvSession->isRW = rw;
74 return rvSession;
77 NSS_IMPLEMENT nssSession *
78 nssSlot_CreateSession
80 NSSSlot *slot,
81 NSSArena *arenaOpt,
82 PRBool readWrite
85 nssSession *rvSession;
86 rvSession = nss_ZNEW(arenaOpt, nssSession);
87 if (!rvSession) {
88 return (nssSession *)NULL;
90 if (readWrite) {
91 rvSession->handle = PK11_GetRWSession(slot->pk11slot);
92 if (rvSession->handle == CK_INVALID_HANDLE) {
93 nss_ZFreeIf(rvSession);
94 return NULL;
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;
115 return rvSession;
116 } else {
117 return NULL;
121 NSS_IMPLEMENT PRStatus
122 nssSession_Destroy
124 nssSession *s
127 CK_RV ckrv = CKR_OK;
128 if (s) {
129 if (s->isRW) {
130 PK11_RestoreROSession(s->slot->pk11slot, s->handle);
132 nss_ZFreeIf(s);
134 return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE;
137 static NSSSlot *
138 nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
140 NSSSlot *rvSlot;
141 NSSArena *arena;
142 arena = nssArena_Create();
143 if (!arena) {
144 return NULL;
146 rvSlot = nss_ZNEW(arena, NSSSlot);
147 if (!rvSlot) {
148 nssArena_Destroy(arena);
149 return NULL;
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;
160 return rvSlot;
163 NSS_IMPLEMENT NSSToken *
164 nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
166 NSSToken *rvToken;
167 NSSArena *arena;
168 arena = nssArena_Create();
169 if (!arena) {
170 return NULL;
172 rvToken = nss_ZNEW(arena, NSSToken);
173 if (!rvToken) {
174 nssArena_Destroy(arena);
175 return NULL;
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,
183 nss3slot->session,
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;
205 return rvToken;
208 NSS_IMPLEMENT void
209 nssToken_UpdateName(NSSToken *token)
211 if (!token) {
212 return;
214 token->base.name = nssUTF8_Duplicate(token->pk11slot->token_name,token->base.arena);
217 NSS_IMPLEMENT PRBool
218 nssSlot_IsPermanent
220 NSSSlot *slot
223 return slot->pk11slot->isPerm;
226 NSS_IMPLEMENT PRBool
227 nssSlot_IsFriendly
229 NSSSlot *slot
232 return PK11_IsFriendly(slot->pk11slot);
235 NSS_IMPLEMENT PRStatus
236 nssToken_Refresh(NSSToken *token)
238 PK11SlotInfo *nss3slot;
240 if (!token) {
241 return PR_SUCCESS;
243 nss3slot = token->pk11slot;
244 token->defaultSession = nssSession_ImportNSS3Session(token->slot->base.arena,
245 nss3slot->session,
246 nss3slot->sessionLock,
247 nss3slot->defRWSession);
248 return PR_SUCCESS;
251 NSS_IMPLEMENT PRStatus
252 nssSlot_Refresh
254 NSSSlot *slot
257 PK11SlotInfo *nss3slot = slot->pk11slot;
258 PRBool doit = PR_FALSE;
259 if (slot->token->base.name[0] == 0) {
260 doit = PR_TRUE;
262 if (PK11_InitToken(nss3slot, PR_FALSE) != SECSuccess) {
263 return PR_FAILURE;
265 if (doit) {
266 nssTrustDomain_UpdateCachedTokenCerts(slot->token->trustDomain,
267 slot->token);
269 return nssToken_Refresh(slot->token);
272 NSS_IMPLEMENT PRStatus
273 nssToken_GetTrustOrder
275 NSSToken *tok
278 PK11SlotInfo *slot;
279 SECMODModule *module;
280 slot = tok->pk11slot;
281 module = PK11_GetModule(slot);
282 return module->trustOrder;
285 NSS_IMPLEMENT PRBool
286 nssSlot_IsLoggedIn
288 NSSSlot *slot
291 if (!slot->pk11slot->needLogin) {
292 return PR_TRUE;
294 return PK11_IsLoggedIn(slot->pk11slot, NULL);
298 NSSTrustDomain *
299 nssToken_GetTrustDomain(NSSToken *token)
301 return token->trustDomain;
304 NSS_EXTERN PRStatus
305 nssTrustDomain_RemoveTokenCertsFromCache
307 NSSTrustDomain *td,
308 NSSToken *token
311 NSS_IMPLEMENT PRStatus
312 nssToken_NotifyCertsNotVisible
314 NSSToken *tok
317 return nssTrustDomain_RemoveTokenCertsFromCache(tok->trustDomain, tok);