Latest update.
This commit is contained in:
+57
-58
@@ -24,7 +24,7 @@ typedef struct {
|
||||
/* Parameter gen parameters */
|
||||
int prime_len;
|
||||
int generator;
|
||||
int use_dsa;
|
||||
int paramgen_type;
|
||||
int subprime_len;
|
||||
int pad;
|
||||
/* message digest used for parameter generation */
|
||||
@@ -69,6 +69,7 @@ static int pkey_dh_init(EVP_PKEY_CTX *ctx)
|
||||
static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
|
||||
{
|
||||
DH_PKEY_CTX *dctx = ctx->data;
|
||||
|
||||
if (dctx != NULL) {
|
||||
OPENSSL_free(dctx->kdf_ukm);
|
||||
ASN1_OBJECT_free(dctx->kdf_oid);
|
||||
@@ -88,7 +89,7 @@ static int pkey_dh_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
|
||||
dctx->prime_len = sctx->prime_len;
|
||||
dctx->subprime_len = sctx->subprime_len;
|
||||
dctx->generator = sctx->generator;
|
||||
dctx->use_dsa = sctx->use_dsa;
|
||||
dctx->paramgen_type = sctx->paramgen_type;
|
||||
dctx->pad = sctx->pad;
|
||||
dctx->md = sctx->md;
|
||||
dctx->rfc5114_param = sctx->rfc5114_param;
|
||||
@@ -120,7 +121,7 @@ static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
|
||||
if (dctx->use_dsa == 0)
|
||||
if (dctx->paramgen_type == DH_PARAMGEN_TYPE_GENERATOR)
|
||||
return -2;
|
||||
dctx->subprime_len = p1;
|
||||
return 1;
|
||||
@@ -130,20 +131,20 @@ static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
|
||||
if (dctx->use_dsa)
|
||||
if (dctx->paramgen_type != DH_PARAMGEN_TYPE_GENERATOR)
|
||||
return -2;
|
||||
dctx->generator = p1;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
|
||||
#ifdef OPENSSL_NO_DSA
|
||||
if (p1 != 0)
|
||||
if (p1 != DH_PARAMGEN_TYPE_GENERATOR)
|
||||
return -2;
|
||||
#else
|
||||
if (p1 < 0 || p1 > 2)
|
||||
return -2;
|
||||
#endif
|
||||
dctx->use_dsa = p1;
|
||||
dctx->paramgen_type = p1;
|
||||
return 1;
|
||||
|
||||
case EVP_PKEY_CTRL_DH_RFC5114:
|
||||
@@ -271,33 +272,22 @@ static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
|
||||
return -2;
|
||||
}
|
||||
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
|
||||
extern int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
|
||||
const EVP_MD *evpmd,
|
||||
const unsigned char *seed_in, size_t seed_len,
|
||||
unsigned char *seed_out, int *counter_ret,
|
||||
unsigned long *h_ret, BN_GENCB *cb);
|
||||
|
||||
extern int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
|
||||
const EVP_MD *evpmd,
|
||||
const unsigned char *seed_in,
|
||||
size_t seed_len, int idx,
|
||||
unsigned char *seed_out, int *counter_ret,
|
||||
unsigned long *h_ret, BN_GENCB *cb);
|
||||
|
||||
static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)
|
||||
static DH *ffc_params_generate(OPENSSL_CTX *libctx, DH_PKEY_CTX *dctx,
|
||||
BN_GENCB *pcb)
|
||||
{
|
||||
DSA *ret;
|
||||
DH *ret;
|
||||
int rv = 0;
|
||||
int res;
|
||||
int prime_len = dctx->prime_len;
|
||||
int subprime_len = dctx->subprime_len;
|
||||
const EVP_MD *md = dctx->md;
|
||||
if (dctx->use_dsa > 2)
|
||||
|
||||
if (dctx->paramgen_type > DH_PARAMGEN_TYPE_FIPS_186_4)
|
||||
return NULL;
|
||||
ret = DSA_new();
|
||||
ret = DH_new();
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
if (subprime_len == -1) {
|
||||
if (prime_len >= 2048)
|
||||
subprime_len = 256;
|
||||
@@ -310,27 +300,47 @@ static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)
|
||||
else
|
||||
md = EVP_sha1();
|
||||
}
|
||||
if (dctx->use_dsa == 1)
|
||||
rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,
|
||||
NULL, 0, NULL, NULL, NULL, pcb);
|
||||
else if (dctx->use_dsa == 2)
|
||||
rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,
|
||||
NULL, 0, -1, NULL, NULL, NULL, pcb);
|
||||
# ifndef FIPS_MODE
|
||||
if (dctx->paramgen_type == DH_PARAMGEN_TYPE_FIPS_186_2)
|
||||
rv = ffc_params_FIPS186_2_generate(libctx, &ret->params,
|
||||
FFC_PARAM_TYPE_DH,
|
||||
prime_len, subprime_len, md, &res,
|
||||
pcb);
|
||||
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)
|
||||
rv = ffc_params_FIPS186_4_generate(libctx, &ret->params,
|
||||
FFC_PARAM_TYPE_DH,
|
||||
prime_len, subprime_len, md, &res,
|
||||
pcb);
|
||||
if (rv <= 0) {
|
||||
DSA_free(ret);
|
||||
DH_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
||||
static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx,
|
||||
EVP_PKEY *pkey)
|
||||
{
|
||||
DH *dh = NULL;
|
||||
DH_PKEY_CTX *dctx = ctx->data;
|
||||
BN_GENCB *pcb;
|
||||
BN_GENCB *pcb = NULL;
|
||||
int ret;
|
||||
|
||||
/*
|
||||
* Look for a safe prime group for key establishment. Which uses
|
||||
* either RFC_3526 (modp_XXXX) or RFC_7919 (ffdheXXXX).
|
||||
*/
|
||||
if (dctx->param_nid != NID_undef) {
|
||||
if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
|
||||
return 0;
|
||||
EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
if (dctx->rfc5114_param) {
|
||||
switch (dctx->rfc5114_param) {
|
||||
case 1:
|
||||
@@ -351,36 +361,25 @@ static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
||||
EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
|
||||
return 1;
|
||||
}
|
||||
#endif /* FIPS_MODE */
|
||||
|
||||
if (dctx->param_nid != 0) {
|
||||
if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
|
||||
return 0;
|
||||
EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ctx->pkey_gencb) {
|
||||
if (ctx->pkey_gencb != NULL) {
|
||||
pcb = BN_GENCB_new();
|
||||
if (pcb == NULL)
|
||||
return 0;
|
||||
evp_pkey_set_cb_translate(pcb, ctx);
|
||||
} else
|
||||
pcb = NULL;
|
||||
#ifndef OPENSSL_NO_DSA
|
||||
if (dctx->use_dsa) {
|
||||
DSA *dsa_dh;
|
||||
dsa_dh = dsa_dh_generate(dctx, pcb);
|
||||
}
|
||||
# ifdef FIPS_MODE
|
||||
dctx->paramgen_type = DH_PARAMGEN_TYPE_FIPS_186_4;
|
||||
# endif /* FIPS_MODE */
|
||||
if (dctx->paramgen_type >= DH_PARAMGEN_TYPE_FIPS_186_2) {
|
||||
dh = ffc_params_generate(NULL, dctx, pcb);
|
||||
BN_GENCB_free(pcb);
|
||||
if (dsa_dh == NULL)
|
||||
return 0;
|
||||
dh = DSA_dup_DH(dsa_dh);
|
||||
DSA_free(dsa_dh);
|
||||
if (!dh)
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
dh = DH_new();
|
||||
if (dh == NULL) {
|
||||
BN_GENCB_free(pcb);
|
||||
@@ -401,11 +400,11 @@ static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
||||
DH_PKEY_CTX *dctx = ctx->data;
|
||||
DH *dh = NULL;
|
||||
|
||||
if (ctx->pkey == NULL && dctx->param_nid == 0) {
|
||||
if (ctx->pkey == NULL && dctx->param_nid == NID_undef) {
|
||||
DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
|
||||
return 0;
|
||||
}
|
||||
if (dctx->param_nid != 0)
|
||||
if (dctx->param_nid != NID_undef)
|
||||
dh = DH_new_by_nid(dctx->param_nid);
|
||||
else
|
||||
dh = DH_new();
|
||||
|
||||
Reference in New Issue
Block a user