1 /* $OpenBSD: dsa_lib.c,v 1.21 2014/07/12 16:03:37 miod 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 69 #ifndef OPENSSL_NO_DH 70 #include <openssl/dh.h> 71 #endif 72 #ifndef OPENSSL_NO_ENGINE 73 #include <openssl/engine.h> 74 #endif 75 76 static const DSA_METHOD *default_DSA_method = NULL; 77 78 void 79 DSA_set_default_method(const DSA_METHOD *meth) 80 { 81 default_DSA_method = meth; 82 } 83 84 const DSA_METHOD * 85 DSA_get_default_method(void) 86 { 87 if (!default_DSA_method) 88 default_DSA_method = DSA_OpenSSL(); 89 return default_DSA_method; 90 } 91 92 DSA * 93 DSA_new(void) 94 { 95 return DSA_new_method(NULL); 96 } 97 98 int 99 DSA_set_method(DSA *dsa, const DSA_METHOD *meth) 100 { 101 /* 102 * NB: The caller is specifically setting a method, so it's not up to us 103 * to deal with which ENGINE it comes from. 104 */ 105 const DSA_METHOD *mtmp; 106 mtmp = dsa->meth; 107 if (mtmp->finish) 108 mtmp->finish(dsa); 109 #ifndef OPENSSL_NO_ENGINE 110 if (dsa->engine) { 111 ENGINE_finish(dsa->engine); 112 dsa->engine = NULL; 113 } 114 #endif 115 dsa->meth = meth; 116 if (meth->init) 117 meth->init(dsa); 118 return 1; 119 } 120 121 DSA * 122 DSA_new_method(ENGINE *engine) 123 { 124 DSA *ret; 125 126 ret = malloc(sizeof(DSA)); 127 if (ret == NULL) { 128 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); 129 return NULL; 130 } 131 ret->meth = DSA_get_default_method(); 132 #ifndef OPENSSL_NO_ENGINE 133 if (engine) { 134 if (!ENGINE_init(engine)) { 135 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 136 free(ret); 137 return NULL; 138 } 139 ret->engine = engine; 140 } else 141 ret->engine = ENGINE_get_default_DSA(); 142 if (ret->engine) { 143 ret->meth = ENGINE_get_DSA(ret->engine); 144 if (!ret->meth) { 145 DSAerr(DSA_F_DSA_NEW_METHOD, ERR_R_ENGINE_LIB); 146 ENGINE_finish(ret->engine); 147 free(ret); 148 return NULL; 149 } 150 } 151 #endif 152 153 ret->pad = 0; 154 ret->version = 0; 155 ret->write_params = 1; 156 ret->p = NULL; 157 ret->q = NULL; 158 ret->g = NULL; 159 160 ret->pub_key = NULL; 161 ret->priv_key = NULL; 162 163 ret->kinv = NULL; 164 ret->r = NULL; 165 ret->method_mont_p = NULL; 166 167 ret->references = 1; 168 ret->flags = ret->meth->flags & ~DSA_FLAG_NON_FIPS_ALLOW; 169 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DSA, ret, &ret->ex_data); 170 if (ret->meth->init != NULL && !ret->meth->init(ret)) { 171 #ifndef OPENSSL_NO_ENGINE 172 if (ret->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 if (r->engine) 199 ENGINE_finish(r->engine); 200 #endif 201 202 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DSA, r, &r->ex_data); 203 204 BN_clear_free(r->p); 205 BN_clear_free(r->q); 206 BN_clear_free(r->g); 207 BN_clear_free(r->pub_key); 208 BN_clear_free(r->priv_key); 209 BN_clear_free(r->kinv); 210 BN_clear_free(r->r); 211 free(r); 212 } 213 214 int 215 DSA_up_ref(DSA *r) 216 { 217 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DSA); 218 return i > 1 ? 1 : 0; 219 } 220 221 int 222 DSA_size(const DSA *r) 223 { 224 int ret, i; 225 ASN1_INTEGER bs; 226 unsigned char buf[4]; /* 4 bytes looks really small. 227 However, i2d_ASN1_INTEGER() will not look 228 beyond the first byte, as long as the second 229 parameter is NULL. */ 230 231 i = BN_num_bits(r->q); 232 bs.length = (i + 7) / 8; 233 bs.data = buf; 234 bs.type = V_ASN1_INTEGER; 235 /* If the top bit is set the asn1 encoding is 1 larger. */ 236 buf[0] = 0xff; 237 238 i = i2d_ASN1_INTEGER(&bs, NULL); 239 i += i; /* r and s */ 240 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE); 241 return ret; 242 } 243 244 int 245 DSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 246 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 247 { 248 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DSA, argl, argp, 249 new_func, dup_func, free_func); 250 } 251 252 int 253 DSA_set_ex_data(DSA *d, int idx, void *arg) 254 { 255 return CRYPTO_set_ex_data(&d->ex_data, idx, arg); 256 } 257 258 void * 259 DSA_get_ex_data(DSA *d, int idx) 260 { 261 return CRYPTO_get_ex_data(&d->ex_data, idx); 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