2008-11-06 02:39:08 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2006-04-13 07:51:24 +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
|
2006-04-13 07:51:24 +08:00
|
|
|
*/
|
|
|
|
|
2020-01-30 05:23:39 +08:00
|
|
|
/*
|
2020-02-03 17:05:31 +08:00
|
|
|
* DH & DSA low level APIs are deprecated for public use, but still ok for
|
2020-01-30 05:23:39 +08:00
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2006-04-13 07:51:24 +08:00
|
|
|
#include <openssl/asn1t.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/evp.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "dh_local.h"
|
2008-03-17 05:05:46 +08:00
|
|
|
#include <openssl/bn.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/dsa.h>
|
2013-07-21 04:31:10 +08:00
|
|
|
#include <openssl/objects.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
2006-04-13 07:51:24 +08:00
|
|
|
|
|
|
|
/* DH pkey context structure */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/* Parameter gen parameters */
|
|
|
|
int prime_len;
|
|
|
|
int generator;
|
2020-02-06 20:28:36 +08:00
|
|
|
int paramgen_type;
|
2013-08-01 01:10:16 +08:00
|
|
|
int subprime_len;
|
2017-09-21 22:40:15 +08:00
|
|
|
int pad;
|
2013-07-21 04:31:10 +08:00
|
|
|
/* message digest used for parameter generation */
|
2013-08-01 01:10:16 +08:00
|
|
|
const EVP_MD *md;
|
2017-05-30 08:19:50 +08:00
|
|
|
int param_nid;
|
2006-04-13 07:51:24 +08:00
|
|
|
/* Keygen callback info */
|
|
|
|
int gentmp[2];
|
2013-07-21 04:31:10 +08:00
|
|
|
/* KDF (if any) to use for DH */
|
|
|
|
char kdf_type;
|
|
|
|
/* OID to use for KDF */
|
|
|
|
ASN1_OBJECT *kdf_oid;
|
|
|
|
/* Message digest to use for key derivation */
|
|
|
|
const EVP_MD *kdf_md;
|
|
|
|
/* User key material */
|
|
|
|
unsigned char *kdf_ukm;
|
|
|
|
size_t kdf_ukmlen;
|
|
|
|
/* KDF output length */
|
|
|
|
size_t kdf_outlen;
|
2006-04-13 07:51:24 +08:00
|
|
|
} DH_PKEY_CTX;
|
|
|
|
|
|
|
|
static int pkey_dh_init(EVP_PKEY_CTX *ctx)
|
|
|
|
{
|
|
|
|
DH_PKEY_CTX *dctx;
|
2015-09-03 21:15:26 +08:00
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL)
|
2006-04-13 07:51:24 +08:00
|
|
|
return 0;
|
2019-04-13 18:32:48 +08:00
|
|
|
dctx->prime_len = 2048;
|
2013-08-01 01:10:16 +08:00
|
|
|
dctx->subprime_len = -1;
|
2006-04-13 07:51:24 +08:00
|
|
|
dctx->generator = 2;
|
2013-07-21 04:31:10 +08:00
|
|
|
dctx->kdf_type = EVP_PKEY_DH_KDF_NONE;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
ctx->data = dctx;
|
|
|
|
ctx->keygen_info = dctx->gentmp;
|
|
|
|
ctx->keygen_info_count = 2;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-05 23:39:16 +08:00
|
|
|
static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
|
|
|
|
{
|
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
2020-02-06 20:28:36 +08:00
|
|
|
|
2016-05-05 23:39:16 +08:00
|
|
|
if (dctx != NULL) {
|
|
|
|
OPENSSL_free(dctx->kdf_ukm);
|
|
|
|
ASN1_OBJECT_free(dctx->kdf_oid);
|
|
|
|
OPENSSL_free(dctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-16 04:51:25 +08:00
|
|
|
static int pkey_dh_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
|
2006-05-25 07:49:30 +08:00
|
|
|
{
|
|
|
|
DH_PKEY_CTX *dctx, *sctx;
|
2019-09-17 03:28:57 +08:00
|
|
|
|
2006-05-25 07:49:30 +08:00
|
|
|
if (!pkey_dh_init(dst))
|
|
|
|
return 0;
|
|
|
|
sctx = src->data;
|
|
|
|
dctx = dst->data;
|
|
|
|
dctx->prime_len = sctx->prime_len;
|
2013-08-01 01:10:16 +08:00
|
|
|
dctx->subprime_len = sctx->subprime_len;
|
2006-05-25 07:49:30 +08:00
|
|
|
dctx->generator = sctx->generator;
|
2020-02-06 20:28:36 +08:00
|
|
|
dctx->paramgen_type = sctx->paramgen_type;
|
2017-09-21 22:40:15 +08:00
|
|
|
dctx->pad = sctx->pad;
|
2013-08-01 01:10:16 +08:00
|
|
|
dctx->md = sctx->md;
|
2017-05-30 08:19:50 +08:00
|
|
|
dctx->param_nid = sctx->param_nid;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
dctx->kdf_type = sctx->kdf_type;
|
|
|
|
dctx->kdf_oid = OBJ_dup(sctx->kdf_oid);
|
2016-05-05 23:39:16 +08:00
|
|
|
if (dctx->kdf_oid == NULL)
|
2013-07-21 04:31:10 +08:00
|
|
|
return 0;
|
|
|
|
dctx->kdf_md = sctx->kdf_md;
|
2016-05-05 23:39:16 +08:00
|
|
|
if (sctx->kdf_ukm != NULL) {
|
Rename some BUF_xxx to OPENSSL_xxx
Rename BUF_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
to OPENSSL_{strdup,strlcat,strlcpy,memdup,strndup,strnlen}
Add #define's for the old names.
Add CRYPTO_{memdup,strndup}, called by OPENSSL_{memdup,strndup} macros.
Reviewed-by: Tim Hudson <tjh@openssl.org>
2015-12-17 05:12:24 +08:00
|
|
|
dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
|
2016-05-05 23:39:16 +08:00
|
|
|
if (dctx->kdf_ukm == NULL)
|
|
|
|
return 0;
|
2013-07-21 04:31:10 +08:00
|
|
|
dctx->kdf_ukmlen = sctx->kdf_ukmlen;
|
|
|
|
}
|
|
|
|
dctx->kdf_outlen = sctx->kdf_outlen;
|
2006-05-25 07:49:30 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
|
|
|
{
|
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
|
|
|
switch (type) {
|
|
|
|
case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
|
|
|
|
if (p1 < 256)
|
|
|
|
return -2;
|
|
|
|
dctx->prime_len = p1;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-08-01 01:10:16 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
|
2020-02-06 20:28:36 +08:00
|
|
|
if (dctx->paramgen_type == DH_PARAMGEN_TYPE_GENERATOR)
|
2013-08-01 01:10:16 +08:00
|
|
|
return -2;
|
|
|
|
dctx->subprime_len = p1;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-09-21 22:40:15 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_PAD:
|
|
|
|
dctx->pad = p1;
|
|
|
|
return 1;
|
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
|
2020-02-06 20:28:36 +08:00
|
|
|
if (dctx->paramgen_type != DH_PARAMGEN_TYPE_GENERATOR)
|
2013-08-01 01:10:16 +08:00
|
|
|
return -2;
|
2006-04-13 07:51:24 +08:00
|
|
|
dctx->generator = p1;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-08-01 01:10:16 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
|
|
|
|
#ifdef OPENSSL_NO_DSA
|
2020-02-06 20:28:36 +08:00
|
|
|
if (p1 != DH_PARAMGEN_TYPE_GENERATOR)
|
2013-08-01 01:10:16 +08:00
|
|
|
return -2;
|
|
|
|
#else
|
|
|
|
if (p1 < 0 || p1 > 2)
|
|
|
|
return -2;
|
|
|
|
#endif
|
2020-02-06 20:28:36 +08:00
|
|
|
dctx->paramgen_type = p1;
|
2013-08-01 01:10:16 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_RFC5114:
|
2017-05-30 08:19:50 +08:00
|
|
|
if (p1 < 1 || p1 > 3 || dctx->param_nid != NID_undef)
|
2011-12-07 08:32:34 +08:00
|
|
|
return -2;
|
2021-04-15 16:25:17 +08:00
|
|
|
dctx->param_nid = p1;
|
2011-12-07 08:32:34 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-05-30 08:19:50 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_NID:
|
2021-04-15 16:25:17 +08:00
|
|
|
if (p1 <= 0 || dctx->param_nid != NID_undef)
|
2017-05-30 08:19:50 +08:00
|
|
|
return -2;
|
|
|
|
dctx->param_nid = p1;
|
|
|
|
return 1;
|
|
|
|
|
2006-04-14 04:16:56 +08:00
|
|
|
case EVP_PKEY_CTRL_PEER_KEY:
|
|
|
|
/* Default behaviour is OK */
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_KDF_TYPE:
|
|
|
|
if (p1 == -2)
|
|
|
|
return dctx->kdf_type;
|
|
|
|
if (p1 != EVP_PKEY_DH_KDF_NONE && p1 != EVP_PKEY_DH_KDF_X9_42)
|
|
|
|
return -2;
|
|
|
|
dctx->kdf_type = p1;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_KDF_MD:
|
|
|
|
dctx->kdf_md = p2;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_GET_DH_KDF_MD:
|
|
|
|
*(const EVP_MD **)p2 = dctx->kdf_md;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
|
|
|
|
if (p1 <= 0)
|
|
|
|
return -2;
|
|
|
|
dctx->kdf_outlen = (size_t)p1;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
|
|
|
|
*(int *)p2 = dctx->kdf_outlen;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_KDF_UKM:
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(dctx->kdf_ukm);
|
2013-07-21 04:31:10 +08:00
|
|
|
dctx->kdf_ukm = p2;
|
|
|
|
if (p2)
|
|
|
|
dctx->kdf_ukmlen = p1;
|
|
|
|
else
|
|
|
|
dctx->kdf_ukmlen = 0;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
|
|
|
|
*(unsigned char **)p2 = dctx->kdf_ukm;
|
|
|
|
return dctx->kdf_ukmlen;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_DH_KDF_OID:
|
2015-03-24 19:52:24 +08:00
|
|
|
ASN1_OBJECT_free(dctx->kdf_oid);
|
2013-07-21 04:31:10 +08:00
|
|
|
dctx->kdf_oid = p2;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
case EVP_PKEY_CTRL_GET_DH_KDF_OID:
|
|
|
|
*(ASN1_OBJECT **)p2 = dctx->kdf_oid;
|
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
default:
|
|
|
|
return -2;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2013-08-01 01:10:16 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
|
|
|
|
const char *type, const char *value)
|
|
|
|
{
|
2015-05-07 02:56:14 +08:00
|
|
|
if (strcmp(type, "dh_paramgen_prime_len") == 0) {
|
2006-04-13 07:51:24 +08:00
|
|
|
int len;
|
|
|
|
len = atoi(value);
|
|
|
|
return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
|
|
|
|
}
|
2015-05-07 02:56:14 +08:00
|
|
|
if (strcmp(type, "dh_rfc5114") == 0) {
|
2011-12-07 08:32:34 +08:00
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
2021-04-15 16:25:17 +08:00
|
|
|
int id;
|
|
|
|
|
|
|
|
id = atoi(value);
|
|
|
|
if (id < 0 || id > 3)
|
2011-12-07 08:32:34 +08:00
|
|
|
return -2;
|
2021-04-15 16:25:17 +08:00
|
|
|
dctx->param_nid = id;
|
2011-12-07 08:32:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-05-30 08:19:50 +08:00
|
|
|
if (strcmp(type, "dh_param") == 0) {
|
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
|
|
|
int nid = OBJ_sn2nid(value);
|
|
|
|
|
|
|
|
if (nid == NID_undef) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DH, DH_R_INVALID_PARAMETER_NAME);
|
2017-05-30 08:19:50 +08:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
dctx->param_nid = nid;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-05-07 02:56:14 +08:00
|
|
|
if (strcmp(type, "dh_paramgen_generator") == 0) {
|
2006-04-13 07:51:24 +08:00
|
|
|
int len;
|
|
|
|
len = atoi(value);
|
|
|
|
return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
|
|
|
|
}
|
2015-05-07 02:56:14 +08:00
|
|
|
if (strcmp(type, "dh_paramgen_subprime_len") == 0) {
|
2013-08-01 01:10:16 +08:00
|
|
|
int len;
|
|
|
|
len = atoi(value);
|
|
|
|
return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len);
|
|
|
|
}
|
2015-05-07 02:56:14 +08:00
|
|
|
if (strcmp(type, "dh_paramgen_type") == 0) {
|
2013-08-01 01:10:16 +08:00
|
|
|
int typ;
|
|
|
|
typ = atoi(value);
|
|
|
|
return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ);
|
|
|
|
}
|
2017-09-21 22:40:15 +08:00
|
|
|
if (strcmp(type, "dh_pad") == 0) {
|
|
|
|
int pad;
|
|
|
|
pad = atoi(value);
|
|
|
|
return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
|
|
|
|
}
|
2006-04-13 07:51:24 +08:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
static DH *ffc_params_generate(OSSL_LIB_CTX *libctx, DH_PKEY_CTX *dctx,
|
2020-02-06 20:28:36 +08:00
|
|
|
BN_GENCB *pcb)
|
2013-08-01 01:10:16 +08:00
|
|
|
{
|
2020-02-06 20:28:36 +08:00
|
|
|
DH *ret;
|
2013-08-01 01:10:16 +08:00
|
|
|
int rv = 0;
|
2020-02-06 20:28:36 +08:00
|
|
|
int res;
|
2013-08-01 01:10:16 +08:00
|
|
|
int prime_len = dctx->prime_len;
|
|
|
|
int subprime_len = dctx->subprime_len;
|
2020-02-06 20:28:36 +08:00
|
|
|
|
|
|
|
if (dctx->paramgen_type > DH_PARAMGEN_TYPE_FIPS_186_4)
|
2013-08-01 01:10:16 +08:00
|
|
|
return NULL;
|
2020-02-06 20:28:36 +08:00
|
|
|
ret = DH_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (ret == NULL)
|
2013-08-01 01:10:16 +08:00
|
|
|
return NULL;
|
2020-02-06 20:28:36 +08:00
|
|
|
|
2013-08-01 01:10:16 +08:00
|
|
|
if (subprime_len == -1) {
|
|
|
|
if (prime_len >= 2048)
|
|
|
|
subprime_len = 256;
|
|
|
|
else
|
|
|
|
subprime_len = 160;
|
|
|
|
}
|
2020-06-17 09:33:16 +08:00
|
|
|
|
|
|
|
if (dctx->md != NULL)
|
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
|
|
|
ossl_ffc_set_digest(&ret->params, EVP_MD_get0_name(dctx->md), NULL);
|
2020-06-17 09:33:16 +08:00
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
# ifndef FIPS_MODULE
|
2020-02-06 20:28:36 +08:00
|
|
|
if (dctx->paramgen_type == DH_PARAMGEN_TYPE_FIPS_186_2)
|
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
|
|
|
rv = ossl_ffc_params_FIPS186_2_generate(libctx, &ret->params,
|
|
|
|
FFC_PARAM_TYPE_DH,
|
|
|
|
prime_len, subprime_len, &res,
|
|
|
|
pcb);
|
2020-02-06 20:28:36 +08:00
|
|
|
else
|
|
|
|
# endif
|
|
|
|
/* For FIPS we always use the DH_PARAMGEN_TYPE_FIPS_186_4 generator */
|
|
|
|
if (dctx->paramgen_type >= DH_PARAMGEN_TYPE_FIPS_186_2)
|
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
|
|
|
rv = ossl_ffc_params_FIPS186_4_generate(libctx, &ret->params,
|
|
|
|
FFC_PARAM_TYPE_DH,
|
|
|
|
prime_len, subprime_len, &res,
|
|
|
|
pcb);
|
2013-08-01 01:10:16 +08:00
|
|
|
if (rv <= 0) {
|
2020-02-06 20:28:36 +08:00
|
|
|
DH_free(ret);
|
2013-08-01 01:10:16 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:28:36 +08:00
|
|
|
static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx,
|
|
|
|
EVP_PKEY *pkey)
|
2006-04-13 07:51:24 +08:00
|
|
|
{
|
|
|
|
DH *dh = NULL;
|
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
2020-01-31 06:18:46 +08:00
|
|
|
BN_GENCB *pcb = NULL;
|
2006-04-13 07:51:24 +08:00
|
|
|
int ret;
|
2020-01-31 06:18:46 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for a safe prime group for key establishment. Which uses
|
|
|
|
* either RFC_3526 (modp_XXXX) or RFC_7919 (ffdheXXXX).
|
2021-04-15 16:25:17 +08:00
|
|
|
* RFC_5114 is also handled here for param_nid = (1..3)
|
2020-01-31 06:18:46 +08:00
|
|
|
*/
|
|
|
|
if (dctx->param_nid != NID_undef) {
|
2021-04-15 16:25:17 +08:00
|
|
|
int type = dctx->param_nid <= 3 ? EVP_PKEY_DHX : EVP_PKEY_DH;
|
|
|
|
|
2020-01-31 06:18:46 +08:00
|
|
|
if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
|
|
|
|
return 0;
|
2021-04-15 16:25:17 +08:00
|
|
|
EVP_PKEY_assign(pkey, type, dh);
|
2011-12-07 08:32:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-01-31 06:18:46 +08:00
|
|
|
if (ctx->pkey_gencb != NULL) {
|
2014-10-31 07:58:19 +08:00
|
|
|
pcb = BN_GENCB_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (pcb == NULL)
|
|
|
|
return 0;
|
2006-04-13 07:51:24 +08:00
|
|
|
evp_pkey_set_cb_translate(pcb, ctx);
|
2020-01-31 06:18:46 +08:00
|
|
|
}
|
2020-04-14 04:34:56 +08:00
|
|
|
# ifdef FIPS_MODULE
|
2020-02-06 20:28:36 +08:00
|
|
|
dctx->paramgen_type = DH_PARAMGEN_TYPE_FIPS_186_4;
|
2020-04-14 04:34:56 +08:00
|
|
|
# endif /* FIPS_MODULE */
|
2020-02-06 20:28:36 +08:00
|
|
|
if (dctx->paramgen_type >= DH_PARAMGEN_TYPE_FIPS_186_2) {
|
|
|
|
dh = ffc_params_generate(NULL, dctx, pcb);
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_GENCB_free(pcb);
|
2020-02-06 20:28:36 +08:00
|
|
|
if (dh == NULL)
|
2013-08-01 01:10:16 +08:00
|
|
|
return 0;
|
|
|
|
EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-04-13 07:51:24 +08:00
|
|
|
dh = DH_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (dh == NULL) {
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_GENCB_free(pcb);
|
2006-04-13 07:51:24 +08:00
|
|
|
return 0;
|
2014-10-31 07:58:19 +08:00
|
|
|
}
|
2006-04-13 07:51:24 +08:00
|
|
|
ret = DH_generate_parameters_ex(dh,
|
|
|
|
dctx->prime_len, dctx->generator, pcb);
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_GENCB_free(pcb);
|
2006-04-13 07:51:24 +08:00
|
|
|
if (ret)
|
|
|
|
EVP_PKEY_assign_DH(pkey, dh);
|
|
|
|
else
|
|
|
|
DH_free(dh);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
|
|
|
{
|
2017-05-30 08:19:50 +08:00
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
2006-04-13 07:51:24 +08:00
|
|
|
DH *dh = NULL;
|
2017-05-30 08:19:50 +08:00
|
|
|
|
2020-01-31 06:18:46 +08:00
|
|
|
if (ctx->pkey == NULL && dctx->param_nid == NID_undef) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DH, DH_R_NO_PARAMETERS_SET);
|
2006-04-13 07:51:24 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2020-01-31 06:18:46 +08:00
|
|
|
if (dctx->param_nid != NID_undef)
|
2017-05-30 08:19:50 +08:00
|
|
|
dh = DH_new_by_nid(dctx->param_nid);
|
|
|
|
else
|
|
|
|
dh = DH_new();
|
2015-10-30 19:12:26 +08:00
|
|
|
if (dh == NULL)
|
2006-04-13 07:51:24 +08:00
|
|
|
return 0;
|
2011-12-07 08:32:34 +08:00
|
|
|
EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, dh);
|
2006-04-13 07:51:24 +08:00
|
|
|
/* Note: if error return, pkey is freed by parent routine */
|
2017-05-30 08:19:50 +08:00
|
|
|
if (ctx->pkey != NULL && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
|
2006-04-13 07:51:24 +08:00
|
|
|
return 0;
|
2021-07-20 16:58:53 +08:00
|
|
|
return DH_generate_key((DH *)EVP_PKEY_get0_DH(pkey));
|
2006-04-13 07:51:24 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-05-24 20:33:46 +08:00
|
|
|
static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
|
|
|
|
size_t *keylen)
|
2006-04-14 04:16:56 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2013-07-21 04:31:10 +08:00
|
|
|
DH *dh;
|
2021-03-16 00:21:45 +08:00
|
|
|
const DH *dhpub;
|
2013-07-21 04:31:10 +08:00
|
|
|
DH_PKEY_CTX *dctx = ctx->data;
|
2021-03-16 00:21:45 +08:00
|
|
|
BIGNUM *dhpubbn;
|
|
|
|
|
|
|
|
if (ctx->pkey == NULL || ctx->peerkey == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_DH, DH_R_KEYS_NOT_SET);
|
2006-04-14 04:16:56 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-07-20 16:58:53 +08:00
|
|
|
dh = (DH *)EVP_PKEY_get0_DH(ctx->pkey);
|
2021-03-16 00:21:45 +08:00
|
|
|
dhpub = EVP_PKEY_get0_DH(ctx->peerkey);
|
|
|
|
if (dhpub == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_DH, DH_R_KEYS_NOT_SET);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dhpubbn = dhpub->pub_key;
|
2013-07-21 04:31:10 +08:00
|
|
|
if (dctx->kdf_type == EVP_PKEY_DH_KDF_NONE) {
|
|
|
|
if (key == NULL) {
|
|
|
|
*keylen = DH_size(dh);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-09-21 22:40:15 +08:00
|
|
|
if (dctx->pad)
|
2021-03-16 00:21:45 +08:00
|
|
|
ret = DH_compute_key_padded(key, dhpubbn, dh);
|
2017-09-21 22:40:15 +08:00
|
|
|
else
|
2021-03-16 00:21:45 +08:00
|
|
|
ret = DH_compute_key(key, dhpubbn, dh);
|
2013-07-21 04:31:10 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
*keylen = ret;
|
|
|
|
return 1;
|
2015-08-04 00:45:26 +08:00
|
|
|
}
|
|
|
|
else if (dctx->kdf_type == EVP_PKEY_DH_KDF_X9_42) {
|
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
unsigned char *Z = NULL;
|
2022-08-22 09:06:57 +08:00
|
|
|
int Zlen = 0;
|
|
|
|
|
2013-07-21 04:31:10 +08:00
|
|
|
if (!dctx->kdf_outlen || !dctx->kdf_oid)
|
|
|
|
return 0;
|
|
|
|
if (key == NULL) {
|
|
|
|
*keylen = dctx->kdf_outlen;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (*keylen != dctx->kdf_outlen)
|
|
|
|
return 0;
|
|
|
|
ret = 0;
|
2021-03-19 12:54:40 +08:00
|
|
|
if ((Zlen = DH_size(dh)) <= 0)
|
|
|
|
return 0;
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((Z = OPENSSL_malloc(Zlen)) == NULL)
|
2021-03-19 12:54:40 +08:00
|
|
|
return 0;
|
2021-03-16 00:21:45 +08:00
|
|
|
if (DH_compute_key_padded(Z, dhpubbn, dh) <= 0)
|
2013-07-21 04:31:10 +08:00
|
|
|
goto err;
|
|
|
|
if (!DH_KDF_X9_42(key, *keylen, Z, Zlen, dctx->kdf_oid,
|
|
|
|
dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
|
|
|
|
goto err;
|
|
|
|
*keylen = dctx->kdf_outlen;
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2015-05-01 05:57:32 +08:00
|
|
|
OPENSSL_clear_free(Z, Zlen);
|
2006-04-14 04:16:56 +08:00
|
|
|
return ret;
|
2013-07-21 04:31:10 +08:00
|
|
|
}
|
2015-08-04 00:45:26 +08:00
|
|
|
return 0;
|
2006-04-14 04:16:56 +08:00
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2019-09-19 21:31:27 +08:00
|
|
|
static const EVP_PKEY_METHOD dh_pkey_meth = {
|
2006-04-13 07:51:24 +08:00
|
|
|
EVP_PKEY_DH,
|
2013-07-21 04:31:10 +08:00
|
|
|
0,
|
2006-04-13 07:51:24 +08:00
|
|
|
pkey_dh_init,
|
2006-05-25 07:49:30 +08:00
|
|
|
pkey_dh_copy,
|
2006-04-13 07:51:24 +08:00
|
|
|
pkey_dh_cleanup,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0,
|
|
|
|
pkey_dh_paramgen,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0,
|
|
|
|
pkey_dh_keygen,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
0,
|
2006-04-13 07:51:24 +08:00
|
|
|
0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0,
|
|
|
|
0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0, 0, 0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-14 04:16:56 +08:00
|
|
|
0,
|
|
|
|
pkey_dh_derive,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2006-04-13 07:51:24 +08:00
|
|
|
pkey_dh_ctrl,
|
|
|
|
pkey_dh_ctrl_str
|
|
|
|
};
|
2015-01-22 11:40:55 +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
|
|
|
const EVP_PKEY_METHOD *ossl_dh_pkey_method(void)
|
2019-09-19 21:31:27 +08:00
|
|
|
{
|
|
|
|
return &dh_pkey_meth;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_PKEY_METHOD dhx_pkey_meth = {
|
2011-12-07 08:32:34 +08:00
|
|
|
EVP_PKEY_DHX,
|
2013-07-21 04:31:10 +08:00
|
|
|
0,
|
2011-12-07 08:32:34 +08:00
|
|
|
pkey_dh_init,
|
|
|
|
pkey_dh_copy,
|
|
|
|
pkey_dh_cleanup,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0,
|
|
|
|
pkey_dh_paramgen,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0,
|
|
|
|
pkey_dh_keygen,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
0,
|
2011-12-07 08:32:34 +08:00
|
|
|
0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0,
|
|
|
|
0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0, 0, 0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0, 0,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
0,
|
|
|
|
pkey_dh_derive,
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2011-12-07 08:32:34 +08:00
|
|
|
pkey_dh_ctrl,
|
|
|
|
pkey_dh_ctrl_str
|
|
|
|
};
|
2019-09-19 21:31:27 +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
|
|
|
const EVP_PKEY_METHOD *ossl_dhx_pkey_method(void)
|
2019-09-19 21:31:27 +08:00
|
|
|
{
|
|
|
|
return &dhx_pkey_meth;
|
|
|
|
}
|