/*
- * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * 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 low level APIs are deprecated for public use, but still ok for
+ * internal use.
+ */
+#include "internal/deprecated.h"
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "e_os.h"
+#include "internal/nelem.h"
#include <openssl/crypto.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <openssl/err.h>
+#include <openssl/obj_mac.h>
+#include <openssl/core_names.h>
#include "testutil.h"
-#ifdef OPENSSL_NO_DH
-int main(int argc, char *argv[])
-{
- printf("No DH support\n");
- return (0);
-}
-#else
+#ifndef OPENSSL_NO_DH
# include <openssl/dh.h>
+# include "crypto/bn_dh.h"
+# include "crypto/dh.h"
static int cb(int p, int n, BN_GENCB *arg);
-static const char rnd_seed[] =
- "string to make the random number generator think it has randomness";
-
static int dh_test(void)
{
+ DH *dh = NULL;
+ BIGNUM *p = NULL, *q = NULL, *g = NULL;
+ const BIGNUM *p2, *q2, *g2;
+ BIGNUM *priv_key = NULL;
+ const BIGNUM *pub_key2, *priv_key2;
BN_GENCB *_cb = NULL;
DH *a = NULL;
DH *b = NULL;
+ DH *c = NULL;
const BIGNUM *ap = NULL, *ag = NULL, *apub_key = NULL;
- const BIGNUM *bpub_key = NULL;
- BIGNUM *bp = NULL, *bg = NULL;
+ const BIGNUM *bpub_key = NULL, *bpriv_key = NULL;
+ BIGNUM *bp = NULL, *bg = NULL, *cpriv_key = NULL;
unsigned char *abuf = NULL;
unsigned char *bbuf = NULL;
- int i, alen, blen, aout, bout;
+ unsigned char *cbuf = NULL;
+ int i, alen, blen, clen, aout, bout, cout;
int ret = 0;
- RAND_seed(rnd_seed, sizeof rnd_seed);
+ if (!TEST_ptr(dh = DH_new())
+ || !TEST_ptr(p = BN_new())
+ || !TEST_ptr(q = BN_new())
+ || !TEST_ptr(g = BN_new())
+ || !TEST_ptr(priv_key = BN_new()))
+ goto err1;
+
+ /*
+ * I) basic tests
+ */
+
+ /* using a small predefined Sophie Germain DH group with generator 3 */
+ if (!TEST_true(BN_set_word(p, 4079L))
+ || !TEST_true(BN_set_word(q, 2039L))
+ || !TEST_true(BN_set_word(g, 3L))
+ || !TEST_true(DH_set0_pqg(dh, p, q, g)))
+ goto err1;
+
+ /* check fails, because p is way too small */
+ if (!DH_check(dh, &i))
+ goto err2;
+ i ^= DH_MODULUS_TOO_SMALL;
+ if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
+ || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
+ || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
+ || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR)
+ || !TEST_false(i & DH_CHECK_Q_NOT_PRIME)
+ || !TEST_false(i & DH_CHECK_INVALID_Q_VALUE)
+ || !TEST_false(i & DH_CHECK_INVALID_J_VALUE)
+ || !TEST_false(i & DH_MODULUS_TOO_SMALL)
+ || !TEST_false(i & DH_MODULUS_TOO_LARGE)
+ || !TEST_false(i))
+ goto err2;
+
+ /* test the combined getter for p, q, and g */
+ DH_get0_pqg(dh, &p2, &q2, &g2);
+ if (!TEST_ptr_eq(p2, p)
+ || !TEST_ptr_eq(q2, q)
+ || !TEST_ptr_eq(g2, g))
+ goto err2;
+
+ /* test the simple getters for p, q, and g */
+ if (!TEST_ptr_eq(DH_get0_p(dh), p2)
+ || !TEST_ptr_eq(DH_get0_q(dh), q2)
+ || !TEST_ptr_eq(DH_get0_g(dh), g2))
+ goto err2;
+
+ /* set the private key only*/
+ if (!TEST_true(BN_set_word(priv_key, 1234L))
+ || !TEST_true(DH_set0_key(dh, NULL, priv_key)))
+ goto err2;
+
+ /* test the combined getter for pub_key and priv_key */
+ DH_get0_key(dh, &pub_key2, &priv_key2);
+ if (!TEST_ptr_eq(pub_key2, NULL)
+ || !TEST_ptr_eq(priv_key2, priv_key))
+ goto err3;
+
+ /* test the simple getters for pub_key and priv_key */
+ if (!TEST_ptr_eq(DH_get0_pub_key(dh), pub_key2)
+ || !TEST_ptr_eq(DH_get0_priv_key(dh), priv_key2))
+ goto err3;
+
+ /* now generate a key pair (expect failure since modulus is too small) */
+ if (!TEST_false(DH_generate_key(dh)))
+ goto err3;
+
+ /* We'll have a stale error on the queue from the above test so clear it */
+ ERR_clear_error();
+
+ /*
+ * II) key generation
+ */
+ /* generate a DH group ... */
if (!TEST_ptr(_cb = BN_GENCB_new()))
- goto err;
+ goto err3;
BN_GENCB_set(_cb, &cb, NULL);
if (!TEST_ptr(a = DH_new())
- || !TEST_true(DH_generate_parameters_ex(a, 64,
+ || !TEST_true(DH_generate_parameters_ex(a, 512,
DH_GENERATOR_5, _cb)))
- goto err;
+ goto err3;
+ /* ... and check whether it is valid */
if (!DH_check(a, &i))
- goto err;
+ goto err3;
if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
|| !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
|| !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
- || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR))
- goto err;
+ || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR)
+ || !TEST_false(i & DH_CHECK_Q_NOT_PRIME)
+ || !TEST_false(i & DH_CHECK_INVALID_Q_VALUE)
+ || !TEST_false(i & DH_CHECK_INVALID_J_VALUE)
+ || !TEST_false(i & DH_MODULUS_TOO_SMALL)
+ || !TEST_false(i & DH_MODULUS_TOO_LARGE)
+ || !TEST_false(i))
+ goto err3;
DH_get0_pqg(a, &ap, NULL, &ag);
+ /* now create another copy of the DH group for the peer */
if (!TEST_ptr(b = DH_new()))
- goto err;
+ goto err3;
if (!TEST_ptr(bp = BN_dup(ap))
|| !TEST_ptr(bg = BN_dup(ag))
|| !TEST_true(DH_set0_pqg(b, bp, NULL, bg)))
- goto err;
+ goto err3;
bp = bg = NULL;
+ /*
+ * III) simulate a key exchange
+ */
+
if (!DH_generate_key(a))
- goto err;
+ goto err3;
DH_get0_key(a, &apub_key, NULL);
if (!DH_generate_key(b))
- goto err;
- DH_get0_key(b, &bpub_key, NULL);
+ goto err3;
+ DH_get0_key(b, &bpub_key, &bpriv_key);
+
+ /* Also test with a private-key-only copy of |b|. */
+ if (!TEST_ptr(c = DHparams_dup(b))
+ || !TEST_ptr(cpriv_key = BN_dup(bpriv_key))
+ || !TEST_true(DH_set0_key(c, NULL, cpriv_key)))
+ goto err3;
+ cpriv_key = NULL;
alen = DH_size(a);
if (!TEST_ptr(abuf = OPENSSL_malloc(alen))
|| !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1))
- goto err;
+ goto err3;
blen = DH_size(b);
if (!TEST_ptr(bbuf = OPENSSL_malloc(blen))
|| !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1))
- goto err;
+ goto err3;
- if (!TEST_true(aout >= 4)
- || !TEST_mem_eq(abuf, aout, bbuf, bout))
- goto err;
+ clen = DH_size(c);
+ if (!TEST_ptr(cbuf = OPENSSL_malloc(clen))
+ || !TEST_true((cout = DH_compute_key(cbuf, apub_key, c)) != -1))
+ goto err3;
- ret = 1;
+ if (!TEST_true(aout >= 20)
+ || !TEST_mem_eq(abuf, aout, bbuf, bout)
+ || !TEST_mem_eq(abuf, aout, cbuf, cout))
+ goto err3;
- err:
+ ret = 1;
+ goto success;
+
+ err1:
+ /* an error occurred before p,q,g were assigned to dh */
+ BN_free(p);
+ BN_free(q);
+ BN_free(g);
+ err2:
+ /* an error occurred before priv_key was assigned to dh */
+ BN_free(priv_key);
+ err3:
+ success:
OPENSSL_free(abuf);
OPENSSL_free(bbuf);
+ OPENSSL_free(cbuf);
DH_free(b);
DH_free(a);
+ DH_free(c);
BN_free(bp);
BN_free(bg);
+ BN_free(cpriv_key);
BN_GENCB_free(_cb);
+ DH_free(dh);
+
return ret;
}
return 1;
}
+static int dh_computekey_range_test(void)
+{
+ int ret = 0, sz;
+ DH *dh = NULL;
+ BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
+ unsigned char *buf = NULL;
+
+ if (!TEST_ptr(p = BN_dup(&ossl_bignum_ffdhe2048_p))
+ || !TEST_ptr(q = BN_dup(&ossl_bignum_ffdhe2048_q))
+ || !TEST_ptr(g = BN_dup(&ossl_bignum_const_2))
+ || !TEST_ptr(dh = DH_new())
+ || !TEST_true(DH_set0_pqg(dh, p, q, g)))
+ goto err;
+ p = q = g = NULL;
+
+ if (!TEST_int_gt(sz = DH_size(dh), 0)
+ || !TEST_ptr(buf = OPENSSL_malloc(sz))
+ || !TEST_ptr(pub = BN_new())
+ || !TEST_ptr(priv = BN_new()))
+ goto err;
+
+ if (!TEST_true(BN_set_word(priv, 1))
+ || !TEST_true(DH_set0_key(dh, NULL, priv)))
+ goto err;
+ priv = NULL;
+ if (!TEST_true(BN_set_word(pub, 1)))
+ goto err;
+
+ /* Given z = pub ^ priv mod p */
+
+ /* Test that z == 1 fails */
+ if (!TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0))
+ goto err;
+ /* Test that z == 0 fails */
+ if (!TEST_ptr(BN_copy(pub, DH_get0_p(dh)))
+ || !TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0))
+ goto err;
+ /* Test that z == p - 1 fails */
+ if (!TEST_true(BN_sub_word(pub, 1))
+ || !TEST_int_le(ossl_dh_compute_key(buf, pub, dh), 0))
+ goto err;
+ /* Test that z == p - 2 passes */
+ if (!TEST_true(BN_sub_word(pub, 1))
+ || !TEST_int_eq(ossl_dh_compute_key(buf, pub, dh), sz))
+ goto err;
+
+ ret = 1;
+err:
+ OPENSSL_free(buf);
+ BN_free(priv);
+ BN_free(pub);
+ BN_free(g);
+ BN_free(q);
+ BN_free(p);
+ DH_free(dh);
+ return ret;
+}
+
/* Test data from RFC 5114 */
static const unsigned char dhtest_1024_160_xA[] = {
0xC2, 0x6C, 0x5D, 0x7C
};
-static const unsigned char dhtest_rfc5114_2048_224_bad_y[] = {
- 0x45, 0x32, 0x5F, 0x51, 0x07, 0xE5, 0xDF, 0x1C, 0xD6, 0x02, 0x82, 0xB3,
- 0x32, 0x8F, 0xA4, 0x0F, 0x87, 0xB8, 0x41, 0xFE, 0xB9, 0x35, 0xDE, 0xAD,
- 0xC6, 0x26, 0x85, 0xB4, 0xFF, 0x94, 0x8C, 0x12, 0x4C, 0xBF, 0x5B, 0x20,
- 0xC4, 0x46, 0xA3, 0x26, 0xEB, 0xA4, 0x25, 0xB7, 0x68, 0x8E, 0xCC, 0x67,
- 0xBA, 0xEA, 0x58, 0xD0, 0xF2, 0xE9, 0xD2, 0x24, 0x72, 0x60, 0xDA, 0x88,
- 0x18, 0x9C, 0xE0, 0x31, 0x6A, 0xAD, 0x50, 0x6D, 0x94, 0x35, 0x8B, 0x83,
- 0x4A, 0x6E, 0xFA, 0x48, 0x73, 0x0F, 0x83, 0x87, 0xFF, 0x6B, 0x66, 0x1F,
- 0xA8, 0x82, 0xC6, 0x01, 0xE5, 0x80, 0xB5, 0xB0, 0x52, 0xD0, 0xE9, 0xD8,
- 0x72, 0xF9, 0x7D, 0x5B, 0x8B, 0xA5, 0x4C, 0xA5, 0x25, 0x95, 0x74, 0xE2,
- 0x7A, 0x61, 0x4E, 0xA7, 0x8F, 0x12, 0xE2, 0xD2, 0x9D, 0x8C, 0x02, 0x70,
- 0x34, 0x44, 0x32, 0xC7, 0xB2, 0xF3, 0xB9, 0xFE, 0x17, 0x2B, 0xD6, 0x1F,
- 0x8B, 0x7E, 0x4A, 0xFA, 0xA3, 0xB5, 0x3E, 0x7A, 0x81, 0x9A, 0x33, 0x66,
- 0x62, 0xA4, 0x50, 0x18, 0x3E, 0xA2, 0x5F, 0x00, 0x07, 0xD8, 0x9B, 0x22,
- 0xE4, 0xEC, 0x84, 0xD5, 0xEB, 0x5A, 0xF3, 0x2A, 0x31, 0x23, 0xD8, 0x44,
- 0x22, 0x2A, 0x8B, 0x37, 0x44, 0xCC, 0xC6, 0x87, 0x4B, 0xBE, 0x50, 0x9D,
- 0x4A, 0xC4, 0x8E, 0x45, 0xCF, 0x72, 0x4D, 0xC0, 0x89, 0xB3, 0x72, 0xED,
- 0x33, 0x2C, 0xBC, 0x7F, 0x16, 0x39, 0x3B, 0xEB, 0xD2, 0xDD, 0xA8, 0x01,
- 0x73, 0x84, 0x62, 0xB9, 0x29, 0xD2, 0xC9, 0x51, 0x32, 0x9E, 0x7A, 0x6A,
- 0xCF, 0xC1, 0x0A, 0xDB, 0x0E, 0xE0, 0x62, 0x77, 0x6F, 0x59, 0x62, 0x72,
- 0x5A, 0x69, 0xA6, 0x5B, 0x70, 0xCA, 0x65, 0xC4, 0x95, 0x6F, 0x9A, 0xC2,
- 0xDF, 0x72, 0x6D, 0xB1, 0x1E, 0x54, 0x7B, 0x51, 0xB4, 0xEF, 0x7F, 0x89,
- 0x93, 0x74, 0x89, 0x59
-};
-
typedef struct {
DH *(*get_param) (void);
const unsigned char *xA;
DH *dhB = NULL;
unsigned char *Z1 = NULL;
unsigned char *Z2 = NULL;
+ int szA, szB;
const rfc5114_td *td = NULL;
- BIGNUM *bady = NULL, *priv_key = NULL, *pub_key = NULL;
+ BIGNUM *priv_key = NULL, *pub_key = NULL;
const BIGNUM *pub_key_tmp;
for (i = 0; i < (int)OSSL_NELEM(rfctd); i++) {
if (!TEST_ptr(priv_key = BN_bin2bn(td->xB, td->xB_len, NULL))
|| !TEST_ptr(pub_key = BN_bin2bn(td->yB, td->yB_len, NULL))
- || !TEST_true( DH_set0_key(dhB, pub_key, priv_key)))
+ || !TEST_true(DH_set0_key(dhB, pub_key, priv_key)))
goto bad_err;
priv_key = pub_key = NULL;
- if (!TEST_uint_eq(td->Z_len, (size_t)DH_size(dhA))
- || !TEST_uint_eq(td->Z_len, (size_t)DH_size(dhB)))
+ if (!TEST_int_gt(szA = DH_size(dhA), 0)
+ || !TEST_int_gt(szB = DH_size(dhB), 0)
+ || !TEST_size_t_eq(td->Z_len, (size_t)szA)
+ || !TEST_size_t_eq(td->Z_len, (size_t)szB))
goto err;
- if (!TEST_ptr(Z1 = OPENSSL_malloc(DH_size(dhA)))
- || !TEST_ptr(Z2 = OPENSSL_malloc(DH_size(dhB))))
+ if (!TEST_ptr(Z1 = OPENSSL_malloc((size_t)szA))
+ || !TEST_ptr(Z2 = OPENSSL_malloc((size_t)szB)))
goto bad_err;
/*
* Work out shared secrets using both sides and compare with expected
OPENSSL_free(Z2);
Z2 = NULL;
}
-
- /* Now i == OSSL_NELEM(rfctd) */
- /* RFC5114 uses unsafe primes, so now test an invalid y value */
- if (!TEST_ptr(dhA = DH_get_2048_224())
- || !TEST_ptr(Z1 = OPENSSL_malloc(DH_size(dhA))))
- goto bad_err;
-
- if (!TEST_ptr(bady = BN_bin2bn(dhtest_rfc5114_2048_224_bad_y,
- sizeof(dhtest_rfc5114_2048_224_bad_y),
- NULL)))
- goto bad_err;
-
- if (!DH_generate_key(dhA))
- goto bad_err;
-
- if (DH_compute_key(Z1, bady, dhA) != -1) {
- /*
- * DH_compute_key should fail with -1. If we get here we unexpectedly
- * allowed an invalid y value
- */
- goto err;
- }
- /* We'll have a stale error on the queue from the above test so clear it */
- ERR_clear_error();
- BN_free(bady);
- DH_free(dhA);
- OPENSSL_free(Z1);
return 1;
bad_err:
- BN_free(bady);
DH_free(dhA);
DH_free(dhB);
BN_free(pub_key);
return 0;
err:
- BN_free(bady);
DH_free(dhA);
DH_free(dhB);
OPENSSL_free(Z1);
return 0;
}
+static int rfc7919_test(void)
+{
+ DH *a = NULL, *b = NULL;
+ const BIGNUM *apub_key = NULL, *bpub_key = NULL;
+ unsigned char *abuf = NULL;
+ unsigned char *bbuf = NULL;
+ int i, alen, blen, aout, bout;
+ int ret = 0;
+
+ if (!TEST_ptr(a = DH_new_by_nid(NID_ffdhe2048)))
+ goto err;
+
+ if (!DH_check(a, &i))
+ goto err;
+ if (!TEST_false(i & DH_CHECK_P_NOT_PRIME)
+ || !TEST_false(i & DH_CHECK_P_NOT_SAFE_PRIME)
+ || !TEST_false(i & DH_UNABLE_TO_CHECK_GENERATOR)
+ || !TEST_false(i & DH_NOT_SUITABLE_GENERATOR)
+ || !TEST_false(i))
+ goto err;
+
+ if (!DH_generate_key(a))
+ goto err;
+ DH_get0_key(a, &apub_key, NULL);
-void register_tests(void)
+ /* now create another copy of the DH group for the peer */
+ if (!TEST_ptr(b = DH_new_by_nid(NID_ffdhe2048)))
+ goto err;
+
+ if (!DH_generate_key(b))
+ goto err;
+ DH_get0_key(b, &bpub_key, NULL);
+
+ alen = DH_size(a);
+ if (!TEST_int_gt(alen, 0) || !TEST_ptr(abuf = OPENSSL_malloc(alen))
+ || !TEST_true((aout = DH_compute_key(abuf, bpub_key, a)) != -1))
+ goto err;
+
+ blen = DH_size(b);
+ if (!TEST_int_gt(blen, 0) || !TEST_ptr(bbuf = OPENSSL_malloc(blen))
+ || !TEST_true((bout = DH_compute_key(bbuf, apub_key, b)) != -1))
+ goto err;
+
+ if (!TEST_true(aout >= 20)
+ || !TEST_mem_eq(abuf, aout, bbuf, bout))
+ goto err;
+
+ ret = 1;
+
+ err:
+ OPENSSL_free(abuf);
+ OPENSSL_free(bbuf);
+ DH_free(a);
+ DH_free(b);
+ return ret;
+}
+
+static int prime_groups[] = {
+ NID_ffdhe2048,
+ NID_ffdhe3072,
+ NID_ffdhe4096,
+ NID_ffdhe6144,
+ NID_ffdhe8192,
+ NID_modp_2048,
+ NID_modp_3072,
+ NID_modp_4096,
+ NID_modp_6144,
+};
+
+static int dh_test_prime_groups(int index)
+{
+ int ok = 0;
+ DH *dh = NULL;
+ const BIGNUM *p, *q, *g;
+
+ if (!TEST_ptr(dh = DH_new_by_nid(prime_groups[index])))
+ goto err;
+ DH_get0_pqg(dh, &p, &q, &g);
+ if (!TEST_ptr(p) || !TEST_ptr(q) || !TEST_ptr(g))
+ goto err;
+
+ if (!TEST_int_eq(DH_get_nid(dh), prime_groups[index]))
+ goto err;
+
+ /* Since q is set there is no need for the private length to be set */
+ if (!TEST_int_eq((int)DH_get_length(dh), 0))
+ goto err;
+
+ ok = 1;
+err:
+ DH_free(dh);
+ return ok;
+}
+
+static int dh_rfc5114_fix_nid_test(void)
{
+ int ok = 0;
+ EVP_PKEY_CTX *paramgen_ctx;
+
+ /* Run the test. Success is any time the test does not cause a SIGSEGV interrupt */
+ paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DHX, 0);
+ if (!TEST_ptr(paramgen_ctx))
+ goto err;
+ if (!TEST_int_eq(EVP_PKEY_paramgen_init(paramgen_ctx), 1))
+ goto err;
+ /* Tested function is called here */
+ if (!TEST_int_eq(EVP_PKEY_CTX_set_dhx_rfc5114(paramgen_ctx, 3), 1))
+ goto err;
+ /* Negative test */
+ if (!TEST_int_eq(EVP_PKEY_CTX_set_dhx_rfc5114(paramgen_ctx, 99), 0))
+ goto err;
+ /* If we're still running then the test passed. */
+ ok = 1;
+err:
+ EVP_PKEY_CTX_free(paramgen_ctx);
+ return ok;
+}
+
+static int dh_set_dh_nid_test(void)
+{
+ int ok = 0;
+ EVP_PKEY_CTX *paramgen_ctx;
+
+ /* Run the test. Success is any time the test does not cause a SIGSEGV interrupt */
+ paramgen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, 0);
+ if (!TEST_ptr(paramgen_ctx))
+ goto err;
+ if (!TEST_int_eq(EVP_PKEY_paramgen_init(paramgen_ctx), 1))
+ goto err;
+ /* Tested function is called here */
+ if (!TEST_int_eq(EVP_PKEY_CTX_set_dh_nid(paramgen_ctx, NID_ffdhe2048), 1))
+ goto err;
+ /* Negative test */
+ if (!TEST_int_eq(EVP_PKEY_CTX_set_dh_nid(paramgen_ctx, NID_secp521r1), 0))
+ goto err;
+ /* If we're still running then the test passed. */
+ ok = 1;
+err:
+ EVP_PKEY_CTX_free(paramgen_ctx);
+ return ok;
+}
+
+static int dh_get_nid(void)
+{
+ int ok = 0;
+ const BIGNUM *p, *q, *g;
+ BIGNUM *pcpy = NULL, *gcpy = NULL, *qcpy = NULL;
+ DH *dh1 = DH_new_by_nid(NID_ffdhe2048);
+ DH *dh2 = DH_new();
+
+ if (!TEST_ptr(dh1)
+ || !TEST_ptr(dh2))
+ goto err;
+
+ /* Set new DH parameters manually using a existing named group's p & g */
+ DH_get0_pqg(dh1, &p, &q, &g);
+ if (!TEST_ptr(p)
+ || !TEST_ptr(q)
+ || !TEST_ptr(g)
+ || !TEST_ptr(pcpy = BN_dup(p))
+ || !TEST_ptr(gcpy = BN_dup(g)))
+ goto err;
+
+ if (!TEST_true(DH_set0_pqg(dh2, pcpy, NULL, gcpy)))
+ goto err;
+ pcpy = gcpy = NULL;
+ /* Test q is set if p and g are provided */
+ if (!TEST_ptr(DH_get0_q(dh2)))
+ goto err;
+
+ /* Test that setting p & g manually returns that it is a named group */
+ if (!TEST_int_eq(DH_get_nid(dh2), NID_ffdhe2048))
+ goto err;
+
+ /* Test that after changing g it is no longer a named group */
+ if (!TEST_ptr(gcpy = BN_dup(BN_value_one())))
+ goto err;
+ if (!TEST_true(DH_set0_pqg(dh2, NULL, NULL, gcpy)))
+ goto err;
+ gcpy = NULL;
+ if (!TEST_int_eq(DH_get_nid(dh2), NID_undef))
+ goto err;
+
+ /* Test that setting an incorrect q results in this not being a named group */
+ if (!TEST_ptr(pcpy = BN_dup(p))
+ || !TEST_ptr(qcpy = BN_dup(q))
+ || !TEST_ptr(gcpy = BN_dup(g))
+ || !TEST_int_eq(BN_add_word(qcpy, 2), 1)
+ || !TEST_true(DH_set0_pqg(dh2, pcpy, qcpy, gcpy)))
+ goto err;
+ pcpy = qcpy = gcpy = NULL;
+ if (!TEST_int_eq(DH_get_nid(dh2), NID_undef))
+ goto err;
+
+ ok = 1;
+err:
+ BN_free(pcpy);
+ BN_free(qcpy);
+ BN_free(gcpy);
+ DH_free(dh2);
+ DH_free(dh1);
+ return ok;
+}
+
+static const unsigned char dh_pub_der[] = {
+ 0x30, 0x82, 0x02, 0x28, 0x30, 0x82, 0x01, 0x1b, 0x06, 0x09, 0x2a, 0x86,
+ 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x82, 0x01, 0x0c, 0x02,
+ 0x82, 0x01, 0x01, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b,
+ 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74,
+ 0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79,
+ 0x8e, 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b,
+ 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d,
+ 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6,
+ 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x37, 0xed, 0x6b, 0x0b, 0xff, 0x5c, 0xb6,
+ 0xf4, 0x06, 0xb7, 0xed, 0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5,
+ 0xae, 0x9f, 0x24, 0x11, 0x7c, 0x4b, 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51,
+ 0xec, 0xe4, 0x5b, 0x3d, 0xc2, 0x00, 0x7c, 0xb8, 0xa1, 0x63, 0xbf, 0x05,
+ 0x98, 0xda, 0x48, 0x36, 0x1c, 0x55, 0xd3, 0x9a, 0x69, 0x16, 0x3f, 0xa8,
+ 0xfd, 0x24, 0xcf, 0x5f, 0x83, 0x65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x03, 0x49, 0x8f, 0xb0, 0xb9, 0xc8, 0x48, 0x9d, 0xc7, 0xdf, 0x8b,
+ 0x1c, 0xbf, 0xda, 0x89, 0x78, 0x6f, 0xd3, 0x62, 0xad, 0x35, 0xb9, 0xd3,
+ 0x9b, 0xd0, 0x25, 0x65
+};
+
+/*
+ * Load PKCS3 DH Parameters that contain an optional private value length.
+ * Loading a named group should not overwrite the private value length field.
+ */
+static int dh_load_pkcs3_namedgroup_privlen_test(void)
+{
+ int ret, privlen = 0;
+ EVP_PKEY *pkey = NULL;
+ const unsigned char *p = dh_pub_der;
+
+ ret = TEST_ptr(pkey = d2i_PUBKEY_ex(NULL, &p, sizeof(dh_pub_der),
+ NULL, NULL))
+ && TEST_true(EVP_PKEY_get_int_param(pkey, OSSL_PKEY_PARAM_DH_PRIV_LEN,
+ &privlen))
+ && TEST_int_eq(privlen, 1024);
+
+ EVP_PKEY_free(pkey);
+ return ret;
+}
+
+#endif
+
+int setup_tests(void)
+{
+#ifdef OPENSSL_NO_DH
+ TEST_note("No DH support");
+#else
ADD_TEST(dh_test);
+ ADD_TEST(dh_computekey_range_test);
ADD_TEST(rfc5114_test);
-}
+ ADD_TEST(rfc7919_test);
+ ADD_ALL_TESTS(dh_test_prime_groups, OSSL_NELEM(prime_groups));
+ ADD_TEST(dh_get_nid);
+ ADD_TEST(dh_load_pkcs3_namedgroup_privlen_test);
+ ADD_TEST(dh_rfc5114_fix_nid_test);
+ ADD_TEST(dh_set_dh_nid_test);
#endif
+ return 1;
+}