1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 /* ====================================================================
5 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com). */
57 #include <openssl/asn1t.h>
58 #include <openssl/cipher.h>
59 #include <openssl/err.h>
60 #include <openssl/mem.h>
61 #include <openssl/pkcs8.h>
62 #include <openssl/rand.h>
63 #include <openssl/x509.h>
68 /* PKCS#5 v2.0 password based encryption structures */
70 ASN1_SEQUENCE(PBE2PARAM) = {
71 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
72 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
73 } ASN1_SEQUENCE_END(PBE2PARAM)
75 IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
77 ASN1_SEQUENCE(PBKDF2PARAM) = {
78 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
79 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
80 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
81 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
82 } ASN1_SEQUENCE_END(PBKDF2PARAM)
84 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM);
86 static int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len)
90 if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0);
91 if (!M_ASN1_OCTET_STRING_set(os,data,len))
93 M_ASN1_OCTET_STRING_free(os);
96 ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os);
100 static int param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
104 iv_len = EVP_CIPHER_CTX_iv_length(c);
105 return ASN1_TYPE_set_octetstring(type, c->oiv, iv_len);
108 /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm:
109 * yes I know this is horrible!
111 * Extended version to allow application supplied PRF NID and IV. */
113 X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
114 unsigned char *salt, int saltlen,
115 unsigned char *aiv, int prf_nid)
117 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
120 unsigned char iv[EVP_MAX_IV_LENGTH];
121 PBE2PARAM *pbe2 = NULL;
122 const ASN1_OBJECT *obj;
124 alg_nid = EVP_CIPHER_nid(cipher);
125 if(alg_nid == NID_undef) {
126 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
129 obj = OBJ_nid2obj(alg_nid);
131 if(!(pbe2 = PBE2PARAM_new())) goto merr;
133 /* Setup the AlgorithmIdentifier for the encryption scheme */
134 scheme = pbe2->encryption;
136 scheme->algorithm = (ASN1_OBJECT*) obj;
137 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr;
139 /* Create random IV */
140 if (EVP_CIPHER_iv_length(cipher))
143 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
144 else if (RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
148 EVP_CIPHER_CTX_init(&ctx);
150 /* Dummy cipherinit to just setup the IV, and PRF */
151 if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0))
153 if(param_to_asn1(&ctx, scheme->parameter) < 0) {
154 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS);
155 EVP_CIPHER_CTX_cleanup(&ctx);
158 /* If prf NID unspecified see if cipher has a preference.
159 * An error is OK here: just means use default PRF.
161 if ((prf_nid == -1) &&
162 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0)
165 prf_nid = NID_hmacWithSHA1;
167 EVP_CIPHER_CTX_cleanup(&ctx);
169 /* If its RC2 then we'd better setup the key length */
171 if(alg_nid == NID_rc2_cbc)
172 keylen = EVP_CIPHER_key_length(cipher);
178 X509_ALGOR_free(pbe2->keyfunc);
180 pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
185 /* Now set up top level AlgorithmIdentifier */
187 if(!(ret = X509_ALGOR_new())) goto merr;
188 if(!(ret->parameter = ASN1_TYPE_new())) goto merr;
190 ret->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_pbes2);
192 /* Encode PBE2PARAM into parameter */
194 if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM),
195 &ret->parameter->value.sequence)) goto merr;
196 ret->parameter->type = V_ASN1_SEQUENCE;
198 PBE2PARAM_free(pbe2);
204 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, ERR_R_MALLOC_FAILURE);
207 PBE2PARAM_free(pbe2);
208 /* Note 'scheme' is freed as part of pbe2 */
209 X509_ALGOR_free(kalg);
210 X509_ALGOR_free(ret);
216 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
217 unsigned char *salt, int saltlen)
219 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
222 X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
223 int prf_nid, int keylen)
225 X509_ALGOR *keyfunc = NULL;
226 PBKDF2PARAM *kdf = NULL;
227 ASN1_OCTET_STRING *osalt = NULL;
229 if(!(kdf = PBKDF2PARAM_new()))
231 if(!(osalt = M_ASN1_OCTET_STRING_new()))
234 kdf->salt->value.octet_string = osalt;
235 kdf->salt->type = V_ASN1_OCTET_STRING;
238 saltlen = PKCS5_SALT_LEN;
239 if (!(osalt->data = OPENSSL_malloc (saltlen)))
242 osalt->length = saltlen;
245 memcpy (osalt->data, salt, saltlen);
246 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0)
250 iter = PKCS5_DEFAULT_ITERATIONS;
252 if(!ASN1_INTEGER_set(kdf->iter, iter))
255 /* If have a key len set it up */
259 if(!(kdf->keylength = M_ASN1_INTEGER_new()))
261 if(!ASN1_INTEGER_set (kdf->keylength, keylen))
265 /* prf can stay NULL if we are using hmacWithSHA1 */
266 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1)
268 kdf->prf = X509_ALGOR_new();
271 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid),
275 /* Finally setup the keyfunc structure */
277 keyfunc = X509_ALGOR_new();
281 keyfunc->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_id_pbkdf2);
283 /* Encode PBKDF2PARAM into parameter of pbe2 */
285 if(!(keyfunc->parameter = ASN1_TYPE_new()))
288 if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM),
289 &keyfunc->parameter->value.sequence))
291 keyfunc->parameter->type = V_ASN1_SEQUENCE;
293 PBKDF2PARAM_free(kdf);
297 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbkdf2_set, ERR_R_MALLOC_FAILURE);
298 PBKDF2PARAM_free(kdf);
299 X509_ALGOR_free(keyfunc);