crypto: add additional argument to KDF derive calls
[openssl.git] / crypto / pkcs12 / p12_key.c
1 /*
2  * Copyright 1999-2020 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 "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include <openssl/bn.h>
14 #include <openssl/trace.h>
15 #include <openssl/kdf.h>
16 #include <openssl/core_names.h>
17 #include "internal/provider.h"
18
19 int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
20                        int saltlen, int id, int iter, int n,
21                        unsigned char *out, const EVP_MD *md_type)
22 {
23     int ret;
24     unsigned char *unipass;
25     int uniplen;
26
27     if (pass == NULL) {
28         unipass = NULL;
29         uniplen = 0;
30     } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
31         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
32         return 0;
33     }
34     ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
35                              id, iter, n, out, md_type);
36     OPENSSL_clear_free(unipass, uniplen);
37     return ret > 0;
38 }
39
40 int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
41                         int saltlen, int id, int iter, int n,
42                         unsigned char *out, const EVP_MD *md_type)
43 {
44     int ret;
45     unsigned char *unipass;
46     int uniplen;
47
48     if (pass == NULL) {
49         unipass = NULL;
50         uniplen = 0;
51     } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
52         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
53         return 0;
54     }
55     ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
56                              id, iter, n, out, md_type);
57     OPENSSL_clear_free(unipass, uniplen);
58     return ret > 0;
59 }
60
61 int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
62                        int saltlen, int id, int iter, int n,
63                        unsigned char *out, const EVP_MD *md_type)
64 {
65     int res = 0;
66     EVP_KDF *kdf;
67     EVP_KDF_CTX *ctx;
68     OSSL_PARAM params[6], *p = params;
69
70     if (n <= 0)
71         return 0;
72
73     /*
74      * The parameter query isn't available but the library context can be
75      * extracted from the passed digest.
76      */
77     kdf = EVP_KDF_fetch(ossl_provider_libctx(EVP_MD_provider(md_type)),
78                         "PKCS12KDF", NULL);
79     if (kdf == NULL)
80         return 0;
81     ctx = EVP_KDF_CTX_new(kdf);
82     EVP_KDF_free(kdf);
83     if (ctx == NULL)
84         return 0;
85
86     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
87                                             (char *)EVP_MD_name(md_type), 0);
88     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
89                                              pass, passlen);
90     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
91                                              salt, saltlen);
92     *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS12_ID, &id);
93     *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
94     *p = OSSL_PARAM_construct_end();
95     if (!EVP_KDF_CTX_set_params(ctx, params))
96         goto err;
97
98     OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
99         BIO_printf(trc_out, "PKCS12_key_gen_uni(): ID %d, ITER %d\n", id, iter);
100         BIO_printf(trc_out, "Password (length %d):\n", passlen);
101         BIO_hex_string(trc_out, 0, passlen, pass, passlen);
102         BIO_printf(trc_out, "\n");
103         BIO_printf(trc_out, "Salt (length %d):\n", saltlen);
104         BIO_hex_string(trc_out, 0, saltlen, salt, saltlen);
105         BIO_printf(trc_out, "\n");
106     } OSSL_TRACE_END(PKCS12_KEYGEN);
107
108     if (EVP_KDF_derive(ctx, out, (size_t)n, NULL)) {
109         res = 1;
110         OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
111             BIO_printf(trc_out, "Output KEY (length %d)\n", n);
112             BIO_hex_string(trc_out, 0, n, out, n);
113             BIO_printf(trc_out, "\n");
114         } OSSL_TRACE_END(PKCS12_KEYGEN);
115     }
116  err:
117     EVP_KDF_CTX_free(ctx);
118     return res;
119 }