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: #include "plarena.h" andre@0: andre@0: #include "seccomon.h" andre@0: #include "secitem.h" andre@0: #include "secport.h" andre@0: #include "hasht.h" andre@0: #include "pkcs11t.h" andre@0: #include "blapi.h" andre@0: #include "hasht.h" andre@0: #include "secasn1.h" andre@0: #include "secder.h" andre@0: #include "lowpbe.h" andre@0: #include "secoid.h" andre@0: #include "alghmac.h" andre@0: #include "softoken.h" andre@0: #include "secerr.h" andre@0: andre@0: SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate) andre@0: andre@0: /* template for PKCS 5 PBE Parameter. This template has been expanded andre@0: * based upon the additions in PKCS 12. This should eventually be moved andre@0: * if RSA updates PKCS 5. andre@0: */ andre@0: static const SEC_ASN1Template NSSPKCS5PBEParameterTemplate[] = andre@0: { andre@0: { SEC_ASN1_SEQUENCE, andre@0: 0, NULL, sizeof(NSSPKCS5PBEParameter) }, andre@0: { SEC_ASN1_OCTET_STRING, andre@0: offsetof(NSSPKCS5PBEParameter, salt) }, andre@0: { SEC_ASN1_INTEGER, andre@0: offsetof(NSSPKCS5PBEParameter, iteration) }, andre@0: { 0 } andre@0: }; andre@0: andre@0: static const SEC_ASN1Template NSSPKCS5PKCS12V2PBEParameterTemplate[] = andre@0: { andre@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSSPKCS5PBEParameter) }, andre@0: { SEC_ASN1_OCTET_STRING, offsetof(NSSPKCS5PBEParameter, salt) }, andre@0: { SEC_ASN1_INTEGER, offsetof(NSSPKCS5PBEParameter, iteration) }, andre@0: { 0 } andre@0: }; andre@0: andre@0: andre@0: /* PKCS5 v2 */ andre@0: andre@0: struct nsspkcs5V2PBEParameterStr { andre@0: SECAlgorithmID keyParams; /* parameters of the key generation */ andre@0: SECAlgorithmID algParams; /* parameters for the encryption or mac op */ andre@0: }; andre@0: andre@0: typedef struct nsspkcs5V2PBEParameterStr nsspkcs5V2PBEParameter; andre@0: #define PBKDF2 andre@0: andre@0: #ifdef PBKDF2 andre@0: static const SEC_ASN1Template NSSPKCS5V2PBES2ParameterTemplate[] = andre@0: { andre@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(nsspkcs5V2PBEParameter) }, andre@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN, andre@0: offsetof(nsspkcs5V2PBEParameter, keyParams), andre@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, andre@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN, andre@0: offsetof(nsspkcs5V2PBEParameter, algParams), andre@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, andre@0: { 0 } andre@0: }; andre@0: andre@0: static const SEC_ASN1Template NSSPKCS5V2PBEParameterTemplate[] = andre@0: { andre@0: { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSSPKCS5PBEParameter) }, andre@0: /* this is really a choice, but since we don't understand any other andre@0: *choice, just inline it. */ andre@0: { SEC_ASN1_OCTET_STRING, offsetof(NSSPKCS5PBEParameter, salt) }, andre@0: { SEC_ASN1_INTEGER, offsetof(NSSPKCS5PBEParameter, iteration) }, andre@0: { SEC_ASN1_INTEGER, offsetof(NSSPKCS5PBEParameter, keyLength) }, andre@0: { SEC_ASN1_INLINE | SEC_ASN1_XTRN, andre@0: offsetof(NSSPKCS5PBEParameter, prfAlg), andre@0: SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, andre@0: { 0 } andre@0: }; andre@0: #endif andre@0: andre@0: SECStatus andre@0: nsspkcs5_HashBuf(const SECHashObject *hashObj, unsigned char *dest, andre@0: unsigned char *src, int len) andre@0: { andre@0: void *ctx; andre@0: unsigned int retLen; andre@0: andre@0: ctx = hashObj->create(); andre@0: if(ctx == NULL) { andre@0: return SECFailure; andre@0: } andre@0: hashObj->begin(ctx); andre@0: hashObj->update(ctx, src, len); andre@0: hashObj->end(ctx, dest, &retLen, hashObj->length); andre@0: hashObj->destroy(ctx, PR_TRUE); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* generate bits using any hash andre@0: */ andre@0: static SECItem * andre@0: nsspkcs5_PBKDF1(const SECHashObject *hashObj, SECItem *salt, SECItem *pwd, andre@0: int iter, PRBool faulty3DES) andre@0: { andre@0: SECItem *hash = NULL, *pre_hash = NULL; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: if((salt == NULL) || (pwd == NULL) || (iter < 0)) { andre@0: return NULL; andre@0: } andre@0: andre@0: hash = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: pre_hash = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: andre@0: if((hash != NULL) && (pre_hash != NULL)) { andre@0: int i, ph_len; andre@0: andre@0: ph_len = hashObj->length; andre@0: if((salt->len + pwd->len) > hashObj->length) { andre@0: ph_len = salt->len + pwd->len; andre@0: } andre@0: andre@0: rv = SECFailure; andre@0: andre@0: /* allocate buffers */ andre@0: hash->len = hashObj->length; andre@0: hash->data = (unsigned char *)PORT_ZAlloc(hash->len); andre@0: pre_hash->data = (unsigned char *)PORT_ZAlloc(ph_len); andre@0: andre@0: /* in pbeSHA1TripleDESCBC there was an allocation error that made andre@0: * it into the caller. We do not want to propagate those errors andre@0: * further, so we are doing it correctly, but reading the old method. andre@0: */ andre@0: if (faulty3DES) { andre@0: pre_hash->len = ph_len; andre@0: } else { andre@0: pre_hash->len = salt->len + pwd->len; andre@0: } andre@0: andre@0: /* preform hash */ andre@0: if ((hash->data != NULL) && (pre_hash->data != NULL)) { andre@0: rv = SECSuccess; andre@0: /* check for 0 length password */ andre@0: if(pwd->len > 0) { andre@0: PORT_Memcpy(pre_hash->data, pwd->data, pwd->len); andre@0: } andre@0: if(salt->len > 0) { andre@0: PORT_Memcpy((pre_hash->data+pwd->len), salt->data, salt->len); andre@0: } andre@0: for(i = 0; ((i < iter) && (rv == SECSuccess)); i++) { andre@0: rv = nsspkcs5_HashBuf(hashObj, hash->data, andre@0: pre_hash->data, pre_hash->len); andre@0: if(rv != SECFailure) { andre@0: pre_hash->len = hashObj->length; andre@0: PORT_Memcpy(pre_hash->data, hash->data, hashObj->length); andre@0: } andre@0: } andre@0: } andre@0: } andre@0: andre@0: if(pre_hash != NULL) { andre@0: SECITEM_FreeItem(pre_hash, PR_TRUE); andre@0: } andre@0: andre@0: if((rv != SECSuccess) && (hash != NULL)) { andre@0: SECITEM_FreeItem(hash, PR_TRUE); andre@0: hash = NULL; andre@0: } andre@0: andre@0: return hash; andre@0: } andre@0: andre@0: /* this bit generation routine is described in PKCS 12 and the proposed andre@0: * extensions to PKCS 5. an initial hash is generated following the andre@0: * instructions laid out in PKCS 5. If the number of bits generated is andre@0: * insufficient, then the method discussed in the proposed extensions to andre@0: * PKCS 5 in PKCS 12 are used. This extension makes use of the HMAC andre@0: * function. And the P_Hash function from the TLS standard. andre@0: */ andre@0: static SECItem * andre@0: nsspkcs5_PFXPBE(const SECHashObject *hashObj, NSSPKCS5PBEParameter *pbe_param, andre@0: SECItem *init_hash, unsigned int bytes_needed) andre@0: { andre@0: SECItem *ret_bits = NULL; andre@0: int hash_size = 0; andre@0: unsigned int i; andre@0: unsigned int hash_iter; andre@0: unsigned int dig_len; andre@0: SECStatus rv = SECFailure; andre@0: unsigned char *state = NULL; andre@0: unsigned int state_len; andre@0: HMACContext *cx = NULL; andre@0: andre@0: hash_size = hashObj->length; andre@0: hash_iter = (bytes_needed + (unsigned int)hash_size - 1) / hash_size; andre@0: andre@0: /* allocate return buffer */ andre@0: ret_bits = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: if(ret_bits == NULL) andre@0: return NULL; andre@0: ret_bits->data = (unsigned char *)PORT_ZAlloc((hash_iter * hash_size) + 1); andre@0: ret_bits->len = (hash_iter * hash_size); andre@0: if(ret_bits->data == NULL) { andre@0: PORT_Free(ret_bits); andre@0: return NULL; andre@0: } andre@0: andre@0: /* allocate intermediate hash buffer. 8 is for the 8 bytes of andre@0: * data which are added based on iteration number andre@0: */ andre@0: andre@0: if ((unsigned int)hash_size > pbe_param->salt.len) { andre@0: state_len = hash_size; andre@0: } else { andre@0: state_len = pbe_param->salt.len; andre@0: } andre@0: state = (unsigned char *)PORT_ZAlloc(state_len); andre@0: if(state == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: if(pbe_param->salt.len > 0) { andre@0: PORT_Memcpy(state, pbe_param->salt.data, pbe_param->salt.len); andre@0: } andre@0: andre@0: cx = HMAC_Create(hashObj, init_hash->data, init_hash->len, PR_TRUE); andre@0: if (cx == NULL) { andre@0: rv = SECFailure; andre@0: goto loser; andre@0: } andre@0: andre@0: for(i = 0; i < hash_iter; i++) { andre@0: andre@0: /* generate output bits */ andre@0: HMAC_Begin(cx); andre@0: HMAC_Update(cx, state, state_len); andre@0: HMAC_Update(cx, pbe_param->salt.data, pbe_param->salt.len); andre@0: rv = HMAC_Finish(cx, ret_bits->data + (i * hash_size), andre@0: &dig_len, hash_size); andre@0: if (rv != SECSuccess) andre@0: goto loser; andre@0: PORT_Assert((unsigned int)hash_size == dig_len); andre@0: andre@0: /* generate new state */ andre@0: HMAC_Begin(cx); andre@0: HMAC_Update(cx, state, state_len); andre@0: rv = HMAC_Finish(cx, state, &state_len, state_len); andre@0: if (rv != SECSuccess) andre@0: goto loser; andre@0: PORT_Assert(state_len == dig_len); andre@0: } andre@0: andre@0: loser: andre@0: if (state != NULL) andre@0: PORT_ZFree(state, state_len); andre@0: HMAC_Destroy(cx, PR_TRUE); andre@0: andre@0: if(rv != SECSuccess) { andre@0: SECITEM_ZfreeItem(ret_bits, PR_TRUE); andre@0: ret_bits = NULL; andre@0: } andre@0: andre@0: return ret_bits; andre@0: } andre@0: andre@0: /* generate bits for the key and iv determination. if enough bits andre@0: * are not generated using PKCS 5, then we need to generate more bits andre@0: * based on the extension proposed in PKCS 12 andre@0: */ andre@0: static SECItem * andre@0: nsspkcs5_PBKDF1Extended(const SECHashObject *hashObj, andre@0: NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem, PRBool faulty3DES) andre@0: { andre@0: SECItem * hash = NULL; andre@0: SECItem * newHash = NULL; andre@0: int bytes_needed; andre@0: int bytes_available; andre@0: andre@0: bytes_needed = pbe_param->ivLen + pbe_param->keyLen; andre@0: bytes_available = hashObj->length; andre@0: andre@0: hash = nsspkcs5_PBKDF1(hashObj, &pbe_param->salt, pwitem, andre@0: pbe_param->iter, faulty3DES); andre@0: andre@0: if(hash == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: if(bytes_needed <= bytes_available) { andre@0: return hash; andre@0: } andre@0: andre@0: newHash = nsspkcs5_PFXPBE(hashObj, pbe_param, hash, bytes_needed); andre@0: if (hash != newHash) andre@0: SECITEM_FreeItem(hash, PR_TRUE); andre@0: return newHash; andre@0: } andre@0: andre@0: #ifdef PBKDF2 andre@0: andre@0: /* andre@0: * PBDKDF2 is PKCS #5 v2.0 it's currently not used by NSS andre@0: */ andre@0: static void andre@0: do_xor(unsigned char *dest, unsigned char *src, int len) andre@0: { andre@0: /* use byt xor, not all platforms are happy about inaligned andre@0: * integer fetches */ andre@0: while (len--) { andre@0: *dest = *dest ^ *src; andre@0: dest++; andre@0: src++; andre@0: } andre@0: } andre@0: andre@0: static SECStatus andre@0: nsspkcs5_PBKFD2_F(const SECHashObject *hashobj, SECItem *pwitem, SECItem *salt, andre@0: int iterations, unsigned int i, unsigned char *T) andre@0: { andre@0: int j; andre@0: HMACContext *cx = NULL; andre@0: unsigned int hLen = hashobj->length; andre@0: SECStatus rv = SECFailure; andre@0: unsigned char *last = NULL; andre@0: unsigned int lastLength = salt->len + 4; andre@0: unsigned int lastBufLength; andre@0: andre@0: cx=HMAC_Create(hashobj,pwitem->data,pwitem->len,PR_FALSE); andre@0: if (cx == NULL) { andre@0: goto loser; andre@0: } andre@0: PORT_Memset(T,0,hLen); andre@0: lastBufLength = PR_MAX(lastLength, hLen); andre@0: last = PORT_Alloc(lastBufLength); andre@0: if (last == NULL) { andre@0: goto loser; andre@0: } andre@0: PORT_Memcpy(last,salt->data,salt->len); andre@0: last[salt->len ] = (i >> 24) & 0xff; andre@0: last[salt->len+1] = (i >> 16) & 0xff; andre@0: last[salt->len+2] = (i >> 8) & 0xff; andre@0: last[salt->len+3] = i & 0xff; andre@0: andre@0: /* NOTE: we need at least one iteration to return success! */ andre@0: for (j=0; j < iterations; j++) { andre@0: HMAC_Begin(cx); andre@0: HMAC_Update(cx,last,lastLength); andre@0: rv =HMAC_Finish(cx,last,&lastLength,hLen); andre@0: if (rv !=SECSuccess) { andre@0: break; andre@0: } andre@0: do_xor(T,last,hLen); andre@0: } andre@0: loser: andre@0: if (cx) { andre@0: HMAC_Destroy(cx, PR_TRUE); andre@0: } andre@0: if (last) { andre@0: PORT_ZFree(last,lastBufLength); andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: static SECItem * andre@0: nsspkcs5_PBKDF2(const SECHashObject *hashobj, NSSPKCS5PBEParameter *pbe_param, andre@0: SECItem *pwitem) andre@0: { andre@0: int iterations = pbe_param->iter; andre@0: int bytesNeeded = pbe_param->keyLen; andre@0: unsigned int dkLen = bytesNeeded; andre@0: unsigned int hLen = hashobj->length; andre@0: unsigned int nblocks = (dkLen+hLen-1) / hLen; andre@0: unsigned int i; andre@0: unsigned char *rp; andre@0: unsigned char *T = NULL; andre@0: SECItem *result = NULL; andre@0: SECItem *salt = &pbe_param->salt; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: result = SECITEM_AllocItem(NULL,NULL,nblocks*hLen); andre@0: if (result == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: T = PORT_Alloc(hLen); andre@0: if (T == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: for (i=1,rp=result->data; i <= nblocks ; i++, rp +=hLen) { andre@0: rv = nsspkcs5_PBKFD2_F(hashobj,pwitem,salt,iterations,i,T); andre@0: if (rv != SECSuccess) { andre@0: break; andre@0: } andre@0: PORT_Memcpy(rp,T,hLen); andre@0: } andre@0: andre@0: loser: andre@0: if (T) { andre@0: PORT_ZFree(T,hLen); andre@0: } andre@0: if (rv != SECSuccess) { andre@0: SECITEM_FreeItem(result,PR_TRUE); andre@0: result = NULL; andre@0: } else { andre@0: result->len = dkLen; andre@0: } andre@0: andre@0: return result; andre@0: } andre@0: #endif andre@0: andre@0: #define HMAC_BUFFER 64 andre@0: #define NSSPBE_ROUNDUP(x,y) ((((x)+((y)-1))/(y))*(y)) andre@0: #define NSSPBE_MIN(x,y) ((x) < (y) ? (x) : (y)) andre@0: /* andre@0: * This is the extended PBE function defined by the final PKCS #12 spec. andre@0: */ andre@0: static SECItem * andre@0: nsspkcs5_PKCS12PBE(const SECHashObject *hashObject, andre@0: NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem, andre@0: PBEBitGenID bitGenPurpose, unsigned int bytesNeeded) andre@0: { andre@0: PLArenaPool *arena = NULL; andre@0: unsigned int SLen,PLen; andre@0: unsigned int hashLength = hashObject->length; andre@0: unsigned char *S, *P; andre@0: SECItem *A = NULL, B, D, I; andre@0: SECItem *salt = &pbe_param->salt; andre@0: unsigned int c,i = 0; andre@0: unsigned int hashLen; andre@0: int iter; andre@0: unsigned char *iterBuf; andre@0: void *hash = NULL; andre@0: andre@0: arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); andre@0: if(!arena) { andre@0: return NULL; andre@0: } andre@0: andre@0: /* how many hash object lengths are needed */ andre@0: c = (bytesNeeded + (hashLength-1))/hashLength; andre@0: andre@0: /* initialize our buffers */ andre@0: D.len = HMAC_BUFFER; andre@0: /* B and D are the same length, use one alloc go get both */ andre@0: D.data = (unsigned char*)PORT_ArenaZAlloc(arena, D.len*2); andre@0: B.len = D.len; andre@0: B.data = D.data + D.len; andre@0: andre@0: /* if all goes well, A will be returned, so don't use our temp arena */ andre@0: A = SECITEM_AllocItem(NULL,NULL,c*hashLength); andre@0: if (A == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: SLen = NSSPBE_ROUNDUP(salt->len,HMAC_BUFFER); andre@0: PLen = NSSPBE_ROUNDUP(pwitem->len,HMAC_BUFFER); andre@0: I.len = SLen+PLen; andre@0: I.data = (unsigned char*)PORT_ArenaZAlloc(arena, I.len); andre@0: if (I.data == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* S & P are only used to initialize I */ andre@0: S = I.data; andre@0: P = S + SLen; andre@0: andre@0: PORT_Memset(D.data, (char)bitGenPurpose, D.len); andre@0: if (SLen) { andre@0: for (i=0; i < SLen; i += salt->len) { andre@0: PORT_Memcpy(S+i, salt->data, NSSPBE_MIN(SLen-i,salt->len)); andre@0: } andre@0: } andre@0: if (PLen) { andre@0: for (i=0; i < PLen; i += pwitem->len) { andre@0: PORT_Memcpy(P+i, pwitem->data, NSSPBE_MIN(PLen-i,pwitem->len)); andre@0: } andre@0: } andre@0: andre@0: iterBuf = (unsigned char*)PORT_ArenaZAlloc(arena,hashLength); andre@0: if (iterBuf == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: hash = hashObject->create(); andre@0: if(!hash) { andre@0: goto loser; andre@0: } andre@0: /* calculate the PBE now */ andre@0: for(i = 0; i < c; i++) { andre@0: int Bidx; /* must be signed or the for loop won't terminate */ andre@0: unsigned int k, j; andre@0: unsigned char *Ai = A->data+i*hashLength; andre@0: andre@0: andre@0: for(iter = 0; iter < pbe_param->iter; iter++) { andre@0: hashObject->begin(hash); andre@0: andre@0: if (iter) { andre@0: hashObject->update(hash, iterBuf, hashLen); andre@0: } else { andre@0: hashObject->update(hash, D.data, D.len); andre@0: hashObject->update(hash, I.data, I.len); andre@0: } andre@0: andre@0: hashObject->end(hash, iterBuf, &hashLen, hashObject->length); andre@0: if(hashLen != hashObject->length) { andre@0: break; andre@0: } andre@0: } andre@0: andre@0: PORT_Memcpy(Ai, iterBuf, hashLength); andre@0: for (Bidx = 0; Bidx < B.len; Bidx += hashLength) { andre@0: PORT_Memcpy(B.data+Bidx,iterBuf,NSSPBE_MIN(B.len-Bidx,hashLength)); andre@0: } andre@0: andre@0: k = I.len/B.len; andre@0: for(j = 0; j < k; j++) { andre@0: unsigned int q, carryBit; andre@0: unsigned char *Ij = I.data + j*B.len; andre@0: andre@0: /* (Ij = Ij+B+1) */ andre@0: for (Bidx = (B.len-1), q=1, carryBit=0; Bidx >= 0; Bidx--,q=0) { andre@0: q += (unsigned int)Ij[Bidx]; andre@0: q += (unsigned int)B.data[Bidx]; andre@0: q += carryBit; andre@0: andre@0: carryBit = (q > 0xff); andre@0: Ij[Bidx] = (unsigned char)(q & 0xff); andre@0: } andre@0: } andre@0: } andre@0: loser: andre@0: if (hash) { andre@0: hashObject->destroy(hash, PR_TRUE); andre@0: } andre@0: if(arena) { andre@0: PORT_FreeArena(arena, PR_TRUE); andre@0: } andre@0: andre@0: if (A) { andre@0: /* if i != c, then we didn't complete the loop above and must of failed andre@0: * somwhere along the way */ andre@0: if (i != c) { andre@0: SECITEM_ZfreeItem(A,PR_TRUE); andre@0: A = NULL; andre@0: } else { andre@0: A->len = bytesNeeded; andre@0: } andre@0: } andre@0: andre@0: return A; andre@0: } andre@0: andre@0: /* andre@0: * generate key as per PKCS 5 andre@0: */ andre@0: SECItem * andre@0: nsspkcs5_ComputeKeyAndIV(NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem, andre@0: SECItem *iv, PRBool faulty3DES) andre@0: { andre@0: SECItem *hash = NULL, *key = NULL; andre@0: const SECHashObject *hashObj; andre@0: PRBool getIV = PR_FALSE; andre@0: andre@0: if((pbe_param == NULL) || (pwitem == NULL)) { andre@0: return NULL; andre@0: } andre@0: andre@0: key = SECITEM_AllocItem(NULL,NULL,pbe_param->keyLen); andre@0: if (key == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: if (iv && (pbe_param->ivLen) && (iv->data == NULL)) { andre@0: getIV = PR_TRUE; andre@0: iv->data = (unsigned char *)PORT_Alloc(pbe_param->ivLen); andre@0: if (iv->data == NULL) { andre@0: goto loser; andre@0: } andre@0: iv->len = pbe_param->ivLen; andre@0: } andre@0: andre@0: hashObj = HASH_GetRawHashObject(pbe_param->hashType); andre@0: switch (pbe_param->pbeType) { andre@0: case NSSPKCS5_PBKDF1: andre@0: hash = nsspkcs5_PBKDF1Extended(hashObj,pbe_param,pwitem,faulty3DES); andre@0: if (hash == NULL) { andre@0: goto loser; andre@0: } andre@0: PORT_Assert(hash->len >= key->len+(getIV ? iv->len : 0)); andre@0: if (getIV) { andre@0: PORT_Memcpy(iv->data, hash->data+(hash->len - iv->len),iv->len); andre@0: } andre@0: andre@0: break; andre@0: #ifdef PBKDF2 andre@0: case NSSPKCS5_PBKDF2: andre@0: hash = nsspkcs5_PBKDF2(hashObj,pbe_param,pwitem); andre@0: if (getIV) { andre@0: PORT_Memcpy(iv->data, pbe_param->ivData, iv->len); andre@0: } andre@0: break; andre@0: #endif andre@0: case NSSPKCS5_PKCS12_V2: andre@0: if (getIV) { andre@0: hash = nsspkcs5_PKCS12PBE(hashObj,pbe_param,pwitem, andre@0: pbeBitGenCipherIV,iv->len); andre@0: if (hash == NULL) { andre@0: goto loser; andre@0: } andre@0: PORT_Memcpy(iv->data,hash->data,iv->len); andre@0: SECITEM_ZfreeItem(hash,PR_TRUE); andre@0: hash = NULL; andre@0: } andre@0: hash = nsspkcs5_PKCS12PBE(hashObj,pbe_param,pwitem, andre@0: pbe_param->keyID,key->len); andre@0: default: andre@0: break; andre@0: } andre@0: andre@0: if (hash == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: if (pbe_param->is2KeyDES) { andre@0: PORT_Memcpy(key->data, hash->data, (key->len * 2) / 3); andre@0: PORT_Memcpy(&(key->data[(key->len * 2) / 3]), key->data, andre@0: key->len / 3); andre@0: } else { andre@0: PORT_Memcpy(key->data, hash->data, key->len); andre@0: } andre@0: andre@0: SECITEM_ZfreeItem(hash, PR_TRUE); andre@0: return key; andre@0: andre@0: loser: andre@0: if (getIV && iv->data) { andre@0: PORT_ZFree(iv->data,iv->len); andre@0: iv->data = NULL; andre@0: } andre@0: andre@0: SECITEM_ZfreeItem(key, PR_TRUE); andre@0: return NULL; andre@0: } andre@0: andre@0: static SECStatus andre@0: nsspkcs5_FillInParam(SECOidTag algorithm, NSSPKCS5PBEParameter *pbe_param) andre@0: { andre@0: PRBool skipType = PR_FALSE; andre@0: andre@0: pbe_param->keyLen = 5; andre@0: pbe_param->ivLen = 8; andre@0: pbe_param->hashType = HASH_AlgSHA1; andre@0: pbe_param->pbeType = NSSPKCS5_PBKDF1; andre@0: pbe_param->encAlg = SEC_OID_RC2_CBC; andre@0: pbe_param->is2KeyDES = PR_FALSE; andre@0: switch(algorithm) { andre@0: /* DES3 Algorithms */ andre@0: case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC: andre@0: pbe_param->is2KeyDES = PR_TRUE; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC: andre@0: pbe_param->pbeType = NSSPKCS5_PKCS12_V2; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_TRIPLE_DES_CBC: andre@0: pbe_param->keyLen = 24; andre@0: pbe_param->encAlg = SEC_OID_DES_EDE3_CBC; andre@0: break; andre@0: andre@0: /* DES Algorithms */ andre@0: case SEC_OID_PKCS5_PBE_WITH_MD2_AND_DES_CBC: andre@0: pbe_param->hashType = HASH_AlgMD2; andre@0: goto finish_des; andre@0: case SEC_OID_PKCS5_PBE_WITH_MD5_AND_DES_CBC: andre@0: pbe_param->hashType = HASH_AlgMD5; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS5_PBE_WITH_SHA1_AND_DES_CBC: andre@0: finish_des: andre@0: pbe_param->keyLen = 8; andre@0: pbe_param->encAlg = SEC_OID_DES_CBC; andre@0: break; andre@0: andre@0: /* RC2 Algorithms */ andre@0: case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC: andre@0: pbe_param->keyLen = 16; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC: andre@0: pbe_param->pbeType = NSSPKCS5_PKCS12_V2; andre@0: break; andre@0: case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC: andre@0: pbe_param->keyLen = 16; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC: andre@0: break; andre@0: andre@0: /* RC4 algorithms */ andre@0: case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_128_BIT_RC4: andre@0: skipType = PR_TRUE; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4: andre@0: pbe_param->keyLen = 16; andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4: andre@0: if (!skipType) { andre@0: pbe_param->pbeType = NSSPKCS5_PKCS12_V2; andre@0: } andre@0: /* fall through */ andre@0: case SEC_OID_PKCS12_PBE_WITH_SHA1_AND_40_BIT_RC4: andre@0: pbe_param->ivLen = 0; andre@0: pbe_param->encAlg = SEC_OID_RC4; andre@0: break; andre@0: andre@0: #ifdef PBKDF2 andre@0: case SEC_OID_PKCS5_PBKDF2: andre@0: case SEC_OID_PKCS5_PBES2: andre@0: case SEC_OID_PKCS5_PBMAC1: andre@0: /* everything else will be filled in by the template */ andre@0: pbe_param->ivLen = 0; andre@0: pbe_param->pbeType = NSSPKCS5_PBKDF2; andre@0: pbe_param->encAlg = SEC_OID_PKCS5_PBKDF2; andre@0: pbe_param->keyLen = 0; /* needs to be set by caller after return */ andre@0: break; andre@0: #endif andre@0: andre@0: default: andre@0: return SECFailure; andre@0: } andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* decode the algid and generate a PKCS 5 parameter from it andre@0: */ andre@0: NSSPKCS5PBEParameter * andre@0: nsspkcs5_NewParam(SECOidTag alg, SECItem *salt, int iterator) andre@0: { andre@0: PLArenaPool *arena = NULL; andre@0: NSSPKCS5PBEParameter *pbe_param = NULL; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: arena = PORT_NewArena(SEC_ASN1_DEFAULT_ARENA_SIZE); andre@0: if (arena == NULL) andre@0: return NULL; andre@0: andre@0: /* allocate memory for the parameter */ andre@0: pbe_param = (NSSPKCS5PBEParameter *)PORT_ArenaZAlloc(arena, andre@0: sizeof(NSSPKCS5PBEParameter)); andre@0: andre@0: if (pbe_param == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: pbe_param->poolp = arena; andre@0: andre@0: rv = nsspkcs5_FillInParam(alg, pbe_param); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: pbe_param->iter = iterator; andre@0: if (salt) { andre@0: rv = SECITEM_CopyItem(arena,&pbe_param->salt,salt); andre@0: } andre@0: andre@0: /* default key gen */ andre@0: pbe_param->keyID = pbeBitGenCipherKey; andre@0: andre@0: loser: andre@0: if (rv != SECSuccess) { andre@0: PORT_FreeArena(arena, PR_TRUE); andre@0: pbe_param = NULL; andre@0: } andre@0: andre@0: return pbe_param; andre@0: } andre@0: andre@0: /* andre@0: * find the hash type needed to implement a specific HMAC. andre@0: * OID definitions are from pkcs 5 v2.0 and 2.1 andre@0: */ andre@0: HASH_HashType andre@0: HASH_FromHMACOid(SECOidTag hmac) andre@0: { andre@0: switch (hmac) { andre@0: case SEC_OID_HMAC_SHA1: andre@0: return HASH_AlgSHA1; andre@0: case SEC_OID_HMAC_SHA256: andre@0: return HASH_AlgSHA256; andre@0: case SEC_OID_HMAC_SHA384: andre@0: return HASH_AlgSHA384; andre@0: case SEC_OID_HMAC_SHA512: andre@0: return HASH_AlgSHA512; andre@0: case SEC_OID_HMAC_SHA224: andre@0: default: andre@0: break; andre@0: } andre@0: return HASH_AlgNULL; andre@0: } andre@0: andre@0: /* decode the algid and generate a PKCS 5 parameter from it andre@0: */ andre@0: NSSPKCS5PBEParameter * andre@0: nsspkcs5_AlgidToParam(SECAlgorithmID *algid) andre@0: { andre@0: NSSPKCS5PBEParameter *pbe_param = NULL; andre@0: nsspkcs5V2PBEParameter pbev2_param; andre@0: SECOidTag algorithm; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: if (algid == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: algorithm = SECOID_GetAlgorithmTag(algid); andre@0: if (algorithm == SEC_OID_UNKNOWN) { andre@0: goto loser; andre@0: } andre@0: andre@0: pbe_param = nsspkcs5_NewParam(algorithm, NULL, 1); andre@0: if (pbe_param == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: /* decode parameter */ andre@0: rv = SECFailure; andre@0: switch (pbe_param->pbeType) { andre@0: case NSSPKCS5_PBKDF1: andre@0: rv = SEC_ASN1DecodeItem(pbe_param->poolp, pbe_param, andre@0: NSSPKCS5PBEParameterTemplate, &algid->parameters); andre@0: break; andre@0: case NSSPKCS5_PKCS12_V2: andre@0: rv = SEC_ASN1DecodeItem(pbe_param->poolp, pbe_param, andre@0: NSSPKCS5PKCS12V2PBEParameterTemplate, &algid->parameters); andre@0: break; andre@0: #ifdef PBKDF2 andre@0: case NSSPKCS5_PBKDF2: andre@0: PORT_Memset(&pbev2_param,0, sizeof(pbev2_param)); andre@0: /* just the PBE */ andre@0: if (algorithm == SEC_OID_PKCS5_PBKDF2) { andre@0: rv = SEC_ASN1DecodeItem(pbe_param->poolp, pbe_param, andre@0: NSSPKCS5V2PBEParameterTemplate, &algid->parameters); andre@0: } else { andre@0: /* PBE data an others */ andre@0: rv = SEC_ASN1DecodeItem(pbe_param->poolp, &pbev2_param, andre@0: NSSPKCS5V2PBES2ParameterTemplate, &algid->parameters); andre@0: if (rv != SECSuccess) { andre@0: break; andre@0: } andre@0: pbe_param->encAlg = SECOID_GetAlgorithmTag(&pbev2_param.algParams); andre@0: rv = SEC_ASN1DecodeItem(pbe_param->poolp, pbe_param, andre@0: NSSPKCS5V2PBEParameterTemplate, andre@0: &pbev2_param.keyParams.parameters); andre@0: if (rv != SECSuccess) { andre@0: break; andre@0: } andre@0: pbe_param->keyLen = DER_GetInteger(&pbe_param->keyLength); andre@0: } andre@0: /* we we are encrypting, save any iv's */ andre@0: if (algorithm == SEC_OID_PKCS5_PBES2) { andre@0: pbe_param->ivLen = pbev2_param.algParams.parameters.len; andre@0: pbe_param->ivData = pbev2_param.algParams.parameters.data; andre@0: } andre@0: pbe_param->hashType = andre@0: HASH_FromHMACOid(SECOID_GetAlgorithmTag(&pbe_param->prfAlg)); andre@0: if (pbe_param->hashType == HASH_AlgNULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: rv = SECFailure; andre@0: } andre@0: break; andre@0: #endif andre@0: } andre@0: andre@0: loser: andre@0: if (rv == SECSuccess) { andre@0: pbe_param->iter = DER_GetInteger(&pbe_param->iteration); andre@0: } else { andre@0: nsspkcs5_DestroyPBEParameter(pbe_param); andre@0: pbe_param = NULL; andre@0: } andre@0: andre@0: return pbe_param; andre@0: } andre@0: andre@0: /* destroy a pbe parameter. it assumes that the parameter was andre@0: * generated using the appropriate create function and therefor andre@0: * contains an arena pool. andre@0: */ andre@0: void andre@0: nsspkcs5_DestroyPBEParameter(NSSPKCS5PBEParameter *pbe_param) andre@0: { andre@0: if (pbe_param != NULL) { andre@0: PORT_FreeArena(pbe_param->poolp, PR_FALSE); andre@0: } andre@0: } andre@0: andre@0: andre@0: /* crypto routines */ andre@0: /* perform DES encryption and decryption. these routines are called andre@0: * by nsspkcs5_CipherData. In the case of an error, NULL is returned. andre@0: */ andre@0: static SECItem * andre@0: sec_pkcs5_des(SECItem *key, SECItem *iv, SECItem *src, PRBool triple_des, andre@0: PRBool encrypt) andre@0: { andre@0: SECItem *dest; andre@0: SECItem *dup_src; andre@0: SECStatus rv = SECFailure; andre@0: int pad; andre@0: andre@0: if((src == NULL) || (key == NULL) || (iv == NULL)) andre@0: return NULL; andre@0: andre@0: dup_src = SECITEM_DupItem(src); andre@0: if(dup_src == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: if(encrypt != PR_FALSE) { andre@0: void *dummy; andre@0: andre@0: dummy = CBC_PadBuffer(NULL, dup_src->data, andre@0: dup_src->len, &dup_src->len, 8 /* DES_BLOCK_SIZE */); andre@0: if(dummy == NULL) { andre@0: SECITEM_FreeItem(dup_src, PR_TRUE); andre@0: return NULL; andre@0: } andre@0: dup_src->data = (unsigned char*)dummy; andre@0: } andre@0: andre@0: dest = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: if(dest != NULL) { andre@0: /* allocate with over flow */ andre@0: dest->data = (unsigned char *)PORT_ZAlloc(dup_src->len + 64); andre@0: if(dest->data != NULL) { andre@0: DESContext *ctxt; andre@0: ctxt = DES_CreateContext(key->data, iv->data, andre@0: (triple_des ? NSS_DES_EDE3_CBC : NSS_DES_CBC), andre@0: encrypt); andre@0: andre@0: if(ctxt != NULL) { andre@0: rv = (encrypt ? DES_Encrypt : DES_Decrypt)( andre@0: ctxt, dest->data, &dest->len, andre@0: dup_src->len + 64, dup_src->data, dup_src->len); andre@0: andre@0: /* remove padding -- assumes 64 bit blocks */ andre@0: if((encrypt == PR_FALSE) && (rv == SECSuccess)) { andre@0: pad = dest->data[dest->len-1]; andre@0: if((pad > 0) && (pad <= 8)) { andre@0: if(dest->data[dest->len-pad] != pad) { andre@0: rv = SECFailure; andre@0: PORT_SetError(SEC_ERROR_BAD_PASSWORD); andre@0: } else { andre@0: dest->len -= pad; andre@0: } andre@0: } else { andre@0: rv = SECFailure; andre@0: PORT_SetError(SEC_ERROR_BAD_PASSWORD); andre@0: } andre@0: } andre@0: DES_DestroyContext(ctxt, PR_TRUE); andre@0: } andre@0: } andre@0: } andre@0: andre@0: if(rv == SECFailure) { andre@0: if(dest != NULL) { andre@0: SECITEM_FreeItem(dest, PR_TRUE); andre@0: } andre@0: dest = NULL; andre@0: } andre@0: andre@0: if(dup_src != NULL) { andre@0: SECITEM_FreeItem(dup_src, PR_TRUE); andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: /* perform aes encryption/decryption if an error occurs, NULL is returned andre@0: */ andre@0: static SECItem * andre@0: sec_pkcs5_aes(SECItem *key, SECItem *iv, SECItem *src, PRBool triple_des, andre@0: PRBool encrypt) andre@0: { andre@0: SECItem *dest; andre@0: SECItem *dup_src; andre@0: SECStatus rv = SECFailure; andre@0: int pad; andre@0: andre@0: if((src == NULL) || (key == NULL) || (iv == NULL)) andre@0: return NULL; andre@0: andre@0: dup_src = SECITEM_DupItem(src); andre@0: if(dup_src == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: if(encrypt != PR_FALSE) { andre@0: void *dummy; andre@0: andre@0: dummy = CBC_PadBuffer(NULL, dup_src->data, andre@0: dup_src->len, &dup_src->len,AES_BLOCK_SIZE); andre@0: if(dummy == NULL) { andre@0: SECITEM_FreeItem(dup_src, PR_TRUE); andre@0: return NULL; andre@0: } andre@0: dup_src->data = (unsigned char*)dummy; andre@0: } andre@0: andre@0: dest = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: if(dest != NULL) { andre@0: /* allocate with over flow */ andre@0: dest->data = (unsigned char *)PORT_ZAlloc(dup_src->len + 64); andre@0: if(dest->data != NULL) { andre@0: AESContext *ctxt; andre@0: ctxt = AES_CreateContext(key->data, iv->data, andre@0: NSS_AES_CBC, encrypt, key->len, 16); andre@0: andre@0: if(ctxt != NULL) { andre@0: rv = (encrypt ? AES_Encrypt : AES_Decrypt)( andre@0: ctxt, dest->data, &dest->len, andre@0: dup_src->len + 64, dup_src->data, dup_src->len); andre@0: andre@0: /* remove padding -- assumes 64 bit blocks */ andre@0: if((encrypt == PR_FALSE) && (rv == SECSuccess)) { andre@0: pad = dest->data[dest->len-1]; andre@0: if((pad > 0) && (pad <= 16)) { andre@0: if(dest->data[dest->len-pad] != pad) { andre@0: rv = SECFailure; andre@0: PORT_SetError(SEC_ERROR_BAD_PASSWORD); andre@0: } else { andre@0: dest->len -= pad; andre@0: } andre@0: } else { andre@0: rv = SECFailure; andre@0: PORT_SetError(SEC_ERROR_BAD_PASSWORD); andre@0: } andre@0: } andre@0: AES_DestroyContext(ctxt, PR_TRUE); andre@0: } andre@0: } andre@0: } andre@0: andre@0: if(rv == SECFailure) { andre@0: if(dest != NULL) { andre@0: SECITEM_FreeItem(dest, PR_TRUE); andre@0: } andre@0: dest = NULL; andre@0: } andre@0: andre@0: if(dup_src != NULL) { andre@0: SECITEM_FreeItem(dup_src, PR_TRUE); andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: /* perform rc2 encryption/decryption if an error occurs, NULL is returned andre@0: */ andre@0: static SECItem * andre@0: sec_pkcs5_rc2(SECItem *key, SECItem *iv, SECItem *src, PRBool dummy, andre@0: PRBool encrypt) andre@0: { andre@0: SECItem *dest; andre@0: SECItem *dup_src; andre@0: SECStatus rv = SECFailure; andre@0: int pad; andre@0: andre@0: if((src == NULL) || (key == NULL) || (iv == NULL)) { andre@0: return NULL; andre@0: } andre@0: andre@0: dup_src = SECITEM_DupItem(src); andre@0: if(dup_src == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: if(encrypt != PR_FALSE) { andre@0: void *dummy; andre@0: andre@0: dummy = CBC_PadBuffer(NULL, dup_src->data, andre@0: dup_src->len, &dup_src->len, 8 /* RC2_BLOCK_SIZE */); andre@0: if(dummy == NULL) { andre@0: SECITEM_FreeItem(dup_src, PR_TRUE); andre@0: return NULL; andre@0: } andre@0: dup_src->data = (unsigned char*)dummy; andre@0: } andre@0: andre@0: dest = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: if(dest != NULL) { andre@0: dest->data = (unsigned char *)PORT_ZAlloc(dup_src->len + 64); andre@0: if(dest->data != NULL) { andre@0: RC2Context *ctxt; andre@0: andre@0: ctxt = RC2_CreateContext(key->data, key->len, iv->data, andre@0: NSS_RC2_CBC, key->len); andre@0: andre@0: if(ctxt != NULL) { andre@0: rv = (encrypt ? RC2_Encrypt: RC2_Decrypt)( andre@0: ctxt, dest->data, &dest->len, andre@0: dup_src->len + 64, dup_src->data, dup_src->len); andre@0: andre@0: /* assumes 8 byte blocks -- remove padding */ andre@0: if((rv == SECSuccess) && (encrypt != PR_TRUE)) { andre@0: pad = dest->data[dest->len-1]; andre@0: if((pad > 0) && (pad <= 8)) { andre@0: if(dest->data[dest->len-pad] != pad) { andre@0: PORT_SetError(SEC_ERROR_BAD_PASSWORD); andre@0: rv = SECFailure; andre@0: } else { andre@0: dest->len -= pad; andre@0: } andre@0: } else { andre@0: PORT_SetError(SEC_ERROR_BAD_PASSWORD); andre@0: rv = SECFailure; andre@0: } andre@0: } andre@0: andre@0: } andre@0: } andre@0: } andre@0: andre@0: if((rv != SECSuccess) && (dest != NULL)) { andre@0: SECITEM_FreeItem(dest, PR_TRUE); andre@0: dest = NULL; andre@0: } andre@0: andre@0: if(dup_src != NULL) { andre@0: SECITEM_FreeItem(dup_src, PR_TRUE); andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: /* perform rc4 encryption and decryption */ andre@0: static SECItem * andre@0: sec_pkcs5_rc4(SECItem *key, SECItem *iv, SECItem *src, PRBool dummy_op, andre@0: PRBool encrypt) andre@0: { andre@0: SECItem *dest; andre@0: SECStatus rv = SECFailure; andre@0: andre@0: if((src == NULL) || (key == NULL) || (iv == NULL)) { andre@0: return NULL; andre@0: } andre@0: andre@0: dest = (SECItem *)PORT_ZAlloc(sizeof(SECItem)); andre@0: if(dest != NULL) { andre@0: dest->data = (unsigned char *)PORT_ZAlloc(sizeof(unsigned char) * andre@0: (src->len + 64)); andre@0: if(dest->data != NULL) { andre@0: RC4Context *ctxt; andre@0: andre@0: ctxt = RC4_CreateContext(key->data, key->len); andre@0: if(ctxt) { andre@0: rv = (encrypt ? RC4_Encrypt : RC4_Decrypt)( andre@0: ctxt, dest->data, &dest->len, andre@0: src->len + 64, src->data, src->len); andre@0: RC4_DestroyContext(ctxt, PR_TRUE); andre@0: } andre@0: } andre@0: } andre@0: andre@0: if((rv != SECSuccess) && (dest)) { andre@0: SECITEM_FreeItem(dest, PR_TRUE); andre@0: dest = NULL; andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: /* function pointer template for crypto functions */ andre@0: typedef SECItem *(* pkcs5_crypto_func)(SECItem *key, SECItem *iv, andre@0: SECItem *src, PRBool op1, PRBool op2); andre@0: andre@0: /* performs the cipher operation on the src and returns the result. andre@0: * if an error occurs, NULL is returned. andre@0: * andre@0: * a null length password is allowed. this corresponds to encrypting andre@0: * the data with ust the salt. andre@0: */ andre@0: /* change this to use PKCS 11? */ andre@0: SECItem * andre@0: nsspkcs5_CipherData(NSSPKCS5PBEParameter *pbe_param, SECItem *pwitem, andre@0: SECItem *src, PRBool encrypt, PRBool *update) andre@0: { andre@0: SECItem *key = NULL, iv; andre@0: SECItem *dest = NULL; andre@0: PRBool tripleDES = PR_TRUE; andre@0: pkcs5_crypto_func cryptof; andre@0: andre@0: iv.data = NULL; andre@0: andre@0: if (update) { andre@0: *update = PR_FALSE; andre@0: } andre@0: andre@0: if ((pwitem == NULL) || (src == NULL)) { andre@0: return NULL; andre@0: } andre@0: andre@0: /* get key, and iv */ andre@0: key = nsspkcs5_ComputeKeyAndIV(pbe_param, pwitem, &iv, PR_FALSE); andre@0: if(key == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: switch(pbe_param->encAlg) { andre@0: /* PKCS 5 v2 only */ andre@0: case SEC_OID_AES_128_CBC: andre@0: case SEC_OID_AES_192_CBC: andre@0: case SEC_OID_AES_256_CBC: andre@0: cryptof = sec_pkcs5_aes; andre@0: break; andre@0: case SEC_OID_DES_EDE3_CBC: andre@0: cryptof = sec_pkcs5_des; andre@0: tripleDES = PR_TRUE; andre@0: break; andre@0: case SEC_OID_DES_CBC: andre@0: cryptof = sec_pkcs5_des; andre@0: tripleDES = PR_FALSE; andre@0: break; andre@0: case SEC_OID_RC2_CBC: andre@0: cryptof = sec_pkcs5_rc2; andre@0: break; andre@0: case SEC_OID_RC4: andre@0: cryptof = sec_pkcs5_rc4; andre@0: break; andre@0: default: andre@0: cryptof = NULL; andre@0: break; andre@0: } andre@0: andre@0: if (cryptof == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: dest = (*cryptof)(key, &iv, src, tripleDES, encrypt); andre@0: /* andre@0: * it's possible for some keys and keydb's to claim to andre@0: * be triple des when they're really des. In this case andre@0: * we simply try des. If des works we set the update flag andre@0: * so the key db knows it needs to update all it's entries. andre@0: * The case can only happen on decrypted of a andre@0: * SEC_OID_DES_EDE3_CBD. andre@0: */ andre@0: if ((dest == NULL) && (encrypt == PR_FALSE) && andre@0: (pbe_param->encAlg == SEC_OID_DES_EDE3_CBC)) { andre@0: dest = (*cryptof)(key, &iv, src, PR_FALSE, encrypt); andre@0: if (update && (dest != NULL)) *update = PR_TRUE; andre@0: } andre@0: andre@0: loser: andre@0: if (key != NULL) { andre@0: SECITEM_ZfreeItem(key, PR_TRUE); andre@0: } andre@0: if (iv.data != NULL) { andre@0: SECITEM_ZfreeItem(&iv, PR_FALSE); andre@0: } andre@0: andre@0: return dest; andre@0: } andre@0: andre@0: /* creates a algorithm ID containing the PBE algorithm and appropriate andre@0: * parameters. the required parameter is the algorithm. if salt is andre@0: * not specified, it is generated randomly. if IV is specified, it overrides andre@0: * the PKCS 5 generation of the IV. andre@0: * andre@0: * the returned SECAlgorithmID should be destroyed using andre@0: * SECOID_DestroyAlgorithmID andre@0: */ andre@0: SECAlgorithmID * andre@0: nsspkcs5_CreateAlgorithmID(PLArenaPool *arena, SECOidTag algorithm, andre@0: NSSPKCS5PBEParameter *pbe_param) andre@0: { andre@0: SECAlgorithmID *algid, *ret_algid = NULL; andre@0: SECItem der_param; andre@0: nsspkcs5V2PBEParameter pkcs5v2_param; andre@0: andre@0: SECStatus rv = SECFailure; andre@0: void *dummy = NULL; andre@0: andre@0: if (arena == NULL) { andre@0: return NULL; andre@0: } andre@0: andre@0: der_param.data = NULL; andre@0: der_param.len = 0; andre@0: andre@0: /* generate the algorithm id */ andre@0: algid = (SECAlgorithmID *)PORT_ArenaZAlloc(arena, sizeof(SECAlgorithmID)); andre@0: if (algid == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: if (pbe_param->iteration.data == NULL) { andre@0: dummy = SEC_ASN1EncodeInteger(pbe_param->poolp,&pbe_param->iteration, andre@0: pbe_param->iter); andre@0: if (dummy == NULL) { andre@0: goto loser; andre@0: } andre@0: } andre@0: switch (pbe_param->pbeType) { andre@0: case NSSPKCS5_PBKDF1: andre@0: dummy = SEC_ASN1EncodeItem(arena, &der_param, pbe_param, andre@0: NSSPKCS5PBEParameterTemplate); andre@0: break; andre@0: case NSSPKCS5_PKCS12_V2: andre@0: dummy = SEC_ASN1EncodeItem(arena, &der_param, pbe_param, andre@0: NSSPKCS5PKCS12V2PBEParameterTemplate); andre@0: break; andre@0: #ifdef PBKDF2 andre@0: case NSSPKCS5_PBKDF2: andre@0: if (pbe_param->keyLength.data == NULL) { andre@0: dummy = SEC_ASN1EncodeInteger(pbe_param->poolp, andre@0: &pbe_param->keyLength, pbe_param->keyLen); andre@0: if (dummy == NULL) { andre@0: goto loser; andre@0: } andre@0: } andre@0: PORT_Memset(&pkcs5v2_param, 0, sizeof(pkcs5v2_param)); andre@0: dummy = SEC_ASN1EncodeItem(arena, &der_param, pbe_param, andre@0: NSSPKCS5V2PBEParameterTemplate); andre@0: if (dummy == NULL) { andre@0: break; andre@0: } andre@0: dummy = NULL; andre@0: rv = SECOID_SetAlgorithmID(arena, &pkcs5v2_param.keyParams, andre@0: SEC_OID_PKCS5_PBKDF2, &der_param); andre@0: if (rv != SECSuccess) { andre@0: break; andre@0: } andre@0: der_param.data = pbe_param->ivData; andre@0: der_param.len = pbe_param->ivLen; andre@0: rv = SECOID_SetAlgorithmID(arena, &pkcs5v2_param.algParams, andre@0: pbe_param->encAlg, pbe_param->ivLen ? &der_param : NULL); andre@0: if (rv != SECSuccess) { andre@0: break; andre@0: } andre@0: dummy = SEC_ASN1EncodeItem(arena, &der_param, &pkcs5v2_param, andre@0: NSSPKCS5V2PBES2ParameterTemplate); andre@0: break; andre@0: #endif andre@0: default: andre@0: break; andre@0: } andre@0: andre@0: if (dummy == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: rv = SECOID_SetAlgorithmID(arena, algid, algorithm, &der_param); andre@0: if (rv != SECSuccess) { andre@0: goto loser; andre@0: } andre@0: andre@0: ret_algid = (SECAlgorithmID *)PORT_ZAlloc(sizeof(SECAlgorithmID)); andre@0: if (ret_algid == NULL) { andre@0: goto loser; andre@0: } andre@0: andre@0: rv = SECOID_CopyAlgorithmID(NULL, ret_algid, algid); andre@0: if (rv != SECSuccess) { andre@0: SECOID_DestroyAlgorithmID(ret_algid, PR_TRUE); andre@0: ret_algid = NULL; andre@0: } andre@0: andre@0: loser: andre@0: andre@0: return ret_algid; andre@0: }