155714Skris/* p5_pbe.c */
2280297Sjkim/*
3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280297Sjkim * 1999.
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
14280297Sjkim *    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 password based encryption structure */
6755714Skris
68109998SmarkmASN1_SEQUENCE(PBEPARAM) = {
69280297Sjkim        ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
70280297Sjkim        ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
71109998Smarkm} ASN1_SEQUENCE_END(PBEPARAM)
7255714Skris
73109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
7455714Skris
75238405Sjkim/* Set an algorithm identifier for a PKCS#5 PBE algorithm */
76238405Sjkim
77238405Sjkimint PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
78280297Sjkim                         const unsigned char *salt, int saltlen)
79280297Sjkim{
80280297Sjkim    PBEPARAM *pbe = NULL;
81280297Sjkim    ASN1_STRING *pbe_str = NULL;
82280297Sjkim    unsigned char *sstr;
8355714Skris
84280297Sjkim    pbe = PBEPARAM_new();
85280297Sjkim    if (!pbe) {
86280297Sjkim        ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
87280297Sjkim        goto err;
88280297Sjkim    }
89280297Sjkim    if (iter <= 0)
90280297Sjkim        iter = PKCS5_DEFAULT_ITER;
91280297Sjkim    if (!ASN1_INTEGER_set(pbe->iter, iter)) {
92280297Sjkim        ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
93280297Sjkim        goto err;
94280297Sjkim    }
95280297Sjkim    if (!saltlen)
96280297Sjkim        saltlen = PKCS5_SALT_LEN;
97280297Sjkim    if (!ASN1_STRING_set(pbe->salt, NULL, saltlen)) {
98280297Sjkim        ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
99280297Sjkim        goto err;
100280297Sjkim    }
101280297Sjkim    sstr = ASN1_STRING_data(pbe->salt);
102280297Sjkim    if (salt)
103280297Sjkim        memcpy(sstr, salt, saltlen);
104306195Sjkim    else if (RAND_bytes(sstr, saltlen) <= 0)
105280297Sjkim        goto err;
10655714Skris
107280297Sjkim    if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) {
108280297Sjkim        ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
109280297Sjkim        goto err;
110280297Sjkim    }
11155714Skris
112280297Sjkim    PBEPARAM_free(pbe);
113280297Sjkim    pbe = NULL;
114238405Sjkim
115280297Sjkim    if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
116280297Sjkim        return 1;
117238405Sjkim
118280297Sjkim err:
119280297Sjkim    if (pbe != NULL)
120280297Sjkim        PBEPARAM_free(pbe);
121280297Sjkim    if (pbe_str != NULL)
122280297Sjkim        ASN1_STRING_free(pbe_str);
123280297Sjkim    return 0;
124280297Sjkim}
12555714Skris
126238405Sjkim/* Return an algorithm identifier for a PKCS#5 PBE algorithm */
127238405Sjkim
128238405SjkimX509_ALGOR *PKCS5_pbe_set(int alg, int iter,
129280297Sjkim                          const unsigned char *salt, int saltlen)
130280297Sjkim{
131280297Sjkim    X509_ALGOR *ret;
132280297Sjkim    ret = X509_ALGOR_new();
133280297Sjkim    if (!ret) {
134280297Sjkim        ASN1err(ASN1_F_PKCS5_PBE_SET, ERR_R_MALLOC_FAILURE);
135280297Sjkim        return NULL;
136280297Sjkim    }
137238405Sjkim
138280297Sjkim    if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen))
139280297Sjkim        return ret;
140238405Sjkim
141280297Sjkim    X509_ALGOR_free(ret);
142280297Sjkim    return NULL;
143280297Sjkim}
144