2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:51:26 +00:00
|
|
|
* Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2006-03-20 12:22:24 +00:00
|
|
|
*
|
2016-05-17 18:51:26 +00:00
|
|
|
* Licensed under the OpenSSL license (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
|
2006-03-20 12:22:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2006-03-20 12:22:24 +00:00
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/asn1.h>
|
2016-03-30 14:21:39 +00:00
|
|
|
#include "dsa_locl.h"
|
2008-03-16 21:05:46 +00:00
|
|
|
#include <openssl/bn.h>
|
2016-03-18 18:30:20 +00:00
|
|
|
#include <openssl/cms.h>
|
2015-03-23 18:42:42 +00:00
|
|
|
#include "internal/asn1_int.h"
|
2016-01-19 00:21:12 +00:00
|
|
|
#include "internal/evp_int.h"
|
2006-03-20 12:22:24 +00:00
|
|
|
|
|
|
|
static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
const unsigned char *p, *pm;
|
|
|
|
int pklen, pmlen;
|
|
|
|
int ptype;
|
2016-08-16 19:18:04 +00:00
|
|
|
const void *pval;
|
|
|
|
const ASN1_STRING *pstr;
|
2015-01-22 03:40:55 +00:00
|
|
|
X509_ALGOR *palg;
|
|
|
|
ASN1_INTEGER *public_key = NULL;
|
|
|
|
|
|
|
|
DSA *dsa = NULL;
|
|
|
|
|
|
|
|
if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
|
|
|
|
return 0;
|
|
|
|
X509_ALGOR_get0(NULL, &ptype, &pval, palg);
|
|
|
|
|
|
|
|
if (ptype == V_ASN1_SEQUENCE) {
|
|
|
|
pstr = pval;
|
|
|
|
pm = pstr->data;
|
|
|
|
pmlen = pstr->length;
|
|
|
|
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((dsa = DSA_new()) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASN1_INTEGER_free(public_key);
|
|
|
|
EVP_PKEY_assign_DSA(pkey, dsa);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2015-04-30 15:30:03 +00:00
|
|
|
ASN1_INTEGER_free(public_key);
|
2015-03-24 14:17:37 +00:00
|
|
|
DSA_free(dsa);
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2006-03-20 12:22:24 +00:00
|
|
|
|
2006-03-20 17:56:05 +00:00
|
|
|
static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
DSA *dsa;
|
|
|
|
int ptype;
|
|
|
|
unsigned char *penc = NULL;
|
|
|
|
int penclen;
|
2015-03-11 20:19:08 +00:00
|
|
|
ASN1_STRING *str = NULL;
|
2015-03-26 14:35:49 +00:00
|
|
|
ASN1_INTEGER *pubint = NULL;
|
2017-10-27 13:18:06 +00:00
|
|
|
ASN1_OBJECT *aobj;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
dsa = pkey->pkey.dsa;
|
|
|
|
if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
|
|
|
|
str = ASN1_STRING_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (str == NULL) {
|
2015-03-11 20:19:08 +00:00
|
|
|
DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
str->length = i2d_DSAparams(dsa, &str->data);
|
|
|
|
if (str->length <= 0) {
|
|
|
|
DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ptype = V_ASN1_SEQUENCE;
|
|
|
|
} else
|
|
|
|
ptype = V_ASN1_UNDEF;
|
|
|
|
|
2015-03-26 14:35:49 +00:00
|
|
|
pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2015-03-26 14:35:49 +00:00
|
|
|
if (pubint == NULL) {
|
|
|
|
DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
penclen = i2d_ASN1_INTEGER(pubint, &penc);
|
|
|
|
ASN1_INTEGER_free(pubint);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (penclen <= 0) {
|
|
|
|
DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-10-27 13:18:06 +00:00
|
|
|
aobj = OBJ_nid2obj(EVP_PKEY_DSA);
|
|
|
|
if (aobj == NULL)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(penc);
|
2015-03-24 11:52:24 +00:00
|
|
|
ASN1_STRING_free(str);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In PKCS#8 DSA: you just get a private key integer and parameters in the
|
2006-03-20 12:22:24 +00:00
|
|
|
* AlgorithmIdentifier the pubkey must be recalculated.
|
|
|
|
*/
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2016-08-16 23:21:55 +00:00
|
|
|
static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-02-18 13:18:48 +00:00
|
|
|
const unsigned char *p, *pm;
|
2015-01-22 03:40:55 +00:00
|
|
|
int pklen, pmlen;
|
|
|
|
int ptype;
|
2016-08-16 19:18:04 +00:00
|
|
|
const void *pval;
|
|
|
|
const ASN1_STRING *pstr;
|
2016-08-16 23:21:55 +00:00
|
|
|
const X509_ALGOR *palg;
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_INTEGER *privkey = NULL;
|
|
|
|
BN_CTX *ctx = NULL;
|
|
|
|
|
|
|
|
DSA *dsa = NULL;
|
|
|
|
|
2016-02-18 13:09:24 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
|
|
|
|
return 0;
|
|
|
|
X509_ALGOR_get0(NULL, &ptype, &pval, palg);
|
|
|
|
|
2016-02-18 13:09:24 +00:00
|
|
|
if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
|
|
|
|
goto decerr;
|
2016-02-18 13:18:48 +00:00
|
|
|
if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
|
2016-02-18 13:09:24 +00:00
|
|
|
goto decerr;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
pstr = pval;
|
|
|
|
pm = pstr->data;
|
|
|
|
pmlen = pstr->length;
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto decerr;
|
|
|
|
/* We have parameters now set private key */
|
2015-04-24 20:39:40 +00:00
|
|
|
if ((dsa->priv_key = BN_secure_new()) == NULL
|
|
|
|
|| !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
/* Calculate public key */
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((dsa->pub_key = BN_new()) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
2015-05-06 17:43:59 +00:00
|
|
|
if ((ctx = BN_CTX_new()) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
|
2017-09-29 11:29:25 +00:00
|
|
|
BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
|
|
|
|
DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
|
|
|
|
goto dsaerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_PKEY_assign_DSA(pkey, dsa);
|
|
|
|
|
2016-02-18 13:09:24 +00:00
|
|
|
ret = 1;
|
|
|
|
goto done;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
decerr:
|
2016-02-18 02:49:14 +00:00
|
|
|
DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
|
2015-01-22 03:40:55 +00:00
|
|
|
dsaerr:
|
2016-02-18 13:09:24 +00:00
|
|
|
DSA_free(dsa);
|
|
|
|
done:
|
2015-01-22 03:40:55 +00:00
|
|
|
BN_CTX_free(ctx);
|
2015-04-30 15:30:03 +00:00
|
|
|
ASN1_STRING_clear_free(privkey);
|
2016-02-18 13:09:24 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-03-20 12:22:24 +00:00
|
|
|
|
2006-03-20 17:56:05 +00:00
|
|
|
static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
|
2006-03-20 12:22:24 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_STRING *params = NULL;
|
|
|
|
ASN1_INTEGER *prkey = NULL;
|
|
|
|
unsigned char *dp = NULL;
|
|
|
|
int dplen;
|
|
|
|
|
|
|
|
if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
|
|
|
|
DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
params = ASN1_STRING_new();
|
|
|
|
|
2015-10-30 11:12:26 +00:00
|
|
|
if (params == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
|
|
|
|
if (params->length <= 0) {
|
|
|
|
DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
params->type = V_ASN1_SEQUENCE;
|
|
|
|
|
|
|
|
/* Get private key into integer */
|
|
|
|
prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
|
|
|
|
|
|
|
|
if (!prkey) {
|
|
|
|
DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
dplen = i2d_ASN1_INTEGER(prkey, &dp);
|
|
|
|
|
2015-03-03 14:20:23 +00:00
|
|
|
ASN1_STRING_clear_free(prkey);
|
2015-07-29 21:28:19 +00:00
|
|
|
prkey = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
|
|
|
|
V_ASN1_SEQUENCE, params, dp, dplen))
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2015-05-01 14:02:07 +00:00
|
|
|
OPENSSL_free(dp);
|
2015-03-24 11:52:24 +00:00
|
|
|
ASN1_STRING_free(params);
|
2015-04-30 15:30:03 +00:00
|
|
|
ASN1_STRING_clear_free(prkey);
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
2006-03-20 12:22:24 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 17:56:05 +00:00
|
|
|
static int int_dsa_size(const EVP_PKEY *pkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-10-17 14:04:09 +00:00
|
|
|
return DSA_size(pkey->pkey.dsa);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-03-20 17:56:05 +00:00
|
|
|
|
|
|
|
static int dsa_bits(const EVP_PKEY *pkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2016-08-01 17:37:03 +00:00
|
|
|
return DSA_bits(pkey->pkey.dsa);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-03-20 17:56:05 +00:00
|
|
|
|
2014-01-18 14:51:40 +00:00
|
|
|
static int dsa_security_bits(const EVP_PKEY *pkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return DSA_security_bits(pkey->pkey.dsa);
|
|
|
|
}
|
2014-01-18 14:51:40 +00:00
|
|
|
|
2006-03-20 17:56:05 +00:00
|
|
|
static int dsa_missing_parameters(const EVP_PKEY *pkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
DSA *dsa;
|
|
|
|
dsa = pkey->pkey.dsa;
|
2016-05-27 13:18:40 +00:00
|
|
|
if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-20 17:56:05 +00:00
|
|
|
|
|
|
|
static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BIGNUM *a;
|
|
|
|
|
2015-12-13 17:28:40 +00:00
|
|
|
if (to->pkey.dsa == NULL) {
|
|
|
|
to->pkey.dsa = DSA_new();
|
|
|
|
if (to->pkey.dsa == NULL)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
|
|
|
|
return 0;
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_free(to->pkey.dsa->p);
|
2015-01-22 03:40:55 +00:00
|
|
|
to->pkey.dsa->p = a;
|
|
|
|
|
|
|
|
if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
|
|
|
|
return 0;
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_free(to->pkey.dsa->q);
|
2015-01-22 03:40:55 +00:00
|
|
|
to->pkey.dsa->q = a;
|
|
|
|
|
|
|
|
if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
|
|
|
|
return 0;
|
2015-05-01 01:37:06 +00:00
|
|
|
BN_free(to->pkey.dsa->g);
|
2015-01-22 03:40:55 +00:00
|
|
|
to->pkey.dsa->g = a;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-20 17:56:05 +00:00
|
|
|
|
|
|
|
static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
|
|
|
|
BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
|
|
|
|
BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-20 17:56:05 +00:00
|
|
|
|
2006-04-12 17:01:19 +00:00
|
|
|
static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
2006-04-12 17:01:19 +00:00
|
|
|
|
2006-03-20 17:56:05 +00:00
|
|
|
static void int_dsa_free(EVP_PKEY *pkey)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
DSA_free(pkey->pkey.dsa);
|
|
|
|
}
|
2006-03-20 17:56:05 +00:00
|
|
|
|
2006-08-28 17:01:04 +00:00
|
|
|
static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
const char *ktype = NULL;
|
|
|
|
const BIGNUM *priv_key, *pub_key;
|
|
|
|
|
|
|
|
if (ptype == 2)
|
|
|
|
priv_key = x->priv_key;
|
|
|
|
else
|
|
|
|
priv_key = NULL;
|
|
|
|
|
|
|
|
if (ptype > 0)
|
|
|
|
pub_key = x->pub_key;
|
|
|
|
else
|
|
|
|
pub_key = NULL;
|
|
|
|
|
|
|
|
if (ptype == 2)
|
|
|
|
ktype = "Private-Key";
|
|
|
|
else if (ptype == 1)
|
|
|
|
ktype = "Public-Key";
|
|
|
|
else
|
|
|
|
ktype = "DSA-Parameters";
|
|
|
|
|
|
|
|
if (priv_key) {
|
|
|
|
if (!BIO_indent(bp, off, 128))
|
|
|
|
goto err;
|
|
|
|
if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
|
|
|
|
<= 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2016-02-14 03:33:56 +00:00
|
|
|
if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-02-14 03:33:56 +00:00
|
|
|
if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-02-14 03:33:56 +00:00
|
|
|
if (!ASN1_bn_print(bp, "P: ", x->p, NULL, off))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-02-14 03:33:56 +00:00
|
|
|
if (!ASN1_bn_print(bp, "Q: ", x->q, NULL, off))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-02-14 03:33:56 +00:00
|
|
|
if (!ASN1_bn_print(bp, "G: ", x->g, NULL, off))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-03-22 13:09:35 +00:00
|
|
|
|
2006-03-28 14:35:32 +00:00
|
|
|
static int dsa_param_decode(EVP_PKEY *pkey,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char **pder, int derlen)
|
|
|
|
{
|
|
|
|
DSA *dsa;
|
2015-05-06 17:43:59 +00:00
|
|
|
|
|
|
|
if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EVP_PKEY_assign_DSA(pkey, dsa);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-28 14:35:32 +00:00
|
|
|
|
|
|
|
static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return i2d_DSAparams(pkey->pkey.dsa, pder);
|
|
|
|
}
|
2006-03-22 13:09:35 +00:00
|
|
|
|
|
|
|
static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_PCTX *ctx)
|
|
|
|
{
|
|
|
|
return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
|
|
|
|
}
|
2006-03-22 13:09:35 +00:00
|
|
|
|
|
|
|
static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_PCTX *ctx)
|
|
|
|
{
|
|
|
|
return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
|
|
|
|
}
|
2006-03-22 13:09:35 +00:00
|
|
|
|
|
|
|
static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_PCTX *ctx)
|
|
|
|
{
|
|
|
|
return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
|
|
|
|
}
|
2006-03-22 13:09:35 +00:00
|
|
|
|
2006-03-23 18:02:23 +00:00
|
|
|
static int old_dsa_priv_decode(EVP_PKEY *pkey,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char **pder, int derlen)
|
|
|
|
{
|
|
|
|
DSA *dsa;
|
2015-05-06 17:43:59 +00:00
|
|
|
|
|
|
|
if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EVP_PKEY_assign_DSA(pkey, dsa);
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-23 18:02:23 +00:00
|
|
|
|
|
|
|
static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
|
|
|
|
}
|
2006-03-23 18:02:23 +00:00
|
|
|
|
2010-03-06 18:05:05 +00:00
|
|
|
static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
|
2015-01-22 03:40:55 +00:00
|
|
|
const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
|
|
|
|
{
|
|
|
|
DSA_SIG *dsa_sig;
|
|
|
|
const unsigned char *p;
|
2016-02-14 03:33:56 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!sig) {
|
|
|
|
if (BIO_puts(bp, "\n") <= 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p = sig->data;
|
|
|
|
dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
|
|
|
|
if (dsa_sig) {
|
|
|
|
int rv = 0;
|
2016-06-09 21:09:48 +00:00
|
|
|
const BIGNUM *r, *s;
|
2016-03-04 02:39:50 +00:00
|
|
|
|
2016-06-09 21:09:48 +00:00
|
|
|
DSA_SIG_get0(dsa_sig, &r, &s);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (BIO_write(bp, "\n", 1) != 1)
|
|
|
|
goto err;
|
|
|
|
|
2016-03-04 02:39:50 +00:00
|
|
|
if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
2016-03-04 02:39:50 +00:00
|
|
|
if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
rv = 1;
|
|
|
|
err:
|
|
|
|
DSA_SIG_free(dsa_sig);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return X509_signature_dump(bp, sig, indent);
|
|
|
|
}
|
2010-03-06 18:05:05 +00:00
|
|
|
|
2006-04-17 17:12:23 +00:00
|
|
|
static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case ASN1_PKEY_CTRL_PKCS7_SIGN:
|
|
|
|
if (arg1 == 0) {
|
|
|
|
int snid, hnid;
|
|
|
|
X509_ALGOR *alg1, *alg2;
|
|
|
|
PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
|
|
|
|
if (alg1 == NULL || alg1->algorithm == NULL)
|
|
|
|
return -1;
|
|
|
|
hnid = OBJ_obj2nid(alg1->algorithm);
|
|
|
|
if (hnid == NID_undef)
|
|
|
|
return -1;
|
|
|
|
if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
|
|
|
|
return -1;
|
|
|
|
X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
|
|
|
|
}
|
|
|
|
return 1;
|
2008-03-12 21:14:28 +00:00
|
|
|
#ifndef OPENSSL_NO_CMS
|
2015-01-22 03:40:55 +00:00
|
|
|
case ASN1_PKEY_CTRL_CMS_SIGN:
|
|
|
|
if (arg1 == 0) {
|
|
|
|
int snid, hnid;
|
|
|
|
X509_ALGOR *alg1, *alg2;
|
|
|
|
CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
|
|
|
|
if (alg1 == NULL || alg1->algorithm == NULL)
|
|
|
|
return -1;
|
|
|
|
hnid = OBJ_obj2nid(alg1->algorithm);
|
|
|
|
if (hnid == NID_undef)
|
|
|
|
return -1;
|
|
|
|
if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
|
|
|
|
return -1;
|
|
|
|
X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case ASN1_PKEY_CTRL_CMS_RI_TYPE:
|
|
|
|
*(int *)arg2 = CMS_RECIPINFO_NONE;
|
|
|
|
return 1;
|
2008-03-12 21:14:28 +00:00
|
|
|
#endif
|
2006-04-17 17:12:23 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
|
|
|
|
*(int *)arg2 = NID_sha256;
|
2019-05-27 14:52:03 +00:00
|
|
|
return 1;
|
2006-05-07 17:09:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
default:
|
|
|
|
return -2;
|
2006-04-17 17:12:23 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-04-17 17:12:23 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2006-04-17 17:12:23 +00:00
|
|
|
|
2006-03-20 12:22:24 +00:00
|
|
|
/* NB these are sorted in pkey_id order, lowest first */
|
|
|
|
|
2016-05-27 19:10:05 +00:00
|
|
|
const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
EVP_PKEY_DSA2,
|
|
|
|
EVP_PKEY_DSA,
|
|
|
|
ASN1_PKEY_ALIAS},
|
|
|
|
|
|
|
|
{
|
|
|
|
EVP_PKEY_DSA1,
|
|
|
|
EVP_PKEY_DSA,
|
|
|
|
ASN1_PKEY_ALIAS},
|
|
|
|
|
|
|
|
{
|
|
|
|
EVP_PKEY_DSA4,
|
|
|
|
EVP_PKEY_DSA,
|
|
|
|
ASN1_PKEY_ALIAS},
|
|
|
|
|
|
|
|
{
|
|
|
|
EVP_PKEY_DSA3,
|
|
|
|
EVP_PKEY_DSA,
|
|
|
|
ASN1_PKEY_ALIAS},
|
|
|
|
|
|
|
|
{
|
|
|
|
EVP_PKEY_DSA,
|
|
|
|
EVP_PKEY_DSA,
|
|
|
|
0,
|
|
|
|
|
|
|
|
"DSA",
|
|
|
|
"OpenSSL DSA method",
|
|
|
|
|
|
|
|
dsa_pub_decode,
|
|
|
|
dsa_pub_encode,
|
|
|
|
dsa_pub_cmp,
|
|
|
|
dsa_pub_print,
|
|
|
|
|
|
|
|
dsa_priv_decode,
|
|
|
|
dsa_priv_encode,
|
|
|
|
dsa_priv_print,
|
|
|
|
|
|
|
|
int_dsa_size,
|
|
|
|
dsa_bits,
|
|
|
|
dsa_security_bits,
|
|
|
|
|
|
|
|
dsa_param_decode,
|
|
|
|
dsa_param_encode,
|
|
|
|
dsa_missing_parameters,
|
|
|
|
dsa_copy_parameters,
|
|
|
|
dsa_cmp_parameters,
|
|
|
|
dsa_param_print,
|
|
|
|
dsa_sig_print,
|
|
|
|
|
|
|
|
int_dsa_free,
|
|
|
|
dsa_pkey_ctrl,
|
|
|
|
old_dsa_priv_decode,
|
|
|
|
old_dsa_priv_encode}
|
|
|
|
};
|