michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include "secitem.h" michael@0: #include "pkcs11.h" michael@0: #include "lgdb.h" michael@0: #include "pcert.h" michael@0: #include "lowkeyi.h" michael@0: #include "blapi.h" michael@0: #include "secder.h" michael@0: #include "secasn1.h" michael@0: michael@0: #include "keydbi.h" michael@0: michael@0: /* michael@0: * ******************** Object Creation Utilities *************************** michael@0: */ michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Certificate Object michael@0: */ michael@0: static CK_RV michael@0: lg_createCertObject(SDB *sdb, CK_OBJECT_HANDLE *handle, michael@0: const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: SECItem derCert; michael@0: NSSLOWCERTCertificate *cert; michael@0: NSSLOWCERTCertTrust *trust = NULL; michael@0: NSSLOWCERTCertTrust userTrust = michael@0: { CERTDB_USER, CERTDB_USER, CERTDB_USER }; michael@0: NSSLOWCERTCertTrust defTrust = michael@0: { CERTDB_TRUSTED_UNKNOWN, michael@0: CERTDB_TRUSTED_UNKNOWN, CERTDB_TRUSTED_UNKNOWN }; michael@0: char *label = NULL; michael@0: char *email = NULL; michael@0: SECStatus rv; michael@0: CK_RV crv; michael@0: PRBool inDB = PR_TRUE; michael@0: NSSLOWCERTCertDBHandle *certHandle = lg_getCertDB(sdb); michael@0: NSSLOWKEYDBHandle *keyHandle = NULL; michael@0: CK_CERTIFICATE_TYPE type; michael@0: const CK_ATTRIBUTE *attribute; michael@0: michael@0: /* we can't store any certs private */ michael@0: if (lg_isTrue(CKA_PRIVATE, templ, count)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: /* We only support X.509 Certs for now */ michael@0: crv = lg_GetULongAttribute(CKA_CERTIFICATE_TYPE, templ, count, &type); michael@0: if (crv != CKR_OK) { michael@0: return crv; michael@0: } michael@0: michael@0: if (type != CKC_X_509) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: /* X.509 Certificate */ michael@0: michael@0: michael@0: if (certHandle == NULL) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: /* get the der cert */ michael@0: attribute = lg_FindAttribute(CKA_VALUE, templ, count); michael@0: if (!attribute) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: derCert.type = 0; michael@0: derCert.data = (unsigned char *)attribute->pValue; michael@0: derCert.len = attribute->ulValueLen ; michael@0: michael@0: label = lg_getString(CKA_LABEL, templ, count); michael@0: michael@0: cert = nsslowcert_FindCertByDERCert(certHandle, &derCert); michael@0: if (cert == NULL) { michael@0: cert = nsslowcert_DecodeDERCertificate(&derCert, label); michael@0: inDB = PR_FALSE; michael@0: } michael@0: if (cert == NULL) { michael@0: if (label) PORT_Free(label); michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: keyHandle = lg_getKeyDB(sdb); michael@0: if (keyHandle) { michael@0: if (nsslowkey_KeyForCertExists(keyHandle,cert)) { michael@0: trust = &userTrust; michael@0: } michael@0: } michael@0: michael@0: if (!inDB) { michael@0: if (!trust) trust = &defTrust; michael@0: rv = nsslowcert_AddPermCert(certHandle, cert, label, trust); michael@0: } else { michael@0: rv = trust ? nsslowcert_ChangeCertTrust(certHandle,cert,trust) : michael@0: SECSuccess; michael@0: } michael@0: michael@0: if (label) PORT_Free(label); michael@0: michael@0: if (rv != SECSuccess) { michael@0: nsslowcert_DestroyCertificate(cert); michael@0: return CKR_DEVICE_ERROR; michael@0: } michael@0: michael@0: /* michael@0: * Add a NULL S/MIME profile if necessary. michael@0: */ michael@0: email = lg_getString(CKA_NSS_EMAIL, templ, count); michael@0: if (email) { michael@0: certDBEntrySMime *entry; michael@0: michael@0: entry = nsslowcert_ReadDBSMimeEntry(certHandle,email); michael@0: if (!entry) { michael@0: nsslowcert_SaveSMimeProfile(certHandle, email, michael@0: &cert->derSubject, NULL, NULL); michael@0: } else { michael@0: nsslowcert_DestroyDBEntry((certDBEntry *)entry); michael@0: } michael@0: PORT_Free(email); michael@0: } michael@0: *handle=lg_mkHandle(sdb,&cert->certKey,LG_TOKEN_TYPE_CERT); michael@0: nsslowcert_DestroyCertificate(cert); michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: unsigned int michael@0: lg_MapTrust(CK_TRUST trust, PRBool clientAuth) michael@0: { michael@0: unsigned int trustCA = clientAuth ? CERTDB_TRUSTED_CLIENT_CA : michael@0: CERTDB_TRUSTED_CA; michael@0: switch (trust) { michael@0: case CKT_NSS_TRUSTED: michael@0: return CERTDB_TERMINAL_RECORD|CERTDB_TRUSTED; michael@0: case CKT_NSS_TRUSTED_DELEGATOR: michael@0: return CERTDB_VALID_CA|trustCA; michael@0: case CKT_NSS_MUST_VERIFY_TRUST: michael@0: return CERTDB_MUST_VERIFY; michael@0: case CKT_NSS_NOT_TRUSTED: michael@0: return CERTDB_TERMINAL_RECORD; michael@0: case CKT_NSS_VALID_DELEGATOR: /* implies must verify */ michael@0: return CERTDB_VALID_CA; michael@0: default: michael@0: break; michael@0: } michael@0: return CERTDB_TRUSTED_UNKNOWN; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Trust Object michael@0: */ michael@0: static CK_RV michael@0: lg_createTrustObject(SDB *sdb, CK_OBJECT_HANDLE *handle, michael@0: const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: const CK_ATTRIBUTE *issuer = NULL; michael@0: const CK_ATTRIBUTE *serial = NULL; michael@0: NSSLOWCERTCertificate *cert = NULL; michael@0: const CK_ATTRIBUTE *trust; michael@0: CK_TRUST sslTrust = CKT_NSS_TRUST_UNKNOWN; michael@0: CK_TRUST clientTrust = CKT_NSS_TRUST_UNKNOWN; michael@0: CK_TRUST emailTrust = CKT_NSS_TRUST_UNKNOWN; michael@0: CK_TRUST signTrust = CKT_NSS_TRUST_UNKNOWN; michael@0: CK_BBOOL stepUp; michael@0: NSSLOWCERTCertTrust dbTrust = { 0 }; michael@0: SECStatus rv; michael@0: NSSLOWCERTCertDBHandle *certHandle = lg_getCertDB(sdb); michael@0: NSSLOWCERTIssuerAndSN issuerSN; michael@0: michael@0: /* we can't store any certs private */ michael@0: if (lg_isTrue(CKA_PRIVATE, templ, count)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: if (certHandle == NULL) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: issuer = lg_FindAttribute(CKA_ISSUER, templ, count); michael@0: serial = lg_FindAttribute(CKA_SERIAL_NUMBER, templ, count); michael@0: michael@0: if (issuer && serial) { michael@0: issuerSN.derIssuer.data = (unsigned char *)issuer->pValue; michael@0: issuerSN.derIssuer.len = issuer->ulValueLen ; michael@0: michael@0: issuerSN.serialNumber.data = (unsigned char *)serial->pValue; michael@0: issuerSN.serialNumber.len = serial->ulValueLen ; michael@0: michael@0: cert = nsslowcert_FindCertByIssuerAndSN(certHandle,&issuerSN); michael@0: } michael@0: michael@0: if (cert == NULL) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: lg_GetULongAttribute(CKA_TRUST_SERVER_AUTH, templ, count, &sslTrust); michael@0: lg_GetULongAttribute(CKA_TRUST_CLIENT_AUTH, templ, count, &clientTrust); michael@0: lg_GetULongAttribute(CKA_TRUST_EMAIL_PROTECTION, templ, count, &emailTrust); michael@0: lg_GetULongAttribute(CKA_TRUST_CODE_SIGNING, templ, count, &signTrust); michael@0: stepUp = CK_FALSE; michael@0: trust = lg_FindAttribute(CKA_TRUST_STEP_UP_APPROVED, templ, count); michael@0: if (trust) { michael@0: if (trust->ulValueLen == sizeof(CK_BBOOL)) { michael@0: stepUp = *(CK_BBOOL*)trust->pValue; michael@0: } michael@0: } michael@0: michael@0: /* preserve certain old fields */ michael@0: if (cert->trust) { michael@0: dbTrust.sslFlags = cert->trust->sslFlags & CERTDB_PRESERVE_TRUST_BITS; michael@0: dbTrust.emailFlags= michael@0: cert->trust->emailFlags & CERTDB_PRESERVE_TRUST_BITS; michael@0: dbTrust.objectSigningFlags = michael@0: cert->trust->objectSigningFlags & CERTDB_PRESERVE_TRUST_BITS; michael@0: } michael@0: michael@0: dbTrust.sslFlags |= lg_MapTrust(sslTrust,PR_FALSE); michael@0: dbTrust.sslFlags |= lg_MapTrust(clientTrust,PR_TRUE); michael@0: dbTrust.emailFlags |= lg_MapTrust(emailTrust,PR_FALSE); michael@0: dbTrust.objectSigningFlags |= lg_MapTrust(signTrust,PR_FALSE); michael@0: if (stepUp) { michael@0: dbTrust.sslFlags |= CERTDB_GOVT_APPROVED_CA; michael@0: } michael@0: michael@0: rv = nsslowcert_ChangeCertTrust(certHandle,cert,&dbTrust); michael@0: *handle=lg_mkHandle(sdb,&cert->certKey,LG_TOKEN_TYPE_TRUST); michael@0: nsslowcert_DestroyCertificate(cert); michael@0: if (rv != SECSuccess) { michael@0: return CKR_DEVICE_ERROR; michael@0: } michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Trust Object michael@0: */ michael@0: static CK_RV michael@0: lg_createSMimeObject(SDB *sdb, CK_OBJECT_HANDLE *handle, michael@0: const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: SECItem derSubj,rawProfile,rawTime,emailKey; michael@0: SECItem *pRawProfile = NULL; michael@0: SECItem *pRawTime = NULL; michael@0: char *email = NULL; michael@0: const CK_ATTRIBUTE *subject = NULL, michael@0: *profile = NULL, michael@0: *time = NULL; michael@0: SECStatus rv; michael@0: NSSLOWCERTCertDBHandle *certHandle; michael@0: CK_RV ck_rv = CKR_OK; michael@0: michael@0: /* we can't store any certs private */ michael@0: if (lg_isTrue(CKA_PRIVATE,templ,count)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: certHandle = lg_getCertDB(sdb); michael@0: if (certHandle == NULL) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: /* lookup SUBJECT */ michael@0: subject = lg_FindAttribute(CKA_SUBJECT,templ,count); michael@0: PORT_Assert(subject); michael@0: if (!subject) { michael@0: ck_rv = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: goto loser; michael@0: } michael@0: michael@0: derSubj.data = (unsigned char *)subject->pValue; michael@0: derSubj.len = subject->ulValueLen ; michael@0: derSubj.type = 0; michael@0: michael@0: /* lookup VALUE */ michael@0: profile = lg_FindAttribute(CKA_VALUE,templ,count); michael@0: if (profile) { michael@0: rawProfile.data = (unsigned char *)profile->pValue; michael@0: rawProfile.len = profile->ulValueLen ; michael@0: rawProfile.type = siBuffer; michael@0: pRawProfile = &rawProfile; michael@0: } michael@0: michael@0: /* lookup Time */ michael@0: time = lg_FindAttribute(CKA_NSS_SMIME_TIMESTAMP,templ,count); michael@0: if (time) { michael@0: rawTime.data = (unsigned char *)time->pValue; michael@0: rawTime.len = time->ulValueLen ; michael@0: rawTime.type = siBuffer; michael@0: pRawTime = &rawTime; michael@0: } michael@0: michael@0: michael@0: email = lg_getString(CKA_NSS_EMAIL,templ,count); michael@0: if (!email) { michael@0: ck_rv = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: goto loser; michael@0: } michael@0: michael@0: /* Store S/MIME Profile by SUBJECT */ michael@0: rv = nsslowcert_SaveSMimeProfile(certHandle, email, &derSubj, michael@0: pRawProfile,pRawTime); michael@0: if (rv != SECSuccess) { michael@0: ck_rv = CKR_DEVICE_ERROR; michael@0: goto loser; michael@0: } michael@0: emailKey.data = (unsigned char *)email; michael@0: emailKey.len = PORT_Strlen(email)+1; michael@0: michael@0: *handle = lg_mkHandle(sdb, &emailKey, LG_TOKEN_TYPE_SMIME); michael@0: michael@0: loser: michael@0: if (email) PORT_Free(email); michael@0: michael@0: return ck_rv; michael@0: } michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Trust Object michael@0: */ michael@0: static CK_RV michael@0: lg_createCrlObject(SDB *sdb, CK_OBJECT_HANDLE *handle, michael@0: const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: PRBool isKRL = PR_FALSE; michael@0: SECItem derSubj,derCrl; michael@0: char *url = NULL; michael@0: const CK_ATTRIBUTE *subject,*crl; michael@0: SECStatus rv; michael@0: NSSLOWCERTCertDBHandle *certHandle; michael@0: michael@0: certHandle = lg_getCertDB(sdb); michael@0: michael@0: /* we can't store any private crls */ michael@0: if (lg_isTrue(CKA_PRIVATE,templ,count)) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: if (certHandle == NULL) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: /* lookup SUBJECT */ michael@0: subject = lg_FindAttribute(CKA_SUBJECT,templ,count); michael@0: if (!subject) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: derSubj.data = (unsigned char *)subject->pValue; michael@0: derSubj.len = subject->ulValueLen ; michael@0: michael@0: /* lookup VALUE */ michael@0: crl = lg_FindAttribute(CKA_VALUE,templ,count); michael@0: PORT_Assert(crl); michael@0: if (!crl) { michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: derCrl.data = (unsigned char *)crl->pValue; michael@0: derCrl.len = crl->ulValueLen ; michael@0: michael@0: url = lg_getString(CKA_NSS_URL,templ,count); michael@0: isKRL = lg_isTrue(CKA_NSS_KRL,templ,count); michael@0: michael@0: /* Store CRL by SUBJECT */ michael@0: rv = nsslowcert_AddCrl(certHandle, &derCrl, &derSubj, url, isKRL); michael@0: michael@0: if (url) { michael@0: PORT_Free(url); michael@0: } michael@0: if (rv != SECSuccess) { michael@0: return CKR_DEVICE_ERROR; michael@0: } michael@0: michael@0: /* if we overwrote the existing CRL, poison the handle entry so we get michael@0: * a new object handle */ michael@0: (void) lg_poisonHandle(sdb, &derSubj, michael@0: isKRL ? LG_TOKEN_KRL_HANDLE : LG_TOKEN_TYPE_CRL); michael@0: *handle = lg_mkHandle(sdb, &derSubj, michael@0: isKRL ? LG_TOKEN_KRL_HANDLE : LG_TOKEN_TYPE_CRL); michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Public Key Object michael@0: */ michael@0: static CK_RV michael@0: lg_createPublicKeyObject(SDB *sdb, CK_KEY_TYPE key_type, michael@0: CK_OBJECT_HANDLE *handle, const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: CK_ATTRIBUTE_TYPE pubKeyAttr = CKA_VALUE; michael@0: CK_RV crv = CKR_OK; michael@0: NSSLOWKEYPrivateKey *priv; michael@0: SECItem pubKeySpace = {siBuffer, NULL, 0}; michael@0: SECItem *pubKey; michael@0: #ifndef NSS_DISABLE_ECC michael@0: SECItem pubKey2Space = {siBuffer, NULL, 0}; michael@0: PLArenaPool *arena = NULL; michael@0: #endif /* NSS_DISABLE_ECC */ michael@0: NSSLOWKEYDBHandle *keyHandle = NULL; michael@0: michael@0: michael@0: switch (key_type) { michael@0: case CKK_RSA: michael@0: pubKeyAttr = CKA_MODULUS; michael@0: break; michael@0: #ifndef NSS_DISABLE_ECC michael@0: case CKK_EC: michael@0: pubKeyAttr = CKA_EC_POINT; michael@0: break; michael@0: #endif /* NSS_DISABLE_ECC */ michael@0: case CKK_DSA: michael@0: case CKK_DH: michael@0: break; michael@0: default: michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: michael@0: pubKey = &pubKeySpace; michael@0: crv = lg_Attribute2SSecItem(NULL,pubKeyAttr,templ,count,pubKey); michael@0: if (crv != CKR_OK) return crv; michael@0: michael@0: #ifndef NSS_DISABLE_ECC michael@0: if (key_type == CKK_EC) { michael@0: SECStatus rv; michael@0: /* michael@0: * for ECC, use the decoded key first. michael@0: */ michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: crv = CKR_HOST_MEMORY; michael@0: goto done; michael@0: } michael@0: rv= SEC_QuickDERDecodeItem(arena, &pubKey2Space, michael@0: SEC_ASN1_GET(SEC_OctetStringTemplate), michael@0: pubKey); michael@0: if (rv != SECSuccess) { michael@0: /* decode didn't work, just try the pubKey */ michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: arena = NULL; michael@0: } else { michael@0: /* try the decoded pub key first */ michael@0: pubKey = &pubKey2Space; michael@0: } michael@0: } michael@0: #endif /* NSS_DISABLE_ECC */ michael@0: michael@0: PORT_Assert(pubKey->data); michael@0: if (pubKey->data == NULL) { michael@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: goto done; michael@0: } michael@0: keyHandle = lg_getKeyDB(sdb); michael@0: if (keyHandle == NULL) { michael@0: crv = CKR_TOKEN_WRITE_PROTECTED; michael@0: goto done; michael@0: } michael@0: if (keyHandle->version != 3) { michael@0: unsigned char buf[SHA1_LENGTH]; michael@0: SHA1_HashBuf(buf,pubKey->data,pubKey->len); michael@0: PORT_Memcpy(pubKey->data,buf,sizeof(buf)); michael@0: pubKey->len = sizeof(buf); michael@0: } michael@0: /* make sure the associated private key already exists */ michael@0: /* only works if we are logged in */ michael@0: priv = nsslowkey_FindKeyByPublicKey(keyHandle, pubKey, sdb /*password*/); michael@0: #ifndef NSS_DISABLE_ECC michael@0: if (priv == NULL && pubKey == &pubKey2Space) { michael@0: /* no match on the decoded key, match the original pubkey */ michael@0: pubKey = &pubKeySpace; michael@0: priv = nsslowkey_FindKeyByPublicKey(keyHandle, pubKey, michael@0: sdb /*password*/); michael@0: } michael@0: #endif michael@0: if (priv == NULL) { michael@0: /* the legacy database can only 'store' public keys which already michael@0: * have their corresponding private keys in the database */ michael@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: goto done; michael@0: } michael@0: lg_nsslowkey_DestroyPrivateKey(priv); michael@0: crv = CKR_OK; michael@0: michael@0: *handle = lg_mkHandle(sdb, pubKey, LG_TOKEN_TYPE_PUB); michael@0: michael@0: done: michael@0: PORT_Free(pubKeySpace.data); michael@0: #ifndef NSS_DISABLE_ECC michael@0: if (arena) michael@0: PORT_FreeArena(arena, PR_FALSE); michael@0: #endif michael@0: michael@0: return crv; michael@0: } michael@0: michael@0: /* make a private key from a verified object */ michael@0: static NSSLOWKEYPrivateKey * michael@0: lg_mkPrivKey(SDB *sdb, const CK_ATTRIBUTE *templ, CK_ULONG count, michael@0: CK_KEY_TYPE key_type, CK_RV *crvp) michael@0: { michael@0: NSSLOWKEYPrivateKey *privKey; michael@0: PLArenaPool *arena; michael@0: CK_RV crv = CKR_OK; michael@0: SECStatus rv; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { michael@0: *crvp = CKR_HOST_MEMORY; michael@0: return NULL; michael@0: } michael@0: michael@0: privKey = (NSSLOWKEYPrivateKey *) michael@0: PORT_ArenaZAlloc(arena,sizeof(NSSLOWKEYPrivateKey)); michael@0: if (privKey == NULL) { michael@0: PORT_FreeArena(arena,PR_FALSE); michael@0: *crvp = CKR_HOST_MEMORY; michael@0: return NULL; michael@0: } michael@0: michael@0: /* in future this would be a switch on key_type */ michael@0: privKey->arena = arena; michael@0: switch (key_type) { michael@0: case CKK_RSA: michael@0: privKey->keyType = NSSLOWKEYRSAKey; michael@0: crv=lg_Attribute2SSecItem(arena,CKA_MODULUS,templ,count, michael@0: &privKey->u.rsa.modulus); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_Attribute2SSecItem(arena,CKA_PUBLIC_EXPONENT,templ,count, michael@0: &privKey->u.rsa.publicExponent); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_PrivAttr2SSecItem(arena,CKA_PRIVATE_EXPONENT,templ,count, michael@0: &privKey->u.rsa.privateExponent, sdb); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_PrivAttr2SSecItem(arena,CKA_PRIME_1,templ,count, michael@0: &privKey->u.rsa.prime1, sdb); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_PrivAttr2SSecItem(arena,CKA_PRIME_2,templ,count, michael@0: &privKey->u.rsa.prime2, sdb); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_PrivAttr2SSecItem(arena,CKA_EXPONENT_1,templ,count, michael@0: &privKey->u.rsa.exponent1, sdb); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_PrivAttr2SSecItem(arena,CKA_EXPONENT_2,templ,count, michael@0: &privKey->u.rsa.exponent2, sdb); michael@0: if (crv != CKR_OK) break; michael@0: crv=lg_PrivAttr2SSecItem(arena,CKA_COEFFICIENT,templ,count, michael@0: &privKey->u.rsa.coefficient, sdb); michael@0: if (crv != CKR_OK) break; michael@0: rv = DER_SetUInteger(privKey->arena, &privKey->u.rsa.version, michael@0: NSSLOWKEY_VERSION); michael@0: if (rv != SECSuccess) crv = CKR_HOST_MEMORY; michael@0: break; michael@0: michael@0: case CKK_DSA: michael@0: privKey->keyType = NSSLOWKEYDSAKey; michael@0: crv = lg_Attribute2SSecItem(arena,CKA_PRIME,templ,count, michael@0: &privKey->u.dsa.params.prime); michael@0: if (crv != CKR_OK) break; michael@0: crv = lg_Attribute2SSecItem(arena,CKA_SUBPRIME,templ,count, michael@0: &privKey->u.dsa.params.subPrime); michael@0: if (crv != CKR_OK) break; michael@0: crv = lg_Attribute2SSecItem(arena,CKA_BASE,templ,count, michael@0: &privKey->u.dsa.params.base); michael@0: if (crv != CKR_OK) break; michael@0: crv = lg_PrivAttr2SSecItem(arena,CKA_VALUE,templ,count, michael@0: &privKey->u.dsa.privateValue, sdb); michael@0: if (crv != CKR_OK) break; michael@0: if (lg_hasAttribute(CKA_NETSCAPE_DB, templ,count)) { michael@0: crv = lg_Attribute2SSecItem(arena, CKA_NETSCAPE_DB,templ,count, michael@0: &privKey->u.dsa.publicValue); michael@0: /* privKey was zero'd so public value is already set to NULL, 0 michael@0: * if we don't set it explicitly */ michael@0: } michael@0: break; michael@0: michael@0: case CKK_DH: michael@0: privKey->keyType = NSSLOWKEYDHKey; michael@0: crv = lg_Attribute2SSecItem(arena,CKA_PRIME,templ,count, michael@0: &privKey->u.dh.prime); michael@0: if (crv != CKR_OK) break; michael@0: crv = lg_Attribute2SSecItem(arena,CKA_BASE,templ,count, michael@0: &privKey->u.dh.base); michael@0: if (crv != CKR_OK) break; michael@0: crv = lg_PrivAttr2SSecItem(arena,CKA_VALUE,templ,count, michael@0: &privKey->u.dh.privateValue, sdb); michael@0: if (crv != CKR_OK) break; michael@0: if (lg_hasAttribute(CKA_NETSCAPE_DB, templ, count)) { michael@0: crv = lg_Attribute2SSecItem(arena, CKA_NETSCAPE_DB,templ,count, michael@0: &privKey->u.dh.publicValue); michael@0: /* privKey was zero'd so public value is already set to NULL, 0 michael@0: * if we don't set it explicitly */ michael@0: } michael@0: break; michael@0: michael@0: #ifndef NSS_DISABLE_ECC michael@0: case CKK_EC: michael@0: privKey->keyType = NSSLOWKEYECKey; michael@0: crv = lg_Attribute2SSecItem(arena, CKA_EC_PARAMS,templ,count, michael@0: &privKey->u.ec.ecParams.DEREncoding); michael@0: if (crv != CKR_OK) break; michael@0: michael@0: /* Fill out the rest of the ecParams structure michael@0: * based on the encoded params michael@0: */ michael@0: if (LGEC_FillParams(arena, &privKey->u.ec.ecParams.DEREncoding, michael@0: &privKey->u.ec.ecParams) != SECSuccess) { michael@0: crv = CKR_DOMAIN_PARAMS_INVALID; michael@0: break; michael@0: } michael@0: crv = lg_PrivAttr2SSecItem(arena,CKA_VALUE,templ,count, michael@0: &privKey->u.ec.privateValue, sdb); michael@0: if (crv != CKR_OK) break; michael@0: if (lg_hasAttribute(CKA_NETSCAPE_DB,templ,count)) { michael@0: crv = lg_Attribute2SSecItem(arena, CKA_NETSCAPE_DB,templ,count, michael@0: &privKey->u.ec.publicValue); michael@0: if (crv != CKR_OK) break; michael@0: /* privKey was zero'd so public value is already set to NULL, 0 michael@0: * if we don't set it explicitly */ michael@0: } michael@0: rv = DER_SetUInteger(privKey->arena, &privKey->u.ec.version, michael@0: NSSLOWKEY_EC_PRIVATE_KEY_VERSION); michael@0: if (rv != SECSuccess) crv = CKR_HOST_MEMORY; michael@0: break; michael@0: #endif /* NSS_DISABLE_ECC */ michael@0: michael@0: default: michael@0: crv = CKR_KEY_TYPE_INCONSISTENT; michael@0: break; michael@0: } michael@0: *crvp = crv; michael@0: if (crv != CKR_OK) { michael@0: PORT_FreeArena(arena,PR_FALSE); michael@0: return NULL; michael@0: } michael@0: return privKey; michael@0: } michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Private Key Object michael@0: */ michael@0: static CK_RV michael@0: lg_createPrivateKeyObject(SDB *sdb, CK_KEY_TYPE key_type, michael@0: CK_OBJECT_HANDLE *handle, const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: NSSLOWKEYPrivateKey *privKey; michael@0: char *label; michael@0: SECStatus rv = SECSuccess; michael@0: CK_RV crv = CKR_DEVICE_ERROR; michael@0: SECItem pubKey; michael@0: NSSLOWKEYDBHandle *keyHandle = lg_getKeyDB(sdb); michael@0: michael@0: if (keyHandle == NULL) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: privKey=lg_mkPrivKey(sdb, templ,count,key_type,&crv); michael@0: if (privKey == NULL) return crv; michael@0: label = lg_getString(CKA_LABEL,templ,count); michael@0: michael@0: crv = lg_Attribute2SSecItem(NULL,CKA_NETSCAPE_DB,templ,count,&pubKey); michael@0: if (crv != CKR_OK) { michael@0: crv = CKR_TEMPLATE_INCOMPLETE; michael@0: rv = SECFailure; michael@0: goto fail; michael@0: } michael@0: #ifdef notdef michael@0: if (keyHandle->version != 3) { michael@0: unsigned char buf[SHA1_LENGTH]; michael@0: SHA1_HashBuf(buf,pubKey.data,pubKey.len); michael@0: PORT_Memcpy(pubKey.data,buf,sizeof(buf)); michael@0: pubKey.len = sizeof(buf); michael@0: } michael@0: #endif michael@0: /* get the key type */ michael@0: if (key_type == CKK_RSA) { michael@0: rv = RSA_PrivateKeyCheck(&privKey->u.rsa); michael@0: if (rv == SECFailure) { michael@0: goto fail; michael@0: } michael@0: } michael@0: rv = nsslowkey_StoreKeyByPublicKey(keyHandle, privKey, &pubKey, michael@0: label, sdb /*->password*/); michael@0: michael@0: fail: michael@0: if (label) PORT_Free(label); michael@0: *handle = lg_mkHandle(sdb,&pubKey,LG_TOKEN_TYPE_PRIV); michael@0: if (pubKey.data) PORT_Free(pubKey.data); michael@0: lg_nsslowkey_DestroyPrivateKey(privKey); michael@0: if (rv != SECSuccess) return crv; michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: michael@0: #define LG_KEY_MAX_RETRIES 10 /* don't hang if we are having problems with the rng */ michael@0: #define LG_KEY_ID_SIZE 18 /* don't use either SHA1 or MD5 sizes */ michael@0: /* michael@0: * Secret keys must have a CKA_ID value to be stored in the database. This code michael@0: * will generate one if there wasn't one already. michael@0: */ michael@0: static CK_RV michael@0: lg_GenerateSecretCKA_ID(NSSLOWKEYDBHandle *handle, SECItem *id, char *label) michael@0: { michael@0: unsigned int retries; michael@0: SECStatus rv = SECSuccess; michael@0: CK_RV crv = CKR_OK; michael@0: michael@0: id->data = NULL; michael@0: if (label) { michael@0: id->data = (unsigned char *)PORT_Strdup(label); michael@0: if (id->data == NULL) { michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: id->len = PORT_Strlen(label)+1; michael@0: if (!nsslowkey_KeyForIDExists(handle,id)) { michael@0: return CKR_OK; michael@0: } michael@0: PORT_Free(id->data); michael@0: id->data = NULL; michael@0: id->len = 0; michael@0: } michael@0: id->data = (unsigned char *)PORT_Alloc(LG_KEY_ID_SIZE); michael@0: if (id->data == NULL) { michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: id->len = LG_KEY_ID_SIZE; michael@0: michael@0: retries = 0; michael@0: do { michael@0: rv = RNG_GenerateGlobalRandomBytes(id->data,id->len); michael@0: } while (rv == SECSuccess && nsslowkey_KeyForIDExists(handle,id) && michael@0: (++retries <= LG_KEY_MAX_RETRIES)); michael@0: michael@0: if ((rv != SECSuccess) || (retries > LG_KEY_MAX_RETRIES)) { michael@0: crv = CKR_DEVICE_ERROR; /* random number generator is bad */ michael@0: PORT_Free(id->data); michael@0: id->data = NULL; michael@0: id->len = 0; michael@0: } michael@0: return crv; michael@0: } michael@0: michael@0: michael@0: static NSSLOWKEYPrivateKey *lg_mkSecretKeyRep(const CK_ATTRIBUTE *templ, michael@0: CK_ULONG count, CK_KEY_TYPE key_type, michael@0: SECItem *pubkey, SDB *sdbpw) michael@0: { michael@0: NSSLOWKEYPrivateKey *privKey = 0; michael@0: PLArenaPool *arena = 0; michael@0: CK_KEY_TYPE keyType; michael@0: PRUint32 keyTypeStorage; michael@0: SECItem keyTypeItem; michael@0: CK_RV crv; michael@0: SECStatus rv; michael@0: static unsigned char derZero[1] = { 0 }; michael@0: michael@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); michael@0: if (arena == NULL) { crv = CKR_HOST_MEMORY; goto loser; } michael@0: michael@0: privKey = (NSSLOWKEYPrivateKey *) michael@0: PORT_ArenaZAlloc(arena,sizeof(NSSLOWKEYPrivateKey)); michael@0: if (privKey == NULL) { crv = CKR_HOST_MEMORY; goto loser; } michael@0: michael@0: privKey->arena = arena; michael@0: michael@0: /* Secret keys are represented in the database as "fake" RSA keys. michael@0: * The RSA key is marked as a secret key representation by setting the michael@0: * public exponent field to 0, which is an invalid RSA exponent. michael@0: * The other fields are set as follows: michael@0: * modulus - CKA_ID value for the secret key michael@0: * private exponent - CKA_VALUE (the key itself) michael@0: * coefficient - CKA_KEY_TYPE, which indicates what encryption algorithm michael@0: * is used for the key. michael@0: * all others - set to integer 0 michael@0: */ michael@0: privKey->keyType = NSSLOWKEYRSAKey; michael@0: michael@0: /* The modulus is set to the key id of the symmetric key */ michael@0: privKey->u.rsa.modulus.data = michael@0: (unsigned char *) PORT_ArenaAlloc(arena, pubkey->len); michael@0: if (privKey->u.rsa.modulus.data == NULL) { michael@0: crv = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: privKey->u.rsa.modulus.len = pubkey->len; michael@0: PORT_Memcpy(privKey->u.rsa.modulus.data, pubkey->data, pubkey->len); michael@0: michael@0: /* The public exponent is set to 0 to indicate a special key */ michael@0: privKey->u.rsa.publicExponent.len = sizeof derZero; michael@0: privKey->u.rsa.publicExponent.data = derZero; michael@0: michael@0: /* The private exponent is the actual key value */ michael@0: crv = lg_PrivAttr2SecItem(arena, CKA_VALUE, templ, count, michael@0: &privKey->u.rsa.privateExponent, sdbpw); michael@0: if (crv != CKR_OK) goto loser; michael@0: michael@0: /* All other fields empty - needs testing */ michael@0: privKey->u.rsa.prime1.len = sizeof derZero; michael@0: privKey->u.rsa.prime1.data = derZero; michael@0: michael@0: privKey->u.rsa.prime2.len = sizeof derZero; michael@0: privKey->u.rsa.prime2.data = derZero; michael@0: michael@0: privKey->u.rsa.exponent1.len = sizeof derZero; michael@0: privKey->u.rsa.exponent1.data = derZero; michael@0: michael@0: privKey->u.rsa.exponent2.len = sizeof derZero; michael@0: privKey->u.rsa.exponent2.data = derZero; michael@0: michael@0: /* Coeficient set to KEY_TYPE */ michael@0: crv = lg_GetULongAttribute(CKA_KEY_TYPE, templ, count, &keyType); michael@0: if (crv != CKR_OK) goto loser; michael@0: /* on 64 bit platforms, we still want to store 32 bits of keyType (This is michael@0: * safe since the PKCS #11 defines for all types are 32 bits or less). */ michael@0: keyTypeStorage = (PRUint32) keyType; michael@0: keyTypeStorage = PR_htonl(keyTypeStorage); michael@0: keyTypeItem.data = (unsigned char *)&keyTypeStorage; michael@0: keyTypeItem.len = sizeof (keyTypeStorage); michael@0: rv = SECITEM_CopyItem(arena, &privKey->u.rsa.coefficient, &keyTypeItem); michael@0: if (rv != SECSuccess) { michael@0: crv = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: /* Private key version field set normally for compatibility */ michael@0: rv = DER_SetUInteger(privKey->arena, michael@0: &privKey->u.rsa.version, NSSLOWKEY_VERSION); michael@0: if (rv != SECSuccess) { crv = CKR_HOST_MEMORY; goto loser; } michael@0: michael@0: loser: michael@0: if (crv != CKR_OK) { michael@0: PORT_FreeArena(arena,PR_FALSE); michael@0: privKey = 0; michael@0: } michael@0: michael@0: return privKey; michael@0: } michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Secret Key Object michael@0: */ michael@0: static CK_RV michael@0: lg_createSecretKeyObject(SDB *sdb, CK_KEY_TYPE key_type, michael@0: CK_OBJECT_HANDLE *handle, const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: CK_RV crv; michael@0: NSSLOWKEYPrivateKey *privKey = NULL; michael@0: NSSLOWKEYDBHandle *keyHandle = NULL; michael@0: SECItem pubKey; michael@0: char *label = NULL; michael@0: SECStatus rv = SECSuccess; michael@0: michael@0: pubKey.data = 0; michael@0: michael@0: /* If the object is a TOKEN object, store in the database */ michael@0: keyHandle = lg_getKeyDB(sdb); michael@0: michael@0: if (keyHandle == NULL) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: label = lg_getString(CKA_LABEL,templ,count); michael@0: michael@0: crv = lg_Attribute2SecItem(NULL,CKA_ID,templ,count,&pubKey); michael@0: /* Should this be ID? */ michael@0: if (crv != CKR_OK) goto loser; michael@0: michael@0: /* if we don't have an ID, generate one */ michael@0: if (pubKey.len == 0) { michael@0: if (pubKey.data) { michael@0: PORT_Free(pubKey.data); michael@0: pubKey.data = NULL; michael@0: } michael@0: crv = lg_GenerateSecretCKA_ID(keyHandle, &pubKey, label); michael@0: if (crv != CKR_OK) goto loser; michael@0: } michael@0: michael@0: privKey = lg_mkSecretKeyRep(templ, count, key_type, &pubKey, sdb); michael@0: if (privKey == NULL) { michael@0: crv = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: rv = nsslowkey_StoreKeyByPublicKey(keyHandle, michael@0: privKey, &pubKey, label, sdb /*->password*/); michael@0: if (rv != SECSuccess) { michael@0: crv = CKR_DEVICE_ERROR; michael@0: goto loser; michael@0: } michael@0: michael@0: *handle = lg_mkHandle(sdb, &pubKey, LG_TOKEN_TYPE_KEY); michael@0: michael@0: loser: michael@0: if (label) PORT_Free(label); michael@0: if (privKey) lg_nsslowkey_DestroyPrivateKey(privKey); michael@0: if (pubKey.data) PORT_Free(pubKey.data); michael@0: michael@0: return crv; michael@0: } michael@0: michael@0: /* michael@0: * check the consistancy and initialize a Key Object michael@0: */ michael@0: static CK_RV michael@0: lg_createKeyObject(SDB *sdb, CK_OBJECT_CLASS objclass, michael@0: CK_OBJECT_HANDLE *handle, const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: CK_RV crv; michael@0: CK_KEY_TYPE key_type; michael@0: michael@0: /* get the key type */ michael@0: crv = lg_GetULongAttribute(CKA_KEY_TYPE, templ, count, &key_type); michael@0: if (crv != CKR_OK) { michael@0: return crv; michael@0: } michael@0: michael@0: switch (objclass) { michael@0: case CKO_PUBLIC_KEY: michael@0: return lg_createPublicKeyObject(sdb,key_type,handle,templ,count); michael@0: case CKO_PRIVATE_KEY: michael@0: return lg_createPrivateKeyObject(sdb,key_type,handle,templ,count); michael@0: case CKO_SECRET_KEY: michael@0: return lg_createSecretKeyObject(sdb,key_type,handle,templ,count); michael@0: default: michael@0: break; michael@0: } michael@0: return CKR_ATTRIBUTE_VALUE_INVALID; michael@0: } michael@0: michael@0: /* michael@0: * Parse the template and create an object stored in the DB that reflects. michael@0: * the object specified in the database. michael@0: */ michael@0: CK_RV michael@0: lg_CreateObject(SDB *sdb, CK_OBJECT_HANDLE *handle, michael@0: const CK_ATTRIBUTE *templ, CK_ULONG count) michael@0: { michael@0: CK_RV crv; michael@0: CK_OBJECT_CLASS objclass; michael@0: michael@0: /* get the object class */ michael@0: crv = lg_GetULongAttribute(CKA_CLASS, templ, count, &objclass); michael@0: if (crv != CKR_OK) { michael@0: return crv; michael@0: } michael@0: michael@0: /* Now handle the specific object class. michael@0: */ michael@0: switch (objclass) { michael@0: case CKO_CERTIFICATE: michael@0: crv = lg_createCertObject(sdb,handle,templ,count); michael@0: break; michael@0: case CKO_NSS_TRUST: michael@0: crv = lg_createTrustObject(sdb,handle,templ,count); michael@0: break; michael@0: case CKO_NSS_CRL: michael@0: crv = lg_createCrlObject(sdb,handle,templ,count); michael@0: break; michael@0: case CKO_NSS_SMIME: michael@0: crv = lg_createSMimeObject(sdb,handle,templ,count); michael@0: break; michael@0: case CKO_PRIVATE_KEY: michael@0: case CKO_PUBLIC_KEY: michael@0: case CKO_SECRET_KEY: michael@0: crv = lg_createKeyObject(sdb,objclass,handle,templ,count); michael@0: break; michael@0: default: michael@0: crv = CKR_ATTRIBUTE_VALUE_INVALID; michael@0: break; michael@0: } michael@0: michael@0: return crv; michael@0: } michael@0: