blob: eb8e61f5402201110981a21d231e975cc2f40b1d [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 *
57 * The DSS routines are based on patches supplied by
58 * Steven Schoch <schoch@sheba.arc.nasa.gov>. */
59
60#include <openssl/dsa.h>
61
Adam Langley2b2d66d2015-01-30 17:08:37 -080062#include <string.h>
63
Adam Langley95c29f32014-06-20 12:00:00 -070064#include <openssl/asn1.h>
Adam Langleybed8ce72014-09-05 17:04:51 -070065#include <openssl/dh.h>
Adam Langley95c29f32014-06-20 12:00:00 -070066#include <openssl/engine.h>
67#include <openssl/err.h>
68#include <openssl/ex_data.h>
69#include <openssl/mem.h>
Brian Smith054e6822015-03-27 21:12:01 -100070#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070071
72#include "internal.h"
Adam Langley683d7bd2015-04-13 11:04:14 -070073#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070074
Adam Langley2b2d66d2015-01-30 17:08:37 -080075
Adam Langley95c29f32014-06-20 12:00:00 -070076extern const DSA_METHOD DSA_default_method;
77
David Benjamin9f33fc62015-04-15 17:29:53 -040078static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
79
Adam Langley95c29f32014-06-20 12:00:00 -070080DSA *DSA_new(void) { return DSA_new_method(NULL); }
81
82DSA *DSA_new_method(const ENGINE *engine) {
83 DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA));
84 if (dsa == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040085 OPENSSL_PUT_ERROR(DSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070086 return NULL;
87 }
88
89 memset(dsa, 0, sizeof(DSA));
90
91 if (engine) {
92 dsa->meth = ENGINE_get_DSA_method(engine);
93 }
94
95 if (dsa->meth == NULL) {
96 dsa->meth = (DSA_METHOD*) &DSA_default_method;
97 }
98 METHOD_ref(dsa->meth);
99
100 dsa->write_params = 1;
101 dsa->references = 1;
102
Adam Langley683d7bd2015-04-13 11:04:14 -0700103 CRYPTO_MUTEX_init(&dsa->method_mont_p_lock);
104
David Benjamin9f33fc62015-04-15 17:29:53 -0400105 if (!CRYPTO_new_ex_data(&g_ex_data_class, dsa, &dsa->ex_data)) {
David Benjamin79680ff2015-10-23 18:46:16 -0400106 CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700107 METHOD_unref(dsa->meth);
108 OPENSSL_free(dsa);
109 return NULL;
110 }
111
112 if (dsa->meth->init && !dsa->meth->init(dsa)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400113 CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
David Benjamin79680ff2015-10-23 18:46:16 -0400114 CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700115 METHOD_unref(dsa->meth);
116 OPENSSL_free(dsa);
117 return NULL;
118 }
119
120 return dsa;
121}
122
123void DSA_free(DSA *dsa) {
124 if (dsa == NULL) {
125 return;
126 }
127
Adam Langley0da323a2015-05-15 12:49:30 -0700128 if (!CRYPTO_refcount_dec_and_test_zero(&dsa->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700129 return;
130 }
131
132 if (dsa->meth->finish) {
133 dsa->meth->finish(dsa);
134 }
135 METHOD_unref(dsa->meth);
136
David Benjamin9f33fc62015-04-15 17:29:53 -0400137 CRYPTO_free_ex_data(&g_ex_data_class, dsa, &dsa->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700138
David Benjamin22ccc2d2015-04-22 13:50:28 -0400139 BN_clear_free(dsa->p);
140 BN_clear_free(dsa->q);
141 BN_clear_free(dsa->g);
142 BN_clear_free(dsa->pub_key);
143 BN_clear_free(dsa->priv_key);
144 BN_clear_free(dsa->kinv);
145 BN_clear_free(dsa->r);
Adam Langley683d7bd2015-04-13 11:04:14 -0700146 CRYPTO_MUTEX_cleanup(&dsa->method_mont_p_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700147 OPENSSL_free(dsa);
148}
149
150int DSA_up_ref(DSA *dsa) {
Adam Langley0da323a2015-05-15 12:49:30 -0700151 CRYPTO_refcount_inc(&dsa->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700152 return 1;
153}
154
155int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in,
156 size_t seed_len, int *out_counter,
157 unsigned long *out_h, BN_GENCB *cb) {
158 if (dsa->meth->generate_parameters) {
159 return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len,
160 out_counter, out_h, cb);
161 }
162 return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len,
163 out_counter, out_h, cb);
164}
165
166int DSA_generate_key(DSA *dsa) {
167 if (dsa->meth->keygen) {
168 return dsa->meth->keygen(dsa);
169 }
170 return DSA_default_method.keygen(dsa);
171}
172
173DSA_SIG *DSA_SIG_new(void) {
174 DSA_SIG *sig;
175 sig = OPENSSL_malloc(sizeof(DSA_SIG));
176 if (!sig) {
177 return NULL;
178 }
179 sig->r = NULL;
180 sig->s = NULL;
181 return sig;
182}
183
184void DSA_SIG_free(DSA_SIG *sig) {
185 if (!sig) {
186 return;
187 }
188
David Benjamin22ccc2d2015-04-22 13:50:28 -0400189 BN_free(sig->r);
190 BN_free(sig->s);
Adam Langley95c29f32014-06-20 12:00:00 -0700191 OPENSSL_free(sig);
192}
193
194DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) {
195 if (dsa->meth->sign) {
196 return dsa->meth->sign(digest, digest_len, dsa);
197 }
198 return DSA_default_method.sign(digest, digest_len, dsa);
199}
200
201int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig,
202 const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500203 int valid;
204 if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700205 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700206 }
David Benjamin95e18c52015-01-10 23:37:17 -0500207 return valid;
Adam Langley95c29f32014-06-20 12:00:00 -0700208}
209
210int DSA_do_check_signature(int *out_valid, const uint8_t *digest,
211 size_t digest_len, DSA_SIG *sig, const DSA *dsa) {
212 if (dsa->meth->verify) {
213 return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa);
214 }
215
216 return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa);
217}
218
219int DSA_sign(int type, const uint8_t *digest, size_t digest_len,
220 uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) {
221 DSA_SIG *s;
222
223 s = DSA_do_sign(digest, digest_len, dsa);
224 if (s == NULL) {
225 *out_siglen = 0;
226 return 0;
227 }
228
229 *out_siglen = i2d_DSA_SIG(s, &out_sig);
230 DSA_SIG_free(s);
231 return 1;
232}
233
234int DSA_verify(int type, const uint8_t *digest, size_t digest_len,
235 const uint8_t *sig, size_t sig_len, const DSA *dsa) {
David Benjamin95e18c52015-01-10 23:37:17 -0500236 int valid;
237 if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) {
238 return -1;
239 }
240 return valid;
241}
242
243int DSA_check_signature(int *out_valid, const uint8_t *digest,
244 size_t digest_len, const uint8_t *sig, size_t sig_len,
245 const DSA *dsa) {
Adam Langley95c29f32014-06-20 12:00:00 -0700246 DSA_SIG *s = NULL;
David Benjamin95e18c52015-01-10 23:37:17 -0500247 int ret = 0;
Adam Langleyca9a5382015-01-08 12:26:55 -0800248 uint8_t *der = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700249
250 s = DSA_SIG_new();
251 if (s == NULL) {
252 goto err;
253 }
254
Adam Langleyca9a5382015-01-08 12:26:55 -0800255 const uint8_t *sigp = sig;
256 if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) {
257 goto err;
258 }
259
260 /* Ensure that the signature uses DER and doesn't have trailing garbage. */
261 int der_len = i2d_DSA_SIG(s, &der);
262 if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) {
Adam Langley95c29f32014-06-20 12:00:00 -0700263 goto err;
264 }
265
David Benjamin95e18c52015-01-10 23:37:17 -0500266 ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa);
Adam Langley95c29f32014-06-20 12:00:00 -0700267
268err:
David Benjamin22ccc2d2015-04-22 13:50:28 -0400269 OPENSSL_free(der);
270 DSA_SIG_free(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700271 return ret;
272}
273
Adam Langley95c29f32014-06-20 12:00:00 -0700274int DSA_size(const DSA *dsa) {
275 int ret, i;
276 ASN1_INTEGER bs;
277 unsigned char buf[4]; /* 4 bytes looks really small.
278 However, i2d_ASN1_INTEGER() will not look
279 beyond the first byte, as long as the second
280 parameter is NULL. */
281
282 i = BN_num_bits(dsa->q);
283 bs.length = (i + 7) / 8;
284 bs.data = buf;
285 bs.type = V_ASN1_INTEGER;
286 /* If the top bit is set the asn1 encoding is 1 larger. */
287 buf[0] = 0xff;
288
289 i = i2d_ASN1_INTEGER(&bs, NULL);
290 i += i; /* r and s */
291 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
292 return ret;
293}
294
295int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv,
296 BIGNUM **out_r) {
297 if (dsa->meth->sign_setup) {
Adam Langleyd4b4f082014-06-20 12:00:00 -0700298 return dsa->meth->sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700299 }
300
Adam Langleyd4b4f082014-06-20 12:00:00 -0700301 return DSA_default_method.sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700302}
303
304int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
305 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400306 int index;
307 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
308 dup_func, free_func)) {
309 return -1;
310 }
311 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700312}
313
314int DSA_set_ex_data(DSA *d, int idx, void *arg) {
315 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
316}
317
318void *DSA_get_ex_data(const DSA *d, int idx) {
319 return CRYPTO_get_ex_data(&d->ex_data, idx);
320}
Adam Langleybed8ce72014-09-05 17:04:51 -0700321
322DH *DSA_dup_DH(const DSA *r) {
323 DH *ret = NULL;
324
325 if (r == NULL) {
326 goto err;
327 }
328 ret = DH_new();
329 if (ret == NULL) {
330 goto err;
331 }
332 if (r->q != NULL) {
333 ret->priv_length = BN_num_bits(r->q);
334 if ((ret->q = BN_dup(r->q)) == NULL) {
335 goto err;
336 }
337 }
338 if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) ||
339 (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) ||
340 (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) ||
341 (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) {
342 goto err;
343 }
344
345 return ret;
346
347err:
David Benjamin22ccc2d2015-04-22 13:50:28 -0400348 DH_free(ret);
Adam Langleybed8ce72014-09-05 17:04:51 -0700349 return NULL;
350}