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: * The following code handles the storage of PKCS 11 modules used by the andre@0: * NSS. For the rest of NSS, only one kind of database handle exists: andre@0: * andre@0: * SFTKDBHandle andre@0: * andre@0: * There is one SFTKDBHandle for the each key database and one for each cert andre@0: * database. These databases are opened as associated pairs, one pair per andre@0: * slot. SFTKDBHandles are reference counted objects. andre@0: * andre@0: * Each SFTKDBHandle points to a low level database handle (SDB). This handle andre@0: * represents the underlying physical database. These objects are not andre@0: * reference counted, an are 'owned' by their respective SFTKDBHandles. andre@0: * andre@0: * andre@0: */ andre@0: #include "sftkdb.h" andre@0: #include "sftkdbti.h" andre@0: #include "pkcs11t.h" andre@0: #include "pkcs11i.h" andre@0: #include "sdb.h" andre@0: #include "prprf.h" andre@0: #include "secasn1.h" andre@0: #include "pratom.h" andre@0: #include "blapi.h" andre@0: #include "secoid.h" andre@0: #include "lowpbe.h" andre@0: #include "secdert.h" andre@0: #include "prsystem.h" andre@0: #include "lgglue.h" andre@0: #include "secerr.h" andre@0: #include "softoken.h" andre@0: andre@0: /****************************************************************** andre@0: * andre@0: * Key DB password handling functions andre@0: * andre@0: * These functions manage the key db password (set, reset, initialize, use). andre@0: * andre@0: * The key is managed on 'this side' of the database. All private data is andre@0: * encrypted before it is sent to the database itself. Besides PBE's, the andre@0: * database management code can also mix in various fixed keys so the data andre@0: * in the database is no longer considered 'plain text'. andre@0: */ andre@0: andre@0: andre@0: /* take string password and turn it into a key. The key is dependent andre@0: * on a global salt entry acquired from the database. This salted andre@0: * value will be based to a pkcs5 pbe function before it is used andre@0: * in an actual encryption */ andre@0: static SECStatus andre@0: sftkdb_passwordToKey(SFTKDBHandle *keydb, SECItem *salt, andre@0: const char *pw, SECItem *key) andre@0: { andre@0: SHA1Context *cx = NULL; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: key->data = PORT_Alloc(SHA1_LENGTH); andre@0: if (key->data == NULL) { andre@0: goto loser; andre@0: } andre@0: key->len = SHA1_LENGTH; andre@0: andre@0: cx = SHA1_NewContext(); andre@0: if ( cx == NULL) { andre@0: goto loser; andre@0: } andre@0: SHA1_Begin(cx); andre@0: if (salt && salt->data ) { andre@0: SHA1_Update(cx, salt->data, salt->len); andre@0: } andre@0: SHA1_Update(cx, (unsigned char *)pw, PORT_Strlen(pw)); andre@0: SHA1_End(cx, key->data, &key->len, key->len); andre@0: rv = SECSuccess; andre@0: andre@0: loser: andre@0: if (cx) { andre@0: SHA1_DestroyContext(cx, PR_TRUE); andre@0: } andre@0: if (rv != SECSuccess) { andre@0: if (key->data != NULL) { andre@0: PORT_ZFree(key->data,key->len); andre@0: } andre@0: key->data = NULL; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * Cipher text stored in the database contains 3 elements: andre@0: * 1) an identifier describing the encryption algorithm. andre@0: * 2) an entry specific salt value. andre@0: * 3) the encrypted value. andre@0: * andre@0: * The following data structure represents the encrypted data in a decoded andre@0: * (but still encrypted) form. andre@0: */ andre@0: typedef struct sftkCipherValueStr sftkCipherValue; andre@0: struct sftkCipherValueStr { andre@0: PLArenaPool *arena; andre@0: SECOidTag alg; andre@0: NSSPKCS5PBEParameter *param; andre@0: SECItem salt; andre@0: SECItem value; andre@0: }; andre@0: andre@0: #define SFTK_CIPHERTEXT_VERSION 3 andre@0: andre@0: struct SFTKDBEncryptedDataInfoStr { andre@0: SECAlgorithmID algorithm; andre@0: SECItem encryptedData; andre@0: }; andre@0: typedef struct SFTKDBEncryptedDataInfoStr SFTKDBEncryptedDataInfo; andre@0: andre@0: SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) andre@0: andre@0: const SEC_ASN1Template sftkdb_EncryptedDataInfoTemplate[] = { andre@0: { SEC_ASN1_SEQUENCE, andre@0: 0, NULL, sizeof(SFTKDBEncryptedDataInfo) }, andre@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN , andre@0: offsetof(SFTKDBEncryptedDataInfo,algorithm), andre@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, andre@0: { SEC_ASN1_OCTET_STRING, andre@0: offsetof(SFTKDBEncryptedDataInfo,encryptedData) }, andre@0: { 0 } andre@0: }; andre@0: andre@0: /* andre@0: * This parses the cipherText into cipher value. NOTE: cipherValue will point andre@0: * to data in cipherText, if cipherText is freed, cipherValue will be invalid. andre@0: */ andre@0: static SECStatus andre@0: sftkdb_decodeCipherText(SECItem *cipherText, sftkCipherValue *cipherValue) andre@0: { andre@0: PLArenaPool *arena = NULL; andre@0: SFTKDBEncryptedDataInfo edi; andre@0: SECStatus rv; andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if (arena == NULL) { andre@0: return SECFailure; andre@0: } andre@0: cipherValue->arena = NULL; andre@0: cipherValue->param = NULL; andre@0: andre@0: rv = SEC_QuickDERDecodeItem(arena, &edi, sftkdb_EncryptedDataInfoTemplate, andre@0: cipherText); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: cipherValue->alg = SECOID_GetAlgorithmTag(&edi.algorithm); andre@0: cipherValue->param = nsspkcs5_AlgidToParam(&edi.algorithm); andre@0: if (cipherValue->param == NULL) { andre@0: goto loser; andre@0: } andre@0: cipherValue->value = edi.encryptedData; andre@0: cipherValue->arena = arena; andre@0: andre@0: return SECSuccess; andre@0: loser: andre@0: if (cipherValue->param) { andre@0: nsspkcs5_DestroyPBEParameter(cipherValue->param); andre@0: cipherValue->param = NULL; andre@0: } andre@0: if (arena) { andre@0: PORT_FreeArena(arena,PR_FALSE); andre@0: } andre@0: return SECFailure; andre@0: } andre@0: andre@0: andre@0: andre@0: /* andre@0: * unlike decode, Encode actually allocates a SECItem the caller must free andre@0: * The caller can pass an optional arena to to indicate where to place andre@0: * the resultant cipherText. andre@0: */ andre@0: static SECStatus andre@0: sftkdb_encodeCipherText(PLArenaPool *arena, sftkCipherValue *cipherValue, andre@0: SECItem **cipherText) andre@0: { andre@0: SFTKDBEncryptedDataInfo edi; andre@0: SECAlgorithmID *algid; andre@0: SECStatus rv; andre@0: PLArenaPool *localArena = NULL; andre@0: andre@0: andre@0: localArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if (localArena == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: algid = nsspkcs5_CreateAlgorithmID(localArena, cipherValue->alg, andre@0: cipherValue->param); andre@0: if (algid == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: rv = SECOID_CopyAlgorithmID(localArena, &edi.algorithm, algid); andre@0: SECOID_DestroyAlgorithmID(algid, PR_TRUE); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: edi.encryptedData = cipherValue->value; andre@0: andre@0: *cipherText = SEC_ASN1EncodeItem(arena, NULL, &edi, andre@0: sftkdb_EncryptedDataInfoTemplate); andre@0: if (*cipherText == NULL) { andre@0: rv = SECFailure; andre@0: } andre@0: andre@0: loser: andre@0: if (localArena) { andre@0: PORT_FreeArena(localArena,PR_FALSE); andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * Use our key to decode a cipherText block from the database. andre@0: * andre@0: * plain text is allocated by nsspkcs5_CipherData and must be freed andre@0: * with SECITEM_FreeItem by the caller. andre@0: */ andre@0: SECStatus andre@0: sftkdb_DecryptAttribute(SECItem *passKey, SECItem *cipherText, SECItem **plain) andre@0: { andre@0: SECStatus rv; andre@0: sftkCipherValue cipherValue; andre@0: andre@0: /* First get the cipher type */ andre@0: rv = sftkdb_decodeCipherText(cipherText, &cipherValue); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: *plain = nsspkcs5_CipherData(cipherValue.param, passKey, &cipherValue.value, andre@0: PR_FALSE, NULL); andre@0: if (*plain == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: loser: andre@0: if (cipherValue.param) { andre@0: nsspkcs5_DestroyPBEParameter(cipherValue.param); andre@0: } andre@0: if (cipherValue.arena) { andre@0: PORT_FreeArena(cipherValue.arena,PR_FALSE); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * encrypt a block. This function returned the encrypted ciphertext which andre@0: * the caller must free. If the caller provides an arena, cipherText will andre@0: * be allocated out of that arena. This also generated the per entry andre@0: * salt automatically. andre@0: */ andre@0: SECStatus andre@0: sftkdb_EncryptAttribute(PLArenaPool *arena, SECItem *passKey, andre@0: SECItem *plainText, SECItem **cipherText) andre@0: { andre@0: SECStatus rv; andre@0: sftkCipherValue cipherValue; andre@0: SECItem *cipher = NULL; andre@0: NSSPKCS5PBEParameter *param = NULL; andre@0: unsigned char saltData[HASH_LENGTH_MAX]; andre@0: andre@0: cipherValue.alg = SEC_OID_PKCS12_PBE_WITH_SHA1_AND_TRIPLE_DES_CBC; andre@0: cipherValue.salt.len = SHA1_LENGTH; andre@0: cipherValue.salt.data = saltData; andre@0: RNG_GenerateGlobalRandomBytes(saltData,cipherValue.salt.len); andre@0: andre@0: param = nsspkcs5_NewParam(cipherValue.alg, &cipherValue.salt, 1); andre@0: if (param == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: cipher = nsspkcs5_CipherData(param, passKey, plainText, PR_TRUE, NULL); andre@0: if (cipher == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: cipherValue.value = *cipher; andre@0: cipherValue.param = param; andre@0: andre@0: rv = sftkdb_encodeCipherText(arena, &cipherValue, cipherText); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: loser: andre@0: if (cipher) { andre@0: SECITEM_FreeItem(cipher, PR_TRUE); andre@0: } andre@0: if (param) { andre@0: nsspkcs5_DestroyPBEParameter(param); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * use the password and the pbe parameters to generate an HMAC for the andre@0: * given plain text data. This is used by sftkdb_VerifyAttribute and andre@0: * sftkdb_SignAttribute. Signature is returned in signData. The caller andre@0: * must preallocate the space in the secitem. andre@0: */ andre@0: static SECStatus andre@0: sftkdb_pbehash(SECOidTag sigOid, SECItem *passKey, andre@0: NSSPKCS5PBEParameter *param, andre@0: CK_OBJECT_HANDLE objectID, CK_ATTRIBUTE_TYPE attrType, andre@0: SECItem *plainText, SECItem *signData) andre@0: { andre@0: SECStatus rv = SECFailure; andre@0: SECItem *key = NULL; andre@0: HMACContext *hashCx = NULL; andre@0: HASH_HashType hashType = HASH_AlgNULL; andre@0: const SECHashObject *hashObj; andre@0: unsigned char addressData[SDB_ULONG_SIZE]; andre@0: andre@0: hashType = HASH_FromHMACOid(param->encAlg); andre@0: if (hashType == HASH_AlgNULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: return SECFailure; andre@0: } andre@0: andre@0: hashObj = HASH_GetRawHashObject(hashType); andre@0: if (hashObj == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: key = nsspkcs5_ComputeKeyAndIV(param, passKey, NULL, PR_FALSE); andre@0: if (!key) { andre@0: goto loser; andre@0: } andre@0: andre@0: hashCx = HMAC_Create(hashObj, key->data, key->len, PR_TRUE); andre@0: if (!hashCx) { andre@0: goto loser; andre@0: } andre@0: HMAC_Begin(hashCx); andre@0: /* Tie this value to a particular object. This is most important for andre@0: * the trust attributes, where and attacker could copy a value for andre@0: * 'validCA' from another cert in the database */ andre@0: sftk_ULong2SDBULong(addressData, objectID); andre@0: HMAC_Update(hashCx, addressData, SDB_ULONG_SIZE); andre@0: sftk_ULong2SDBULong(addressData, attrType); andre@0: HMAC_Update(hashCx, addressData, SDB_ULONG_SIZE); andre@0: andre@0: HMAC_Update(hashCx, plainText->data, plainText->len); andre@0: rv = HMAC_Finish(hashCx, signData->data, &signData->len, signData->len); andre@0: andre@0: loser: andre@0: if (hashCx) { andre@0: HMAC_Destroy(hashCx, PR_TRUE); andre@0: } andre@0: if (key) { andre@0: SECITEM_FreeItem(key,PR_TRUE); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * Use our key to verify a signText block from the database matches andre@0: * the plainText from the database. The signText is a PKCS 5 v2 pbe. andre@0: * plainText is the plainText of the attribute. andre@0: */ andre@0: SECStatus andre@0: sftkdb_VerifyAttribute(SECItem *passKey, CK_OBJECT_HANDLE objectID, andre@0: CK_ATTRIBUTE_TYPE attrType, andre@0: SECItem *plainText, SECItem *signText) andre@0: { andre@0: SECStatus rv; andre@0: sftkCipherValue signValue; andre@0: SECItem signature; andre@0: unsigned char signData[HASH_LENGTH_MAX]; andre@0: andre@0: andre@0: /* First get the cipher type */ andre@0: rv = sftkdb_decodeCipherText(signText, &signValue); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: signature.data = signData; andre@0: signature.len = sizeof(signData); andre@0: andre@0: rv = sftkdb_pbehash(signValue.alg, passKey, signValue.param, andre@0: objectID, attrType, plainText, &signature); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: if (SECITEM_CompareItem(&signValue.value,&signature) != 0) { andre@0: PORT_SetError(SEC_ERROR_BAD_SIGNATURE); andre@0: rv = SECFailure; andre@0: } andre@0: andre@0: loser: andre@0: if (signValue.param) { andre@0: nsspkcs5_DestroyPBEParameter(signValue.param); andre@0: } andre@0: if (signValue.arena) { andre@0: PORT_FreeArena(signValue.arena,PR_FALSE); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * Use our key to create a signText block the plain text of an andre@0: * attribute. The signText is a PKCS 5 v2 pbe. andre@0: */ andre@0: SECStatus andre@0: sftkdb_SignAttribute(PLArenaPool *arena, SECItem *passKey, andre@0: CK_OBJECT_HANDLE objectID, CK_ATTRIBUTE_TYPE attrType, andre@0: SECItem *plainText, SECItem **signature) andre@0: { andre@0: SECStatus rv; andre@0: sftkCipherValue signValue; andre@0: NSSPKCS5PBEParameter *param = NULL; andre@0: unsigned char saltData[HASH_LENGTH_MAX]; andre@0: unsigned char signData[HASH_LENGTH_MAX]; andre@0: SECOidTag hmacAlg = SEC_OID_HMAC_SHA256; /* hash for authentication */ andre@0: SECOidTag prfAlg = SEC_OID_HMAC_SHA256; /* hash for pb key generation */ andre@0: HASH_HashType prfType; andre@0: unsigned int hmacLength; andre@0: unsigned int prfLength; andre@0: andre@0: /* this code allows us to fetch the lengths and hashes on the fly andre@0: * by simply changing the OID above */ andre@0: prfType = HASH_FromHMACOid(prfAlg); andre@0: PORT_Assert(prfType != HASH_AlgNULL); andre@0: prfLength = HASH_GetRawHashObject(prfType)->length; andre@0: PORT_Assert(prfLength <= HASH_LENGTH_MAX); andre@0: andre@0: hmacLength = HASH_GetRawHashObject(HASH_FromHMACOid(hmacAlg))->length; andre@0: PORT_Assert(hmacLength <= HASH_LENGTH_MAX); andre@0: andre@0: /* initialize our CipherValue structure */ andre@0: signValue.alg = SEC_OID_PKCS5_PBMAC1; andre@0: signValue.salt.len = prfLength; andre@0: signValue.salt.data = saltData; andre@0: signValue.value.data = signData; andre@0: signValue.value.len = hmacLength; andre@0: RNG_GenerateGlobalRandomBytes(saltData,prfLength); andre@0: andre@0: /* initialize our pkcs5 parameter */ andre@0: param = nsspkcs5_NewParam(signValue.alg, &signValue.salt, 1); andre@0: if (param == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: param->keyID = pbeBitGenIntegrityKey; andre@0: /* set the PKCS 5 v2 parameters, not extractable from the andre@0: * data passed into nsspkcs5_NewParam */ andre@0: param->encAlg = hmacAlg; andre@0: param->hashType = prfType; andre@0: param->keyLen = hmacLength; andre@0: rv = SECOID_SetAlgorithmID(param->poolp, ¶m->prfAlg, prfAlg, NULL); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: andre@0: /* calculate the mac */ andre@0: rv = sftkdb_pbehash(signValue.alg, passKey, param, objectID, attrType, andre@0: plainText, &signValue.value); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: signValue.param = param; andre@0: andre@0: /* write it out */ andre@0: rv = sftkdb_encodeCipherText(arena, &signValue, signature); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: loser: andre@0: if (param) { andre@0: nsspkcs5_DestroyPBEParameter(param); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * safely swith the passed in key for the one caches in the keydb handle andre@0: * andre@0: * A key attached to the handle tells us the the token is logged in. andre@0: * We can used the key attached to the handle in sftkdb_EncryptAttribute andre@0: * and sftkdb_DecryptAttribute calls. andre@0: */ andre@0: static void andre@0: sftkdb_switchKeys(SFTKDBHandle *keydb, SECItem *passKey) andre@0: { andre@0: unsigned char *data; andre@0: int len; andre@0: andre@0: if (keydb->passwordLock == NULL) { andre@0: PORT_Assert(keydb->type != SFTK_KEYDB_TYPE); andre@0: return; andre@0: } andre@0: andre@0: /* an atomic pointer set would be nice */ andre@0: SKIP_AFTER_FORK(PZ_Lock(keydb->passwordLock)); andre@0: data = keydb->passwordKey.data; andre@0: len = keydb->passwordKey.len; andre@0: keydb->passwordKey.data = passKey->data; andre@0: keydb->passwordKey.len = passKey->len; andre@0: passKey->data = data; andre@0: passKey->len = len; andre@0: SKIP_AFTER_FORK(PZ_Unlock(keydb->passwordLock)); andre@0: } andre@0: andre@0: /* andre@0: * returns true if we are in a middle of a merge style update. andre@0: */ andre@0: PRBool andre@0: sftkdb_InUpdateMerge(SFTKDBHandle *keydb) andre@0: { andre@0: return keydb->updateID ? PR_TRUE : PR_FALSE; andre@0: } andre@0: andre@0: /* andre@0: * returns true if we are looking for the password for the user's old source andre@0: * database as part of a merge style update. andre@0: */ andre@0: PRBool andre@0: sftkdb_NeedUpdateDBPassword(SFTKDBHandle *keydb) andre@0: { andre@0: if (!sftkdb_InUpdateMerge(keydb)) { andre@0: return PR_FALSE; andre@0: } andre@0: if (keydb->updateDBIsInit && !keydb->updatePasswordKey) { andre@0: return PR_TRUE; andre@0: } andre@0: return PR_FALSE; andre@0: } andre@0: andre@0: /* andre@0: * fetch an update password key from a handle. andre@0: */ andre@0: SECItem * andre@0: sftkdb_GetUpdatePasswordKey(SFTKDBHandle *handle) andre@0: { andre@0: SECItem *key = NULL; andre@0: andre@0: /* if we're a cert db, fetch it from our peer key db */ andre@0: if (handle->type == SFTK_CERTDB_TYPE) { andre@0: handle = handle->peerDB; andre@0: } andre@0: andre@0: /* don't have one */ andre@0: if (!handle) { andre@0: return NULL; andre@0: } andre@0: andre@0: PZ_Lock(handle->passwordLock); andre@0: if (handle->updatePasswordKey) { andre@0: key = SECITEM_DupItem(handle->updatePasswordKey); andre@0: } andre@0: PZ_Unlock(handle->passwordLock); andre@0: andre@0: return key; andre@0: } andre@0: andre@0: /* andre@0: * free the update password key from a handle. andre@0: */ andre@0: void andre@0: sftkdb_FreeUpdatePasswordKey(SFTKDBHandle *handle) andre@0: { andre@0: SECItem *key = NULL; andre@0: andre@0: /* don't have one */ andre@0: if (!handle) { andre@0: return; andre@0: } andre@0: andre@0: /* if we're a cert db, we don't have one */ andre@0: if (handle->type == SFTK_CERTDB_TYPE) { andre@0: return; andre@0: } andre@0: andre@0: PZ_Lock(handle->passwordLock); andre@0: if (handle->updatePasswordKey) { andre@0: key = handle->updatePasswordKey; andre@0: handle->updatePasswordKey = NULL; andre@0: } andre@0: PZ_Unlock(handle->passwordLock); andre@0: andre@0: if (key) { andre@0: SECITEM_ZfreeItem(key, PR_TRUE); andre@0: } andre@0: andre@0: return; andre@0: } andre@0: andre@0: /* andre@0: * what password db we use depends heavily on the update state machine andre@0: * andre@0: * 1) no update db, return the normal database. andre@0: * 2) update db and no merge return the update db. andre@0: * 3) update db and in merge: andre@0: * return the update db if we need the update db's password, andre@0: * otherwise return our normal datbase. andre@0: */ andre@0: static SDB * andre@0: sftk_getPWSDB(SFTKDBHandle *keydb) andre@0: { andre@0: if (!keydb->update) { andre@0: return keydb->db; andre@0: } andre@0: if (!sftkdb_InUpdateMerge(keydb)) { andre@0: return keydb->update; andre@0: } andre@0: if (sftkdb_NeedUpdateDBPassword(keydb)) { andre@0: return keydb->update; andre@0: } andre@0: return keydb->db; andre@0: } andre@0: andre@0: /* andre@0: * return success if we have a valid password entry. andre@0: * This is will show up outside of PKCS #11 as CKF_USER_PIN_INIT andre@0: * in the token flags. andre@0: */ andre@0: SECStatus andre@0: sftkdb_HasPasswordSet(SFTKDBHandle *keydb) andre@0: { andre@0: SECItem salt, value; andre@0: unsigned char saltData[SDB_MAX_META_DATA_LEN]; andre@0: unsigned char valueData[SDB_MAX_META_DATA_LEN]; andre@0: CK_RV crv; andre@0: SDB *db; andre@0: andre@0: if (keydb == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: db = sftk_getPWSDB(keydb); andre@0: if (db == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: salt.data = saltData; andre@0: salt.len = sizeof(saltData); andre@0: value.data = valueData; andre@0: value.len = sizeof(valueData); andre@0: crv = (*db->sdb_GetMetaData)(db, "password", &salt, &value); andre@0: andre@0: /* If no password is set, we can update right away */ andre@0: if (((keydb->db->sdb_flags & SDB_RDONLY) == 0) && keydb->update andre@0: && crv != CKR_OK) { andre@0: /* update the peer certdb if it exists */ andre@0: if (keydb->peerDB) { andre@0: sftkdb_Update(keydb->peerDB, NULL); andre@0: } andre@0: sftkdb_Update(keydb, NULL); andre@0: } andre@0: return (crv == CKR_OK) ? SECSuccess : SECFailure; andre@0: } andre@0: andre@0: #define SFTK_PW_CHECK_STRING "password-check" andre@0: #define SFTK_PW_CHECK_LEN 14 andre@0: andre@0: /* andre@0: * check if the supplied password is valid andre@0: */ andre@0: SECStatus andre@0: sftkdb_CheckPassword(SFTKDBHandle *keydb, const char *pw, PRBool *tokenRemoved) andre@0: { andre@0: SECStatus rv; andre@0: SECItem salt, value; andre@0: unsigned char saltData[SDB_MAX_META_DATA_LEN]; andre@0: unsigned char valueData[SDB_MAX_META_DATA_LEN]; andre@0: SECItem key; andre@0: SECItem *result = NULL; andre@0: SDB *db; andre@0: CK_RV crv; andre@0: andre@0: if (keydb == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: db = sftk_getPWSDB(keydb); andre@0: if (db == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: key.data = NULL; andre@0: key.len = 0; andre@0: andre@0: if (pw == NULL) pw=""; andre@0: andre@0: /* get the entry from the database */ andre@0: salt.data = saltData; andre@0: salt.len = sizeof(saltData); andre@0: value.data = valueData; andre@0: value.len = sizeof(valueData); andre@0: crv = (*db->sdb_GetMetaData)(db, "password", &salt, &value); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto done; andre@0: } andre@0: andre@0: /* get our intermediate key based on the entry salt value */ andre@0: rv = sftkdb_passwordToKey(keydb, &salt, pw, &key); andre@0: if (rv != SECSuccess) { andre@0: goto done; andre@0: } andre@0: andre@0: /* decrypt the entry value */ andre@0: rv = sftkdb_DecryptAttribute(&key, &value, &result); andre@0: if (rv != SECSuccess) { andre@0: goto done; andre@0: } andre@0: andre@0: /* if it's what we expect, update our key in the database handle and andre@0: * return Success */ andre@0: if ((result->len == SFTK_PW_CHECK_LEN) && andre@0: PORT_Memcmp(result->data, SFTK_PW_CHECK_STRING, SFTK_PW_CHECK_LEN) == 0){ andre@0: /* andre@0: * We have a password, now lets handle any potential update cases.. andre@0: * andre@0: * First, the normal case: no update. In this case we only need the andre@0: * the password for our only DB, which we now have, we switch andre@0: * the keys and fall through. andre@0: * Second regular (non-merge) update: The target DB does not yet have andre@0: * a password initialized, we now have the password for the source DB, andre@0: * so we can switch the keys and simply update the target database. andre@0: * Merge update case: This one is trickier. andre@0: * 1) If we need the source DB password, then we just got it here. andre@0: * We need to save that password, andre@0: * then we need to check to see if we need or have the target andre@0: * database password. andre@0: * If we have it (it's the same as the source), or don't need andre@0: * it (it's not set or is ""), we can start the update now. andre@0: * If we don't have it, we need the application to get it from andre@0: * the user. Clear our sessions out to simulate a token andre@0: * removal. C_GetTokenInfo will change the token description andre@0: * and the token will still appear to be logged out. andre@0: * 2) If we already have the source DB password, this password is andre@0: * for the target database. We can now move forward with the andre@0: * update, as we now have both required passwords. andre@0: * andre@0: */ andre@0: PZ_Lock(keydb->passwordLock); andre@0: if (sftkdb_NeedUpdateDBPassword(keydb)) { andre@0: /* Squirrel this special key away. andre@0: * This has the side effect of turning sftkdb_NeedLegacyPW off, andre@0: * as well as changing which database is returned from andre@0: * SFTK_GET_PW_DB (thus effecting both sftkdb_CheckPassword() andre@0: * and sftkdb_HasPasswordSet()) */ andre@0: keydb->updatePasswordKey = SECITEM_DupItem(&key); andre@0: PZ_Unlock(keydb->passwordLock); andre@0: if (keydb->updatePasswordKey == NULL) { andre@0: /* PORT_Error set by SECITEM_DupItem */ andre@0: rv = SECFailure; andre@0: goto done; andre@0: } andre@0: andre@0: /* Simulate a token removal -- we need to do this any andre@0: * any case at this point so the token name is correct. */ andre@0: *tokenRemoved = PR_TRUE; andre@0: andre@0: /* andre@0: * OK, we got the update DB password, see if we need a password andre@0: * for the target... andre@0: */ andre@0: if (sftkdb_HasPasswordSet(keydb) == SECSuccess) { andre@0: /* We have a password, do we know what the password is? andre@0: * check 1) for the password the user supplied for the andre@0: * update DB, andre@0: * and 2) for the null password. andre@0: * andre@0: * RECURSION NOTE: we are calling ourselves here. This means andre@0: * any updates, switchKeys, etc will have been completed andre@0: * if these functions return successfully, in those cases andre@0: * just exit returning Success. We don't recurse infinitely andre@0: * because we are making this call from a NeedUpdateDBPassword andre@0: * block and we've already set that update password at this andre@0: * point. */ andre@0: rv = sftkdb_CheckPassword(keydb, pw, tokenRemoved); andre@0: if (rv == SECSuccess) { andre@0: /* source and target databases have the same password, we andre@0: * are good to go */ andre@0: goto done; andre@0: } andre@0: sftkdb_CheckPassword(keydb, "", tokenRemoved); andre@0: andre@0: /* andre@0: * Important 'NULL' code here. At this point either we andre@0: * succeeded in logging in with "" or we didn't. andre@0: * andre@0: * If we did succeed at login, our machine state will be set andre@0: * to logged in appropriately. The application will find that andre@0: * it's logged in as soon as it opens a new session. We have andre@0: * also completed the update. Life is good. andre@0: * andre@0: * If we did not succeed, well the user still successfully andre@0: * logged into the update database, since we faked the token andre@0: * removal it's just like the user logged into his smart card andre@0: * then removed it. the actual login work, so we report that andre@0: * success back to the user, but we won't actually be andre@0: * logged in. The application will find this out when it andre@0: * checks it's login state, thus triggering another password andre@0: * prompt so we can get the real target DB password. andre@0: * andre@0: * summary, we exit from here with SECSuccess no matter what. andre@0: */ andre@0: rv = SECSuccess; andre@0: goto done; andre@0: } else { andre@0: /* there is no password, just fall through to update. andre@0: * update will write the source DB's password record andre@0: * into the target DB just like it would in a non-merge andre@0: * update case. */ andre@0: } andre@0: } else { andre@0: PZ_Unlock(keydb->passwordLock); andre@0: } andre@0: /* load the keys, so the keydb can parse it's key set */ andre@0: sftkdb_switchKeys(keydb, &key); andre@0: andre@0: /* we need to update, do it now */ andre@0: if (((keydb->db->sdb_flags & SDB_RDONLY) == 0) && keydb->update) { andre@0: /* update the peer certdb if it exists */ andre@0: if (keydb->peerDB) { andre@0: sftkdb_Update(keydb->peerDB, &key); andre@0: } andre@0: sftkdb_Update(keydb, &key); andre@0: } andre@0: } else { andre@0: rv = SECFailure; andre@0: /*PORT_SetError( bad password); */ andre@0: } andre@0: andre@0: done: andre@0: if (key.data) { andre@0: PORT_ZFree(key.data,key.len); andre@0: } andre@0: if (result) { andre@0: SECITEM_FreeItem(result,PR_TRUE); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * return Success if the there is a cached password key. andre@0: */ andre@0: SECStatus andre@0: sftkdb_PWCached(SFTKDBHandle *keydb) andre@0: { andre@0: return keydb->passwordKey.data ? SECSuccess : SECFailure; andre@0: } andre@0: andre@0: andre@0: static CK_RV andre@0: sftk_updateMacs(PLArenaPool *arena, SFTKDBHandle *handle, andre@0: CK_OBJECT_HANDLE id, SECItem *newKey) andre@0: { andre@0: CK_RV crv = CKR_OK; andre@0: CK_RV crv2; andre@0: CK_ATTRIBUTE authAttrs[] = { andre@0: {CKA_MODULUS, NULL, 0}, andre@0: {CKA_PUBLIC_EXPONENT, NULL, 0}, andre@0: {CKA_CERT_SHA1_HASH, NULL, 0}, andre@0: {CKA_CERT_MD5_HASH, NULL, 0}, andre@0: {CKA_TRUST_SERVER_AUTH, NULL, 0}, andre@0: {CKA_TRUST_CLIENT_AUTH, NULL, 0}, andre@0: {CKA_TRUST_EMAIL_PROTECTION, NULL, 0}, andre@0: {CKA_TRUST_CODE_SIGNING, NULL, 0}, andre@0: {CKA_TRUST_STEP_UP_APPROVED, NULL, 0}, andre@0: {CKA_NSS_OVERRIDE_EXTENSIONS, NULL, 0}, andre@0: }; andre@0: CK_ULONG authAttrCount = sizeof(authAttrs)/sizeof(CK_ATTRIBUTE); andre@0: int i, count; andre@0: SFTKDBHandle *keyHandle = handle; andre@0: SDB *keyTarget = NULL; andre@0: andre@0: id &= SFTK_OBJ_ID_MASK; andre@0: andre@0: if (handle->type != SFTK_KEYDB_TYPE) { andre@0: keyHandle = handle->peerDB; andre@0: } andre@0: andre@0: if (keyHandle == NULL) { andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* old DB's don't have meta data, finished with MACs */ andre@0: keyTarget = SFTK_GET_SDB(keyHandle); andre@0: if ((keyTarget->sdb_flags &SDB_HAS_META) == 0) { andre@0: return CKR_OK; andre@0: } andre@0: andre@0: /* andre@0: * STEP 1: find the MACed attributes of this object andre@0: */ andre@0: crv2 = sftkdb_GetAttributeValue(handle, id, authAttrs, authAttrCount); andre@0: count = 0; andre@0: /* allocate space for the attributes */ andre@0: for (i=0; i < authAttrCount; i++) { andre@0: if ((authAttrs[i].ulValueLen == -1) || (authAttrs[i].ulValueLen == 0)){ andre@0: continue; andre@0: } andre@0: count++; andre@0: authAttrs[i].pValue = PORT_ArenaAlloc(arena,authAttrs[i].ulValueLen); andre@0: if (authAttrs[i].pValue == NULL) { andre@0: crv = CKR_HOST_MEMORY; andre@0: break; andre@0: } andre@0: } andre@0: andre@0: /* if count was zero, none were found, finished with MACs */ andre@0: if (count == 0) { andre@0: return CKR_OK; andre@0: } andre@0: andre@0: crv = sftkdb_GetAttributeValue(handle, id, authAttrs, authAttrCount); andre@0: /* ignore error code, we expect some possible errors */ andre@0: andre@0: /* GetAttributeValue just verified the old macs, safe to write andre@0: * them out then... */ andre@0: for (i=0; i < authAttrCount; i++) { andre@0: SECItem *signText; andre@0: SECItem plainText; andre@0: SECStatus rv; andre@0: andre@0: if ((authAttrs[i].ulValueLen == -1) || (authAttrs[i].ulValueLen == 0)){ andre@0: continue; andre@0: } andre@0: andre@0: plainText.data = authAttrs[i].pValue; andre@0: plainText.len = authAttrs[i].ulValueLen; andre@0: rv = sftkdb_SignAttribute(arena, newKey, id, andre@0: authAttrs[i].type, &plainText, &signText); andre@0: if (rv != SECSuccess) { andre@0: return CKR_GENERAL_ERROR; andre@0: } andre@0: rv = sftkdb_PutAttributeSignature(handle, keyTarget, id, andre@0: authAttrs[i].type, signText); andre@0: if (rv != SECSuccess) { andre@0: return CKR_GENERAL_ERROR; andre@0: } andre@0: } andre@0: andre@0: return CKR_OK; andre@0: } andre@0: andre@0: static CK_RV andre@0: sftk_updateEncrypted(PLArenaPool *arena, SFTKDBHandle *keydb, andre@0: CK_OBJECT_HANDLE id, SECItem *newKey) andre@0: { andre@0: CK_RV crv = CKR_OK; andre@0: CK_RV crv2; andre@0: CK_ATTRIBUTE *first, *last; andre@0: CK_ATTRIBUTE privAttrs[] = { andre@0: {CKA_VALUE, NULL, 0}, andre@0: {CKA_PRIVATE_EXPONENT, NULL, 0}, andre@0: {CKA_PRIME_1, NULL, 0}, andre@0: {CKA_PRIME_2, NULL, 0}, andre@0: {CKA_EXPONENT_1, NULL, 0}, andre@0: {CKA_EXPONENT_2, NULL, 0}, andre@0: {CKA_COEFFICIENT, NULL, 0} }; andre@0: CK_ULONG privAttrCount = sizeof(privAttrs)/sizeof(CK_ATTRIBUTE); andre@0: int i, count; andre@0: andre@0: /* andre@0: * STEP 1. Read the old attributes in the clear. andre@0: */ andre@0: andre@0: /* Get the attribute sizes. andre@0: * ignore the error code, we will have unknown attributes here */ andre@0: crv2 = sftkdb_GetAttributeValue(keydb, id, privAttrs, privAttrCount); andre@0: andre@0: /* andre@0: * find the valid block of attributes and fill allocate space for andre@0: * their data */ andre@0: first = last = NULL; andre@0: for (i=0; i < privAttrCount; i++) { andre@0: /* find the block of attributes that are appropriate for this andre@0: * objects. There should only be once contiguous block, if not andre@0: * there's an error. andre@0: * andre@0: * find the first and last good entry. andre@0: */ andre@0: if ((privAttrs[i].ulValueLen == -1) || (privAttrs[i].ulValueLen == 0)){ andre@0: if (!first) continue; andre@0: if (!last) { andre@0: /* previous entry was last good entry */ andre@0: last= &privAttrs[i-1]; andre@0: } andre@0: continue; andre@0: } andre@0: if (!first) { andre@0: first = &privAttrs[i]; andre@0: } andre@0: if (last) { andre@0: /* OOPS, we've found another good entry beyond the end of the andre@0: * last good entry, we need to fail here. */ andre@0: crv = CKR_GENERAL_ERROR; andre@0: break; andre@0: } andre@0: privAttrs[i].pValue = PORT_ArenaAlloc(arena,privAttrs[i].ulValueLen); andre@0: if (privAttrs[i].pValue == NULL) { andre@0: crv = CKR_HOST_MEMORY; andre@0: break; andre@0: } andre@0: } andre@0: if (first == NULL) { andre@0: /* no valid entries found, return error based on crv2 */ andre@0: return crv2; andre@0: } andre@0: if (last == NULL) { andre@0: last = &privAttrs[privAttrCount-1]; andre@0: } andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: /* read the attributes */ andre@0: count = (last-first)+1; andre@0: crv = sftkdb_GetAttributeValue(keydb, id, first, count); andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: andre@0: /* andre@0: * STEP 2: read the encrypt the attributes with the new key. andre@0: */ andre@0: for (i=0; i < count; i++) { andre@0: SECItem plainText; andre@0: SECItem *result; andre@0: SECStatus rv; andre@0: andre@0: plainText.data = first[i].pValue; andre@0: plainText.len = first[i].ulValueLen; andre@0: rv = sftkdb_EncryptAttribute(arena, newKey, &plainText, &result); andre@0: if (rv != SECSuccess) { andre@0: return CKR_GENERAL_ERROR; andre@0: } andre@0: first[i].pValue = result->data; andre@0: first[i].ulValueLen = result->len; andre@0: /* clear our sensitive data out */ andre@0: PORT_Memset(plainText.data, 0, plainText.len); andre@0: } andre@0: andre@0: andre@0: /* andre@0: * STEP 3: write the newly encrypted attributes out directly andre@0: */ andre@0: id &= SFTK_OBJ_ID_MASK; andre@0: keydb->newKey = newKey; andre@0: crv = (*keydb->db->sdb_SetAttributeValue)(keydb->db, id, first, count); andre@0: keydb->newKey = NULL; andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: static CK_RV andre@0: sftk_convertAttributes(SFTKDBHandle *handle, andre@0: CK_OBJECT_HANDLE id, SECItem *newKey) andre@0: { andre@0: CK_RV crv = CKR_OK; andre@0: PLArenaPool *arena = NULL; andre@0: andre@0: /* get a new arena to simplify cleanup */ andre@0: arena = PORT_NewArena(1024); andre@0: if (!arena) { andre@0: return CKR_HOST_MEMORY; andre@0: } andre@0: andre@0: /* andre@0: * first handle the MACS andre@0: */ andre@0: crv = sftk_updateMacs(arena, handle, id, newKey); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: andre@0: if (handle->type == SFTK_KEYDB_TYPE) { andre@0: crv = sftk_updateEncrypted(arena, handle, id, newKey); andre@0: if (crv != CKR_OK) { andre@0: goto loser; andre@0: } andre@0: } andre@0: andre@0: /* free up our mess */ andre@0: /* NOTE: at this point we know we've cleared out any unencrypted data */ andre@0: PORT_FreeArena(arena, PR_FALSE); andre@0: return CKR_OK; andre@0: andre@0: loser: andre@0: /* there may be unencrypted data, clear it out down */ andre@0: PORT_FreeArena(arena, PR_TRUE); andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * must be called with the old key active. andre@0: */ andre@0: CK_RV andre@0: sftkdb_convertObjects(SFTKDBHandle *handle, CK_ATTRIBUTE *template, andre@0: CK_ULONG count, SECItem *newKey) andre@0: { andre@0: SDBFind *find = NULL; andre@0: CK_ULONG idCount = SFTK_MAX_IDS; andre@0: CK_OBJECT_HANDLE ids[SFTK_MAX_IDS]; andre@0: CK_RV crv, crv2; andre@0: int i; andre@0: andre@0: crv = sftkdb_FindObjectsInit(handle, template, count, &find); andre@0: andre@0: if (crv != CKR_OK) { andre@0: return crv; andre@0: } andre@0: while ((crv == CKR_OK) && (idCount == SFTK_MAX_IDS)) { andre@0: crv = sftkdb_FindObjects(handle, find, ids, SFTK_MAX_IDS, &idCount); andre@0: for (i=0; (crv == CKR_OK) && (i < idCount); i++) { andre@0: crv = sftk_convertAttributes(handle, ids[i], newKey); andre@0: } andre@0: } andre@0: crv2 = sftkdb_FindObjectsFinal(handle, find); andre@0: if (crv == CKR_OK) crv = crv2; andre@0: andre@0: return crv; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * change the database password. andre@0: */ andre@0: SECStatus andre@0: sftkdb_ChangePassword(SFTKDBHandle *keydb, andre@0: char *oldPin, char *newPin, PRBool *tokenRemoved) andre@0: { andre@0: SECStatus rv = SECSuccess; andre@0: SECItem plainText; andre@0: SECItem newKey; andre@0: SECItem *result = NULL; andre@0: SECItem salt, value; andre@0: SFTKDBHandle *certdb; andre@0: unsigned char saltData[SDB_MAX_META_DATA_LEN]; andre@0: unsigned char valueData[SDB_MAX_META_DATA_LEN]; andre@0: CK_RV crv; andre@0: SDB *db; andre@0: andre@0: if (keydb == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: db = SFTK_GET_SDB(keydb); andre@0: if (db == NULL) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: newKey.data = NULL; andre@0: andre@0: /* make sure we have a valid old pin */ andre@0: crv = (*keydb->db->sdb_Begin)(keydb->db); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: salt.data = saltData; andre@0: salt.len = sizeof(saltData); andre@0: value.data = valueData; andre@0: value.len = sizeof(valueData); andre@0: crv = (*db->sdb_GetMetaData)(db, "password", &salt, &value); andre@0: if (crv == CKR_OK) { andre@0: rv = sftkdb_CheckPassword(keydb, oldPin, tokenRemoved); andre@0: if (rv == SECFailure) { andre@0: goto loser; andre@0: } andre@0: } else { andre@0: salt.len = SHA1_LENGTH; andre@0: RNG_GenerateGlobalRandomBytes(salt.data,salt.len); andre@0: } andre@0: andre@0: rv = sftkdb_passwordToKey(keydb, &salt, newPin, &newKey); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: andre@0: /* andre@0: * convert encrypted entries here. andre@0: */ andre@0: crv = sftkdb_convertObjects(keydb, NULL, 0, &newKey); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: /* fix up certdb macs */ andre@0: certdb = keydb->peerDB; andre@0: if (certdb) { andre@0: CK_ATTRIBUTE objectType = { CKA_CLASS, 0, sizeof(CK_OBJECT_CLASS) }; andre@0: CK_OBJECT_CLASS myClass = CKO_NETSCAPE_TRUST; andre@0: andre@0: objectType.pValue = &myClass; andre@0: crv = sftkdb_convertObjects(certdb, &objectType, 1, &newKey); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: myClass = CKO_PUBLIC_KEY; andre@0: crv = sftkdb_convertObjects(certdb, &objectType, 1, &newKey); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: } andre@0: andre@0: andre@0: plainText.data = (unsigned char *)SFTK_PW_CHECK_STRING; andre@0: plainText.len = SFTK_PW_CHECK_LEN; andre@0: andre@0: rv = sftkdb_EncryptAttribute(NULL, &newKey, &plainText, &result); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: value.data = result->data; andre@0: value.len = result->len; andre@0: crv = (*keydb->db->sdb_PutMetaData)(keydb->db, "password", &salt, &value); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: crv = (*keydb->db->sdb_Commit)(keydb->db); andre@0: if (crv != CKR_OK) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: keydb->newKey = NULL; andre@0: andre@0: sftkdb_switchKeys(keydb, &newKey); andre@0: andre@0: loser: andre@0: if (newKey.data) { andre@0: PORT_ZFree(newKey.data,newKey.len); andre@0: } andre@0: if (result) { andre@0: SECITEM_FreeItem(result, PR_FALSE); andre@0: } andre@0: if (rv != SECSuccess) { andre@0: (*keydb->db->sdb_Abort)(keydb->db); andre@0: } andre@0: andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: * lose our cached password andre@0: */ andre@0: SECStatus andre@0: sftkdb_ClearPassword(SFTKDBHandle *keydb) andre@0: { andre@0: SECItem oldKey; andre@0: oldKey.data = NULL; andre@0: oldKey.len = 0; andre@0: sftkdb_switchKeys(keydb, &oldKey); andre@0: if (oldKey.data) { andre@0: PORT_ZFree(oldKey.data, oldKey.len); andre@0: } andre@0: return SECSuccess; andre@0: } andre@0: andre@0: