2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-29 07:17:34 +08:00
|
|
|
*
|
2018-12-06 20:49:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08: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-29 07:17:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/pkcs12.h>
|
2004-05-18 03:14:22 +08:00
|
|
|
#include <openssl/bn.h>
|
2018-12-13 15:07:25 +08:00
|
|
|
#include <openssl/trace.h>
|
2020-08-11 08:29:02 +08:00
|
|
|
#include <openssl/kdf.h>
|
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include "internal/provider.h"
|
1999-03-29 07:17:34 +08:00
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
int PKCS12_key_gen_asc_ex(const char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type,
|
|
|
|
OSSL_LIB_CTX *ctx, const char *propq)
|
1999-03-29 07:17:34 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
int ret;
|
|
|
|
unsigned char *unipass;
|
|
|
|
int uniplen;
|
2008-12-30 00:11:58 +08:00
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pass == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
unipass = NULL;
|
|
|
|
uniplen = 0;
|
|
|
|
} else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter,
|
|
|
|
n, out, md_type, ctx, propq);
|
2015-05-01 05:57:32 +08:00
|
|
|
OPENSSL_clear_free(unipass, uniplen);
|
2020-08-12 23:37:50 +08:00
|
|
|
return ret > 0;
|
1999-03-29 07:17:34 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
|
|
|
{
|
|
|
|
return PKCS12_key_gen_asc_ex(pass, passlen, salt, saltlen, id, iter, n,
|
|
|
|
out, md_type, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PKCS12_key_gen_utf8_ex(const char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type,
|
|
|
|
OSSL_LIB_CTX *ctx, const char *propq)
|
2015-11-25 06:34:51 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *unipass;
|
|
|
|
int uniplen;
|
|
|
|
|
2019-09-17 03:28:57 +08:00
|
|
|
if (pass == NULL) {
|
2015-11-25 06:34:51 +08:00
|
|
|
unipass = NULL;
|
|
|
|
uniplen = 0;
|
|
|
|
} else if (!OPENSSL_utf82uni(pass, passlen, &unipass, &uniplen)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
|
2015-11-25 06:34:51 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
ret = PKCS12_key_gen_uni_ex(unipass, uniplen, salt, saltlen, id, iter,
|
|
|
|
n, out, md_type, ctx, propq);
|
2015-11-25 06:34:51 +08:00
|
|
|
OPENSSL_clear_free(unipass, uniplen);
|
2020-08-12 23:37:50 +08:00
|
|
|
return ret > 0;
|
2015-11-25 06:34:51 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:56:36 +08: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)
|
|
|
|
{
|
|
|
|
return PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, id, iter, n,
|
|
|
|
out, md_type, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PKCS12_key_gen_uni_ex(unsigned char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
1999-03-29 07:17:34 +08:00
|
|
|
{
|
2020-08-11 08:29:02 +08:00
|
|
|
int res = 0;
|
|
|
|
EVP_KDF *kdf;
|
|
|
|
EVP_KDF_CTX *ctx;
|
|
|
|
OSSL_PARAM params[6], *p = params;
|
|
|
|
|
|
|
|
if (n <= 0)
|
|
|
|
return 0;
|
2000-03-08 06:35:27 +08:00
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
kdf = EVP_KDF_fetch(libctx, "PKCS12KDF", propq);
|
2020-08-11 08:29:02 +08:00
|
|
|
if (kdf == NULL)
|
|
|
|
return 0;
|
|
|
|
ctx = EVP_KDF_CTX_new(kdf);
|
|
|
|
EVP_KDF_free(kdf);
|
2015-11-27 21:02:12 +08:00
|
|
|
if (ctx == NULL)
|
2020-08-11 08:29:02 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
(char *)EVP_MD_get0_name(md_type),
|
|
|
|
0);
|
2020-08-11 08:29:02 +08:00
|
|
|
*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();
|
2015-11-27 21:02:12 +08:00
|
|
|
|
2018-12-13 15:07:25 +08:00
|
|
|
OSSL_TRACE_BEGIN(PKCS12_KEYGEN) {
|
2021-02-17 15:56:36 +08:00
|
|
|
BIO_printf(trc_out, "PKCS12_key_gen_uni_ex(): ID %d, ITER %d\n", id, iter);
|
2018-12-13 15:07:25 +08:00
|
|
|
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-02 04:19:41 +08:00
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
if (EVP_KDF_derive(ctx, out, (size_t)n, params)) {
|
2020-08-11 08:29:02 +08:00
|
|
|
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 11:40:55 +08:00
|
|
|
}
|
2020-08-11 08:29:02 +08:00
|
|
|
EVP_KDF_CTX_free(ctx);
|
|
|
|
return res;
|
1999-03-29 07:17:34 +08:00
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
|
|
|
|
int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
|
|
|
|
int saltlen, int id, int iter, int n,
|
|
|
|
unsigned char *out, const EVP_MD *md_type)
|
|
|
|
{
|
|
|
|
return PKCS12_key_gen_uni_ex(pass, passlen, salt, saltlen, id, iter, n, out, md_type, NULL, NULL);
|
|
|
|
}
|