1 /* $OpenBSD: dsa_lib.c,v 1.37 2022/08/31 13:28:39 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 /* Original version from Steven Schoch <schoch@sheba.arc.nasa.gov> */ 60 61 #include <stdio.h> 62 63 #include <openssl/opensslconf.h> 64 65 #include <openssl/asn1.h> 66 #include <openssl/bn.h> 67 #include <openssl/dsa.h> 68 #include <openssl/err.h> 69 70 #ifndef OPENSSL_NO_DH 71 #include <openssl/dh.h> 72 #endif 73 #ifndef OPENSSL_NO_ENGINE 74 #include <openssl/engine.h> 75 #endif 76 77 #include "dh_local.h" 78 #include "dsa_locl.h" 79 80 static const DSA_METHOD *default_DSA_method = NULL; 81 82 void 83 DSA_set_default_method(const DSA_METHOD *meth) 84 { 85 default_DSA_method = meth; 86 } 87 88 const DSA_METHOD * 89 DSA_get_default_method(void) 90 { 91 if (!default_DSA_method) 92 default_DSA_method = DSA_OpenSSL(); 93 return default_DSA_method; 94 } 95 96 DSA * 97 DSA_new(void) 98 { 99 return DSA_new_method(NULL); 100 } 101 102 int 103 DSA_set_method(DSA *dsa, const DSA_METHOD *meth) 104 { 105 /* 106 * NB: The caller is specifically setting a method, so it's not up to us 107 * to deal with which ENGINE it comes from. 108 */ 109 const DSA_METHOD *mtmp; 110 mtmp = dsa->meth; 111 if (mtmp->finish) 112 mtmp->finish(dsa); 113 #ifndef OPENSSL_NO_ENGINE 114 ENGINE_finish(dsa->engine); 115 dsa->engine = NULL; 116 #endif 117 dsa->meth = meth; 118 if (meth->init) 119 meth->init(dsa); 120 return 1; 121 } 122 123 DSA * 124 DSA_new_method(ENGINE *engine) 125 { 126 DSA *ret; 127 128 ret = malloc(sizeof(DSA)); 129 if (ret == NULL) { 130 DSAerror(ERR_R_MALLOC_FAILURE); 131 return NULL; 132 } 133 ret->meth = DSA_get_default_method(); 134 #ifndef OPENSSL_NO_ENGINE 135 if (engine) { 136 if (!ENGINE_init(engine)) { 137 DSAerror(ERR_R_ENGINE_LIB); 138 free(ret); 139 return NULL; 140 } 141 ret->engine = engine; 142 } else 143 ret->engine = ENGINE_get_default_DSA(); 144 if (ret->engine) { 145 ret->meth = ENGINE_get_DSA(ret->engine); 146 if (ret->meth == NULL) { 147 DSAerror(ERR_R_ENGINE_LIB); 148 ENGINE_finish(ret->engine); 149 free(ret); 150 return NULL; 151 } 152 } 153 #endif 154 155 ret->pad = 0; 156 ret->version = 0; 157 ret->p = NULL; 158 ret->q = NULL; 159 ret->g = NULL; 160 161 ret->pub_key = NULL; 162 ret->priv_key = NULL; 163 164 ret->kinv = NULL; 165 ret->r = NULL; 166 ret->method_mont_p = NULL; 167 168 ret->references = 1; 169 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; 170 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 171 if (ret->meth->init != NULL && !ret->meth->init(ret)) { 172 #ifndef OPENSSL_NO_ENGINE 173 ENGINE_finish(ret->engine); 174 #endif 175 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 176 free(ret); 177 ret = NULL; 178 } 179 180 return ret; 181 } 182 183 void 184 DSA_free(DSA *r) 185 { 186 int i; 187 188 if (r == NULL) 189 return; 190 191 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DSA); 192 if (i > 0) 193 return; 194 195 if (r->meth->finish) 196 r->meth->finish(r); 197 #ifndef OPENSSL_NO_ENGINE 198 ENGINE_finish(r->engine); 199 #endif 200 201 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); 202 203 BN_clear_free(r->p); 204 BN_clear_free(r->q); 205 BN_clear_free(r->g); 206 BN_clear_free(r->pub_key); 207 BN_clear_free(r->priv_key); 208 BN_clear_free(r->kinv); 209 BN_clear_free(r->r); 210 free(r); 211 } 212 213 int 214 DSA_up_ref(DSA *r) 215 { 216 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); 217 return i > 1 ? 1 : 0; 218 } 219 220 int 221 DSA_size(const DSA *r) 222 { 223 DSA_SIG signature; 224 int ret = 0; 225 226 signature.r = r->q; 227 signature.s = r->q; 228 229 if ((ret = i2d_DSA_SIG(&signature, NULL)) < 0) 230 ret = 0; 231 232 return ret; 233 } 234 235 int 236 DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 237 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 238 { 239 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, 240 new_func, dup_func, free_func); 241 } 242 243 int 244 DSA_set_ex_data(DSA *d, int idx, void *arg) 245 { 246 return CRYPTO_set_ex_data(&d->ex_data, idx, arg); 247 } 248 249 void * 250 DSA_get_ex_data(DSA *d, int idx) 251 { 252 return CRYPTO_get_ex_data(&d->ex_data, idx); 253 } 254 255 int 256 DSA_security_bits(const DSA *d) 257 { 258 if (d->p == NULL || d->q == NULL) 259 return -1; 260 261 return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q)); 262 } 263 264 #ifndef OPENSSL_NO_DH 265 DH * 266 DSA_dup_DH(const DSA *r) 267 { 268 /* 269 * DSA has p, q, g, optional pub_key, optional priv_key. 270 * DH has p, optional length, g, optional pub_key, optional priv_key, 271 * optional q. 272 */ 273 DH *ret = NULL; 274 275 if (r == NULL) 276 goto err; 277 ret = DH_new(); 278 if (ret == NULL) 279 goto err; 280 if (r->p != NULL) 281 if ((ret->p = BN_dup(r->p)) == NULL) 282 goto err; 283 if (r->q != NULL) { 284 ret->length = BN_num_bits(r->q); 285 if ((ret->q = BN_dup(r->q)) == NULL) 286 goto err; 287 } 288 if (r->g != NULL) 289 if ((ret->g = BN_dup(r->g)) == NULL) 290 goto err; 291 if (r->pub_key != NULL) 292 if ((ret->pub_key = BN_dup(r->pub_key)) == NULL) 293 goto err; 294 if (r->priv_key != NULL) 295 if ((ret->priv_key = BN_dup(r->priv_key)) == NULL) 296 goto err; 297 298 return ret; 299 300 err: 301 DH_free(ret); 302 return NULL; 303 } 304 #endif 305 306 void 307 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) 308 { 309 if (p != NULL) 310 *p = d->p; 311 if (q != NULL) 312 *q = d->q; 313 if (g != NULL) 314 *g = d->g; 315 } 316 317 int 318 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) 319 { 320 if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) || 321 (d->g == NULL && g == NULL)) 322 return 0; 323 324 if (p != NULL) { 325 BN_free(d->p); 326 d->p = p; 327 } 328 if (q != NULL) { 329 BN_free(d->q); 330 d->q = q; 331 } 332 if (g != NULL) { 333 BN_free(d->g); 334 d->g = g; 335 } 336 337 return 1; 338 } 339 340 void 341 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key) 342 { 343 if (pub_key != NULL) 344 *pub_key = d->pub_key; 345 if (priv_key != NULL) 346 *priv_key = d->priv_key; 347 } 348 349 int 350 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) 351 { 352 if (d->pub_key == NULL && pub_key == NULL) 353 return 0; 354 355 if (pub_key != NULL) { 356 BN_free(d->pub_key); 357 d->pub_key = pub_key; 358 } 359 if (priv_key != NULL) { 360 BN_free(d->priv_key); 361 d->priv_key = priv_key; 362 } 363 364 return 1; 365 } 366 367 const BIGNUM * 368 DSA_get0_p(const DSA *d) 369 { 370 return d->p; 371 } 372 373 const BIGNUM * 374 DSA_get0_q(const DSA *d) 375 { 376 return d->q; 377 } 378 379 const BIGNUM * 380 DSA_get0_g(const DSA *d) 381 { 382 return d->g; 383 } 384 385 const BIGNUM * 386 DSA_get0_pub_key(const DSA *d) 387 { 388 return d->pub_key; 389 } 390 391 const BIGNUM * 392 DSA_get0_priv_key(const DSA *d) 393 { 394 return d->priv_key; 395 } 396 397 void 398 DSA_clear_flags(DSA *d, int flags) 399 { 400 d->flags &= ~flags; 401 } 402 403 int 404 DSA_test_flags(const DSA *d, int flags) 405 { 406 return d->flags & flags; 407 } 408 409 void 410 DSA_set_flags(DSA *d, int flags) 411 { 412 d->flags |= flags; 413 } 414 415 ENGINE * 416 DSA_get0_engine(DSA *d) 417 { 418 return d->engine; 419 } 420 421 int 422 DSA_bits(const DSA *dsa) 423 { 424 return BN_num_bits(dsa->p); 425 } 426