]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_key.c
a4ed0e516c421d108cbfae11f639226ef70f1683
[thirdparty/openssl.git] / crypto / pkcs12 / p12_key.c
1 /*
2 * Copyright 1999-2021 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_ex(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 OSSL_LIB_CTX *ctx, const char *propq)
23 {
24 int ret;
25 unsigned char *unipass;
26 int uniplen;
27
28 if (pass == NULL) {
29 unipass = NULL;
30 uniplen = 0;
31 } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
32 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
33 return 0;
34 }
35 ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter,
36 n, out, md_type, ctx, propq);
37 OPENSSL_clear_free(unipass, uniplen);
38 return ret > 0;
39 }
40
41 int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
42 int saltlen, int id, int iter, int n,
43 unsigned char *out, const EVP_MD *md_type)
44 {
45 return PKCS12_key_gen_asc_ex(pass, passlen, salt, saltlen, id, iter, n,
46 out, md_type, NULL, NULL);
47 }
48
49 int PKCS12_key_gen_utf8_ex(const char *pass, int passlen, unsigned char *salt,
50 int saltlen, int id, int iter, int n,
51 unsigned char *out, const EVP_MD *md_type,
52 OSSL_LIB_CTX *ctx, const char *propq)
53 {
54 int ret;
55 unsigned char *unipass;
56 int uniplen;
57
58 if (pass == NULL) {
59 unipass = NULL;
60 uniplen = 0;
61 } else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
62 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
63 return 0;
64 }
65 ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter,
66 n, out, md_type, ctx, propq);
67 OPENSSL_clear_free(unipass, uniplen);
68 return ret > 0;
69 }
70
71 int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
72 int saltlen, int id, int iter, int n,
73 unsigned char *out, const EVP_MD *md_type)
74 {
75 return PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, id, iter, n,
76 out, md_type, NULL, NULL);
77 }
78
79 int PKCS12_key_gen_uni_ex(unsigned char *pass, int passlen, unsigned char *salt,
80 int saltlen, int id, int iter, int n,
81 unsigned char *out, const EVP_MD *md_type,
82 OSSL_LIB_CTX *libctx, const char *propq)
83 {
84 int res = 0;
85 EVP_KDF *kdf;
86 EVP_KDF_CTX *ctx;
87 OSSL_PARAM params[6], *p = params;
88
89 if (n <= 0)
90 return 0;
91
92 kdf = EVP_KDF_fetch(libctx, "PKCS12KDF", propq);
93 if (kdf == NULL)
94 return 0;
95 ctx = EVP_KDF_CTX_new(kdf);
96 EVP_KDF_free(kdf);
97 if (ctx == NULL)
98 return 0;
99
100 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
101 (char *)EVP_MD_name(md_type), 0);
102 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
103 pass, passlen);
104 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
105 salt, saltlen);
106 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS12_ID, &id);
107 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
108 *p = OSSL_PARAM_construct_end();
109
110 OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
111 BIO_printf(trc_out, "PKCS12_key_gen_uni_ex(): ID %d, ITER %d\n", id, iter);
112 BIO_printf(trc_out, "Password (length %d):\n", passlen);
113 BIO_hex_string(trc_out, 0, passlen, pass, passlen);
114 BIO_printf(trc_out, "\n");
115 BIO_printf(trc_out, "Salt (length %d):\n", saltlen);
116 BIO_hex_string(trc_out, 0, saltlen, salt, saltlen);
117 BIO_printf(trc_out, "\n");
118 } OSSL_TRACE_END(PKCS12_KEYGEN);
119
120 if (EVP_KDF_derive(ctx, out, (size_t)n, params)) {
121 res = 1;
122 OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
123 BIO_printf(trc_out, "Output KEY (length %d)\n", n);
124 BIO_hex_string(trc_out, 0, n, out, n);
125 BIO_printf(trc_out, "\n");
126 } OSSL_TRACE_END(PKCS12_KEYGEN);
127 }
128 EVP_KDF_CTX_free(ctx);
129 return res;
130 }
131
132 int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
133 int saltlen, int id, int iter, int n,
134 unsigned char *out, const EVP_MD *md_type)
135 {
136 return PKCS12_key_gen_uni_ex(pass, passlen, salt, saltlen, id, iter, n, out, md_type, NULL, NULL);
137 }