155714Skris/* p5_pbev2.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296465Sdelphij * 1999-2004.
555714Skris */
655714Skris/* ====================================================================
755714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
855714Skris *
955714Skris * Redistribution and use in source and binary forms, with or without
1055714Skris * modification, are permitted provided that the following conditions
1155714Skris * are met:
1255714Skris *
1355714Skris * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
1555714Skris *
1655714Skris * 2. Redistributions in binary form must reproduce the above copyright
1755714Skris *    notice, this list of conditions and the following disclaimer in
1855714Skris *    the documentation and/or other materials provided with the
1955714Skris *    distribution.
2055714Skris *
2155714Skris * 3. All advertising materials mentioning features or use of this
2255714Skris *    software must display the following acknowledgment:
2355714Skris *    "This product includes software developed by the OpenSSL Project
2455714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2555714Skris *
2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2755714Skris *    endorse or promote products derived from this software without
2855714Skris *    prior written permission. For written permission, please contact
2955714Skris *    licensing@OpenSSL.org.
3055714Skris *
3155714Skris * 5. Products derived from this software may not be called "OpenSSL"
3255714Skris *    nor may "OpenSSL" appear in their names without prior written
3355714Skris *    permission of the OpenSSL Project.
3455714Skris *
3555714Skris * 6. Redistributions of any form whatsoever must retain the following
3655714Skris *    acknowledgment:
3755714Skris *    "This product includes software developed by the OpenSSL Project
3855714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3955714Skris *
4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4355714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5255714Skris * ====================================================================
5355714Skris *
5455714Skris * This product includes cryptographic software written by Eric Young
5555714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5655714Skris * Hudson (tjh@cryptsoft.com).
5755714Skris *
5855714Skris */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
62109998Smarkm#include <openssl/asn1t.h>
6355714Skris#include <openssl/x509.h>
6455714Skris#include <openssl/rand.h>
6555714Skris
6655714Skris/* PKCS#5 v2.0 password based encryption structures */
6755714Skris
68109998SmarkmASN1_SEQUENCE(PBE2PARAM) = {
69296465Sdelphij        ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
70296465Sdelphij        ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
71109998Smarkm} ASN1_SEQUENCE_END(PBE2PARAM)
7255714Skris
73109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
7455714Skris
75109998SmarkmASN1_SEQUENCE(PBKDF2PARAM) = {
76296465Sdelphij        ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY),
77296465Sdelphij        ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER),
78296465Sdelphij        ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER),
79296465Sdelphij        ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR)
80109998Smarkm} ASN1_SEQUENCE_END(PBKDF2PARAM)
8155714Skris
82109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
8355714Skris
84296465Sdelphij/*
85296465Sdelphij * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: yes I know
86296465Sdelphij * this is horrible!
8755714Skris */
8855714Skris
8955714SkrisX509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
90296465Sdelphij                           unsigned char *salt, int saltlen)
9155714Skris{
92296465Sdelphij    X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
93296465Sdelphij    int alg_nid;
94296465Sdelphij    EVP_CIPHER_CTX ctx;
95296465Sdelphij    unsigned char iv[EVP_MAX_IV_LENGTH];
96296465Sdelphij    PBKDF2PARAM *kdf = NULL;
97296465Sdelphij    PBE2PARAM *pbe2 = NULL;
98296465Sdelphij    ASN1_OCTET_STRING *osalt = NULL;
99296465Sdelphij    ASN1_OBJECT *obj;
10055714Skris
101296465Sdelphij    alg_nid = EVP_CIPHER_type(cipher);
102296465Sdelphij    if (alg_nid == NID_undef) {
103296465Sdelphij        ASN1err(ASN1_F_PKCS5_PBE2_SET,
104296465Sdelphij                ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
105296465Sdelphij        goto err;
106296465Sdelphij    }
107296465Sdelphij    obj = OBJ_nid2obj(alg_nid);
10859191Skris
109296465Sdelphij    if (!(pbe2 = PBE2PARAM_new()))
110296465Sdelphij        goto merr;
11155714Skris
112296465Sdelphij    /* Setup the AlgorithmIdentifier for the encryption scheme */
113296465Sdelphij    scheme = pbe2->encryption;
11455714Skris
115296465Sdelphij    scheme->algorithm = obj;
116296465Sdelphij    if (!(scheme->parameter = ASN1_TYPE_new()))
117296465Sdelphij        goto merr;
11855714Skris
119296465Sdelphij    /* Create random IV */
120296465Sdelphij    if (EVP_CIPHER_iv_length(cipher) &&
121296465Sdelphij        RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
122296465Sdelphij        goto err;
12355714Skris
124296465Sdelphij    EVP_CIPHER_CTX_init(&ctx);
125109998Smarkm
126296465Sdelphij    /* Dummy cipherinit to just setup the IV */
127296465Sdelphij    EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0);
128296465Sdelphij    if (EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) {
129296465Sdelphij        ASN1err(ASN1_F_PKCS5_PBE2_SET, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
130296465Sdelphij        EVP_CIPHER_CTX_cleanup(&ctx);
131296465Sdelphij        goto err;
132296465Sdelphij    }
133296465Sdelphij    EVP_CIPHER_CTX_cleanup(&ctx);
13455714Skris
135296465Sdelphij    if (!(kdf = PBKDF2PARAM_new()))
136296465Sdelphij        goto merr;
137296465Sdelphij    if (!(osalt = M_ASN1_OCTET_STRING_new()))
138296465Sdelphij        goto merr;
13955714Skris
140296465Sdelphij    if (!saltlen)
141296465Sdelphij        saltlen = PKCS5_SALT_LEN;
142296465Sdelphij    if (!(osalt->data = OPENSSL_malloc(saltlen)))
143296465Sdelphij        goto merr;
144296465Sdelphij    osalt->length = saltlen;
145296465Sdelphij    if (salt)
146296465Sdelphij        memcpy(osalt->data, salt, saltlen);
147296465Sdelphij    else if (RAND_pseudo_bytes(osalt->data, saltlen) < 0)
148296465Sdelphij        goto merr;
14955714Skris
150296465Sdelphij    if (iter <= 0)
151296465Sdelphij        iter = PKCS5_DEFAULT_ITER;
152296465Sdelphij    if (!ASN1_INTEGER_set(kdf->iter, iter))
153296465Sdelphij        goto merr;
15455714Skris
155296465Sdelphij    /* Now include salt in kdf structure */
156296465Sdelphij    kdf->salt->value.octet_string = osalt;
157296465Sdelphij    kdf->salt->type = V_ASN1_OCTET_STRING;
158296465Sdelphij    osalt = NULL;
15955714Skris
160296465Sdelphij    /* If its RC2 then we'd better setup the key length */
16155714Skris
162296465Sdelphij    if (alg_nid == NID_rc2_cbc) {
163296465Sdelphij        if (!(kdf->keylength = M_ASN1_INTEGER_new()))
164296465Sdelphij            goto merr;
165296465Sdelphij        if (!ASN1_INTEGER_set(kdf->keylength, EVP_CIPHER_key_length(cipher)))
166296465Sdelphij            goto merr;
167296465Sdelphij    }
16855714Skris
169296465Sdelphij    /* prf can stay NULL because we are using hmacWithSHA1 */
17055714Skris
171296465Sdelphij    /* Now setup the PBE2PARAM keyfunc structure */
17255714Skris
173296465Sdelphij    pbe2->keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
17455714Skris
175296465Sdelphij    /* Encode PBKDF2PARAM into parameter of pbe2 */
17655714Skris
177296465Sdelphij    if (!(pbe2->keyfunc->parameter = ASN1_TYPE_new()))
178296465Sdelphij        goto merr;
17955714Skris
180296465Sdelphij    if (!ASN1_pack_string_of(PBKDF2PARAM, kdf, i2d_PBKDF2PARAM,
181296465Sdelphij                             &pbe2->keyfunc->parameter->value.sequence))
182296465Sdelphij         goto merr;
183296465Sdelphij    pbe2->keyfunc->parameter->type = V_ASN1_SEQUENCE;
18455714Skris
185296465Sdelphij    PBKDF2PARAM_free(kdf);
186296465Sdelphij    kdf = NULL;
18755714Skris
188296465Sdelphij    /* Now set up top level AlgorithmIdentifier */
18955714Skris
190296465Sdelphij    if (!(ret = X509_ALGOR_new()))
191296465Sdelphij        goto merr;
192296465Sdelphij    if (!(ret->parameter = ASN1_TYPE_new()))
193296465Sdelphij        goto merr;
19455714Skris
195296465Sdelphij    ret->algorithm = OBJ_nid2obj(NID_pbes2);
19655714Skris
197296465Sdelphij    /* Encode PBE2PARAM into parameter */
19855714Skris
199296465Sdelphij    if (!ASN1_pack_string_of(PBE2PARAM, pbe2, i2d_PBE2PARAM,
200296465Sdelphij                             &ret->parameter->value.sequence))
201296465Sdelphij         goto merr;
202296465Sdelphij    ret->parameter->type = V_ASN1_SEQUENCE;
20355714Skris
204296465Sdelphij    PBE2PARAM_free(pbe2);
205296465Sdelphij    pbe2 = NULL;
20655714Skris
207296465Sdelphij    return ret;
20855714Skris
209296465Sdelphij merr:
210296465Sdelphij    ASN1err(ASN1_F_PKCS5_PBE2_SET, ERR_R_MALLOC_FAILURE);
21155714Skris
212296465Sdelphij err:
213296465Sdelphij    PBE2PARAM_free(pbe2);
214296465Sdelphij    /* Note 'scheme' is freed as part of pbe2 */
215296465Sdelphij    M_ASN1_OCTET_STRING_free(osalt);
216296465Sdelphij    PBKDF2PARAM_free(kdf);
217296465Sdelphij    X509_ALGOR_free(kalg);
218296465Sdelphij    X509_ALGOR_free(ret);
21955714Skris
220296465Sdelphij    return NULL;
22155714Skris
22255714Skris}
223