2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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)
24 unsigned char *unipass;
30 } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
31 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
34 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
35 id, iter, n, out, md_type);
36 OPENSSL_clear_free(unipass, uniplen);
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)
45 unsigned char *unipass;
51 } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
52 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
55 ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
56 id, iter, n, out, md_type);
57 OPENSSL_clear_free(unipass, uniplen);
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)
68 OSSL_PARAM params[6], *p = params;
74 * The parameter query isn't available but the library context can be
75 * extracted from the passed digest.
77 kdf = EVP_KDF_fetch(ossl_provider_libctx(EVP_MD_provider(md_type)),
81 ctx = EVP_KDF_CTX_new(kdf);
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,
90 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
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))
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);
108 if (EVP_KDF_derive(ctx, out, (size_t)n, NULL)) {
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);
117 EVP_KDF_CTX_free(ctx);