security/nss/lib/certdb/stanpcertdb.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "prtime.h"
michael@0 6
michael@0 7 #include "cert.h"
michael@0 8 #include "certi.h"
michael@0 9 #include "certdb.h"
michael@0 10 #include "secitem.h"
michael@0 11 #include "secder.h"
michael@0 12
michael@0 13 /* Call to PK11_FreeSlot below */
michael@0 14
michael@0 15 #include "secasn1.h"
michael@0 16 #include "secerr.h"
michael@0 17 #include "nssilock.h"
michael@0 18 #include "prmon.h"
michael@0 19 #include "base64.h"
michael@0 20 #include "sechash.h"
michael@0 21 #include "plhash.h"
michael@0 22 #include "pk11func.h" /* sigh */
michael@0 23
michael@0 24 #include "nsspki.h"
michael@0 25 #include "pki.h"
michael@0 26 #include "pkim.h"
michael@0 27 #include "pki3hack.h"
michael@0 28 #include "ckhelper.h"
michael@0 29 #include "base.h"
michael@0 30 #include "pkistore.h"
michael@0 31 #include "dev3hack.h"
michael@0 32 #include "dev.h"
michael@0 33
michael@0 34 PRBool
michael@0 35 SEC_CertNicknameConflict(const char *nickname, const SECItem *derSubject,
michael@0 36 CERTCertDBHandle *handle)
michael@0 37 {
michael@0 38 CERTCertificate *cert;
michael@0 39 PRBool conflict = PR_FALSE;
michael@0 40
michael@0 41 cert=CERT_FindCertByNickname(handle, nickname);
michael@0 42
michael@0 43 if (!cert) {
michael@0 44 return conflict;
michael@0 45 }
michael@0 46
michael@0 47 conflict = !SECITEM_ItemsAreEqual(derSubject,&cert->derSubject);
michael@0 48 CERT_DestroyCertificate(cert);
michael@0 49 return conflict;
michael@0 50 }
michael@0 51
michael@0 52 SECStatus
michael@0 53 SEC_DeletePermCertificate(CERTCertificate *cert)
michael@0 54 {
michael@0 55 PRStatus nssrv;
michael@0 56 NSSTrustDomain *td = STAN_GetDefaultTrustDomain();
michael@0 57 NSSCertificate *c = STAN_GetNSSCertificate(cert);
michael@0 58 CERTCertTrust *certTrust;
michael@0 59
michael@0 60 if (c == NULL) {
michael@0 61 /* error code is set */
michael@0 62 return SECFailure;
michael@0 63 }
michael@0 64
michael@0 65 certTrust = nssTrust_GetCERTCertTrustForCert(c, cert);
michael@0 66 if (certTrust) {
michael@0 67 NSSTrust *nssTrust = nssTrustDomain_FindTrustForCertificate(td, c);
michael@0 68 if (nssTrust) {
michael@0 69 nssrv = STAN_DeleteCertTrustMatchingSlot(c);
michael@0 70 if (nssrv != PR_SUCCESS) {
michael@0 71 CERT_MapStanError();
michael@0 72 }
michael@0 73 /* This call always returns PR_SUCCESS! */
michael@0 74 (void) nssTrust_Destroy(nssTrust);
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 /* get rid of the token instances */
michael@0 79 nssrv = NSSCertificate_DeleteStoredObject(c, NULL);
michael@0 80
michael@0 81 /* get rid of the cache entry */
michael@0 82 nssTrustDomain_LockCertCache(td);
michael@0 83 nssTrustDomain_RemoveCertFromCacheLOCKED(td, c);
michael@0 84 nssTrustDomain_UnlockCertCache(td);
michael@0 85
michael@0 86 return (nssrv == PR_SUCCESS) ? SECSuccess : SECFailure;
michael@0 87 }
michael@0 88
michael@0 89 SECStatus
michael@0 90 CERT_GetCertTrust(const CERTCertificate *cert, CERTCertTrust *trust)
michael@0 91 {
michael@0 92 SECStatus rv;
michael@0 93 CERT_LockCertTrust(cert);
michael@0 94 if ( cert->trust == NULL ) {
michael@0 95 rv = SECFailure;
michael@0 96 } else {
michael@0 97 *trust = *cert->trust;
michael@0 98 rv = SECSuccess;
michael@0 99 }
michael@0 100 CERT_UnlockCertTrust(cert);
michael@0 101 return(rv);
michael@0 102 }
michael@0 103
michael@0 104 extern const NSSError NSS_ERROR_NO_ERROR;
michael@0 105 extern const NSSError NSS_ERROR_INTERNAL_ERROR;
michael@0 106 extern const NSSError NSS_ERROR_NO_MEMORY;
michael@0 107 extern const NSSError NSS_ERROR_INVALID_POINTER;
michael@0 108 extern const NSSError NSS_ERROR_INVALID_ARENA;
michael@0 109 extern const NSSError NSS_ERROR_INVALID_ARENA_MARK;
michael@0 110 extern const NSSError NSS_ERROR_DUPLICATE_POINTER;
michael@0 111 extern const NSSError NSS_ERROR_POINTER_NOT_REGISTERED;
michael@0 112 extern const NSSError NSS_ERROR_TRACKER_NOT_EMPTY;
michael@0 113 extern const NSSError NSS_ERROR_TRACKER_NOT_INITIALIZED;
michael@0 114 extern const NSSError NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD;
michael@0 115 extern const NSSError NSS_ERROR_VALUE_TOO_LARGE;
michael@0 116 extern const NSSError NSS_ERROR_UNSUPPORTED_TYPE;
michael@0 117 extern const NSSError NSS_ERROR_BUFFER_TOO_SHORT;
michael@0 118 extern const NSSError NSS_ERROR_INVALID_ATOB_CONTEXT;
michael@0 119 extern const NSSError NSS_ERROR_INVALID_BASE64;
michael@0 120 extern const NSSError NSS_ERROR_INVALID_BTOA_CONTEXT;
michael@0 121 extern const NSSError NSS_ERROR_INVALID_ITEM;
michael@0 122 extern const NSSError NSS_ERROR_INVALID_STRING;
michael@0 123 extern const NSSError NSS_ERROR_INVALID_ASN1ENCODER;
michael@0 124 extern const NSSError NSS_ERROR_INVALID_ASN1DECODER;
michael@0 125 extern const NSSError NSS_ERROR_INVALID_BER;
michael@0 126 extern const NSSError NSS_ERROR_INVALID_ATAV;
michael@0 127 extern const NSSError NSS_ERROR_INVALID_ARGUMENT;
michael@0 128 extern const NSSError NSS_ERROR_INVALID_UTF8;
michael@0 129 extern const NSSError NSS_ERROR_INVALID_NSSOID;
michael@0 130 extern const NSSError NSS_ERROR_UNKNOWN_ATTRIBUTE;
michael@0 131 extern const NSSError NSS_ERROR_NOT_FOUND;
michael@0 132 extern const NSSError NSS_ERROR_INVALID_PASSWORD;
michael@0 133 extern const NSSError NSS_ERROR_USER_CANCELED;
michael@0 134 extern const NSSError NSS_ERROR_MAXIMUM_FOUND;
michael@0 135 extern const NSSError NSS_ERROR_CERTIFICATE_ISSUER_NOT_FOUND;
michael@0 136 extern const NSSError NSS_ERROR_CERTIFICATE_IN_CACHE;
michael@0 137 extern const NSSError NSS_ERROR_HASH_COLLISION;
michael@0 138 extern const NSSError NSS_ERROR_DEVICE_ERROR;
michael@0 139 extern const NSSError NSS_ERROR_INVALID_CERTIFICATE;
michael@0 140 extern const NSSError NSS_ERROR_BUSY;
michael@0 141 extern const NSSError NSS_ERROR_ALREADY_INITIALIZED;
michael@0 142 extern const NSSError NSS_ERROR_PKCS11;
michael@0 143
michael@0 144
michael@0 145 /* Look at the stan error stack and map it to NSS 3 errors */
michael@0 146 #define STAN_MAP_ERROR(x,y) \
michael@0 147 else if (error == (x)) { \
michael@0 148 secError = y; \
michael@0 149 } \
michael@0 150
michael@0 151 /*
michael@0 152 * map Stan errors into NSS errors
michael@0 153 * This function examines the stan error stack and automatically sets
michael@0 154 * PORT_SetError(); to the appropriate SEC_ERROR value.
michael@0 155 */
michael@0 156 void
michael@0 157 CERT_MapStanError()
michael@0 158 {
michael@0 159 PRInt32 *errorStack;
michael@0 160 NSSError error, prevError;
michael@0 161 int secError;
michael@0 162 int i;
michael@0 163
michael@0 164 error = 0;
michael@0 165
michael@0 166 errorStack = NSS_GetErrorStack();
michael@0 167 if (errorStack == 0) {
michael@0 168 PORT_SetError(0);
michael@0 169 return;
michael@0 170 }
michael@0 171 error = prevError = CKR_GENERAL_ERROR;
michael@0 172 /* get the 'top 2' error codes from the stack */
michael@0 173 for (i=0; errorStack[i]; i++) {
michael@0 174 prevError = error;
michael@0 175 error = errorStack[i];
michael@0 176 }
michael@0 177 if (error == NSS_ERROR_PKCS11) {
michael@0 178 /* map it */
michael@0 179 secError = PK11_MapError(prevError);
michael@0 180 }
michael@0 181 STAN_MAP_ERROR(NSS_ERROR_NO_ERROR, 0)
michael@0 182 STAN_MAP_ERROR(NSS_ERROR_NO_MEMORY, SEC_ERROR_NO_MEMORY)
michael@0 183 STAN_MAP_ERROR(NSS_ERROR_INVALID_BASE64, SEC_ERROR_BAD_DATA)
michael@0 184 STAN_MAP_ERROR(NSS_ERROR_INVALID_BER, SEC_ERROR_BAD_DER)
michael@0 185 STAN_MAP_ERROR(NSS_ERROR_INVALID_ATAV, SEC_ERROR_INVALID_AVA)
michael@0 186 STAN_MAP_ERROR(NSS_ERROR_INVALID_PASSWORD,SEC_ERROR_BAD_PASSWORD)
michael@0 187 STAN_MAP_ERROR(NSS_ERROR_BUSY, SEC_ERROR_BUSY)
michael@0 188 STAN_MAP_ERROR(NSS_ERROR_DEVICE_ERROR, SEC_ERROR_IO)
michael@0 189 STAN_MAP_ERROR(NSS_ERROR_CERTIFICATE_ISSUER_NOT_FOUND,
michael@0 190 SEC_ERROR_UNKNOWN_ISSUER)
michael@0 191 STAN_MAP_ERROR(NSS_ERROR_INVALID_CERTIFICATE, SEC_ERROR_CERT_NOT_VALID)
michael@0 192 STAN_MAP_ERROR(NSS_ERROR_INVALID_UTF8, SEC_ERROR_BAD_DATA)
michael@0 193 STAN_MAP_ERROR(NSS_ERROR_INVALID_NSSOID, SEC_ERROR_BAD_DATA)
michael@0 194
michael@0 195 /* these are library failure for lack of a better error code */
michael@0 196 STAN_MAP_ERROR(NSS_ERROR_NOT_FOUND, SEC_ERROR_LIBRARY_FAILURE)
michael@0 197 STAN_MAP_ERROR(NSS_ERROR_CERTIFICATE_IN_CACHE,
michael@0 198 SEC_ERROR_LIBRARY_FAILURE)
michael@0 199 STAN_MAP_ERROR(NSS_ERROR_MAXIMUM_FOUND, SEC_ERROR_LIBRARY_FAILURE)
michael@0 200 STAN_MAP_ERROR(NSS_ERROR_USER_CANCELED, SEC_ERROR_LIBRARY_FAILURE)
michael@0 201 STAN_MAP_ERROR(NSS_ERROR_TRACKER_NOT_INITIALIZED,
michael@0 202 SEC_ERROR_LIBRARY_FAILURE)
michael@0 203 STAN_MAP_ERROR(NSS_ERROR_ALREADY_INITIALIZED, SEC_ERROR_LIBRARY_FAILURE)
michael@0 204 STAN_MAP_ERROR(NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD,
michael@0 205 SEC_ERROR_LIBRARY_FAILURE)
michael@0 206 STAN_MAP_ERROR(NSS_ERROR_HASH_COLLISION, SEC_ERROR_LIBRARY_FAILURE)
michael@0 207
michael@0 208 STAN_MAP_ERROR(NSS_ERROR_INTERNAL_ERROR, SEC_ERROR_LIBRARY_FAILURE)
michael@0 209
michael@0 210 /* these are all invalid arguments */
michael@0 211 STAN_MAP_ERROR(NSS_ERROR_INVALID_ARGUMENT, SEC_ERROR_INVALID_ARGS)
michael@0 212 STAN_MAP_ERROR(NSS_ERROR_INVALID_POINTER, SEC_ERROR_INVALID_ARGS)
michael@0 213 STAN_MAP_ERROR(NSS_ERROR_INVALID_ARENA, SEC_ERROR_INVALID_ARGS)
michael@0 214 STAN_MAP_ERROR(NSS_ERROR_INVALID_ARENA_MARK, SEC_ERROR_INVALID_ARGS)
michael@0 215 STAN_MAP_ERROR(NSS_ERROR_DUPLICATE_POINTER, SEC_ERROR_INVALID_ARGS)
michael@0 216 STAN_MAP_ERROR(NSS_ERROR_POINTER_NOT_REGISTERED, SEC_ERROR_INVALID_ARGS)
michael@0 217 STAN_MAP_ERROR(NSS_ERROR_TRACKER_NOT_EMPTY, SEC_ERROR_INVALID_ARGS)
michael@0 218 STAN_MAP_ERROR(NSS_ERROR_VALUE_TOO_LARGE, SEC_ERROR_INVALID_ARGS)
michael@0 219 STAN_MAP_ERROR(NSS_ERROR_UNSUPPORTED_TYPE, SEC_ERROR_INVALID_ARGS)
michael@0 220 STAN_MAP_ERROR(NSS_ERROR_BUFFER_TOO_SHORT, SEC_ERROR_INVALID_ARGS)
michael@0 221 STAN_MAP_ERROR(NSS_ERROR_INVALID_ATOB_CONTEXT, SEC_ERROR_INVALID_ARGS)
michael@0 222 STAN_MAP_ERROR(NSS_ERROR_INVALID_BTOA_CONTEXT, SEC_ERROR_INVALID_ARGS)
michael@0 223 STAN_MAP_ERROR(NSS_ERROR_INVALID_ITEM, SEC_ERROR_INVALID_ARGS)
michael@0 224 STAN_MAP_ERROR(NSS_ERROR_INVALID_STRING, SEC_ERROR_INVALID_ARGS)
michael@0 225 STAN_MAP_ERROR(NSS_ERROR_INVALID_ASN1ENCODER, SEC_ERROR_INVALID_ARGS)
michael@0 226 STAN_MAP_ERROR(NSS_ERROR_INVALID_ASN1DECODER, SEC_ERROR_INVALID_ARGS)
michael@0 227 STAN_MAP_ERROR(NSS_ERROR_UNKNOWN_ATTRIBUTE, SEC_ERROR_INVALID_ARGS)
michael@0 228 else {
michael@0 229 secError = SEC_ERROR_LIBRARY_FAILURE;
michael@0 230 }
michael@0 231 PORT_SetError(secError);
michael@0 232 }
michael@0 233
michael@0 234
michael@0 235
michael@0 236 SECStatus
michael@0 237 CERT_ChangeCertTrust(CERTCertDBHandle *handle, CERTCertificate *cert,
michael@0 238 CERTCertTrust *trust)
michael@0 239 {
michael@0 240 SECStatus rv = SECSuccess;
michael@0 241 PRStatus ret;
michael@0 242
michael@0 243 ret = STAN_ChangeCertTrust(cert, trust);
michael@0 244 if (ret != PR_SUCCESS) {
michael@0 245 rv = SECFailure;
michael@0 246 CERT_MapStanError();
michael@0 247 }
michael@0 248 return rv;
michael@0 249 }
michael@0 250
michael@0 251 extern const NSSError NSS_ERROR_INVALID_CERTIFICATE;
michael@0 252
michael@0 253 SECStatus
michael@0 254 __CERT_AddTempCertToPerm(CERTCertificate *cert, char *nickname,
michael@0 255 CERTCertTrust *trust)
michael@0 256 {
michael@0 257 NSSUTF8 *stanNick;
michael@0 258 PK11SlotInfo *slot;
michael@0 259 NSSToken *internal;
michael@0 260 NSSCryptoContext *context;
michael@0 261 nssCryptokiObject *permInstance;
michael@0 262 NSSCertificate *c = STAN_GetNSSCertificate(cert);
michael@0 263 nssCertificateStoreTrace lockTrace = {NULL, NULL, PR_FALSE, PR_FALSE};
michael@0 264 nssCertificateStoreTrace unlockTrace = {NULL, NULL, PR_FALSE, PR_FALSE};
michael@0 265 SECStatus rv;
michael@0 266 PRStatus ret;
michael@0 267
michael@0 268 if (c == NULL) {
michael@0 269 CERT_MapStanError();
michael@0 270 return SECFailure;
michael@0 271 }
michael@0 272
michael@0 273 context = c->object.cryptoContext;
michael@0 274 if (!context) {
michael@0 275 PORT_SetError(SEC_ERROR_ADDING_CERT);
michael@0 276 return SECFailure; /* wasn't a temp cert */
michael@0 277 }
michael@0 278 stanNick = nssCertificate_GetNickname(c, NULL);
michael@0 279 if (stanNick && nickname && strcmp(nickname, stanNick) != 0) {
michael@0 280 /* different: take the new nickname */
michael@0 281 cert->nickname = NULL;
michael@0 282 nss_ZFreeIf(stanNick);
michael@0 283 stanNick = NULL;
michael@0 284 }
michael@0 285 if (!stanNick && nickname) {
michael@0 286 /* Either there was no nickname yet, or we have a new nickname */
michael@0 287 stanNick = nssUTF8_Duplicate((NSSUTF8 *)nickname, NULL);
michael@0 288 } /* else: old stanNick is identical to new nickname */
michael@0 289 /* Delete the temp instance */
michael@0 290 nssCertificateStore_Lock(context->certStore, &lockTrace);
michael@0 291 nssCertificateStore_RemoveCertLOCKED(context->certStore, c);
michael@0 292 nssCertificateStore_Unlock(context->certStore, &lockTrace, &unlockTrace);
michael@0 293 c->object.cryptoContext = NULL;
michael@0 294 /* Import the perm instance onto the internal token */
michael@0 295 slot = PK11_GetInternalKeySlot();
michael@0 296 internal = PK11Slot_GetNSSToken(slot);
michael@0 297 permInstance = nssToken_ImportCertificate(internal, NULL,
michael@0 298 NSSCertificateType_PKIX,
michael@0 299 &c->id,
michael@0 300 stanNick,
michael@0 301 &c->encoding,
michael@0 302 &c->issuer,
michael@0 303 &c->subject,
michael@0 304 &c->serial,
michael@0 305 cert->emailAddr,
michael@0 306 PR_TRUE);
michael@0 307 nss_ZFreeIf(stanNick);
michael@0 308 stanNick = NULL;
michael@0 309 PK11_FreeSlot(slot);
michael@0 310 if (!permInstance) {
michael@0 311 if (NSS_GetError() == NSS_ERROR_INVALID_CERTIFICATE) {
michael@0 312 PORT_SetError(SEC_ERROR_REUSED_ISSUER_AND_SERIAL);
michael@0 313 }
michael@0 314 return SECFailure;
michael@0 315 }
michael@0 316 nssPKIObject_AddInstance(&c->object, permInstance);
michael@0 317 nssTrustDomain_AddCertsToCache(STAN_GetDefaultTrustDomain(), &c, 1);
michael@0 318 /* reset the CERTCertificate fields */
michael@0 319 cert->nssCertificate = NULL;
michael@0 320 cert = STAN_GetCERTCertificateOrRelease(c); /* should return same pointer */
michael@0 321 if (!cert) {
michael@0 322 CERT_MapStanError();
michael@0 323 return SECFailure;
michael@0 324 }
michael@0 325 cert->istemp = PR_FALSE;
michael@0 326 cert->isperm = PR_TRUE;
michael@0 327 if (!trust) {
michael@0 328 return SECSuccess;
michael@0 329 }
michael@0 330 ret = STAN_ChangeCertTrust(cert, trust);
michael@0 331 rv = SECSuccess;
michael@0 332 if (ret != PR_SUCCESS) {
michael@0 333 rv = SECFailure;
michael@0 334 CERT_MapStanError();
michael@0 335 }
michael@0 336 return rv;
michael@0 337 }
michael@0 338
michael@0 339 SECStatus
michael@0 340 CERT_AddTempCertToPerm(CERTCertificate *cert, char *nickname,
michael@0 341 CERTCertTrust *trust)
michael@0 342 {
michael@0 343 return __CERT_AddTempCertToPerm(cert, nickname, trust);
michael@0 344 }
michael@0 345
michael@0 346 CERTCertificate *
michael@0 347 CERT_NewTempCertificate(CERTCertDBHandle *handle, SECItem *derCert,
michael@0 348 char *nickname, PRBool isperm, PRBool copyDER)
michael@0 349 {
michael@0 350 NSSCertificate *c;
michael@0 351 CERTCertificate *cc;
michael@0 352 NSSCertificate *tempCert = NULL;
michael@0 353 nssPKIObject *pkio;
michael@0 354 NSSCryptoContext *gCC = STAN_GetDefaultCryptoContext();
michael@0 355 NSSTrustDomain *gTD = STAN_GetDefaultTrustDomain();
michael@0 356 if (!isperm) {
michael@0 357 NSSDER encoding;
michael@0 358 NSSITEM_FROM_SECITEM(&encoding, derCert);
michael@0 359 /* First, see if it is already a temp cert */
michael@0 360 c = NSSCryptoContext_FindCertificateByEncodedCertificate(gCC,
michael@0 361 &encoding);
michael@0 362 if (!c) {
michael@0 363 /* Then, see if it is already a perm cert */
michael@0 364 c = NSSTrustDomain_FindCertificateByEncodedCertificate(handle,
michael@0 365 &encoding);
michael@0 366 }
michael@0 367 if (c) {
michael@0 368 /* actually, that search ends up going by issuer/serial,
michael@0 369 * so it is still possible to return a cert with the same
michael@0 370 * issuer/serial but a different encoding, and we're
michael@0 371 * going to reject that
michael@0 372 */
michael@0 373 if (!nssItem_Equal(&c->encoding, &encoding, NULL)) {
michael@0 374 nssCertificate_Destroy(c);
michael@0 375 PORT_SetError(SEC_ERROR_REUSED_ISSUER_AND_SERIAL);
michael@0 376 cc = NULL;
michael@0 377 } else {
michael@0 378 cc = STAN_GetCERTCertificateOrRelease(c);
michael@0 379 if (cc == NULL) {
michael@0 380 CERT_MapStanError();
michael@0 381 }
michael@0 382 }
michael@0 383 return cc;
michael@0 384 }
michael@0 385 }
michael@0 386 pkio = nssPKIObject_Create(NULL, NULL, gTD, gCC, nssPKIMonitor);
michael@0 387 if (!pkio) {
michael@0 388 CERT_MapStanError();
michael@0 389 return NULL;
michael@0 390 }
michael@0 391 c = nss_ZNEW(pkio->arena, NSSCertificate);
michael@0 392 if (!c) {
michael@0 393 CERT_MapStanError();
michael@0 394 nssPKIObject_Destroy(pkio);
michael@0 395 return NULL;
michael@0 396 }
michael@0 397 c->object = *pkio;
michael@0 398 if (copyDER) {
michael@0 399 nssItem_Create(c->object.arena, &c->encoding,
michael@0 400 derCert->len, derCert->data);
michael@0 401 } else {
michael@0 402 NSSITEM_FROM_SECITEM(&c->encoding, derCert);
michael@0 403 }
michael@0 404 /* Forces a decoding of the cert in order to obtain the parts used
michael@0 405 * below
michael@0 406 */
michael@0 407 /* 'c' is not adopted here, if we fail loser frees what has been
michael@0 408 * allocated so far for 'c' */
michael@0 409 cc = STAN_GetCERTCertificate(c);
michael@0 410 if (!cc) {
michael@0 411 CERT_MapStanError();
michael@0 412 goto loser;
michael@0 413 }
michael@0 414 nssItem_Create(c->object.arena,
michael@0 415 &c->issuer, cc->derIssuer.len, cc->derIssuer.data);
michael@0 416 nssItem_Create(c->object.arena,
michael@0 417 &c->subject, cc->derSubject.len, cc->derSubject.data);
michael@0 418 if (PR_TRUE) {
michael@0 419 /* CERTCertificate stores serial numbers decoded. I need the DER
michael@0 420 * here. sigh.
michael@0 421 */
michael@0 422 SECItem derSerial = { 0 };
michael@0 423 CERT_SerialNumberFromDERCert(&cc->derCert, &derSerial);
michael@0 424 if (!derSerial.data) goto loser;
michael@0 425 nssItem_Create(c->object.arena, &c->serial, derSerial.len, derSerial.data);
michael@0 426 PORT_Free(derSerial.data);
michael@0 427 }
michael@0 428 if (nickname) {
michael@0 429 c->object.tempName = nssUTF8_Create(c->object.arena,
michael@0 430 nssStringType_UTF8String,
michael@0 431 (NSSUTF8 *)nickname,
michael@0 432 PORT_Strlen(nickname));
michael@0 433 }
michael@0 434 if (cc->emailAddr && cc->emailAddr[0]) {
michael@0 435 c->email = nssUTF8_Create(c->object.arena,
michael@0 436 nssStringType_PrintableString,
michael@0 437 (NSSUTF8 *)cc->emailAddr,
michael@0 438 PORT_Strlen(cc->emailAddr));
michael@0 439 }
michael@0 440
michael@0 441 tempCert = NSSCryptoContext_FindOrImportCertificate(gCC, c);
michael@0 442 if (!tempCert) {
michael@0 443 CERT_MapStanError();
michael@0 444 goto loser;
michael@0 445 }
michael@0 446 /* destroy our copy */
michael@0 447 NSSCertificate_Destroy(c);
michael@0 448 /* and use the stored entry */
michael@0 449 c = tempCert;
michael@0 450 cc = STAN_GetCERTCertificateOrRelease(c);
michael@0 451 if (!cc) {
michael@0 452 /* STAN_GetCERTCertificateOrRelease destroys c on failure. */
michael@0 453 CERT_MapStanError();
michael@0 454 return NULL;
michael@0 455 }
michael@0 456
michael@0 457 cc->istemp = PR_TRUE;
michael@0 458 cc->isperm = PR_FALSE;
michael@0 459 return cc;
michael@0 460 loser:
michael@0 461 /* Perhaps this should be nssCertificate_Destroy(c) */
michael@0 462 nssPKIObject_Destroy(&c->object);
michael@0 463 return NULL;
michael@0 464 }
michael@0 465
michael@0 466 /* This symbol is exported for backward compatibility. */
michael@0 467 CERTCertificate *
michael@0 468 __CERT_NewTempCertificate(CERTCertDBHandle *handle, SECItem *derCert,
michael@0 469 char *nickname, PRBool isperm, PRBool copyDER)
michael@0 470 {
michael@0 471 return CERT_NewTempCertificate(handle, derCert, nickname,
michael@0 472 isperm, copyDER);
michael@0 473 }
michael@0 474
michael@0 475 /* maybe all the wincx's should be some const for internal token login? */
michael@0 476 CERTCertificate *
michael@0 477 CERT_FindCertByIssuerAndSN(CERTCertDBHandle *handle, CERTIssuerAndSN *issuerAndSN)
michael@0 478 {
michael@0 479 PK11SlotInfo *slot;
michael@0 480 CERTCertificate *cert;
michael@0 481
michael@0 482 cert = PK11_FindCertByIssuerAndSN(&slot,issuerAndSN,NULL);
michael@0 483 if (cert && slot) {
michael@0 484 PK11_FreeSlot(slot);
michael@0 485 }
michael@0 486
michael@0 487 return cert;
michael@0 488 }
michael@0 489
michael@0 490 static NSSCertificate *
michael@0 491 get_best_temp_or_perm(NSSCertificate *ct, NSSCertificate *cp)
michael@0 492 {
michael@0 493 NSSUsage usage;
michael@0 494 NSSCertificate *arr[3];
michael@0 495 if (!ct) {
michael@0 496 return nssCertificate_AddRef(cp);
michael@0 497 } else if (!cp) {
michael@0 498 return nssCertificate_AddRef(ct);
michael@0 499 }
michael@0 500 arr[0] = ct;
michael@0 501 arr[1] = cp;
michael@0 502 arr[2] = NULL;
michael@0 503 usage.anyUsage = PR_TRUE;
michael@0 504 return nssCertificateArray_FindBestCertificate(arr, NULL, &usage, NULL);
michael@0 505 }
michael@0 506
michael@0 507 CERTCertificate *
michael@0 508 CERT_FindCertByName(CERTCertDBHandle *handle, SECItem *name)
michael@0 509 {
michael@0 510 NSSCertificate *cp, *ct, *c;
michael@0 511 NSSDER subject;
michael@0 512 NSSUsage usage;
michael@0 513 NSSCryptoContext *cc;
michael@0 514 NSSITEM_FROM_SECITEM(&subject, name);
michael@0 515 usage.anyUsage = PR_TRUE;
michael@0 516 cc = STAN_GetDefaultCryptoContext();
michael@0 517 ct = NSSCryptoContext_FindBestCertificateBySubject(cc, &subject,
michael@0 518 NULL, &usage, NULL);
michael@0 519 cp = NSSTrustDomain_FindBestCertificateBySubject(handle, &subject,
michael@0 520 NULL, &usage, NULL);
michael@0 521 c = get_best_temp_or_perm(ct, cp);
michael@0 522 if (ct) {
michael@0 523 CERT_DestroyCertificate(STAN_GetCERTCertificateOrRelease(ct));
michael@0 524 }
michael@0 525 if (cp) {
michael@0 526 CERT_DestroyCertificate(STAN_GetCERTCertificateOrRelease(cp));
michael@0 527 }
michael@0 528 return c ? STAN_GetCERTCertificateOrRelease(c) : NULL;
michael@0 529 }
michael@0 530
michael@0 531 CERTCertificate *
michael@0 532 CERT_FindCertByKeyID(CERTCertDBHandle *handle, SECItem *name, SECItem *keyID)
michael@0 533 {
michael@0 534 CERTCertList *list;
michael@0 535 CERTCertificate *cert = NULL;
michael@0 536 CERTCertListNode *node, *head;
michael@0 537
michael@0 538 list = CERT_CreateSubjectCertList(NULL,handle,name,0,PR_FALSE);
michael@0 539 if (list == NULL) return NULL;
michael@0 540
michael@0 541 node = head = CERT_LIST_HEAD(list);
michael@0 542 if (head) {
michael@0 543 do {
michael@0 544 if (node->cert &&
michael@0 545 SECITEM_ItemsAreEqual(&node->cert->subjectKeyID, keyID) ) {
michael@0 546 cert = CERT_DupCertificate(node->cert);
michael@0 547 goto done;
michael@0 548 }
michael@0 549 node = CERT_LIST_NEXT(node);
michael@0 550 } while (node && head != node);
michael@0 551 }
michael@0 552 PORT_SetError(SEC_ERROR_UNKNOWN_ISSUER);
michael@0 553 done:
michael@0 554 if (list) {
michael@0 555 CERT_DestroyCertList(list);
michael@0 556 }
michael@0 557 return cert;
michael@0 558 }
michael@0 559
michael@0 560 CERTCertificate *
michael@0 561 CERT_FindCertByNickname(CERTCertDBHandle *handle, const char *nickname)
michael@0 562 {
michael@0 563 NSSCryptoContext *cc;
michael@0 564 NSSCertificate *c, *ct;
michael@0 565 CERTCertificate *cert;
michael@0 566 NSSUsage usage;
michael@0 567 usage.anyUsage = PR_TRUE;
michael@0 568 cc = STAN_GetDefaultCryptoContext();
michael@0 569 ct = NSSCryptoContext_FindBestCertificateByNickname(cc, nickname,
michael@0 570 NULL, &usage, NULL);
michael@0 571 cert = PK11_FindCertFromNickname(nickname, NULL);
michael@0 572 c = NULL;
michael@0 573 if (cert) {
michael@0 574 c = get_best_temp_or_perm(ct, STAN_GetNSSCertificate(cert));
michael@0 575 CERT_DestroyCertificate(cert);
michael@0 576 if (ct) {
michael@0 577 CERT_DestroyCertificate(STAN_GetCERTCertificateOrRelease(ct));
michael@0 578 }
michael@0 579 } else {
michael@0 580 c = ct;
michael@0 581 }
michael@0 582 return c ? STAN_GetCERTCertificateOrRelease(c) : NULL;
michael@0 583 }
michael@0 584
michael@0 585 CERTCertificate *
michael@0 586 CERT_FindCertByDERCert(CERTCertDBHandle *handle, SECItem *derCert)
michael@0 587 {
michael@0 588 NSSCryptoContext *cc;
michael@0 589 NSSCertificate *c;
michael@0 590 NSSDER encoding;
michael@0 591 NSSITEM_FROM_SECITEM(&encoding, derCert);
michael@0 592 cc = STAN_GetDefaultCryptoContext();
michael@0 593 c = NSSCryptoContext_FindCertificateByEncodedCertificate(cc, &encoding);
michael@0 594 if (!c) {
michael@0 595 c = NSSTrustDomain_FindCertificateByEncodedCertificate(handle,
michael@0 596 &encoding);
michael@0 597 if (!c) return NULL;
michael@0 598 }
michael@0 599 return STAN_GetCERTCertificateOrRelease(c);
michael@0 600 }
michael@0 601
michael@0 602 static CERTCertificate *
michael@0 603 common_FindCertByNicknameOrEmailAddrForUsage(CERTCertDBHandle *handle,
michael@0 604 const char *name,
michael@0 605 PRBool anyUsage,
michael@0 606 SECCertUsage lookingForUsage)
michael@0 607 {
michael@0 608 NSSCryptoContext *cc;
michael@0 609 NSSCertificate *c, *ct;
michael@0 610 CERTCertificate *cert = NULL;
michael@0 611 NSSUsage usage;
michael@0 612 CERTCertList *certlist;
michael@0 613
michael@0 614 if (NULL == name) {
michael@0 615 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 616 return NULL;
michael@0 617 }
michael@0 618
michael@0 619 usage.anyUsage = anyUsage;
michael@0 620
michael@0 621 if (!anyUsage) {
michael@0 622 usage.nss3lookingForCA = PR_FALSE;
michael@0 623 usage.nss3usage = lookingForUsage;
michael@0 624 }
michael@0 625
michael@0 626 cc = STAN_GetDefaultCryptoContext();
michael@0 627 ct = NSSCryptoContext_FindBestCertificateByNickname(cc, name,
michael@0 628 NULL, &usage, NULL);
michael@0 629 if (!ct && PORT_Strchr(name, '@') != NULL) {
michael@0 630 char* lowercaseName = CERT_FixupEmailAddr(name);
michael@0 631 if (lowercaseName) {
michael@0 632 ct = NSSCryptoContext_FindBestCertificateByEmail(cc, lowercaseName,
michael@0 633 NULL, &usage, NULL);
michael@0 634 PORT_Free(lowercaseName);
michael@0 635 }
michael@0 636 }
michael@0 637
michael@0 638 if (anyUsage) {
michael@0 639 cert = PK11_FindCertFromNickname(name, NULL);
michael@0 640 }
michael@0 641 else {
michael@0 642 if (ct) {
michael@0 643 /* Does ct really have the required usage? */
michael@0 644 nssDecodedCert *dc;
michael@0 645 dc = nssCertificate_GetDecoding(ct);
michael@0 646 if (!dc->matchUsage(dc, &usage)) {
michael@0 647 CERT_DestroyCertificate(STAN_GetCERTCertificateOrRelease(ct));
michael@0 648 ct = NULL;
michael@0 649 }
michael@0 650 }
michael@0 651
michael@0 652 certlist = PK11_FindCertsFromNickname(name, NULL);
michael@0 653 if (certlist) {
michael@0 654 SECStatus rv = CERT_FilterCertListByUsage(certlist,
michael@0 655 lookingForUsage,
michael@0 656 PR_FALSE);
michael@0 657 if (SECSuccess == rv &&
michael@0 658 !CERT_LIST_END(CERT_LIST_HEAD(certlist), certlist)) {
michael@0 659 cert = CERT_DupCertificate(CERT_LIST_HEAD(certlist)->cert);
michael@0 660 }
michael@0 661 CERT_DestroyCertList(certlist);
michael@0 662 }
michael@0 663 }
michael@0 664
michael@0 665 if (cert) {
michael@0 666 c = get_best_temp_or_perm(ct, STAN_GetNSSCertificate(cert));
michael@0 667 CERT_DestroyCertificate(cert);
michael@0 668 if (ct) {
michael@0 669 CERT_DestroyCertificate(STAN_GetCERTCertificateOrRelease(ct));
michael@0 670 }
michael@0 671 } else {
michael@0 672 c = ct;
michael@0 673 }
michael@0 674 return c ? STAN_GetCERTCertificateOrRelease(c) : NULL;
michael@0 675 }
michael@0 676
michael@0 677 CERTCertificate *
michael@0 678 CERT_FindCertByNicknameOrEmailAddr(CERTCertDBHandle *handle, const char *name)
michael@0 679 {
michael@0 680 return common_FindCertByNicknameOrEmailAddrForUsage(handle, name,
michael@0 681 PR_TRUE, 0);
michael@0 682 }
michael@0 683
michael@0 684 CERTCertificate *
michael@0 685 CERT_FindCertByNicknameOrEmailAddrForUsage(CERTCertDBHandle *handle,
michael@0 686 const char *name,
michael@0 687 SECCertUsage lookingForUsage)
michael@0 688 {
michael@0 689 return common_FindCertByNicknameOrEmailAddrForUsage(handle, name,
michael@0 690 PR_FALSE,
michael@0 691 lookingForUsage);
michael@0 692 }
michael@0 693
michael@0 694 static void
michael@0 695 add_to_subject_list(CERTCertList *certList, CERTCertificate *cert,
michael@0 696 PRBool validOnly, PRTime sorttime)
michael@0 697 {
michael@0 698 SECStatus secrv;
michael@0 699 if (!validOnly ||
michael@0 700 CERT_CheckCertValidTimes(cert, sorttime, PR_FALSE)
michael@0 701 == secCertTimeValid) {
michael@0 702 secrv = CERT_AddCertToListSorted(certList, cert,
michael@0 703 CERT_SortCBValidity,
michael@0 704 (void *)&sorttime);
michael@0 705 if (secrv != SECSuccess) {
michael@0 706 CERT_DestroyCertificate(cert);
michael@0 707 }
michael@0 708 } else {
michael@0 709 CERT_DestroyCertificate(cert);
michael@0 710 }
michael@0 711 }
michael@0 712
michael@0 713 CERTCertList *
michael@0 714 CERT_CreateSubjectCertList(CERTCertList *certList, CERTCertDBHandle *handle,
michael@0 715 const SECItem *name, PRTime sorttime,
michael@0 716 PRBool validOnly)
michael@0 717 {
michael@0 718 NSSCryptoContext *cc;
michael@0 719 NSSCertificate **tSubjectCerts, **pSubjectCerts;
michael@0 720 NSSCertificate **ci;
michael@0 721 CERTCertificate *cert;
michael@0 722 NSSDER subject;
michael@0 723 PRBool myList = PR_FALSE;
michael@0 724 cc = STAN_GetDefaultCryptoContext();
michael@0 725 NSSITEM_FROM_SECITEM(&subject, name);
michael@0 726 /* Collect both temp and perm certs for the subject */
michael@0 727 tSubjectCerts = NSSCryptoContext_FindCertificatesBySubject(cc,
michael@0 728 &subject,
michael@0 729 NULL,
michael@0 730 0,
michael@0 731 NULL);
michael@0 732 pSubjectCerts = NSSTrustDomain_FindCertificatesBySubject(handle,
michael@0 733 &subject,
michael@0 734 NULL,
michael@0 735 0,
michael@0 736 NULL);
michael@0 737 if (!tSubjectCerts && !pSubjectCerts) {
michael@0 738 return NULL;
michael@0 739 }
michael@0 740 if (certList == NULL) {
michael@0 741 certList = CERT_NewCertList();
michael@0 742 myList = PR_TRUE;
michael@0 743 if (!certList) goto loser;
michael@0 744 }
michael@0 745 /* Iterate over the matching temp certs. Add them to the list */
michael@0 746 ci = tSubjectCerts;
michael@0 747 while (ci && *ci) {
michael@0 748 cert = STAN_GetCERTCertificateOrRelease(*ci);
michael@0 749 /* *ci may be invalid at this point, don't reference it again */
michael@0 750 if (cert) {
michael@0 751 /* NOTE: add_to_subject_list adopts the incoming cert. */
michael@0 752 add_to_subject_list(certList, cert, validOnly, sorttime);
michael@0 753 }
michael@0 754 ci++;
michael@0 755 }
michael@0 756 /* Iterate over the matching perm certs. Add them to the list */
michael@0 757 ci = pSubjectCerts;
michael@0 758 while (ci && *ci) {
michael@0 759 cert = STAN_GetCERTCertificateOrRelease(*ci);
michael@0 760 /* *ci may be invalid at this point, don't reference it again */
michael@0 761 if (cert) {
michael@0 762 /* NOTE: add_to_subject_list adopts the incoming cert. */
michael@0 763 add_to_subject_list(certList, cert, validOnly, sorttime);
michael@0 764 }
michael@0 765 ci++;
michael@0 766 }
michael@0 767 /* all the references have been adopted or freed at this point, just
michael@0 768 * free the arrays now */
michael@0 769 nss_ZFreeIf(tSubjectCerts);
michael@0 770 nss_ZFreeIf(pSubjectCerts);
michael@0 771 return certList;
michael@0 772 loser:
michael@0 773 /* need to free the references in tSubjectCerts and pSubjectCerts! */
michael@0 774 nssCertificateArray_Destroy(tSubjectCerts);
michael@0 775 nssCertificateArray_Destroy(pSubjectCerts);
michael@0 776 if (myList && certList != NULL) {
michael@0 777 CERT_DestroyCertList(certList);
michael@0 778 }
michael@0 779 return NULL;
michael@0 780 }
michael@0 781
michael@0 782 void
michael@0 783 CERT_DestroyCertificate(CERTCertificate *cert)
michael@0 784 {
michael@0 785 if ( cert ) {
michael@0 786 /* don't use STAN_GetNSSCertificate because we don't want to
michael@0 787 * go to the trouble of translating the CERTCertificate into
michael@0 788 * an NSSCertificate just to destroy it. If it hasn't been done
michael@0 789 * yet, don't do it at all.
michael@0 790 */
michael@0 791 NSSCertificate *tmp = cert->nssCertificate;
michael@0 792 if (tmp) {
michael@0 793 /* delete the NSSCertificate */
michael@0 794 NSSCertificate_Destroy(tmp);
michael@0 795 } else if (cert->arena) {
michael@0 796 PORT_FreeArena(cert->arena, PR_FALSE);
michael@0 797 }
michael@0 798 }
michael@0 799 return;
michael@0 800 }
michael@0 801
michael@0 802 int
michael@0 803 CERT_GetDBContentVersion(CERTCertDBHandle *handle)
michael@0 804 {
michael@0 805 /* should read the DB content version from the pkcs #11 device */
michael@0 806 return 0;
michael@0 807 }
michael@0 808
michael@0 809 SECStatus
michael@0 810 certdb_SaveSingleProfile(CERTCertificate *cert, const char *emailAddr,
michael@0 811 SECItem *emailProfile, SECItem *profileTime)
michael@0 812 {
michael@0 813 PRTime oldtime;
michael@0 814 PRTime newtime;
michael@0 815 SECStatus rv = SECFailure;
michael@0 816 PRBool saveit;
michael@0 817 SECItem oldprof, oldproftime;
michael@0 818 SECItem *oldProfile = NULL;
michael@0 819 SECItem *oldProfileTime = NULL;
michael@0 820 PK11SlotInfo *slot = NULL;
michael@0 821 NSSCertificate *c;
michael@0 822 NSSCryptoContext *cc;
michael@0 823 nssSMIMEProfile *stanProfile = NULL;
michael@0 824 PRBool freeOldProfile = PR_FALSE;
michael@0 825
michael@0 826 c = STAN_GetNSSCertificate(cert);
michael@0 827 if (!c) return SECFailure;
michael@0 828 cc = c->object.cryptoContext;
michael@0 829 if (cc != NULL) {
michael@0 830 stanProfile = nssCryptoContext_FindSMIMEProfileForCertificate(cc, c);
michael@0 831 if (stanProfile) {
michael@0 832 PORT_Assert(stanProfile->profileData);
michael@0 833 SECITEM_FROM_NSSITEM(&oldprof, stanProfile->profileData);
michael@0 834 oldProfile = &oldprof;
michael@0 835 SECITEM_FROM_NSSITEM(&oldproftime, stanProfile->profileTime);
michael@0 836 oldProfileTime = &oldproftime;
michael@0 837 }
michael@0 838 } else {
michael@0 839 oldProfile = PK11_FindSMimeProfile(&slot, (char *)emailAddr,
michael@0 840 &cert->derSubject, &oldProfileTime);
michael@0 841 freeOldProfile = PR_TRUE;
michael@0 842 }
michael@0 843
michael@0 844 saveit = PR_FALSE;
michael@0 845
michael@0 846 /* both profileTime and emailProfile have to exist or not exist */
michael@0 847 if ( emailProfile == NULL ) {
michael@0 848 profileTime = NULL;
michael@0 849 } else if ( profileTime == NULL ) {
michael@0 850 emailProfile = NULL;
michael@0 851 }
michael@0 852
michael@0 853 if ( oldProfileTime == NULL ) {
michael@0 854 saveit = PR_TRUE;
michael@0 855 } else {
michael@0 856 /* there was already a profile for this email addr */
michael@0 857 if ( profileTime ) {
michael@0 858 /* we have an old and new profile - save whichever is more recent*/
michael@0 859 if ( oldProfileTime->len == 0 ) {
michael@0 860 /* always replace if old entry doesn't have a time */
michael@0 861 oldtime = LL_MININT;
michael@0 862 } else {
michael@0 863 rv = DER_UTCTimeToTime(&oldtime, oldProfileTime);
michael@0 864 if ( rv != SECSuccess ) {
michael@0 865 goto loser;
michael@0 866 }
michael@0 867 }
michael@0 868
michael@0 869 rv = DER_UTCTimeToTime(&newtime, profileTime);
michael@0 870 if ( rv != SECSuccess ) {
michael@0 871 goto loser;
michael@0 872 }
michael@0 873
michael@0 874 if ( LL_CMP(newtime, >, oldtime ) ) {
michael@0 875 /* this is a newer profile, save it and cert */
michael@0 876 saveit = PR_TRUE;
michael@0 877 }
michael@0 878 } else {
michael@0 879 saveit = PR_TRUE;
michael@0 880 }
michael@0 881 }
michael@0 882
michael@0 883
michael@0 884 if (saveit) {
michael@0 885 if (cc) {
michael@0 886 if (stanProfile) {
michael@0 887 /* stanProfile is already stored in the crypto context,
michael@0 888 * overwrite the data
michael@0 889 */
michael@0 890 NSSArena *arena = stanProfile->object.arena;
michael@0 891 stanProfile->profileTime = nssItem_Create(arena,
michael@0 892 NULL,
michael@0 893 profileTime->len,
michael@0 894 profileTime->data);
michael@0 895 stanProfile->profileData = nssItem_Create(arena,
michael@0 896 NULL,
michael@0 897 emailProfile->len,
michael@0 898 emailProfile->data);
michael@0 899 } else if (profileTime && emailProfile) {
michael@0 900 PRStatus nssrv;
michael@0 901 NSSItem profTime, profData;
michael@0 902 NSSITEM_FROM_SECITEM(&profTime, profileTime);
michael@0 903 NSSITEM_FROM_SECITEM(&profData, emailProfile);
michael@0 904 stanProfile = nssSMIMEProfile_Create(c, &profTime, &profData);
michael@0 905 if (!stanProfile) goto loser;
michael@0 906 nssrv = nssCryptoContext_ImportSMIMEProfile(cc, stanProfile);
michael@0 907 rv = (nssrv == PR_SUCCESS) ? SECSuccess : SECFailure;
michael@0 908 }
michael@0 909 } else {
michael@0 910 rv = PK11_SaveSMimeProfile(slot, (char *)emailAddr,
michael@0 911 &cert->derSubject, emailProfile, profileTime);
michael@0 912 }
michael@0 913 } else {
michael@0 914 rv = SECSuccess;
michael@0 915 }
michael@0 916
michael@0 917 loser:
michael@0 918 if (oldProfile && freeOldProfile) {
michael@0 919 SECITEM_FreeItem(oldProfile,PR_TRUE);
michael@0 920 }
michael@0 921 if (oldProfileTime && freeOldProfile) {
michael@0 922 SECITEM_FreeItem(oldProfileTime,PR_TRUE);
michael@0 923 }
michael@0 924 if (stanProfile) {
michael@0 925 nssSMIMEProfile_Destroy(stanProfile);
michael@0 926 }
michael@0 927 if (slot) {
michael@0 928 PK11_FreeSlot(slot);
michael@0 929 }
michael@0 930
michael@0 931 return(rv);
michael@0 932 }
michael@0 933
michael@0 934 /*
michael@0 935 *
michael@0 936 * Manage S/MIME profiles
michael@0 937 *
michael@0 938 */
michael@0 939
michael@0 940 SECStatus
michael@0 941 CERT_SaveSMimeProfile(CERTCertificate *cert, SECItem *emailProfile,
michael@0 942 SECItem *profileTime)
michael@0 943 {
michael@0 944 const char *emailAddr;
michael@0 945 SECStatus rv;
michael@0 946
michael@0 947 if (!cert) {
michael@0 948 return SECFailure;
michael@0 949 }
michael@0 950
michael@0 951 if (cert->slot && !PK11_IsInternal(cert->slot)) {
michael@0 952 /* this cert comes from an external source, we need to add it
michael@0 953 to the cert db before creating an S/MIME profile */
michael@0 954 PK11SlotInfo* internalslot = PK11_GetInternalKeySlot();
michael@0 955 if (!internalslot) {
michael@0 956 return SECFailure;
michael@0 957 }
michael@0 958 rv = PK11_ImportCert(internalslot, cert,
michael@0 959 CK_INVALID_HANDLE, NULL, PR_FALSE);
michael@0 960
michael@0 961 PK11_FreeSlot(internalslot);
michael@0 962 if (rv != SECSuccess ) {
michael@0 963 return SECFailure;
michael@0 964 }
michael@0 965 }
michael@0 966
michael@0 967 if (cert->slot && cert->isperm && CERT_IsUserCert(cert) &&
michael@0 968 (!emailProfile || !emailProfile->len)) {
michael@0 969 /* Don't clobber emailProfile for user certs. */
michael@0 970 return SECSuccess;
michael@0 971 }
michael@0 972
michael@0 973 for (emailAddr = CERT_GetFirstEmailAddress(cert); emailAddr != NULL;
michael@0 974 emailAddr = CERT_GetNextEmailAddress(cert,emailAddr)) {
michael@0 975 rv = certdb_SaveSingleProfile(cert,emailAddr,emailProfile,profileTime);
michael@0 976 if (rv != SECSuccess) {
michael@0 977 return SECFailure;
michael@0 978 }
michael@0 979 }
michael@0 980 return SECSuccess;
michael@0 981
michael@0 982 }
michael@0 983
michael@0 984
michael@0 985 SECItem *
michael@0 986 CERT_FindSMimeProfile(CERTCertificate *cert)
michael@0 987 {
michael@0 988 PK11SlotInfo *slot = NULL;
michael@0 989 NSSCertificate *c;
michael@0 990 NSSCryptoContext *cc;
michael@0 991 SECItem *rvItem = NULL;
michael@0 992
michael@0 993 if (!cert || !cert->emailAddr || !cert->emailAddr[0]) {
michael@0 994 PORT_SetError(SEC_ERROR_INVALID_ARGS);
michael@0 995 return NULL;
michael@0 996 }
michael@0 997 c = STAN_GetNSSCertificate(cert);
michael@0 998 if (!c) return NULL;
michael@0 999 cc = c->object.cryptoContext;
michael@0 1000 if (cc != NULL) {
michael@0 1001 nssSMIMEProfile *stanProfile;
michael@0 1002 stanProfile = nssCryptoContext_FindSMIMEProfileForCertificate(cc, c);
michael@0 1003 if (stanProfile) {
michael@0 1004 rvItem = SECITEM_AllocItem(NULL, NULL,
michael@0 1005 stanProfile->profileData->size);
michael@0 1006 if (rvItem) {
michael@0 1007 rvItem->data = stanProfile->profileData->data;
michael@0 1008 }
michael@0 1009 nssSMIMEProfile_Destroy(stanProfile);
michael@0 1010 }
michael@0 1011 return rvItem;
michael@0 1012 }
michael@0 1013 rvItem =
michael@0 1014 PK11_FindSMimeProfile(&slot, cert->emailAddr, &cert->derSubject, NULL);
michael@0 1015 if (slot) {
michael@0 1016 PK11_FreeSlot(slot);
michael@0 1017 }
michael@0 1018 return rvItem;
michael@0 1019 }
michael@0 1020
michael@0 1021 /*
michael@0 1022 * deprecated functions that are now just stubs.
michael@0 1023 */
michael@0 1024 /*
michael@0 1025 * Close the database
michael@0 1026 */
michael@0 1027 void
michael@0 1028 __CERT_ClosePermCertDB(CERTCertDBHandle *handle)
michael@0 1029 {
michael@0 1030 PORT_Assert("CERT_ClosePermCertDB is Deprecated" == NULL);
michael@0 1031 return;
michael@0 1032 }
michael@0 1033
michael@0 1034 SECStatus
michael@0 1035 CERT_OpenCertDBFilename(CERTCertDBHandle *handle, char *certdbname,
michael@0 1036 PRBool readOnly)
michael@0 1037 {
michael@0 1038 PORT_Assert("CERT_OpenCertDBFilename is Deprecated" == NULL);
michael@0 1039 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
michael@0 1040 return SECFailure;
michael@0 1041 }
michael@0 1042
michael@0 1043 SECItem *
michael@0 1044 SECKEY_HashPassword(char *pw, SECItem *salt)
michael@0 1045 {
michael@0 1046 PORT_Assert("SECKEY_HashPassword is Deprecated" == NULL);
michael@0 1047 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
michael@0 1048 return NULL;
michael@0 1049 }
michael@0 1050
michael@0 1051 SECStatus
michael@0 1052 __CERT_TraversePermCertsForSubject(CERTCertDBHandle *handle,
michael@0 1053 SECItem *derSubject,
michael@0 1054 void *cb, void *cbarg)
michael@0 1055 {
michael@0 1056 PORT_Assert("CERT_TraversePermCertsForSubject is Deprecated" == NULL);
michael@0 1057 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
michael@0 1058 return SECFailure;
michael@0 1059 }
michael@0 1060
michael@0 1061
michael@0 1062 SECStatus
michael@0 1063 __CERT_TraversePermCertsForNickname(CERTCertDBHandle *handle, char *nickname,
michael@0 1064 void *cb, void *cbarg)
michael@0 1065 {
michael@0 1066 PORT_Assert("CERT_TraversePermCertsForNickname is Deprecated" == NULL);
michael@0 1067 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
michael@0 1068 return SECFailure;
michael@0 1069 }
michael@0 1070
michael@0 1071
michael@0 1072

mercurial