2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/rand.h>
16 /* PKCS#5 v2.0 password based encryption structures */
18 ASN1_SEQUENCE(PBE2PARAM) = {
19 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
20 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
21 } ASN1_SEQUENCE_END(PBE2PARAM)
23 IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
25 ASN1_SEQUENCE(PBKDF2PARAM) = {
26 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
27 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
28 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
29 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
30 } ASN1_SEQUENCE_END(PBKDF2PARAM)
32 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
35 * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know
36 * this is horrible! Extended version to allow application supplied PRF NID
40 X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
41 unsigned char *salt, int saltlen,
42 unsigned char *aiv, int prf_nid)
44 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
46 EVP_CIPHER_CTX *ctx = NULL;
47 unsigned char iv[EVP_MAX_IV_LENGTH];
48 PBE2PARAM *pbe2 = NULL;
51 alg_nid = EVP_CIPHER_type(cipher);
52 if (alg_nid == NID_undef) {
53 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,
54 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
57 obj = OBJ_nid2obj(alg_nid);
59 if ((pbe2 = PBE2PARAM_new()) == NULL)
62 /* Setup the AlgorithmIdentifier for the encryption scheme */
63 scheme = pbe2->encryption;
64 scheme->algorithm = obj;
65 if ((scheme->parameter = ASN1_TYPE_new()) == NULL)
68 /* Create random IV */
69 if (EVP_CIPHER_iv_length(cipher)) {
71 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
72 else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) <= 0)
76 ctx = EVP_CIPHER_CTX_new();
80 /* Dummy cipherinit to just setup the IV, and PRF */
81 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0))
83 if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) < 0) {
84 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
88 * If prf NID unspecified see if cipher has a preference. An error is OK
89 * here: just means use default PRF.
91 if ((prf_nid == -1) &&
92 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
94 prf_nid = NID_hmacWithSHA256;
96 EVP_CIPHER_CTX_free(ctx);
99 /* If its RC2 then we'd better setup the key length */
101 if (alg_nid == NID_rc2_cbc)
102 keylen = EVP_CIPHER_key_length(cipher);
108 X509_ALGOR_free(pbe2->keyfunc);
110 pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
115 /* Now set up top level AlgorithmIdentifier */
117 if ((ret = X509_ALGOR_new()) == NULL)
120 ret->algorithm = OBJ_nid2obj(NID_pbes2);
122 /* Encode PBE2PARAM into parameter */
124 if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
128 PBE2PARAM_free(pbe2);
134 ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, ERR_R_MALLOC_FAILURE);
137 EVP_CIPHER_CTX_free(ctx);
138 PBE2PARAM_free(pbe2);
139 /* Note 'scheme' is freed as part of pbe2 */
140 X509_ALGOR_free(kalg);
141 X509_ALGOR_free(ret);
147 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
148 unsigned char *salt, int saltlen)
150 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
153 X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
154 int prf_nid, int keylen)
156 X509_ALGOR *keyfunc = NULL;
157 PBKDF2PARAM *kdf = NULL;
158 ASN1_OCTET_STRING *osalt = NULL;
160 if ((kdf = PBKDF2PARAM_new()) == NULL)
162 if ((osalt = ASN1_OCTET_STRING_new()) == NULL)
165 kdf->salt->value.octet_string = osalt;
166 kdf->salt->type = V_ASN1_OCTET_STRING;
169 saltlen = PKCS5_SALT_LEN;
170 if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL)
173 osalt->length = saltlen;
176 memcpy(osalt->data, salt, saltlen);
177 else if (RAND_bytes(osalt->data, saltlen) <= 0)
181 iter = PKCS5_DEFAULT_ITER;
183 if (!ASN1_INTEGER_set(kdf->iter, iter))
186 /* If have a key len set it up */
189 if ((kdf->keylength = ASN1_INTEGER_new()) == NULL)
191 if (!ASN1_INTEGER_set(kdf->keylength, keylen))
195 /* prf can stay NULL if we are using hmacWithSHA1 */
196 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
197 kdf->prf = X509_ALGOR_new();
198 if (kdf->prf == NULL)
200 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), V_ASN1_NULL, NULL);
203 /* Finally setup the keyfunc structure */
205 keyfunc = X509_ALGOR_new();
209 keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
211 /* Encode PBKDF2PARAM into parameter of pbe2 */
213 if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf,
214 &keyfunc->parameter))
217 PBKDF2PARAM_free(kdf);
221 ASN1err(ASN1_F_PKCS5_PBKDF2_SET, ERR_R_MALLOC_FAILURE);
222 PBKDF2PARAM_free(kdf);
223 X509_ALGOR_free(keyfunc);