andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: /* andre@0: * This file manages PKCS #11 instances of certificates. andre@0: */ andre@0: andre@0: #include "secport.h" andre@0: #include "seccomon.h" andre@0: #include "secmod.h" andre@0: #include "secmodi.h" andre@0: #include "secmodti.h" andre@0: #include "pkcs11.h" andre@0: #include "pk11func.h" andre@0: #include "cert.h" andre@0: #include "certi.h" andre@0: #include "secitem.h" andre@0: #include "key.h" andre@0: #include "secoid.h" andre@0: #include "pkcs7t.h" andre@0: #include "cmsreclist.h" andre@0: andre@0: #include "certdb.h" andre@0: #include "secerr.h" andre@0: #include "sslerr.h" andre@0: andre@0: #include "pki3hack.h" andre@0: #include "dev3hack.h" andre@0: andre@0: #include "devm.h" andre@0: #include "nsspki.h" andre@0: #include "pki.h" andre@0: #include "pkim.h" andre@0: #include "pkitm.h" andre@0: #include "pkistore.h" /* to remove temp cert */ andre@0: #include "devt.h" andre@0: andre@0: extern const NSSError NSS_ERROR_NOT_FOUND; andre@0: extern const NSSError NSS_ERROR_INVALID_CERTIFICATE; andre@0: andre@0: struct nss3_cert_cbstr { andre@0: SECStatus(* callback)(CERTCertificate*, void *); andre@0: nssList *cached; andre@0: void *arg; andre@0: }; andre@0: andre@0: /* Translate from NSSCertificate to CERTCertificate, then pass the latter andre@0: * to a callback. andre@0: */ andre@0: static PRStatus convert_cert(NSSCertificate *c, void *arg) andre@0: { andre@0: CERTCertificate *nss3cert; andre@0: SECStatus secrv; andre@0: struct nss3_cert_cbstr *nss3cb = (struct nss3_cert_cbstr *)arg; andre@0: /* 'c' is not adopted. caller will free it */ andre@0: nss3cert = STAN_GetCERTCertificate(c); andre@0: if (!nss3cert) return PR_FAILURE; andre@0: secrv = (*nss3cb->callback)(nss3cert, nss3cb->arg); andre@0: return (secrv) ? PR_FAILURE : PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: * build a cert nickname based on the token name and the label of the andre@0: * certificate If the label in NULL, build a label based on the ID. andre@0: */ andre@0: static int toHex(int x) { return (x < 10) ? (x+'0') : (x+'a'-10); } andre@0: #define MAX_CERT_ID 4 andre@0: #define DEFAULT_STRING "Cert ID " andre@0: static char * andre@0: pk11_buildNickname(PK11SlotInfo *slot,CK_ATTRIBUTE *cert_label, andre@0: CK_ATTRIBUTE *key_label, CK_ATTRIBUTE *cert_id) andre@0: { andre@0: int prefixLen = PORT_Strlen(slot->token_name); andre@0: int suffixLen = 0; andre@0: char *suffix = NULL; andre@0: char buildNew[sizeof(DEFAULT_STRING)+MAX_CERT_ID*2]; andre@0: char *next,*nickname; andre@0: andre@0: if (cert_label && (cert_label->ulValueLen)) { andre@0: suffixLen = cert_label->ulValueLen; andre@0: suffix = (char*)cert_label->pValue; andre@0: } else if (key_label && (key_label->ulValueLen)) { andre@0: suffixLen = key_label->ulValueLen; andre@0: suffix = (char*)key_label->pValue; andre@0: } else if (cert_id && cert_id->ulValueLen > 0) { andre@0: int i,first = cert_id->ulValueLen - MAX_CERT_ID; andre@0: int offset = sizeof(DEFAULT_STRING); andre@0: char *idValue = (char *)cert_id->pValue; andre@0: andre@0: PORT_Memcpy(buildNew,DEFAULT_STRING,sizeof(DEFAULT_STRING)-1); andre@0: next = buildNew + offset; andre@0: if (first < 0) first = 0; andre@0: for (i=first; i < (int) cert_id->ulValueLen; i++) { andre@0: *next++ = toHex((idValue[i] >> 4) & 0xf); andre@0: *next++ = toHex(idValue[i] & 0xf); andre@0: } andre@0: *next++ = 0; andre@0: suffix = buildNew; andre@0: suffixLen = PORT_Strlen(buildNew); andre@0: } else { andre@0: PORT_SetError( SEC_ERROR_LIBRARY_FAILURE ); andre@0: return NULL; andre@0: } andre@0: andre@0: /* if is internal key slot, add code to skip the prefix!! */ andre@0: next = nickname = (char *)PORT_Alloc(prefixLen+1+suffixLen+1); andre@0: if (nickname == NULL) return NULL; andre@0: andre@0: PORT_Memcpy(next,slot->token_name,prefixLen); andre@0: next += prefixLen; andre@0: *next++ = ':'; andre@0: PORT_Memcpy(next,suffix,suffixLen); andre@0: next += suffixLen; andre@0: *next++ = 0; andre@0: return nickname; andre@0: } andre@0: andre@0: PRBool andre@0: PK11_IsUserCert(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: CK_OBJECT_HANDLE certID) andre@0: { andre@0: CK_OBJECT_CLASS theClass; andre@0: andre@0: if (slot == NULL) return PR_FALSE; andre@0: if (cert == NULL) return PR_FALSE; andre@0: andre@0: theClass = CKO_PRIVATE_KEY; andre@0: if (pk11_LoginStillRequired(slot,NULL)) { andre@0: theClass = CKO_PUBLIC_KEY; andre@0: } andre@0: if (PK11_MatchItem(slot, certID , theClass) != CK_INVALID_HANDLE) { andre@0: return PR_TRUE; andre@0: } andre@0: andre@0: if (theClass == CKO_PUBLIC_KEY) { andre@0: SECKEYPublicKey *pubKey= CERT_ExtractPublicKey(cert); andre@0: CK_ATTRIBUTE theTemplate; andre@0: andre@0: if (pubKey == NULL) { andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: PK11_SETATTRS(&theTemplate,0,NULL,0); andre@0: switch (pubKey->keyType) { andre@0: case rsaKey: andre@0: PK11_SETATTRS(&theTemplate,CKA_MODULUS, pubKey->u.rsa.modulus.data, andre@0: pubKey->u.rsa.modulus.len); andre@0: break; andre@0: case dsaKey: andre@0: PK11_SETATTRS(&theTemplate,CKA_VALUE, pubKey->u.dsa.publicValue.data, andre@0: pubKey->u.dsa.publicValue.len); andre@0: break; andre@0: case dhKey: andre@0: PK11_SETATTRS(&theTemplate,CKA_VALUE, pubKey->u.dh.publicValue.data, andre@0: pubKey->u.dh.publicValue.len); andre@0: break; andre@0: case ecKey: andre@0: PK11_SETATTRS(&theTemplate,CKA_EC_POINT, andre@0: pubKey->u.ec.publicValue.data, andre@0: pubKey->u.ec.publicValue.len); andre@0: break; andre@0: case keaKey: andre@0: case fortezzaKey: andre@0: case nullKey: andre@0: /* fall through and return false */ andre@0: break; andre@0: } andre@0: andre@0: if (theTemplate.ulValueLen == 0) { andre@0: SECKEY_DestroyPublicKey(pubKey); andre@0: return PR_FALSE; andre@0: } andre@0: pk11_SignedToUnsigned(&theTemplate); andre@0: if (pk11_FindObjectByTemplate(slot,&theTemplate,1) != CK_INVALID_HANDLE) { andre@0: SECKEY_DestroyPublicKey(pubKey); andre@0: return PR_TRUE; andre@0: } andre@0: SECKEY_DestroyPublicKey(pubKey); andre@0: } andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: /* andre@0: * Check out if a cert has ID of zero. This is a magic ID that tells andre@0: * NSS that this cert may be an automagically trusted cert. andre@0: * The Cert has to be self signed as well. That check is done elsewhere. andre@0: * andre@0: */ andre@0: PRBool andre@0: pk11_isID0(PK11SlotInfo *slot, CK_OBJECT_HANDLE certID) andre@0: { andre@0: CK_ATTRIBUTE keyID = {CKA_ID, NULL, 0}; andre@0: PRBool isZero = PR_FALSE; andre@0: int i; andre@0: CK_RV crv; andre@0: andre@0: andre@0: crv = PK11_GetAttributes(NULL,slot,certID,&keyID,1); andre@0: if (crv != CKR_OK) { andre@0: return isZero; andre@0: } andre@0: andre@0: if (keyID.ulValueLen != 0) { andre@0: char *value = (char *)keyID.pValue; andre@0: isZero = PR_TRUE; /* ID exists, may be zero */ andre@0: for (i=0; i < (int) keyID.ulValueLen; i++) { andre@0: if (value[i] != 0) { andre@0: isZero = PR_FALSE; /* nope */ andre@0: break; andre@0: } andre@0: } andre@0: } andre@0: PORT_Free(keyID.pValue); andre@0: return isZero; andre@0: andre@0: } andre@0: andre@0: /* andre@0: * Create an NSSCertificate from a slot/certID pair, return it as a andre@0: * CERTCertificate. Optionally, output the nickname string. andre@0: */ andre@0: static CERTCertificate * andre@0: pk11_fastCert(PK11SlotInfo *slot, CK_OBJECT_HANDLE certID, andre@0: CK_ATTRIBUTE *privateLabel, char **nickptr) andre@0: { andre@0: NSSCertificate *c; andre@0: nssCryptokiObject *co = NULL; andre@0: nssPKIObject *pkio; andre@0: NSSToken *token; andre@0: NSSTrustDomain *td = STAN_GetDefaultTrustDomain(); andre@0: PRStatus status; andre@0: andre@0: /* Get the cryptoki object from the handle */ andre@0: token = PK11Slot_GetNSSToken(slot); andre@0: if (token->defaultSession) { andre@0: co = nssCryptokiObject_Create(token, token->defaultSession, certID); andre@0: } else { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: } andre@0: if (!co) { andre@0: return NULL; andre@0: } andre@0: andre@0: /* Create a PKI object from the cryptoki instance */ andre@0: pkio = nssPKIObject_Create(NULL, co, td, NULL, nssPKIMonitor); andre@0: if (!pkio) { andre@0: nssCryptokiObject_Destroy(co); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Create a certificate */ andre@0: c = nssCertificate_Create(pkio); andre@0: if (!c) { andre@0: nssPKIObject_Destroy(pkio); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Build and output a nickname, if desired. andre@0: * This must be done before calling nssTrustDomain_AddCertsToCache andre@0: * because that function may destroy c, pkio and co! andre@0: */ andre@0: if ((nickptr) && (co->label)) { andre@0: CK_ATTRIBUTE label, id; andre@0: andre@0: label.type = CKA_LABEL; andre@0: label.pValue = co->label; andre@0: label.ulValueLen = PORT_Strlen(co->label); andre@0: andre@0: id.type = CKA_ID; andre@0: id.pValue = c->id.data; andre@0: id.ulValueLen = c->id.size; andre@0: andre@0: *nickptr = pk11_buildNickname(slot, &label, privateLabel, &id); andre@0: } andre@0: andre@0: /* This function may destroy the cert in "c" and all its subordinate andre@0: * structures, and replace the value in "c" with the address of a andre@0: * different NSSCertificate that it found in the cache. andre@0: * Presumably, the nickname which we just output above remains valid. :) andre@0: */ andre@0: status = nssTrustDomain_AddCertsToCache(td, &c, 1); andre@0: return STAN_GetCERTCertificateOrRelease(c); andre@0: } andre@0: andre@0: /* andre@0: * Build an CERTCertificate structure from a PKCS#11 object ID.... certID andre@0: * Must be a CertObject. This code does not explicitly checks that. andre@0: */ andre@0: CERTCertificate * andre@0: PK11_MakeCertFromHandle(PK11SlotInfo *slot,CK_OBJECT_HANDLE certID, andre@0: CK_ATTRIBUTE *privateLabel) andre@0: { andre@0: char * nickname = NULL; andre@0: CERTCertificate *cert = NULL; andre@0: CERTCertTrust *trust; andre@0: PRBool isFortezzaRootCA = PR_FALSE; andre@0: PRBool swapNickname = PR_FALSE; andre@0: andre@0: cert = pk11_fastCert(slot,certID,privateLabel, &nickname); andre@0: if (cert == NULL) andre@0: goto loser; andre@0: andre@0: if (nickname) { andre@0: if (cert->nickname != NULL) { andre@0: cert->dbnickname = cert->nickname; andre@0: } andre@0: cert->nickname = PORT_ArenaStrdup(cert->arena,nickname); andre@0: PORT_Free(nickname); andre@0: nickname = NULL; andre@0: swapNickname = PR_TRUE; andre@0: } andre@0: andre@0: /* remember where this cert came from.... If we have just looked andre@0: * it up from the database and it already has a slot, don't add a new andre@0: * one. */ andre@0: if (cert->slot == NULL) { andre@0: cert->slot = PK11_ReferenceSlot(slot); andre@0: cert->pkcs11ID = certID; andre@0: cert->ownSlot = PR_TRUE; andre@0: cert->series = slot->series; andre@0: } andre@0: andre@0: trust = (CERTCertTrust*)PORT_ArenaAlloc(cert->arena, sizeof(CERTCertTrust)); andre@0: if (trust == NULL) andre@0: goto loser; andre@0: PORT_Memset(trust,0, sizeof(CERTCertTrust)); andre@0: andre@0: if(! pk11_HandleTrustObject(slot, cert, trust) ) { andre@0: unsigned int type; andre@0: andre@0: /* build some cert trust flags */ andre@0: if (CERT_IsCACert(cert, &type)) { andre@0: unsigned int trustflags = CERTDB_VALID_CA; andre@0: andre@0: /* Allow PKCS #11 modules to give us trusted CA's. We only accept andre@0: * valid CA's which are self-signed here. They must have an object andre@0: * ID of '0'. */ andre@0: if (pk11_isID0(slot,certID) && andre@0: cert->isRoot) { andre@0: trustflags |= CERTDB_TRUSTED_CA; andre@0: /* is the slot a fortezza card? allow the user or andre@0: * admin to turn on objectSigning, but don't turn andre@0: * full trust on explicitly */ andre@0: if (PK11_DoesMechanism(slot,CKM_KEA_KEY_DERIVE)) { andre@0: trust->objectSigningFlags |= CERTDB_VALID_CA; andre@0: isFortezzaRootCA = PR_TRUE; andre@0: } andre@0: } andre@0: if ((type & NS_CERT_TYPE_SSL_CA) == NS_CERT_TYPE_SSL_CA) { andre@0: trust->sslFlags |= trustflags; andre@0: } andre@0: if ((type & NS_CERT_TYPE_EMAIL_CA) == NS_CERT_TYPE_EMAIL_CA) { andre@0: trust->emailFlags |= trustflags; andre@0: } andre@0: if ((type & NS_CERT_TYPE_OBJECT_SIGNING_CA) andre@0: == NS_CERT_TYPE_OBJECT_SIGNING_CA) { andre@0: trust->objectSigningFlags |= trustflags; andre@0: } andre@0: } andre@0: } andre@0: andre@0: if (PK11_IsUserCert(slot,cert,certID)) { andre@0: trust->sslFlags |= CERTDB_USER; andre@0: trust->emailFlags |= CERTDB_USER; andre@0: /* trust->objectSigningFlags |= CERTDB_USER; */ andre@0: } andre@0: CERT_LockCertTrust(cert); andre@0: cert->trust = trust; andre@0: CERT_UnlockCertTrust(cert); andre@0: andre@0: return cert; andre@0: andre@0: loser: andre@0: if (nickname) andre@0: PORT_Free(nickname); andre@0: if (cert) andre@0: CERT_DestroyCertificate(cert); andre@0: return NULL; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Build get a certificate from a private key andre@0: */ andre@0: CERTCertificate * andre@0: PK11_GetCertFromPrivateKey(SECKEYPrivateKey *privKey) andre@0: { andre@0: PK11SlotInfo *slot = privKey->pkcs11Slot; andre@0: CK_OBJECT_HANDLE handle = privKey->pkcs11ID; andre@0: CK_OBJECT_HANDLE certID = andre@0: PK11_MatchItem(slot,handle,CKO_CERTIFICATE); andre@0: CERTCertificate *cert; andre@0: andre@0: if (certID == CK_INVALID_HANDLE) { andre@0: PORT_SetError(SSL_ERROR_NO_CERTIFICATE); andre@0: return NULL; andre@0: } andre@0: cert = PK11_MakeCertFromHandle(slot,certID,NULL); andre@0: return (cert); andre@0: andre@0: } andre@0: andre@0: /* andre@0: * delete a cert and it's private key (if no other certs are pointing to the andre@0: * private key. andre@0: */ andre@0: SECStatus andre@0: PK11_DeleteTokenCertAndKey(CERTCertificate *cert,void *wincx) andre@0: { andre@0: SECKEYPrivateKey *privKey = PK11_FindKeyByAnyCert(cert,wincx); andre@0: CK_OBJECT_HANDLE pubKey; andre@0: PK11SlotInfo *slot = NULL; andre@0: andre@0: pubKey = pk11_FindPubKeyByAnyCert(cert, &slot, wincx); andre@0: if (privKey) { andre@0: /* For 3.4, utilize the generic cert delete function */ andre@0: SEC_DeletePermCertificate(cert); andre@0: PK11_DeleteTokenPrivateKey(privKey, PR_FALSE); andre@0: } andre@0: if ((pubKey != CK_INVALID_HANDLE) && (slot != NULL)) { andre@0: PK11_DestroyTokenObject(slot,pubKey); andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* andre@0: * cert callback structure andre@0: */ andre@0: typedef struct pk11DoCertCallbackStr { andre@0: SECStatus(* callback)(PK11SlotInfo *slot, CERTCertificate*, void *); andre@0: SECStatus(* noslotcallback)(CERTCertificate*, void *); andre@0: SECStatus(* itemcallback)(CERTCertificate*, SECItem *, void *); andre@0: void *callbackArg; andre@0: } pk11DoCertCallback; andre@0: andre@0: andre@0: typedef struct pk11CertCallbackStr { andre@0: SECStatus(* callback)(CERTCertificate*,SECItem *,void *); andre@0: void *callbackArg; andre@0: } pk11CertCallback; andre@0: andre@0: struct fake_der_cb_argstr andre@0: { andre@0: SECStatus(* callback)(CERTCertificate*, SECItem *, void *); andre@0: void *arg; andre@0: }; andre@0: andre@0: static SECStatus fake_der_cb(CERTCertificate *c, void *a) andre@0: { andre@0: struct fake_der_cb_argstr *fda = (struct fake_der_cb_argstr *)a; andre@0: return (*fda->callback)(c, &c->derCert, fda->arg); andre@0: } andre@0: andre@0: /* andre@0: * Extract all the certs on a card from a slot. andre@0: */ andre@0: SECStatus andre@0: PK11_TraverseSlotCerts(SECStatus(* callback)(CERTCertificate*,SECItem *,void *), andre@0: void *arg, void *wincx) andre@0: { andre@0: NSSTrustDomain *defaultTD = STAN_GetDefaultTrustDomain(); andre@0: struct fake_der_cb_argstr fda; andre@0: struct nss3_cert_cbstr pk11cb; andre@0: andre@0: /* authenticate to the tokens first */ andre@0: (void) pk11_TraverseAllSlots( NULL, NULL, PR_TRUE, wincx); andre@0: andre@0: fda.callback = callback; andre@0: fda.arg = arg; andre@0: pk11cb.callback = fake_der_cb; andre@0: pk11cb.arg = &fda; andre@0: NSSTrustDomain_TraverseCertificates(defaultTD, convert_cert, &pk11cb); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: static void andre@0: transfer_token_certs_to_collection(nssList *certList, NSSToken *token, andre@0: nssPKIObjectCollection *collection) andre@0: { andre@0: NSSCertificate **certs; andre@0: PRUint32 i, count; andre@0: NSSToken **tokens, **tp; andre@0: count = nssList_Count(certList); andre@0: if (count == 0) { andre@0: return; andre@0: } andre@0: certs = nss_ZNEWARRAY(NULL, NSSCertificate *, count); andre@0: if (!certs) { andre@0: return; andre@0: } andre@0: nssList_GetArray(certList, (void **)certs, count); andre@0: for (i=0; iobject, NULL); andre@0: if (tokens) { andre@0: for (tp = tokens; *tp; tp++) { andre@0: if (*tp == token) { andre@0: nssPKIObjectCollection_AddObject(collection, andre@0: (nssPKIObject *)certs[i]); andre@0: } andre@0: } andre@0: nssTokenArray_Destroy(tokens); andre@0: } andre@0: CERT_DestroyCertificate(STAN_GetCERTCertificateOrRelease(certs[i])); andre@0: } andre@0: nss_ZFreeIf(certs); andre@0: } andre@0: andre@0: CERTCertificate * andre@0: PK11_FindCertFromNickname(const char *nickname, void *wincx) andre@0: { andre@0: PRStatus status; andre@0: CERTCertificate *rvCert = NULL; andre@0: NSSCertificate *cert = NULL; andre@0: NSSCertificate **certs = NULL; andre@0: static const NSSUsage usage = {PR_TRUE /* ... */ }; andre@0: NSSToken *token; andre@0: NSSTrustDomain *defaultTD = STAN_GetDefaultTrustDomain(); andre@0: PK11SlotInfo *slot = NULL; andre@0: SECStatus rv; andre@0: char *nickCopy; andre@0: char *delimit = NULL; andre@0: char *tokenName; andre@0: andre@0: nickCopy = PORT_Strdup(nickname); andre@0: if (!nickCopy) { andre@0: /* error code is set */ andre@0: return NULL; andre@0: } andre@0: if ((delimit = PORT_Strchr(nickCopy,':')) != NULL) { andre@0: tokenName = nickCopy; andre@0: nickname = delimit + 1; andre@0: *delimit = '\0'; andre@0: /* find token by name */ andre@0: token = NSSTrustDomain_FindTokenByName(defaultTD, (NSSUTF8 *)tokenName); andre@0: if (token) { andre@0: slot = PK11_ReferenceSlot(token->pk11slot); andre@0: } else { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: } andre@0: *delimit = ':'; andre@0: } else { andre@0: slot = PK11_GetInternalKeySlot(); andre@0: token = PK11Slot_GetNSSToken(slot); andre@0: } andre@0: if (token) { andre@0: nssList *certList; andre@0: nssCryptokiObject **instances; andre@0: nssPKIObjectCollection *collection; andre@0: nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly; andre@0: if (!PK11_IsPresent(slot)) { andre@0: goto loser; andre@0: } andre@0: rv = pk11_AuthenticateUnfriendly(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: collection = nssCertificateCollection_Create(defaultTD, NULL); andre@0: if (!collection) { andre@0: goto loser; andre@0: } andre@0: certList = nssList_Create(NULL, PR_FALSE); andre@0: if (!certList) { andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: goto loser; andre@0: } andre@0: (void)nssTrustDomain_GetCertsForNicknameFromCache(defaultTD, andre@0: nickname, andre@0: certList); andre@0: transfer_token_certs_to_collection(certList, token, collection); andre@0: instances = nssToken_FindCertificatesByNickname(token, andre@0: NULL, andre@0: nickname, andre@0: tokenOnly, andre@0: 0, andre@0: &status); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: /* if it wasn't found, repeat the process for email address */ andre@0: if (nssPKIObjectCollection_Count(collection) == 0 && andre@0: PORT_Strchr(nickname, '@') != NULL) andre@0: { andre@0: char* lowercaseName = CERT_FixupEmailAddr(nickname); andre@0: if (lowercaseName) { andre@0: (void)nssTrustDomain_GetCertsForEmailAddressFromCache(defaultTD, andre@0: lowercaseName, andre@0: certList); andre@0: transfer_token_certs_to_collection(certList, token, collection); andre@0: instances = nssToken_FindCertificatesByEmail(token, andre@0: NULL, andre@0: lowercaseName, andre@0: tokenOnly, andre@0: 0, andre@0: &status); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: PORT_Free(lowercaseName); andre@0: } andre@0: } andre@0: certs = nssPKIObjectCollection_GetCertificates(collection, andre@0: NULL, 0, NULL); andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: if (certs) { andre@0: cert = nssCertificateArray_FindBestCertificate(certs, NULL, andre@0: &usage, NULL); andre@0: if (cert) { andre@0: rvCert = STAN_GetCERTCertificateOrRelease(cert); andre@0: } andre@0: nssCertificateArray_Destroy(certs); andre@0: } andre@0: nssList_Destroy(certList); andre@0: } andre@0: if (slot) { andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: if (nickCopy) PORT_Free(nickCopy); andre@0: return rvCert; andre@0: loser: andre@0: if (slot) { andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: if (nickCopy) PORT_Free(nickCopy); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Traverse slots callback */ andre@0: typedef struct FindCertsEmailArgStr { andre@0: char *email; andre@0: CERTCertList *certList; andre@0: } FindCertsEmailArg; andre@0: andre@0: SECStatus andre@0: FindCertsEmailCallback(CERTCertificate *cert, SECItem *item, void *arg) andre@0: { andre@0: FindCertsEmailArg *cbparam = (FindCertsEmailArg *) arg; andre@0: const char *cert_email = CERT_GetFirstEmailAddress(cert); andre@0: PRBool found = PR_FALSE; andre@0: andre@0: /* Email address present in certificate? */ andre@0: if (cert_email == NULL){ andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* Parameter correctly set? */ andre@0: if (cbparam->email == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* Loop over all email addresses */ andre@0: do { andre@0: if (!strcmp(cert_email, cbparam->email)) { andre@0: /* found one matching email address */ andre@0: PRTime now = PR_Now(); andre@0: found = PR_TRUE; andre@0: CERT_AddCertToListSorted(cbparam->certList, andre@0: CERT_DupCertificate(cert), andre@0: CERT_SortCBValidity, &now); andre@0: } andre@0: cert_email = CERT_GetNextEmailAddress(cert, cert_email); andre@0: } while (cert_email && !found); andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* Find all certificates with matching email address */ andre@0: CERTCertList * andre@0: PK11_FindCertsFromEmailAddress(const char *email, void *wincx) andre@0: { andre@0: FindCertsEmailArg cbparam; andre@0: SECStatus rv; andre@0: andre@0: cbparam.certList = CERT_NewCertList(); andre@0: if (cbparam.certList == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: cbparam.email = CERT_FixupEmailAddr(email); andre@0: if (cbparam.email == NULL) { andre@0: CERT_DestroyCertList(cbparam.certList); andre@0: return NULL; andre@0: } andre@0: andre@0: rv = PK11_TraverseSlotCerts(FindCertsEmailCallback, &cbparam, NULL); andre@0: if (rv != SECSuccess) { andre@0: CERT_DestroyCertList(cbparam.certList); andre@0: PORT_Free(cbparam.email); andre@0: return NULL; andre@0: } andre@0: andre@0: /* empty list? */ andre@0: if (CERT_LIST_HEAD(cbparam.certList) == NULL || andre@0: CERT_LIST_END(CERT_LIST_HEAD(cbparam.certList), cbparam.certList)) { andre@0: CERT_DestroyCertList(cbparam.certList); andre@0: cbparam.certList = NULL; andre@0: } andre@0: andre@0: PORT_Free(cbparam.email); andre@0: return cbparam.certList; andre@0: } andre@0: andre@0: andre@0: CERTCertList * andre@0: PK11_FindCertsFromNickname(const char *nickname, void *wincx) andre@0: { andre@0: char *nickCopy; andre@0: char *delimit = NULL; andre@0: char *tokenName; andre@0: int i; andre@0: CERTCertList *certList = NULL; andre@0: nssPKIObjectCollection *collection = NULL; andre@0: NSSCertificate **foundCerts = NULL; andre@0: NSSTrustDomain *defaultTD = STAN_GetDefaultTrustDomain(); andre@0: NSSCertificate *c; andre@0: NSSToken *token; andre@0: PK11SlotInfo *slot; andre@0: SECStatus rv; andre@0: andre@0: nickCopy = PORT_Strdup(nickname); andre@0: if (!nickCopy) { andre@0: /* error code is set */ andre@0: return NULL; andre@0: } andre@0: if ((delimit = PORT_Strchr(nickCopy,':')) != NULL) { andre@0: tokenName = nickCopy; andre@0: nickname = delimit + 1; andre@0: *delimit = '\0'; andre@0: /* find token by name */ andre@0: token = NSSTrustDomain_FindTokenByName(defaultTD, (NSSUTF8 *)tokenName); andre@0: if (token) { andre@0: slot = PK11_ReferenceSlot(token->pk11slot); andre@0: } else { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: slot = NULL; andre@0: } andre@0: *delimit = ':'; andre@0: } else { andre@0: slot = PK11_GetInternalKeySlot(); andre@0: token = PK11Slot_GetNSSToken(slot); andre@0: } andre@0: if (token) { andre@0: PRStatus status; andre@0: nssList *nameList; andre@0: nssCryptokiObject **instances; andre@0: nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly; andre@0: rv = pk11_AuthenticateUnfriendly(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: PK11_FreeSlot(slot); andre@0: if (nickCopy) PORT_Free(nickCopy); andre@0: return NULL; andre@0: } andre@0: collection = nssCertificateCollection_Create(defaultTD, NULL); andre@0: if (!collection) { andre@0: PK11_FreeSlot(slot); andre@0: if (nickCopy) PORT_Free(nickCopy); andre@0: return NULL; andre@0: } andre@0: nameList = nssList_Create(NULL, PR_FALSE); andre@0: if (!nameList) { andre@0: PK11_FreeSlot(slot); andre@0: if (nickCopy) PORT_Free(nickCopy); andre@0: return NULL; andre@0: } andre@0: (void)nssTrustDomain_GetCertsForNicknameFromCache(defaultTD, andre@0: nickname, andre@0: nameList); andre@0: transfer_token_certs_to_collection(nameList, token, collection); andre@0: instances = nssToken_FindCertificatesByNickname(token, andre@0: NULL, andre@0: nickname, andre@0: tokenOnly, andre@0: 0, andre@0: &status); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: andre@0: /* if it wasn't found, repeat the process for email address */ andre@0: if (nssPKIObjectCollection_Count(collection) == 0 && andre@0: PORT_Strchr(nickname, '@') != NULL) andre@0: { andre@0: char* lowercaseName = CERT_FixupEmailAddr(nickname); andre@0: if (lowercaseName) { andre@0: (void)nssTrustDomain_GetCertsForEmailAddressFromCache(defaultTD, andre@0: lowercaseName, andre@0: nameList); andre@0: transfer_token_certs_to_collection(nameList, token, collection); andre@0: instances = nssToken_FindCertificatesByEmail(token, andre@0: NULL, andre@0: lowercaseName, andre@0: tokenOnly, andre@0: 0, andre@0: &status); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: PORT_Free(lowercaseName); andre@0: } andre@0: } andre@0: andre@0: nssList_Destroy(nameList); andre@0: foundCerts = nssPKIObjectCollection_GetCertificates(collection, andre@0: NULL, 0, NULL); andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: } andre@0: if (slot) { andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: if (nickCopy) PORT_Free(nickCopy); andre@0: if (foundCerts) { andre@0: PRTime now = PR_Now(); andre@0: certList = CERT_NewCertList(); andre@0: for (i=0, c = *foundCerts; c; c = foundCerts[++i]) { andre@0: if (certList) { andre@0: CERTCertificate *certCert = STAN_GetCERTCertificateOrRelease(c); andre@0: /* c may be invalid after this, don't reference it */ andre@0: if (certCert) { andre@0: /* CERT_AddCertToListSorted adopts certCert */ andre@0: CERT_AddCertToListSorted(certList, certCert, andre@0: CERT_SortCBValidity, &now); andre@0: } andre@0: } else { andre@0: nssCertificate_Destroy(c); andre@0: } andre@0: } andre@0: if (certList && CERT_LIST_HEAD(certList) == NULL) { andre@0: CERT_DestroyCertList(certList); andre@0: certList = NULL; andre@0: } andre@0: /* all the certs have been adopted or freed, free the raw array */ andre@0: nss_ZFreeIf(foundCerts); andre@0: } andre@0: return certList; andre@0: } andre@0: andre@0: /* andre@0: * extract a key ID for a certificate... andre@0: * NOTE: We call this function from PKCS11.c If we ever use andre@0: * pkcs11 to extract the public key (we currently do not), this will break. andre@0: */ andre@0: SECItem * andre@0: PK11_GetPubIndexKeyID(CERTCertificate *cert) andre@0: { andre@0: SECKEYPublicKey *pubk; andre@0: SECItem *newItem = NULL; andre@0: andre@0: pubk = CERT_ExtractPublicKey(cert); andre@0: if (pubk == NULL) return NULL; andre@0: andre@0: switch (pubk->keyType) { andre@0: case rsaKey: andre@0: newItem = SECITEM_DupItem(&pubk->u.rsa.modulus); andre@0: break; andre@0: case dsaKey: andre@0: newItem = SECITEM_DupItem(&pubk->u.dsa.publicValue); andre@0: break; andre@0: case dhKey: andre@0: newItem = SECITEM_DupItem(&pubk->u.dh.publicValue); andre@0: break; andre@0: case ecKey: andre@0: newItem = SECITEM_DupItem(&pubk->u.ec.publicValue); andre@0: break; andre@0: case fortezzaKey: andre@0: default: andre@0: newItem = NULL; /* Fortezza Fix later... */ andre@0: } andre@0: SECKEY_DestroyPublicKey(pubk); andre@0: /* make hash of it */ andre@0: return newItem; andre@0: } andre@0: andre@0: /* andre@0: * generate a CKA_ID from a certificate. andre@0: */ andre@0: SECItem * andre@0: pk11_mkcertKeyID(CERTCertificate *cert) andre@0: { andre@0: SECItem *pubKeyData = PK11_GetPubIndexKeyID(cert) ; andre@0: SECItem *certCKA_ID; andre@0: andre@0: if (pubKeyData == NULL) return NULL; andre@0: andre@0: certCKA_ID = PK11_MakeIDFromPubKey(pubKeyData); andre@0: SECITEM_FreeItem(pubKeyData,PR_TRUE); andre@0: return certCKA_ID; andre@0: } andre@0: andre@0: /* andre@0: * Write the cert into the token. andre@0: */ andre@0: SECStatus andre@0: PK11_ImportCert(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: CK_OBJECT_HANDLE key, const char *nickname, andre@0: PRBool includeTrust) andre@0: { andre@0: PRStatus status; andre@0: NSSCertificate *c; andre@0: nssCryptokiObject *keyobj, *certobj; andre@0: NSSToken *token = PK11Slot_GetNSSToken(slot); andre@0: SECItem *keyID = pk11_mkcertKeyID(cert); andre@0: char *emailAddr = NULL; andre@0: nssCertificateStoreTrace lockTrace = {NULL, NULL, PR_FALSE, PR_FALSE}; andre@0: nssCertificateStoreTrace unlockTrace = {NULL, NULL, PR_FALSE, PR_FALSE}; andre@0: andre@0: if (keyID == NULL) { andre@0: goto loser; /* error code should be set already */ andre@0: } andre@0: if (!token) { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: goto loser; andre@0: } andre@0: andre@0: if (PK11_IsInternal(slot) && cert->emailAddr && cert->emailAddr[0]) { andre@0: emailAddr = cert->emailAddr; andre@0: } andre@0: andre@0: /* need to get the cert as a stan cert */ andre@0: if (cert->nssCertificate) { andre@0: c = cert->nssCertificate; andre@0: } else { andre@0: c = STAN_GetNSSCertificate(cert); andre@0: if (c == NULL) { andre@0: goto loser; andre@0: } andre@0: } andre@0: andre@0: /* set the id for the cert */ andre@0: nssItem_Create(c->object.arena, &c->id, keyID->len, keyID->data); andre@0: if (!c->id.data) { andre@0: goto loser; andre@0: } andre@0: andre@0: if (key != CK_INVALID_HANDLE) { andre@0: /* create an object for the key, ... */ andre@0: keyobj = nss_ZNEW(NULL, nssCryptokiObject); andre@0: if (!keyobj) { andre@0: goto loser; andre@0: } andre@0: keyobj->token = nssToken_AddRef(token); andre@0: keyobj->handle = key; andre@0: keyobj->isTokenObject = PR_TRUE; andre@0: andre@0: /* ... in order to set matching attributes for the key */ andre@0: status = nssCryptokiPrivateKey_SetCertificate(keyobj, NULL, nickname, andre@0: &c->id, &c->subject); andre@0: nssCryptokiObject_Destroy(keyobj); andre@0: if (status != PR_SUCCESS) { andre@0: goto loser; andre@0: } andre@0: } andre@0: andre@0: /* do the token import */ andre@0: certobj = nssToken_ImportCertificate(token, NULL, andre@0: NSSCertificateType_PKIX, andre@0: &c->id, andre@0: nickname, andre@0: &c->encoding, andre@0: &c->issuer, andre@0: &c->subject, andre@0: &c->serial, andre@0: emailAddr, andre@0: PR_TRUE); andre@0: if (!certobj) { andre@0: if (NSS_GetError() == NSS_ERROR_INVALID_CERTIFICATE) { andre@0: PORT_SetError(SEC_ERROR_REUSED_ISSUER_AND_SERIAL); andre@0: SECITEM_FreeItem(keyID,PR_TRUE); andre@0: return SECFailure; andre@0: } andre@0: goto loser; andre@0: } andre@0: andre@0: if (c->object.cryptoContext) { andre@0: /* Delete the temp instance */ andre@0: NSSCryptoContext *cc = c->object.cryptoContext; andre@0: nssCertificateStore_Lock(cc->certStore, &lockTrace); andre@0: nssCertificateStore_RemoveCertLOCKED(cc->certStore, c); andre@0: nssCertificateStore_Unlock(cc->certStore, &lockTrace, &unlockTrace); andre@0: c->object.cryptoContext = NULL; andre@0: cert->istemp = PR_FALSE; andre@0: cert->isperm = PR_TRUE; andre@0: } andre@0: andre@0: /* add the new instance to the cert, force an update of the andre@0: * CERTCertificate, and finish andre@0: */ andre@0: nssPKIObject_AddInstance(&c->object, certobj); andre@0: /* nssTrustDomain_AddCertsToCache may release a reference to 'c' and andre@0: * replace 'c' by a different value. So we add a reference to 'c' to andre@0: * prevent 'c' from being destroyed. */ andre@0: nssCertificate_AddRef(c); andre@0: nssTrustDomain_AddCertsToCache(STAN_GetDefaultTrustDomain(), &c, 1); andre@0: /* XXX should we pass the original value of 'c' to andre@0: * STAN_ForceCERTCertificateUpdate? */ andre@0: (void)STAN_ForceCERTCertificateUpdate(c); andre@0: nssCertificate_Destroy(c); andre@0: SECITEM_FreeItem(keyID,PR_TRUE); andre@0: return SECSuccess; andre@0: loser: andre@0: CERT_MapStanError(); andre@0: SECITEM_FreeItem(keyID,PR_TRUE); andre@0: if (PORT_GetError() != SEC_ERROR_TOKEN_NOT_LOGGED_IN) { andre@0: PORT_SetError(SEC_ERROR_ADDING_CERT); andre@0: } andre@0: return SECFailure; andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_ImportDERCert(PK11SlotInfo *slot, SECItem *derCert, andre@0: CK_OBJECT_HANDLE key, char *nickname, PRBool includeTrust) andre@0: { andre@0: CERTCertificate *cert; andre@0: SECStatus rv; andre@0: andre@0: cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(), andre@0: derCert, NULL, PR_FALSE, PR_TRUE); andre@0: if (cert == NULL) return SECFailure; andre@0: andre@0: rv = PK11_ImportCert(slot, cert, key, nickname, includeTrust); andre@0: CERT_DestroyCertificate (cert); andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * get a certificate handle, look at the cached handle first.. andre@0: */ andre@0: CK_OBJECT_HANDLE andre@0: pk11_getcerthandle(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: CK_ATTRIBUTE *theTemplate,int tsize) andre@0: { andre@0: CK_OBJECT_HANDLE certh; andre@0: andre@0: if (cert->slot == slot) { andre@0: certh = cert->pkcs11ID; andre@0: if ((certh == CK_INVALID_HANDLE) || andre@0: (cert->series != slot->series)) { andre@0: certh = pk11_FindObjectByTemplate(slot,theTemplate,tsize); andre@0: cert->pkcs11ID = certh; andre@0: cert->series = slot->series; andre@0: } andre@0: } else { andre@0: certh = pk11_FindObjectByTemplate(slot,theTemplate,tsize); andre@0: } andre@0: return certh; andre@0: } andre@0: andre@0: /* andre@0: * return the private key From a given Cert andre@0: */ andre@0: SECKEYPrivateKey * andre@0: PK11_FindPrivateKeyFromCert(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: void *wincx) andre@0: { andre@0: int err; andre@0: CK_OBJECT_CLASS certClass = CKO_CERTIFICATE; andre@0: CK_ATTRIBUTE theTemplate[] = { andre@0: { CKA_VALUE, NULL, 0 }, andre@0: { CKA_CLASS, NULL, 0 } andre@0: }; andre@0: /* if you change the array, change the variable below as well */ andre@0: int tsize = sizeof(theTemplate)/sizeof(theTemplate[0]); andre@0: CK_OBJECT_HANDLE certh; andre@0: CK_OBJECT_HANDLE keyh; andre@0: CK_ATTRIBUTE *attrs = theTemplate; andre@0: PRBool needLogin; andre@0: SECStatus rv; andre@0: andre@0: PK11_SETATTRS(attrs, CKA_VALUE, cert->derCert.data, andre@0: cert->derCert.len); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &certClass, sizeof(certClass)); andre@0: andre@0: /* andre@0: * issue the find andre@0: */ andre@0: rv = pk11_AuthenticateUnfriendly(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: return NULL; andre@0: } andre@0: andre@0: certh = pk11_getcerthandle(slot,cert,theTemplate,tsize); andre@0: if (certh == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: /* andre@0: * prevent a login race condition. If slot is logged in between andre@0: * our call to pk11_LoginStillRequired and the andre@0: * PK11_MatchItem. The matchItem call will either succeed, or andre@0: * we will call it one more time after calling PK11_Authenticate andre@0: * (which is a noop on an authenticated token). andre@0: */ andre@0: needLogin = pk11_LoginStillRequired(slot,wincx); andre@0: keyh = PK11_MatchItem(slot,certh,CKO_PRIVATE_KEY); andre@0: if ((keyh == CK_INVALID_HANDLE) && needLogin && andre@0: (SSL_ERROR_NO_CERTIFICATE == (err = PORT_GetError()) || andre@0: SEC_ERROR_TOKEN_NOT_LOGGED_IN == err )) { andre@0: /* try it again authenticated */ andre@0: rv = PK11_Authenticate(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: return NULL; andre@0: } andre@0: keyh = PK11_MatchItem(slot,certh,CKO_PRIVATE_KEY); andre@0: } andre@0: if (keyh == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: return PK11_MakePrivKey(slot, nullKey, PR_TRUE, keyh, wincx); andre@0: } andre@0: andre@0: /* andre@0: * import a cert for a private key we have already generated. Set the label andre@0: * on both to be the nickname. This is for the Key Gen, orphaned key case. andre@0: */ andre@0: PK11SlotInfo * andre@0: PK11_KeyForCertExists(CERTCertificate *cert, CK_OBJECT_HANDLE *keyPtr, andre@0: void *wincx) andre@0: { andre@0: PK11SlotList *list; andre@0: PK11SlotListElement *le; andre@0: SECItem *keyID; andre@0: CK_OBJECT_HANDLE key; andre@0: PK11SlotInfo *slot = NULL; andre@0: SECStatus rv; andre@0: int err; andre@0: andre@0: keyID = pk11_mkcertKeyID(cert); andre@0: /* get them all! */ andre@0: list = PK11_GetAllTokens(CKM_INVALID_MECHANISM,PR_FALSE,PR_TRUE,wincx); andre@0: if ((keyID == NULL) || (list == NULL)) { andre@0: if (keyID) SECITEM_FreeItem(keyID,PR_TRUE); andre@0: if (list) PK11_FreeSlotList(list); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Look for the slot that holds the Key */ andre@0: for (le = list->head ; le; le = le->next) { andre@0: /* andre@0: * prevent a login race condition. If le->slot is logged in between andre@0: * our call to pk11_LoginStillRequired and the andre@0: * pk11_FindPrivateKeyFromCertID, the find will either succeed, or andre@0: * we will call it one more time after calling PK11_Authenticate andre@0: * (which is a noop on an authenticated token). andre@0: */ andre@0: PRBool needLogin = pk11_LoginStillRequired(le->slot,wincx); andre@0: key = pk11_FindPrivateKeyFromCertID(le->slot,keyID); andre@0: if ((key == CK_INVALID_HANDLE) && needLogin && andre@0: (SSL_ERROR_NO_CERTIFICATE == (err = PORT_GetError()) || andre@0: SEC_ERROR_TOKEN_NOT_LOGGED_IN == err )) { andre@0: /* authenticate and try again */ andre@0: rv = PK11_Authenticate(le->slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) continue; andre@0: key = pk11_FindPrivateKeyFromCertID(le->slot,keyID); andre@0: } andre@0: if (key != CK_INVALID_HANDLE) { andre@0: slot = PK11_ReferenceSlot(le->slot); andre@0: if (keyPtr) *keyPtr = key; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: SECITEM_FreeItem(keyID,PR_TRUE); andre@0: PK11_FreeSlotList(list); andre@0: return slot; andre@0: andre@0: } andre@0: /* andre@0: * import a cert for a private key we have already generated. Set the label andre@0: * on both to be the nickname. This is for the Key Gen, orphaned key case. andre@0: */ andre@0: PK11SlotInfo * andre@0: PK11_KeyForDERCertExists(SECItem *derCert, CK_OBJECT_HANDLE *keyPtr, andre@0: void *wincx) andre@0: { andre@0: CERTCertificate *cert; andre@0: PK11SlotInfo *slot = NULL; andre@0: andre@0: /* letting this use go -- the only thing that the cert is used for is andre@0: * to get the ID attribute. andre@0: */ andre@0: cert = CERT_DecodeDERCertificate(derCert, PR_FALSE, NULL); andre@0: if (cert == NULL) return NULL; andre@0: andre@0: slot = PK11_KeyForCertExists(cert, keyPtr, wincx); andre@0: CERT_DestroyCertificate (cert); andre@0: return slot; andre@0: } andre@0: andre@0: PK11SlotInfo * andre@0: PK11_ImportCertForKey(CERTCertificate *cert, const char *nickname, andre@0: void *wincx) andre@0: { andre@0: PK11SlotInfo *slot = NULL; andre@0: CK_OBJECT_HANDLE key; andre@0: andre@0: slot = PK11_KeyForCertExists(cert,&key,wincx); andre@0: andre@0: if (slot) { andre@0: if (PK11_ImportCert(slot,cert,key,nickname,PR_FALSE) != SECSuccess) { andre@0: PK11_FreeSlot(slot); andre@0: slot = NULL; andre@0: } andre@0: } else { andre@0: PORT_SetError(SEC_ERROR_ADDING_CERT); andre@0: } andre@0: andre@0: return slot; andre@0: } andre@0: andre@0: PK11SlotInfo * andre@0: PK11_ImportDERCertForKey(SECItem *derCert, char *nickname,void *wincx) andre@0: { andre@0: CERTCertificate *cert; andre@0: PK11SlotInfo *slot = NULL; andre@0: andre@0: cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(), andre@0: derCert, NULL, PR_FALSE, PR_TRUE); andre@0: if (cert == NULL) return NULL; andre@0: andre@0: slot = PK11_ImportCertForKey(cert, nickname, wincx); andre@0: CERT_DestroyCertificate (cert); andre@0: return slot; andre@0: } andre@0: andre@0: static CK_OBJECT_HANDLE andre@0: pk11_FindCertObjectByTemplate(PK11SlotInfo **slotPtr, andre@0: CK_ATTRIBUTE *searchTemplate, int count, void *wincx) andre@0: { andre@0: PK11SlotList *list; andre@0: PK11SlotListElement *le; andre@0: CK_OBJECT_HANDLE certHandle = CK_INVALID_HANDLE; andre@0: PK11SlotInfo *slot = NULL; andre@0: SECStatus rv; andre@0: andre@0: *slotPtr = NULL; andre@0: andre@0: /* get them all! */ andre@0: list = PK11_GetAllTokens(CKM_INVALID_MECHANISM,PR_FALSE,PR_TRUE,wincx); andre@0: if (list == NULL) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: andre@0: /* Look for the slot that holds the Key */ andre@0: for (le = list->head ; le; le = le->next) { andre@0: rv = pk11_AuthenticateUnfriendly(le->slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) continue; andre@0: andre@0: certHandle = pk11_FindObjectByTemplate(le->slot,searchTemplate,count); andre@0: if (certHandle != CK_INVALID_HANDLE) { andre@0: slot = PK11_ReferenceSlot(le->slot); andre@0: break; andre@0: } andre@0: } andre@0: andre@0: PK11_FreeSlotList(list); andre@0: andre@0: if (slot == NULL) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: *slotPtr = slot; andre@0: return certHandle; andre@0: } andre@0: andre@0: CERTCertificate * andre@0: PK11_FindCertByIssuerAndSNOnToken(PK11SlotInfo *slot, andre@0: CERTIssuerAndSN *issuerSN, void *wincx) andre@0: { andre@0: CERTCertificate *rvCert = NULL; andre@0: NSSCertificate *cert = NULL; andre@0: NSSDER issuer, serial; andre@0: NSSTrustDomain *td = STAN_GetDefaultTrustDomain(); andre@0: NSSToken *token = slot->nssToken; andre@0: nssSession *session; andre@0: nssCryptokiObject *instance = NULL; andre@0: nssPKIObject *object = NULL; andre@0: SECItem *derSerial; andre@0: PRStatus status; andre@0: andre@0: if (!issuerSN || !issuerSN->derIssuer.data || !issuerSN->derIssuer.len || andre@0: !issuerSN->serialNumber.data || !issuerSN->serialNumber.len || andre@0: issuerSN->derIssuer.len > CERT_MAX_DN_BYTES || andre@0: issuerSN->serialNumber.len > CERT_MAX_SERIAL_NUMBER_BYTES ) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: /* Paranoia */ andre@0: if (token == NULL) { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: return NULL; andre@0: } andre@0: andre@0: andre@0: /* PKCS#11 needs to use DER-encoded serial numbers. Create a andre@0: * CERTIssuerAndSN that actually has the encoded value and pass that andre@0: * to PKCS#11 (and the crypto context). andre@0: */ andre@0: derSerial = SEC_ASN1EncodeItem(NULL, NULL, andre@0: &issuerSN->serialNumber, andre@0: SEC_ASN1_GET(SEC_IntegerTemplate)); andre@0: if (!derSerial) { andre@0: return NULL; andre@0: } andre@0: andre@0: NSSITEM_FROM_SECITEM(&issuer, &issuerSN->derIssuer); andre@0: NSSITEM_FROM_SECITEM(&serial, derSerial); andre@0: andre@0: session = nssToken_GetDefaultSession(token); andre@0: if (!session) { andre@0: goto loser; andre@0: } andre@0: andre@0: instance = nssToken_FindCertificateByIssuerAndSerialNumber(token,session, andre@0: &issuer, &serial, nssTokenSearchType_TokenForced, &status); andre@0: andre@0: SECITEM_FreeItem(derSerial, PR_TRUE); andre@0: andre@0: if (!instance) { andre@0: goto loser; andre@0: } andre@0: object = nssPKIObject_Create(NULL, instance, td, NULL, nssPKIMonitor); andre@0: if (!object) { andre@0: goto loser; andre@0: } andre@0: instance = NULL; /* adopted by the previous call */ andre@0: cert = nssCertificate_Create(object); andre@0: if (!cert) { andre@0: goto loser; andre@0: } andre@0: object = NULL; /* adopted by the previous call */ andre@0: nssTrustDomain_AddCertsToCache(td, &cert,1); andre@0: /* on failure, cert is freed below */ andre@0: rvCert = STAN_GetCERTCertificate(cert); andre@0: if (!rvCert) { andre@0: goto loser; andre@0: } andre@0: return rvCert; andre@0: andre@0: loser: andre@0: if (instance) { andre@0: nssCryptokiObject_Destroy(instance); andre@0: } andre@0: if (object) { andre@0: nssPKIObject_Destroy(object); andre@0: } andre@0: if (cert) { andre@0: nssCertificate_Destroy(cert); andre@0: } andre@0: return NULL; andre@0: } andre@0: andre@0: static PRCallOnceType keyIDHashCallOnce; andre@0: andre@0: static PRStatus PR_CALLBACK andre@0: pk11_keyIDHash_populate(void *wincx) andre@0: { andre@0: CERTCertList *certList; andre@0: CERTCertListNode *node = NULL; andre@0: SECItem subjKeyID = {siBuffer, NULL, 0}; andre@0: SECItem *slotid = NULL; andre@0: SECMODModuleList *modules, *mlp; andre@0: SECMODListLock *moduleLock; andre@0: int i; andre@0: andre@0: certList = PK11_ListCerts(PK11CertListUser, wincx); andre@0: if (!certList) { andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: for (node = CERT_LIST_HEAD(certList); andre@0: !CERT_LIST_END(node, certList); andre@0: node = CERT_LIST_NEXT(node)) { andre@0: if (CERT_FindSubjectKeyIDExtension(node->cert, andre@0: &subjKeyID) == SECSuccess && andre@0: subjKeyID.data != NULL) { andre@0: cert_AddSubjectKeyIDMapping(&subjKeyID, node->cert); andre@0: SECITEM_FreeItem(&subjKeyID, PR_FALSE); andre@0: } andre@0: } andre@0: CERT_DestroyCertList(certList); andre@0: andre@0: /* andre@0: * Record the state of each slot in a hash. The concatenation of slotID andre@0: * and moduleID is used as its key, with the slot series as its value. andre@0: */ andre@0: slotid = SECITEM_AllocItem(NULL, NULL, andre@0: sizeof(CK_SLOT_ID) + sizeof(SECMODModuleID)); andre@0: if (!slotid) { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: return PR_FAILURE; andre@0: } andre@0: moduleLock = SECMOD_GetDefaultModuleListLock(); andre@0: if (!moduleLock) { andre@0: PORT_SetError(SEC_ERROR_NOT_INITIALIZED); andre@0: return PR_FAILURE; andre@0: } andre@0: SECMOD_GetReadLock(moduleLock); andre@0: modules = SECMOD_GetDefaultModuleList(); andre@0: for (mlp = modules; mlp; mlp = mlp->next) { andre@0: for (i = 0; i < mlp->module->slotCount; i++) { andre@0: memcpy(slotid->data, &mlp->module->slots[i]->slotID, andre@0: sizeof(CK_SLOT_ID)); andre@0: memcpy(&slotid->data[sizeof(CK_SLOT_ID)], &mlp->module->moduleID, andre@0: sizeof(SECMODModuleID)); andre@0: cert_UpdateSubjectKeyIDSlotCheck(slotid, andre@0: mlp->module->slots[i]->series); andre@0: } andre@0: } andre@0: SECMOD_ReleaseReadLock(moduleLock); andre@0: SECITEM_FreeItem(slotid, PR_TRUE); andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: * We're looking for a cert which we have the private key for that's on the andre@0: * list of recipients. This searches one slot. andre@0: * this is the new version for NSS SMIME code andre@0: * this stuff should REALLY be in the SMIME code, but some things in here are not public andre@0: * (they should be!) andre@0: */ andre@0: static CERTCertificate * andre@0: pk11_FindCertObjectByRecipientNew(PK11SlotInfo *slot, NSSCMSRecipient **recipientlist, int *rlIndex, void *pwarg) andre@0: { andre@0: NSSCMSRecipient *ri = NULL; andre@0: int i; andre@0: PRBool tokenRescanDone = PR_FALSE; andre@0: CERTCertTrust trust; andre@0: andre@0: for (i=0; (ri = recipientlist[i]) != NULL; i++) { andre@0: CERTCertificate *cert = NULL; andre@0: if (ri->kind == RLSubjKeyID) { andre@0: SECItem *derCert = cert_FindDERCertBySubjectKeyID(ri->id.subjectKeyID); andre@0: if (!derCert && !tokenRescanDone) { andre@0: /* andre@0: * We didn't find the cert by its key ID. If we have slots andre@0: * with removable tokens, a failure from andre@0: * cert_FindDERCertBySubjectKeyID doesn't necessarily imply andre@0: * that the cert is unavailable - the token might simply andre@0: * have been inserted after the initial run of andre@0: * pk11_keyIDHash_populate (wrapped by PR_CallOnceWithArg), andre@0: * or a different token might have been present in that andre@0: * slot, initially. Let's check for new tokens... andre@0: */ andre@0: PK11SlotList *sl = PK11_GetAllTokens(CKM_INVALID_MECHANISM, andre@0: PR_FALSE, PR_FALSE, pwarg); andre@0: if (sl) { andre@0: PK11SlotListElement *le; andre@0: SECItem *slotid = SECITEM_AllocItem(NULL, NULL, andre@0: sizeof(CK_SLOT_ID) + sizeof(SECMODModuleID)); andre@0: if (!slotid) { andre@0: PORT_SetError(SEC_ERROR_NO_MEMORY); andre@0: return NULL; andre@0: } andre@0: for (le = sl->head; le; le = le->next) { andre@0: memcpy(slotid->data, &le->slot->slotID, andre@0: sizeof(CK_SLOT_ID)); andre@0: memcpy(&slotid->data[sizeof(CK_SLOT_ID)], andre@0: &le->slot->module->moduleID, andre@0: sizeof(SECMODModuleID)); andre@0: /* andre@0: * Any changes with the slot since our last check? andre@0: * If so, re-read the certs in that specific slot. andre@0: */ andre@0: if (cert_SubjectKeyIDSlotCheckSeries(slotid) andre@0: != PK11_GetSlotSeries(le->slot)) { andre@0: CERTCertListNode *node = NULL; andre@0: SECItem subjKeyID = {siBuffer, NULL, 0}; andre@0: CERTCertList *cl = PK11_ListCertsInSlot(le->slot); andre@0: if (!cl) { andre@0: continue; andre@0: } andre@0: for (node = CERT_LIST_HEAD(cl); andre@0: !CERT_LIST_END(node, cl); andre@0: node = CERT_LIST_NEXT(node)) { andre@0: if (CERT_IsUserCert(node->cert) && andre@0: CERT_FindSubjectKeyIDExtension(node->cert, andre@0: &subjKeyID) == SECSuccess) { andre@0: if (subjKeyID.data) { andre@0: cert_AddSubjectKeyIDMapping(&subjKeyID, andre@0: node->cert); andre@0: cert_UpdateSubjectKeyIDSlotCheck(slotid, andre@0: PK11_GetSlotSeries(le->slot)); andre@0: } andre@0: SECITEM_FreeItem(&subjKeyID, PR_FALSE); andre@0: } andre@0: } andre@0: CERT_DestroyCertList(cl); andre@0: } andre@0: } andre@0: PK11_FreeSlotList(sl); andre@0: SECITEM_FreeItem(slotid, PR_TRUE); andre@0: } andre@0: /* only check once per message/recipientlist */ andre@0: tokenRescanDone = PR_TRUE; andre@0: /* do another lookup (hopefully we found that cert...) */ andre@0: derCert = cert_FindDERCertBySubjectKeyID(ri->id.subjectKeyID); andre@0: } andre@0: if (derCert) { andre@0: cert = PK11_FindCertFromDERCertItem(slot, derCert, pwarg); andre@0: SECITEM_FreeItem(derCert, PR_TRUE); andre@0: } andre@0: } else { andre@0: cert = PK11_FindCertByIssuerAndSNOnToken(slot, ri->id.issuerAndSN, andre@0: pwarg); andre@0: } andre@0: if (cert) { andre@0: /* this isn't our cert */ andre@0: if (CERT_GetCertTrust(cert, &trust) != SECSuccess || andre@0: ((trust.emailFlags & CERTDB_USER) != CERTDB_USER)) { andre@0: CERT_DestroyCertificate(cert); andre@0: continue; andre@0: } andre@0: ri->slot = PK11_ReferenceSlot(slot); andre@0: *rlIndex = i; andre@0: return cert; andre@0: } andre@0: } andre@0: *rlIndex = -1; andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * This function is the same as above, but it searches all the slots. andre@0: * this is the new version for NSS SMIME code andre@0: * this stuff should REALLY be in the SMIME code, but some things in here are not public andre@0: * (they should be!) andre@0: */ andre@0: static CERTCertificate * andre@0: pk11_AllFindCertObjectByRecipientNew(NSSCMSRecipient **recipientlist, void *wincx, int *rlIndex) andre@0: { andre@0: PK11SlotList *list; andre@0: PK11SlotListElement *le; andre@0: CERTCertificate *cert = NULL; andre@0: SECStatus rv; andre@0: andre@0: /* get them all! */ andre@0: list = PK11_GetAllTokens(CKM_INVALID_MECHANISM,PR_FALSE,PR_TRUE,wincx); andre@0: if (list == NULL) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: /* Look for the slot that holds the Key */ andre@0: for (le = list->head ; le; le = le->next) { andre@0: rv = pk11_AuthenticateUnfriendly(le->slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) continue; andre@0: andre@0: cert = pk11_FindCertObjectByRecipientNew(le->slot, andre@0: recipientlist, rlIndex, wincx); andre@0: if (cert) andre@0: break; andre@0: } andre@0: andre@0: PK11_FreeSlotList(list); andre@0: andre@0: return cert; andre@0: } andre@0: andre@0: /* andre@0: * We're looking for a cert which we have the private key for that's on the andre@0: * list of recipients. This searches one slot. andre@0: */ andre@0: static CERTCertificate * andre@0: pk11_FindCertObjectByRecipient(PK11SlotInfo *slot, andre@0: SEC_PKCS7RecipientInfo **recipientArray, andre@0: SEC_PKCS7RecipientInfo **rip, void *pwarg) andre@0: { andre@0: SEC_PKCS7RecipientInfo *ri = NULL; andre@0: CERTCertTrust trust; andre@0: int i; andre@0: andre@0: for (i=0; (ri = recipientArray[i]) != NULL; i++) { andre@0: CERTCertificate *cert; andre@0: andre@0: cert = PK11_FindCertByIssuerAndSNOnToken(slot, ri->issuerAndSN, andre@0: pwarg); andre@0: if (cert) { andre@0: /* this isn't our cert */ andre@0: if (CERT_GetCertTrust(cert, &trust) != SECSuccess || andre@0: ((trust.emailFlags & CERTDB_USER) != CERTDB_USER)) { andre@0: CERT_DestroyCertificate(cert); andre@0: continue; andre@0: } andre@0: *rip = ri; andre@0: return cert; andre@0: } andre@0: andre@0: } andre@0: *rip = NULL; andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * This function is the same as above, but it searches all the slots. andre@0: */ andre@0: static CERTCertificate * andre@0: pk11_AllFindCertObjectByRecipient(PK11SlotInfo **slotPtr, andre@0: SEC_PKCS7RecipientInfo **recipientArray,SEC_PKCS7RecipientInfo **rip, andre@0: void *wincx) andre@0: { andre@0: PK11SlotList *list; andre@0: PK11SlotListElement *le; andre@0: CERTCertificate * cert = NULL; andre@0: PK11SlotInfo *slot = NULL; andre@0: SECStatus rv; andre@0: andre@0: *slotPtr = NULL; andre@0: andre@0: /* get them all! */ andre@0: list = PK11_GetAllTokens(CKM_INVALID_MECHANISM,PR_FALSE,PR_TRUE,wincx); andre@0: if (list == NULL) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: *rip = NULL; andre@0: andre@0: /* Look for the slot that holds the Key */ andre@0: for (le = list->head ; le; le = le->next) { andre@0: rv = pk11_AuthenticateUnfriendly(le->slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) continue; andre@0: andre@0: cert = pk11_FindCertObjectByRecipient(le->slot, recipientArray, andre@0: rip, wincx); andre@0: if (cert) { andre@0: slot = PK11_ReferenceSlot(le->slot); andre@0: break; andre@0: } andre@0: } andre@0: andre@0: PK11_FreeSlotList(list); andre@0: andre@0: if (slot == NULL) { andre@0: return NULL; andre@0: } andre@0: *slotPtr = slot; andre@0: PORT_Assert(cert != NULL); andre@0: return cert; andre@0: } andre@0: andre@0: /* andre@0: * We need to invert the search logic for PKCS 7 because if we search for andre@0: * each cert on the list over all the slots, we wind up with lots of spurious andre@0: * password prompts. This way we get only one password prompt per slot, at andre@0: * the max, and most of the time we can find the cert, and only prompt for andre@0: * the key... andre@0: */ andre@0: CERTCertificate * andre@0: PK11_FindCertAndKeyByRecipientList(PK11SlotInfo **slotPtr, andre@0: SEC_PKCS7RecipientInfo **array, SEC_PKCS7RecipientInfo **rip, andre@0: SECKEYPrivateKey**privKey, void *wincx) andre@0: { andre@0: CERTCertificate *cert = NULL; andre@0: andre@0: *privKey = NULL; andre@0: *slotPtr = NULL; andre@0: cert = pk11_AllFindCertObjectByRecipient(slotPtr,array,rip,wincx); andre@0: if (!cert) { andre@0: return NULL; andre@0: } andre@0: andre@0: *privKey = PK11_FindKeyByAnyCert(cert, wincx); andre@0: if (*privKey == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: return cert; andre@0: loser: andre@0: if (cert) CERT_DestroyCertificate(cert); andre@0: if (*slotPtr) PK11_FreeSlot(*slotPtr); andre@0: *slotPtr = NULL; andre@0: return NULL; andre@0: } andre@0: andre@0: /* andre@0: * This is the new version of the above function for NSS SMIME code andre@0: * this stuff should REALLY be in the SMIME code, but some things in here are not public andre@0: * (they should be!) andre@0: */ andre@0: int andre@0: PK11_FindCertAndKeyByRecipientListNew(NSSCMSRecipient **recipientlist, void *wincx) andre@0: { andre@0: CERTCertificate *cert; andre@0: NSSCMSRecipient *rl; andre@0: PRStatus rv; andre@0: int rlIndex; andre@0: andre@0: rv = PR_CallOnceWithArg(&keyIDHashCallOnce, pk11_keyIDHash_populate, wincx); andre@0: if (rv != PR_SUCCESS) andre@0: return -1; andre@0: andre@0: cert = pk11_AllFindCertObjectByRecipientNew(recipientlist, wincx, &rlIndex); andre@0: if (!cert) { andre@0: return -1; andre@0: } andre@0: andre@0: rl = recipientlist[rlIndex]; andre@0: andre@0: /* at this point, rl->slot is set */ andre@0: andre@0: rl->privkey = PK11_FindKeyByAnyCert(cert, wincx); andre@0: if (rl->privkey == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* make a cert from the cert handle */ andre@0: rl->cert = cert; andre@0: return rlIndex; andre@0: andre@0: loser: andre@0: if (cert) CERT_DestroyCertificate(cert); andre@0: if (rl->slot) PK11_FreeSlot(rl->slot); andre@0: rl->slot = NULL; andre@0: return -1; andre@0: } andre@0: andre@0: CERTCertificate * andre@0: PK11_FindCertByIssuerAndSN(PK11SlotInfo **slotPtr, CERTIssuerAndSN *issuerSN, andre@0: void *wincx) andre@0: { andre@0: CERTCertificate *rvCert = NULL; andre@0: NSSCertificate *cert; andre@0: NSSDER issuer, serial; andre@0: NSSCryptoContext *cc; andre@0: SECItem *derSerial; andre@0: andre@0: if (!issuerSN || !issuerSN->derIssuer.data || !issuerSN->derIssuer.len || andre@0: !issuerSN->serialNumber.data || !issuerSN->serialNumber.len || andre@0: issuerSN->derIssuer.len > CERT_MAX_DN_BYTES || andre@0: issuerSN->serialNumber.len > CERT_MAX_SERIAL_NUMBER_BYTES ) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: if (slotPtr) *slotPtr = NULL; andre@0: andre@0: /* PKCS#11 needs to use DER-encoded serial numbers. Create a andre@0: * CERTIssuerAndSN that actually has the encoded value and pass that andre@0: * to PKCS#11 (and the crypto context). andre@0: */ andre@0: derSerial = SEC_ASN1EncodeItem(NULL, NULL, andre@0: &issuerSN->serialNumber, andre@0: SEC_ASN1_GET(SEC_IntegerTemplate)); andre@0: if (!derSerial) { andre@0: return NULL; andre@0: } andre@0: andre@0: NSSITEM_FROM_SECITEM(&issuer, &issuerSN->derIssuer); andre@0: NSSITEM_FROM_SECITEM(&serial, derSerial); andre@0: andre@0: cc = STAN_GetDefaultCryptoContext(); andre@0: cert = NSSCryptoContext_FindCertificateByIssuerAndSerialNumber(cc, andre@0: &issuer, andre@0: &serial); andre@0: if (cert) { andre@0: SECITEM_FreeItem(derSerial, PR_TRUE); andre@0: return STAN_GetCERTCertificateOrRelease(cert); andre@0: } andre@0: andre@0: do { andre@0: /* free the old cert on retry. Associated slot was not present */ andre@0: if (rvCert) { andre@0: CERT_DestroyCertificate(rvCert); andre@0: rvCert = NULL; andre@0: } andre@0: andre@0: cert = NSSTrustDomain_FindCertificateByIssuerAndSerialNumber( andre@0: STAN_GetDefaultTrustDomain(), andre@0: &issuer, andre@0: &serial); andre@0: if (!cert) { andre@0: break; andre@0: } andre@0: andre@0: rvCert = STAN_GetCERTCertificateOrRelease(cert); andre@0: if (rvCert == NULL) { andre@0: break; andre@0: } andre@0: andre@0: /* Check to see if the cert's token is still there */ andre@0: } while (!PK11_IsPresent(rvCert->slot)); andre@0: andre@0: if (rvCert && slotPtr) *slotPtr = PK11_ReferenceSlot(rvCert->slot); andre@0: andre@0: SECITEM_FreeItem(derSerial, PR_TRUE); andre@0: return rvCert; andre@0: } andre@0: andre@0: CK_OBJECT_HANDLE andre@0: PK11_FindObjectForCert(CERTCertificate *cert, void *wincx, PK11SlotInfo **pSlot) andre@0: { andre@0: CK_OBJECT_HANDLE certHandle; andre@0: CK_OBJECT_CLASS certClass = CKO_CERTIFICATE; andre@0: CK_ATTRIBUTE *attr; andre@0: CK_ATTRIBUTE searchTemplate[]= { andre@0: { CKA_CLASS, NULL, 0 }, andre@0: { CKA_VALUE, NULL, 0 }, andre@0: }; andre@0: int templateSize = sizeof(searchTemplate)/sizeof(searchTemplate[0]); andre@0: andre@0: attr = searchTemplate; andre@0: PK11_SETATTRS(attr, CKA_CLASS, &certClass, sizeof(certClass)); attr++; andre@0: PK11_SETATTRS(attr, CKA_VALUE, cert->derCert.data, cert->derCert.len); andre@0: andre@0: if (cert->slot) { andre@0: certHandle = pk11_getcerthandle(cert->slot, cert, searchTemplate, andre@0: templateSize); andre@0: if (certHandle != CK_INVALID_HANDLE) { andre@0: *pSlot = PK11_ReferenceSlot(cert->slot); andre@0: return certHandle; andre@0: } andre@0: } andre@0: andre@0: certHandle = pk11_FindCertObjectByTemplate(pSlot, searchTemplate, andre@0: templateSize, wincx); andre@0: if (certHandle != CK_INVALID_HANDLE) { andre@0: if (cert->slot == NULL) { andre@0: cert->slot = PK11_ReferenceSlot(*pSlot); andre@0: cert->pkcs11ID = certHandle; andre@0: cert->ownSlot = PR_TRUE; andre@0: cert->series = cert->slot->series; andre@0: } andre@0: } andre@0: andre@0: return(certHandle); andre@0: } andre@0: andre@0: SECKEYPrivateKey * andre@0: PK11_FindKeyByAnyCert(CERTCertificate *cert, void *wincx) andre@0: { andre@0: CK_OBJECT_HANDLE certHandle; andre@0: CK_OBJECT_HANDLE keyHandle; andre@0: PK11SlotInfo *slot = NULL; andre@0: SECKEYPrivateKey *privKey = NULL; andre@0: PRBool needLogin; andre@0: SECStatus rv; andre@0: int err; andre@0: andre@0: certHandle = PK11_FindObjectForCert(cert, wincx, &slot); andre@0: if (certHandle == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: /* andre@0: * prevent a login race condition. If slot is logged in between andre@0: * our call to pk11_LoginStillRequired and the andre@0: * PK11_MatchItem. The matchItem call will either succeed, or andre@0: * we will call it one more time after calling PK11_Authenticate andre@0: * (which is a noop on an authenticated token). andre@0: */ andre@0: needLogin = pk11_LoginStillRequired(slot,wincx); andre@0: keyHandle = PK11_MatchItem(slot,certHandle,CKO_PRIVATE_KEY); andre@0: if ((keyHandle == CK_INVALID_HANDLE) && needLogin && andre@0: (SSL_ERROR_NO_CERTIFICATE == (err = PORT_GetError()) || andre@0: SEC_ERROR_TOKEN_NOT_LOGGED_IN == err ) ) { andre@0: /* authenticate and try again */ andre@0: rv = PK11_Authenticate(slot, PR_TRUE, wincx); andre@0: if (rv == SECSuccess) { andre@0: keyHandle = PK11_MatchItem(slot,certHandle,CKO_PRIVATE_KEY); andre@0: } andre@0: } andre@0: if (keyHandle != CK_INVALID_HANDLE) { andre@0: privKey = PK11_MakePrivKey(slot, nullKey, PR_TRUE, keyHandle, wincx); andre@0: } andre@0: if (slot) { andre@0: PK11_FreeSlot(slot); andre@0: } andre@0: return privKey; andre@0: } andre@0: andre@0: CK_OBJECT_HANDLE andre@0: pk11_FindPubKeyByAnyCert(CERTCertificate *cert, PK11SlotInfo **slot, void *wincx) andre@0: { andre@0: CK_OBJECT_HANDLE certHandle; andre@0: CK_OBJECT_HANDLE keyHandle; andre@0: andre@0: certHandle = PK11_FindObjectForCert(cert, wincx, slot); andre@0: if (certHandle == CK_INVALID_HANDLE) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: keyHandle = PK11_MatchItem(*slot,certHandle,CKO_PUBLIC_KEY); andre@0: if (keyHandle == CK_INVALID_HANDLE) { andre@0: PK11_FreeSlot(*slot); andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: return keyHandle; andre@0: } andre@0: andre@0: /* andre@0: * find the number of certs in the slot with the same subject name andre@0: */ andre@0: int andre@0: PK11_NumberCertsForCertSubject(CERTCertificate *cert) andre@0: { andre@0: CK_OBJECT_CLASS certClass = CKO_CERTIFICATE; andre@0: CK_ATTRIBUTE theTemplate[] = { andre@0: { CKA_CLASS, NULL, 0 }, andre@0: { CKA_SUBJECT, NULL, 0 }, andre@0: }; andre@0: CK_ATTRIBUTE *attr = theTemplate; andre@0: int templateSize = sizeof(theTemplate)/sizeof(theTemplate[0]); andre@0: andre@0: PK11_SETATTRS(attr,CKA_CLASS, &certClass, sizeof(certClass)); attr++; andre@0: PK11_SETATTRS(attr,CKA_SUBJECT,cert->derSubject.data,cert->derSubject.len); andre@0: andre@0: if (cert->slot == NULL) { andre@0: PK11SlotList *list = PK11_GetAllTokens(CKM_INVALID_MECHANISM, andre@0: PR_FALSE,PR_TRUE,NULL); andre@0: PK11SlotListElement *le; andre@0: int count = 0; andre@0: andre@0: if (!list) { andre@0: /* error code is set */ andre@0: return 0; andre@0: } andre@0: andre@0: /* loop through all the fortezza tokens */ andre@0: for (le = list->head; le; le = le->next) { andre@0: count += PK11_NumberObjectsFor(le->slot,theTemplate,templateSize); andre@0: } andre@0: PK11_FreeSlotList(list); andre@0: return count; andre@0: } andre@0: andre@0: return PK11_NumberObjectsFor(cert->slot,theTemplate,templateSize); andre@0: } andre@0: andre@0: /* andre@0: * Walk all the certs with the same subject andre@0: */ andre@0: SECStatus andre@0: PK11_TraverseCertsForSubject(CERTCertificate *cert, andre@0: SECStatus(* callback)(CERTCertificate*, void *), void *arg) andre@0: { andre@0: if(!cert) { andre@0: return SECFailure; andre@0: } andre@0: if (cert->slot == NULL) { andre@0: PK11SlotList *list = PK11_GetAllTokens(CKM_INVALID_MECHANISM, andre@0: PR_FALSE,PR_TRUE,NULL); andre@0: PK11SlotListElement *le; andre@0: andre@0: if (!list) { andre@0: /* error code is set */ andre@0: return SECFailure; andre@0: } andre@0: /* loop through all the tokens */ andre@0: for (le = list->head; le; le = le->next) { andre@0: PK11_TraverseCertsForSubjectInSlot(cert,le->slot,callback,arg); andre@0: } andre@0: PK11_FreeSlotList(list); andre@0: return SECSuccess; andre@0: andre@0: } andre@0: andre@0: return PK11_TraverseCertsForSubjectInSlot(cert, cert->slot, callback, arg); andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_TraverseCertsForSubjectInSlot(CERTCertificate *cert, PK11SlotInfo *slot, andre@0: SECStatus(* callback)(CERTCertificate*, void *), void *arg) andre@0: { andre@0: PRStatus nssrv = PR_SUCCESS; andre@0: NSSToken *token; andre@0: NSSDER subject; andre@0: NSSTrustDomain *td; andre@0: nssList *subjectList; andre@0: nssPKIObjectCollection *collection; andre@0: nssCryptokiObject **instances; andre@0: NSSCertificate **certs; andre@0: nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly; andre@0: td = STAN_GetDefaultTrustDomain(); andre@0: NSSITEM_FROM_SECITEM(&subject, &cert->derSubject); andre@0: token = PK11Slot_GetNSSToken(slot); andre@0: if (!nssToken_IsPresent(token)) { andre@0: return SECSuccess; andre@0: } andre@0: collection = nssCertificateCollection_Create(td, NULL); andre@0: if (!collection) { andre@0: return SECFailure; andre@0: } andre@0: subjectList = nssList_Create(NULL, PR_FALSE); andre@0: if (!subjectList) { andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: return SECFailure; andre@0: } andre@0: (void)nssTrustDomain_GetCertsForSubjectFromCache(td, &subject, andre@0: subjectList); andre@0: transfer_token_certs_to_collection(subjectList, token, collection); andre@0: instances = nssToken_FindCertificatesBySubject(token, NULL, andre@0: &subject, andre@0: tokenOnly, 0, &nssrv); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: nssList_Destroy(subjectList); andre@0: certs = nssPKIObjectCollection_GetCertificates(collection, andre@0: NULL, 0, NULL); andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: if (certs) { andre@0: CERTCertificate *oldie; andre@0: NSSCertificate **cp; andre@0: for (cp = certs; *cp; cp++) { andre@0: oldie = STAN_GetCERTCertificate(*cp); andre@0: if (!oldie) { andre@0: continue; andre@0: } andre@0: if ((*callback)(oldie, arg) != SECSuccess) { andre@0: nssrv = PR_FAILURE; andre@0: break; andre@0: } andre@0: } andre@0: nssCertificateArray_Destroy(certs); andre@0: } andre@0: return (nssrv == PR_SUCCESS) ? SECSuccess : SECFailure; andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_TraverseCertsForNicknameInSlot(SECItem *nickname, PK11SlotInfo *slot, andre@0: SECStatus(* callback)(CERTCertificate*, void *), void *arg) andre@0: { andre@0: struct nss3_cert_cbstr pk11cb; andre@0: PRStatus nssrv = PR_SUCCESS; andre@0: NSSToken *token; andre@0: NSSTrustDomain *td; andre@0: NSSUTF8 *nick; andre@0: PRBool created = PR_FALSE; andre@0: nssCryptokiObject **instances; andre@0: nssPKIObjectCollection *collection = NULL; andre@0: NSSCertificate **certs; andre@0: nssList *nameList = NULL; andre@0: nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly; andre@0: pk11cb.callback = callback; andre@0: pk11cb.arg = arg; andre@0: token = PK11Slot_GetNSSToken(slot); andre@0: if (!nssToken_IsPresent(token)) { andre@0: return SECSuccess; andre@0: } andre@0: if (nickname->data[nickname->len-1] != '\0') { andre@0: nick = nssUTF8_Create(NULL, nssStringType_UTF8String, andre@0: nickname->data, nickname->len); andre@0: created = PR_TRUE; andre@0: } else { andre@0: nick = (NSSUTF8 *)nickname->data; andre@0: } andre@0: td = STAN_GetDefaultTrustDomain(); andre@0: collection = nssCertificateCollection_Create(td, NULL); andre@0: if (!collection) { andre@0: goto loser; andre@0: } andre@0: nameList = nssList_Create(NULL, PR_FALSE); andre@0: if (!nameList) { andre@0: goto loser; andre@0: } andre@0: (void)nssTrustDomain_GetCertsForNicknameFromCache(td, nick, nameList); andre@0: transfer_token_certs_to_collection(nameList, token, collection); andre@0: instances = nssToken_FindCertificatesByNickname(token, NULL, andre@0: nick, andre@0: tokenOnly, 0, &nssrv); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: nssList_Destroy(nameList); andre@0: certs = nssPKIObjectCollection_GetCertificates(collection, andre@0: NULL, 0, NULL); andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: if (certs) { andre@0: CERTCertificate *oldie; andre@0: NSSCertificate **cp; andre@0: for (cp = certs; *cp; cp++) { andre@0: oldie = STAN_GetCERTCertificate(*cp); andre@0: if (!oldie) { andre@0: continue; andre@0: } andre@0: if ((*callback)(oldie, arg) != SECSuccess) { andre@0: nssrv = PR_FAILURE; andre@0: break; andre@0: } andre@0: } andre@0: nssCertificateArray_Destroy(certs); andre@0: } andre@0: if (created) nss_ZFreeIf(nick); andre@0: return (nssrv == PR_SUCCESS) ? SECSuccess : SECFailure; andre@0: loser: andre@0: if (created) { andre@0: nss_ZFreeIf(nick); andre@0: } andre@0: if (collection) { andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: } andre@0: if (nameList) { andre@0: nssList_Destroy(nameList); andre@0: } andre@0: return SECFailure; andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_TraverseCertsInSlot(PK11SlotInfo *slot, andre@0: SECStatus(* callback)(CERTCertificate*, void *), void *arg) andre@0: { andre@0: PRStatus nssrv; andre@0: NSSTrustDomain *td = STAN_GetDefaultTrustDomain(); andre@0: NSSToken *tok; andre@0: nssList *certList = NULL; andre@0: nssCryptokiObject **instances; andre@0: nssPKIObjectCollection *collection; andre@0: NSSCertificate **certs; andre@0: nssTokenSearchType tokenOnly = nssTokenSearchType_TokenOnly; andre@0: tok = PK11Slot_GetNSSToken(slot); andre@0: if (!nssToken_IsPresent(tok)) { andre@0: return SECSuccess; andre@0: } andre@0: collection = nssCertificateCollection_Create(td, NULL); andre@0: if (!collection) { andre@0: return SECFailure; andre@0: } andre@0: certList = nssList_Create(NULL, PR_FALSE); andre@0: if (!certList) { andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: return SECFailure; andre@0: } andre@0: (void)nssTrustDomain_GetCertsFromCache(td, certList); andre@0: transfer_token_certs_to_collection(certList, tok, collection); andre@0: instances = nssToken_FindObjects(tok, NULL, CKO_CERTIFICATE, andre@0: tokenOnly, 0, &nssrv); andre@0: nssPKIObjectCollection_AddInstances(collection, instances, 0); andre@0: nss_ZFreeIf(instances); andre@0: nssList_Destroy(certList); andre@0: certs = nssPKIObjectCollection_GetCertificates(collection, andre@0: NULL, 0, NULL); andre@0: nssPKIObjectCollection_Destroy(collection); andre@0: if (certs) { andre@0: CERTCertificate *oldie; andre@0: NSSCertificate **cp; andre@0: for (cp = certs; *cp; cp++) { andre@0: oldie = STAN_GetCERTCertificate(*cp); andre@0: if (!oldie) { andre@0: continue; andre@0: } andre@0: if ((*callback)(oldie, arg) != SECSuccess) { andre@0: nssrv = PR_FAILURE; andre@0: break; andre@0: } andre@0: } andre@0: nssCertificateArray_Destroy(certs); andre@0: } andre@0: return (nssrv == PR_SUCCESS) ? SECSuccess : SECFailure; andre@0: } andre@0: andre@0: /* andre@0: * return the certificate associated with a derCert andre@0: */ andre@0: CERTCertificate * andre@0: PK11_FindCertFromDERCert(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: void *wincx) andre@0: { andre@0: return PK11_FindCertFromDERCertItem(slot, &cert->derCert, wincx); andre@0: } andre@0: andre@0: CERTCertificate * andre@0: PK11_FindCertFromDERCertItem(PK11SlotInfo *slot, const SECItem *inDerCert, andre@0: void *wincx) andre@0: andre@0: { andre@0: NSSDER derCert; andre@0: NSSToken *tok; andre@0: NSSTrustDomain *td = STAN_GetDefaultTrustDomain(); andre@0: nssCryptokiObject *co = NULL; andre@0: SECStatus rv; andre@0: andre@0: tok = PK11Slot_GetNSSToken(slot); andre@0: NSSITEM_FROM_SECITEM(&derCert, inDerCert); andre@0: rv = pk11_AuthenticateUnfriendly(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: PK11_FreeSlot(slot); andre@0: return NULL; andre@0: } andre@0: andre@0: co = nssToken_FindCertificateByEncodedCertificate(tok, NULL, &derCert, andre@0: nssTokenSearchType_TokenOnly, NULL); andre@0: andre@0: return co ? PK11_MakeCertFromHandle(slot, co->handle, NULL) : NULL; andre@0: andre@0: } andre@0: andre@0: /* andre@0: * import a cert for a private key we have already generated. Set the label andre@0: * on both to be the nickname. andre@0: */ andre@0: static CK_OBJECT_HANDLE andre@0: pk11_findKeyObjectByDERCert(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: void *wincx) andre@0: { andre@0: SECItem *keyID; andre@0: CK_OBJECT_HANDLE key; andre@0: SECStatus rv; andre@0: PRBool needLogin; andre@0: int err; andre@0: andre@0: if((slot == NULL) || (cert == NULL)) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: keyID = pk11_mkcertKeyID(cert); andre@0: if(keyID == NULL) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: /* andre@0: * prevent a login race condition. If slot is logged in between andre@0: * our call to pk11_LoginStillRequired and the andre@0: * pk11_FindPrivateKeyFromCerID. The matchItem call will either succeed, or andre@0: * we will call it one more time after calling PK11_Authenticate andre@0: * (which is a noop on an authenticated token). andre@0: */ andre@0: needLogin = pk11_LoginStillRequired(slot,wincx); andre@0: key = pk11_FindPrivateKeyFromCertID(slot, keyID); andre@0: if ((key == CK_INVALID_HANDLE) && needLogin && andre@0: (SSL_ERROR_NO_CERTIFICATE == (err = PORT_GetError()) || andre@0: SEC_ERROR_TOKEN_NOT_LOGGED_IN == err )) { andre@0: /* authenticate and try again */ andre@0: rv = PK11_Authenticate(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) goto loser; andre@0: key = pk11_FindPrivateKeyFromCertID(slot, keyID); andre@0: } andre@0: andre@0: loser: andre@0: SECITEM_ZfreeItem(keyID, PR_TRUE); andre@0: return key; andre@0: } andre@0: andre@0: SECKEYPrivateKey * andre@0: PK11_FindKeyByDERCert(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: void *wincx) andre@0: { andre@0: CK_OBJECT_HANDLE keyHandle; andre@0: andre@0: if((slot == NULL) || (cert == NULL)) { andre@0: return NULL; andre@0: } andre@0: andre@0: keyHandle = pk11_findKeyObjectByDERCert(slot, cert, wincx); andre@0: if (keyHandle == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: andre@0: return PK11_MakePrivKey(slot,nullKey,PR_TRUE,keyHandle,wincx); andre@0: } andre@0: andre@0: SECStatus andre@0: PK11_ImportCertForKeyToSlot(PK11SlotInfo *slot, CERTCertificate *cert, andre@0: char *nickname, andre@0: PRBool addCertUsage,void *wincx) andre@0: { andre@0: CK_OBJECT_HANDLE keyHandle; andre@0: andre@0: if((slot == NULL) || (cert == NULL) || (nickname == NULL)) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: keyHandle = pk11_findKeyObjectByDERCert(slot, cert, wincx); andre@0: if (keyHandle == CK_INVALID_HANDLE) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: return PK11_ImportCert(slot, cert, keyHandle, nickname, addCertUsage); andre@0: } andre@0: andre@0: andre@0: /* remove when the real version comes out */ andre@0: #define SEC_OID_MISSI_KEA 300 /* until we have v3 stuff merged */ andre@0: PRBool andre@0: KEAPQGCompare(CERTCertificate *server,CERTCertificate *cert) { andre@0: andre@0: /* not implemented */ andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: PRBool andre@0: PK11_FortezzaHasKEA(CERTCertificate *cert) andre@0: { andre@0: /* look at the subject and see if it is a KEA for MISSI key */ andre@0: SECOidData *oid; andre@0: CERTCertTrust trust; andre@0: andre@0: if (CERT_GetCertTrust(cert, &trust) != SECSuccess || andre@0: ((trust.sslFlags & CERTDB_USER) != CERTDB_USER)) { andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: oid = SECOID_FindOID(&cert->subjectPublicKeyInfo.algorithm.algorithm); andre@0: if (!oid) { andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: return (PRBool)((oid->offset == SEC_OID_MISSI_KEA_DSS_OLD) || andre@0: (oid->offset == SEC_OID_MISSI_KEA_DSS) || andre@0: (oid->offset == SEC_OID_MISSI_KEA)) ; andre@0: } andre@0: andre@0: /* andre@0: * Find a kea cert on this slot that matches the domain of it's peer andre@0: */ andre@0: static CERTCertificate andre@0: *pk11_GetKEAMate(PK11SlotInfo *slot,CERTCertificate *peer) andre@0: { andre@0: int i; andre@0: CERTCertificate *returnedCert = NULL; andre@0: andre@0: for (i=0; i < slot->cert_count; i++) { andre@0: CERTCertificate *cert = slot->cert_array[i]; andre@0: andre@0: if (PK11_FortezzaHasKEA(cert) && KEAPQGCompare(peer,cert)) { andre@0: returnedCert = CERT_DupCertificate(cert); andre@0: break; andre@0: } andre@0: } andre@0: return returnedCert; andre@0: } andre@0: andre@0: /* andre@0: * The following is a FORTEZZA only Certificate request. We call this when we andre@0: * are doing a non-client auth SSL connection. We are only interested in the andre@0: * fortezza slots, and we are only interested in certs that share the same root andre@0: * key as the server. andre@0: */ andre@0: CERTCertificate * andre@0: PK11_FindBestKEAMatch(CERTCertificate *server, void *wincx) andre@0: { andre@0: PK11SlotList *keaList = PK11_GetAllTokens(CKM_KEA_KEY_DERIVE, andre@0: PR_FALSE,PR_TRUE,wincx); andre@0: PK11SlotListElement *le; andre@0: CERTCertificate *returnedCert = NULL; andre@0: SECStatus rv; andre@0: andre@0: if (!keaList) { andre@0: /* error code is set */ andre@0: return NULL; andre@0: } andre@0: andre@0: /* loop through all the fortezza tokens */ andre@0: for (le = keaList->head; le; le = le->next) { andre@0: rv = PK11_Authenticate(le->slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) continue; andre@0: if (le->slot->session == CK_INVALID_SESSION) { andre@0: continue; andre@0: } andre@0: returnedCert = pk11_GetKEAMate(le->slot,server); andre@0: if (returnedCert) break; andre@0: } andre@0: PK11_FreeSlotList(keaList); andre@0: andre@0: return returnedCert; andre@0: } andre@0: andre@0: /* andre@0: * find a matched pair of kea certs to key exchange parameters from one andre@0: * fortezza card to another as necessary. andre@0: */ andre@0: SECStatus andre@0: PK11_GetKEAMatchedCerts(PK11SlotInfo *slot1, PK11SlotInfo *slot2, andre@0: CERTCertificate **cert1, CERTCertificate **cert2) andre@0: { andre@0: CERTCertificate *returnedCert = NULL; andre@0: int i; andre@0: andre@0: for (i=0; i < slot1->cert_count; i++) { andre@0: CERTCertificate *cert = slot1->cert_array[i]; andre@0: andre@0: if (PK11_FortezzaHasKEA(cert)) { andre@0: returnedCert = pk11_GetKEAMate(slot2,cert); andre@0: if (returnedCert != NULL) { andre@0: *cert2 = returnedCert; andre@0: *cert1 = CERT_DupCertificate(cert); andre@0: return SECSuccess; andre@0: } andre@0: } andre@0: } andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* andre@0: * return the private key From a given Cert andre@0: */ andre@0: CK_OBJECT_HANDLE andre@0: PK11_FindCertInSlot(PK11SlotInfo *slot, CERTCertificate *cert, void *wincx) andre@0: { andre@0: CK_OBJECT_CLASS certClass = CKO_CERTIFICATE; andre@0: CK_ATTRIBUTE theTemplate[] = { andre@0: { CKA_VALUE, NULL, 0 }, andre@0: { CKA_CLASS, NULL, 0 } andre@0: }; andre@0: /* if you change the array, change the variable below as well */ andre@0: int tsize = sizeof(theTemplate)/sizeof(theTemplate[0]); andre@0: CK_ATTRIBUTE *attrs = theTemplate; andre@0: SECStatus rv; andre@0: andre@0: PK11_SETATTRS(attrs, CKA_VALUE, cert->derCert.data, andre@0: cert->derCert.len); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &certClass, sizeof(certClass)); andre@0: andre@0: /* andre@0: * issue the find andre@0: */ andre@0: rv = pk11_AuthenticateUnfriendly(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: return CK_INVALID_HANDLE; andre@0: } andre@0: andre@0: return pk11_getcerthandle(slot,cert,theTemplate,tsize); andre@0: } andre@0: andre@0: /* Looking for PK11_GetKeyIDFromCert? andre@0: * Use PK11_GetLowLevelKeyIDForCert instead. andre@0: */ andre@0: andre@0: andre@0: struct listCertsStr { andre@0: PK11CertListType type; andre@0: CERTCertList *certList; andre@0: }; andre@0: andre@0: static PRStatus andre@0: pk11ListCertCallback(NSSCertificate *c, void *arg) andre@0: { andre@0: struct listCertsStr *listCertP = (struct listCertsStr *)arg; andre@0: CERTCertificate *newCert = NULL; andre@0: PK11CertListType type = listCertP->type; andre@0: CERTCertList *certList = listCertP->certList; andre@0: PRBool isUnique = PR_FALSE; andre@0: PRBool isCA = PR_FALSE; andre@0: char *nickname = NULL; andre@0: unsigned int certType; andre@0: SECStatus rv; andre@0: andre@0: if ((type == PK11CertListUnique) || (type == PK11CertListRootUnique) || andre@0: (type == PK11CertListCAUnique) || (type == PK11CertListUserUnique) ) { andre@0: /* only list one instance of each certificate, even if several exist */ andre@0: isUnique = PR_TRUE; andre@0: } andre@0: if ((type == PK11CertListCA) || (type == PK11CertListRootUnique) || andre@0: (type == PK11CertListCAUnique)) { andre@0: isCA = PR_TRUE; andre@0: } andre@0: andre@0: /* if we want user certs and we don't have one skip this cert */ andre@0: if ( ( (type == PK11CertListUser) || (type == PK11CertListUserUnique) ) && andre@0: !NSSCertificate_IsPrivateKeyAvailable(c, NULL,NULL)) { andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* PK11CertListRootUnique means we want CA certs without a private key. andre@0: * This is for legacy app support . PK11CertListCAUnique should be used andre@0: * instead to get all CA certs, regardless of private key andre@0: */ andre@0: if ((type == PK11CertListRootUnique) && andre@0: NSSCertificate_IsPrivateKeyAvailable(c, NULL,NULL)) { andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* caller still owns the reference to 'c' */ andre@0: newCert = STAN_GetCERTCertificate(c); andre@0: if (!newCert) { andre@0: return PR_SUCCESS; andre@0: } andre@0: /* if we want CA certs and it ain't one, skip it */ andre@0: if( isCA && (!CERT_IsCACert(newCert, &certType)) ) { andre@0: return PR_SUCCESS; andre@0: } andre@0: if (isUnique) { andre@0: CERT_DupCertificate(newCert); andre@0: andre@0: nickname = STAN_GetCERTCertificateName(certList->arena, c); andre@0: andre@0: /* put slot certs at the end */ andre@0: if (newCert->slot && !PK11_IsInternal(newCert->slot)) { andre@0: rv = CERT_AddCertToListTailWithData(certList,newCert,nickname); andre@0: } else { andre@0: rv = CERT_AddCertToListHeadWithData(certList,newCert,nickname); andre@0: } andre@0: /* if we didn't add the cert to the list, don't leak it */ andre@0: if (rv != SECSuccess) { andre@0: CERT_DestroyCertificate(newCert); andre@0: } andre@0: } else { andre@0: /* add multiple instances to the cert list */ andre@0: nssCryptokiObject **ip; andre@0: nssCryptokiObject **instances = nssPKIObject_GetInstances(&c->object); andre@0: if (!instances) { andre@0: return PR_SUCCESS; andre@0: } andre@0: for (ip = instances; *ip; ip++) { andre@0: nssCryptokiObject *instance = *ip; andre@0: PK11SlotInfo *slot = instance->token->pk11slot; andre@0: andre@0: /* put the same CERTCertificate in the list for all instances */ andre@0: CERT_DupCertificate(newCert); andre@0: andre@0: nickname = STAN_GetCERTCertificateNameForInstance( andre@0: certList->arena, c, instance); andre@0: andre@0: /* put slot certs at the end */ andre@0: if (slot && !PK11_IsInternal(slot)) { andre@0: rv = CERT_AddCertToListTailWithData(certList,newCert,nickname); andre@0: } else { andre@0: rv = CERT_AddCertToListHeadWithData(certList,newCert,nickname); andre@0: } andre@0: /* if we didn't add the cert to the list, don't leak it */ andre@0: if (rv != SECSuccess) { andre@0: CERT_DestroyCertificate(newCert); andre@0: } andre@0: } andre@0: nssCryptokiObjectArray_Destroy(instances); andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: andre@0: CERTCertList * andre@0: PK11_ListCerts(PK11CertListType type, void *pwarg) andre@0: { andre@0: NSSTrustDomain *defaultTD = STAN_GetDefaultTrustDomain(); andre@0: CERTCertList *certList = NULL; andre@0: struct listCertsStr listCerts; andre@0: certList = CERT_NewCertList(); andre@0: listCerts.type = type; andre@0: listCerts.certList = certList; andre@0: andre@0: /* authenticate to the slots */ andre@0: (void) pk11_TraverseAllSlots( NULL, NULL, PR_TRUE, pwarg); andre@0: NSSTrustDomain_TraverseCertificates(defaultTD, pk11ListCertCallback, andre@0: &listCerts); andre@0: return certList; andre@0: } andre@0: andre@0: SECItem * andre@0: PK11_GetLowLevelKeyIDForCert(PK11SlotInfo *slot, andre@0: CERTCertificate *cert, void *wincx) andre@0: { andre@0: CK_OBJECT_CLASS certClass = CKO_CERTIFICATE; andre@0: CK_ATTRIBUTE theTemplate[] = { andre@0: { CKA_VALUE, NULL, 0 }, andre@0: { CKA_CLASS, NULL, 0 } andre@0: }; andre@0: /* if you change the array, change the variable below as well */ andre@0: int tsize = sizeof(theTemplate)/sizeof(theTemplate[0]); andre@0: CK_OBJECT_HANDLE certHandle; andre@0: CK_ATTRIBUTE *attrs = theTemplate; andre@0: PK11SlotInfo *slotRef = NULL; andre@0: SECItem *item; andre@0: SECStatus rv; andre@0: andre@0: if (slot) { andre@0: PK11_SETATTRS(attrs, CKA_VALUE, cert->derCert.data, andre@0: cert->derCert.len); attrs++; andre@0: PK11_SETATTRS(attrs, CKA_CLASS, &certClass, sizeof(certClass)); andre@0: andre@0: rv = pk11_AuthenticateUnfriendly(slot, PR_TRUE, wincx); andre@0: if (rv != SECSuccess) { andre@0: return NULL; andre@0: } andre@0: certHandle = pk11_getcerthandle(slot,cert,theTemplate,tsize); andre@0: } else { andre@0: certHandle = PK11_FindObjectForCert(cert, wincx, &slotRef); andre@0: if (certHandle == CK_INVALID_HANDLE) { andre@0: return pk11_mkcertKeyID(cert); andre@0: } andre@0: slot = slotRef; andre@0: } andre@0: andre@0: if (certHandle == CK_INVALID_HANDLE) { andre@0: return NULL; andre@0: } andre@0: andre@0: item = pk11_GetLowLevelKeyFromHandle(slot,certHandle); andre@0: if (slotRef) PK11_FreeSlot(slotRef); andre@0: return item; andre@0: } andre@0: andre@0: /* argument type for listCertsCallback */ andre@0: typedef struct { andre@0: CERTCertList *list; andre@0: PK11SlotInfo *slot; andre@0: } ListCertsArg; andre@0: andre@0: static SECStatus andre@0: listCertsCallback(CERTCertificate* cert, void*arg) andre@0: { andre@0: ListCertsArg *cdata = (ListCertsArg*)arg; andre@0: char *nickname = NULL; andre@0: nssCryptokiObject *instance, **ci; andre@0: nssCryptokiObject **instances; andre@0: NSSCertificate *c = STAN_GetNSSCertificate(cert); andre@0: SECStatus rv; andre@0: andre@0: if (c == NULL) { andre@0: return SECFailure; andre@0: } andre@0: instances = nssPKIObject_GetInstances(&c->object); andre@0: if (!instances) { andre@0: return SECFailure; andre@0: } andre@0: instance = NULL; andre@0: for (ci = instances; *ci; ci++) { andre@0: if ((*ci)->token->pk11slot == cdata->slot) { andre@0: instance = *ci; andre@0: break; andre@0: } andre@0: } andre@0: PORT_Assert(instance != NULL); andre@0: if (!instance) { andre@0: nssCryptokiObjectArray_Destroy(instances); andre@0: PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); andre@0: return SECFailure; andre@0: } andre@0: nickname = STAN_GetCERTCertificateNameForInstance(cdata->list->arena, andre@0: c, instance); andre@0: nssCryptokiObjectArray_Destroy(instances); andre@0: andre@0: CERT_DupCertificate(cert); andre@0: rv = CERT_AddCertToListTailWithData(cdata->list, cert, nickname); andre@0: if (rv != SECSuccess) { andre@0: CERT_DestroyCertificate(cert); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: CERTCertList * andre@0: PK11_ListCertsInSlot(PK11SlotInfo *slot) andre@0: { andre@0: SECStatus status; andre@0: CERTCertList *certs; andre@0: ListCertsArg cdata; andre@0: andre@0: certs = CERT_NewCertList(); andre@0: if(certs == NULL) return NULL; andre@0: cdata.list = certs; andre@0: cdata.slot = slot; andre@0: andre@0: status = PK11_TraverseCertsInSlot(slot, listCertsCallback, andre@0: &cdata); andre@0: andre@0: if( status != SECSuccess ) { andre@0: CERT_DestroyCertList(certs); andre@0: certs = NULL; andre@0: } andre@0: andre@0: return certs; andre@0: } andre@0: andre@0: PK11SlotList * andre@0: PK11_GetAllSlotsForCert(CERTCertificate *cert, void *arg) andre@0: { andre@0: nssCryptokiObject **ip; andre@0: PK11SlotList *slotList; andre@0: NSSCertificate *c; andre@0: nssCryptokiObject **instances; andre@0: PRBool found = PR_FALSE; andre@0: andre@0: if (!cert) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return NULL; andre@0: } andre@0: andre@0: c = STAN_GetNSSCertificate(cert); andre@0: if (!c) { andre@0: CERT_MapStanError(); andre@0: return NULL; andre@0: } andre@0: andre@0: /* add multiple instances to the cert list */ andre@0: instances = nssPKIObject_GetInstances(&c->object); andre@0: if (!instances) { andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: return NULL; andre@0: } andre@0: andre@0: slotList = PK11_NewSlotList(); andre@0: if (!slotList) { andre@0: nssCryptokiObjectArray_Destroy(instances); andre@0: return NULL; andre@0: } andre@0: andre@0: for (ip = instances; *ip; ip++) { andre@0: nssCryptokiObject *instance = *ip; andre@0: PK11SlotInfo *slot = instance->token->pk11slot; andre@0: if (slot) { andre@0: PK11_AddSlotToList(slotList, slot, PR_TRUE); andre@0: found = PR_TRUE; andre@0: } andre@0: } andre@0: if (!found) { andre@0: PK11_FreeSlotList(slotList); andre@0: PORT_SetError(SEC_ERROR_NO_TOKEN); andre@0: slotList = NULL; andre@0: } andre@0: andre@0: nssCryptokiObjectArray_Destroy(instances); andre@0: return slotList; andre@0: }