2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 14:52:22 -04:00
|
|
|
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-28 23:17:34 +00:00
|
|
|
*
|
2018-12-06 13:49:51 +01:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 14:52:22 -04:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-03-28 23:17:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 16:56:48 +02:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/pkcs12.h>
|
2004-05-17 19:14:22 +00:00
|
|
|
#include <openssl/bn.h>
|
2018-12-13 08:07:25 +01:00
|
|
|
#include <openssl/trace.h>
|
2020-08-11 10:29:02 +10:00
|
|
|
#include <openssl/kdf.h>
|
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include "internal/provider.h"
|
1999-03-28 23:17:34 +00:00
|
|
|
|
2000-05-04 00:08:35 +00:00
|
|
|
int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
|
2015-01-22 03:40:55 +00:00
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
int ret;
|
|
|
|
unsigned char *unipass;
|
|
|
|
int uniplen;
|
2008-12-29 16:11:58 +00:00
|
|
|
|
2019-09-16 15:28:57 -04:00
|
|
|
if (pass == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
unipass = NULL;
|
|
|
|
uniplen = 0;
|
|
|
|
} else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
|
|
|
|
id, iter, n, out, md_type);
|
2015-04-30 17:57:32 -04:00
|
|
|
OPENSSL_clear_free(unipass, uniplen);
|
2020-08-12 17:37:50 +02:00
|
|
|
return ret > 0;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|
|
|
|
|
2015-11-24 23:34:51 +01:00
|
|
|
int PKCS12_key_gen_utf8(const char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *unipass;
|
|
|
|
int uniplen;
|
|
|
|
|
2019-09-16 15:28:57 -04:00
|
|
|
if (pass == NULL) {
|
2015-11-24 23:34:51 +01:00
|
|
|
unipass = NULL;
|
|
|
|
uniplen = 0;
|
|
|
|
} else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UTF8, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
|
|
|
|
id, iter, n, out, md_type);
|
|
|
|
OPENSSL_clear_free(unipass, uniplen);
|
2020-08-12 17:37:50 +02:00
|
|
|
return ret > 0;
|
2015-11-24 23:34:51 +01:00
|
|
|
}
|
|
|
|
|
2000-05-04 00:08:35 +00:00
|
|
|
int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
|
2015-01-22 03:40:55 +00:00
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2020-08-11 10:29:02 +10:00
|
|
|
int res = 0;
|
|
|
|
EVP_KDF *kdf;
|
|
|
|
EVP_KDF_CTX *ctx;
|
|
|
|
OSSL_PARAM params[6], *p = params;
|
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
return 0;
|
2000-03-07 22:35:27 +00:00
|
|
|
|
2020-08-11 10:29:02 +10:00
|
|
|
/*
|
|
|
|
* The parameter query isn't available but the library context can be
|
|
|
|
* extracted from the passed digest.
|
|
|
|
*/
|
|
|
|
kdf = EVP_KDF_fetch(ossl_provider_library_context(EVP_MD_provider(md_type)),
|
|
|
|
"PKCS12KDF", NULL);
|
|
|
|
if (kdf == NULL)
|
|
|
|
return 0;
|
|
|
|
ctx = EVP_KDF_CTX_new(kdf);
|
|
|
|
EVP_KDF_free(kdf);
|
2015-11-27 14:02:12 +01:00
|
|
|
if (ctx == NULL)
|
2020-08-11 10:29:02 +10:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
|
|
|
|
(char *)EVP_MD_name(md_type), 0);
|
|
|
|
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
|
|
|
|
pass, passlen);
|
|
|
|
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
|
|
|
|
salt, saltlen);
|
|
|
|
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS12_ID, &id);
|
|
|
|
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
|
|
|
|
*p = OSSL_PARAM_construct_end();
|
|
|
|
if (!EVP_KDF_CTX_set_params(ctx, params))
|
2015-11-27 14:02:12 +01:00
|
|
|
goto err;
|
|
|
|
|
2018-12-13 08:07:25 +01:00
|
|
|
OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
|
|
|
|
BIO_printf(trc_out, "PKCS12_key_gen_uni(): ID %d, ITER %d\n", id, iter);
|
|
|
|
BIO_printf(trc_out, "Password (length %d):\n", passlen);
|
|
|
|
BIO_hex_string(trc_out, 0, passlen, pass, passlen);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
BIO_printf(trc_out, "Salt (length %d):\n", saltlen);
|
|
|
|
BIO_hex_string(trc_out, 0, saltlen, salt, saltlen);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
} OSSL_TRACE_END(PKCS12_KEYGEN);
|
2016-10-01 21:19:41 +01:00
|
|
|
|
2020-08-11 10:29:02 +10:00
|
|
|
if (EVP_KDF_derive(ctx, out, (size_t)n)) {
|
|
|
|
res = 1;
|
|
|
|
OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
|
|
|
|
BIO_printf(trc_out, "Output KEY (length %d)\n", n);
|
|
|
|
BIO_hex_string(trc_out, 0, n, out, n);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
} OSSL_TRACE_END(PKCS12_KEYGEN);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
err:
|
2020-08-11 10:29:02 +10:00
|
|
|
EVP_KDF_CTX_free(ctx);
|
|
|
|
return res;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|