diff options
Diffstat (limited to 'src/lib/libssl/ssl_lib.c')
-rw-r--r-- | src/lib/libssl/ssl_lib.c | 2061 |
1 files changed, 0 insertions, 2061 deletions
diff --git a/src/lib/libssl/ssl_lib.c b/src/lib/libssl/ssl_lib.c deleted file mode 100644 index 635b25062e..0000000000 --- a/src/lib/libssl/ssl_lib.c +++ /dev/null | |||
@@ -1,2061 +0,0 @@ | |||
1 | /*! \file ssl/ssl_lib.c | ||
2 | * \brief Version independent SSL functions. | ||
3 | */ | ||
4 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * This package is an SSL implementation written | ||
8 | * by Eric Young (eay@cryptsoft.com). | ||
9 | * The implementation was written so as to conform with Netscapes SSL. | ||
10 | * | ||
11 | * This library is free for commercial and non-commercial use as long as | ||
12 | * the following conditions are aheared to. The following conditions | ||
13 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
14 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
15 | * included with this distribution is covered by the same copyright terms | ||
16 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
17 | * | ||
18 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
19 | * the code are not to be removed. | ||
20 | * If this package is used in a product, Eric Young should be given attribution | ||
21 | * as the author of the parts of the library used. | ||
22 | * This can be in the form of a textual message at program startup or | ||
23 | * in documentation (online or textual) provided with the package. | ||
24 | * | ||
25 | * Redistribution and use in source and binary forms, with or without | ||
26 | * modification, are permitted provided that the following conditions | ||
27 | * are met: | ||
28 | * 1. Redistributions of source code must retain the copyright | ||
29 | * notice, this list of conditions and the following disclaimer. | ||
30 | * 2. Redistributions in binary form must reproduce the above copyright | ||
31 | * notice, this list of conditions and the following disclaimer in the | ||
32 | * documentation and/or other materials provided with the distribution. | ||
33 | * 3. All advertising materials mentioning features or use of this software | ||
34 | * must display the following acknowledgement: | ||
35 | * "This product includes cryptographic software written by | ||
36 | * Eric Young (eay@cryptsoft.com)" | ||
37 | * The word 'cryptographic' can be left out if the rouines from the library | ||
38 | * being used are not cryptographic related :-). | ||
39 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
40 | * the apps directory (application code) you must include an acknowledgement: | ||
41 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
42 | * | ||
43 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
53 | * SUCH DAMAGE. | ||
54 | * | ||
55 | * The licence and distribution terms for any publically available version or | ||
56 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
57 | * copied and put under another distribution licence | ||
58 | * [including the GNU Public Licence.] | ||
59 | */ | ||
60 | |||
61 | |||
62 | #include <assert.h> | ||
63 | #include <stdio.h> | ||
64 | #include <openssl/objects.h> | ||
65 | #include <openssl/lhash.h> | ||
66 | #include <openssl/x509v3.h> | ||
67 | #include "ssl_locl.h" | ||
68 | |||
69 | const char *SSL_version_str=OPENSSL_VERSION_TEXT; | ||
70 | |||
71 | static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ssl_meth=NULL; | ||
72 | static STACK_OF(CRYPTO_EX_DATA_FUNCS) *ssl_ctx_meth=NULL; | ||
73 | static int ssl_meth_num=0; | ||
74 | static int ssl_ctx_meth_num=0; | ||
75 | |||
76 | OPENSSL_GLOBAL SSL3_ENC_METHOD ssl3_undef_enc_method={ | ||
77 | /* evil casts, but these functions are only called if there's a library bug */ | ||
78 | (int (*)(SSL *,int))ssl_undefined_function, | ||
79 | (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, | ||
80 | ssl_undefined_function, | ||
81 | (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function, | ||
82 | (int (*)(SSL*, int))ssl_undefined_function, | ||
83 | (int (*)(SSL *, EVP_MD_CTX *, EVP_MD_CTX *, const char*, int, unsigned char *))ssl_undefined_function | ||
84 | }; | ||
85 | |||
86 | int SSL_clear(SSL *s) | ||
87 | { | ||
88 | int state; | ||
89 | |||
90 | if (s->method == NULL) | ||
91 | { | ||
92 | SSLerr(SSL_F_SSL_CLEAR,SSL_R_NO_METHOD_SPECIFIED); | ||
93 | return(0); | ||
94 | } | ||
95 | |||
96 | s->error=0; | ||
97 | s->hit=0; | ||
98 | s->shutdown=0; | ||
99 | |||
100 | #if 0 /* Disabled since version 1.10 of this file (early return not | ||
101 | * needed because SSL_clear is not called when doing renegotiation) */ | ||
102 | /* This is set if we are doing dynamic renegotiation so keep | ||
103 | * the old cipher. It is sort of a SSL_clear_lite :-) */ | ||
104 | if (s->new_session) return(1); | ||
105 | #else | ||
106 | if (s->new_session) | ||
107 | { | ||
108 | SSLerr(SSL_F_SSL_CLEAR,SSL_R_INTERNAL_ERROR); | ||
109 | return 0; | ||
110 | } | ||
111 | #endif | ||
112 | |||
113 | state=s->state; /* Keep to check if we throw away the session-id */ | ||
114 | s->type=0; | ||
115 | |||
116 | s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT); | ||
117 | |||
118 | s->version=s->method->version; | ||
119 | s->client_version=s->version; | ||
120 | s->rwstate=SSL_NOTHING; | ||
121 | s->rstate=SSL_ST_READ_HEADER; | ||
122 | s->read_ahead=s->ctx->read_ahead; | ||
123 | |||
124 | if (s->init_buf != NULL) | ||
125 | { | ||
126 | BUF_MEM_free(s->init_buf); | ||
127 | s->init_buf=NULL; | ||
128 | } | ||
129 | |||
130 | ssl_clear_cipher_ctx(s); | ||
131 | |||
132 | if (ssl_clear_bad_session(s)) | ||
133 | { | ||
134 | SSL_SESSION_free(s->session); | ||
135 | s->session=NULL; | ||
136 | } | ||
137 | |||
138 | s->first_packet=0; | ||
139 | |||
140 | #if 1 | ||
141 | /* Check to see if we were changed into a different method, if | ||
142 | * so, revert back if we are not doing session-id reuse. */ | ||
143 | if ((s->session == NULL) && (s->method != s->ctx->method)) | ||
144 | { | ||
145 | s->method->ssl_free(s); | ||
146 | s->method=s->ctx->method; | ||
147 | if (!s->method->ssl_new(s)) | ||
148 | return(0); | ||
149 | } | ||
150 | else | ||
151 | #endif | ||
152 | s->method->ssl_clear(s); | ||
153 | return(1); | ||
154 | } | ||
155 | |||
156 | /** Used to change an SSL_CTXs default SSL method type */ | ||
157 | int SSL_CTX_set_ssl_version(SSL_CTX *ctx,SSL_METHOD *meth) | ||
158 | { | ||
159 | STACK_OF(SSL_CIPHER) *sk; | ||
160 | |||
161 | ctx->method=meth; | ||
162 | |||
163 | sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list), | ||
164 | &(ctx->cipher_list_by_id),SSL_DEFAULT_CIPHER_LIST); | ||
165 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) | ||
166 | { | ||
167 | SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); | ||
168 | return(0); | ||
169 | } | ||
170 | return(1); | ||
171 | } | ||
172 | |||
173 | SSL *SSL_new(SSL_CTX *ctx) | ||
174 | { | ||
175 | SSL *s; | ||
176 | |||
177 | if (ctx == NULL) | ||
178 | { | ||
179 | SSLerr(SSL_F_SSL_NEW,SSL_R_NULL_SSL_CTX); | ||
180 | return(NULL); | ||
181 | } | ||
182 | if (ctx->method == NULL) | ||
183 | { | ||
184 | SSLerr(SSL_F_SSL_NEW,SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); | ||
185 | return(NULL); | ||
186 | } | ||
187 | |||
188 | s=(SSL *)OPENSSL_malloc(sizeof(SSL)); | ||
189 | if (s == NULL) goto err; | ||
190 | memset(s,0,sizeof(SSL)); | ||
191 | |||
192 | if (ctx->cert != NULL) | ||
193 | { | ||
194 | /* Earlier library versions used to copy the pointer to | ||
195 | * the CERT, not its contents; only when setting new | ||
196 | * parameters for the per-SSL copy, ssl_cert_new would be | ||
197 | * called (and the direct reference to the per-SSL_CTX | ||
198 | * settings would be lost, but those still were indirectly | ||
199 | * accessed for various purposes, and for that reason they | ||
200 | * used to be known as s->ctx->default_cert). | ||
201 | * Now we don't look at the SSL_CTX's CERT after having | ||
202 | * duplicated it once. */ | ||
203 | |||
204 | s->cert = ssl_cert_dup(ctx->cert); | ||
205 | if (s->cert == NULL) | ||
206 | goto err; | ||
207 | } | ||
208 | else | ||
209 | s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */ | ||
210 | s->sid_ctx_length=ctx->sid_ctx_length; | ||
211 | memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx)); | ||
212 | s->verify_mode=ctx->verify_mode; | ||
213 | s->verify_depth=ctx->verify_depth; | ||
214 | s->verify_callback=ctx->default_verify_callback; | ||
215 | s->purpose = ctx->purpose; | ||
216 | s->trust = ctx->trust; | ||
217 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); | ||
218 | s->ctx=ctx; | ||
219 | |||
220 | s->verify_result=X509_V_OK; | ||
221 | |||
222 | s->method=ctx->method; | ||
223 | |||
224 | if (!s->method->ssl_new(s)) | ||
225 | goto err; | ||
226 | |||
227 | s->quiet_shutdown=ctx->quiet_shutdown; | ||
228 | s->references=1; | ||
229 | s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1; | ||
230 | s->options=ctx->options; | ||
231 | s->mode=ctx->mode; | ||
232 | SSL_clear(s); | ||
233 | |||
234 | CRYPTO_new_ex_data(ssl_meth,s,&s->ex_data); | ||
235 | |||
236 | return(s); | ||
237 | err: | ||
238 | if (s != NULL) | ||
239 | { | ||
240 | if (s->cert != NULL) | ||
241 | ssl_cert_free(s->cert); | ||
242 | if (s->ctx != NULL) | ||
243 | SSL_CTX_free(s->ctx); /* decrement reference count */ | ||
244 | OPENSSL_free(s); | ||
245 | } | ||
246 | SSLerr(SSL_F_SSL_NEW,ERR_R_MALLOC_FAILURE); | ||
247 | return(NULL); | ||
248 | } | ||
249 | |||
250 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx, | ||
251 | unsigned int sid_ctx_len) | ||
252 | { | ||
253 | if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) | ||
254 | { | ||
255 | SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | ||
256 | return 0; | ||
257 | } | ||
258 | ctx->sid_ctx_length=sid_ctx_len; | ||
259 | memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len); | ||
260 | |||
261 | return 1; | ||
262 | } | ||
263 | |||
264 | int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx, | ||
265 | unsigned int sid_ctx_len) | ||
266 | { | ||
267 | if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) | ||
268 | { | ||
269 | SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); | ||
270 | return 0; | ||
271 | } | ||
272 | ssl->sid_ctx_length=sid_ctx_len; | ||
273 | memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len); | ||
274 | |||
275 | return 1; | ||
276 | } | ||
277 | |||
278 | int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) | ||
279 | { | ||
280 | if(X509_PURPOSE_get_by_id(purpose) == -1) { | ||
281 | SSLerr(SSL_F_SSL_CTX_SET_PURPOSE, SSL_R_INVALID_PURPOSE); | ||
282 | return 0; | ||
283 | } | ||
284 | s->purpose = purpose; | ||
285 | return 1; | ||
286 | } | ||
287 | |||
288 | int SSL_set_purpose(SSL *s, int purpose) | ||
289 | { | ||
290 | if(X509_PURPOSE_get_by_id(purpose) == -1) { | ||
291 | SSLerr(SSL_F_SSL_SET_PURPOSE, SSL_R_INVALID_PURPOSE); | ||
292 | return 0; | ||
293 | } | ||
294 | s->purpose = purpose; | ||
295 | return 1; | ||
296 | } | ||
297 | |||
298 | int SSL_CTX_set_trust(SSL_CTX *s, int trust) | ||
299 | { | ||
300 | if(X509_TRUST_get_by_id(trust) == -1) { | ||
301 | SSLerr(SSL_F_SSL_CTX_SET_TRUST, SSL_R_INVALID_TRUST); | ||
302 | return 0; | ||
303 | } | ||
304 | s->trust = trust; | ||
305 | return 1; | ||
306 | } | ||
307 | |||
308 | int SSL_set_trust(SSL *s, int trust) | ||
309 | { | ||
310 | if(X509_TRUST_get_by_id(trust) == -1) { | ||
311 | SSLerr(SSL_F_SSL_SET_TRUST, SSL_R_INVALID_TRUST); | ||
312 | return 0; | ||
313 | } | ||
314 | s->trust = trust; | ||
315 | return 1; | ||
316 | } | ||
317 | |||
318 | void SSL_free(SSL *s) | ||
319 | { | ||
320 | int i; | ||
321 | |||
322 | if(s == NULL) | ||
323 | return; | ||
324 | |||
325 | i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL); | ||
326 | #ifdef REF_PRINT | ||
327 | REF_PRINT("SSL",s); | ||
328 | #endif | ||
329 | if (i > 0) return; | ||
330 | #ifdef REF_CHECK | ||
331 | if (i < 0) | ||
332 | { | ||
333 | fprintf(stderr,"SSL_free, bad reference count\n"); | ||
334 | abort(); /* ok */ | ||
335 | } | ||
336 | #endif | ||
337 | |||
338 | CRYPTO_free_ex_data(ssl_meth,(char *)s,&s->ex_data); | ||
339 | |||
340 | if (s->bbio != NULL) | ||
341 | { | ||
342 | /* If the buffering BIO is in place, pop it off */ | ||
343 | if (s->bbio == s->wbio) | ||
344 | { | ||
345 | s->wbio=BIO_pop(s->wbio); | ||
346 | } | ||
347 | BIO_free(s->bbio); | ||
348 | s->bbio=NULL; | ||
349 | } | ||
350 | if (s->rbio != NULL) | ||
351 | BIO_free_all(s->rbio); | ||
352 | if ((s->wbio != NULL) && (s->wbio != s->rbio)) | ||
353 | BIO_free_all(s->wbio); | ||
354 | |||
355 | if (s->init_buf != NULL) BUF_MEM_free(s->init_buf); | ||
356 | |||
357 | /* add extra stuff */ | ||
358 | if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list); | ||
359 | if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id); | ||
360 | |||
361 | /* Make the next call work :-) */ | ||
362 | if (s->session != NULL) | ||
363 | { | ||
364 | ssl_clear_bad_session(s); | ||
365 | SSL_SESSION_free(s->session); | ||
366 | } | ||
367 | |||
368 | ssl_clear_cipher_ctx(s); | ||
369 | |||
370 | if (s->cert != NULL) ssl_cert_free(s->cert); | ||
371 | /* Free up if allocated */ | ||
372 | |||
373 | if (s->ctx) SSL_CTX_free(s->ctx); | ||
374 | |||
375 | if (s->client_CA != NULL) | ||
376 | sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free); | ||
377 | |||
378 | if (s->method != NULL) s->method->ssl_free(s); | ||
379 | |||
380 | OPENSSL_free(s); | ||
381 | } | ||
382 | |||
383 | void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) | ||
384 | { | ||
385 | /* If the output buffering BIO is still in place, remove it | ||
386 | */ | ||
387 | if (s->bbio != NULL) | ||
388 | { | ||
389 | if (s->wbio == s->bbio) | ||
390 | { | ||
391 | s->wbio=s->wbio->next_bio; | ||
392 | s->bbio->next_bio=NULL; | ||
393 | } | ||
394 | } | ||
395 | if ((s->rbio != NULL) && (s->rbio != rbio)) | ||
396 | BIO_free_all(s->rbio); | ||
397 | if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) | ||
398 | BIO_free_all(s->wbio); | ||
399 | s->rbio=rbio; | ||
400 | s->wbio=wbio; | ||
401 | } | ||
402 | |||
403 | BIO *SSL_get_rbio(SSL *s) | ||
404 | { return(s->rbio); } | ||
405 | |||
406 | BIO *SSL_get_wbio(SSL *s) | ||
407 | { return(s->wbio); } | ||
408 | |||
409 | int SSL_get_fd(SSL *s) | ||
410 | { | ||
411 | int ret= -1; | ||
412 | BIO *b,*r; | ||
413 | |||
414 | b=SSL_get_rbio(s); | ||
415 | r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); | ||
416 | if (r != NULL) | ||
417 | BIO_get_fd(r,&ret); | ||
418 | return(ret); | ||
419 | } | ||
420 | |||
421 | #ifndef NO_SOCK | ||
422 | int SSL_set_fd(SSL *s,int fd) | ||
423 | { | ||
424 | int ret=0; | ||
425 | BIO *bio=NULL; | ||
426 | |||
427 | bio=BIO_new(BIO_s_socket()); | ||
428 | |||
429 | if (bio == NULL) | ||
430 | { | ||
431 | SSLerr(SSL_F_SSL_SET_FD,ERR_R_BUF_LIB); | ||
432 | goto err; | ||
433 | } | ||
434 | BIO_set_fd(bio,fd,BIO_NOCLOSE); | ||
435 | SSL_set_bio(s,bio,bio); | ||
436 | ret=1; | ||
437 | err: | ||
438 | return(ret); | ||
439 | } | ||
440 | |||
441 | int SSL_set_wfd(SSL *s,int fd) | ||
442 | { | ||
443 | int ret=0; | ||
444 | BIO *bio=NULL; | ||
445 | |||
446 | if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET) | ||
447 | || ((int)BIO_get_fd(s->rbio,NULL) != fd)) | ||
448 | { | ||
449 | bio=BIO_new(BIO_s_socket()); | ||
450 | |||
451 | if (bio == NULL) | ||
452 | { SSLerr(SSL_F_SSL_SET_WFD,ERR_R_BUF_LIB); goto err; } | ||
453 | BIO_set_fd(bio,fd,BIO_NOCLOSE); | ||
454 | SSL_set_bio(s,SSL_get_rbio(s),bio); | ||
455 | } | ||
456 | else | ||
457 | SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); | ||
458 | ret=1; | ||
459 | err: | ||
460 | return(ret); | ||
461 | } | ||
462 | |||
463 | int SSL_set_rfd(SSL *s,int fd) | ||
464 | { | ||
465 | int ret=0; | ||
466 | BIO *bio=NULL; | ||
467 | |||
468 | if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET) | ||
469 | || ((int)BIO_get_fd(s->wbio,NULL) != fd)) | ||
470 | { | ||
471 | bio=BIO_new(BIO_s_socket()); | ||
472 | |||
473 | if (bio == NULL) | ||
474 | { | ||
475 | SSLerr(SSL_F_SSL_SET_RFD,ERR_R_BUF_LIB); | ||
476 | goto err; | ||
477 | } | ||
478 | BIO_set_fd(bio,fd,BIO_NOCLOSE); | ||
479 | SSL_set_bio(s,bio,SSL_get_wbio(s)); | ||
480 | } | ||
481 | else | ||
482 | SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); | ||
483 | ret=1; | ||
484 | err: | ||
485 | return(ret); | ||
486 | } | ||
487 | #endif | ||
488 | |||
489 | |||
490 | /* return length of latest Finished message we sent, copy to 'buf' */ | ||
491 | size_t SSL_get_finished(SSL *s, void *buf, size_t count) | ||
492 | { | ||
493 | size_t ret = 0; | ||
494 | |||
495 | if (s->s3 != NULL) | ||
496 | { | ||
497 | ret = s->s3->tmp.finish_md_len; | ||
498 | if (count > ret) | ||
499 | count = ret; | ||
500 | memcpy(buf, s->s3->tmp.finish_md, count); | ||
501 | } | ||
502 | return ret; | ||
503 | } | ||
504 | |||
505 | /* return length of latest Finished message we expected, copy to 'buf' */ | ||
506 | size_t SSL_get_peer_finished(SSL *s, void *buf, size_t count) | ||
507 | { | ||
508 | size_t ret = 0; | ||
509 | |||
510 | if (s->s3 != NULL) | ||
511 | { | ||
512 | ret = s->s3->tmp.peer_finish_md_len; | ||
513 | if (count > ret) | ||
514 | count = ret; | ||
515 | memcpy(buf, s->s3->tmp.peer_finish_md, count); | ||
516 | } | ||
517 | return ret; | ||
518 | } | ||
519 | |||
520 | |||
521 | int SSL_get_verify_mode(SSL *s) | ||
522 | { | ||
523 | return(s->verify_mode); | ||
524 | } | ||
525 | |||
526 | int SSL_get_verify_depth(SSL *s) | ||
527 | { | ||
528 | return(s->verify_depth); | ||
529 | } | ||
530 | |||
531 | int (*SSL_get_verify_callback(SSL *s))(int,X509_STORE_CTX *) | ||
532 | { | ||
533 | return(s->verify_callback); | ||
534 | } | ||
535 | |||
536 | int SSL_CTX_get_verify_mode(SSL_CTX *ctx) | ||
537 | { | ||
538 | return(ctx->verify_mode); | ||
539 | } | ||
540 | |||
541 | int SSL_CTX_get_verify_depth(SSL_CTX *ctx) | ||
542 | { | ||
543 | return(ctx->verify_depth); | ||
544 | } | ||
545 | |||
546 | int (*SSL_CTX_get_verify_callback(SSL_CTX *ctx))(int,X509_STORE_CTX *) | ||
547 | { | ||
548 | return(ctx->default_verify_callback); | ||
549 | } | ||
550 | |||
551 | void SSL_set_verify(SSL *s,int mode, | ||
552 | int (*callback)(int ok,X509_STORE_CTX *ctx)) | ||
553 | { | ||
554 | s->verify_mode=mode; | ||
555 | if (callback != NULL) | ||
556 | s->verify_callback=callback; | ||
557 | } | ||
558 | |||
559 | void SSL_set_verify_depth(SSL *s,int depth) | ||
560 | { | ||
561 | s->verify_depth=depth; | ||
562 | } | ||
563 | |||
564 | void SSL_set_read_ahead(SSL *s,int yes) | ||
565 | { | ||
566 | s->read_ahead=yes; | ||
567 | } | ||
568 | |||
569 | int SSL_get_read_ahead(SSL *s) | ||
570 | { | ||
571 | return(s->read_ahead); | ||
572 | } | ||
573 | |||
574 | int SSL_pending(SSL *s) | ||
575 | { | ||
576 | return(s->method->ssl_pending(s)); | ||
577 | } | ||
578 | |||
579 | X509 *SSL_get_peer_certificate(SSL *s) | ||
580 | { | ||
581 | X509 *r; | ||
582 | |||
583 | if ((s == NULL) || (s->session == NULL)) | ||
584 | r=NULL; | ||
585 | else | ||
586 | r=s->session->peer; | ||
587 | |||
588 | if (r == NULL) return(r); | ||
589 | |||
590 | CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); | ||
591 | |||
592 | return(r); | ||
593 | } | ||
594 | |||
595 | STACK_OF(X509) *SSL_get_peer_cert_chain(SSL *s) | ||
596 | { | ||
597 | STACK_OF(X509) *r; | ||
598 | |||
599 | if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) | ||
600 | r=NULL; | ||
601 | else | ||
602 | r=s->session->sess_cert->cert_chain; | ||
603 | |||
604 | /* If we are a client, cert_chain includes the peer's own | ||
605 | * certificate; if we are a server, it does not. */ | ||
606 | |||
607 | return(r); | ||
608 | } | ||
609 | |||
610 | /* Now in theory, since the calling process own 't' it should be safe to | ||
611 | * modify. We need to be able to read f without being hassled */ | ||
612 | void SSL_copy_session_id(SSL *t,SSL *f) | ||
613 | { | ||
614 | CERT *tmp; | ||
615 | |||
616 | /* Do we need to to SSL locking? */ | ||
617 | SSL_set_session(t,SSL_get_session(f)); | ||
618 | |||
619 | /* what if we are setup as SSLv2 but want to talk SSLv3 or | ||
620 | * vice-versa */ | ||
621 | if (t->method != f->method) | ||
622 | { | ||
623 | t->method->ssl_free(t); /* cleanup current */ | ||
624 | t->method=f->method; /* change method */ | ||
625 | t->method->ssl_new(t); /* setup new */ | ||
626 | } | ||
627 | |||
628 | tmp=t->cert; | ||
629 | if (f->cert != NULL) | ||
630 | { | ||
631 | CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); | ||
632 | t->cert=f->cert; | ||
633 | } | ||
634 | else | ||
635 | t->cert=NULL; | ||
636 | if (tmp != NULL) ssl_cert_free(tmp); | ||
637 | SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); | ||
638 | } | ||
639 | |||
640 | /* Fix this so it checks all the valid key/cert options */ | ||
641 | int SSL_CTX_check_private_key(SSL_CTX *ctx) | ||
642 | { | ||
643 | if ( (ctx == NULL) || | ||
644 | (ctx->cert == NULL) || | ||
645 | (ctx->cert->key->x509 == NULL)) | ||
646 | { | ||
647 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); | ||
648 | return(0); | ||
649 | } | ||
650 | if (ctx->cert->key->privatekey == NULL) | ||
651 | { | ||
652 | SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); | ||
653 | return(0); | ||
654 | } | ||
655 | return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); | ||
656 | } | ||
657 | |||
658 | /* Fix this function so that it takes an optional type parameter */ | ||
659 | int SSL_check_private_key(SSL *ssl) | ||
660 | { | ||
661 | if (ssl == NULL) | ||
662 | { | ||
663 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,ERR_R_PASSED_NULL_PARAMETER); | ||
664 | return(0); | ||
665 | } | ||
666 | if (ssl->cert == NULL) | ||
667 | { | ||
668 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); | ||
669 | return 0; | ||
670 | } | ||
671 | if (ssl->cert->key->x509 == NULL) | ||
672 | { | ||
673 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_CERTIFICATE_ASSIGNED); | ||
674 | return(0); | ||
675 | } | ||
676 | if (ssl->cert->key->privatekey == NULL) | ||
677 | { | ||
678 | SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY,SSL_R_NO_PRIVATE_KEY_ASSIGNED); | ||
679 | return(0); | ||
680 | } | ||
681 | return(X509_check_private_key(ssl->cert->key->x509, | ||
682 | ssl->cert->key->privatekey)); | ||
683 | } | ||
684 | |||
685 | int SSL_accept(SSL *s) | ||
686 | { | ||
687 | if (s->handshake_func == 0) | ||
688 | /* Not properly initialized yet */ | ||
689 | SSL_set_accept_state(s); | ||
690 | |||
691 | return(s->method->ssl_accept(s)); | ||
692 | } | ||
693 | |||
694 | int SSL_connect(SSL *s) | ||
695 | { | ||
696 | if (s->handshake_func == 0) | ||
697 | /* Not properly initialized yet */ | ||
698 | SSL_set_connect_state(s); | ||
699 | |||
700 | return(s->method->ssl_connect(s)); | ||
701 | } | ||
702 | |||
703 | long SSL_get_default_timeout(SSL *s) | ||
704 | { | ||
705 | return(s->method->get_timeout()); | ||
706 | } | ||
707 | |||
708 | int SSL_read(SSL *s,char *buf,int num) | ||
709 | { | ||
710 | if (s->handshake_func == 0) | ||
711 | { | ||
712 | SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED); | ||
713 | return -1; | ||
714 | } | ||
715 | |||
716 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) | ||
717 | { | ||
718 | s->rwstate=SSL_NOTHING; | ||
719 | return(0); | ||
720 | } | ||
721 | return(s->method->ssl_read(s,buf,num)); | ||
722 | } | ||
723 | |||
724 | int SSL_peek(SSL *s,char *buf,int num) | ||
725 | { | ||
726 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) | ||
727 | { | ||
728 | return(0); | ||
729 | } | ||
730 | return(s->method->ssl_peek(s,buf,num)); | ||
731 | } | ||
732 | |||
733 | int SSL_write(SSL *s,const char *buf,int num) | ||
734 | { | ||
735 | if (s->handshake_func == 0) | ||
736 | { | ||
737 | SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED); | ||
738 | return -1; | ||
739 | } | ||
740 | |||
741 | if (s->shutdown & SSL_SENT_SHUTDOWN) | ||
742 | { | ||
743 | s->rwstate=SSL_NOTHING; | ||
744 | SSLerr(SSL_F_SSL_WRITE,SSL_R_PROTOCOL_IS_SHUTDOWN); | ||
745 | return(-1); | ||
746 | } | ||
747 | return(s->method->ssl_write(s,buf,num)); | ||
748 | } | ||
749 | |||
750 | int SSL_shutdown(SSL *s) | ||
751 | { | ||
752 | /* Note that this function behaves differently from what one might | ||
753 | * expect. Return values are 0 for no success (yet), | ||
754 | * 1 for success; but calling it once is usually not enough, | ||
755 | * even if blocking I/O is used (see ssl3_shutdown). | ||
756 | */ | ||
757 | |||
758 | if (s->handshake_func == 0) | ||
759 | { | ||
760 | SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED); | ||
761 | return -1; | ||
762 | } | ||
763 | |||
764 | if ((s != NULL) && !SSL_in_init(s)) | ||
765 | return(s->method->ssl_shutdown(s)); | ||
766 | else | ||
767 | return(1); | ||
768 | } | ||
769 | |||
770 | int SSL_renegotiate(SSL *s) | ||
771 | { | ||
772 | s->new_session=1; | ||
773 | return(s->method->ssl_renegotiate(s)); | ||
774 | } | ||
775 | |||
776 | long SSL_ctrl(SSL *s,int cmd,long larg,char *parg) | ||
777 | { | ||
778 | long l; | ||
779 | |||
780 | switch (cmd) | ||
781 | { | ||
782 | case SSL_CTRL_GET_READ_AHEAD: | ||
783 | return(s->read_ahead); | ||
784 | case SSL_CTRL_SET_READ_AHEAD: | ||
785 | l=s->read_ahead; | ||
786 | s->read_ahead=larg; | ||
787 | return(l); | ||
788 | case SSL_CTRL_OPTIONS: | ||
789 | return(s->options|=larg); | ||
790 | case SSL_CTRL_MODE: | ||
791 | return(s->mode|=larg); | ||
792 | default: | ||
793 | return(s->method->ssl_ctrl(s,cmd,larg,parg)); | ||
794 | } | ||
795 | } | ||
796 | |||
797 | long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)()) | ||
798 | { | ||
799 | switch(cmd) | ||
800 | { | ||
801 | default: | ||
802 | return(s->method->ssl_callback_ctrl(s,cmd,fp)); | ||
803 | } | ||
804 | } | ||
805 | |||
806 | struct lhash_st *SSL_CTX_sessions(SSL_CTX *ctx) | ||
807 | { | ||
808 | return ctx->sessions; | ||
809 | } | ||
810 | |||
811 | long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,char *parg) | ||
812 | { | ||
813 | long l; | ||
814 | |||
815 | switch (cmd) | ||
816 | { | ||
817 | case SSL_CTRL_GET_READ_AHEAD: | ||
818 | return(ctx->read_ahead); | ||
819 | case SSL_CTRL_SET_READ_AHEAD: | ||
820 | l=ctx->read_ahead; | ||
821 | ctx->read_ahead=larg; | ||
822 | return(l); | ||
823 | |||
824 | case SSL_CTRL_SET_SESS_CACHE_SIZE: | ||
825 | l=ctx->session_cache_size; | ||
826 | ctx->session_cache_size=larg; | ||
827 | return(l); | ||
828 | case SSL_CTRL_GET_SESS_CACHE_SIZE: | ||
829 | return(ctx->session_cache_size); | ||
830 | case SSL_CTRL_SET_SESS_CACHE_MODE: | ||
831 | l=ctx->session_cache_mode; | ||
832 | ctx->session_cache_mode=larg; | ||
833 | return(l); | ||
834 | case SSL_CTRL_GET_SESS_CACHE_MODE: | ||
835 | return(ctx->session_cache_mode); | ||
836 | |||
837 | case SSL_CTRL_SESS_NUMBER: | ||
838 | return(ctx->sessions->num_items); | ||
839 | case SSL_CTRL_SESS_CONNECT: | ||
840 | return(ctx->stats.sess_connect); | ||
841 | case SSL_CTRL_SESS_CONNECT_GOOD: | ||
842 | return(ctx->stats.sess_connect_good); | ||
843 | case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: | ||
844 | return(ctx->stats.sess_connect_renegotiate); | ||
845 | case SSL_CTRL_SESS_ACCEPT: | ||
846 | return(ctx->stats.sess_accept); | ||
847 | case SSL_CTRL_SESS_ACCEPT_GOOD: | ||
848 | return(ctx->stats.sess_accept_good); | ||
849 | case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: | ||
850 | return(ctx->stats.sess_accept_renegotiate); | ||
851 | case SSL_CTRL_SESS_HIT: | ||
852 | return(ctx->stats.sess_hit); | ||
853 | case SSL_CTRL_SESS_CB_HIT: | ||
854 | return(ctx->stats.sess_cb_hit); | ||
855 | case SSL_CTRL_SESS_MISSES: | ||
856 | return(ctx->stats.sess_miss); | ||
857 | case SSL_CTRL_SESS_TIMEOUTS: | ||
858 | return(ctx->stats.sess_timeout); | ||
859 | case SSL_CTRL_SESS_CACHE_FULL: | ||
860 | return(ctx->stats.sess_cache_full); | ||
861 | case SSL_CTRL_OPTIONS: | ||
862 | return(ctx->options|=larg); | ||
863 | case SSL_CTRL_MODE: | ||
864 | return(ctx->mode|=larg); | ||
865 | default: | ||
866 | return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); | ||
867 | } | ||
868 | } | ||
869 | |||
870 | long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)()) | ||
871 | { | ||
872 | switch(cmd) | ||
873 | { | ||
874 | default: | ||
875 | return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); | ||
876 | } | ||
877 | } | ||
878 | |||
879 | int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b) | ||
880 | { | ||
881 | long l; | ||
882 | |||
883 | l=a->id-b->id; | ||
884 | if (l == 0L) | ||
885 | return(0); | ||
886 | else | ||
887 | return((l > 0)?1:-1); | ||
888 | } | ||
889 | |||
890 | int ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const *ap, | ||
891 | const SSL_CIPHER * const *bp) | ||
892 | { | ||
893 | long l; | ||
894 | |||
895 | l=(*ap)->id-(*bp)->id; | ||
896 | if (l == 0L) | ||
897 | return(0); | ||
898 | else | ||
899 | return((l > 0)?1:-1); | ||
900 | } | ||
901 | |||
902 | /** return a STACK of the ciphers available for the SSL and in order of | ||
903 | * preference */ | ||
904 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(SSL *s) | ||
905 | { | ||
906 | if ((s != NULL) && (s->cipher_list != NULL)) | ||
907 | { | ||
908 | return(s->cipher_list); | ||
909 | } | ||
910 | else if ((s->ctx != NULL) && | ||
911 | (s->ctx->cipher_list != NULL)) | ||
912 | { | ||
913 | return(s->ctx->cipher_list); | ||
914 | } | ||
915 | return(NULL); | ||
916 | } | ||
917 | |||
918 | /** return a STACK of the ciphers available for the SSL and in order of | ||
919 | * algorithm id */ | ||
920 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) | ||
921 | { | ||
922 | if ((s != NULL) && (s->cipher_list_by_id != NULL)) | ||
923 | { | ||
924 | return(s->cipher_list_by_id); | ||
925 | } | ||
926 | else if ((s != NULL) && (s->ctx != NULL) && | ||
927 | (s->ctx->cipher_list_by_id != NULL)) | ||
928 | { | ||
929 | return(s->ctx->cipher_list_by_id); | ||
930 | } | ||
931 | return(NULL); | ||
932 | } | ||
933 | |||
934 | /** The old interface to get the same thing as SSL_get_ciphers() */ | ||
935 | const char *SSL_get_cipher_list(SSL *s,int n) | ||
936 | { | ||
937 | SSL_CIPHER *c; | ||
938 | STACK_OF(SSL_CIPHER) *sk; | ||
939 | |||
940 | if (s == NULL) return(NULL); | ||
941 | sk=SSL_get_ciphers(s); | ||
942 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) | ||
943 | return(NULL); | ||
944 | c=sk_SSL_CIPHER_value(sk,n); | ||
945 | if (c == NULL) return(NULL); | ||
946 | return(c->name); | ||
947 | } | ||
948 | |||
949 | /** specify the ciphers to be used by default by the SSL_CTX */ | ||
950 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) | ||
951 | { | ||
952 | STACK_OF(SSL_CIPHER) *sk; | ||
953 | |||
954 | sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, | ||
955 | &ctx->cipher_list_by_id,str); | ||
956 | /* XXXX */ | ||
957 | return((sk == NULL)?0:1); | ||
958 | } | ||
959 | |||
960 | /** specify the ciphers to be used by the SSL */ | ||
961 | int SSL_set_cipher_list(SSL *s,const char *str) | ||
962 | { | ||
963 | STACK_OF(SSL_CIPHER) *sk; | ||
964 | |||
965 | sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, | ||
966 | &s->cipher_list_by_id,str); | ||
967 | /* XXXX */ | ||
968 | return((sk == NULL)?0:1); | ||
969 | } | ||
970 | |||
971 | /* works well for SSLv2, not so good for SSLv3 */ | ||
972 | char *SSL_get_shared_ciphers(SSL *s,char *buf,int len) | ||
973 | { | ||
974 | char *p; | ||
975 | const char *cp; | ||
976 | STACK_OF(SSL_CIPHER) *sk; | ||
977 | SSL_CIPHER *c; | ||
978 | int i; | ||
979 | |||
980 | if ((s->session == NULL) || (s->session->ciphers == NULL) || | ||
981 | (len < 2)) | ||
982 | return(NULL); | ||
983 | |||
984 | p=buf; | ||
985 | sk=s->session->ciphers; | ||
986 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) | ||
987 | { | ||
988 | /* Decrement for either the ':' or a '\0' */ | ||
989 | len--; | ||
990 | c=sk_SSL_CIPHER_value(sk,i); | ||
991 | for (cp=c->name; *cp; ) | ||
992 | { | ||
993 | if (len-- == 0) | ||
994 | { | ||
995 | *p='\0'; | ||
996 | return(buf); | ||
997 | } | ||
998 | else | ||
999 | *(p++)= *(cp++); | ||
1000 | } | ||
1001 | *(p++)=':'; | ||
1002 | } | ||
1003 | p[-1]='\0'; | ||
1004 | return(buf); | ||
1005 | } | ||
1006 | |||
1007 | int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p) | ||
1008 | { | ||
1009 | int i,j=0; | ||
1010 | SSL_CIPHER *c; | ||
1011 | unsigned char *q; | ||
1012 | |||
1013 | if (sk == NULL) return(0); | ||
1014 | q=p; | ||
1015 | |||
1016 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) | ||
1017 | { | ||
1018 | c=sk_SSL_CIPHER_value(sk,i); | ||
1019 | j=ssl_put_cipher_by_char(s,c,p); | ||
1020 | p+=j; | ||
1021 | } | ||
1022 | return(p-q); | ||
1023 | } | ||
1024 | |||
1025 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num, | ||
1026 | STACK_OF(SSL_CIPHER) **skp) | ||
1027 | { | ||
1028 | SSL_CIPHER *c; | ||
1029 | STACK_OF(SSL_CIPHER) *sk; | ||
1030 | int i,n; | ||
1031 | |||
1032 | n=ssl_put_cipher_by_char(s,NULL,NULL); | ||
1033 | if ((num%n) != 0) | ||
1034 | { | ||
1035 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); | ||
1036 | return(NULL); | ||
1037 | } | ||
1038 | if ((skp == NULL) || (*skp == NULL)) | ||
1039 | sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ | ||
1040 | else | ||
1041 | { | ||
1042 | sk= *skp; | ||
1043 | sk_SSL_CIPHER_zero(sk); | ||
1044 | } | ||
1045 | |||
1046 | for (i=0; i<num; i+=n) | ||
1047 | { | ||
1048 | c=ssl_get_cipher_by_char(s,p); | ||
1049 | p+=n; | ||
1050 | if (c != NULL) | ||
1051 | { | ||
1052 | if (!sk_SSL_CIPHER_push(sk,c)) | ||
1053 | { | ||
1054 | SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,ERR_R_MALLOC_FAILURE); | ||
1055 | goto err; | ||
1056 | } | ||
1057 | } | ||
1058 | } | ||
1059 | |||
1060 | if (skp != NULL) | ||
1061 | *skp=sk; | ||
1062 | return(sk); | ||
1063 | err: | ||
1064 | if ((skp == NULL) || (*skp == NULL)) | ||
1065 | sk_SSL_CIPHER_free(sk); | ||
1066 | return(NULL); | ||
1067 | } | ||
1068 | |||
1069 | unsigned long SSL_SESSION_hash(SSL_SESSION *a) | ||
1070 | { | ||
1071 | unsigned long l; | ||
1072 | |||
1073 | l=(unsigned long) | ||
1074 | ((unsigned int) a->session_id[0] )| | ||
1075 | ((unsigned int) a->session_id[1]<< 8L)| | ||
1076 | ((unsigned long)a->session_id[2]<<16L)| | ||
1077 | ((unsigned long)a->session_id[3]<<24L); | ||
1078 | return(l); | ||
1079 | } | ||
1080 | |||
1081 | int SSL_SESSION_cmp(SSL_SESSION *a,SSL_SESSION *b) | ||
1082 | { | ||
1083 | if (a->ssl_version != b->ssl_version) | ||
1084 | return(1); | ||
1085 | if (a->session_id_length != b->session_id_length) | ||
1086 | return(1); | ||
1087 | return(memcmp(a->session_id,b->session_id,a->session_id_length)); | ||
1088 | } | ||
1089 | |||
1090 | SSL_CTX *SSL_CTX_new(SSL_METHOD *meth) | ||
1091 | { | ||
1092 | SSL_CTX *ret=NULL; | ||
1093 | |||
1094 | if (meth == NULL) | ||
1095 | { | ||
1096 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_NULL_SSL_METHOD_PASSED); | ||
1097 | return(NULL); | ||
1098 | } | ||
1099 | |||
1100 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) | ||
1101 | { | ||
1102 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); | ||
1103 | goto err; | ||
1104 | } | ||
1105 | ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); | ||
1106 | if (ret == NULL) | ||
1107 | goto err; | ||
1108 | |||
1109 | memset(ret,0,sizeof(SSL_CTX)); | ||
1110 | |||
1111 | ret->method=meth; | ||
1112 | |||
1113 | ret->cert_store=NULL; | ||
1114 | ret->session_cache_mode=SSL_SESS_CACHE_SERVER; | ||
1115 | ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; | ||
1116 | ret->session_cache_head=NULL; | ||
1117 | ret->session_cache_tail=NULL; | ||
1118 | |||
1119 | /* We take the system default */ | ||
1120 | ret->session_timeout=meth->get_timeout(); | ||
1121 | |||
1122 | ret->new_session_cb=NULL; | ||
1123 | ret->remove_session_cb=NULL; | ||
1124 | ret->get_session_cb=NULL; | ||
1125 | |||
1126 | memset((char *)&ret->stats,0,sizeof(ret->stats)); | ||
1127 | |||
1128 | ret->references=1; | ||
1129 | ret->quiet_shutdown=0; | ||
1130 | |||
1131 | /* ret->cipher=NULL;*/ | ||
1132 | /* ret->s2->challenge=NULL; | ||
1133 | ret->master_key=NULL; | ||
1134 | ret->key_arg=NULL; | ||
1135 | ret->s2->conn_id=NULL; */ | ||
1136 | |||
1137 | ret->info_callback=NULL; | ||
1138 | |||
1139 | ret->app_verify_callback=NULL; | ||
1140 | ret->app_verify_arg=NULL; | ||
1141 | |||
1142 | ret->read_ahead=0; | ||
1143 | ret->verify_mode=SSL_VERIFY_NONE; | ||
1144 | ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ | ||
1145 | ret->default_verify_callback=NULL; | ||
1146 | if ((ret->cert=ssl_cert_new()) == NULL) | ||
1147 | goto err; | ||
1148 | |||
1149 | ret->default_passwd_callback=NULL; | ||
1150 | ret->default_passwd_callback_userdata=NULL; | ||
1151 | ret->client_cert_cb=NULL; | ||
1152 | |||
1153 | ret->sessions=lh_new(SSL_SESSION_hash,SSL_SESSION_cmp); | ||
1154 | if (ret->sessions == NULL) goto err; | ||
1155 | ret->cert_store=X509_STORE_new(); | ||
1156 | if (ret->cert_store == NULL) goto err; | ||
1157 | |||
1158 | ssl_create_cipher_list(ret->method, | ||
1159 | &ret->cipher_list,&ret->cipher_list_by_id, | ||
1160 | SSL_DEFAULT_CIPHER_LIST); | ||
1161 | if (ret->cipher_list == NULL | ||
1162 | || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) | ||
1163 | { | ||
1164 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_LIBRARY_HAS_NO_CIPHERS); | ||
1165 | goto err2; | ||
1166 | } | ||
1167 | |||
1168 | if ((ret->rsa_md5=EVP_get_digestbyname("ssl2-md5")) == NULL) | ||
1169 | { | ||
1170 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES); | ||
1171 | goto err2; | ||
1172 | } | ||
1173 | if ((ret->md5=EVP_get_digestbyname("ssl3-md5")) == NULL) | ||
1174 | { | ||
1175 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES); | ||
1176 | goto err2; | ||
1177 | } | ||
1178 | if ((ret->sha1=EVP_get_digestbyname("ssl3-sha1")) == NULL) | ||
1179 | { | ||
1180 | SSLerr(SSL_F_SSL_CTX_NEW,SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES); | ||
1181 | goto err2; | ||
1182 | } | ||
1183 | |||
1184 | if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) | ||
1185 | goto err; | ||
1186 | |||
1187 | CRYPTO_new_ex_data(ssl_ctx_meth,(char *)ret,&ret->ex_data); | ||
1188 | |||
1189 | ret->extra_certs=NULL; | ||
1190 | ret->comp_methods=SSL_COMP_get_compression_methods(); | ||
1191 | |||
1192 | return(ret); | ||
1193 | err: | ||
1194 | SSLerr(SSL_F_SSL_CTX_NEW,ERR_R_MALLOC_FAILURE); | ||
1195 | err2: | ||
1196 | if (ret != NULL) SSL_CTX_free(ret); | ||
1197 | return(NULL); | ||
1198 | } | ||
1199 | |||
1200 | static void SSL_COMP_free(SSL_COMP *comp) | ||
1201 | { OPENSSL_free(comp); } | ||
1202 | |||
1203 | void SSL_CTX_free(SSL_CTX *a) | ||
1204 | { | ||
1205 | int i; | ||
1206 | |||
1207 | if (a == NULL) return; | ||
1208 | |||
1209 | i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); | ||
1210 | #ifdef REF_PRINT | ||
1211 | REF_PRINT("SSL_CTX",a); | ||
1212 | #endif | ||
1213 | if (i > 0) return; | ||
1214 | #ifdef REF_CHECK | ||
1215 | if (i < 0) | ||
1216 | { | ||
1217 | fprintf(stderr,"SSL_CTX_free, bad reference count\n"); | ||
1218 | abort(); /* ok */ | ||
1219 | } | ||
1220 | #endif | ||
1221 | CRYPTO_free_ex_data(ssl_ctx_meth,(char *)a,&a->ex_data); | ||
1222 | |||
1223 | if (a->sessions != NULL) | ||
1224 | { | ||
1225 | SSL_CTX_flush_sessions(a,0); | ||
1226 | lh_free(a->sessions); | ||
1227 | } | ||
1228 | if (a->cert_store != NULL) | ||
1229 | X509_STORE_free(a->cert_store); | ||
1230 | if (a->cipher_list != NULL) | ||
1231 | sk_SSL_CIPHER_free(a->cipher_list); | ||
1232 | if (a->cipher_list_by_id != NULL) | ||
1233 | sk_SSL_CIPHER_free(a->cipher_list_by_id); | ||
1234 | if (a->cert != NULL) | ||
1235 | ssl_cert_free(a->cert); | ||
1236 | if (a->client_CA != NULL) | ||
1237 | sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); | ||
1238 | if (a->extra_certs != NULL) | ||
1239 | sk_X509_pop_free(a->extra_certs,X509_free); | ||
1240 | if (a->comp_methods != NULL) | ||
1241 | sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); | ||
1242 | OPENSSL_free(a); | ||
1243 | } | ||
1244 | |||
1245 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) | ||
1246 | { | ||
1247 | ctx->default_passwd_callback=cb; | ||
1248 | } | ||
1249 | |||
1250 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) | ||
1251 | { | ||
1252 | ctx->default_passwd_callback_userdata=u; | ||
1253 | } | ||
1254 | |||
1255 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,int (*cb)(),char *arg) | ||
1256 | { | ||
1257 | /* now | ||
1258 | * int (*cb)(X509_STORE_CTX *), | ||
1259 | * but should be | ||
1260 | * int (*cb)(X509_STORE_CTX *, void *arg) | ||
1261 | */ | ||
1262 | ctx->app_verify_callback=cb; | ||
1263 | ctx->app_verify_arg=arg; /* never used */ | ||
1264 | } | ||
1265 | |||
1266 | void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) | ||
1267 | { | ||
1268 | ctx->verify_mode=mode; | ||
1269 | ctx->default_verify_callback=cb; | ||
1270 | /* This needs cleaning up EAY EAY EAY */ | ||
1271 | X509_STORE_set_verify_cb_func(ctx->cert_store,cb); | ||
1272 | } | ||
1273 | |||
1274 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) | ||
1275 | { | ||
1276 | ctx->verify_depth=depth; | ||
1277 | } | ||
1278 | |||
1279 | void ssl_set_cert_masks(CERT *c, SSL_CIPHER *cipher) | ||
1280 | { | ||
1281 | CERT_PKEY *cpk; | ||
1282 | int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; | ||
1283 | int rsa_enc_export,dh_rsa_export,dh_dsa_export; | ||
1284 | int rsa_tmp_export,dh_tmp_export,kl; | ||
1285 | unsigned long mask,emask; | ||
1286 | |||
1287 | if (c == NULL) return; | ||
1288 | |||
1289 | kl=SSL_C_EXPORT_PKEYLENGTH(cipher); | ||
1290 | |||
1291 | #ifndef NO_RSA | ||
1292 | rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); | ||
1293 | rsa_tmp_export=(c->rsa_tmp_cb != NULL || | ||
1294 | (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); | ||
1295 | #else | ||
1296 | rsa_tmp=rsa_tmp_export=0; | ||
1297 | #endif | ||
1298 | #ifndef NO_DH | ||
1299 | dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); | ||
1300 | dh_tmp_export=(c->dh_tmp_cb != NULL || | ||
1301 | (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); | ||
1302 | #else | ||
1303 | dh_tmp=dh_tmp_export=0; | ||
1304 | #endif | ||
1305 | |||
1306 | cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); | ||
1307 | rsa_enc= (cpk->x509 != NULL && cpk->privatekey != NULL); | ||
1308 | rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); | ||
1309 | cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); | ||
1310 | rsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); | ||
1311 | cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); | ||
1312 | dsa_sign=(cpk->x509 != NULL && cpk->privatekey != NULL); | ||
1313 | cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); | ||
1314 | dh_rsa= (cpk->x509 != NULL && cpk->privatekey != NULL); | ||
1315 | dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); | ||
1316 | cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); | ||
1317 | /* FIX THIS EAY EAY EAY */ | ||
1318 | dh_dsa= (cpk->x509 != NULL && cpk->privatekey != NULL); | ||
1319 | dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); | ||
1320 | |||
1321 | mask=0; | ||
1322 | emask=0; | ||
1323 | |||
1324 | #ifdef CIPHER_DEBUG | ||
1325 | printf("rt=%d rte=%d dht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", | ||
1326 | rsa_tmp,rsa_tmp_export,dh_tmp, | ||
1327 | rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); | ||
1328 | #endif | ||
1329 | |||
1330 | if (rsa_enc || (rsa_tmp && rsa_sign)) | ||
1331 | mask|=SSL_kRSA; | ||
1332 | if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) | ||
1333 | emask|=SSL_kRSA; | ||
1334 | |||
1335 | #if 0 | ||
1336 | /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ | ||
1337 | if ( (dh_tmp || dh_rsa || dh_dsa) && | ||
1338 | (rsa_enc || rsa_sign || dsa_sign)) | ||
1339 | mask|=SSL_kEDH; | ||
1340 | if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && | ||
1341 | (rsa_enc || rsa_sign || dsa_sign)) | ||
1342 | emask|=SSL_kEDH; | ||
1343 | #endif | ||
1344 | |||
1345 | if (dh_tmp_export) | ||
1346 | emask|=SSL_kEDH; | ||
1347 | |||
1348 | if (dh_tmp) | ||
1349 | mask|=SSL_kEDH; | ||
1350 | |||
1351 | if (dh_rsa) mask|=SSL_kDHr; | ||
1352 | if (dh_rsa_export) emask|=SSL_kDHr; | ||
1353 | |||
1354 | if (dh_dsa) mask|=SSL_kDHd; | ||
1355 | if (dh_dsa_export) emask|=SSL_kDHd; | ||
1356 | |||
1357 | if (rsa_enc || rsa_sign) | ||
1358 | { | ||
1359 | mask|=SSL_aRSA; | ||
1360 | emask|=SSL_aRSA; | ||
1361 | } | ||
1362 | |||
1363 | if (dsa_sign) | ||
1364 | { | ||
1365 | mask|=SSL_aDSS; | ||
1366 | emask|=SSL_aDSS; | ||
1367 | } | ||
1368 | |||
1369 | mask|=SSL_aNULL; | ||
1370 | emask|=SSL_aNULL; | ||
1371 | |||
1372 | c->mask=mask; | ||
1373 | c->export_mask=emask; | ||
1374 | c->valid=1; | ||
1375 | } | ||
1376 | |||
1377 | /* THIS NEEDS CLEANING UP */ | ||
1378 | X509 *ssl_get_server_send_cert(SSL *s) | ||
1379 | { | ||
1380 | unsigned long alg,mask,kalg; | ||
1381 | CERT *c; | ||
1382 | int i,is_export; | ||
1383 | |||
1384 | c=s->cert; | ||
1385 | ssl_set_cert_masks(c, s->s3->tmp.new_cipher); | ||
1386 | alg=s->s3->tmp.new_cipher->algorithms; | ||
1387 | is_export=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher); | ||
1388 | mask=is_export?c->export_mask:c->mask; | ||
1389 | kalg=alg&(SSL_MKEY_MASK|SSL_AUTH_MASK); | ||
1390 | |||
1391 | if (kalg & SSL_kDHr) | ||
1392 | i=SSL_PKEY_DH_RSA; | ||
1393 | else if (kalg & SSL_kDHd) | ||
1394 | i=SSL_PKEY_DH_DSA; | ||
1395 | else if (kalg & SSL_aDSS) | ||
1396 | i=SSL_PKEY_DSA_SIGN; | ||
1397 | else if (kalg & SSL_aRSA) | ||
1398 | { | ||
1399 | if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL) | ||
1400 | i=SSL_PKEY_RSA_SIGN; | ||
1401 | else | ||
1402 | i=SSL_PKEY_RSA_ENC; | ||
1403 | } | ||
1404 | else /* if (kalg & SSL_aNULL) */ | ||
1405 | { | ||
1406 | SSLerr(SSL_F_SSL_GET_SERVER_SEND_CERT,SSL_R_INTERNAL_ERROR); | ||
1407 | return(NULL); | ||
1408 | } | ||
1409 | if (c->pkeys[i].x509 == NULL) return(NULL); | ||
1410 | return(c->pkeys[i].x509); | ||
1411 | } | ||
1412 | |||
1413 | EVP_PKEY *ssl_get_sign_pkey(SSL *s,SSL_CIPHER *cipher) | ||
1414 | { | ||
1415 | unsigned long alg; | ||
1416 | CERT *c; | ||
1417 | |||
1418 | alg=cipher->algorithms; | ||
1419 | c=s->cert; | ||
1420 | |||
1421 | if ((alg & SSL_aDSS) && | ||
1422 | (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) | ||
1423 | return(c->pkeys[SSL_PKEY_DSA_SIGN].privatekey); | ||
1424 | else if (alg & SSL_aRSA) | ||
1425 | { | ||
1426 | if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) | ||
1427 | return(c->pkeys[SSL_PKEY_RSA_SIGN].privatekey); | ||
1428 | else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) | ||
1429 | return(c->pkeys[SSL_PKEY_RSA_ENC].privatekey); | ||
1430 | else | ||
1431 | return(NULL); | ||
1432 | } | ||
1433 | else /* if (alg & SSL_aNULL) */ | ||
1434 | { | ||
1435 | SSLerr(SSL_F_SSL_GET_SIGN_PKEY,SSL_R_INTERNAL_ERROR); | ||
1436 | return(NULL); | ||
1437 | } | ||
1438 | } | ||
1439 | |||
1440 | void ssl_update_cache(SSL *s,int mode) | ||
1441 | { | ||
1442 | int i; | ||
1443 | |||
1444 | /* If the session_id_length is 0, we are not supposed to cache it, | ||
1445 | * and it would be rather hard to do anyway :-) */ | ||
1446 | if (s->session->session_id_length == 0) return; | ||
1447 | |||
1448 | if ((s->ctx->session_cache_mode & mode) | ||
1449 | && (!s->hit) | ||
1450 | && SSL_CTX_add_session(s->ctx,s->session) | ||
1451 | && (s->ctx->new_session_cb != NULL)) | ||
1452 | { | ||
1453 | CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); | ||
1454 | if (!s->ctx->new_session_cb(s,s->session)) | ||
1455 | SSL_SESSION_free(s->session); | ||
1456 | } | ||
1457 | |||
1458 | /* auto flush every 255 connections */ | ||
1459 | i=s->ctx->session_cache_mode; | ||
1460 | if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && | ||
1461 | ((i & mode) == mode)) | ||
1462 | { | ||
1463 | if ( (((mode & SSL_SESS_CACHE_CLIENT) | ||
1464 | ?s->ctx->stats.sess_connect_good | ||
1465 | :s->ctx->stats.sess_accept_good) & 0xff) == 0xff) | ||
1466 | { | ||
1467 | SSL_CTX_flush_sessions(s->ctx,time(NULL)); | ||
1468 | } | ||
1469 | } | ||
1470 | } | ||
1471 | |||
1472 | SSL_METHOD *SSL_get_ssl_method(SSL *s) | ||
1473 | { | ||
1474 | return(s->method); | ||
1475 | } | ||
1476 | |||
1477 | int SSL_set_ssl_method(SSL *s,SSL_METHOD *meth) | ||
1478 | { | ||
1479 | int conn= -1; | ||
1480 | int ret=1; | ||
1481 | |||
1482 | if (s->method != meth) | ||
1483 | { | ||
1484 | if (s->handshake_func != NULL) | ||
1485 | conn=(s->handshake_func == s->method->ssl_connect); | ||
1486 | |||
1487 | if (s->method->version == meth->version) | ||
1488 | s->method=meth; | ||
1489 | else | ||
1490 | { | ||
1491 | s->method->ssl_free(s); | ||
1492 | s->method=meth; | ||
1493 | ret=s->method->ssl_new(s); | ||
1494 | } | ||
1495 | |||
1496 | if (conn == 1) | ||
1497 | s->handshake_func=meth->ssl_connect; | ||
1498 | else if (conn == 0) | ||
1499 | s->handshake_func=meth->ssl_accept; | ||
1500 | } | ||
1501 | return(ret); | ||
1502 | } | ||
1503 | |||
1504 | int SSL_get_error(SSL *s,int i) | ||
1505 | { | ||
1506 | int reason; | ||
1507 | unsigned long l; | ||
1508 | BIO *bio; | ||
1509 | |||
1510 | if (i > 0) return(SSL_ERROR_NONE); | ||
1511 | |||
1512 | /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake | ||
1513 | * etc, where we do encode the error */ | ||
1514 | if ((l=ERR_peek_error()) != 0) | ||
1515 | { | ||
1516 | if (ERR_GET_LIB(l) == ERR_LIB_SYS) | ||
1517 | return(SSL_ERROR_SYSCALL); | ||
1518 | else | ||
1519 | return(SSL_ERROR_SSL); | ||
1520 | } | ||
1521 | |||
1522 | if ((i < 0) && SSL_want_read(s)) | ||
1523 | { | ||
1524 | bio=SSL_get_rbio(s); | ||
1525 | if (BIO_should_read(bio)) | ||
1526 | return(SSL_ERROR_WANT_READ); | ||
1527 | else if (BIO_should_write(bio)) | ||
1528 | /* This one doesn't make too much sense ... We never try | ||
1529 | * to write to the rbio, and an application program where | ||
1530 | * rbio and wbio are separate couldn't even know what it | ||
1531 | * should wait for. | ||
1532 | * However if we ever set s->rwstate incorrectly | ||
1533 | * (so that we have SSL_want_read(s) instead of | ||
1534 | * SSL_want_write(s)) and rbio and wbio *are* the same, | ||
1535 | * this test works around that bug; so it might be safer | ||
1536 | * to keep it. */ | ||
1537 | return(SSL_ERROR_WANT_WRITE); | ||
1538 | else if (BIO_should_io_special(bio)) | ||
1539 | { | ||
1540 | reason=BIO_get_retry_reason(bio); | ||
1541 | if (reason == BIO_RR_CONNECT) | ||
1542 | return(SSL_ERROR_WANT_CONNECT); | ||
1543 | else | ||
1544 | return(SSL_ERROR_SYSCALL); /* unknown */ | ||
1545 | } | ||
1546 | } | ||
1547 | |||
1548 | if ((i < 0) && SSL_want_write(s)) | ||
1549 | { | ||
1550 | bio=SSL_get_wbio(s); | ||
1551 | if (BIO_should_write(bio)) | ||
1552 | return(SSL_ERROR_WANT_WRITE); | ||
1553 | else if (BIO_should_read(bio)) | ||
1554 | /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ | ||
1555 | return(SSL_ERROR_WANT_READ); | ||
1556 | else if (BIO_should_io_special(bio)) | ||
1557 | { | ||
1558 | reason=BIO_get_retry_reason(bio); | ||
1559 | if (reason == BIO_RR_CONNECT) | ||
1560 | return(SSL_ERROR_WANT_CONNECT); | ||
1561 | else | ||
1562 | return(SSL_ERROR_SYSCALL); | ||
1563 | } | ||
1564 | } | ||
1565 | if ((i < 0) && SSL_want_x509_lookup(s)) | ||
1566 | { | ||
1567 | return(SSL_ERROR_WANT_X509_LOOKUP); | ||
1568 | } | ||
1569 | |||
1570 | if (i == 0) | ||
1571 | { | ||
1572 | if (s->version == SSL2_VERSION) | ||
1573 | { | ||
1574 | /* assume it is the socket being closed */ | ||
1575 | return(SSL_ERROR_ZERO_RETURN); | ||
1576 | } | ||
1577 | else | ||
1578 | { | ||
1579 | if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && | ||
1580 | (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) | ||
1581 | return(SSL_ERROR_ZERO_RETURN); | ||
1582 | } | ||
1583 | } | ||
1584 | return(SSL_ERROR_SYSCALL); | ||
1585 | } | ||
1586 | |||
1587 | int SSL_do_handshake(SSL *s) | ||
1588 | { | ||
1589 | int ret=1; | ||
1590 | |||
1591 | if (s->handshake_func == NULL) | ||
1592 | { | ||
1593 | SSLerr(SSL_F_SSL_DO_HANDSHAKE,SSL_R_CONNECTION_TYPE_NOT_SET); | ||
1594 | return(-1); | ||
1595 | } | ||
1596 | |||
1597 | s->method->ssl_renegotiate_check(s); | ||
1598 | |||
1599 | if (SSL_in_init(s) || SSL_in_before(s)) | ||
1600 | { | ||
1601 | ret=s->handshake_func(s); | ||
1602 | } | ||
1603 | return(ret); | ||
1604 | } | ||
1605 | |||
1606 | /* For the next 2 functions, SSL_clear() sets shutdown and so | ||
1607 | * one of these calls will reset it */ | ||
1608 | void SSL_set_accept_state(SSL *s) | ||
1609 | { | ||
1610 | s->server=1; | ||
1611 | s->shutdown=0; | ||
1612 | s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; | ||
1613 | s->handshake_func=s->method->ssl_accept; | ||
1614 | /* clear the current cipher */ | ||
1615 | ssl_clear_cipher_ctx(s); | ||
1616 | } | ||
1617 | |||
1618 | void SSL_set_connect_state(SSL *s) | ||
1619 | { | ||
1620 | s->server=0; | ||
1621 | s->shutdown=0; | ||
1622 | s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; | ||
1623 | s->handshake_func=s->method->ssl_connect; | ||
1624 | /* clear the current cipher */ | ||
1625 | ssl_clear_cipher_ctx(s); | ||
1626 | } | ||
1627 | |||
1628 | int ssl_undefined_function(SSL *s) | ||
1629 | { | ||
1630 | SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | ||
1631 | return(0); | ||
1632 | } | ||
1633 | |||
1634 | SSL_METHOD *ssl_bad_method(int ver) | ||
1635 | { | ||
1636 | SSLerr(SSL_F_SSL_BAD_METHOD,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | ||
1637 | return(NULL); | ||
1638 | } | ||
1639 | |||
1640 | const char *SSL_get_version(SSL *s) | ||
1641 | { | ||
1642 | if (s->version == TLS1_VERSION) | ||
1643 | return("TLSv1"); | ||
1644 | else if (s->version == SSL3_VERSION) | ||
1645 | return("SSLv3"); | ||
1646 | else if (s->version == SSL2_VERSION) | ||
1647 | return("SSLv2"); | ||
1648 | else | ||
1649 | return("unknown"); | ||
1650 | } | ||
1651 | |||
1652 | SSL *SSL_dup(SSL *s) | ||
1653 | { | ||
1654 | STACK_OF(X509_NAME) *sk; | ||
1655 | X509_NAME *xn; | ||
1656 | SSL *ret; | ||
1657 | int i; | ||
1658 | |||
1659 | if ((ret=SSL_new(SSL_get_SSL_CTX(s))) == NULL) | ||
1660 | return(NULL); | ||
1661 | |||
1662 | if (s->session != NULL) | ||
1663 | { | ||
1664 | /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */ | ||
1665 | SSL_copy_session_id(ret,s); | ||
1666 | } | ||
1667 | else | ||
1668 | { | ||
1669 | /* No session has been established yet, so we have to expect | ||
1670 | * that s->cert or ret->cert will be changed later -- | ||
1671 | * they should not both point to the same object, | ||
1672 | * and thus we can't use SSL_copy_session_id. */ | ||
1673 | |||
1674 | ret->method = s->method; | ||
1675 | ret->method->ssl_new(ret); | ||
1676 | |||
1677 | if (s->cert != NULL) | ||
1678 | { | ||
1679 | ret->cert = ssl_cert_dup(s->cert); | ||
1680 | if (ret->cert == NULL) | ||
1681 | goto err; | ||
1682 | } | ||
1683 | |||
1684 | SSL_set_session_id_context(ret, | ||
1685 | s->sid_ctx, s->sid_ctx_length); | ||
1686 | } | ||
1687 | |||
1688 | SSL_set_read_ahead(ret,SSL_get_read_ahead(s)); | ||
1689 | SSL_set_verify(ret,SSL_get_verify_mode(s), | ||
1690 | SSL_get_verify_callback(s)); | ||
1691 | SSL_set_verify_depth(ret,SSL_get_verify_depth(s)); | ||
1692 | |||
1693 | SSL_set_info_callback(ret,SSL_get_info_callback(s)); | ||
1694 | |||
1695 | ret->debug=s->debug; | ||
1696 | ret->options=s->options; | ||
1697 | |||
1698 | /* copy app data, a little dangerous perhaps */ | ||
1699 | if (!CRYPTO_dup_ex_data(ssl_meth,&ret->ex_data,&s->ex_data)) | ||
1700 | goto err; | ||
1701 | |||
1702 | /* setup rbio, and wbio */ | ||
1703 | if (s->rbio != NULL) | ||
1704 | { | ||
1705 | if (!BIO_dup_state(s->rbio,(char *)&ret->rbio)) | ||
1706 | goto err; | ||
1707 | } | ||
1708 | if (s->wbio != NULL) | ||
1709 | { | ||
1710 | if (s->wbio != s->rbio) | ||
1711 | { | ||
1712 | if (!BIO_dup_state(s->wbio,(char *)&ret->wbio)) | ||
1713 | goto err; | ||
1714 | } | ||
1715 | else | ||
1716 | ret->wbio=ret->rbio; | ||
1717 | } | ||
1718 | |||
1719 | /* dup the cipher_list and cipher_list_by_id stacks */ | ||
1720 | if (s->cipher_list != NULL) | ||
1721 | { | ||
1722 | if ((ret->cipher_list=sk_SSL_CIPHER_dup(s->cipher_list)) == NULL) | ||
1723 | goto err; | ||
1724 | } | ||
1725 | if (s->cipher_list_by_id != NULL) | ||
1726 | if ((ret->cipher_list_by_id=sk_SSL_CIPHER_dup(s->cipher_list_by_id)) | ||
1727 | == NULL) | ||
1728 | goto err; | ||
1729 | |||
1730 | /* Dup the client_CA list */ | ||
1731 | if (s->client_CA != NULL) | ||
1732 | { | ||
1733 | if ((sk=sk_X509_NAME_dup(s->client_CA)) == NULL) goto err; | ||
1734 | ret->client_CA=sk; | ||
1735 | for (i=0; i<sk_X509_NAME_num(sk); i++) | ||
1736 | { | ||
1737 | xn=sk_X509_NAME_value(sk,i); | ||
1738 | if (sk_X509_NAME_set(sk,i,X509_NAME_dup(xn)) == NULL) | ||
1739 | { | ||
1740 | X509_NAME_free(xn); | ||
1741 | goto err; | ||
1742 | } | ||
1743 | } | ||
1744 | } | ||
1745 | |||
1746 | ret->shutdown=s->shutdown; | ||
1747 | ret->state=s->state; | ||
1748 | ret->handshake_func=s->handshake_func; | ||
1749 | ret->server=s->server; | ||
1750 | |||
1751 | if (0) | ||
1752 | { | ||
1753 | err: | ||
1754 | if (ret != NULL) SSL_free(ret); | ||
1755 | ret=NULL; | ||
1756 | } | ||
1757 | return(ret); | ||
1758 | } | ||
1759 | |||
1760 | void ssl_clear_cipher_ctx(SSL *s) | ||
1761 | { | ||
1762 | if (s->enc_read_ctx != NULL) | ||
1763 | { | ||
1764 | EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); | ||
1765 | OPENSSL_free(s->enc_read_ctx); | ||
1766 | s->enc_read_ctx=NULL; | ||
1767 | } | ||
1768 | if (s->enc_write_ctx != NULL) | ||
1769 | { | ||
1770 | EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); | ||
1771 | OPENSSL_free(s->enc_write_ctx); | ||
1772 | s->enc_write_ctx=NULL; | ||
1773 | } | ||
1774 | if (s->expand != NULL) | ||
1775 | { | ||
1776 | COMP_CTX_free(s->expand); | ||
1777 | s->expand=NULL; | ||
1778 | } | ||
1779 | if (s->compress != NULL) | ||
1780 | { | ||
1781 | COMP_CTX_free(s->compress); | ||
1782 | s->compress=NULL; | ||
1783 | } | ||
1784 | } | ||
1785 | |||
1786 | /* Fix this function so that it takes an optional type parameter */ | ||
1787 | X509 *SSL_get_certificate(SSL *s) | ||
1788 | { | ||
1789 | if (s->cert != NULL) | ||
1790 | return(s->cert->key->x509); | ||
1791 | else | ||
1792 | return(NULL); | ||
1793 | } | ||
1794 | |||
1795 | /* Fix this function so that it takes an optional type parameter */ | ||
1796 | EVP_PKEY *SSL_get_privatekey(SSL *s) | ||
1797 | { | ||
1798 | if (s->cert != NULL) | ||
1799 | return(s->cert->key->privatekey); | ||
1800 | else | ||
1801 | return(NULL); | ||
1802 | } | ||
1803 | |||
1804 | SSL_CIPHER *SSL_get_current_cipher(SSL *s) | ||
1805 | { | ||
1806 | if ((s->session != NULL) && (s->session->cipher != NULL)) | ||
1807 | return(s->session->cipher); | ||
1808 | return(NULL); | ||
1809 | } | ||
1810 | |||
1811 | int ssl_init_wbio_buffer(SSL *s,int push) | ||
1812 | { | ||
1813 | BIO *bbio; | ||
1814 | |||
1815 | if (s->bbio == NULL) | ||
1816 | { | ||
1817 | bbio=BIO_new(BIO_f_buffer()); | ||
1818 | if (bbio == NULL) return(0); | ||
1819 | s->bbio=bbio; | ||
1820 | } | ||
1821 | else | ||
1822 | { | ||
1823 | bbio=s->bbio; | ||
1824 | if (s->bbio == s->wbio) | ||
1825 | s->wbio=BIO_pop(s->wbio); | ||
1826 | } | ||
1827 | (void)BIO_reset(bbio); | ||
1828 | /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ | ||
1829 | if (!BIO_set_read_buffer_size(bbio,1)) | ||
1830 | { | ||
1831 | SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER,ERR_R_BUF_LIB); | ||
1832 | return(0); | ||
1833 | } | ||
1834 | if (push) | ||
1835 | { | ||
1836 | if (s->wbio != bbio) | ||
1837 | s->wbio=BIO_push(bbio,s->wbio); | ||
1838 | } | ||
1839 | else | ||
1840 | { | ||
1841 | if (s->wbio == bbio) | ||
1842 | s->wbio=BIO_pop(bbio); | ||
1843 | } | ||
1844 | return(1); | ||
1845 | } | ||
1846 | |||
1847 | void ssl_free_wbio_buffer(SSL *s) | ||
1848 | { | ||
1849 | if (s->bbio == NULL) return; | ||
1850 | |||
1851 | if (s->bbio == s->wbio) | ||
1852 | { | ||
1853 | /* remove buffering */ | ||
1854 | s->wbio=BIO_pop(s->wbio); | ||
1855 | #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ | ||
1856 | assert(s->wbio != NULL); | ||
1857 | #endif | ||
1858 | } | ||
1859 | BIO_free(s->bbio); | ||
1860 | s->bbio=NULL; | ||
1861 | } | ||
1862 | |||
1863 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) | ||
1864 | { | ||
1865 | ctx->quiet_shutdown=mode; | ||
1866 | } | ||
1867 | |||
1868 | int SSL_CTX_get_quiet_shutdown(SSL_CTX *ctx) | ||
1869 | { | ||
1870 | return(ctx->quiet_shutdown); | ||
1871 | } | ||
1872 | |||
1873 | void SSL_set_quiet_shutdown(SSL *s,int mode) | ||
1874 | { | ||
1875 | s->quiet_shutdown=mode; | ||
1876 | } | ||
1877 | |||
1878 | int SSL_get_quiet_shutdown(SSL *s) | ||
1879 | { | ||
1880 | return(s->quiet_shutdown); | ||
1881 | } | ||
1882 | |||
1883 | void SSL_set_shutdown(SSL *s,int mode) | ||
1884 | { | ||
1885 | s->shutdown=mode; | ||
1886 | } | ||
1887 | |||
1888 | int SSL_get_shutdown(SSL *s) | ||
1889 | { | ||
1890 | return(s->shutdown); | ||
1891 | } | ||
1892 | |||
1893 | int SSL_version(SSL *s) | ||
1894 | { | ||
1895 | return(s->version); | ||
1896 | } | ||
1897 | |||
1898 | SSL_CTX *SSL_get_SSL_CTX(SSL *ssl) | ||
1899 | { | ||
1900 | return(ssl->ctx); | ||
1901 | } | ||
1902 | |||
1903 | #ifndef NO_STDIO | ||
1904 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) | ||
1905 | { | ||
1906 | return(X509_STORE_set_default_paths(ctx->cert_store)); | ||
1907 | } | ||
1908 | |||
1909 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, | ||
1910 | const char *CApath) | ||
1911 | { | ||
1912 | return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); | ||
1913 | } | ||
1914 | #endif | ||
1915 | |||
1916 | void SSL_set_info_callback(SSL *ssl,void (*cb)()) | ||
1917 | { | ||
1918 | ssl->info_callback=cb; | ||
1919 | } | ||
1920 | |||
1921 | void (*SSL_get_info_callback(SSL *ssl))(void) | ||
1922 | { | ||
1923 | return((void (*)())ssl->info_callback); | ||
1924 | } | ||
1925 | |||
1926 | int SSL_state(SSL *ssl) | ||
1927 | { | ||
1928 | return(ssl->state); | ||
1929 | } | ||
1930 | |||
1931 | void SSL_set_verify_result(SSL *ssl,long arg) | ||
1932 | { | ||
1933 | ssl->verify_result=arg; | ||
1934 | } | ||
1935 | |||
1936 | long SSL_get_verify_result(SSL *ssl) | ||
1937 | { | ||
1938 | return(ssl->verify_result); | ||
1939 | } | ||
1940 | |||
1941 | int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, | ||
1942 | CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) | ||
1943 | { | ||
1944 | ssl_meth_num++; | ||
1945 | return(CRYPTO_get_ex_new_index(ssl_meth_num-1, | ||
1946 | &ssl_meth,argl,argp,new_func,dup_func,free_func)); | ||
1947 | } | ||
1948 | |||
1949 | int SSL_set_ex_data(SSL *s,int idx,void *arg) | ||
1950 | { | ||
1951 | return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); | ||
1952 | } | ||
1953 | |||
1954 | void *SSL_get_ex_data(SSL *s,int idx) | ||
1955 | { | ||
1956 | return(CRYPTO_get_ex_data(&s->ex_data,idx)); | ||
1957 | } | ||
1958 | |||
1959 | int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, | ||
1960 | CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) | ||
1961 | { | ||
1962 | ssl_ctx_meth_num++; | ||
1963 | return(CRYPTO_get_ex_new_index(ssl_ctx_meth_num-1, | ||
1964 | &ssl_ctx_meth,argl,argp,new_func,dup_func,free_func)); | ||
1965 | } | ||
1966 | |||
1967 | int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) | ||
1968 | { | ||
1969 | return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); | ||
1970 | } | ||
1971 | |||
1972 | void *SSL_CTX_get_ex_data(SSL_CTX *s,int idx) | ||
1973 | { | ||
1974 | return(CRYPTO_get_ex_data(&s->ex_data,idx)); | ||
1975 | } | ||
1976 | |||
1977 | int ssl_ok(SSL *s) | ||
1978 | { | ||
1979 | return(1); | ||
1980 | } | ||
1981 | |||
1982 | X509_STORE *SSL_CTX_get_cert_store(SSL_CTX *ctx) | ||
1983 | { | ||
1984 | return(ctx->cert_store); | ||
1985 | } | ||
1986 | |||
1987 | void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) | ||
1988 | { | ||
1989 | if (ctx->cert_store != NULL) | ||
1990 | X509_STORE_free(ctx->cert_store); | ||
1991 | ctx->cert_store=store; | ||
1992 | } | ||
1993 | |||
1994 | int SSL_want(SSL *s) | ||
1995 | { | ||
1996 | return(s->rwstate); | ||
1997 | } | ||
1998 | |||
1999 | /*! | ||
2000 | * \brief Set the callback for generating temporary RSA keys. | ||
2001 | * \param ctx the SSL context. | ||
2002 | * \param cb the callback | ||
2003 | */ | ||
2004 | |||
2005 | #ifndef NO_RSA | ||
2006 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, | ||
2007 | int is_export, | ||
2008 | int keylength)) | ||
2009 | { | ||
2010 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)())cb); | ||
2011 | } | ||
2012 | |||
2013 | void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, | ||
2014 | int is_export, | ||
2015 | int keylength)) | ||
2016 | { | ||
2017 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)())cb); | ||
2018 | } | ||
2019 | #endif | ||
2020 | |||
2021 | #ifdef DOXYGEN | ||
2022 | /*! | ||
2023 | * \brief The RSA temporary key callback function. | ||
2024 | * \param ssl the SSL session. | ||
2025 | * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. | ||
2026 | * \param keylength if \c is_export is \c TRUE, then \c keylength is the size | ||
2027 | * of the required key in bits. | ||
2028 | * \return the temporary RSA key. | ||
2029 | * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback | ||
2030 | */ | ||
2031 | |||
2032 | RSA *cb(SSL *ssl,int is_export,int keylength) | ||
2033 | {} | ||
2034 | #endif | ||
2035 | |||
2036 | /*! | ||
2037 | * \brief Set the callback for generating temporary DH keys. | ||
2038 | * \param ctx the SSL context. | ||
2039 | * \param dh the callback | ||
2040 | */ | ||
2041 | |||
2042 | #ifndef NO_DH | ||
2043 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, | ||
2044 | int keylength)) | ||
2045 | { | ||
2046 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)())dh); | ||
2047 | } | ||
2048 | |||
2049 | void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, | ||
2050 | int keylength)) | ||
2051 | { | ||
2052 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)())dh); | ||
2053 | } | ||
2054 | #endif | ||
2055 | |||
2056 | #if defined(_WINDLL) && defined(WIN16) | ||
2057 | #include "../crypto/bio/bss_file.c" | ||
2058 | #endif | ||
2059 | |||
2060 | IMPLEMENT_STACK_OF(SSL_CIPHER) | ||
2061 | IMPLEMENT_STACK_OF(SSL_COMP) | ||