| OLD | NEW |
| (Empty) |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| 4 | |
| 5 #ifdef DEBUG | |
| 6 static const char CVS_ID[] = "@(#) $RCSfile: dev3hack.c,v $ $Revision: 1.27 $ $D
ate: 2012/04/25 14:50:04 $"; | |
| 7 #endif /* DEBUG */ | |
| 8 | |
| 9 #ifndef PKIT_H | |
| 10 #include "pkit.h" | |
| 11 #endif /* PKIT_H */ | |
| 12 | |
| 13 #ifndef DEVM_H | |
| 14 #include "devm.h" | |
| 15 #endif /* DEVM_H */ | |
| 16 | |
| 17 #include "pki3hack.h" | |
| 18 #include "dev3hack.h" | |
| 19 #include "pkim.h" | |
| 20 | |
| 21 #ifndef BASE_H | |
| 22 #include "base.h" | |
| 23 #endif /* BASE_H */ | |
| 24 | |
| 25 #include "pk11func.h" | |
| 26 #include "secmodti.h" | |
| 27 #include "secerr.h" | |
| 28 | |
| 29 NSS_IMPLEMENT nssSession * | |
| 30 nssSession_ImportNSS3Session(NSSArena *arenaOpt, | |
| 31 CK_SESSION_HANDLE session, | |
| 32 PZLock *lock, PRBool rw) | |
| 33 { | |
| 34 nssSession *rvSession = NULL; | |
| 35 if (session != CK_INVALID_SESSION) { | |
| 36 rvSession = nss_ZNEW(arenaOpt, nssSession); | |
| 37 if (rvSession) { | |
| 38 rvSession->handle = session; | |
| 39 rvSession->lock = lock; | |
| 40 rvSession->ownLock = PR_FALSE; | |
| 41 rvSession->isRW = rw; | |
| 42 } | |
| 43 } | |
| 44 return rvSession; | |
| 45 } | |
| 46 | |
| 47 NSS_IMPLEMENT nssSession * | |
| 48 nssSlot_CreateSession | |
| 49 ( | |
| 50 NSSSlot *slot, | |
| 51 NSSArena *arenaOpt, | |
| 52 PRBool readWrite | |
| 53 ) | |
| 54 { | |
| 55 nssSession *rvSession; | |
| 56 | |
| 57 if (!readWrite) { | |
| 58 /* nss3hack version only returns rw swssions */ | |
| 59 return NULL; | |
| 60 } | |
| 61 rvSession = nss_ZNEW(arenaOpt, nssSession); | |
| 62 if (!rvSession) { | |
| 63 return (nssSession *)NULL; | |
| 64 } | |
| 65 | |
| 66 rvSession->handle = PK11_GetRWSession(slot->pk11slot); | |
| 67 if (rvSession->handle == CK_INVALID_HANDLE) { | |
| 68 nss_ZFreeIf(rvSession); | |
| 69 return NULL; | |
| 70 } | |
| 71 rvSession->isRW = PR_TRUE; | |
| 72 rvSession->slot = slot; | |
| 73 /* | |
| 74 * The session doesn't need its own lock. Here's why. | |
| 75 * 1. If we are reusing the default RW session of the slot, | |
| 76 * the slot lock is already locked to protect the session. | |
| 77 * 2. If the module is not thread safe, the slot (or rather | |
| 78 * module) lock is already locked. | |
| 79 * 3. If the module is thread safe and we are using a new | |
| 80 * session, no higher-level lock has been locked and we | |
| 81 * would need a lock for the new session. However, the | |
| 82 * current usage of the session is that it is always | |
| 83 * used and destroyed within the same function and never | |
| 84 * shared with another thread. | |
| 85 * So the session is either already protected by another | |
| 86 * lock or only used by one thread. | |
| 87 */ | |
| 88 rvSession->lock = NULL; | |
| 89 rvSession->ownLock = PR_FALSE; | |
| 90 return rvSession; | |
| 91 } | |
| 92 | |
| 93 NSS_IMPLEMENT PRStatus | |
| 94 nssSession_Destroy | |
| 95 ( | |
| 96 nssSession *s | |
| 97 ) | |
| 98 { | |
| 99 CK_RV ckrv = CKR_OK; | |
| 100 if (s) { | |
| 101 if (s->isRW) { | |
| 102 PK11_RestoreROSession(s->slot->pk11slot, s->handle); | |
| 103 } | |
| 104 nss_ZFreeIf(s); | |
| 105 } | |
| 106 return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE; | |
| 107 } | |
| 108 | |
| 109 static NSSSlot * | |
| 110 nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot) | |
| 111 { | |
| 112 NSSSlot *rvSlot; | |
| 113 NSSArena *arena; | |
| 114 arena = nssArena_Create(); | |
| 115 if (!arena) { | |
| 116 return NULL; | |
| 117 } | |
| 118 rvSlot = nss_ZNEW(arena, NSSSlot); | |
| 119 if (!rvSlot) { | |
| 120 nssArena_Destroy(arena); | |
| 121 return NULL; | |
| 122 } | |
| 123 rvSlot->base.refCount = 1; | |
| 124 rvSlot->base.lock = PZ_NewLock(nssILockOther); | |
| 125 rvSlot->base.arena = arena; | |
| 126 rvSlot->pk11slot = nss3slot; | |
| 127 rvSlot->epv = nss3slot->functionList; | |
| 128 rvSlot->slotID = nss3slot->slotID; | |
| 129 /* Grab the slot name from the PKCS#11 fixed-length buffer */ | |
| 130 rvSlot->base.name = nssUTF8_Duplicate(nss3slot->slot_name,td->arena); | |
| 131 rvSlot->lock = (nss3slot->isThreadSafe) ? NULL : nss3slot->sessionLock; | |
| 132 return rvSlot; | |
| 133 } | |
| 134 | |
| 135 NSSToken * | |
| 136 nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot) | |
| 137 { | |
| 138 NSSToken *rvToken; | |
| 139 NSSArena *arena; | |
| 140 | |
| 141 /* Don't create a token object for a disabled slot */ | |
| 142 if (nss3slot->disabled) { | |
| 143 PORT_SetError(SEC_ERROR_NO_TOKEN); | |
| 144 return NULL; | |
| 145 } | |
| 146 arena = nssArena_Create(); | |
| 147 if (!arena) { | |
| 148 return NULL; | |
| 149 } | |
| 150 rvToken = nss_ZNEW(arena, NSSToken); | |
| 151 if (!rvToken) { | |
| 152 nssArena_Destroy(arena); | |
| 153 return NULL; | |
| 154 } | |
| 155 rvToken->base.refCount = 1; | |
| 156 rvToken->base.lock = PZ_NewLock(nssILockOther); | |
| 157 if (!rvToken->base.lock) { | |
| 158 nssArena_Destroy(arena); | |
| 159 return NULL; | |
| 160 } | |
| 161 rvToken->base.arena = arena; | |
| 162 rvToken->pk11slot = nss3slot; | |
| 163 rvToken->epv = nss3slot->functionList; | |
| 164 rvToken->defaultSession = nssSession_ImportNSS3Session(td->arena, | |
| 165 nss3slot->session, | |
| 166 nss3slot->sessionLock, | |
| 167 nss3slot->defRWSession); | |
| 168 #if 0 /* we should do this instead of blindly continuing. */ | |
| 169 if (!rvToken->defaultSession) { | |
| 170 PORT_SetError(SEC_ERROR_NO_TOKEN); | |
| 171 goto loser; | |
| 172 } | |
| 173 #endif | |
| 174 if (!PK11_IsInternal(nss3slot) && PK11_IsHW(nss3slot)) { | |
| 175 rvToken->cache = nssTokenObjectCache_Create(rvToken, | |
| 176 PR_TRUE, PR_TRUE, PR_TRUE); | |
| 177 if (!rvToken->cache) | |
| 178 goto loser; | |
| 179 } | |
| 180 rvToken->trustDomain = td; | |
| 181 /* Grab the token name from the PKCS#11 fixed-length buffer */ | |
| 182 rvToken->base.name = nssUTF8_Duplicate(nss3slot->token_name,td->arena); | |
| 183 rvToken->slot = nssSlot_CreateFromPK11SlotInfo(td, nss3slot); | |
| 184 if (!rvToken->slot) { | |
| 185 goto loser; | |
| 186 } | |
| 187 rvToken->slot->token = rvToken; | |
| 188 if (rvToken->defaultSession) | |
| 189 rvToken->defaultSession->slot = rvToken->slot; | |
| 190 return rvToken; | |
| 191 loser: | |
| 192 PZ_DestroyLock(rvToken->base.lock); | |
| 193 nssArena_Destroy(arena); | |
| 194 return NULL; | |
| 195 } | |
| 196 | |
| 197 NSS_IMPLEMENT void | |
| 198 nssToken_UpdateName(NSSToken *token) | |
| 199 { | |
| 200 if (!token) { | |
| 201 return; | |
| 202 } | |
| 203 token->base.name = nssUTF8_Duplicate(token->pk11slot->token_name,token->base
.arena); | |
| 204 } | |
| 205 | |
| 206 NSS_IMPLEMENT PRBool | |
| 207 nssSlot_IsPermanent | |
| 208 ( | |
| 209 NSSSlot *slot | |
| 210 ) | |
| 211 { | |
| 212 return slot->pk11slot->isPerm; | |
| 213 } | |
| 214 | |
| 215 NSS_IMPLEMENT PRBool | |
| 216 nssSlot_IsFriendly | |
| 217 ( | |
| 218 NSSSlot *slot | |
| 219 ) | |
| 220 { | |
| 221 return PK11_IsFriendly(slot->pk11slot); | |
| 222 } | |
| 223 | |
| 224 NSS_IMPLEMENT PRStatus | |
| 225 nssToken_Refresh(NSSToken *token) | |
| 226 { | |
| 227 PK11SlotInfo *nss3slot; | |
| 228 | |
| 229 if (!token) { | |
| 230 return PR_SUCCESS; | |
| 231 } | |
| 232 nss3slot = token->pk11slot; | |
| 233 token->defaultSession = | |
| 234 nssSession_ImportNSS3Session(token->slot->base.arena, | |
| 235 nss3slot->session, | |
| 236 nss3slot->sessionLock, | |
| 237 nss3slot->defRWSession); | |
| 238 return token->defaultSession ? PR_SUCCESS : PR_FAILURE; | |
| 239 } | |
| 240 | |
| 241 NSS_IMPLEMENT PRStatus | |
| 242 nssSlot_Refresh | |
| 243 ( | |
| 244 NSSSlot *slot | |
| 245 ) | |
| 246 { | |
| 247 PK11SlotInfo *nss3slot = slot->pk11slot; | |
| 248 PRBool doit = PR_FALSE; | |
| 249 if (slot->token && slot->token->base.name[0] == 0) { | |
| 250 doit = PR_TRUE; | |
| 251 } | |
| 252 if (PK11_InitToken(nss3slot, PR_FALSE) != SECSuccess) { | |
| 253 return PR_FAILURE; | |
| 254 } | |
| 255 if (doit) { | |
| 256 nssTrustDomain_UpdateCachedTokenCerts(slot->token->trustDomain, | |
| 257 slot->token); | |
| 258 } | |
| 259 return nssToken_Refresh(slot->token); | |
| 260 } | |
| 261 | |
| 262 NSS_IMPLEMENT PRStatus | |
| 263 nssToken_GetTrustOrder | |
| 264 ( | |
| 265 NSSToken *tok | |
| 266 ) | |
| 267 { | |
| 268 PK11SlotInfo *slot; | |
| 269 SECMODModule *module; | |
| 270 slot = tok->pk11slot; | |
| 271 module = PK11_GetModule(slot); | |
| 272 return module->trustOrder; | |
| 273 } | |
| 274 | |
| 275 NSS_IMPLEMENT PRBool | |
| 276 nssSlot_IsLoggedIn | |
| 277 ( | |
| 278 NSSSlot *slot | |
| 279 ) | |
| 280 { | |
| 281 if (!slot->pk11slot->needLogin) { | |
| 282 return PR_TRUE; | |
| 283 } | |
| 284 return PK11_IsLoggedIn(slot->pk11slot, NULL); | |
| 285 } | |
| 286 | |
| 287 | |
| 288 NSSTrustDomain * | |
| 289 nssToken_GetTrustDomain(NSSToken *token) | |
| 290 { | |
| 291 return token->trustDomain; | |
| 292 } | |
| 293 | |
| 294 NSS_EXTERN PRStatus | |
| 295 nssTrustDomain_RemoveTokenCertsFromCache | |
| 296 ( | |
| 297 NSSTrustDomain *td, | |
| 298 NSSToken *token | |
| 299 ); | |
| 300 | |
| 301 NSS_IMPLEMENT PRStatus | |
| 302 nssToken_NotifyCertsNotVisible | |
| 303 ( | |
| 304 NSSToken *tok | |
| 305 ) | |
| 306 { | |
| 307 return nssTrustDomain_RemoveTokenCertsFromCache(tok->trustDomain, tok); | |
| 308 } | |
| 309 | |
| OLD | NEW |