Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / pk11wrap / dev3hack.c
blob52673eb4845076efc87be876310a12fa20d7576c
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.22 $ $Date: 2006/10/09 22:14:04 $";
39 #endif /* DEBUG */
41 #ifndef NSS_3_4_CODE
42 #define NSS_3_4_CODE
43 #endif /* NSS_3_4_CODE */
45 #ifndef PKIT_H
46 #include "pkit.h"
47 #endif /* PKIT_H */
49 #ifndef DEVM_H
50 #include "devm.h"
51 #endif /* DEVM_H */
53 #include "pki3hack.h"
54 #include "dev3hack.h"
55 #include "pkim.h"
57 #ifndef BASE_H
58 #include "base.h"
59 #endif /* BASE_H */
61 #include "pk11func.h"
62 #include "secmodti.h"
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 if (!rvSession) {
72 return NULL;
74 rvSession->handle = session;
75 rvSession->lock = lock;
76 rvSession->ownLock = PR_FALSE;
77 rvSession->isRW = rw;
78 return rvSession;
81 NSS_IMPLEMENT nssSession *
82 nssSlot_CreateSession
84 NSSSlot *slot,
85 NSSArena *arenaOpt,
86 PRBool readWrite
89 nssSession *rvSession;
90 rvSession = nss_ZNEW(arenaOpt, nssSession);
91 if (!rvSession) {
92 return (nssSession *)NULL;
94 if (readWrite) {
95 rvSession->handle = PK11_GetRWSession(slot->pk11slot);
96 if (rvSession->handle == CK_INVALID_HANDLE) {
97 nss_ZFreeIf(rvSession);
98 return NULL;
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;
119 return rvSession;
120 } else {
121 return NULL;
125 NSS_IMPLEMENT PRStatus
126 nssSession_Destroy
128 nssSession *s
131 CK_RV ckrv = CKR_OK;
132 if (s) {
133 if (s->isRW) {
134 PK11_RestoreROSession(s->slot->pk11slot, s->handle);
136 nss_ZFreeIf(s);
138 return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE;
141 static NSSSlot *
142 nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
144 NSSSlot *rvSlot;
145 NSSArena *arena;
146 arena = nssArena_Create();
147 if (!arena) {
148 return NULL;
150 rvSlot = nss_ZNEW(arena, NSSSlot);
151 if (!rvSlot) {
152 nssArena_Destroy(arena);
153 return NULL;
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;
164 return rvSlot;
167 NSS_IMPLEMENT NSSToken *
168 nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
170 NSSToken *rvToken;
171 NSSArena *arena;
172 arena = nssArena_Create();
173 if (!arena) {
174 return NULL;
176 rvToken = nss_ZNEW(arena, NSSToken);
177 if (!rvToken) {
178 nssArena_Destroy(arena);
179 return NULL;
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,
187 nss3slot->session,
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;
209 return rvToken;
212 NSS_IMPLEMENT void
213 nssToken_UpdateName(NSSToken *token)
215 if (!token) {
216 return;
218 token->base.name = nssUTF8_Duplicate(token->pk11slot->token_name,token->base.arena);
221 NSS_IMPLEMENT PRBool
222 nssSlot_IsPermanent
224 NSSSlot *slot
227 return slot->pk11slot->isPerm;
230 NSS_IMPLEMENT PRBool
231 nssSlot_IsFriendly
233 NSSSlot *slot
236 return PK11_IsFriendly(slot->pk11slot);
239 NSS_IMPLEMENT PRStatus
240 nssToken_Refresh(NSSToken *token)
242 PK11SlotInfo *nss3slot;
244 if (!token) {
245 return PR_SUCCESS;
247 nss3slot = token->pk11slot;
248 token->defaultSession = nssSession_ImportNSS3Session(token->slot->base.arena,
249 nss3slot->session,
250 nss3slot->sessionLock,
251 nss3slot->defRWSession);
252 return PR_SUCCESS;
255 NSS_IMPLEMENT PRStatus
256 nssSlot_Refresh
258 NSSSlot *slot
261 PK11SlotInfo *nss3slot = slot->pk11slot;
262 PRBool doit = PR_FALSE;
263 if (slot->token->base.name[0] == 0) {
264 doit = PR_TRUE;
266 if (PK11_InitToken(nss3slot, PR_FALSE) != SECSuccess) {
267 return PR_FAILURE;
269 if (doit) {
270 nssTrustDomain_UpdateCachedTokenCerts(slot->token->trustDomain,
271 slot->token);
273 return nssToken_Refresh(slot->token);
276 NSS_IMPLEMENT PRStatus
277 nssToken_GetTrustOrder
279 NSSToken *tok
282 PK11SlotInfo *slot;
283 SECMODModule *module;
284 slot = tok->pk11slot;
285 module = PK11_GetModule(slot);
286 return module->trustOrder;
289 NSS_IMPLEMENT PRBool
290 nssSlot_IsLoggedIn
292 NSSSlot *slot
295 if (!slot->pk11slot->needLogin) {
296 return PR_TRUE;
298 return PK11_IsLoggedIn(slot->pk11slot, NULL);
302 NSSTrustDomain *
303 nssToken_GetTrustDomain(NSSToken *token)
305 return token->trustDomain;
308 NSS_EXTERN PRStatus
309 nssTrustDomain_RemoveTokenCertsFromCache
311 NSSTrustDomain *td,
312 NSSToken *token
315 NSS_IMPLEMENT PRStatus
316 nssToken_NotifyCertsNotVisible
318 NSSToken *tok
321 return nssTrustDomain_RemoveTokenCertsFromCache(tok->trustDomain, tok);