Ensure that NULL r and s parameters cannot be set on DSA_SIG/ECDSA_SIGs.
[openssl.git] / crypto / dsa / dsa_asn1.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "dsa_locl.h"
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/rand.h>
16
17 struct DSA_SIG_st {
18     BIGNUM *r;
19     BIGNUM *s;
20 };
21
22 ASN1_SEQUENCE(DSA_SIG) = {
23         ASN1_SIMPLE(DSA_SIG, r, CBIGNUM),
24         ASN1_SIMPLE(DSA_SIG, s, CBIGNUM)
25 } static_ASN1_SEQUENCE_END(DSA_SIG)
26
27 IMPLEMENT_ASN1_FUNCTIONS_const(DSA_SIG)
28
29 void DSA_SIG_get0(BIGNUM **pr, BIGNUM **ps, const DSA_SIG *sig)
30 {
31     *pr = sig->r;
32     *ps = sig->s;
33 }
34
35 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
36 {
37     if (r == NULL || s == NULL)
38         return 0;
39     BN_clear_free(sig->r);
40     BN_clear_free(sig->s);
41     sig->r = r;
42     sig->s = s;
43     return 1;
44 }
45
46 /* Override the default free and new methods */
47 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
48                   void *exarg)
49 {
50     if (operation == ASN1_OP_NEW_PRE) {
51         *pval = (ASN1_VALUE *)DSA_new();
52         if (*pval != NULL)
53             return 2;
54         return 0;
55     } else if (operation == ASN1_OP_FREE_PRE) {
56         DSA_free((DSA *)*pval);
57         *pval = NULL;
58         return 2;
59     }
60     return 1;
61 }
62
63 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
64         ASN1_SIMPLE(DSA, version, LONG),
65         ASN1_SIMPLE(DSA, p, BIGNUM),
66         ASN1_SIMPLE(DSA, q, BIGNUM),
67         ASN1_SIMPLE(DSA, g, BIGNUM),
68         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
69         ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
70 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
71
72 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPrivateKey, DSAPrivateKey)
73
74 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
75         ASN1_SIMPLE(DSA, p, BIGNUM),
76         ASN1_SIMPLE(DSA, q, BIGNUM),
77         ASN1_SIMPLE(DSA, g, BIGNUM),
78 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
79
80 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAparams, DSAparams)
81
82 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
83         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
84         ASN1_SIMPLE(DSA, p, BIGNUM),
85         ASN1_SIMPLE(DSA, q, BIGNUM),
86         ASN1_SIMPLE(DSA, g, BIGNUM)
87 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
88
89 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(DSA, DSAPublicKey, DSAPublicKey)
90
91 DSA *DSAparams_dup(DSA *dsa)
92 {
93     return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
94 }
95
96 int DSA_sign(int type, const unsigned char *dgst, int dlen,
97              unsigned char *sig, unsigned int *siglen, DSA *dsa)
98 {
99     DSA_SIG *s;
100     RAND_seed(dgst, dlen);
101     s = DSA_do_sign(dgst, dlen, dsa);
102     if (s == NULL) {
103         *siglen = 0;
104         return (0);
105     }
106     *siglen = i2d_DSA_SIG(s, &sig);
107     DSA_SIG_free(s);
108     return (1);
109 }
110
111 /* data has already been hashed (probably with SHA or SHA-1). */
112 /*-
113  * returns
114  *      1: correct signature
115  *      0: incorrect signature
116  *     -1: error
117  */
118 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
119                const unsigned char *sigbuf, int siglen, DSA *dsa)
120 {
121     DSA_SIG *s;
122     const unsigned char *p = sigbuf;
123     unsigned char *der = NULL;
124     int derlen = -1;
125     int ret = -1;
126
127     s = DSA_SIG_new();
128     if (s == NULL)
129         return (ret);
130     if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
131         goto err;
132     /* Ensure signature uses DER and doesn't have trailing garbage */
133     derlen = i2d_DSA_SIG(s, &der);
134     if (derlen != siglen || memcmp(sigbuf, der, derlen))
135         goto err;
136     ret = DSA_do_verify(dgst, dgst_len, s, dsa);
137  err:
138     OPENSSL_clear_free(der, derlen);
139     DSA_SIG_free(s);
140     return (ret);
141 }