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 password based encryption structure */
18 ASN1_SEQUENCE(PBEPARAM) = {
19 ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
20 ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
21 } ASN1_SEQUENCE_END(PBEPARAM)
23 IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
25 /* Set an algorithm identifier for a PKCS#5 PBE algorithm */
27 int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
28 const unsigned char *salt, int saltlen)
31 ASN1_STRING *pbe_str = NULL;
32 unsigned char *sstr = NULL;
36 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
40 iter = PKCS5_DEFAULT_ITER;
41 if (!ASN1_INTEGER_set(pbe->iter, iter)) {
42 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
46 saltlen = PKCS5_SALT_LEN;
48 sstr = OPENSSL_malloc(saltlen);
50 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
54 memcpy(sstr, salt, saltlen);
55 else if (RAND_bytes(sstr, saltlen) <= 0)
58 ASN1_STRING_set0(pbe->salt, sstr, saltlen);
61 if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) {
62 ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
69 if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
75 ASN1_STRING_free(pbe_str);
79 /* Return an algorithm identifier for a PKCS#5 PBE algorithm */
81 X509_ALGOR *PKCS5_pbe_set(int alg, int iter,
82 const unsigned char *salt, int saltlen)
85 ret = X509_ALGOR_new();
87 ASN1err(ASN1_F_PKCS5_PBE_SET, ERR_R_MALLOC_FAILURE);
91 if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen))