Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* 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 Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 62 | #include <string.h> |
| 63 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | #include <openssl/asn1.h> |
Adam Langley | bed8ce7 | 2014-09-05 17:04:51 -0700 | [diff] [blame] | 65 | #include <openssl/dh.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 66 | #include <openssl/engine.h> |
| 67 | #include <openssl/err.h> |
| 68 | #include <openssl/ex_data.h> |
| 69 | #include <openssl/mem.h> |
Brian Smith | 054e682 | 2015-03-27 21:12:01 -1000 | [diff] [blame^] | 70 | #include <openssl/thread.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 71 | |
| 72 | #include "internal.h" |
| 73 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 74 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 75 | extern const DSA_METHOD DSA_default_method; |
| 76 | |
| 77 | DSA *DSA_new(void) { return DSA_new_method(NULL); } |
| 78 | |
| 79 | DSA *DSA_new_method(const ENGINE *engine) { |
| 80 | DSA *dsa = (DSA *)OPENSSL_malloc(sizeof(DSA)); |
| 81 | if (dsa == NULL) { |
| 82 | OPENSSL_PUT_ERROR(DSA, DSA_new_method, ERR_R_MALLOC_FAILURE); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | memset(dsa, 0, sizeof(DSA)); |
| 87 | |
| 88 | if (engine) { |
| 89 | dsa->meth = ENGINE_get_DSA_method(engine); |
| 90 | } |
| 91 | |
| 92 | if (dsa->meth == NULL) { |
| 93 | dsa->meth = (DSA_METHOD*) &DSA_default_method; |
| 94 | } |
| 95 | METHOD_ref(dsa->meth); |
| 96 | |
| 97 | dsa->write_params = 1; |
| 98 | dsa->references = 1; |
| 99 | |
| 100 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data)) { |
| 101 | METHOD_unref(dsa->meth); |
| 102 | OPENSSL_free(dsa); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | if (dsa->meth->init && !dsa->meth->init(dsa)) { |
| 107 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data); |
| 108 | METHOD_unref(dsa->meth); |
| 109 | OPENSSL_free(dsa); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | return dsa; |
| 114 | } |
| 115 | |
| 116 | void DSA_free(DSA *dsa) { |
| 117 | if (dsa == NULL) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (CRYPTO_add(&dsa->references, -1, CRYPTO_LOCK_DSA) > 0) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | if (dsa->meth->finish) { |
| 126 | dsa->meth->finish(dsa); |
| 127 | } |
| 128 | METHOD_unref(dsa->meth); |
| 129 | |
| 130 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, dsa, &dsa->ex_data); |
| 131 | |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 132 | if (dsa->p != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | BN_clear_free(dsa->p); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 134 | } |
| 135 | if (dsa->q != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | BN_clear_free(dsa->q); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 137 | } |
| 138 | if (dsa->g != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | BN_clear_free(dsa->g); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 140 | } |
| 141 | if (dsa->pub_key != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | BN_clear_free(dsa->pub_key); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 143 | } |
| 144 | if (dsa->priv_key != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | BN_clear_free(dsa->priv_key); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 146 | } |
| 147 | if (dsa->kinv != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | BN_clear_free(dsa->kinv); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 149 | } |
| 150 | if (dsa->r != NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | BN_clear_free(dsa->r); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 152 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | OPENSSL_free(dsa); |
| 154 | } |
| 155 | |
| 156 | int DSA_up_ref(DSA *dsa) { |
| 157 | CRYPTO_add(&dsa->references, 1, CRYPTO_LOCK_DSA); |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | int DSA_generate_parameters_ex(DSA *dsa, unsigned bits, const uint8_t *seed_in, |
| 162 | size_t seed_len, int *out_counter, |
| 163 | unsigned long *out_h, BN_GENCB *cb) { |
| 164 | if (dsa->meth->generate_parameters) { |
| 165 | return dsa->meth->generate_parameters(dsa, bits, seed_in, seed_len, |
| 166 | out_counter, out_h, cb); |
| 167 | } |
| 168 | return DSA_default_method.generate_parameters(dsa, bits, seed_in, seed_len, |
| 169 | out_counter, out_h, cb); |
| 170 | } |
| 171 | |
| 172 | int DSA_generate_key(DSA *dsa) { |
| 173 | if (dsa->meth->keygen) { |
| 174 | return dsa->meth->keygen(dsa); |
| 175 | } |
| 176 | return DSA_default_method.keygen(dsa); |
| 177 | } |
| 178 | |
| 179 | DSA_SIG *DSA_SIG_new(void) { |
| 180 | DSA_SIG *sig; |
| 181 | sig = OPENSSL_malloc(sizeof(DSA_SIG)); |
| 182 | if (!sig) { |
| 183 | return NULL; |
| 184 | } |
| 185 | sig->r = NULL; |
| 186 | sig->s = NULL; |
| 187 | return sig; |
| 188 | } |
| 189 | |
| 190 | void DSA_SIG_free(DSA_SIG *sig) { |
| 191 | if (!sig) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | if (sig->r) { |
| 196 | BN_free(sig->r); |
| 197 | } |
| 198 | if (sig->s) { |
| 199 | BN_free(sig->s); |
| 200 | } |
| 201 | OPENSSL_free(sig); |
| 202 | } |
| 203 | |
| 204 | DSA_SIG *DSA_do_sign(const uint8_t *digest, size_t digest_len, DSA *dsa) { |
| 205 | if (dsa->meth->sign) { |
| 206 | return dsa->meth->sign(digest, digest_len, dsa); |
| 207 | } |
| 208 | return DSA_default_method.sign(digest, digest_len, dsa); |
| 209 | } |
| 210 | |
| 211 | int DSA_do_verify(const uint8_t *digest, size_t digest_len, DSA_SIG *sig, |
| 212 | const DSA *dsa) { |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 213 | int valid; |
| 214 | if (!DSA_do_check_signature(&valid, digest, digest_len, sig, dsa)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 215 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 216 | } |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 217 | return valid; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | int DSA_do_check_signature(int *out_valid, const uint8_t *digest, |
| 221 | size_t digest_len, DSA_SIG *sig, const DSA *dsa) { |
| 222 | if (dsa->meth->verify) { |
| 223 | return dsa->meth->verify(out_valid, digest, digest_len, sig, dsa); |
| 224 | } |
| 225 | |
| 226 | return DSA_default_method.verify(out_valid, digest, digest_len, sig, dsa); |
| 227 | } |
| 228 | |
| 229 | int DSA_sign(int type, const uint8_t *digest, size_t digest_len, |
| 230 | uint8_t *out_sig, unsigned int *out_siglen, DSA *dsa) { |
| 231 | DSA_SIG *s; |
| 232 | |
| 233 | s = DSA_do_sign(digest, digest_len, dsa); |
| 234 | if (s == NULL) { |
| 235 | *out_siglen = 0; |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | *out_siglen = i2d_DSA_SIG(s, &out_sig); |
| 240 | DSA_SIG_free(s); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | int DSA_verify(int type, const uint8_t *digest, size_t digest_len, |
| 245 | const uint8_t *sig, size_t sig_len, const DSA *dsa) { |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 246 | int valid; |
| 247 | if (!DSA_check_signature(&valid, digest, digest_len, sig, sig_len, dsa)) { |
| 248 | return -1; |
| 249 | } |
| 250 | return valid; |
| 251 | } |
| 252 | |
| 253 | int DSA_check_signature(int *out_valid, const uint8_t *digest, |
| 254 | size_t digest_len, const uint8_t *sig, size_t sig_len, |
| 255 | const DSA *dsa) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | DSA_SIG *s = NULL; |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 257 | int ret = 0; |
Adam Langley | ca9a538 | 2015-01-08 12:26:55 -0800 | [diff] [blame] | 258 | uint8_t *der = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 259 | |
| 260 | s = DSA_SIG_new(); |
| 261 | if (s == NULL) { |
| 262 | goto err; |
| 263 | } |
| 264 | |
Adam Langley | ca9a538 | 2015-01-08 12:26:55 -0800 | [diff] [blame] | 265 | const uint8_t *sigp = sig; |
| 266 | if (d2i_DSA_SIG(&s, &sigp, sig_len) == NULL || sigp != sig + sig_len) { |
| 267 | goto err; |
| 268 | } |
| 269 | |
| 270 | /* Ensure that the signature uses DER and doesn't have trailing garbage. */ |
| 271 | int der_len = i2d_DSA_SIG(s, &der); |
| 272 | if (der_len < 0 || (size_t)der_len != sig_len || memcmp(sig, der, sig_len)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | goto err; |
| 274 | } |
| 275 | |
David Benjamin | 95e18c5 | 2015-01-10 23:37:17 -0500 | [diff] [blame] | 276 | ret = DSA_do_check_signature(out_valid, digest, digest_len, s, dsa); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | |
| 278 | err: |
Adam Langley | ca9a538 | 2015-01-08 12:26:55 -0800 | [diff] [blame] | 279 | if (der != NULL) { |
| 280 | OPENSSL_free(der); |
| 281 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | if (s) { |
| 283 | DSA_SIG_free(s); |
| 284 | } |
| 285 | return ret; |
| 286 | } |
| 287 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | int DSA_size(const DSA *dsa) { |
| 289 | int ret, i; |
| 290 | ASN1_INTEGER bs; |
| 291 | unsigned char buf[4]; /* 4 bytes looks really small. |
| 292 | However, i2d_ASN1_INTEGER() will not look |
| 293 | beyond the first byte, as long as the second |
| 294 | parameter is NULL. */ |
| 295 | |
| 296 | i = BN_num_bits(dsa->q); |
| 297 | bs.length = (i + 7) / 8; |
| 298 | bs.data = buf; |
| 299 | bs.type = V_ASN1_INTEGER; |
| 300 | /* If the top bit is set the asn1 encoding is 1 larger. */ |
| 301 | buf[0] = 0xff; |
| 302 | |
| 303 | i = i2d_ASN1_INTEGER(&bs, NULL); |
| 304 | i += i; /* r and s */ |
| 305 | ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | int DSA_sign_setup(const DSA *dsa, BN_CTX *ctx, BIGNUM **out_kinv, |
| 310 | BIGNUM **out_r) { |
| 311 | if (dsa->meth->sign_setup) { |
Adam Langley | d4b4f08 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 312 | return dsa->meth->sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Adam Langley | d4b4f08 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | return DSA_default_method.sign_setup(dsa, ctx, out_kinv, out_r, NULL, 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 319 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) { |
| 320 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, new_func, |
| 321 | dup_func, free_func); |
| 322 | } |
| 323 | |
| 324 | int DSA_set_ex_data(DSA *d, int idx, void *arg) { |
| 325 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
| 326 | } |
| 327 | |
| 328 | void *DSA_get_ex_data(const DSA *d, int idx) { |
| 329 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
| 330 | } |
Adam Langley | bed8ce7 | 2014-09-05 17:04:51 -0700 | [diff] [blame] | 331 | |
| 332 | DH *DSA_dup_DH(const DSA *r) { |
| 333 | DH *ret = NULL; |
| 334 | |
| 335 | if (r == NULL) { |
| 336 | goto err; |
| 337 | } |
| 338 | ret = DH_new(); |
| 339 | if (ret == NULL) { |
| 340 | goto err; |
| 341 | } |
| 342 | if (r->q != NULL) { |
| 343 | ret->priv_length = BN_num_bits(r->q); |
| 344 | if ((ret->q = BN_dup(r->q)) == NULL) { |
| 345 | goto err; |
| 346 | } |
| 347 | } |
| 348 | if ((r->p != NULL && (ret->p = BN_dup(r->p)) == NULL) || |
| 349 | (r->g != NULL && (ret->g = BN_dup(r->g)) == NULL) || |
| 350 | (r->pub_key != NULL && (ret->pub_key = BN_dup(r->pub_key)) == NULL) || |
| 351 | (r->priv_key != NULL && (ret->priv_key = BN_dup(r->priv_key)) == NULL)) { |
| 352 | goto err; |
| 353 | } |
| 354 | |
| 355 | return ret; |
| 356 | |
| 357 | err: |
| 358 | if (ret != NULL) { |
| 359 | DH_free(ret); |
| 360 | } |
| 361 | return NULL; |
| 362 | } |