diff options
Diffstat (limited to 'src/lib/libcrypto/dh/dh_lib.c')
-rw-r--r-- | src/lib/libcrypto/dh/dh_lib.c | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c index 96f118c153..ba5fd41057 100644 --- a/src/lib/libcrypto/dh/dh_lib.c +++ b/src/lib/libcrypto/dh/dh_lib.c | |||
@@ -64,95 +64,78 @@ | |||
64 | 64 | ||
65 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; | 65 | const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT; |
66 | 66 | ||
67 | static DH_METHOD *default_DH_method; | 67 | static const DH_METHOD *default_DH_method = NULL; |
68 | static int dh_meth_num = 0; | 68 | |
69 | static STACK_OF(CRYPTO_EX_DATA_FUNCS) *dh_meth = NULL; | 69 | void DH_set_default_method(const DH_METHOD *meth) |
70 | 70 | { | |
71 | void DH_set_default_openssl_method(DH_METHOD *meth) | 71 | default_DH_method = meth; |
72 | { | 72 | } |
73 | ENGINE *e; | ||
74 | /* We'll need to notify the "openssl" ENGINE of this | ||
75 | * change too. We won't bother locking things down at | ||
76 | * our end as there was never any locking in these | ||
77 | * functions! */ | ||
78 | if(default_DH_method != meth) | ||
79 | { | ||
80 | default_DH_method = meth; | ||
81 | e = ENGINE_by_id("openssl"); | ||
82 | if(e) | ||
83 | { | ||
84 | ENGINE_set_DH(e, meth); | ||
85 | ENGINE_free(e); | ||
86 | } | ||
87 | } | ||
88 | } | ||
89 | 73 | ||
90 | DH_METHOD *DH_get_default_openssl_method(void) | 74 | const DH_METHOD *DH_get_default_method(void) |
91 | { | 75 | { |
92 | if(!default_DH_method) default_DH_method = DH_OpenSSL(); | 76 | if(!default_DH_method) |
77 | default_DH_method = DH_OpenSSL(); | ||
93 | return default_DH_method; | 78 | return default_DH_method; |
94 | } | 79 | } |
95 | 80 | ||
96 | #if 0 | 81 | int DH_set_method(DH *dh, const DH_METHOD *meth) |
97 | DH_METHOD *DH_set_method(DH *dh, DH_METHOD *meth) | 82 | { |
98 | { | 83 | /* NB: The caller is specifically setting a method, so it's not up to us |
99 | DH_METHOD *mtmp; | 84 | * to deal with which ENGINE it comes from. */ |
85 | const DH_METHOD *mtmp; | ||
100 | mtmp = dh->meth; | 86 | mtmp = dh->meth; |
101 | if (mtmp->finish) mtmp->finish(dh); | 87 | if (mtmp->finish) mtmp->finish(dh); |
88 | if (dh->engine) | ||
89 | { | ||
90 | ENGINE_finish(dh->engine); | ||
91 | dh->engine = NULL; | ||
92 | } | ||
102 | dh->meth = meth; | 93 | dh->meth = meth; |
103 | if (meth->init) meth->init(dh); | 94 | if (meth->init) meth->init(dh); |
104 | return mtmp; | 95 | return 1; |
105 | } | 96 | } |
106 | #else | ||
107 | int DH_set_method(DH *dh, ENGINE *engine) | ||
108 | { | ||
109 | ENGINE *mtmp; | ||
110 | DH_METHOD *meth; | ||
111 | mtmp = dh->engine; | ||
112 | meth = ENGINE_get_DH(mtmp); | ||
113 | if (!ENGINE_init(engine)) | ||
114 | return 0; | ||
115 | if (meth->finish) meth->finish(dh); | ||
116 | dh->engine= engine; | ||
117 | meth = ENGINE_get_DH(engine); | ||
118 | if (meth->init) meth->init(dh); | ||
119 | /* SHOULD ERROR CHECK THIS!!! */ | ||
120 | ENGINE_finish(mtmp); | ||
121 | return 1; | ||
122 | } | ||
123 | #endif | ||
124 | 97 | ||
125 | DH *DH_new(void) | 98 | DH *DH_new(void) |
126 | { | 99 | { |
127 | return DH_new_method(NULL); | 100 | return DH_new_method(NULL); |
128 | } | 101 | } |
129 | 102 | ||
130 | #if 0 | ||
131 | DH *DH_new_method(DH_METHOD *meth) | ||
132 | #else | ||
133 | DH *DH_new_method(ENGINE *engine) | 103 | DH *DH_new_method(ENGINE *engine) |
134 | #endif | ||
135 | { | 104 | { |
136 | DH_METHOD *meth; | ||
137 | DH *ret; | 105 | DH *ret; |
138 | ret=(DH *)OPENSSL_malloc(sizeof(DH)); | ||
139 | 106 | ||
107 | ret=(DH *)OPENSSL_malloc(sizeof(DH)); | ||
140 | if (ret == NULL) | 108 | if (ret == NULL) |
141 | { | 109 | { |
142 | DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE); | 110 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE); |
143 | return(NULL); | 111 | return(NULL); |
144 | } | 112 | } |
145 | if(engine) | 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 | } | ||
146 | ret->engine = engine; | 123 | ret->engine = engine; |
124 | } | ||
147 | else | 125 | else |
126 | ret->engine = ENGINE_get_default_DH(); | ||
127 | if(ret->engine) | ||
148 | { | 128 | { |
149 | if((ret->engine=ENGINE_get_default_DH()) == NULL) | 129 | ret->meth = ENGINE_get_DH(ret->engine); |
130 | if(!ret->meth) | ||
150 | { | 131 | { |
132 | DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB); | ||
133 | ENGINE_finish(ret->engine); | ||
151 | OPENSSL_free(ret); | 134 | OPENSSL_free(ret); |
152 | return NULL; | 135 | return NULL; |
153 | } | 136 | } |
154 | } | 137 | } |
155 | meth = ENGINE_get_DH(ret->engine); | 138 | |
156 | ret->pad=0; | 139 | ret->pad=0; |
157 | ret->version=0; | 140 | ret->version=0; |
158 | ret->p=NULL; | 141 | ret->p=NULL; |
@@ -167,11 +150,13 @@ DH *DH_new_method(ENGINE *engine) | |||
167 | ret->counter = NULL; | 150 | ret->counter = NULL; |
168 | ret->method_mont_p=NULL; | 151 | ret->method_mont_p=NULL; |
169 | ret->references = 1; | 152 | ret->references = 1; |
170 | ret->flags=meth->flags; | 153 | ret->flags=ret->meth->flags; |
171 | CRYPTO_new_ex_data(dh_meth,ret,&ret->ex_data); | 154 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); |
172 | if ((meth->init != NULL) && !meth->init(ret)) | 155 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) |
173 | { | 156 | { |
174 | CRYPTO_free_ex_data(dh_meth,ret,&ret->ex_data); | 157 | if (ret->engine) |
158 | ENGINE_finish(ret->engine); | ||
159 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data); | ||
175 | OPENSSL_free(ret); | 160 | OPENSSL_free(ret); |
176 | ret=NULL; | 161 | ret=NULL; |
177 | } | 162 | } |
@@ -180,7 +165,6 @@ DH *DH_new_method(ENGINE *engine) | |||
180 | 165 | ||
181 | void DH_free(DH *r) | 166 | void DH_free(DH *r) |
182 | { | 167 | { |
183 | DH_METHOD *meth; | ||
184 | int i; | 168 | int i; |
185 | if(r == NULL) return; | 169 | if(r == NULL) return; |
186 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); | 170 | i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH); |
@@ -196,11 +180,12 @@ void DH_free(DH *r) | |||
196 | } | 180 | } |
197 | #endif | 181 | #endif |
198 | 182 | ||
199 | meth = ENGINE_get_DH(r->engine); | 183 | if (r->meth->finish) |
200 | if(meth->finish) meth->finish(r); | 184 | r->meth->finish(r); |
201 | ENGINE_finish(r->engine); | 185 | if (r->engine) |
186 | ENGINE_finish(r->engine); | ||
202 | 187 | ||
203 | CRYPTO_free_ex_data(dh_meth, r, &r->ex_data); | 188 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data); |
204 | 189 | ||
205 | if (r->p != NULL) BN_clear_free(r->p); | 190 | if (r->p != NULL) BN_clear_free(r->p); |
206 | if (r->g != NULL) BN_clear_free(r->g); | 191 | if (r->g != NULL) BN_clear_free(r->g); |
@@ -213,12 +198,27 @@ void DH_free(DH *r) | |||
213 | OPENSSL_free(r); | 198 | OPENSSL_free(r); |
214 | } | 199 | } |
215 | 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 | |||
216 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, | 217 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
217 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) | 218 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) |
218 | { | 219 | { |
219 | dh_meth_num++; | 220 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp, |
220 | return(CRYPTO_get_ex_new_index(dh_meth_num-1, | 221 | new_func, dup_func, free_func); |
221 | &dh_meth,argl,argp,new_func,dup_func,free_func)); | ||
222 | } | 222 | } |
223 | 223 | ||
224 | int DH_set_ex_data(DH *d, int idx, void *arg) | 224 | int DH_set_ex_data(DH *d, int idx, void *arg) |
@@ -231,7 +231,7 @@ void *DH_get_ex_data(DH *d, int idx) | |||
231 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); | 231 | return(CRYPTO_get_ex_data(&d->ex_data,idx)); |
232 | } | 232 | } |
233 | 233 | ||
234 | int DH_size(DH *dh) | 234 | int DH_size(const DH *dh) |
235 | { | 235 | { |
236 | return(BN_num_bytes(dh->p)); | 236 | return(BN_num_bytes(dh->p)); |
237 | } | 237 | } |