2019-11-18 08:57:56 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2019-11-18 08:57:56 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-02-03 17:05:31 +08:00
|
|
|
/*
|
|
|
|
* DH low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2019-11-18 08:57:56 +08:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include "prov/bio.h" /* ossl_prov_bio_printf() */
|
|
|
|
#include "prov/implementations.h" /* rsa_keymgmt_functions */
|
|
|
|
#include "prov/providercommonerr.h" /* PROV_R_BN_ERROR */
|
2020-04-15 23:14:00 +08:00
|
|
|
#include "internal/ffc.h"
|
|
|
|
#include "crypto/dh.h"
|
2020-08-17 03:25:08 +08:00
|
|
|
#include "encoder_local.h"
|
2019-11-18 08:57:56 +08:00
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_keymgmt_new_fn *ossl_prov_get_keymgmt_dh_new(void)
|
2019-11-18 08:57:56 +08:00
|
|
|
{
|
2020-02-03 23:36:24 +08:00
|
|
|
return ossl_prov_get_keymgmt_new(dh_keymgmt_functions);
|
|
|
|
}
|
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_keymgmt_free_fn *ossl_prov_get_keymgmt_dh_free(void)
|
2020-02-03 23:36:24 +08:00
|
|
|
{
|
|
|
|
return ossl_prov_get_keymgmt_free(dh_keymgmt_functions);
|
|
|
|
}
|
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_keymgmt_import_fn *ossl_prov_get_keymgmt_dh_import(void)
|
2020-02-03 23:36:24 +08:00
|
|
|
{
|
|
|
|
return ossl_prov_get_keymgmt_import(dh_keymgmt_functions);
|
2019-11-18 08:57:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_print_dh(BIO *out, DH *dh, enum dh_print_type type)
|
|
|
|
{
|
|
|
|
const char *type_label = NULL;
|
|
|
|
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
2020-04-15 23:14:00 +08:00
|
|
|
const BIGNUM *p = NULL;
|
2019-11-18 08:57:56 +08:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case dh_print_priv:
|
|
|
|
type_label = "DH Private-Key";
|
|
|
|
break;
|
|
|
|
case dh_print_pub:
|
|
|
|
type_label = "DH Public-Key";
|
|
|
|
break;
|
|
|
|
case dh_print_params:
|
|
|
|
type_label = "DH Parameters";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == dh_print_priv) {
|
|
|
|
priv_key = DH_get0_priv_key(dh);
|
|
|
|
if (priv_key == NULL)
|
|
|
|
goto null_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == dh_print_priv || type == dh_print_pub) {
|
|
|
|
pub_key = DH_get0_pub_key(dh);
|
|
|
|
if (pub_key == NULL)
|
|
|
|
goto null_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = DH_get0_p(dh);
|
2020-04-15 23:14:00 +08:00
|
|
|
if (p == NULL)
|
2019-11-18 08:57:56 +08:00
|
|
|
goto null_err;
|
|
|
|
|
2020-05-06 19:29:57 +08:00
|
|
|
if (BIO_printf(out, "%s: (%d bit)\n", type_label, BN_num_bits(p))
|
2019-11-18 08:57:56 +08:00
|
|
|
<= 0)
|
|
|
|
goto err;
|
|
|
|
if (priv_key != NULL
|
2020-04-15 23:14:00 +08:00
|
|
|
&& !ossl_prov_print_labeled_bignum(out, "private-key:", priv_key))
|
2019-11-18 08:57:56 +08:00
|
|
|
goto err;
|
|
|
|
if (pub_key != NULL
|
2020-04-15 23:14:00 +08:00
|
|
|
&& !ossl_prov_print_labeled_bignum(out, "public-key:", pub_key))
|
2019-11-18 08:57:56 +08:00
|
|
|
goto err;
|
2020-04-15 23:14:00 +08:00
|
|
|
if (!ffc_params_prov_print(out, dh_get0_params(dh)))
|
2019-11-18 08:57:56 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
err:
|
|
|
|
return 0;
|
|
|
|
null_err:
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_prepare_dh_params(const void *dh, int nid,
|
2020-02-16 17:54:08 +08:00
|
|
|
void **pstr, int *pstrtype)
|
2019-11-18 08:57:56 +08:00
|
|
|
{
|
|
|
|
ASN1_STRING *params = ASN1_STRING_new();
|
|
|
|
|
|
|
|
if (params == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nid == EVP_PKEY_DHX)
|
|
|
|
params->length = i2d_DHxparams(dh, ¶ms->data);
|
|
|
|
else
|
|
|
|
params->length = i2d_DHparams(dh, ¶ms->data);
|
|
|
|
|
|
|
|
if (params->length <= 0) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
|
|
|
ASN1_STRING_free(params);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
params->type = V_ASN1_SEQUENCE;
|
|
|
|
|
|
|
|
*pstr = params;
|
|
|
|
*pstrtype = V_ASN1_SEQUENCE;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder)
|
|
|
|
{
|
2020-07-30 16:14:27 +08:00
|
|
|
const BIGNUM *bn = NULL;
|
|
|
|
ASN1_INTEGER *pub_key = NULL;
|
2019-11-18 08:57:56 +08:00
|
|
|
int ret;
|
|
|
|
|
2020-07-30 16:14:27 +08:00
|
|
|
if ((bn = DH_get0_pub_key(dh)) == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
|
2019-11-18 08:57:56 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = i2d_ASN1_INTEGER(pub_key, pder);
|
|
|
|
|
|
|
|
ASN1_STRING_clear_free(pub_key);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder)
|
|
|
|
{
|
2020-07-30 16:14:27 +08:00
|
|
|
const BIGNUM *bn = NULL;
|
|
|
|
ASN1_INTEGER *priv_key = NULL;
|
2019-11-18 08:57:56 +08:00
|
|
|
int ret;
|
|
|
|
|
2020-07-30 16:14:27 +08:00
|
|
|
if ((bn = DH_get0_priv_key(dh)) == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
|
2019-11-18 08:57:56 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = i2d_ASN1_INTEGER(priv_key, pder);
|
|
|
|
|
|
|
|
ASN1_STRING_clear_free(priv_key);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-04 09:39:49 +08:00
|
|
|
|
|
|
|
int ossl_prov_dh_type_to_evp(const DH *dh)
|
|
|
|
{
|
|
|
|
return DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH;
|
|
|
|
}
|