Adapt OPENSSL_DEBUG_PKCS5V2 to the new generic trace API
[openssl.git] / crypto / evp / p5_crpt2.c
1 /*
2  * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <stdlib.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/hmac.h>
17 #include <openssl/trace.h>
18 #include "internal/evp_int.h"
19 #include "evp_locl.h"
20
21 int PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
22                       const unsigned char *salt, int saltlen, int iter,
23                       const EVP_MD *digest, int keylen, unsigned char *out)
24 {
25     const char *empty = "";
26     int rv = 1;
27     EVP_KDF_CTX *kctx;
28
29     /* Keep documented behaviour. */
30     if (pass == NULL) {
31         pass = empty;
32         passlen = 0;
33     } else if (passlen == -1) {
34         passlen = strlen(pass);
35     }
36     if (salt == NULL && saltlen == 0)
37         salt = (unsigned char *)empty;
38
39     kctx = EVP_KDF_CTX_new_id(EVP_KDF_PBKDF2);
40     if (kctx == NULL)
41         return 0;
42     if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, pass, (size_t)passlen) != 1
43             || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
44                             salt, (size_t)saltlen) != 1
45             || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, iter) != 1
46             || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, digest) != 1
47             || EVP_KDF_derive(kctx, out, keylen) != 1)
48         rv = 0;
49
50     EVP_KDF_CTX_free(kctx);
51
52     OSSL_TRACE_BEGIN(PKCS5V2) {
53         BIO_printf(trc_out, "Password:\n");
54         BIO_hex_string(trc_out,
55                        0, passlen, pass, passlen);
56         BIO_printf(trc_out, "\n");
57         BIO_printf(trc_out, "Salt:\n");
58         BIO_hex_string(trc_out,
59                        0, saltlen, salt, saltlen);
60         BIO_printf(trc_out, "\n");
61         BIO_printf(trc_out, "Iteration count %d\n", iter);
62         BIO_printf(trc_out, "Key:\n");
63         BIO_hex_string(trc_out,
64                        0, keylen, out, keylen);
65         BIO_printf(trc_out, "\n");
66     } OSSL_TRACE_END(PKCS5V2);
67     return rv;
68 }
69
70 int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
71                            const unsigned char *salt, int saltlen, int iter,
72                            int keylen, unsigned char *out)
73 {
74     return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
75                              keylen, out);
76 }
77
78 /*
79  * Now the key derivation function itself. This is a bit evil because it has
80  * to check the ASN1 parameters are valid: and there are quite a few of
81  * them...
82  */
83
84 int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
85                           ASN1_TYPE *param, const EVP_CIPHER *c,
86                           const EVP_MD *md, int en_de)
87 {
88     PBE2PARAM *pbe2 = NULL;
89     const EVP_CIPHER *cipher;
90     EVP_PBE_KEYGEN *kdf;
91
92     int rv = 0;
93
94     pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
95     if (pbe2 == NULL) {
96         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
97         goto err;
98     }
99
100     /* See if we recognise the key derivation function */
101     if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
102                         NULL, NULL, &kdf)) {
103         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN,
104                EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
105         goto err;
106     }
107
108     /*
109      * lets see if we recognise the encryption algorithm.
110      */
111
112     cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
113
114     if (!cipher) {
115         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_UNSUPPORTED_CIPHER);
116         goto err;
117     }
118
119     /* Fixup cipher based on AlgorithmIdentifier */
120     if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
121         goto err;
122     if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
123         EVPerr(EVP_F_PKCS5_V2_PBE_KEYIVGEN, EVP_R_CIPHER_PARAMETER_ERROR);
124         goto err;
125     }
126     rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
127  err:
128     PBE2PARAM_free(pbe2);
129     return rv;
130 }
131
132 int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
133                              int passlen, ASN1_TYPE *param,
134                              const EVP_CIPHER *c, const EVP_MD *md, int en_de)
135 {
136     unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
137     int saltlen, iter;
138     int rv = 0;
139     unsigned int keylen = 0;
140     int prf_nid, hmac_md_nid;
141     PBKDF2PARAM *kdf = NULL;
142     const EVP_MD *prfmd;
143
144     if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
145         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_NO_CIPHER_SET);
146         goto err;
147     }
148     keylen = EVP_CIPHER_CTX_key_length(ctx);
149     OPENSSL_assert(keylen <= sizeof(key));
150
151     /* Decode parameter */
152
153     kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
154
155     if (kdf == NULL) {
156         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_DECODE_ERROR);
157         goto err;
158     }
159
160     keylen = EVP_CIPHER_CTX_key_length(ctx);
161
162     /* Now check the parameters of the kdf */
163
164     if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
165         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_KEYLENGTH);
166         goto err;
167     }
168
169     if (kdf->prf)
170         prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
171     else
172         prf_nid = NID_hmacWithSHA1;
173
174     if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
175         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
176         goto err;
177     }
178
179     prfmd = EVP_get_digestbynid(hmac_md_nid);
180     if (prfmd == NULL) {
181         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_PRF);
182         goto err;
183     }
184
185     if (kdf->salt->type != V_ASN1_OCTET_STRING) {
186         EVPerr(EVP_F_PKCS5_V2_PBKDF2_KEYIVGEN, EVP_R_UNSUPPORTED_SALT_TYPE);
187         goto err;
188     }
189
190     /* it seems that its all OK */
191     salt = kdf->salt->value.octet_string->data;
192     saltlen = kdf->salt->value.octet_string->length;
193     iter = ASN1_INTEGER_get(kdf->iter);
194     if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
195                            keylen, key))
196         goto err;
197     rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
198  err:
199     OPENSSL_cleanse(key, keylen);
200     PBKDF2PARAM_free(kdf);
201     return rv;
202 }