2020-01-31 06:18:46 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2020-01-31 06:18:46 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* DH parameters from RFC7919 and RFC3526 */
|
|
|
|
|
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"
|
|
|
|
|
2020-01-31 06:18:46 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "internal/cryptlib.h"
|
2020-04-15 23:14:00 +08:00
|
|
|
#include "internal/ffc.h"
|
2020-01-31 06:18:46 +08:00
|
|
|
#include "dh_local.h"
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/objects.h>
|
2020-12-02 02:11:59 +08:00
|
|
|
#include "internal/nelem.h"
|
2020-02-16 11:03:46 +08:00
|
|
|
#include "crypto/dh.h"
|
2020-03-07 05:47:58 +08:00
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
static DH *dh_param_init(OSSL_LIB_CTX *libctx, const DH_NAMED_GROUP *group)
|
2020-01-31 06:18:46 +08:00
|
|
|
{
|
Fix external symbols related to dh keys
Partial fix for #12964
This adds ossl_ names for the following symbols:
dh_new_by_nid_ex, dh_new_ex, dh_generate_ffc_parameters, dh_generate_public_key,
dh_get_named_group_uid_from_size, dh_gen_type_id2name, dh_gen_type_name2id,
dh_cache_named_group, dh_get0_params, dh_get0_nid,
dh_params_fromdata, dh_key_fromdata, dh_params_todata, dh_key_todata,
dh_check_pub_key_partial, dh_check_priv_key, dh_check_pairwise,
dh_get_method, dh_buf2key, dh_key2buf, dh_KDF_X9_42_asn1,
dh_pkey_method, dhx_pkey_method
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14231)
2021-02-18 13:56:53 +08:00
|
|
|
DH *dh = ossl_dh_new_ex(libctx);
|
2020-01-31 06:18:46 +08:00
|
|
|
|
|
|
|
if (dh == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2022-06-06 16:22:00 +08:00
|
|
|
ossl_ffc_named_group_set(&dh->params, group);
|
2020-12-02 02:11:59 +08:00
|
|
|
dh->params.nid = ossl_ffc_named_group_get_uid(group);
|
2020-01-31 06:18:46 +08:00
|
|
|
dh->dirty_cnt++;
|
|
|
|
return dh;
|
|
|
|
}
|
|
|
|
|
Fix external symbols related to dh keys
Partial fix for #12964
This adds ossl_ names for the following symbols:
dh_new_by_nid_ex, dh_new_ex, dh_generate_ffc_parameters, dh_generate_public_key,
dh_get_named_group_uid_from_size, dh_gen_type_id2name, dh_gen_type_name2id,
dh_cache_named_group, dh_get0_params, dh_get0_nid,
dh_params_fromdata, dh_key_fromdata, dh_params_todata, dh_key_todata,
dh_check_pub_key_partial, dh_check_priv_key, dh_check_pairwise,
dh_get_method, dh_buf2key, dh_key2buf, dh_KDF_X9_42_asn1,
dh_pkey_method, dhx_pkey_method
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14231)
2021-02-18 13:56:53 +08:00
|
|
|
DH *ossl_dh_new_by_nid_ex(OSSL_LIB_CTX *libctx, int nid)
|
2020-01-31 06:18:46 +08:00
|
|
|
{
|
2020-12-02 02:11:59 +08:00
|
|
|
const DH_NAMED_GROUP *group;
|
2020-03-07 05:47:58 +08:00
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
if ((group = ossl_ffc_uid_to_dh_named_group(nid)) != NULL)
|
|
|
|
return dh_param_init(libctx, group);
|
2020-04-15 23:14:00 +08:00
|
|
|
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DH, DH_R_INVALID_PARAMETER_NID);
|
2020-03-07 05:47:58 +08:00
|
|
|
return NULL;
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|
|
|
|
|
2020-02-16 11:03:46 +08:00
|
|
|
DH *DH_new_by_nid(int nid)
|
|
|
|
{
|
Fix external symbols related to dh keys
Partial fix for #12964
This adds ossl_ names for the following symbols:
dh_new_by_nid_ex, dh_new_ex, dh_generate_ffc_parameters, dh_generate_public_key,
dh_get_named_group_uid_from_size, dh_gen_type_id2name, dh_gen_type_name2id,
dh_cache_named_group, dh_get0_params, dh_get0_nid,
dh_params_fromdata, dh_key_fromdata, dh_params_todata, dh_key_todata,
dh_check_pub_key_partial, dh_check_priv_key, dh_check_pairwise,
dh_get_method, dh_buf2key, dh_key2buf, dh_KDF_X9_42_asn1,
dh_pkey_method, dhx_pkey_method
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14231)
2021-02-18 13:56:53 +08:00
|
|
|
return ossl_dh_new_by_nid_ex(NULL, nid);
|
2020-04-15 23:14:00 +08:00
|
|
|
}
|
|
|
|
|
Fix external symbols related to dh keys
Partial fix for #12964
This adds ossl_ names for the following symbols:
dh_new_by_nid_ex, dh_new_ex, dh_generate_ffc_parameters, dh_generate_public_key,
dh_get_named_group_uid_from_size, dh_gen_type_id2name, dh_gen_type_name2id,
dh_cache_named_group, dh_get0_params, dh_get0_nid,
dh_params_fromdata, dh_key_fromdata, dh_params_todata, dh_key_todata,
dh_check_pub_key_partial, dh_check_priv_key, dh_check_pairwise,
dh_get_method, dh_buf2key, dh_key2buf, dh_KDF_X9_42_asn1,
dh_pkey_method, dhx_pkey_method
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14231)
2021-02-18 13:56:53 +08:00
|
|
|
void ossl_dh_cache_named_group(DH *dh)
|
2020-01-31 06:18:46 +08:00
|
|
|
{
|
2020-12-02 02:11:59 +08:00
|
|
|
const DH_NAMED_GROUP *group;
|
2020-03-07 05:47:58 +08:00
|
|
|
|
|
|
|
if (dh == NULL)
|
2020-04-20 09:07:38 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
dh->params.nid = NID_undef; /* flush cached value */
|
2020-01-31 06:18:46 +08:00
|
|
|
|
2020-04-20 09:07:38 +08:00
|
|
|
/* Exit if p or g is not set */
|
|
|
|
if (dh->params.p == NULL
|
|
|
|
|| dh->params.g == NULL)
|
|
|
|
return;
|
2020-01-31 06:18:46 +08:00
|
|
|
|
2020-12-02 02:11:59 +08:00
|
|
|
if ((group = ossl_ffc_numbers_to_dh_named_group(dh->params.p,
|
|
|
|
dh->params.q,
|
|
|
|
dh->params.g)) != NULL) {
|
|
|
|
if (dh->params.q == NULL)
|
|
|
|
dh->params.q = (BIGNUM *)ossl_ffc_named_group_get_q(group);
|
2022-06-06 16:22:00 +08:00
|
|
|
/* cache the nid and default key length */
|
2020-12-02 02:11:59 +08:00
|
|
|
dh->params.nid = ossl_ffc_named_group_get_uid(group);
|
2022-06-06 16:22:00 +08:00
|
|
|
dh->params.keylength = ossl_ffc_named_group_get_keylength(group);
|
2020-12-02 02:11:59 +08:00
|
|
|
dh->dirty_cnt++;
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|
2020-04-20 09:07:38 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 11:00:34 +08:00
|
|
|
int ossl_dh_is_named_safe_prime_group(const DH *dh)
|
|
|
|
{
|
|
|
|
int id = DH_get_nid(dh);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exclude RFC5114 groups (id = 1..3) since they do not have
|
|
|
|
* q = (p - 1) / 2
|
|
|
|
*/
|
|
|
|
return (id > 3);
|
|
|
|
}
|
|
|
|
|
2020-04-20 09:07:38 +08:00
|
|
|
int DH_get_nid(const DH *dh)
|
|
|
|
{
|
|
|
|
if (dh == NULL)
|
|
|
|
return NID_undef;
|
|
|
|
|
|
|
|
return dh->params.nid;
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|