/src/nss/lib/pki/pkistore.c
| Line | Count | Source (jump to first uncovered line) | 
| 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 |  | #ifndef PKIM_H | 
| 6 |  | #include "pkim.h" | 
| 7 |  | #endif /* PKIM_H */ | 
| 8 |  |  | 
| 9 |  | #ifndef PKI_H | 
| 10 |  | #include "pki.h" | 
| 11 |  | #endif /* PKI_H */ | 
| 12 |  |  | 
| 13 |  | #ifndef NSSPKI_H | 
| 14 |  | #include "nsspki.h" | 
| 15 |  | #endif /* NSSPKI_H */ | 
| 16 |  |  | 
| 17 |  | #ifndef BASE_H | 
| 18 |  | #include "base.h" | 
| 19 |  | #endif /* BASE_H */ | 
| 20 |  |  | 
| 21 |  | #ifndef PKISTORE_H | 
| 22 |  | #include "pkistore.h" | 
| 23 |  | #endif /* PKISTORE_H */ | 
| 24 |  |  | 
| 25 |  | #include "cert.h" | 
| 26 |  | #include "pki3hack.h" | 
| 27 |  |  | 
| 28 |  | #include "prbit.h" | 
| 29 |  |  | 
| 30 |  | /* | 
| 31 |  |  * Certificate Store | 
| 32 |  |  * | 
| 33 |  |  * This differs from the cache in that it is a true storage facility.  Items | 
| 34 |  |  * stay in until they are explicitly removed.  It is only used by crypto | 
| 35 |  |  * contexts at this time, but may be more generally useful... | 
| 36 |  |  * | 
| 37 |  |  */ | 
| 38 |  |  | 
| 39 |  | struct nssCertificateStoreStr { | 
| 40 |  |     PRBool i_alloced_arena; | 
| 41 |  |     NSSArena *arena; | 
| 42 |  |     PZLock *lock; | 
| 43 |  |     nssHash *subject; | 
| 44 |  |     nssHash *issuer_and_serial; | 
| 45 |  | }; | 
| 46 |  |  | 
| 47 |  | typedef struct certificate_hash_entry_str certificate_hash_entry; | 
| 48 |  |  | 
| 49 |  | struct certificate_hash_entry_str { | 
| 50 |  |     NSSCertificate *cert; | 
| 51 |  |     NSSTrust *trust; | 
| 52 |  |     nssSMIMEProfile *profile; | 
| 53 |  | }; | 
| 54 |  |  | 
| 55 |  | /* forward static declarations */ | 
| 56 |  | static NSSCertificate * | 
| 57 |  | nssCertStore_FindCertByIssuerAndSerialNumberLocked( | 
| 58 |  |     nssCertificateStore *store, | 
| 59 |  |     NSSDER *issuer, | 
| 60 |  |     NSSDER *serial); | 
| 61 |  |  | 
| 62 |  | NSS_IMPLEMENT nssCertificateStore * | 
| 63 |  | nssCertificateStore_Create(NSSArena *arenaOpt) | 
| 64 | 1 | { | 
| 65 | 1 |     NSSArena *arena; | 
| 66 | 1 |     nssCertificateStore *store; | 
| 67 | 1 |     PRBool i_alloced_arena; | 
| 68 | 1 |     if (arenaOpt) { | 
| 69 | 1 |         arena = arenaOpt; | 
| 70 | 1 |         i_alloced_arena = PR_FALSE; | 
| 71 | 1 |     } else { | 
| 72 | 0 |         arena = nssArena_Create(); | 
| 73 | 0 |         if (!arena) { | 
| 74 | 0 |             return NULL; | 
| 75 | 0 |         } | 
| 76 | 0 |         i_alloced_arena = PR_TRUE; | 
| 77 | 0 |     } | 
| 78 | 1 |     store = nss_ZNEW(arena, nssCertificateStore); | 
| 79 | 1 |     if (!store) { | 
| 80 | 0 |         goto loser; | 
| 81 | 0 |     } | 
| 82 | 1 |     store->lock = PZ_NewLock(nssILockOther); | 
| 83 | 1 |     if (!store->lock) { | 
| 84 | 0 |         goto loser; | 
| 85 | 0 |     } | 
| 86 |  |     /* Create the issuer/serial --> {cert, trust, S/MIME profile } hash */ | 
| 87 | 1 |     store->issuer_and_serial = nssHash_CreateCertificate(arena, 0); | 
| 88 | 1 |     if (!store->issuer_and_serial) { | 
| 89 | 0 |         goto loser; | 
| 90 | 0 |     } | 
| 91 |  |     /* Create the subject DER --> subject list hash */ | 
| 92 | 1 |     store->subject = nssHash_CreateItem(arena, 0); | 
| 93 | 1 |     if (!store->subject) { | 
| 94 | 0 |         goto loser; | 
| 95 | 0 |     } | 
| 96 | 1 |     store->arena = arena; | 
| 97 | 1 |     store->i_alloced_arena = i_alloced_arena; | 
| 98 | 1 |     return store; | 
| 99 | 0 | loser: | 
| 100 | 0 |     if (store) { | 
| 101 | 0 |         if (store->lock) { | 
| 102 | 0 |             PZ_DestroyLock(store->lock); | 
| 103 | 0 |         } | 
| 104 | 0 |         if (store->issuer_and_serial) { | 
| 105 | 0 |             nssHash_Destroy(store->issuer_and_serial); | 
| 106 | 0 |         } | 
| 107 | 0 |         if (store->subject) { | 
| 108 | 0 |             nssHash_Destroy(store->subject); | 
| 109 | 0 |         } | 
| 110 | 0 |     } | 
| 111 | 0 |     if (i_alloced_arena) { | 
| 112 | 0 |         nssArena_Destroy(arena); | 
| 113 | 0 |     } | 
| 114 | 0 |     return NULL; | 
| 115 | 1 | } | 
| 116 |  |  | 
| 117 |  | extern const NSSError NSS_ERROR_BUSY; | 
| 118 |  |  | 
| 119 |  | NSS_IMPLEMENT PRStatus | 
| 120 |  | nssCertificateStore_Destroy(nssCertificateStore *store) | 
| 121 | 1 | { | 
| 122 | 1 |     if (nssHash_Count(store->issuer_and_serial) > 0) { | 
| 123 | 0 |         nss_SetError(NSS_ERROR_BUSY); | 
| 124 | 0 |         return PR_FAILURE; | 
| 125 | 0 |     } | 
| 126 | 1 |     PZ_DestroyLock(store->lock); | 
| 127 | 1 |     nssHash_Destroy(store->issuer_and_serial); | 
| 128 | 1 |     nssHash_Destroy(store->subject); | 
| 129 | 1 |     if (store->i_alloced_arena) { | 
| 130 | 0 |         nssArena_Destroy(store->arena); | 
| 131 | 1 |     } else { | 
| 132 | 1 |         nss_ZFreeIf(store); | 
| 133 | 1 |     } | 
| 134 | 1 |     return PR_SUCCESS; | 
| 135 | 1 | } | 
| 136 |  |  | 
| 137 |  | static PRStatus | 
| 138 |  | add_certificate_entry( | 
| 139 |  |     nssCertificateStore *store, | 
| 140 |  |     NSSCertificate *cert) | 
| 141 | 6 | { | 
| 142 | 6 |     PRStatus nssrv; | 
| 143 | 6 |     certificate_hash_entry *entry; | 
| 144 | 6 |     entry = nss_ZNEW(cert->object.arena, certificate_hash_entry); | 
| 145 | 6 |     if (!entry) { | 
| 146 | 0 |         return PR_FAILURE; | 
| 147 | 0 |     } | 
| 148 | 6 |     entry->cert = cert; | 
| 149 | 6 |     nssrv = nssHash_Add(store->issuer_and_serial, cert, entry); | 
| 150 | 6 |     if (nssrv != PR_SUCCESS) { | 
| 151 | 0 |         nss_ZFreeIf(entry); | 
| 152 | 0 |     } | 
| 153 | 6 |     return nssrv; | 
| 154 | 6 | } | 
| 155 |  |  | 
| 156 |  | static PRStatus | 
| 157 |  | add_subject_entry( | 
| 158 |  |     nssCertificateStore *store, | 
| 159 |  |     NSSCertificate *cert) | 
| 160 | 6 | { | 
| 161 | 6 |     PRStatus nssrv; | 
| 162 | 6 |     nssList *subjectList; | 
| 163 | 6 |     subjectList = (nssList *)nssHash_Lookup(store->subject, &cert->subject); | 
| 164 | 6 |     if (subjectList) { | 
| 165 |  |         /* The subject is already in, add this cert to the list */ | 
| 166 | 1 |         nssrv = nssList_AddUnique(subjectList, cert); | 
| 167 | 5 |     } else { | 
| 168 |  |         /* Create a new subject list for the subject */ | 
| 169 | 5 |         subjectList = nssList_Create(NULL, PR_FALSE); | 
| 170 | 5 |         if (!subjectList) { | 
| 171 | 0 |             return PR_FAILURE; | 
| 172 | 0 |         } | 
| 173 | 5 |         nssList_SetSortFunction(subjectList, nssCertificate_SubjectListSort); | 
| 174 |  |         /* Add the cert entry to this list of subjects */ | 
| 175 | 5 |         nssrv = nssList_Add(subjectList, cert); | 
| 176 | 5 |         if (nssrv != PR_SUCCESS) { | 
| 177 | 0 |             return nssrv; | 
| 178 | 0 |         } | 
| 179 |  |         /* Add the subject list to the cache */ | 
| 180 | 5 |         nssrv = nssHash_Add(store->subject, &cert->subject, subjectList); | 
| 181 | 5 |     } | 
| 182 | 6 |     return nssrv; | 
| 183 | 6 | } | 
| 184 |  |  | 
| 185 |  | /* declared below */ | 
| 186 |  | static void | 
| 187 |  | remove_certificate_entry( | 
| 188 |  |     nssCertificateStore *store, | 
| 189 |  |     NSSCertificate *cert); | 
| 190 |  |  | 
| 191 |  | /* Caller must hold store->lock */ | 
| 192 |  | static PRStatus | 
| 193 |  | nssCertificateStore_AddLocked( | 
| 194 |  |     nssCertificateStore *store, | 
| 195 |  |     NSSCertificate *cert) | 
| 196 | 6 | { | 
| 197 | 6 |     PRStatus nssrv = add_certificate_entry(store, cert); | 
| 198 | 6 |     if (nssrv == PR_SUCCESS) { | 
| 199 | 6 |         nssrv = add_subject_entry(store, cert); | 
| 200 | 6 |         if (nssrv == PR_FAILURE) { | 
| 201 | 0 |             remove_certificate_entry(store, cert); | 
| 202 | 0 |         } | 
| 203 | 6 |     } | 
| 204 | 6 |     return nssrv; | 
| 205 | 6 | } | 
| 206 |  |  | 
| 207 |  | NSS_IMPLEMENT NSSCertificate * | 
| 208 |  | nssCertificateStore_FindOrAdd( | 
| 209 |  |     nssCertificateStore *store, | 
| 210 |  |     NSSCertificate *c) | 
| 211 | 6 | { | 
| 212 | 6 |     PRStatus nssrv; | 
| 213 | 6 |     NSSCertificate *rvCert = NULL; | 
| 214 |  |  | 
| 215 | 6 |     PZ_Lock(store->lock); | 
| 216 | 6 |     rvCert = nssCertStore_FindCertByIssuerAndSerialNumberLocked( | 
| 217 | 6 |         store, &c->issuer, &c->serial); | 
| 218 | 6 |     if (!rvCert) { | 
| 219 | 6 |         nssrv = nssCertificateStore_AddLocked(store, c); | 
| 220 | 6 |         if (PR_SUCCESS == nssrv) { | 
| 221 | 6 |             rvCert = nssCertificate_AddRef(c); | 
| 222 | 6 |         } | 
| 223 | 6 |     } | 
| 224 | 6 |     PZ_Unlock(store->lock); | 
| 225 | 6 |     return rvCert; | 
| 226 | 6 | } | 
| 227 |  |  | 
| 228 |  | static void | 
| 229 |  | remove_certificate_entry( | 
| 230 |  |     nssCertificateStore *store, | 
| 231 |  |     NSSCertificate *cert) | 
| 232 | 6 | { | 
| 233 | 6 |     certificate_hash_entry *entry; | 
| 234 | 6 |     entry = (certificate_hash_entry *) | 
| 235 | 6 |         nssHash_Lookup(store->issuer_and_serial, cert); | 
| 236 | 6 |     if (entry) { | 
| 237 | 6 |         nssHash_Remove(store->issuer_and_serial, cert); | 
| 238 | 6 |         if (entry->trust) { | 
| 239 | 0 |             nssTrust_Destroy(entry->trust); | 
| 240 | 0 |         } | 
| 241 | 6 |         if (entry->profile) { | 
| 242 | 0 |             nssSMIMEProfile_Destroy(entry->profile); | 
| 243 | 0 |         } | 
| 244 | 6 |         nss_ZFreeIf(entry); | 
| 245 | 6 |     } | 
| 246 | 6 | } | 
| 247 |  |  | 
| 248 |  | static void | 
| 249 |  | remove_subject_entry( | 
| 250 |  |     nssCertificateStore *store, | 
| 251 |  |     NSSCertificate *cert) | 
| 252 | 6 | { | 
| 253 | 6 |     nssList *subjectList; | 
| 254 |  |     /* Get the subject list for the cert's subject */ | 
| 255 | 6 |     subjectList = (nssList *)nssHash_Lookup(store->subject, &cert->subject); | 
| 256 | 6 |     if (subjectList) { | 
| 257 |  |         /* Remove the cert from the subject hash */ | 
| 258 | 6 |         nssList_Remove(subjectList, cert); | 
| 259 | 6 |         nssHash_Remove(store->subject, &cert->subject); | 
| 260 | 6 |         if (nssList_Count(subjectList) == 0) { | 
| 261 | 5 |             nssList_Destroy(subjectList); | 
| 262 | 5 |         } else { | 
| 263 |  |             /* The cert being released may have keyed the subject entry. | 
| 264 |  |              * Since there are still subject certs around, get another and | 
| 265 |  |              * rekey the entry just in case. | 
| 266 |  |              */ | 
| 267 | 1 |             NSSCertificate *subjectCert; | 
| 268 | 1 |             (void)nssList_GetArray(subjectList, (void **)&subjectCert, 1); | 
| 269 | 1 |             nssHash_Add(store->subject, &subjectCert->subject, subjectList); | 
| 270 | 1 |         } | 
| 271 | 6 |     } | 
| 272 | 6 | } | 
| 273 |  |  | 
| 274 |  | NSS_IMPLEMENT void | 
| 275 |  | nssCertificateStore_RemoveCertLOCKED( | 
| 276 |  |     nssCertificateStore *store, | 
| 277 |  |     NSSCertificate *cert) | 
| 278 | 6 | { | 
| 279 | 6 |     certificate_hash_entry *entry; | 
| 280 | 6 |     entry = (certificate_hash_entry *) | 
| 281 | 6 |         nssHash_Lookup(store->issuer_and_serial, cert); | 
| 282 | 6 |     if (entry && entry->cert == cert) { | 
| 283 | 6 |         remove_certificate_entry(store, cert); | 
| 284 | 6 |         remove_subject_entry(store, cert); | 
| 285 | 6 |     } | 
| 286 | 6 | } | 
| 287 |  |  | 
| 288 |  | NSS_IMPLEMENT void | 
| 289 |  | nssCertificateStore_Lock(nssCertificateStore *store, nssCertificateStoreTrace *out) | 
| 290 | 18.6k | { | 
| 291 | 18.6k | #ifdef DEBUG | 
| 292 | 18.6k |     PORT_Assert(out); | 
| 293 | 18.6k |     out->store = store; | 
| 294 | 18.6k |     out->lock = store->lock; | 
| 295 | 18.6k |     out->locked = PR_TRUE; | 
| 296 | 18.6k |     PZ_Lock(out->lock); | 
| 297 |  | #else | 
| 298 |  |     PZ_Lock(store->lock); | 
| 299 |  | #endif | 
| 300 | 18.6k | } | 
| 301 |  |  | 
| 302 |  | NSS_IMPLEMENT void | 
| 303 |  | nssCertificateStore_Unlock( | 
| 304 |  |     nssCertificateStore *store, const nssCertificateStoreTrace *in, | 
| 305 |  |     nssCertificateStoreTrace *out) | 
| 306 | 18.6k | { | 
| 307 | 18.6k | #ifdef DEBUG | 
| 308 | 18.6k |     PORT_Assert(in); | 
| 309 | 18.6k |     PORT_Assert(out); | 
| 310 | 18.6k |     out->store = store; | 
| 311 | 18.6k |     out->lock = store->lock; | 
| 312 | 18.6k |     PORT_Assert(!out->locked); | 
| 313 | 18.6k |     out->unlocked = PR_TRUE; | 
| 314 |  |  | 
| 315 | 18.6k |     PORT_Assert(in->store == out->store); | 
| 316 | 18.6k |     PORT_Assert(in->lock == out->lock); | 
| 317 | 18.6k |     PORT_Assert(in->locked); | 
| 318 | 18.6k |     PORT_Assert(!in->unlocked); | 
| 319 |  |  | 
| 320 | 18.6k |     PZ_Unlock(out->lock); | 
| 321 |  | #else | 
| 322 |  |     PZ_Unlock(store->lock); | 
| 323 |  | #endif | 
| 324 | 18.6k | } | 
| 325 |  |  | 
| 326 |  | static NSSCertificate ** | 
| 327 |  | get_array_from_list( | 
| 328 |  |     nssList *certList, | 
| 329 |  |     NSSCertificate *rvOpt[], | 
| 330 |  |     PRUint32 maximumOpt, | 
| 331 |  |     NSSArena *arenaOpt) | 
| 332 | 0 | { | 
| 333 | 0 |     PRUint32 count; | 
| 334 | 0 |     NSSCertificate **rvArray = NULL; | 
| 335 | 0 |     count = nssList_Count(certList); | 
| 336 | 0 |     if (count == 0) { | 
| 337 | 0 |         return NULL; | 
| 338 | 0 |     } | 
| 339 | 0 |     if (maximumOpt > 0) { | 
| 340 | 0 |         count = PR_MIN(maximumOpt, count); | 
| 341 | 0 |     } | 
| 342 | 0 |     if (rvOpt) { | 
| 343 | 0 |         nssList_GetArray(certList, (void **)rvOpt, count); | 
| 344 | 0 |     } else { | 
| 345 | 0 |         rvArray = nss_ZNEWARRAY(arenaOpt, NSSCertificate *, count + 1); | 
| 346 | 0 |         if (rvArray) { | 
| 347 | 0 |             nssList_GetArray(certList, (void **)rvArray, count); | 
| 348 | 0 |         } | 
| 349 | 0 |     } | 
| 350 | 0 |     return rvArray; | 
| 351 | 0 | } | 
| 352 |  |  | 
| 353 |  | NSS_IMPLEMENT NSSCertificate ** | 
| 354 |  | nssCertificateStore_FindCertificatesBySubject( | 
| 355 |  |     nssCertificateStore *store, | 
| 356 |  |     NSSDER *subject, | 
| 357 |  |     NSSCertificate *rvOpt[], | 
| 358 |  |     PRUint32 maximumOpt, | 
| 359 |  |     NSSArena *arenaOpt) | 
| 360 | 0 | { | 
| 361 | 0 |     NSSCertificate **rvArray = NULL; | 
| 362 | 0 |     nssList *subjectList; | 
| 363 | 0 |     PZ_Lock(store->lock); | 
| 364 | 0 |     subjectList = (nssList *)nssHash_Lookup(store->subject, subject); | 
| 365 | 0 |     if (subjectList) { | 
| 366 | 0 |         nssCertificateList_AddReferences(subjectList); | 
| 367 | 0 |         rvArray = get_array_from_list(subjectList, | 
| 368 | 0 |                                       rvOpt, maximumOpt, arenaOpt); | 
| 369 | 0 |     } | 
| 370 | 0 |     PZ_Unlock(store->lock); | 
| 371 | 0 |     return rvArray; | 
| 372 | 0 | } | 
| 373 |  |  | 
| 374 |  | /* Because only subject indexing is implemented, all other lookups require | 
| 375 |  |  * full traversal (unfortunately, PLHashTable doesn't allow you to exit | 
| 376 |  |  * early from the enumeration).  The assumptions are that 1) lookups by | 
| 377 |  |  * fields other than subject will be rare, and 2) the hash will not have | 
| 378 |  |  * a large number of entries.  These assumptions will be tested. | 
| 379 |  |  * | 
| 380 |  |  * XXX | 
| 381 |  |  * For NSS 3.4, it is worth consideration to do all forms of indexing, | 
| 382 |  |  * because the only crypto context is global and persistent. | 
| 383 |  |  */ | 
| 384 |  |  | 
| 385 |  | struct nickname_template_str { | 
| 386 |  |     NSSUTF8 *nickname; | 
| 387 |  |     nssList *subjectList; | 
| 388 |  | }; | 
| 389 |  |  | 
| 390 |  | static void | 
| 391 |  | match_nickname(const void *k, void *v, void *a) | 
| 392 | 0 | { | 
| 393 | 0 |     PRStatus nssrv; | 
| 394 | 0 |     NSSCertificate *c; | 
| 395 | 0 |     NSSUTF8 *nickname; | 
| 396 | 0 |     nssList *subjectList = (nssList *)v; | 
| 397 | 0 |     struct nickname_template_str *nt = (struct nickname_template_str *)a; | 
| 398 | 0 |     nssrv = nssList_GetArray(subjectList, (void **)&c, 1); | 
| 399 | 0 |     nickname = nssCertificate_GetNickname(c, NULL); | 
| 400 | 0 |     if (nssrv == PR_SUCCESS && nickname && | 
| 401 | 0 |         nssUTF8_Equal(nickname, nt->nickname, &nssrv)) { | 
| 402 | 0 |         nt->subjectList = subjectList; | 
| 403 | 0 |     } | 
| 404 | 0 |     nss_ZFreeIf(nickname); | 
| 405 | 0 | } | 
| 406 |  |  | 
| 407 |  | /* | 
| 408 |  |  * Find all cached certs with this label. | 
| 409 |  |  */ | 
| 410 |  | NSS_IMPLEMENT NSSCertificate ** | 
| 411 |  | nssCertificateStore_FindCertificatesByNickname( | 
| 412 |  |     nssCertificateStore *store, | 
| 413 |  |     const NSSUTF8 *nickname, | 
| 414 |  |     NSSCertificate *rvOpt[], | 
| 415 |  |     PRUint32 maximumOpt, | 
| 416 |  |     NSSArena *arenaOpt) | 
| 417 | 0 | { | 
| 418 | 0 |     NSSCertificate **rvArray = NULL; | 
| 419 | 0 |     struct nickname_template_str nt; | 
| 420 | 0 |     nt.nickname = (char *)nickname; | 
| 421 | 0 |     nt.subjectList = NULL; | 
| 422 | 0 |     PZ_Lock(store->lock); | 
| 423 | 0 |     nssHash_Iterate(store->subject, match_nickname, &nt); | 
| 424 | 0 |     if (nt.subjectList) { | 
| 425 | 0 |         nssCertificateList_AddReferences(nt.subjectList); | 
| 426 | 0 |         rvArray = get_array_from_list(nt.subjectList, | 
| 427 | 0 |                                       rvOpt, maximumOpt, arenaOpt); | 
| 428 | 0 |     } | 
| 429 | 0 |     PZ_Unlock(store->lock); | 
| 430 | 0 |     return rvArray; | 
| 431 | 0 | } | 
| 432 |  |  | 
| 433 |  | struct email_template_str { | 
| 434 |  |     NSSASCII7 *email; | 
| 435 |  |     nssList *emailList; | 
| 436 |  | }; | 
| 437 |  |  | 
| 438 |  | static void | 
| 439 |  | match_email(const void *k, void *v, void *a) | 
| 440 | 0 | { | 
| 441 | 0 |     PRStatus nssrv; | 
| 442 | 0 |     NSSCertificate *c; | 
| 443 | 0 |     nssList *subjectList = (nssList *)v; | 
| 444 | 0 |     struct email_template_str *et = (struct email_template_str *)a; | 
| 445 | 0 |     nssrv = nssList_GetArray(subjectList, (void **)&c, 1); | 
| 446 | 0 |     if (nssrv == PR_SUCCESS && | 
| 447 | 0 |         nssUTF8_Equal(c->email, et->email, &nssrv)) { | 
| 448 | 0 |         nssListIterator *iter = nssList_CreateIterator(subjectList); | 
| 449 | 0 |         if (iter) { | 
| 450 | 0 |             for (c = (NSSCertificate *)nssListIterator_Start(iter); | 
| 451 | 0 |                  c != (NSSCertificate *)NULL; | 
| 452 | 0 |                  c = (NSSCertificate *)nssListIterator_Next(iter)) { | 
| 453 | 0 |                 nssList_Add(et->emailList, c); | 
| 454 | 0 |             } | 
| 455 | 0 |             nssListIterator_Finish(iter); | 
| 456 | 0 |             nssListIterator_Destroy(iter); | 
| 457 | 0 |         } | 
| 458 | 0 |     } | 
| 459 | 0 | } | 
| 460 |  |  | 
| 461 |  | /* | 
| 462 |  |  * Find all cached certs with this email address. | 
| 463 |  |  */ | 
| 464 |  | NSS_IMPLEMENT NSSCertificate ** | 
| 465 |  | nssCertificateStore_FindCertificatesByEmail( | 
| 466 |  |     nssCertificateStore *store, | 
| 467 |  |     NSSASCII7 *email, | 
| 468 |  |     NSSCertificate *rvOpt[], | 
| 469 |  |     PRUint32 maximumOpt, | 
| 470 |  |     NSSArena *arenaOpt) | 
| 471 | 0 | { | 
| 472 | 0 |     NSSCertificate **rvArray = NULL; | 
| 473 | 0 |     struct email_template_str et; | 
| 474 | 0 |     et.email = email; | 
| 475 | 0 |     et.emailList = nssList_Create(NULL, PR_FALSE); | 
| 476 | 0 |     if (!et.emailList) { | 
| 477 | 0 |         return NULL; | 
| 478 | 0 |     } | 
| 479 | 0 |     PZ_Lock(store->lock); | 
| 480 | 0 |     nssHash_Iterate(store->subject, match_email, &et); | 
| 481 | 0 |     if (et.emailList) { | 
| 482 |  |         /* get references before leaving the store's lock protection */ | 
| 483 | 0 |         nssCertificateList_AddReferences(et.emailList); | 
| 484 | 0 |     } | 
| 485 | 0 |     PZ_Unlock(store->lock); | 
| 486 | 0 |     if (et.emailList) { | 
| 487 | 0 |         rvArray = get_array_from_list(et.emailList, | 
| 488 | 0 |                                       rvOpt, maximumOpt, arenaOpt); | 
| 489 | 0 |         nssList_Destroy(et.emailList); | 
| 490 | 0 |     } | 
| 491 | 0 |     return rvArray; | 
| 492 | 0 | } | 
| 493 |  |  | 
| 494 |  | /* Caller holds store->lock */ | 
| 495 |  | static NSSCertificate * | 
| 496 |  | nssCertStore_FindCertByIssuerAndSerialNumberLocked( | 
| 497 |  |     nssCertificateStore *store, | 
| 498 |  |     NSSDER *issuer, | 
| 499 |  |     NSSDER *serial) | 
| 500 | 12 | { | 
| 501 | 12 |     certificate_hash_entry *entry; | 
| 502 | 12 |     NSSCertificate *rvCert = NULL; | 
| 503 | 12 |     NSSCertificate index; | 
| 504 |  |  | 
| 505 | 12 |     index.issuer = *issuer; | 
| 506 | 12 |     index.serial = *serial; | 
| 507 | 12 |     entry = (certificate_hash_entry *) | 
| 508 | 12 |         nssHash_Lookup(store->issuer_and_serial, &index); | 
| 509 | 12 |     if (entry) { | 
| 510 | 0 |         rvCert = nssCertificate_AddRef(entry->cert); | 
| 511 | 0 |     } | 
| 512 | 12 |     return rvCert; | 
| 513 | 12 | } | 
| 514 |  |  | 
| 515 |  | NSS_IMPLEMENT NSSCertificate * | 
| 516 |  | nssCertificateStore_FindCertificateByIssuerAndSerialNumber( | 
| 517 |  |     nssCertificateStore *store, | 
| 518 |  |     NSSDER *issuer, | 
| 519 |  |     NSSDER *serial) | 
| 520 | 6 | { | 
| 521 | 6 |     NSSCertificate *rvCert = NULL; | 
| 522 |  |  | 
| 523 | 6 |     PZ_Lock(store->lock); | 
| 524 | 6 |     rvCert = nssCertStore_FindCertByIssuerAndSerialNumberLocked( | 
| 525 | 6 |         store, issuer, serial); | 
| 526 | 6 |     PZ_Unlock(store->lock); | 
| 527 | 6 |     return rvCert; | 
| 528 | 6 | } | 
| 529 |  |  | 
| 530 |  | NSS_IMPLEMENT NSSCertificate * | 
| 531 |  | nssCertificateStore_FindCertificateByEncodedCertificate( | 
| 532 |  |     nssCertificateStore *store, | 
| 533 |  |     NSSDER *encoding) | 
| 534 | 10 | { | 
| 535 | 10 |     PRStatus nssrv = PR_FAILURE; | 
| 536 | 10 |     NSSDER issuer, serial; | 
| 537 | 10 |     NSSCertificate *rvCert = NULL; | 
| 538 | 10 |     nssrv = nssPKIX509_GetIssuerAndSerialFromDER(encoding, &issuer, &serial); | 
| 539 | 10 |     if (nssrv != PR_SUCCESS) { | 
| 540 | 4 |         return NULL; | 
| 541 | 4 |     } | 
| 542 | 6 |     rvCert = nssCertificateStore_FindCertificateByIssuerAndSerialNumber(store, | 
| 543 | 6 |                                                                         &issuer, | 
| 544 | 6 |                                                                         &serial); | 
| 545 | 6 |     PORT_Free(issuer.data); | 
| 546 | 6 |     PORT_Free(serial.data); | 
| 547 | 6 |     return rvCert; | 
| 548 | 10 | } | 
| 549 |  |  | 
| 550 |  | NSS_EXTERN PRStatus | 
| 551 |  | nssCertificateStore_AddTrust( | 
| 552 |  |     nssCertificateStore *store, | 
| 553 |  |     NSSTrust *trust) | 
| 554 | 0 | { | 
| 555 | 0 |     NSSCertificate *cert; | 
| 556 | 0 |     certificate_hash_entry *entry; | 
| 557 | 0 |     cert = trust->certificate; | 
| 558 | 0 |     PZ_Lock(store->lock); | 
| 559 | 0 |     entry = (certificate_hash_entry *) | 
| 560 | 0 |         nssHash_Lookup(store->issuer_and_serial, cert); | 
| 561 | 0 |     if (entry) { | 
| 562 | 0 |         NSSTrust *newTrust = nssTrust_AddRef(trust); | 
| 563 | 0 |         if (entry->trust) { | 
| 564 | 0 |             nssTrust_Destroy(entry->trust); | 
| 565 | 0 |         } | 
| 566 | 0 |         entry->trust = newTrust; | 
| 567 | 0 |     } | 
| 568 | 0 |     PZ_Unlock(store->lock); | 
| 569 | 0 |     return (entry) ? PR_SUCCESS : PR_FAILURE; | 
| 570 | 0 | } | 
| 571 |  |  | 
| 572 |  | NSS_IMPLEMENT NSSTrust * | 
| 573 |  | nssCertificateStore_FindTrustForCertificate( | 
| 574 |  |     nssCertificateStore *store, | 
| 575 |  |     NSSCertificate *cert) | 
| 576 | 6 | { | 
| 577 | 6 |     certificate_hash_entry *entry; | 
| 578 | 6 |     NSSTrust *rvTrust = NULL; | 
| 579 | 6 |     PZ_Lock(store->lock); | 
| 580 | 6 |     entry = (certificate_hash_entry *) | 
| 581 | 6 |         nssHash_Lookup(store->issuer_and_serial, cert); | 
| 582 | 6 |     if (entry && entry->trust) { | 
| 583 | 0 |         rvTrust = nssTrust_AddRef(entry->trust); | 
| 584 | 0 |     } | 
| 585 | 6 |     PZ_Unlock(store->lock); | 
| 586 | 6 |     return rvTrust; | 
| 587 | 6 | } | 
| 588 |  |  | 
| 589 |  | NSS_EXTERN PRStatus | 
| 590 |  | nssCertificateStore_AddSMIMEProfile( | 
| 591 |  |     nssCertificateStore *store, | 
| 592 |  |     nssSMIMEProfile *profile) | 
| 593 | 0 | { | 
| 594 | 0 |     NSSCertificate *cert; | 
| 595 | 0 |     certificate_hash_entry *entry; | 
| 596 | 0 |     cert = profile->certificate; | 
| 597 | 0 |     PZ_Lock(store->lock); | 
| 598 | 0 |     entry = (certificate_hash_entry *) | 
| 599 | 0 |         nssHash_Lookup(store->issuer_and_serial, cert); | 
| 600 | 0 |     if (entry) { | 
| 601 | 0 |         nssSMIMEProfile *newProfile = nssSMIMEProfile_AddRef(profile); | 
| 602 | 0 |         if (entry->profile) { | 
| 603 | 0 |             nssSMIMEProfile_Destroy(entry->profile); | 
| 604 | 0 |         } | 
| 605 | 0 |         entry->profile = newProfile; | 
| 606 | 0 |     } | 
| 607 | 0 |     PZ_Unlock(store->lock); | 
| 608 | 0 |     return (entry) ? PR_SUCCESS : PR_FAILURE; | 
| 609 | 0 | } | 
| 610 |  |  | 
| 611 |  | NSS_IMPLEMENT nssSMIMEProfile * | 
| 612 |  | nssCertificateStore_FindSMIMEProfileForCertificate( | 
| 613 |  |     nssCertificateStore *store, | 
| 614 |  |     NSSCertificate *cert) | 
| 615 | 0 | { | 
| 616 | 0 |     certificate_hash_entry *entry; | 
| 617 | 0 |     nssSMIMEProfile *rvProfile = NULL; | 
| 618 | 0 |     PZ_Lock(store->lock); | 
| 619 | 0 |     entry = (certificate_hash_entry *) | 
| 620 | 0 |         nssHash_Lookup(store->issuer_and_serial, cert); | 
| 621 | 0 |     if (entry && entry->profile) { | 
| 622 | 0 |         rvProfile = nssSMIMEProfile_AddRef(entry->profile); | 
| 623 | 0 |     } | 
| 624 | 0 |     PZ_Unlock(store->lock); | 
| 625 | 0 |     return rvProfile; | 
| 626 | 0 | } | 
| 627 |  |  | 
| 628 |  | /* XXX this is also used by cache and should be somewhere else */ | 
| 629 |  |  | 
| 630 |  | static PLHashNumber | 
| 631 |  | nss_certificate_hash(const void *key) | 
| 632 | 48 | { | 
| 633 | 48 |     unsigned int i; | 
| 634 | 48 |     PLHashNumber h; | 
| 635 | 48 |     NSSCertificate *c = (NSSCertificate *)key; | 
| 636 | 48 |     h = 0; | 
| 637 | 1.87k |     for (i = 0; i < c->issuer.size; i++) | 
| 638 | 1.82k |         h = PR_ROTATE_LEFT32(h, 4) ^ ((unsigned char *)c->issuer.data)[i]; | 
| 639 | 342 |     for (i = 0; i < c->serial.size; i++) | 
| 640 | 294 |         h = PR_ROTATE_LEFT32(h, 4) ^ ((unsigned char *)c->serial.data)[i]; | 
| 641 | 48 |     return h; | 
| 642 | 48 | } | 
| 643 |  |  | 
| 644 |  | static int | 
| 645 |  | nss_compare_certs(const void *v1, const void *v2) | 
| 646 | 18 | { | 
| 647 | 18 |     PRStatus ignore; | 
| 648 | 18 |     NSSCertificate *c1 = (NSSCertificate *)v1; | 
| 649 | 18 |     NSSCertificate *c2 = (NSSCertificate *)v2; | 
| 650 | 18 |     return (int)(nssItem_Equal(&c1->issuer, &c2->issuer, &ignore) && | 
| 651 | 18 |                  nssItem_Equal(&c1->serial, &c2->serial, &ignore)); | 
| 652 | 18 | } | 
| 653 |  |  | 
| 654 |  | NSS_IMPLEMENT nssHash * | 
| 655 |  | nssHash_CreateCertificate( | 
| 656 |  |     NSSArena *arenaOpt, | 
| 657 |  |     PRUint32 numBuckets) | 
| 658 | 2 | { | 
| 659 | 2 |     return nssHash_Create(arenaOpt, | 
| 660 | 2 |                           numBuckets, | 
| 661 | 2 |                           nss_certificate_hash, | 
| 662 | 2 |                           nss_compare_certs, | 
| 663 | 2 |                           PL_CompareValues); | 
| 664 | 2 | } | 
| 665 |  |  | 
| 666 |  | NSS_IMPLEMENT void | 
| 667 |  | nssCertificateStore_DumpStoreInfo( | 
| 668 |  |     nssCertificateStore *store, | 
| 669 |  |     void (*cert_dump_iter)(const void *, void *, void *), | 
| 670 |  |     void *arg) | 
| 671 | 0 | { | 
| 672 | 0 |     PZ_Lock(store->lock); | 
| 673 | 0 |     nssHash_Iterate(store->issuer_and_serial, cert_dump_iter, arg); | 
| 674 | 0 |     PZ_Unlock(store->lock); | 
| 675 | 0 | } |