/src/openssl111/crypto/dsa/dsa_lib.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1995-2018 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 "internal/refcount.h" | 
| 13 |  | #include <openssl/bn.h> | 
| 14 |  | #include "dsa_local.h" | 
| 15 |  | #include <openssl/asn1.h> | 
| 16 |  | #include <openssl/engine.h> | 
| 17 |  | #include <openssl/dh.h> | 
| 18 |  |  | 
| 19 |  | DSA *DSA_new(void) | 
| 20 | 219k | { | 
| 21 | 219k |     return DSA_new_method(NULL); | 
| 22 | 219k | } | 
| 23 |  |  | 
| 24 |  | int DSA_set_method(DSA *dsa, const DSA_METHOD *meth) | 
| 25 | 0 | { | 
| 26 |  |     /* | 
| 27 |  |      * NB: The caller is specifically setting a method, so it's not up to us | 
| 28 |  |      * to deal with which ENGINE it comes from. | 
| 29 |  |      */ | 
| 30 | 0 |     const DSA_METHOD *mtmp; | 
| 31 | 0 |     mtmp = dsa->meth; | 
| 32 | 0 |     if (mtmp->finish) | 
| 33 | 0 |         mtmp->finish(dsa); | 
| 34 | 0 | #ifndef OPENSSL_NO_ENGINE | 
| 35 | 0 |     ENGINE_finish(dsa->engine); | 
| 36 | 0 |     dsa->engine = NULL; | 
| 37 | 0 | #endif | 
| 38 | 0 |     dsa->meth = meth; | 
| 39 | 0 |     if (meth->init) | 
| 40 | 0 |         meth->init(dsa); | 
| 41 | 0 |     return 1; | 
| 42 | 0 | } | 
| 43 |  |  | 
| 44 |  | const DSA_METHOD *DSA_get_method(DSA *d) | 
| 45 | 39.7k | { | 
| 46 | 39.7k |     return d->meth; | 
| 47 | 39.7k | } | 
| 48 |  |  | 
| 49 |  | DSA *DSA_new_method(ENGINE *engine) | 
| 50 | 48.6k | { | 
| 51 | 48.6k |     DSA *ret = OPENSSL_zalloc(sizeof(*ret)); | 
| 52 |  |  | 
| 53 | 48.6k |     if (ret == NULL) { | 
| 54 | 0 |         DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | 
| 55 | 0 |         return NULL; | 
| 56 | 0 |     } | 
| 57 |  |  | 
| 58 | 48.6k |     ret->references = 1; | 
| 59 | 48.6k |     ret->lock = CRYPTO_THREAD_lock_new(); | 
| 60 | 48.6k |     if (ret->lock == NULL) { | 
| 61 | 0 |         DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | 
| 62 | 0 |         OPENSSL_free(ret); | 
| 63 | 0 |         return NULL; | 
| 64 | 0 |     } | 
| 65 |  |  | 
| 66 | 48.6k |     ret->meth = DSA_get_default_method(); | 
| 67 | 48.6k | #ifndef OPENSSL_NO_ENGINE | 
| 68 | 48.6k |     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; /* early default init */ | 
| 69 | 48.6k |     if (engine) { | 
| 70 | 0 |         if (!ENGINE_init(engine)) { | 
| 71 | 0 |             DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | 
| 72 | 0 |             goto err; | 
| 73 | 0 |         } | 
| 74 | 0 |         ret->engine = engine; | 
| 75 | 0 |     } else | 
| 76 | 48.6k |         ret->engine = ENGINE_get_default_DSA(); | 
| 77 | 48.6k |     if (ret->engine) { | 
| 78 | 0 |         ret->meth = ENGINE_get_DSA(ret->engine); | 
| 79 | 0 |         if (ret->meth == NULL) { | 
| 80 | 0 |             DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); | 
| 81 | 0 |             goto err; | 
| 82 | 0 |         } | 
| 83 | 0 |     } | 
| 84 | 48.6k | #endif | 
| 85 |  |  | 
| 86 | 48.6k |     ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; | 
| 87 |  |  | 
| 88 | 48.6k |     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data)) | 
| 89 | 0 |         goto err; | 
| 90 |  |  | 
| 91 | 48.6k |     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | 
| 92 | 0 |         DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_INIT_FAIL); | 
| 93 | 0 |         goto err; | 
| 94 | 0 |     } | 
| 95 |  |  | 
| 96 | 48.6k |     return ret; | 
| 97 |  |  | 
| 98 | 0 |  err: | 
| 99 | 0 |     DSA_free(ret); | 
| 100 | 0 |     return NULL; | 
| 101 | 48.6k | } | 
| 102 |  |  | 
| 103 |  | void DSA_free(DSA *r) | 
| 104 | 640k | { | 
| 105 | 640k |     int i; | 
| 106 |  |  | 
| 107 | 640k |     if (r == NULL) | 
| 108 | 363k |         return; | 
| 109 |  |  | 
| 110 | 276k |     CRYPTO_DOWN_REF(&r->references, &i, r->lock); | 
| 111 | 276k |     REF_PRINT_COUNT("DSA", r); | 
| 112 | 276k |     if (i > 0) | 
| 113 | 36.9k |         return; | 
| 114 | 239k |     REF_ASSERT_ISNT(i < 0); | 
| 115 |  |  | 
| 116 | 239k |     if (r->meth != NULL && r->meth->finish != NULL) | 
| 117 | 239k |         r->meth->finish(r); | 
| 118 | 239k | #ifndef OPENSSL_NO_ENGINE | 
| 119 | 239k |     ENGINE_finish(r->engine); | 
| 120 | 239k | #endif | 
| 121 |  |  | 
| 122 | 239k |     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); | 
| 123 |  |  | 
| 124 | 239k |     CRYPTO_THREAD_lock_free(r->lock); | 
| 125 |  |  | 
| 126 | 239k |     BN_clear_free(r->p); | 
| 127 | 239k |     BN_clear_free(r->q); | 
| 128 | 239k |     BN_clear_free(r->g); | 
| 129 | 239k |     BN_clear_free(r->pub_key); | 
| 130 | 239k |     BN_clear_free(r->priv_key); | 
| 131 | 239k |     OPENSSL_free(r); | 
| 132 | 239k | } | 
| 133 |  |  | 
| 134 |  | int DSA_up_ref(DSA *r) | 
| 135 | 36.9k | { | 
| 136 | 36.9k |     int i; | 
| 137 |  |  | 
| 138 | 36.9k |     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) | 
| 139 | 0 |         return 0; | 
| 140 |  |  | 
| 141 | 36.9k |     REF_PRINT_COUNT("DSA", r); | 
| 142 | 36.9k |     REF_ASSERT_ISNT(i < 2); | 
| 143 | 36.9k |     return ((i > 1) ? 1 : 0); | 
| 144 | 36.9k | } | 
| 145 |  |  | 
| 146 |  | int DSA_size(const DSA *r) | 
| 147 | 439 | { | 
| 148 | 439 |     int ret, i; | 
| 149 | 439 |     ASN1_INTEGER bs; | 
| 150 | 439 |     unsigned char buf[4];       /* 4 bytes looks really small. However, | 
| 151 |  |                                  * i2d_ASN1_INTEGER() will not look beyond | 
| 152 |  |                                  * the first byte, as long as the second | 
| 153 |  |                                  * parameter is NULL. */ | 
| 154 |  |  | 
| 155 | 439 |     i = BN_num_bits(r->q); | 
| 156 | 439 |     bs.length = (i + 7) / 8; | 
| 157 | 439 |     bs.data = buf; | 
| 158 | 439 |     bs.type = V_ASN1_INTEGER; | 
| 159 |  |     /* If the top bit is set the asn1 encoding is 1 larger. */ | 
| 160 | 439 |     buf[0] = 0xff; | 
| 161 |  |  | 
| 162 | 439 |     i = i2d_ASN1_INTEGER(&bs, NULL); | 
| 163 | 439 |     i += i;                     /* r and s */ | 
| 164 | 439 |     ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); | 
| 165 | 439 |     return ret; | 
| 166 | 439 | } | 
| 167 |  |  | 
| 168 |  | int DSA_set_ex_data(DSA *d, int idx, void *arg) | 
| 169 | 0 | { | 
| 170 | 0 |     return CRYPTO_set_ex_data(&d->ex_data, idx, arg); | 
| 171 | 0 | } | 
| 172 |  |  | 
| 173 |  | void *DSA_get_ex_data(DSA *d, int idx) | 
| 174 | 0 | { | 
| 175 | 0 |     return CRYPTO_get_ex_data(&d->ex_data, idx); | 
| 176 | 0 | } | 
| 177 |  |  | 
| 178 |  | int DSA_security_bits(const DSA *d) | 
| 179 | 6.11k | { | 
| 180 | 6.11k |     if (d->p && d->q) | 
| 181 | 6.11k |         return BN_security_bits(BN_num_bits(d->p), BN_num_bits(d->q)); | 
| 182 | 0 |     return -1; | 
| 183 | 6.11k | } | 
| 184 |  |  | 
| 185 |  | #ifndef OPENSSL_NO_DH | 
| 186 |  | DH *DSA_dup_DH(const DSA *r) | 
| 187 | 0 | { | 
| 188 |  |     /* | 
| 189 |  |      * DSA has p, q, g, optional pub_key, optional priv_key. DH has p, | 
| 190 |  |      * optional length, g, optional pub_key, optional priv_key, optional q. | 
| 191 |  |      */ | 
| 192 |  | 
 | 
| 193 | 0 |     DH *ret = NULL; | 
| 194 | 0 |     BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub_key = NULL, *priv_key = NULL; | 
| 195 |  | 
 | 
| 196 | 0 |     if (r == NULL) | 
| 197 | 0 |         goto err; | 
| 198 | 0 |     ret = DH_new(); | 
| 199 | 0 |     if (ret == NULL) | 
| 200 | 0 |         goto err; | 
| 201 | 0 |     if (r->p != NULL || r->g != NULL || r->q != NULL) { | 
| 202 | 0 |         if (r->p == NULL || r->g == NULL || r->q == NULL) { | 
| 203 |  |             /* Shouldn't happen */ | 
| 204 | 0 |             goto err; | 
| 205 | 0 |         } | 
| 206 | 0 |         p = BN_dup(r->p); | 
| 207 | 0 |         g = BN_dup(r->g); | 
| 208 | 0 |         q = BN_dup(r->q); | 
| 209 | 0 |         if (p == NULL || g == NULL || q == NULL || !DH_set0_pqg(ret, p, q, g)) | 
| 210 | 0 |             goto err; | 
| 211 | 0 |         p = g = q = NULL; | 
| 212 | 0 |     } | 
| 213 |  |  | 
| 214 | 0 |     if (r->pub_key != NULL) { | 
| 215 | 0 |         pub_key = BN_dup(r->pub_key); | 
| 216 | 0 |         if (pub_key == NULL) | 
| 217 | 0 |             goto err; | 
| 218 | 0 |         if (r->priv_key != NULL) { | 
| 219 | 0 |             priv_key = BN_dup(r->priv_key); | 
| 220 | 0 |             if (priv_key == NULL) | 
| 221 | 0 |                 goto err; | 
| 222 | 0 |         } | 
| 223 | 0 |         if (!DH_set0_key(ret, pub_key, priv_key)) | 
| 224 | 0 |             goto err; | 
| 225 | 0 |     } else if (r->priv_key != NULL) { | 
| 226 |  |         /* Shouldn't happen */ | 
| 227 | 0 |         goto err; | 
| 228 | 0 |     } | 
| 229 |  |  | 
| 230 | 0 |     return ret; | 
| 231 |  |  | 
| 232 | 0 |  err: | 
| 233 | 0 |     BN_free(p); | 
| 234 | 0 |     BN_free(g); | 
| 235 | 0 |     BN_free(q); | 
| 236 | 0 |     BN_free(pub_key); | 
| 237 | 0 |     BN_free(priv_key); | 
| 238 | 0 |     DH_free(ret); | 
| 239 | 0 |     return NULL; | 
| 240 | 0 | } | 
| 241 |  | #endif | 
| 242 |  |  | 
| 243 |  | void DSA_get0_pqg(const DSA *d, | 
| 244 |  |                   const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) | 
| 245 |  | { | 
| 246 |  |     if (p != NULL) | 
| 247 |  |         *p = d->p; | 
| 248 |  |     if (q != NULL) | 
| 249 |  |         *q = d->q; | 
| 250 |  |     if (g != NULL) | 
| 251 |  |         *g = d->g; | 
| 252 |  | } | 
| 253 |  |  | 
| 254 |  | int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) | 
| 255 |  | { | 
| 256 |  |     /* If the fields p, q and g in d are NULL, the corresponding input | 
| 257 |  |      * parameters MUST be non-NULL. | 
| 258 |  |      */ | 
| 259 |  |     if ((d->p == NULL && p == NULL) | 
| 260 |  |         || (d->q == NULL && q == NULL) | 
| 261 |  |         || (d->g == NULL && g == NULL)) | 
| 262 |  |         return 0; | 
| 263 |  |  | 
| 264 |  |     if (p != NULL) { | 
| 265 |  |         BN_free(d->p); | 
| 266 |  |         d->p = p; | 
| 267 |  |     } | 
| 268 |  |     if (q != NULL) { | 
| 269 |  |         BN_free(d->q); | 
| 270 |  |         d->q = q; | 
| 271 |  |     } | 
| 272 |  |     if (g != NULL) { | 
| 273 |  |         BN_free(d->g); | 
| 274 |  |         d->g = g; | 
| 275 |  |     } | 
| 276 |  |  | 
| 277 |  |     return 1; | 
| 278 |  | } | 
| 279 |  |  | 
| 280 |  | void DSA_get0_key(const DSA *d, | 
| 281 |  |                   const BIGNUM **pub_key, const BIGNUM **priv_key) | 
| 282 | 40.2k | { | 
| 283 | 40.2k |     if (pub_key != NULL) | 
| 284 | 39.6k |         *pub_key = d->pub_key; | 
| 285 | 40.2k |     if (priv_key != NULL) | 
| 286 | 39.2k |         *priv_key = d->priv_key; | 
| 287 | 40.2k | } | 
| 288 |  |  | 
| 289 |  | int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) | 
| 290 |  | { | 
| 291 |  |     /* If the field pub_key in d is NULL, the corresponding input | 
| 292 |  |      * parameters MUST be non-NULL.  The priv_key field may | 
| 293 |  |      * be left NULL. | 
| 294 |  |      */ | 
| 295 |  |     if (d->pub_key == NULL && pub_key == NULL) | 
| 296 |  |         return 0; | 
| 297 |  |  | 
| 298 |  |     if (pub_key != NULL) { | 
| 299 |  |         BN_free(d->pub_key); | 
| 300 |  |         d->pub_key = pub_key; | 
| 301 |  |     } | 
| 302 |  |     if (priv_key != NULL) { | 
| 303 |  |         BN_free(d->priv_key); | 
| 304 |  |         d->priv_key = priv_key; | 
| 305 |  |     } | 
| 306 |  |  | 
| 307 |  |     return 1; | 
| 308 |  | } | 
| 309 |  |  | 
| 310 |  | const BIGNUM *DSA_get0_p(const DSA *d) | 
| 311 | 34.0k | { | 
| 312 | 34.0k |     return d->p; | 
| 313 | 34.0k | } | 
| 314 |  |  | 
| 315 |  | const BIGNUM *DSA_get0_q(const DSA *d) | 
| 316 | 10.1k | { | 
| 317 | 10.1k |     return d->q; | 
| 318 | 10.1k | } | 
| 319 |  |  | 
| 320 |  | const BIGNUM *DSA_get0_g(const DSA *d) | 
| 321 | 31.9k | { | 
| 322 | 31.9k |     return d->g; | 
| 323 | 31.9k | } | 
| 324 |  |  | 
| 325 |  | const BIGNUM *DSA_get0_pub_key(const DSA *d) | 
| 326 | 44.2k | { | 
| 327 | 44.2k |     return d->pub_key; | 
| 328 | 44.2k | } | 
| 329 |  |  | 
| 330 |  | const BIGNUM *DSA_get0_priv_key(const DSA *d) | 
| 331 | 21.9k | { | 
| 332 | 21.9k |     return d->priv_key; | 
| 333 | 21.9k | } | 
| 334 |  |  | 
| 335 |  | void DSA_clear_flags(DSA *d, int flags) | 
| 336 | 0 | { | 
| 337 | 0 |     d->flags &= ~flags; | 
| 338 | 0 | } | 
| 339 |  |  | 
| 340 |  | int DSA_test_flags(const DSA *d, int flags) | 
| 341 | 0 | { | 
| 342 | 0 |     return d->flags & flags; | 
| 343 | 0 | } | 
| 344 |  |  | 
| 345 |  | void DSA_set_flags(DSA *d, int flags) | 
| 346 | 0 | { | 
| 347 | 0 |     d->flags |= flags; | 
| 348 | 0 | } | 
| 349 |  |  | 
| 350 |  | ENGINE *DSA_get0_engine(DSA *d) | 
| 351 | 0 | { | 
| 352 | 0 |     return d->engine; | 
| 353 | 0 | } | 
| 354 |  |  | 
| 355 |  | int DSA_bits(const DSA *dsa) | 
| 356 |  | { | 
| 357 |  |     return BN_num_bits(dsa->p); | 
| 358 |  | } |