diff options
Diffstat (limited to 'src/lib/libcrypto/dh/dh_lib.c')
| -rw-r--r-- | src/lib/libcrypto/dh/dh_lib.c | 159 |
1 files changed, 148 insertions, 11 deletions
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c index a300b38396..ba5fd41057 100644 --- a/src/lib/libcrypto/dh/dh_lib.c +++ b/src/lib/libcrypto/dh/dh_lib.c | |||
| @@ -58,21 +58,84 @@ | |||
| 58 | 58 | ||
| 59 | #include <stdio.h> | 59 | #include <stdio.h> |
| 60 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
| 61 | #include "bn.h" | 61 | #include <openssl/bn.h> |
| 62 | #include "dh.h" | 62 | #include <openssl/dh.h> |
| 63 | #include <openssl/engine.h> | ||
| 63 | 64 | ||
| 64 | char *DH_version="Diffie-Hellman part of SSLeay 0.9.0b 29-Jun-1998"; | 65 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; |
| 65 | 66 | ||
| 66 | DH *DH_new() | 67 | static const DH_METHOD *default_DH_method = NULL; |
| 68 | |||
| 69 | void DH_set_default_method(const DH_METHOD *meth) | ||
| 70 | { | ||
| 71 | default_DH_method = meth; | ||
| 72 | } | ||
| 73 | |||
| 74 | const DH_METHOD *DH_get_default_method(void) | ||
| 75 | { | ||
| 76 | if(!default_DH_method) | ||
| 77 | default_DH_method = DH_OpenSSL(); | ||
| 78 | return default_DH_method; | ||
| 79 | } | ||
| 80 | |||
| 81 | int DH_set_method(DH *dh, const DH_METHOD *meth) | ||
| 82 | { | ||
| 83 | /* NB: The caller is specifically setting a method, so it's not up to us | ||
| 84 | * to deal with which ENGINE it comes from. */ | ||
| 85 | const DH_METHOD *mtmp; | ||
| 86 | mtmp = dh->meth; | ||
| 87 | if (mtmp->finish) mtmp->finish(dh); | ||
| 88 | if (dh->engine) | ||
| 89 | { | ||
| 90 | ENGINE_finish(dh->engine); | ||
| 91 | dh->engine = NULL; | ||
| 92 | } | ||
| 93 | dh->meth = meth; | ||
| 94 | if (meth->init) meth->init(dh); | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | |||
| 98 | DH *DH_new(void) | ||
| 99 | { | ||
| 100 | return DH_new_method(NULL); | ||
| 101 | } | ||
| 102 | |||
| 103 | DH *DH_new_method(ENGINE *engine) | ||
| 67 | { | 104 | { |
| 68 | DH *ret; | 105 | DH *ret; |
| 69 | 106 | ||
| 70 | ret=(DH *)Malloc(sizeof(DH)); | 107 | ret=(DH *)OPENSSL_malloc(sizeof(DH)); |
| 71 | if (ret == NULL) | 108 | if (ret == NULL) |
| 72 | { | 109 | { |
| 73 | DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE); | 110 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE); |
| 74 | return(NULL); | 111 | return(NULL); |
| 75 | } | 112 | } |
| 113 | |||
| 114 | ret->meth = DH_get_default_method(); | ||
| 115 | if (engine) | ||
| 116 | { | ||
| 117 | if (!ENGINE_init(engine)) | ||
| 118 | { | ||
| 119 | DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB); | ||
| 120 | OPENSSL_free(ret); | ||
| 121 | return NULL; | ||
| 122 | } | ||
| 123 | ret->engine = engine; | ||
| 124 | } | ||
| 125 | else | ||
| 126 | ret->engine = ENGINE_get_default_DH(); | ||
| 127 | if(ret->engine) | ||
| 128 | { | ||
| 129 | ret->meth = ENGINE_get_DH(ret->engine); | ||
| 130 | if(!ret->meth) | ||
| 131 | { | ||
| 132 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB); | ||
| 133 | ENGINE_finish(ret->engine); | ||
| 134 | OPENSSL_free(ret); | ||
| 135 | return NULL; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 76 | ret->pad=0; | 139 | ret->pad=0; |
| 77 | ret->version=0; | 140 | ret->version=0; |
| 78 | ret->p=NULL; | 141 | ret->p=NULL; |
| @@ -80,21 +143,95 @@ DH *DH_new() | |||
| 80 | ret->length=0; | 143 | ret->length=0; |
| 81 | ret->pub_key=NULL; | 144 | ret->pub_key=NULL; |
| 82 | ret->priv_key=NULL; | 145 | ret->priv_key=NULL; |
| 146 | ret->q=NULL; | ||
| 147 | ret->j=NULL; | ||
| 148 | ret->seed = NULL; | ||
| 149 | ret->seedlen = 0; | ||
| 150 | ret->counter = NULL; | ||
| 151 | ret->method_mont_p=NULL; | ||
| 152 | ret->references = 1; | ||
| 153 | ret->flags=ret->meth->flags; | ||
| 154 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | ||
| 155 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) | ||
| 156 | { | ||
| 157 | if (ret->engine) | ||
| 158 | ENGINE_finish(ret->engine); | ||
| 159 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | ||
| 160 | OPENSSL_free(ret); | ||
| 161 | ret=NULL; | ||
| 162 | } | ||
| 83 | return(ret); | 163 | return(ret); |
| 84 | } | 164 | } |
| 85 | 165 | ||
| 86 | void DH_free(r) | 166 | void DH_free(DH *r) |
| 87 | DH *r; | ||
| 88 | { | 167 | { |
| 168 | int i; | ||
| 169 | if(r == NULL) return; | ||
| 170 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); | ||
| 171 | #ifdef REF_PRINT | ||
| 172 | REF_PRINT("DH",r); | ||
| 173 | #endif | ||
| 174 | if (i > 0) return; | ||
| 175 | #ifdef REF_CHECK | ||
| 176 | if (i < 0) | ||
| 177 | { | ||
| 178 | fprintf(stderr,"DH_free, bad reference count\n"); | ||
| 179 | abort(); | ||
| 180 | } | ||
| 181 | #endif | ||
| 182 | |||
| 183 | if (r->meth->finish) | ||
| 184 | r->meth->finish(r); | ||
| 185 | if (r->engine) | ||
| 186 | ENGINE_finish(r->engine); | ||
| 187 | |||
| 188 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); | ||
| 189 | |||
| 89 | if (r->p != NULL) BN_clear_free(r->p); | 190 | if (r->p != NULL) BN_clear_free(r->p); |
| 90 | if (r->g != NULL) BN_clear_free(r->g); | 191 | if (r->g != NULL) BN_clear_free(r->g); |
| 192 | if (r->q != NULL) BN_clear_free(r->q); | ||
| 193 | if (r->j != NULL) BN_clear_free(r->j); | ||
| 194 | if (r->seed) OPENSSL_free(r->seed); | ||
| 195 | if (r->counter != NULL) BN_clear_free(r->counter); | ||
| 91 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); | 196 | if (r->pub_key != NULL) BN_clear_free(r->pub_key); |
| 92 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); | 197 | if (r->priv_key != NULL) BN_clear_free(r->priv_key); |
| 93 | Free(r); | 198 | OPENSSL_free(r); |
| 199 | } | ||
| 200 | |||
| 201 | int DH_up_ref(DH *r) | ||
| 202 | { | ||
| 203 | int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH); | ||
| 204 | #ifdef REF_PRINT | ||
| 205 | REF_PRINT("DH",r); | ||
| 206 | #endif | ||
| 207 | #ifdef REF_CHECK | ||
| 208 | if (i < 2) | ||
| 209 | { | ||
| 210 | fprintf(stderr, "DH_up, bad reference count\n"); | ||
| 211 | abort(); | ||
| 212 | } | ||
| 213 | #endif | ||
| 214 | return ((i > 1) ? 1 : 0); | ||
| 215 | } | ||
| 216 | |||
| 217 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | ||
| 218 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | ||
| 219 | { | ||
| 220 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, | ||
| 221 | new_func, dup_func, free_func); | ||
| 222 | } | ||
| 223 | |||
| 224 | int DH_set_ex_data(DH *d, int idx, void *arg) | ||
| 225 | { | ||
| 226 | return(CRYPTO_set_ex_data(&d->ex_data,idx,arg)); | ||
| 227 | } | ||
| 228 | |||
| 229 | void *DH_get_ex_data(DH *d, int idx) | ||
| 230 | { | ||
| 231 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | ||
| 94 | } | 232 | } |
| 95 | 233 | ||
| 96 | int DH_size(dh) | 234 | int DH_size(const DH *dh) |
| 97 | DH *dh; | ||
| 98 | { | 235 | { |
| 99 | return(BN_num_bytes(dh->p)); | 236 | return(BN_num_bytes(dh->p)); |
| 100 | } | 237 | } |
