2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1999-12-22 09:39:23 +08:00
|
|
|
*
|
2018-12-06 20:36:05 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +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-12-22 09:39:23 +08:00
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
|
1999-12-22 09:39:23 +08:00
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2000-12-09 03:09:35 +08:00
|
|
|
#include <openssl/bn.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "dh_local.h"
|
2000-12-09 03:09:35 +08:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/asn1t.h>
|
2021-02-17 11:13:51 +08:00
|
|
|
#include "crypto/dh.h"
|
1999-12-22 09:39:23 +08:00
|
|
|
|
2000-12-09 03:09:35 +08:00
|
|
|
/* Override the default free and new methods */
|
2005-09-02 04:42:52 +08:00
|
|
|
static int dh_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
|
2015-01-22 11:40:55 +08:00
|
|
|
void *exarg)
|
1999-12-22 09:39:23 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
if (operation == ASN1_OP_NEW_PRE) {
|
|
|
|
*pval = (ASN1_VALUE *)DH_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (*pval != NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
} else if (operation == ASN1_OP_FREE_PRE) {
|
|
|
|
DH_free((DH *)*pval);
|
|
|
|
*pval = NULL;
|
|
|
|
return 2;
|
2019-07-07 16:56:46 +08:00
|
|
|
} else if (operation == ASN1_OP_D2I_POST) {
|
2020-10-26 20:59:09 +08:00
|
|
|
DH *dh = (DH *)*pval;
|
|
|
|
|
|
|
|
DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
|
|
|
|
DH_set_flags(dh, DH_FLAG_TYPE_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
|
|
|
ossl_dh_cache_named_group(dh);
|
2020-10-26 20:59:09 +08:00
|
|
|
dh->dirty_cnt++;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
return 1;
|
1999-12-22 09:39:23 +08:00
|
|
|
}
|
|
|
|
|
2000-12-09 03:09:35 +08:00
|
|
|
ASN1_SEQUENCE_cb(DHparams, dh_cb) = {
|
2020-01-24 12:09:33 +08:00
|
|
|
ASN1_SIMPLE(DH, params.p, BIGNUM),
|
|
|
|
ASN1_SIMPLE(DH, params.g, BIGNUM),
|
2017-04-12 17:52:52 +08:00
|
|
|
ASN1_OPT_EMBED(DH, length, ZINT32),
|
2016-03-10 09:56:43 +08:00
|
|
|
} ASN1_SEQUENCE_END_cb(DH, DHparams)
|
2000-12-09 03:09:35 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DH, DHparams, DHparams)
|
2009-09-06 23:49:46 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Internal only structures for handling X9.42 DH: this gets translated to or
|
|
|
|
* from a DH structure straight away.
|
2011-12-07 08:32:34 +08:00
|
|
|
*/
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
typedef struct {
|
|
|
|
ASN1_BIT_STRING *seed;
|
|
|
|
BIGNUM *counter;
|
|
|
|
} int_dhvparams;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
BIGNUM *p;
|
|
|
|
BIGNUM *q;
|
|
|
|
BIGNUM *g;
|
|
|
|
BIGNUM *j;
|
|
|
|
int_dhvparams *vparams;
|
|
|
|
} int_dhx942_dh;
|
2011-12-07 08:32:34 +08:00
|
|
|
|
|
|
|
ASN1_SEQUENCE(DHvparams) = {
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_SIMPLE(int_dhvparams, seed, ASN1_BIT_STRING),
|
|
|
|
ASN1_SIMPLE(int_dhvparams, counter, BIGNUM)
|
2015-09-05 20:32:58 +08:00
|
|
|
} static_ASN1_SEQUENCE_END_name(int_dhvparams, DHvparams)
|
2011-12-07 08:32:34 +08:00
|
|
|
|
|
|
|
ASN1_SEQUENCE(DHxparams) = {
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_SIMPLE(int_dhx942_dh, p, BIGNUM),
|
|
|
|
ASN1_SIMPLE(int_dhx942_dh, g, BIGNUM),
|
|
|
|
ASN1_SIMPLE(int_dhx942_dh, q, BIGNUM),
|
|
|
|
ASN1_OPT(int_dhx942_dh, j, BIGNUM),
|
|
|
|
ASN1_OPT(int_dhx942_dh, vparams, DHvparams),
|
2015-09-05 20:32:58 +08:00
|
|
|
} static_ASN1_SEQUENCE_END_name(int_dhx942_dh, DHxparams)
|
2011-12-07 08:32:34 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
int_dhx942_dh *d2i_int_dhx(int_dhx942_dh **a,
|
|
|
|
const unsigned char **pp, long length);
|
|
|
|
int i2d_int_dhx(const int_dhx942_dh *a, unsigned char **pp);
|
2011-12-07 08:32:34 +08:00
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(int_dhx942_dh, DHxparams, int_dhx)
|
2011-12-07 08:32:34 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
DH *d2i_DHxparams(DH **a, const unsigned char **pp, long length)
|
|
|
|
{
|
2020-01-24 12:09:33 +08:00
|
|
|
FFC_PARAMS *params;
|
2015-01-22 11:40:55 +08:00
|
|
|
int_dhx942_dh *dhx = NULL;
|
|
|
|
DH *dh = NULL;
|
2020-02-06 20:28:36 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
dh = DH_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (dh == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
dhx = d2i_int_dhx(NULL, pp, length);
|
2015-10-30 19:12:26 +08:00
|
|
|
if (dhx == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
DH_free(dh);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
if (a != NULL) {
|
2015-03-24 22:17:37 +08:00
|
|
|
DH_free(*a);
|
2015-01-22 11:40:55 +08:00
|
|
|
*a = dh;
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
params = &dh->params;
|
2020-01-31 06:18:46 +08:00
|
|
|
DH_set0_pqg(dh, dhx->p, dhx->q, dhx->g);
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_set0_j(params, dhx->j);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-01-24 12:09:33 +08:00
|
|
|
if (dhx->vparams != NULL) {
|
|
|
|
/* The counter has a maximum value of 4 * numbits(p) - 1 */
|
|
|
|
size_t counter = (size_t)BN_get_word(dhx->vparams->counter);
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_set_validate_params(params, dhx->vparams->seed->data,
|
|
|
|
dhx->vparams->seed->length,
|
|
|
|
counter);
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_BIT_STRING_free(dhx->vparams->seed);
|
2020-01-24 12:09:33 +08:00
|
|
|
BN_free(dhx->vparams->counter);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(dhx->vparams);
|
|
|
|
dhx->vparams = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
OPENSSL_free(dhx);
|
2020-10-26 20:59:09 +08:00
|
|
|
DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
|
|
|
|
DH_set_flags(dh, DH_FLAG_TYPE_DHX);
|
2015-01-22 11:40:55 +08:00
|
|
|
return dh;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i2d_DHxparams(const DH *dh, unsigned char **pp)
|
|
|
|
{
|
2020-01-24 12:09:33 +08:00
|
|
|
int ret = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
int_dhx942_dh dhx;
|
2020-01-24 12:09:33 +08:00
|
|
|
int_dhvparams dhv = { NULL, NULL };
|
|
|
|
ASN1_BIT_STRING seed;
|
|
|
|
size_t seedlen = 0;
|
|
|
|
const FFC_PARAMS *params = &dh->params;
|
|
|
|
int counter;
|
|
|
|
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_get0_pqg(params, (const BIGNUM **)&dhx.p,
|
|
|
|
(const BIGNUM **)&dhx.q, (const BIGNUM **)&dhx.g);
|
2020-01-24 12:09:33 +08:00
|
|
|
dhx.j = params->j;
|
ffc: add _ossl to exported but internal functions
The functions updated are:
ffc_generate_private_key, ffc_named_group_from_uid,
ffc_named_group_to_uid, ffc_params_FIPS186_2_gen_verify,
ffc_params_FIPS186_2_generate, ffc_params_FIPS186_2_validate,
ffc_params_FIPS186_4_gen_verify, ffc_params_FIPS186_4_generate,
ffc_params_FIPS186_4_validate, ffc_params_cleanup, ffc_params_cmp,
ffc_params_copy, ffc_params_enable_flags, ffc_params_flags_from_name,
ffc_params_flags_to_name, ffc_params_fromdata,
ffc_params_get0_pqg, ffc_params_get_validate_params,
ffc_params_init, ffc_params_print, ffc_params_set0_j,
ffc_params_set0_pqg, ffc_params_set_flags, ffc_params_set_gindex,
ffc_params_set_h, ffc_params_set_pcounter, ffc_params_set_seed,
ffc_params_set_validate_params, ffc_params_simple_validate,
ffc_params_todata, ffc_params_validate_unverifiable_g, ffc_set_digest,
ffc_set_group_pqg, ffc_validate_private_key, ffc_validate_public_key
and ffc_validate_public_key_partial.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13041)
2020-09-30 13:07:24 +08:00
|
|
|
ossl_ffc_params_get_validate_params(params, &seed.data, &seedlen, &counter);
|
2020-01-24 12:09:33 +08:00
|
|
|
seed.length = (int)seedlen;
|
|
|
|
|
|
|
|
if (counter != -1 && seed.data != NULL && seed.length > 0) {
|
|
|
|
seed.flags = ASN1_STRING_FLAG_BITS_LEFT;
|
|
|
|
dhv.seed = &seed;
|
|
|
|
dhv.counter = BN_new();
|
|
|
|
if (dhv.counter == NULL)
|
|
|
|
return 0;
|
|
|
|
if (!BN_set_word(dhv.counter, (BN_ULONG)counter))
|
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
dhx.vparams = &dhv;
|
2020-01-24 12:09:33 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
dhx.vparams = NULL;
|
2020-01-24 12:09:33 +08:00
|
|
|
}
|
|
|
|
ret = i2d_int_dhx(&dhx, pp);
|
|
|
|
err:
|
|
|
|
BN_free(dhv.counter);
|
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|