Add simple ASN.1 utils for DSA signature DER.
[openssl.git] / crypto / dsa / dsa_asn1.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 #include "internal/asn1_dsa.h"
17
18 DSA_SIG *DSA_SIG_new(void)
19 {
20     DSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig));
21     if (sig == NULL)
22         DSAerr(DSA_F_DSA_SIG_NEW, ERR_R_MALLOC_FAILURE);
23     return sig;
24 }
25
26 void DSA_SIG_free(DSA_SIG *sig)
27 {
28     if (sig == NULL)
29         return;
30     BN_clear_free(sig->r);
31     BN_clear_free(sig->s);
32     OPENSSL_free(sig);
33 }
34
35 DSA_SIG *d2i_DSA_SIG(DSA_SIG **psig, const unsigned char **ppin, long len)
36 {
37     DSA_SIG *sig;
38
39     if (len < 0)
40         return NULL;
41     if (psig != NULL && *psig != NULL) {
42         sig = *psig;
43     } else {
44         sig = DSA_SIG_new();
45         if (sig == NULL)
46             return NULL;
47     }
48     if (sig->r == NULL)
49         sig->r = BN_new();
50     if (sig->s == NULL)
51         sig->s = BN_new();
52     if (decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) {
53         if (psig == NULL || *psig == NULL)
54             DSA_SIG_free(sig);
55         return NULL;
56     }
57     if (psig != NULL && *psig == NULL)
58         *psig = sig;
59     return sig;
60 }
61
62 int i2d_DSA_SIG(const DSA_SIG *sig, unsigned char **ppout)
63 {
64     unsigned char *buf = NULL;
65     unsigned char *tmp;
66     unsigned char **pp = NULL;
67     size_t len;
68     size_t encoded_len;
69
70     if (ppout != NULL && *ppout == NULL) {
71         if ((len = encode_der_dsa_sig(sig->r, sig->s, NULL, SIZE_MAX)) == 0)
72             return -1;
73         buf = OPENSSL_malloc(len);
74         if (buf == NULL)
75             return -1;
76         tmp = buf;
77         pp = &tmp;
78     } else {
79         len = SIZE_MAX;
80         pp = ppout;
81     }
82     if ((encoded_len = encode_der_dsa_sig(sig->r, sig->s, pp, len)) == 0) {
83         OPENSSL_free(buf);
84         return -1;
85     }
86     if (buf != NULL)
87         *ppout = buf;
88     return (int)encoded_len;
89 }
90
91 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
92 {
93     if (pr != NULL)
94         *pr = sig->r;
95     if (ps != NULL)
96         *ps = sig->s;
97 }
98
99 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
100 {
101     if (r == NULL || s == NULL)
102         return 0;
103     BN_clear_free(sig->r);
104     BN_clear_free(sig->s);
105     sig->r = r;
106     sig->s = s;
107     return 1;
108 }
109
110 /* Override the default free and new methods */
111 static int dsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
112                   void *exarg)
113 {
114     if (operation == ASN1_OP_NEW_PRE) {
115         *pval = (ASN1_VALUE *)DSA_new();
116         if (*pval != NULL)
117             return 2;
118         return 0;
119     } else if (operation == ASN1_OP_FREE_PRE) {
120         DSA_free((DSA *)*pval);
121         *pval = NULL;
122         return 2;
123     }
124     return 1;
125 }
126
127 ASN1_SEQUENCE_cb(DSAPrivateKey, dsa_cb) = {
128         ASN1_EMBED(DSA, version, INT32),
129         ASN1_SIMPLE(DSA, p, BIGNUM),
130         ASN1_SIMPLE(DSA, q, BIGNUM),
131         ASN1_SIMPLE(DSA, g, BIGNUM),
132         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
133         ASN1_SIMPLE(DSA, priv_key, CBIGNUM)
134 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPrivateKey)
135
136 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPrivateKey, DSAPrivateKey)
137
138 ASN1_SEQUENCE_cb(DSAparams, dsa_cb) = {
139         ASN1_SIMPLE(DSA, p, BIGNUM),
140         ASN1_SIMPLE(DSA, q, BIGNUM),
141         ASN1_SIMPLE(DSA, g, BIGNUM),
142 } static_ASN1_SEQUENCE_END_cb(DSA, DSAparams)
143
144 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAparams, DSAparams)
145
146 ASN1_SEQUENCE_cb(DSAPublicKey, dsa_cb) = {
147         ASN1_SIMPLE(DSA, pub_key, BIGNUM),
148         ASN1_SIMPLE(DSA, p, BIGNUM),
149         ASN1_SIMPLE(DSA, q, BIGNUM),
150         ASN1_SIMPLE(DSA, g, BIGNUM)
151 } static_ASN1_SEQUENCE_END_cb(DSA, DSAPublicKey)
152
153 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(DSA, DSAPublicKey, DSAPublicKey)
154
155 DSA *DSAparams_dup(const DSA *dsa)
156 {
157     return ASN1_item_dup(ASN1_ITEM_rptr(DSAparams), dsa);
158 }
159
160 int DSA_sign(int type, const unsigned char *dgst, int dlen,
161              unsigned char *sig, unsigned int *siglen, DSA *dsa)
162 {
163     DSA_SIG *s;
164
165     s = DSA_do_sign(dgst, dlen, dsa);
166     if (s == NULL) {
167         *siglen = 0;
168         return 0;
169     }
170     *siglen = i2d_DSA_SIG(s, &sig);
171     DSA_SIG_free(s);
172     return 1;
173 }
174
175 /* data has already been hashed (probably with SHA or SHA-1). */
176 /*-
177  * returns
178  *      1: correct signature
179  *      0: incorrect signature
180  *     -1: error
181  */
182 int DSA_verify(int type, const unsigned char *dgst, int dgst_len,
183                const unsigned char *sigbuf, int siglen, DSA *dsa)
184 {
185     DSA_SIG *s;
186     const unsigned char *p = sigbuf;
187     unsigned char *der = NULL;
188     int derlen = -1;
189     int ret = -1;
190
191     s = DSA_SIG_new();
192     if (s == NULL)
193         return ret;
194     if (d2i_DSA_SIG(&s, &p, siglen) == NULL)
195         goto err;
196     /* Ensure signature uses DER and doesn't have trailing garbage */
197     derlen = i2d_DSA_SIG(s, &der);
198     if (derlen != siglen || memcmp(sigbuf, der, derlen))
199         goto err;
200     ret = DSA_do_verify(dgst, dgst_len, s, dsa);
201  err:
202     OPENSSL_clear_free(der, derlen);
203     DSA_SIG_free(s);
204     return ret;
205 }