sync code with last improvements from OpenBSD

This commit is contained in:
purplerain 2023-08-17 07:36:55 +00:00
parent 454dab66ed
commit 27298272ec
Signed by: purplerain
GPG key ID: F42C07F07E2E35B7
237 changed files with 4666 additions and 2149 deletions

View file

@ -1,4 +1,4 @@
/* $OpenBSD: dh_ameth.c,v 1.33 2023/08/10 16:57:15 tb Exp $ */
/* $OpenBSD: dh_ameth.c,v 1.39 2023/08/12 07:59:48 tb Exp $ */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
* project 2006.
*/
@ -70,7 +70,7 @@
#include "evp_local.h"
static void
int_dh_free(EVP_PKEY *pkey)
dh_free(EVP_PKEY *pkey)
{
DH_free(pkey->pkey.dh);
}
@ -78,83 +78,86 @@ int_dh_free(EVP_PKEY *pkey)
static int
dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
{
const unsigned char *p, *pm;
int pklen, pmlen;
X509_ALGOR *algor;
int ptype;
const void *pval;
const ASN1_STRING *pstr;
X509_ALGOR *palg;
ASN1_INTEGER *public_key = NULL;
const ASN1_STRING *astr;
const unsigned char *key, *params, *p;
int key_len, params_len;
ASN1_INTEGER *aint = NULL;
DH *dh = NULL;
int ret = 0;
if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
return 0;
X509_ALGOR_get0(NULL, &ptype, &pval, palg);
if (!X509_PUBKEY_get0_param(NULL, &key, &key_len, &algor, pubkey))
goto err;
X509_ALGOR_get0(NULL, &ptype, &pval, algor);
if (ptype != V_ASN1_SEQUENCE) {
DHerror(DH_R_PARAMETER_ENCODING_ERROR);
goto err;
}
pstr = pval;
pm = pstr->data;
pmlen = pstr->length;
astr = pval;
params = astr->data;
params_len = astr->length;
if (!(dh = d2i_DHparams(NULL, &pm, pmlen))) {
p = params;
if ((dh = d2i_DHparams(NULL, &p, params_len)) == NULL) {
DHerror(DH_R_DECODE_ERROR);
goto err;
}
if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen))) {
p = key;
if ((aint = d2i_ASN1_INTEGER(NULL, &p, key_len)) == NULL) {
DHerror(DH_R_DECODE_ERROR);
goto err;
}
/* We have parameters now set public key */
if (!(dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) {
BN_free(dh->pub_key);
if ((dh->pub_key = ASN1_INTEGER_to_BN(aint, NULL)) == NULL) {
DHerror(DH_R_BN_DECODE_ERROR);
goto err;
}
ASN1_INTEGER_free(public_key);
EVP_PKEY_assign_DH(pkey, dh);
return 1;
if (!EVP_PKEY_assign_DH(pkey, dh))
goto err;
dh = NULL;
err:
if (public_key)
ASN1_INTEGER_free(public_key);
ret = 1;
err:
ASN1_INTEGER_free(aint);
DH_free(dh);
return 0;
return ret;
}
static int
dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
{
const DH *dh = pkey->pkey.dh;
ASN1_STRING *params = NULL;
ASN1_STRING *astr = NULL;
int ptype = V_ASN1_SEQUENCE;
ASN1_INTEGER *key = NULL;
ASN1_INTEGER *aint = NULL;
ASN1_OBJECT *aobj;
unsigned char *params_der = NULL, *key_der = NULL;
unsigned char *params = NULL, *key = NULL;
int params_len = 0, key_len = 0;
int ret = 0;
if ((params_len = i2d_DHparams(dh, &params_der)) <= 0) {
if ((params_len = i2d_DHparams(dh, &params)) <= 0) {
DHerror(ERR_R_MALLOC_FAILURE);
params_len = 0;
goto err;
}
if ((params = ASN1_STRING_new()) == NULL) {
if ((astr = ASN1_STRING_new()) == NULL) {
DHerror(ERR_R_MALLOC_FAILURE);
goto err;
}
ASN1_STRING_set0(params, params_der, params_len);
params_der = NULL;
ASN1_STRING_set0(astr, params, params_len);
params = NULL;
params_len = 0;
if ((key = BN_to_ASN1_INTEGER(dh->pub_key, NULL)) == NULL)
if ((aint = BN_to_ASN1_INTEGER(dh->pub_key, NULL)) == NULL)
goto err;
if ((key_len = i2d_ASN1_INTEGER(key, &key_der)) <= 0) {
if ((key_len = i2d_ASN1_INTEGER(aint, &key)) <= 0) {
DHerror(ERR_R_MALLOC_FAILURE);
key_len = 0;
goto err;
@ -162,19 +165,19 @@ dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
if ((aobj = OBJ_nid2obj(EVP_PKEY_DH)) == NULL)
goto err;
if (!X509_PUBKEY_set0_param(pk, aobj, ptype, params, key_der, key_len))
if (!X509_PUBKEY_set0_param(pk, aobj, ptype, astr, key, key_len))
goto err;
params = NULL;
key_der = NULL;
astr = NULL;
key = NULL;
key_len = 0;
ret = 1;
err:
ASN1_STRING_free(params);
ASN1_INTEGER_free(key);
freezero(params_der, params_len);
freezero(key_der, key_len);
ASN1_STRING_free(astr);
ASN1_INTEGER_free(aint);
freezero(params, params_len);
freezero(key, key_len);
return ret;
}
@ -188,84 +191,90 @@ dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
static int
dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
{
const unsigned char *p, *pm;
int pklen, pmlen;
const X509_ALGOR *algor;
int ptype;
const void *pval;
const ASN1_STRING *pstr;
const X509_ALGOR *palg;
ASN1_INTEGER *privkey = NULL;
const ASN1_STRING *astr;
const unsigned char *key, *params, *p;
int key_len, params_len;
ASN1_INTEGER *aint = NULL;
DH *dh = NULL;
int ret = 0;
if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
return 0;
if (!PKCS8_pkey_get0(NULL, &key, &key_len, &algor, p8))
goto err;
X509_ALGOR_get0(NULL, &ptype, &pval, algor);
X509_ALGOR_get0(NULL, &ptype, &pval, palg);
if (ptype != V_ASN1_SEQUENCE)
goto decerr;
if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen)))
goto decerr;
pstr = pval;
pm = pstr->data;
pmlen = pstr->length;
if (!(dh = d2i_DHparams(NULL, &pm, pmlen)))
goto decerr;
/* We have parameters now set private key */
if (!(dh->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
DHerror(DH_R_BN_ERROR);
goto dherr;
if (ptype != V_ASN1_SEQUENCE) {
DHerror(DH_R_PARAMETER_ENCODING_ERROR);
goto err;
}
astr = pval;
params = astr->data;
params_len = astr->length;
p = params;
if ((dh = d2i_DHparams(NULL, &p, params_len)) == NULL) {
DHerror(DH_R_DECODE_ERROR);
goto err;
}
p = key;
if ((aint = d2i_ASN1_INTEGER(NULL, &p, key_len)) == NULL) {
DHerror(DH_R_DECODE_ERROR);
goto err;
}
BN_free(dh->priv_key);
if ((dh->priv_key = ASN1_INTEGER_to_BN(aint, NULL)) == NULL) {
DHerror(DH_R_BN_DECODE_ERROR);
goto err;
}
/* Calculate public key */
if (!DH_generate_key(dh))
goto dherr;
goto err;
EVP_PKEY_assign_DH(pkey, dh);
if (!EVP_PKEY_assign_DH(pkey, dh))
goto err;
dh = NULL;
ASN1_INTEGER_free(privkey);
ret = 1;
return 1;
decerr:
DHerror(EVP_R_DECODE_ERROR);
dherr:
ASN1_INTEGER_free(privkey);
err:
ASN1_INTEGER_free(aint);
DH_free(dh);
return 0;
return ret;
}
static int
dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
const DH *dh = pkey->pkey.dh;
ASN1_STRING *params = NULL;
ASN1_STRING *astr = NULL;
int ptype = V_ASN1_SEQUENCE;
ASN1_INTEGER *key = NULL;
ASN1_INTEGER *aint = NULL;
ASN1_OBJECT *aobj;
unsigned char *params_der = NULL, *key_der = NULL;
unsigned char *params = NULL, *key = NULL;
int params_len = 0, key_len = 0;
int ret = 0;
if ((params_len = i2d_DHparams(dh, &params_der)) <= 0) {
if ((params_len = i2d_DHparams(dh, &params)) <= 0) {
DHerror(ERR_R_MALLOC_FAILURE);
params_len = 0;
goto err;
}
if ((params = ASN1_STRING_type_new(V_ASN1_SEQUENCE)) == NULL) {
if ((astr = ASN1_STRING_type_new(V_ASN1_SEQUENCE)) == NULL) {
DHerror(ERR_R_MALLOC_FAILURE);
goto err;
}
ASN1_STRING_set0(params, params_der, params_len);
params_der = NULL;
ASN1_STRING_set0(astr, params, params_len);
params = NULL;
params_len = 0;
if ((key = BN_to_ASN1_INTEGER(dh->priv_key, NULL)) == NULL) {
if ((aint = BN_to_ASN1_INTEGER(dh->priv_key, NULL)) == NULL) {
DHerror(DH_R_BN_ERROR);
goto err;
}
if ((key_len = i2d_ASN1_INTEGER(key, &key_der)) <= 0) {
if ((key_len = i2d_ASN1_INTEGER(aint, &key)) <= 0) {
DHerror(ERR_R_MALLOC_FAILURE);
key_len = 0;
goto err;
@ -273,40 +282,49 @@ dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
if ((aobj = OBJ_nid2obj(NID_dhKeyAgreement)) == NULL)
goto err;
if (!PKCS8_pkey_set0(p8, aobj, 0, ptype, params, key_der, key_len))
if (!PKCS8_pkey_set0(p8, aobj, 0, ptype, astr, key, key_len))
goto err;
params = NULL;
key_der = NULL;
astr = NULL;
key = NULL;
key_len = 0;
ret = 1;
err:
ASN1_STRING_free(params);
ASN1_INTEGER_free(key);
freezero(params_der, params_len);
freezero(key_der, key_len);
ASN1_STRING_free(astr);
ASN1_INTEGER_free(aint);
freezero(params, params_len);
freezero(key, key_len);
return ret;
}
static int
dh_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
dh_param_decode(EVP_PKEY *pkey, const unsigned char **params, int params_len)
{
DH *dh;
DH *dh = NULL;
int ret = 0;
if (!(dh = d2i_DHparams(NULL, pder, derlen))) {
if ((dh = d2i_DHparams(NULL, params, params_len)) == NULL) {
DHerror(ERR_R_DH_LIB);
return 0;
goto err;
}
EVP_PKEY_assign_DH(pkey, dh);
return 1;
if (!EVP_PKEY_assign_DH(pkey, dh))
goto err;
dh = NULL;
ret = 1;
err:
DH_free(dh);
return ret;
}
static int
dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
dh_param_encode(const EVP_PKEY *pkey, unsigned char **params)
{
return i2d_DHparams(pkey->pkey.dh, pder);
return i2d_DHparams(pkey->pkey.dh, params);
}
static int
@ -370,7 +388,7 @@ do_dh_print(BIO *bp, const DH *x, int indent, ASN1_PCTX *ctx, int ptype)
}
static int
int_dh_size(const EVP_PKEY *pkey)
dh_size(const EVP_PKEY *pkey)
{
return DH_size(pkey->pkey.dh);
}
@ -416,11 +434,11 @@ dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
}
static int
dh_missing_parameters(const EVP_PKEY *a)
dh_missing_parameters(const EVP_PKEY *pkey)
{
if (!a->pkey.dh->p || !a->pkey.dh->g)
return 1;
return 0;
const DH *dh = pkey->pkey.dh;
return dh->p == NULL || dh->g == NULL;
}
static int
@ -520,7 +538,7 @@ const EVP_PKEY_ASN1_METHOD dh_asn1_meth = {
.priv_encode = dh_priv_encode,
.priv_print = dh_private_print,
.pkey_size = int_dh_size,
.pkey_size = dh_size,
.pkey_bits = dh_bits,
.pkey_security_bits = dh_security_bits,
@ -531,7 +549,7 @@ const EVP_PKEY_ASN1_METHOD dh_asn1_meth = {
.param_cmp = dh_cmp_parameters,
.param_print = dh_param_print,
.pkey_free = int_dh_free,
.pkey_free = dh_free,
.pkey_check = NULL,
.pkey_public_check = dh_pkey_public_check,

View file

@ -1,25 +1,25 @@
/* $OpenBSD: dh_lib.c,v 1.39 2023/07/08 15:29:03 beck Exp $ */
/* $OpenBSD: dh_lib.c,v 1.41 2023/08/13 12:09:14 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL.
*
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
*
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@ -34,10 +34,10 @@
* Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
*
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@ -49,7 +49,7 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
@ -96,19 +96,19 @@ DH_set_method(DH *dh, const DH_METHOD *meth)
* NB: The caller is specifically setting a method, so it's not up to us
* to deal with which ENGINE it comes from.
*/
const DH_METHOD *mtmp;
const DH_METHOD *mtmp;
mtmp = dh->meth;
if (mtmp->finish)
mtmp = dh->meth;
if (mtmp->finish)
mtmp->finish(dh);
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(dh->engine);
dh->engine = NULL;
#endif
dh->meth = meth;
if (meth->init)
dh->meth = meth;
if (meth->init)
meth->init(dh);
return 1;
return 1;
}
LCRYPTO_ALIAS(DH_set_method);
@ -122,61 +122,46 @@ LCRYPTO_ALIAS(DH_new);
DH *
DH_new_method(ENGINE *engine)
{
DH *ret;
DH *dh;
ret = malloc(sizeof(DH));
if (ret == NULL) {
if ((dh = calloc(1, sizeof(*dh))) == NULL) {
DHerror(ERR_R_MALLOC_FAILURE);
return NULL;
goto err;
}
ret->meth = DH_get_default_method();
dh->meth = DH_get_default_method();
dh->flags = dh->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
dh->references = 1;
#ifndef OPENSSL_NO_ENGINE
if (engine) {
if (engine != NULL) {
if (!ENGINE_init(engine)) {
DHerror(ERR_R_ENGINE_LIB);
free(ret);
return NULL;
goto err;
}
ret->engine = engine;
dh->engine = engine;
} else
ret->engine = ENGINE_get_default_DH();
if(ret->engine) {
ret->meth = ENGINE_get_DH(ret->engine);
if (ret->meth == NULL) {
dh->engine = ENGINE_get_default_DH();
if (dh->engine != NULL) {
if ((dh->meth = ENGINE_get_DH(dh->engine)) == NULL) {
DHerror(ERR_R_ENGINE_LIB);
ENGINE_finish(ret->engine);
free(ret);
return NULL;
goto err;
}
dh->flags = dh->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
}
#endif
ret->pad = 0;
ret->version = 0;
ret->p = NULL;
ret->g = NULL;
ret->length = 0;
ret->pub_key = NULL;
ret->priv_key = NULL;
ret->q = NULL;
ret->j = NULL;
ret->seed = NULL;
ret->seedlen = 0;
ret->counter = NULL;
ret->method_mont_p=NULL;
ret->references = 1;
ret->flags = ret->meth->flags & ~DH_FLAG_NON_FIPS_ALLOW;
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
if (ret->meth->init != NULL && !ret->meth->init(ret)) {
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(ret->engine);
#endif
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
free(ret);
ret = NULL;
}
return ret;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, dh, &dh->ex_data))
goto err;
if (dh->meth->init != NULL && !dh->meth->init(dh))
goto err;
return dh;
err:
DH_free(dh);
return NULL;
}
LCRYPTO_ALIAS(DH_new_method);
@ -191,7 +176,7 @@ DH_free(DH *r)
if (i > 0)
return;
if (r->meth->finish)
if (r->meth != NULL && r->meth->finish != NULL)
r->meth->finish(r);
#ifndef OPENSSL_NO_ENGINE
ENGINE_finish(r->engine);