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