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: dsa_ameth.c,v 1.46 2023/08/10 16:57:15 tb Exp $ */
/* $OpenBSD: dsa_ameth.c,v 1.55 2023/08/12 07:59:48 tb Exp $ */
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
* project 2006.
*/
@ -75,31 +75,32 @@
static int
dsa_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;
DSA *dsa = 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) {
pstr = pval;
pm = pstr->data;
pmlen = pstr->length;
astr = pval;
params = astr->data;
params_len = astr->length;
if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen))) {
p = params;
if ((dsa = d2i_DSAparams(NULL, &p, params_len)) == NULL) {
DSAerror(DSA_R_DECODE_ERROR);
goto err;
}
} else if (ptype == V_ASN1_NULL || ptype == V_ASN1_UNDEF) {
if (!(dsa = DSA_new())) {
if ((dsa = DSA_new()) == NULL) {
DSAerror(ERR_R_MALLOC_FAILURE);
goto err;
}
@ -108,12 +109,13 @@ dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
goto err;
}
if (!(public_key = d2i_ASN1_INTEGER(NULL, &p, pklen))) {
p = key;
if ((aint = d2i_ASN1_INTEGER(NULL, &p, key_len)) == NULL) {
DSAerror(DSA_R_DECODE_ERROR);
goto err;
}
if (!(dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) {
BN_free(dsa->pub_key);
if ((dsa->pub_key = ASN1_INTEGER_to_BN(aint, NULL)) == NULL) {
DSAerror(DSA_R_BN_DECODE_ERROR);
goto err;
}
@ -124,50 +126,52 @@ dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
goto err;
}
ASN1_INTEGER_free(public_key);
EVP_PKEY_assign_DSA(pkey, dsa);
return 1;
if (!EVP_PKEY_assign_DSA(pkey, dsa))
goto err;
dsa = NULL;
err:
if (public_key)
ASN1_INTEGER_free(public_key);
ret = 1;
err:
ASN1_INTEGER_free(aint);
DSA_free(dsa);
return 0;
return ret;
}
static int
dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
{
const DSA *dsa = pkey->pkey.dsa;
ASN1_STRING *params = NULL;
ASN1_STRING *astr = NULL;
int ptype = V_ASN1_UNDEF;
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 (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
if ((params_len = i2d_DSAparams(dsa, &params_der)) <= 0) {
if (pkey->save_parameters > 0 && !EVP_PKEY_missing_parameters(pkey)) {
if ((params_len = i2d_DSAparams(dsa, &params)) <= 0) {
DSAerror(ERR_R_MALLOC_FAILURE);
params_len = 0;
goto err;
}
if ((params = ASN1_STRING_new()) == NULL) {
if ((astr = ASN1_STRING_new()) == NULL) {
DSAerror(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;
ptype = V_ASN1_SEQUENCE;
}
if ((key = BN_to_ASN1_INTEGER(dsa->pub_key, NULL)) == NULL) {
if ((aint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL)) == NULL) {
DSAerror(ERR_R_MALLOC_FAILURE);
goto err;
}
if ((key_len = i2d_ASN1_INTEGER(key, &key_der)) <= 0) {
if ((key_len = i2d_ASN1_INTEGER(aint, &key)) <= 0) {
DSAerror(ERR_R_MALLOC_FAILURE);
key_len = 0;
goto err;
@ -175,98 +179,104 @@ dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
if ((aobj = OBJ_nid2obj(EVP_PKEY_DSA)) == 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;
}
/* In PKCS#8 DSA: you just get a private key integer and parameters in the
/*
* In PKCS#8 DSA: you just get a private key integer and parameters in the
* AlgorithmIdentifier the pubkey must be recalculated.
*/
static int
dsa_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;
BN_CTX *ctx = NULL;
DSA *dsa = NULL;
int ret = 0;
if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
return 0;
X509_ALGOR_get0(NULL, &ptype, &pval, palg);
if (ptype != V_ASN1_SEQUENCE)
goto decerr;
if (!PKCS8_pkey_get0(NULL, &key, &key_len, &algor, p8))
goto err;
X509_ALGOR_get0(NULL, &ptype, &pval, algor);
if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
goto decerr;
if (privkey->type == V_ASN1_NEG_INTEGER)
goto decerr;
if (ptype != V_ASN1_SEQUENCE) {
DSAerror(DSA_R_PARAMETER_ENCODING_ERROR);
goto err;
}
pstr = pval;
pm = pstr->data;
pmlen = pstr->length;
if (!(dsa = d2i_DSAparams(NULL, &pm, pmlen)))
goto decerr;
/* We have parameters now set private key */
if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
DSAerror(DSA_R_BN_ERROR);
goto dsaerr;
astr = pval;
params = astr->data;
params_len = astr->length;
p = params;
if ((dsa = d2i_DSAparams(NULL, &p, params_len)) == NULL) {
DSAerror(DSA_R_DECODE_ERROR);
goto err;
}
p = key;
if ((aint = d2i_ASN1_INTEGER(NULL, &p, key_len)) == NULL) {
DSAerror(DSA_R_DECODE_ERROR);
goto err;
}
BN_free(dsa->priv_key);
if ((dsa->priv_key = ASN1_INTEGER_to_BN(aint, NULL)) == NULL) {
DSAerror(DSA_R_BN_DECODE_ERROR);
goto err;
}
/* Check the key for basic consistency before doing expensive things. */
if (!dsa_check_key(dsa))
goto dsaerr;
goto err;
/* Calculate public key */
if (!(dsa->pub_key = BN_new())) {
BN_free(dsa->pub_key);
if ((dsa->pub_key = BN_new()) == NULL) {
DSAerror(ERR_R_MALLOC_FAILURE);
goto dsaerr;
goto err;
}
if ((ctx = BN_CTX_new()) == NULL) {
DSAerror(ERR_R_MALLOC_FAILURE);
goto dsaerr;
goto err;
}
BN_CTX_start(ctx);
if (!BN_mod_exp_ct(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
DSAerror(DSA_R_BN_ERROR);
goto dsaerr;
goto err;
}
if (!EVP_PKEY_assign_DSA(pkey, dsa))
goto decerr;
goto err;
dsa = NULL;
ret = 1;
goto done;
decerr:
DSAerror(DSA_R_DECODE_ERROR);
dsaerr:
err:
DSA_free(dsa);
done:
BN_CTX_end(ctx);
BN_CTX_free(ctx);
ASN1_INTEGER_free(privkey);
ASN1_INTEGER_free(aint);
return ret;
}
@ -275,32 +285,32 @@ static int
dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
{
const DSA *dsa = pkey->pkey.dsa;
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_DSAparams(dsa, &params_der)) <= 0) {
if ((params_len = i2d_DSAparams(dsa, &params)) <= 0) {
DSAerror(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) {
DSAerror(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(dsa->priv_key, NULL)) == NULL) {
if ((aint = BN_to_ASN1_INTEGER(dsa->priv_key, NULL)) == NULL) {
DSAerror(DSA_R_BN_ERROR);
goto err;
}
if ((key_len = i2d_ASN1_INTEGER(key, &key_der)) <= 0) {
if ((key_len = i2d_ASN1_INTEGER(aint, &key)) <= 0) {
DSAerror(ERR_R_MALLOC_FAILURE);
key_len = 0;
goto err;
@ -308,25 +318,25 @@ dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
if ((aobj = OBJ_nid2obj(NID_dsa)) == 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
int_dsa_size(const EVP_PKEY *pkey)
dsa_size(const EVP_PKEY *pkey)
{
return DSA_size(pkey->pkey.dsa);
}
@ -346,12 +356,9 @@ dsa_security_bits(const EVP_PKEY *pkey)
static int
dsa_missing_parameters(const EVP_PKEY *pkey)
{
DSA *dsa;
const DSA *dsa = pkey->pkey.dsa;
dsa = pkey->pkey.dsa;
if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
return 1;
return 0;
return dsa->p == NULL || dsa->q == NULL || dsa->g == NULL;
}
static int
@ -397,7 +404,7 @@ dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
}
static void
int_dsa_free(EVP_PKEY *pkey)
dsa_free(EVP_PKEY *pkey)
{
DSA_free(pkey->pkey.dsa);
}
@ -452,26 +459,33 @@ do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
}
static int
dsa_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
dsa_param_decode(EVP_PKEY *pkey, const unsigned char **params, int params_len)
{
DSA *dsa;
DSA *dsa = NULL;
int ret = 0;
if (!(dsa = d2i_DSAparams(NULL, pder, derlen))) {
if ((dsa = d2i_DSAparams(NULL, params, params_len)) == NULL) {
DSAerror(ERR_R_DSA_LIB);
return 0;
goto err;
}
if (!dsa_check_key(dsa)) {
DSA_free(dsa);
return 0;
}
EVP_PKEY_assign_DSA(pkey, dsa);
return 1;
if (!dsa_check_key(dsa))
goto err;
if (!EVP_PKEY_assign_DSA(pkey, dsa))
goto err;
dsa = NULL;
ret = 1;
err:
DSA_free(dsa);
return ret;
}
static int
dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
dsa_param_encode(const EVP_PKEY *pkey, unsigned char **params)
{
return i2d_DSAparams(pkey->pkey.dsa, pder);
return i2d_DSAparams(pkey->pkey.dsa, params);
}
static int
@ -493,13 +507,14 @@ dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx)
}
static int
old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **key, int key_len)
{
DSA *dsa;
DSA *dsa = NULL;
BN_CTX *ctx = NULL;
BIGNUM *result;
int ret = 0;
if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
if ((dsa = d2i_DSAPrivateKey(NULL, key, key_len)) == NULL) {
DSAerror(ERR_R_DSA_LIB);
goto err;
}
@ -551,23 +566,24 @@ old_dsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen)
goto err;
}
BN_CTX_end(ctx);
BN_CTX_free(ctx);
if (!EVP_PKEY_assign_DSA(pkey, dsa))
goto err;
dsa = NULL;
EVP_PKEY_assign_DSA(pkey, dsa);
return 1;
ret = 1;
err:
BN_CTX_end(ctx);
BN_CTX_free(ctx);
DSA_free(dsa);
return 0;
return ret;
}
static int
old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **key)
{
return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
return i2d_DSAPrivateKey(pkey->pkey.dsa, key);
}
static int
@ -701,7 +717,7 @@ const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
.priv_encode = dsa_priv_encode,
.priv_print = dsa_priv_print,
.pkey_size = int_dsa_size,
.pkey_size = dsa_size,
.pkey_bits = dsa_bits,
.pkey_security_bits = dsa_security_bits,
@ -713,7 +729,7 @@ const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[] = {
.param_print = dsa_param_print,
.sig_print = dsa_sig_print,
.pkey_free = int_dsa_free,
.pkey_free = dsa_free,
.pkey_ctrl = dsa_pkey_ctrl,
.old_priv_decode = old_dsa_priv_decode,
.old_priv_encode = old_dsa_priv_encode

View file

@ -1,4 +1,4 @@
/* $OpenBSD: dsa_lib.c,v 1.43 2023/07/08 14:28:15 beck Exp $ */
/* $OpenBSD: dsa_lib.c,v 1.44 2023/08/12 06:14:36 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@ -127,61 +127,46 @@ LCRYPTO_ALIAS(DSA_set_method);
DSA *
DSA_new_method(ENGINE *engine)
{
DSA *ret;
DSA *dsa;
ret = malloc(sizeof(DSA));
if (ret == NULL) {
if ((dsa = calloc(1, sizeof(DSA))) == NULL) {
DSAerror(ERR_R_MALLOC_FAILURE);
return NULL;
goto err;
}
ret->meth = DSA_get_default_method();
dsa->meth = DSA_get_default_method();
dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
dsa->references = 1;
#ifndef OPENSSL_NO_ENGINE
if (engine) {
if (!ENGINE_init(engine)) {
DSAerror(ERR_R_ENGINE_LIB);
free(ret);
return NULL;
goto err;
}
ret->engine = engine;
dsa->engine = engine;
} else
ret->engine = ENGINE_get_default_DSA();
if (ret->engine) {
ret->meth = ENGINE_get_DSA(ret->engine);
if (ret->meth == NULL) {
dsa->engine = ENGINE_get_default_DSA();
if (dsa->engine != NULL) {
if ((dsa->meth = ENGINE_get_DSA(dsa->engine)) == NULL) {
DSAerror(ERR_R_ENGINE_LIB);
ENGINE_finish(ret->engine);
free(ret);
return NULL;
goto err;
}
dsa->flags = dsa->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
}
#endif
ret->pad = 0;
ret->version = 0;
ret->p = NULL;
ret->q = NULL;
ret->g = NULL;
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data))
goto err;
if (dsa->meth->init != NULL && !dsa->meth->init(dsa))
goto err;
ret->pub_key = NULL;
ret->priv_key = NULL;
return dsa;
ret->kinv = NULL;
ret->r = NULL;
ret->method_mont_p = NULL;
err:
DSA_free(dsa);
ret->references = 1;
ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW;
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, 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_DSA, ret, &ret->ex_data);
free(ret);
ret = NULL;
}
return ret;
return NULL;
}
LCRYPTO_ALIAS(DSA_new_method);
@ -197,7 +182,7 @@ DSA_free(DSA *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);