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