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