提交 706a13f1 编写于 作者: D Dr. Stephen Henson

Make DSA_SIG opaque.

This adds a new accessor function DSA_SIG_get0.
The customisation of DSA_SIG structure initialisation has been removed this
means that the 'r' and 's' components are automatically allocated when
DSA_SIG_new() is called. Update documentation.
Reviewed-by: NRich Salz <rsalz@openssl.org>
上级 9cae86d5
...@@ -488,13 +488,16 @@ static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, ...@@ -488,13 +488,16 @@ static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length); dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
if (dsa_sig) { if (dsa_sig) {
int rv = 0; int rv = 0;
BIGNUM *r, *s;
DSA_SIG_get0(&r, &s, dsa_sig);
if (BIO_write(bp, "\n", 1) != 1) if (BIO_write(bp, "\n", 1) != 1)
goto err; goto err;
if (!ASN1_bn_print(bp, "r: ", dsa_sig->r, NULL, indent)) if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
goto err; goto err;
if (!ASN1_bn_print(bp, "s: ", dsa_sig->s, NULL, indent)) if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
goto err; goto err;
rv = 1; rv = 1;
err: err:
......
...@@ -62,33 +62,21 @@ ...@@ -62,33 +62,21 @@
#include <openssl/asn1.h> #include <openssl/asn1.h>
#include <openssl/asn1t.h> #include <openssl/asn1t.h>
#include <openssl/rand.h> #include <openssl/rand.h>
#include "dsa_locl.h"
/* Override the default new methods */ ASN1_SEQUENCE(DSA_SIG) = {
static int sig_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
void *exarg)
{
if (operation == ASN1_OP_NEW_PRE) {
DSA_SIG *sig;
sig = OPENSSL_malloc(sizeof(*sig));
if (sig == NULL) {
DSAerr(DSA_F_SIG_CB, ERR_R_MALLOC_FAILURE);
return 0;
}
sig->r = NULL;
sig->s = NULL;
*pval = (ASN1_VALUE *)sig;
return 2;
}
return 1;
}
ASN1_SEQUENCE_cb(DSA_SIG, sig_cb) = {
ASN1_SIMPLE(DSA_SIG, r, CBIGNUM), ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
ASN1_SIMPLE(DSA_SIG, s, CBIGNUM) ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
} static_ASN1_SEQUENCE_END_cb(DSA_SIG, DSA_SIG) } static_ASN1_SEQUENCE_END(DSA_SIG)
IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG) IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, DSA_SIG *sig)
{
*pr = sig->r;
*ps = sig->s;
}
/* Override the default free and new methods */ /* Override the default free and new methods */
static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
void *exarg) void *exarg)
......
...@@ -65,3 +65,8 @@ int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N, ...@@ -65,3 +65,8 @@ int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
size_t seed_len, int idx, unsigned char *seed_out, size_t seed_len, int idx, unsigned char *seed_out,
int *counter_ret, unsigned long *h_ret, int *counter_ret, unsigned long *h_ret,
BN_GENCB *cb); BN_GENCB *cb);
struct DSA_SIG_st {
BIGNUM *r;
BIGNUM *s;
};
...@@ -133,13 +133,15 @@ const DSA_METHOD *DSA_OpenSSL(void) ...@@ -133,13 +133,15 @@ const DSA_METHOD *DSA_OpenSSL(void)
static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
{ {
BIGNUM *kinv = NULL, *r = NULL, *s = NULL; BIGNUM *kinv = NULL;
BIGNUM *m; BIGNUM *m;
BIGNUM *xr; BIGNUM *xr;
BIGNUM *r, *s;
BN_CTX *ctx = NULL; BN_CTX *ctx = NULL;
int reason = ERR_R_BN_LIB; int reason = ERR_R_BN_LIB;
DSA_SIG *ret = NULL; DSA_SIG *ret = NULL;
int noredo = 0; int noredo = 0;
int rv = 0;
m = BN_new(); m = BN_new();
xr = BN_new(); xr = BN_new();
...@@ -151,9 +153,12 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) ...@@ -151,9 +153,12 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
goto err; goto err;
} }
s = BN_new(); ret = DSA_SIG_new();
if (s == NULL) if (ret == NULL)
goto err; goto err;
DSA_SIG_get0(&r, &s, ret);
ctx = BN_CTX_new(); ctx = BN_CTX_new();
if (ctx == NULL) if (ctx == NULL)
goto err; goto err;
...@@ -193,23 +198,20 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa) ...@@ -193,23 +198,20 @@ static DSA_SIG *dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
} }
goto redo; goto redo;
} }
ret = DSA_SIG_new();
if (ret == NULL) rv = 1;
goto err;
ret->r = r;
ret->s = s;
err: err:
if (ret == NULL) { if (rv == 0) {
DSAerr(DSA_F_DSA_DO_SIGN, reason); DSAerr(DSA_F_DSA_DO_SIGN, reason);
BN_free(r); DSA_SIG_free(ret);
BN_free(s); ret = NULL;
} }
BN_CTX_free(ctx); BN_CTX_free(ctx);
BN_clear_free(m); BN_clear_free(m);
BN_clear_free(xr); BN_clear_free(xr);
BN_clear_free(kinv); BN_clear_free(kinv);
return (ret); return ret;
} }
static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in, static int dsa_sign_setup_no_digest(DSA *dsa, BN_CTX *ctx_in,
...@@ -223,7 +225,7 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, ...@@ -223,7 +225,7 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
const unsigned char *dgst, int dlen) const unsigned char *dgst, int dlen)
{ {
BN_CTX *ctx = NULL; BN_CTX *ctx = NULL;
BIGNUM *k, *kq, *K, *kinv = NULL, *r = NULL; BIGNUM *k, *kq, *K, *kinv = NULL, *r = *rp;
int ret = 0; int ret = 0;
if (!dsa->p || !dsa->q || !dsa->g) { if (!dsa->p || !dsa->q || !dsa->g) {
...@@ -242,9 +244,6 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, ...@@ -242,9 +244,6 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
} else } else
ctx = ctx_in; ctx = ctx_in;
if ((r = BN_new()) == NULL)
goto err;
/* Get random k */ /* Get random k */
do { do {
if (dgst != NULL) { if (dgst != NULL) {
...@@ -305,19 +304,15 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in, ...@@ -305,19 +304,15 @@ static int dsa_sign_setup(DSA *dsa, BN_CTX *ctx_in,
BN_clear_free(*kinvp); BN_clear_free(*kinvp);
*kinvp = kinv; *kinvp = kinv;
kinv = NULL; kinv = NULL;
BN_clear_free(*rp);
*rp = r;
ret = 1; ret = 1;
err: err:
if (!ret) { if (!ret)
DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB); DSAerr(DSA_F_DSA_SIGN_SETUP, ERR_R_BN_LIB);
BN_clear_free(r);
}
if (ctx != ctx_in) if (ctx != ctx_in)
BN_CTX_free(ctx); BN_CTX_free(ctx);
BN_clear_free(k); BN_clear_free(k);
BN_clear_free(kq); BN_clear_free(kq);
return (ret); return ret;
} }
static int dsa_do_verify(const unsigned char *dgst, int dgst_len, static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
...@@ -326,6 +321,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, ...@@ -326,6 +321,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
BN_CTX *ctx; BN_CTX *ctx;
BIGNUM *u1, *u2, *t1; BIGNUM *u1, *u2, *t1;
BN_MONT_CTX *mont = NULL; BN_MONT_CTX *mont = NULL;
BIGNUM *r, *s;
int ret = -1, i; int ret = -1, i;
if (!dsa->p || !dsa->q || !dsa->g) { if (!dsa->p || !dsa->q || !dsa->g) {
DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS); DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);
...@@ -350,13 +346,15 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, ...@@ -350,13 +346,15 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL) if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)
goto err; goto err;
if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || DSA_SIG_get0(&r, &s, sig);
BN_ucmp(sig->r, dsa->q) >= 0) {
if (BN_is_zero(r) || BN_is_negative(r) ||
BN_ucmp(r, dsa->q) >= 0) {
ret = 0; ret = 0;
goto err; goto err;
} }
if (BN_is_zero(sig->s) || BN_is_negative(sig->s) || if (BN_is_zero(s) || BN_is_negative(s) ||
BN_ucmp(sig->s, dsa->q) >= 0) { BN_ucmp(s, dsa->q) >= 0) {
ret = 0; ret = 0;
goto err; goto err;
} }
...@@ -364,7 +362,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, ...@@ -364,7 +362,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
/* /*
* Calculate W = inv(S) mod Q save W in u2 * Calculate W = inv(S) mod Q save W in u2
*/ */
if ((BN_mod_inverse(u2, sig->s, dsa->q, ctx)) == NULL) if ((BN_mod_inverse(u2, s, dsa->q, ctx)) == NULL)
goto err; goto err;
/* save M in u1 */ /* save M in u1 */
...@@ -383,7 +381,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, ...@@ -383,7 +381,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
goto err; goto err;
/* u2 = r * w mod q */ /* u2 = r * w mod q */
if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx)) if (!BN_mod_mul(u2, r, u2, dsa->q, ctx))
goto err; goto err;
if (dsa->flags & DSA_FLAG_CACHE_MONT_P) { if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {
...@@ -403,7 +401,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len, ...@@ -403,7 +401,7 @@ static int dsa_do_verify(const unsigned char *dgst, int dgst_len,
/* /*
* V is now in u1. If the signature is correct, it will be equal to R. * V is now in u1. If the signature is correct, it will be equal to R.
*/ */
ret = (BN_ucmp(u1, sig->r) == 0); ret = (BN_ucmp(u1, r) == 0);
err: err:
if (ret < 0) if (ret < 0)
......
...@@ -9,8 +9,8 @@ DSA_SIG_new, DSA_SIG_free - allocate and free DSA signature objects ...@@ -9,8 +9,8 @@ DSA_SIG_new, DSA_SIG_free - allocate and free DSA signature objects
#include <openssl/dsa.h> #include <openssl/dsa.h>
DSA_SIG *DSA_SIG_new(void); DSA_SIG *DSA_SIG_new(void);
void DSA_SIG_free(DSA_SIG *a); void DSA_SIG_free(DSA_SIG *a);
void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, DSA_SIG *sig);
=head1 DESCRIPTION =head1 DESCRIPTION
...@@ -19,6 +19,9 @@ DSA_SIG_new() allocates and initializes a B<DSA_SIG> structure. ...@@ -19,6 +19,9 @@ DSA_SIG_new() allocates and initializes a B<DSA_SIG> structure.
DSA_SIG_free() frees the B<DSA_SIG> structure and its components. The DSA_SIG_free() frees the B<DSA_SIG> structure and its components. The
values are erased before the memory is returned to the system. values are erased before the memory is returned to the system.
DSA_SIG_get0() returns internal pointers the B<r> and B<s> values contained
in B<sig>. The values can then be examined or initialised.
=head1 RETURN VALUES =head1 RETURN VALUES
If the allocation fails, DSA_SIG_new() returns B<NULL> and sets an If the allocation fails, DSA_SIG_new() returns B<NULL> and sets an
......
...@@ -121,10 +121,7 @@ extern "C" { ...@@ -121,10 +121,7 @@ extern "C" {
/* typedef struct dsa_st DSA; */ /* typedef struct dsa_st DSA; */
/* typedef struct dsa_method DSA_METHOD; */ /* typedef struct dsa_method DSA_METHOD; */
typedef struct DSA_SIG_st { typedef struct DSA_SIG_st DSA_SIG;
BIGNUM *r;
BIGNUM *s;
} DSA_SIG;
struct dsa_method { struct dsa_method {
const char *name; const char *name;
...@@ -187,6 +184,7 @@ DSA_SIG *DSA_SIG_new(void); ...@@ -187,6 +184,7 @@ DSA_SIG *DSA_SIG_new(void);
void DSA_SIG_free(DSA_SIG *a); void DSA_SIG_free(DSA_SIG *a);
int i2d_DSA_SIG(const DSA_SIG *a, unsigned char **pp); int i2d_DSA_SIG(const DSA_SIG *a, unsigned char **pp);
DSA_SIG *d2i_DSA_SIG(DSA_SIG **v, const unsigned char **pp, long length); DSA_SIG *d2i_DSA_SIG(DSA_SIG **v, const unsigned char **pp, long length);
void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, DSA_SIG *sig);
DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa); DSA_SIG *DSA_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
int DSA_do_verify(const unsigned char *dgst, int dgst_len, int DSA_do_verify(const unsigned char *dgst, int dgst_len,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册