]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/p5_pbev2.c
Update copyright year
[thirdparty/openssl.git] / crypto / asn1 / p5_pbev2.c
CommitLineData
0f113f3e 1/*
605856d7 2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
d2e26dcc 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d2e26dcc
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
9d6b1ce6 12#include <openssl/asn1t.h>
f0e8ae72 13#include <openssl/x509.h>
ec577822 14#include <openssl/rand.h>
d2e26dcc
DSH
15
16/* PKCS#5 v2.0 password based encryption structures */
17
9d6b1ce6 18ASN1_SEQUENCE(PBE2PARAM) = {
0f113f3e
MC
19 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR),
20 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR)
d339187b 21} ASN1_SEQUENCE_END(PBE2PARAM)
d2e26dcc 22
9d6b1ce6 23IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM)
d2e26dcc 24
9d6b1ce6 25ASN1_SEQUENCE(PBKDF2PARAM) = {
0f113f3e
MC
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)
d339187b 30} ASN1_SEQUENCE_END(PBKDF2PARAM)
d2e26dcc 31
9d6b1ce6 32IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM)
d2e26dcc 33
0f113f3e
MC
34/*
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
37 * and IV.
8e21c146
DSH
38 */
39
ae519a24 40X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter,
0f113f3e
MC
41 unsigned char *salt, int saltlen,
42 unsigned char *aiv, int prf_nid)
8e21c146 43{
2191dc84 44 X509_ALGOR *scheme = NULL, *ret = NULL;
0f113f3e 45 int alg_nid, keylen;
846ec07d 46 EVP_CIPHER_CTX *ctx = NULL;
0f113f3e
MC
47 unsigned char iv[EVP_MAX_IV_LENGTH];
48 PBE2PARAM *pbe2 = NULL;
0f113f3e
MC
49
50 alg_nid = EVP_CIPHER_type(cipher);
51 if (alg_nid == NID_undef) {
9311d0c4 52 ERR_raise(ERR_LIB_ASN1, ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
0f113f3e
MC
53 goto err;
54 }
0f113f3e 55
75ebbd9a 56 if ((pbe2 = PBE2PARAM_new()) == NULL)
0f113f3e
MC
57 goto merr;
58
59 /* Setup the AlgorithmIdentifier for the encryption scheme */
60 scheme = pbe2->encryption;
2191dc84 61 scheme->algorithm = OBJ_nid2obj(alg_nid);
75ebbd9a 62 if ((scheme->parameter = ASN1_TYPE_new()) == NULL)
0f113f3e
MC
63 goto merr;
64
65 /* Create random IV */
66 if (EVP_CIPHER_iv_length(cipher)) {
67 if (aiv)
68 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
266483d2 69 else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) <= 0)
0f113f3e
MC
70 goto err;
71 }
72
846ec07d
RL
73 ctx = EVP_CIPHER_CTX_new();
74 if (ctx == NULL)
75 goto merr;
0f113f3e
MC
76
77 /* Dummy cipherinit to just setup the IV, and PRF */
846ec07d 78 if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0))
0f113f3e 79 goto err;
49c9c1b3 80 if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) <= 0) {
9311d0c4 81 ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
0f113f3e
MC
82 goto err;
83 }
84 /*
85 * If prf NID unspecified see if cipher has a preference. An error is OK
86 * here: just means use default PRF.
87 */
88 if ((prf_nid == -1) &&
846ec07d 89 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) {
0f113f3e 90 ERR_clear_error();
8fc06e88 91 prf_nid = NID_hmacWithSHA256;
0f113f3e 92 }
846ec07d
RL
93 EVP_CIPHER_CTX_free(ctx);
94 ctx = NULL;
0f113f3e
MC
95
96 /* If its RC2 then we'd better setup the key length */
97
98 if (alg_nid == NID_rc2_cbc)
99 keylen = EVP_CIPHER_key_length(cipher);
100 else
101 keylen = -1;
102
103 /* Setup keyfunc */
104
105 X509_ALGOR_free(pbe2->keyfunc);
106
107 pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen);
108
12a765a5 109 if (pbe2->keyfunc == NULL)
0f113f3e
MC
110 goto merr;
111
112 /* Now set up top level AlgorithmIdentifier */
113
75ebbd9a 114 if ((ret = X509_ALGOR_new()) == NULL)
0f113f3e 115 goto merr;
0f113f3e
MC
116
117 ret->algorithm = OBJ_nid2obj(NID_pbes2);
118
119 /* Encode PBE2PARAM into parameter */
120
e93c8748
DSH
121 if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
122 &ret->parameter))
0f113f3e 123 goto merr;
0f113f3e
MC
124
125 PBE2PARAM_free(pbe2);
126 pbe2 = NULL;
127
128 return ret;
129
130 merr:
9311d0c4 131 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
132
133 err:
846ec07d 134 EVP_CIPHER_CTX_free(ctx);
0f113f3e
MC
135 PBE2PARAM_free(pbe2);
136 /* Note 'scheme' is freed as part of pbe2 */
0f113f3e
MC
137 X509_ALGOR_free(ret);
138
139 return NULL;
0f113f3e 140}
8e21c146 141
0f113f3e
MC
142X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter,
143 unsigned char *salt, int saltlen)
144{
145 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1);
146}
8e21c146 147
0f113f3e
MC
148X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen,
149 int prf_nid, int keylen)
150{
151 X509_ALGOR *keyfunc = NULL;
152 PBKDF2PARAM *kdf = NULL;
153 ASN1_OCTET_STRING *osalt = NULL;
0fccb00b 154
75ebbd9a 155 if ((kdf = PBKDF2PARAM_new()) == NULL)
0f113f3e 156 goto merr;
75ebbd9a 157 if ((osalt = ASN1_OCTET_STRING_new()) == NULL)
0f113f3e 158 goto merr;
8e21c146 159
0f113f3e
MC
160 kdf->salt->value.octet_string = osalt;
161 kdf->salt->type = V_ASN1_OCTET_STRING;
8e21c146 162
75ebbd9a 163 if (saltlen == 0)
0f113f3e 164 saltlen = PKCS5_SALT_LEN;
75ebbd9a 165 if ((osalt->data = OPENSSL_malloc(saltlen)) == NULL)
0f113f3e 166 goto merr;
8e21c146 167
0f113f3e 168 osalt->length = saltlen;
8e21c146 169
0f113f3e
MC
170 if (salt)
171 memcpy(osalt->data, salt, saltlen);
266483d2 172 else if (RAND_bytes(osalt->data, saltlen) <= 0)
0f113f3e 173 goto merr;
f9678b8b 174
0f113f3e
MC
175 if (iter <= 0)
176 iter = PKCS5_DEFAULT_ITER;
8e21c146 177
0f113f3e
MC
178 if (!ASN1_INTEGER_set(kdf->iter, iter))
179 goto merr;
8e21c146 180
0f113f3e 181 /* If have a key len set it up */
8e21c146 182
0f113f3e 183 if (keylen > 0) {
75ebbd9a 184 if ((kdf->keylength = ASN1_INTEGER_new()) == NULL)
0f113f3e
MC
185 goto merr;
186 if (!ASN1_INTEGER_set(kdf->keylength, keylen))
187 goto merr;
188 }
8e21c146 189
0f113f3e
MC
190 /* prf can stay NULL if we are using hmacWithSHA1 */
191 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) {
192 kdf->prf = X509_ALGOR_new();
90945fa3 193 if (kdf->prf == NULL)
0f113f3e
MC
194 goto merr;
195 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), V_ASN1_NULL, NULL);
196 }
8e21c146 197
0f113f3e 198 /* Finally setup the keyfunc structure */
8e21c146 199
0f113f3e 200 keyfunc = X509_ALGOR_new();
90945fa3 201 if (keyfunc == NULL)
0f113f3e 202 goto merr;
8e21c146 203
0f113f3e 204 keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2);
8e21c146 205
0f113f3e 206 /* Encode PBKDF2PARAM into parameter of pbe2 */
8e21c146 207
e93c8748
DSH
208 if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), kdf,
209 &keyfunc->parameter))
0f113f3e 210 goto merr;
8e21c146 211
0f113f3e
MC
212 PBKDF2PARAM_free(kdf);
213 return keyfunc;
8e21c146 214
0f113f3e 215 merr:
9311d0c4 216 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
217 PBKDF2PARAM_free(kdf);
218 X509_ALGOR_free(keyfunc);
219 return NULL;
8e21c146 220}