diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libssl/s3_srvr.c | 1683 |
1 files changed, 0 insertions, 1683 deletions
diff --git a/src/lib/libssl/s3_srvr.c b/src/lib/libssl/s3_srvr.c deleted file mode 100644 index e003d88357..0000000000 --- a/src/lib/libssl/s3_srvr.c +++ /dev/null | |||
@@ -1,1683 +0,0 @@ | |||
1 | /* ssl/s3_srvr.c */ | ||
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 | #define REUSE_CIPHER_BUG | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <openssl/buffer.h> | ||
63 | #include <openssl/rand.h> | ||
64 | #include <openssl/objects.h> | ||
65 | #include <openssl/md5.h> | ||
66 | #include <openssl/sha.h> | ||
67 | #include <openssl/evp.h> | ||
68 | #include <openssl/x509.h> | ||
69 | #include "ssl_locl.h" | ||
70 | |||
71 | static SSL_METHOD *ssl3_get_server_method(int ver); | ||
72 | static int ssl3_get_client_hello(SSL *s); | ||
73 | static int ssl3_send_server_hello(SSL *s); | ||
74 | static int ssl3_send_server_key_exchange(SSL *s); | ||
75 | static int ssl3_send_certificate_request(SSL *s); | ||
76 | static int ssl3_send_server_done(SSL *s); | ||
77 | static int ssl3_get_cert_verify(SSL *s); | ||
78 | static int ssl3_get_client_key_exchange(SSL *s); | ||
79 | static int ssl3_get_client_certificate(SSL *s); | ||
80 | static int ssl3_send_hello_request(SSL *s); | ||
81 | |||
82 | static SSL_METHOD *ssl3_get_server_method(int ver) | ||
83 | { | ||
84 | if (ver == SSL3_VERSION) | ||
85 | return(SSLv3_server_method()); | ||
86 | else | ||
87 | return(NULL); | ||
88 | } | ||
89 | |||
90 | SSL_METHOD *SSLv3_server_method(void) | ||
91 | { | ||
92 | static int init=1; | ||
93 | static SSL_METHOD SSLv3_server_data; | ||
94 | |||
95 | if (init) | ||
96 | { | ||
97 | memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(), | ||
98 | sizeof(SSL_METHOD)); | ||
99 | SSLv3_server_data.ssl_accept=ssl3_accept; | ||
100 | SSLv3_server_data.get_ssl_method=ssl3_get_server_method; | ||
101 | init=0; | ||
102 | } | ||
103 | return(&SSLv3_server_data); | ||
104 | } | ||
105 | |||
106 | int ssl3_accept(SSL *s) | ||
107 | { | ||
108 | BUF_MEM *buf; | ||
109 | unsigned long l,Time=time(NULL); | ||
110 | void (*cb)()=NULL; | ||
111 | long num1; | ||
112 | int ret= -1; | ||
113 | int new_state,state,skip=0; | ||
114 | |||
115 | RAND_seed(&Time,sizeof(Time)); | ||
116 | ERR_clear_error(); | ||
117 | clear_sys_error(); | ||
118 | |||
119 | if (s->info_callback != NULL) | ||
120 | cb=s->info_callback; | ||
121 | else if (s->ctx->info_callback != NULL) | ||
122 | cb=s->ctx->info_callback; | ||
123 | |||
124 | /* init things to blank */ | ||
125 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | ||
126 | s->in_handshake++; | ||
127 | |||
128 | if (s->cert == NULL) | ||
129 | { | ||
130 | SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); | ||
131 | return(-1); | ||
132 | } | ||
133 | |||
134 | for (;;) | ||
135 | { | ||
136 | state=s->state; | ||
137 | |||
138 | switch (s->state) | ||
139 | { | ||
140 | case SSL_ST_RENEGOTIATE: | ||
141 | s->new_session=1; | ||
142 | /* s->state=SSL_ST_ACCEPT; */ | ||
143 | |||
144 | case SSL_ST_BEFORE: | ||
145 | case SSL_ST_ACCEPT: | ||
146 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: | ||
147 | case SSL_ST_OK|SSL_ST_ACCEPT: | ||
148 | |||
149 | s->server=1; | ||
150 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); | ||
151 | |||
152 | if ((s->version>>8) != 3) | ||
153 | abort(); | ||
154 | /* s->version=SSL3_VERSION; */ | ||
155 | s->type=SSL_ST_ACCEPT; | ||
156 | |||
157 | if (s->init_buf == NULL) | ||
158 | { | ||
159 | if ((buf=BUF_MEM_new()) == NULL) | ||
160 | { | ||
161 | ret= -1; | ||
162 | goto end; | ||
163 | } | ||
164 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) | ||
165 | { | ||
166 | ret= -1; | ||
167 | goto end; | ||
168 | } | ||
169 | s->init_buf=buf; | ||
170 | } | ||
171 | |||
172 | if (!ssl3_setup_buffers(s)) | ||
173 | { | ||
174 | ret= -1; | ||
175 | goto end; | ||
176 | } | ||
177 | |||
178 | /* Ok, we now need to push on a buffering BIO so that | ||
179 | * the output is sent in a way that TCP likes :-) | ||
180 | */ | ||
181 | if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } | ||
182 | |||
183 | s->init_num=0; | ||
184 | |||
185 | if (s->state != SSL_ST_RENEGOTIATE) | ||
186 | { | ||
187 | s->state=SSL3_ST_SR_CLNT_HELLO_A; | ||
188 | ssl3_init_finished_mac(s); | ||
189 | s->ctx->stats.sess_accept++; | ||
190 | } | ||
191 | else | ||
192 | { | ||
193 | s->ctx->stats.sess_accept_renegotiate++; | ||
194 | s->state=SSL3_ST_SW_HELLO_REQ_A; | ||
195 | } | ||
196 | break; | ||
197 | |||
198 | case SSL3_ST_SW_HELLO_REQ_A: | ||
199 | case SSL3_ST_SW_HELLO_REQ_B: | ||
200 | |||
201 | s->shutdown=0; | ||
202 | ret=ssl3_send_hello_request(s); | ||
203 | if (ret <= 0) goto end; | ||
204 | s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; | ||
205 | s->state=SSL3_ST_SW_FLUSH; | ||
206 | s->init_num=0; | ||
207 | |||
208 | ssl3_init_finished_mac(s); | ||
209 | break; | ||
210 | |||
211 | case SSL3_ST_SW_HELLO_REQ_C: | ||
212 | s->state=SSL_ST_OK; | ||
213 | ret=1; | ||
214 | goto end; | ||
215 | /* break; */ | ||
216 | |||
217 | case SSL3_ST_SR_CLNT_HELLO_A: | ||
218 | case SSL3_ST_SR_CLNT_HELLO_B: | ||
219 | case SSL3_ST_SR_CLNT_HELLO_C: | ||
220 | |||
221 | s->shutdown=0; | ||
222 | ret=ssl3_get_client_hello(s); | ||
223 | if (ret <= 0) goto end; | ||
224 | s->state=SSL3_ST_SW_SRVR_HELLO_A; | ||
225 | s->init_num=0; | ||
226 | break; | ||
227 | |||
228 | case SSL3_ST_SW_SRVR_HELLO_A: | ||
229 | case SSL3_ST_SW_SRVR_HELLO_B: | ||
230 | ret=ssl3_send_server_hello(s); | ||
231 | if (ret <= 0) goto end; | ||
232 | |||
233 | if (s->hit) | ||
234 | s->state=SSL3_ST_SW_CHANGE_A; | ||
235 | else | ||
236 | s->state=SSL3_ST_SW_CERT_A; | ||
237 | s->init_num=0; | ||
238 | break; | ||
239 | |||
240 | case SSL3_ST_SW_CERT_A: | ||
241 | case SSL3_ST_SW_CERT_B: | ||
242 | /* Check if it is anon DH */ | ||
243 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | ||
244 | { | ||
245 | ret=ssl3_send_server_certificate(s); | ||
246 | if (ret <= 0) goto end; | ||
247 | } | ||
248 | else | ||
249 | skip=1; | ||
250 | s->state=SSL3_ST_SW_KEY_EXCH_A; | ||
251 | s->init_num=0; | ||
252 | break; | ||
253 | |||
254 | case SSL3_ST_SW_KEY_EXCH_A: | ||
255 | case SSL3_ST_SW_KEY_EXCH_B: | ||
256 | l=s->s3->tmp.new_cipher->algorithms; | ||
257 | |||
258 | /* clear this, it may get reset by | ||
259 | * send_server_key_exchange */ | ||
260 | if (s->options & SSL_OP_EPHEMERAL_RSA) | ||
261 | s->s3->tmp.use_rsa_tmp=1; | ||
262 | else | ||
263 | s->s3->tmp.use_rsa_tmp=0; | ||
264 | |||
265 | /* only send if a DH key exchange, fortezza or | ||
266 | * RSA but we have a sign only certificate */ | ||
267 | if (s->s3->tmp.use_rsa_tmp | ||
268 | || (l & (SSL_DH|SSL_kFZA)) | ||
269 | || ((l & SSL_kRSA) | ||
270 | && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL | ||
271 | || (SSL_IS_EXPORT(l) | ||
272 | && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_EXPORT_PKEYLENGTH(l) | ||
273 | ) | ||
274 | ) | ||
275 | ) | ||
276 | ) | ||
277 | { | ||
278 | ret=ssl3_send_server_key_exchange(s); | ||
279 | if (ret <= 0) goto end; | ||
280 | } | ||
281 | else | ||
282 | skip=1; | ||
283 | |||
284 | s->state=SSL3_ST_SW_CERT_REQ_A; | ||
285 | s->init_num=0; | ||
286 | break; | ||
287 | |||
288 | case SSL3_ST_SW_CERT_REQ_A: | ||
289 | case SSL3_ST_SW_CERT_REQ_B: | ||
290 | if (!(s->verify_mode & SSL_VERIFY_PEER) || | ||
291 | ((s->session->peer != NULL) && | ||
292 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE))) | ||
293 | { | ||
294 | /* no cert request */ | ||
295 | skip=1; | ||
296 | s->s3->tmp.cert_request=0; | ||
297 | s->state=SSL3_ST_SW_SRVR_DONE_A; | ||
298 | } | ||
299 | else | ||
300 | { | ||
301 | s->s3->tmp.cert_request=1; | ||
302 | ret=ssl3_send_certificate_request(s); | ||
303 | if (ret <= 0) goto end; | ||
304 | s->state=SSL3_ST_SW_SRVR_DONE_A; | ||
305 | s->init_num=0; | ||
306 | } | ||
307 | break; | ||
308 | |||
309 | case SSL3_ST_SW_SRVR_DONE_A: | ||
310 | case SSL3_ST_SW_SRVR_DONE_B: | ||
311 | ret=ssl3_send_server_done(s); | ||
312 | if (ret <= 0) goto end; | ||
313 | s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; | ||
314 | s->state=SSL3_ST_SW_FLUSH; | ||
315 | s->init_num=0; | ||
316 | break; | ||
317 | |||
318 | case SSL3_ST_SW_FLUSH: | ||
319 | /* number of bytes to be flushed */ | ||
320 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | ||
321 | if (num1 > 0) | ||
322 | { | ||
323 | s->rwstate=SSL_WRITING; | ||
324 | num1=BIO_flush(s->wbio); | ||
325 | if (num1 <= 0) { ret= -1; goto end; } | ||
326 | s->rwstate=SSL_NOTHING; | ||
327 | } | ||
328 | |||
329 | s->state=s->s3->tmp.next_state; | ||
330 | break; | ||
331 | |||
332 | case SSL3_ST_SR_CERT_A: | ||
333 | case SSL3_ST_SR_CERT_B: | ||
334 | /* could be sent for a DH cert, even if we | ||
335 | * have not asked for it :-) */ | ||
336 | ret=ssl3_get_client_certificate(s); | ||
337 | if (ret <= 0) goto end; | ||
338 | s->init_num=0; | ||
339 | s->state=SSL3_ST_SR_KEY_EXCH_A; | ||
340 | break; | ||
341 | |||
342 | case SSL3_ST_SR_KEY_EXCH_A: | ||
343 | case SSL3_ST_SR_KEY_EXCH_B: | ||
344 | ret=ssl3_get_client_key_exchange(s); | ||
345 | if (ret <= 0) goto end; | ||
346 | s->state=SSL3_ST_SR_CERT_VRFY_A; | ||
347 | s->init_num=0; | ||
348 | |||
349 | /* We need to get hashes here so if there is | ||
350 | * a client cert, it can be verified */ | ||
351 | s->method->ssl3_enc->cert_verify_mac(s, | ||
352 | &(s->s3->finish_dgst1), | ||
353 | &(s->s3->tmp.finish_md[0])); | ||
354 | s->method->ssl3_enc->cert_verify_mac(s, | ||
355 | &(s->s3->finish_dgst2), | ||
356 | &(s->s3->tmp.finish_md[MD5_DIGEST_LENGTH])); | ||
357 | |||
358 | break; | ||
359 | |||
360 | case SSL3_ST_SR_CERT_VRFY_A: | ||
361 | case SSL3_ST_SR_CERT_VRFY_B: | ||
362 | |||
363 | /* we should decide if we expected this one */ | ||
364 | ret=ssl3_get_cert_verify(s); | ||
365 | if (ret <= 0) goto end; | ||
366 | |||
367 | s->state=SSL3_ST_SR_FINISHED_A; | ||
368 | s->init_num=0; | ||
369 | break; | ||
370 | |||
371 | case SSL3_ST_SR_FINISHED_A: | ||
372 | case SSL3_ST_SR_FINISHED_B: | ||
373 | ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, | ||
374 | SSL3_ST_SR_FINISHED_B); | ||
375 | if (ret <= 0) goto end; | ||
376 | if (s->hit) | ||
377 | s->state=SSL_ST_OK; | ||
378 | else | ||
379 | s->state=SSL3_ST_SW_CHANGE_A; | ||
380 | s->init_num=0; | ||
381 | break; | ||
382 | |||
383 | case SSL3_ST_SW_CHANGE_A: | ||
384 | case SSL3_ST_SW_CHANGE_B: | ||
385 | |||
386 | s->session->cipher=s->s3->tmp.new_cipher; | ||
387 | if (!s->method->ssl3_enc->setup_key_block(s)) | ||
388 | { ret= -1; goto end; } | ||
389 | |||
390 | ret=ssl3_send_change_cipher_spec(s, | ||
391 | SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); | ||
392 | |||
393 | if (ret <= 0) goto end; | ||
394 | s->state=SSL3_ST_SW_FINISHED_A; | ||
395 | s->init_num=0; | ||
396 | |||
397 | if (!s->method->ssl3_enc->change_cipher_state(s, | ||
398 | SSL3_CHANGE_CIPHER_SERVER_WRITE)) | ||
399 | { | ||
400 | ret= -1; | ||
401 | goto end; | ||
402 | } | ||
403 | |||
404 | break; | ||
405 | |||
406 | case SSL3_ST_SW_FINISHED_A: | ||
407 | case SSL3_ST_SW_FINISHED_B: | ||
408 | ret=ssl3_send_finished(s, | ||
409 | SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, | ||
410 | s->method->ssl3_enc->server_finished, | ||
411 | s->method->ssl3_enc->server_finished_len); | ||
412 | if (ret <= 0) goto end; | ||
413 | s->state=SSL3_ST_SW_FLUSH; | ||
414 | if (s->hit) | ||
415 | s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; | ||
416 | else | ||
417 | s->s3->tmp.next_state=SSL_ST_OK; | ||
418 | s->init_num=0; | ||
419 | break; | ||
420 | |||
421 | case SSL_ST_OK: | ||
422 | /* clean a few things up */ | ||
423 | ssl3_cleanup_key_block(s); | ||
424 | |||
425 | BUF_MEM_free(s->init_buf); | ||
426 | s->init_buf=NULL; | ||
427 | |||
428 | /* remove buffering on output */ | ||
429 | ssl_free_wbio_buffer(s); | ||
430 | |||
431 | s->new_session=0; | ||
432 | s->init_num=0; | ||
433 | |||
434 | ssl_update_cache(s,SSL_SESS_CACHE_SERVER); | ||
435 | |||
436 | s->ctx->stats.sess_accept_good++; | ||
437 | /* s->server=1; */ | ||
438 | s->handshake_func=ssl3_accept; | ||
439 | ret=1; | ||
440 | |||
441 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | ||
442 | |||
443 | goto end; | ||
444 | /* break; */ | ||
445 | |||
446 | default: | ||
447 | SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); | ||
448 | ret= -1; | ||
449 | goto end; | ||
450 | /* break; */ | ||
451 | } | ||
452 | |||
453 | if (!s->s3->tmp.reuse_message && !skip) | ||
454 | { | ||
455 | if (s->debug) | ||
456 | { | ||
457 | if ((ret=BIO_flush(s->wbio)) <= 0) | ||
458 | goto end; | ||
459 | } | ||
460 | |||
461 | |||
462 | if ((cb != NULL) && (s->state != state)) | ||
463 | { | ||
464 | new_state=s->state; | ||
465 | s->state=state; | ||
466 | cb(s,SSL_CB_ACCEPT_LOOP,1); | ||
467 | s->state=new_state; | ||
468 | } | ||
469 | } | ||
470 | skip=0; | ||
471 | } | ||
472 | end: | ||
473 | /* BIO_flush(s->wbio); */ | ||
474 | |||
475 | if (cb != NULL) | ||
476 | cb(s,SSL_CB_ACCEPT_EXIT,ret); | ||
477 | s->in_handshake--; | ||
478 | return(ret); | ||
479 | } | ||
480 | |||
481 | static int ssl3_send_hello_request(SSL *s) | ||
482 | { | ||
483 | unsigned char *p; | ||
484 | |||
485 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) | ||
486 | { | ||
487 | p=(unsigned char *)s->init_buf->data; | ||
488 | *(p++)=SSL3_MT_CLIENT_REQUEST; | ||
489 | *(p++)=0; | ||
490 | *(p++)=0; | ||
491 | *(p++)=0; | ||
492 | |||
493 | s->state=SSL3_ST_SW_HELLO_REQ_B; | ||
494 | /* number of bytes to write */ | ||
495 | s->init_num=4; | ||
496 | s->init_off=0; | ||
497 | } | ||
498 | |||
499 | /* SSL3_ST_SW_HELLO_REQ_B */ | ||
500 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
501 | } | ||
502 | |||
503 | static int ssl3_get_client_hello(SSL *s) | ||
504 | { | ||
505 | int i,j,ok,al,ret= -1; | ||
506 | long n; | ||
507 | unsigned long id; | ||
508 | unsigned char *p,*d,*q; | ||
509 | SSL_CIPHER *c; | ||
510 | SSL_COMP *comp=NULL; | ||
511 | STACK_OF(SSL_CIPHER) *ciphers=NULL; | ||
512 | |||
513 | /* We do this so that we will respond with our native type. | ||
514 | * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, | ||
515 | * This down switching should be handled by a different method. | ||
516 | * If we are SSLv3, we will respond with SSLv3, even if prompted with | ||
517 | * TLSv1. | ||
518 | */ | ||
519 | if (s->state == SSL3_ST_SR_CLNT_HELLO_A) | ||
520 | { | ||
521 | s->first_packet=1; | ||
522 | s->state=SSL3_ST_SR_CLNT_HELLO_B; | ||
523 | } | ||
524 | n=ssl3_get_message(s, | ||
525 | SSL3_ST_SR_CLNT_HELLO_B, | ||
526 | SSL3_ST_SR_CLNT_HELLO_C, | ||
527 | SSL3_MT_CLIENT_HELLO, | ||
528 | SSL3_RT_MAX_PLAIN_LENGTH, | ||
529 | &ok); | ||
530 | |||
531 | if (!ok) return((int)n); | ||
532 | d=p=(unsigned char *)s->init_buf->data; | ||
533 | |||
534 | /* The version number has already been checked in ssl3_get_message. | ||
535 | * I a native TLSv1/SSLv3 method, the match must be correct except | ||
536 | * perhaps for the first message */ | ||
537 | /* s->client_version=(((int)p[0])<<8)|(int)p[1]; */ | ||
538 | p+=2; | ||
539 | |||
540 | /* load the client random */ | ||
541 | memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); | ||
542 | p+=SSL3_RANDOM_SIZE; | ||
543 | |||
544 | /* get the session-id */ | ||
545 | j= *(p++); | ||
546 | |||
547 | s->hit=0; | ||
548 | if (j == 0) | ||
549 | { | ||
550 | if (!ssl_get_new_session(s,1)) | ||
551 | goto err; | ||
552 | } | ||
553 | else | ||
554 | { | ||
555 | i=ssl_get_prev_session(s,p,j); | ||
556 | if (i == 1) | ||
557 | { /* previous session */ | ||
558 | s->hit=1; | ||
559 | } | ||
560 | else if (i == -1) | ||
561 | goto err; | ||
562 | else /* i == 0 */ | ||
563 | { | ||
564 | if (!ssl_get_new_session(s,1)) | ||
565 | goto err; | ||
566 | } | ||
567 | } | ||
568 | |||
569 | p+=j; | ||
570 | n2s(p,i); | ||
571 | if ((i == 0) && (j != 0)) | ||
572 | { | ||
573 | /* we need a cipher if we are not resuming a session */ | ||
574 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
575 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); | ||
576 | goto f_err; | ||
577 | } | ||
578 | if ((i+p) > (d+n)) | ||
579 | { | ||
580 | /* not enough data */ | ||
581 | al=SSL_AD_DECODE_ERROR; | ||
582 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); | ||
583 | goto f_err; | ||
584 | } | ||
585 | if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) | ||
586 | == NULL)) | ||
587 | { | ||
588 | goto err; | ||
589 | } | ||
590 | p+=i; | ||
591 | |||
592 | /* If it is a hit, check that the cipher is in the list */ | ||
593 | if ((s->hit) && (i > 0)) | ||
594 | { | ||
595 | j=0; | ||
596 | id=s->session->cipher->id; | ||
597 | |||
598 | #ifdef CIPHER_DEBUG | ||
599 | printf("client sent %d ciphers\n",sk_num(ciphers)); | ||
600 | #endif | ||
601 | for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) | ||
602 | { | ||
603 | c=sk_SSL_CIPHER_value(ciphers,i); | ||
604 | #ifdef CIPHER_DEBUG | ||
605 | printf("client [%2d of %2d]:%s\n", | ||
606 | i,sk_num(ciphers),SSL_CIPHER_get_name(c)); | ||
607 | #endif | ||
608 | if (c->id == id) | ||
609 | { | ||
610 | j=1; | ||
611 | break; | ||
612 | } | ||
613 | } | ||
614 | if (j == 0) | ||
615 | { | ||
616 | if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) | ||
617 | { | ||
618 | /* Very bad for multi-threading.... */ | ||
619 | s->session->cipher=sk_SSL_CIPHER_value(ciphers, | ||
620 | 0); | ||
621 | } | ||
622 | else | ||
623 | { | ||
624 | /* we need to have the cipher in the cipher | ||
625 | * list if we are asked to reuse it */ | ||
626 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
627 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING); | ||
628 | goto f_err; | ||
629 | } | ||
630 | } | ||
631 | } | ||
632 | |||
633 | /* compression */ | ||
634 | i= *(p++); | ||
635 | q=p; | ||
636 | for (j=0; j<i; j++) | ||
637 | { | ||
638 | if (p[j] == 0) break; | ||
639 | } | ||
640 | |||
641 | p+=i; | ||
642 | if (j >= i) | ||
643 | { | ||
644 | /* no compress */ | ||
645 | al=SSL_AD_DECODE_ERROR; | ||
646 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); | ||
647 | goto f_err; | ||
648 | } | ||
649 | |||
650 | /* Worst case, we will use the NULL compression, but if we have other | ||
651 | * options, we will now look for them. We have i-1 compression | ||
652 | * algorithms from the client, starting at q. */ | ||
653 | s->s3->tmp.new_compression=NULL; | ||
654 | if (s->ctx->comp_methods != NULL) | ||
655 | { /* See if we have a match */ | ||
656 | int m,nn,o,v,done=0; | ||
657 | |||
658 | nn=sk_SSL_COMP_num(s->ctx->comp_methods); | ||
659 | for (m=0; m<nn; m++) | ||
660 | { | ||
661 | comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); | ||
662 | v=comp->id; | ||
663 | for (o=0; o<i; o++) | ||
664 | { | ||
665 | if (v == q[o]) | ||
666 | { | ||
667 | done=1; | ||
668 | break; | ||
669 | } | ||
670 | } | ||
671 | if (done) break; | ||
672 | } | ||
673 | if (done) | ||
674 | s->s3->tmp.new_compression=comp; | ||
675 | else | ||
676 | comp=NULL; | ||
677 | } | ||
678 | |||
679 | /* TLS does not mind if there is extra stuff */ | ||
680 | if (s->version == SSL3_VERSION) | ||
681 | { | ||
682 | if (p > (d+n)) | ||
683 | { | ||
684 | /* wrong number of bytes, | ||
685 | * there could be more to follow */ | ||
686 | al=SSL_AD_DECODE_ERROR; | ||
687 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); | ||
688 | goto f_err; | ||
689 | } | ||
690 | } | ||
691 | |||
692 | /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must | ||
693 | * pick a cipher */ | ||
694 | |||
695 | if (!s->hit) | ||
696 | { | ||
697 | s->session->compress_meth=(comp == NULL)?0:comp->id; | ||
698 | if (s->session->ciphers != NULL) | ||
699 | sk_SSL_CIPHER_free(s->session->ciphers); | ||
700 | s->session->ciphers=ciphers; | ||
701 | if (ciphers == NULL) | ||
702 | { | ||
703 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
704 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); | ||
705 | goto f_err; | ||
706 | } | ||
707 | ciphers=NULL; | ||
708 | c=ssl3_choose_cipher(s,s->session->ciphers, | ||
709 | ssl_get_ciphers_by_id(s)); | ||
710 | |||
711 | if (c == NULL) | ||
712 | { | ||
713 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
714 | SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); | ||
715 | goto f_err; | ||
716 | } | ||
717 | s->s3->tmp.new_cipher=c; | ||
718 | } | ||
719 | else | ||
720 | { | ||
721 | /* Session-id reuse */ | ||
722 | #ifdef REUSE_CIPHER_BUG | ||
723 | STACK_OF(SSL_CIPHER) *sk; | ||
724 | SSL_CIPHER *nc=NULL; | ||
725 | SSL_CIPHER *ec=NULL; | ||
726 | |||
727 | if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) | ||
728 | { | ||
729 | sk=s->session->ciphers; | ||
730 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) | ||
731 | { | ||
732 | c=sk_SSL_CIPHER_value(sk,i); | ||
733 | if (c->algorithms & SSL_eNULL) | ||
734 | nc=c; | ||
735 | if (SSL_C_IS_EXPORT(c)) | ||
736 | ec=c; | ||
737 | } | ||
738 | if (nc != NULL) | ||
739 | s->s3->tmp.new_cipher=nc; | ||
740 | else if (ec != NULL) | ||
741 | s->s3->tmp.new_cipher=ec; | ||
742 | else | ||
743 | s->s3->tmp.new_cipher=s->session->cipher; | ||
744 | } | ||
745 | else | ||
746 | #endif | ||
747 | s->s3->tmp.new_cipher=s->session->cipher; | ||
748 | } | ||
749 | |||
750 | /* we now have the following setup. | ||
751 | * client_random | ||
752 | * cipher_list - our prefered list of ciphers | ||
753 | * ciphers - the clients prefered list of ciphers | ||
754 | * compression - basically ignored right now | ||
755 | * ssl version is set - sslv3 | ||
756 | * s->session - The ssl session has been setup. | ||
757 | * s->hit - sesson reuse flag | ||
758 | * s->tmp.new_cipher - the new cipher to use. | ||
759 | */ | ||
760 | |||
761 | ret=1; | ||
762 | if (0) | ||
763 | { | ||
764 | f_err: | ||
765 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
766 | } | ||
767 | err: | ||
768 | if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); | ||
769 | return(ret); | ||
770 | } | ||
771 | |||
772 | static int ssl3_send_server_hello(SSL *s) | ||
773 | { | ||
774 | unsigned char *buf; | ||
775 | unsigned char *p,*d; | ||
776 | int i,sl; | ||
777 | unsigned long l,Time; | ||
778 | |||
779 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) | ||
780 | { | ||
781 | buf=(unsigned char *)s->init_buf->data; | ||
782 | p=s->s3->server_random; | ||
783 | Time=time(NULL); /* Time */ | ||
784 | l2n(Time,p); | ||
785 | RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); | ||
786 | /* Do the message type and length last */ | ||
787 | d=p= &(buf[4]); | ||
788 | |||
789 | *(p++)=s->version>>8; | ||
790 | *(p++)=s->version&0xff; | ||
791 | |||
792 | /* Random stuff */ | ||
793 | memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); | ||
794 | p+=SSL3_RANDOM_SIZE; | ||
795 | |||
796 | /* now in theory we have 3 options to sending back the | ||
797 | * session id. If it is a re-use, we send back the | ||
798 | * old session-id, if it is a new session, we send | ||
799 | * back the new session-id or we send back a 0 length | ||
800 | * session-id if we want it to be single use. | ||
801 | * Currently I will not implement the '0' length session-id | ||
802 | * 12-Jan-98 - I'll now support the '0' length stuff. | ||
803 | */ | ||
804 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) | ||
805 | s->session->session_id_length=0; | ||
806 | |||
807 | sl=s->session->session_id_length; | ||
808 | *(p++)=sl; | ||
809 | memcpy(p,s->session->session_id,sl); | ||
810 | p+=sl; | ||
811 | |||
812 | /* put the cipher */ | ||
813 | i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); | ||
814 | p+=i; | ||
815 | |||
816 | /* put the compression method */ | ||
817 | if (s->s3->tmp.new_compression == NULL) | ||
818 | *(p++)=0; | ||
819 | else | ||
820 | *(p++)=s->s3->tmp.new_compression->id; | ||
821 | |||
822 | /* do the header */ | ||
823 | l=(p-d); | ||
824 | d=buf; | ||
825 | *(d++)=SSL3_MT_SERVER_HELLO; | ||
826 | l2n3(l,d); | ||
827 | |||
828 | s->state=SSL3_ST_CW_CLNT_HELLO_B; | ||
829 | /* number of bytes to write */ | ||
830 | s->init_num=p-buf; | ||
831 | s->init_off=0; | ||
832 | } | ||
833 | |||
834 | /* SSL3_ST_CW_CLNT_HELLO_B */ | ||
835 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
836 | } | ||
837 | |||
838 | static int ssl3_send_server_done(SSL *s) | ||
839 | { | ||
840 | unsigned char *p; | ||
841 | |||
842 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) | ||
843 | { | ||
844 | p=(unsigned char *)s->init_buf->data; | ||
845 | |||
846 | /* do the header */ | ||
847 | *(p++)=SSL3_MT_SERVER_DONE; | ||
848 | *(p++)=0; | ||
849 | *(p++)=0; | ||
850 | *(p++)=0; | ||
851 | |||
852 | s->state=SSL3_ST_SW_SRVR_DONE_B; | ||
853 | /* number of bytes to write */ | ||
854 | s->init_num=4; | ||
855 | s->init_off=0; | ||
856 | } | ||
857 | |||
858 | /* SSL3_ST_CW_CLNT_HELLO_B */ | ||
859 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
860 | } | ||
861 | |||
862 | static int ssl3_send_server_key_exchange(SSL *s) | ||
863 | { | ||
864 | #ifndef NO_RSA | ||
865 | unsigned char *q; | ||
866 | int j,num; | ||
867 | RSA *rsa; | ||
868 | unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | ||
869 | #endif | ||
870 | #ifndef NO_DH | ||
871 | DH *dh,*dhp; | ||
872 | #endif | ||
873 | EVP_PKEY *pkey; | ||
874 | unsigned char *p,*d; | ||
875 | int al,i; | ||
876 | unsigned long type; | ||
877 | int n; | ||
878 | CERT *cert; | ||
879 | BIGNUM *r[4]; | ||
880 | int nr[4],kn; | ||
881 | BUF_MEM *buf; | ||
882 | EVP_MD_CTX md_ctx; | ||
883 | |||
884 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) | ||
885 | { | ||
886 | type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; | ||
887 | cert=s->cert; | ||
888 | |||
889 | buf=s->init_buf; | ||
890 | |||
891 | r[0]=r[1]=r[2]=r[3]=NULL; | ||
892 | n=0; | ||
893 | #ifndef NO_RSA | ||
894 | if (type & SSL_kRSA) | ||
895 | { | ||
896 | rsa=cert->rsa_tmp; | ||
897 | if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) | ||
898 | { | ||
899 | rsa=s->cert->rsa_tmp_cb(s, | ||
900 | SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | ||
901 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
902 | CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA); | ||
903 | cert->rsa_tmp=rsa; | ||
904 | } | ||
905 | if (rsa == NULL) | ||
906 | { | ||
907 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
908 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); | ||
909 | goto f_err; | ||
910 | } | ||
911 | r[0]=rsa->n; | ||
912 | r[1]=rsa->e; | ||
913 | s->s3->tmp.use_rsa_tmp=1; | ||
914 | } | ||
915 | else | ||
916 | #endif | ||
917 | #ifndef NO_DH | ||
918 | if (type & SSL_kEDH) | ||
919 | { | ||
920 | dhp=cert->dh_tmp; | ||
921 | if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) | ||
922 | dhp=s->cert->dh_tmp_cb(s, | ||
923 | !SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), | ||
924 | SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); | ||
925 | if (dhp == NULL) | ||
926 | { | ||
927 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
928 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); | ||
929 | goto f_err; | ||
930 | } | ||
931 | if ((dh=DHparams_dup(dhp)) == NULL) | ||
932 | { | ||
933 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); | ||
934 | goto err; | ||
935 | } | ||
936 | |||
937 | s->s3->tmp.dh=dh; | ||
938 | if ((dhp->pub_key == NULL || | ||
939 | dhp->priv_key == NULL || | ||
940 | (s->options & SSL_OP_SINGLE_DH_USE))) | ||
941 | { | ||
942 | if(!DH_generate_key(dh)) | ||
943 | { | ||
944 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, | ||
945 | ERR_R_DH_LIB); | ||
946 | goto err; | ||
947 | } | ||
948 | } | ||
949 | else | ||
950 | { | ||
951 | dh->pub_key=BN_dup(dhp->pub_key); | ||
952 | dh->priv_key=BN_dup(dhp->priv_key); | ||
953 | if ((dh->pub_key == NULL) || | ||
954 | (dh->priv_key == NULL)) | ||
955 | { | ||
956 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); | ||
957 | goto err; | ||
958 | } | ||
959 | } | ||
960 | r[0]=dh->p; | ||
961 | r[1]=dh->g; | ||
962 | r[2]=dh->pub_key; | ||
963 | } | ||
964 | else | ||
965 | #endif | ||
966 | { | ||
967 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
968 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | ||
969 | goto f_err; | ||
970 | } | ||
971 | for (i=0; r[i] != NULL; i++) | ||
972 | { | ||
973 | nr[i]=BN_num_bytes(r[i]); | ||
974 | n+=2+nr[i]; | ||
975 | } | ||
976 | |||
977 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | ||
978 | { | ||
979 | if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) | ||
980 | == NULL) | ||
981 | { | ||
982 | al=SSL_AD_DECODE_ERROR; | ||
983 | goto f_err; | ||
984 | } | ||
985 | kn=EVP_PKEY_size(pkey); | ||
986 | } | ||
987 | else | ||
988 | { | ||
989 | pkey=NULL; | ||
990 | kn=0; | ||
991 | } | ||
992 | |||
993 | if (!BUF_MEM_grow(buf,n+4+kn)) | ||
994 | { | ||
995 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); | ||
996 | goto err; | ||
997 | } | ||
998 | d=(unsigned char *)s->init_buf->data; | ||
999 | p= &(d[4]); | ||
1000 | |||
1001 | for (i=0; r[i] != NULL; i++) | ||
1002 | { | ||
1003 | s2n(nr[i],p); | ||
1004 | BN_bn2bin(r[i],p); | ||
1005 | p+=nr[i]; | ||
1006 | } | ||
1007 | |||
1008 | /* not anonymous */ | ||
1009 | if (pkey != NULL) | ||
1010 | { | ||
1011 | /* n is the length of the params, they start at &(d[4]) | ||
1012 | * and p points to the space at the end. */ | ||
1013 | #ifndef NO_RSA | ||
1014 | if (pkey->type == EVP_PKEY_RSA) | ||
1015 | { | ||
1016 | q=md_buf; | ||
1017 | j=0; | ||
1018 | for (num=2; num > 0; num--) | ||
1019 | { | ||
1020 | EVP_DigestInit(&md_ctx,(num == 2) | ||
1021 | ?s->ctx->md5:s->ctx->sha1); | ||
1022 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); | ||
1023 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | ||
1024 | EVP_DigestUpdate(&md_ctx,&(d[4]),n); | ||
1025 | EVP_DigestFinal(&md_ctx,q, | ||
1026 | (unsigned int *)&i); | ||
1027 | q+=i; | ||
1028 | j+=i; | ||
1029 | } | ||
1030 | i=RSA_private_encrypt(j,md_buf,&(p[2]), | ||
1031 | pkey->pkey.rsa,RSA_PKCS1_PADDING); | ||
1032 | if (i <= 0) | ||
1033 | { | ||
1034 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); | ||
1035 | goto err; | ||
1036 | } | ||
1037 | s2n(i,p); | ||
1038 | n+=i+2; | ||
1039 | } | ||
1040 | else | ||
1041 | #endif | ||
1042 | #if !defined(NO_DSA) | ||
1043 | if (pkey->type == EVP_PKEY_DSA) | ||
1044 | { | ||
1045 | /* lets do DSS */ | ||
1046 | EVP_SignInit(&md_ctx,EVP_dss1()); | ||
1047 | EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); | ||
1048 | EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | ||
1049 | EVP_SignUpdate(&md_ctx,&(d[4]),n); | ||
1050 | if (!EVP_SignFinal(&md_ctx,&(p[2]), | ||
1051 | (unsigned int *)&i,pkey)) | ||
1052 | { | ||
1053 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); | ||
1054 | goto err; | ||
1055 | } | ||
1056 | s2n(i,p); | ||
1057 | n+=i+2; | ||
1058 | } | ||
1059 | else | ||
1060 | #endif | ||
1061 | { | ||
1062 | /* Is this error check actually needed? */ | ||
1063 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1064 | SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); | ||
1065 | goto f_err; | ||
1066 | } | ||
1067 | } | ||
1068 | |||
1069 | *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; | ||
1070 | l2n3(n,d); | ||
1071 | |||
1072 | /* we should now have things packed up, so lets send | ||
1073 | * it off */ | ||
1074 | s->init_num=n+4; | ||
1075 | s->init_off=0; | ||
1076 | } | ||
1077 | |||
1078 | /* SSL3_ST_SW_KEY_EXCH_B */ | ||
1079 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
1080 | f_err: | ||
1081 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
1082 | err: | ||
1083 | return(-1); | ||
1084 | } | ||
1085 | |||
1086 | static int ssl3_send_certificate_request(SSL *s) | ||
1087 | { | ||
1088 | unsigned char *p,*d; | ||
1089 | int i,j,nl,off,n; | ||
1090 | STACK_OF(X509_NAME) *sk=NULL; | ||
1091 | X509_NAME *name; | ||
1092 | BUF_MEM *buf; | ||
1093 | |||
1094 | if (s->state == SSL3_ST_SW_CERT_REQ_A) | ||
1095 | { | ||
1096 | buf=s->init_buf; | ||
1097 | |||
1098 | d=p=(unsigned char *)&(buf->data[4]); | ||
1099 | |||
1100 | /* get the list of acceptable cert types */ | ||
1101 | p++; | ||
1102 | n=ssl3_get_req_cert_type(s,p); | ||
1103 | d[0]=n; | ||
1104 | p+=n; | ||
1105 | n++; | ||
1106 | |||
1107 | off=n; | ||
1108 | p+=2; | ||
1109 | n+=2; | ||
1110 | |||
1111 | sk=SSL_get_client_CA_list(s); | ||
1112 | nl=0; | ||
1113 | if (sk != NULL) | ||
1114 | { | ||
1115 | for (i=0; i<sk_X509_NAME_num(sk); i++) | ||
1116 | { | ||
1117 | name=sk_X509_NAME_value(sk,i); | ||
1118 | j=i2d_X509_NAME(name,NULL); | ||
1119 | if (!BUF_MEM_grow(buf,4+n+j+2)) | ||
1120 | { | ||
1121 | SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); | ||
1122 | goto err; | ||
1123 | } | ||
1124 | p=(unsigned char *)&(buf->data[4+n]); | ||
1125 | if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) | ||
1126 | { | ||
1127 | s2n(j,p); | ||
1128 | i2d_X509_NAME(name,&p); | ||
1129 | n+=2+j; | ||
1130 | nl+=2+j; | ||
1131 | } | ||
1132 | else | ||
1133 | { | ||
1134 | d=p; | ||
1135 | i2d_X509_NAME(name,&p); | ||
1136 | j-=2; s2n(j,d); j+=2; | ||
1137 | n+=j; | ||
1138 | nl+=j; | ||
1139 | } | ||
1140 | } | ||
1141 | } | ||
1142 | /* else no CA names */ | ||
1143 | p=(unsigned char *)&(buf->data[4+off]); | ||
1144 | s2n(nl,p); | ||
1145 | |||
1146 | d=(unsigned char *)buf->data; | ||
1147 | *(d++)=SSL3_MT_CERTIFICATE_REQUEST; | ||
1148 | l2n3(n,d); | ||
1149 | |||
1150 | /* we should now have things packed up, so lets send | ||
1151 | * it off */ | ||
1152 | |||
1153 | s->init_num=n+4; | ||
1154 | s->init_off=0; | ||
1155 | } | ||
1156 | |||
1157 | /* SSL3_ST_SW_CERT_REQ_B */ | ||
1158 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
1159 | err: | ||
1160 | return(-1); | ||
1161 | } | ||
1162 | |||
1163 | static int ssl3_get_client_key_exchange(SSL *s) | ||
1164 | { | ||
1165 | int i,al,ok; | ||
1166 | long n; | ||
1167 | unsigned long l; | ||
1168 | unsigned char *p; | ||
1169 | #ifndef NO_RSA | ||
1170 | RSA *rsa=NULL; | ||
1171 | EVP_PKEY *pkey=NULL; | ||
1172 | #endif | ||
1173 | #ifndef NO_DH | ||
1174 | BIGNUM *pub=NULL; | ||
1175 | DH *dh_srvr; | ||
1176 | #endif | ||
1177 | |||
1178 | n=ssl3_get_message(s, | ||
1179 | SSL3_ST_SR_KEY_EXCH_A, | ||
1180 | SSL3_ST_SR_KEY_EXCH_B, | ||
1181 | SSL3_MT_CLIENT_KEY_EXCHANGE, | ||
1182 | 400, /* ???? */ | ||
1183 | &ok); | ||
1184 | |||
1185 | if (!ok) return((int)n); | ||
1186 | p=(unsigned char *)s->init_buf->data; | ||
1187 | |||
1188 | l=s->s3->tmp.new_cipher->algorithms; | ||
1189 | |||
1190 | #ifndef NO_RSA | ||
1191 | if (l & SSL_kRSA) | ||
1192 | { | ||
1193 | /* FIX THIS UP EAY EAY EAY EAY */ | ||
1194 | if (s->s3->tmp.use_rsa_tmp) | ||
1195 | { | ||
1196 | if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) | ||
1197 | rsa=s->cert->rsa_tmp; | ||
1198 | /* Don't do a callback because rsa_tmp should | ||
1199 | * be sent already */ | ||
1200 | if (rsa == NULL) | ||
1201 | { | ||
1202 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1203 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); | ||
1204 | goto f_err; | ||
1205 | |||
1206 | } | ||
1207 | } | ||
1208 | else | ||
1209 | { | ||
1210 | pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; | ||
1211 | if ( (pkey == NULL) || | ||
1212 | (pkey->type != EVP_PKEY_RSA) || | ||
1213 | (pkey->pkey.rsa == NULL)) | ||
1214 | { | ||
1215 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1216 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); | ||
1217 | goto f_err; | ||
1218 | } | ||
1219 | rsa=pkey->pkey.rsa; | ||
1220 | } | ||
1221 | |||
1222 | /* TLS */ | ||
1223 | if (s->version > SSL3_VERSION) | ||
1224 | { | ||
1225 | n2s(p,i); | ||
1226 | if (n != i+2) | ||
1227 | { | ||
1228 | if (!(s->options & SSL_OP_TLS_D5_BUG)) | ||
1229 | { | ||
1230 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); | ||
1231 | goto err; | ||
1232 | } | ||
1233 | else | ||
1234 | p-=2; | ||
1235 | } | ||
1236 | else | ||
1237 | n=i; | ||
1238 | } | ||
1239 | |||
1240 | i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); | ||
1241 | |||
1242 | #if 1 | ||
1243 | /* If a bad decrypt, use a random master key */ | ||
1244 | if ((i != SSL_MAX_MASTER_KEY_LENGTH) || | ||
1245 | ((p[0] != (s->client_version>>8)) || | ||
1246 | (p[1] != (s->client_version & 0xff)))) | ||
1247 | { | ||
1248 | int bad=1; | ||
1249 | |||
1250 | if ((i == SSL_MAX_MASTER_KEY_LENGTH) && | ||
1251 | (p[0] == (s->version>>8)) && | ||
1252 | (p[1] == 0)) | ||
1253 | { | ||
1254 | if (s->options & SSL_OP_TLS_ROLLBACK_BUG) | ||
1255 | bad=0; | ||
1256 | } | ||
1257 | if (bad) | ||
1258 | { | ||
1259 | p[0]=(s->version>>8); | ||
1260 | p[1]=(s->version & 0xff); | ||
1261 | RAND_bytes(&(p[2]),SSL_MAX_MASTER_KEY_LENGTH-2); | ||
1262 | i=SSL_MAX_MASTER_KEY_LENGTH; | ||
1263 | } | ||
1264 | /* else, an SSLeay bug, ssl only server, tls client */ | ||
1265 | } | ||
1266 | #else | ||
1267 | if (i != SSL_MAX_MASTER_KEY_LENGTH) | ||
1268 | { | ||
1269 | al=SSL_AD_DECODE_ERROR; | ||
1270 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); | ||
1271 | goto f_err; | ||
1272 | } | ||
1273 | |||
1274 | if ((p[0] != (s->version>>8)) || (p[1] != (s->version & 0xff))) | ||
1275 | { | ||
1276 | al=SSL_AD_DECODE_ERROR; | ||
1277 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); | ||
1278 | goto f_err; | ||
1279 | } | ||
1280 | #endif | ||
1281 | |||
1282 | s->session->master_key_length= | ||
1283 | s->method->ssl3_enc->generate_master_secret(s, | ||
1284 | s->session->master_key, | ||
1285 | p,i); | ||
1286 | memset(p,0,i); | ||
1287 | } | ||
1288 | else | ||
1289 | #endif | ||
1290 | #ifndef NO_DH | ||
1291 | if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | ||
1292 | { | ||
1293 | n2s(p,i); | ||
1294 | if (n != i+2) | ||
1295 | { | ||
1296 | if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) | ||
1297 | { | ||
1298 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); | ||
1299 | goto err; | ||
1300 | } | ||
1301 | else | ||
1302 | { | ||
1303 | p-=2; | ||
1304 | i=(int)n; | ||
1305 | } | ||
1306 | } | ||
1307 | |||
1308 | if (n == 0L) /* the parameters are in the cert */ | ||
1309 | { | ||
1310 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1311 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); | ||
1312 | goto f_err; | ||
1313 | } | ||
1314 | else | ||
1315 | { | ||
1316 | if (s->s3->tmp.dh == NULL) | ||
1317 | { | ||
1318 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1319 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); | ||
1320 | goto f_err; | ||
1321 | } | ||
1322 | else | ||
1323 | dh_srvr=s->s3->tmp.dh; | ||
1324 | } | ||
1325 | |||
1326 | pub=BN_bin2bn(p,i,NULL); | ||
1327 | if (pub == NULL) | ||
1328 | { | ||
1329 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); | ||
1330 | goto err; | ||
1331 | } | ||
1332 | |||
1333 | i=DH_compute_key(p,pub,dh_srvr); | ||
1334 | |||
1335 | if (i <= 0) | ||
1336 | { | ||
1337 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); | ||
1338 | goto err; | ||
1339 | } | ||
1340 | |||
1341 | DH_free(s->s3->tmp.dh); | ||
1342 | s->s3->tmp.dh=NULL; | ||
1343 | |||
1344 | BN_clear_free(pub); | ||
1345 | pub=NULL; | ||
1346 | s->session->master_key_length= | ||
1347 | s->method->ssl3_enc->generate_master_secret(s, | ||
1348 | s->session->master_key,p,i); | ||
1349 | } | ||
1350 | else | ||
1351 | #endif | ||
1352 | { | ||
1353 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1354 | SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE); | ||
1355 | goto f_err; | ||
1356 | } | ||
1357 | |||
1358 | return(1); | ||
1359 | f_err: | ||
1360 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
1361 | #if !defined(NO_DH) || !defined(NO_RSA) | ||
1362 | err: | ||
1363 | #endif | ||
1364 | return(-1); | ||
1365 | } | ||
1366 | |||
1367 | static int ssl3_get_cert_verify(SSL *s) | ||
1368 | { | ||
1369 | EVP_PKEY *pkey=NULL; | ||
1370 | unsigned char *p; | ||
1371 | int al,ok,ret=0; | ||
1372 | long n; | ||
1373 | int type=0,i,j; | ||
1374 | X509 *peer; | ||
1375 | |||
1376 | n=ssl3_get_message(s, | ||
1377 | SSL3_ST_SR_CERT_VRFY_A, | ||
1378 | SSL3_ST_SR_CERT_VRFY_B, | ||
1379 | -1, | ||
1380 | 512, /* 512? */ | ||
1381 | &ok); | ||
1382 | |||
1383 | if (!ok) return((int)n); | ||
1384 | |||
1385 | if (s->session->peer != NULL) | ||
1386 | { | ||
1387 | peer=s->session->peer; | ||
1388 | pkey=X509_get_pubkey(peer); | ||
1389 | type=X509_certificate_type(peer,pkey); | ||
1390 | } | ||
1391 | else | ||
1392 | { | ||
1393 | peer=NULL; | ||
1394 | pkey=NULL; | ||
1395 | } | ||
1396 | |||
1397 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) | ||
1398 | { | ||
1399 | s->s3->tmp.reuse_message=1; | ||
1400 | if ((peer != NULL) && (type | EVP_PKT_SIGN)) | ||
1401 | { | ||
1402 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
1403 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); | ||
1404 | goto f_err; | ||
1405 | } | ||
1406 | ret=1; | ||
1407 | goto end; | ||
1408 | } | ||
1409 | |||
1410 | if (peer == NULL) | ||
1411 | { | ||
1412 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); | ||
1413 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
1414 | goto f_err; | ||
1415 | } | ||
1416 | |||
1417 | if (!(type & EVP_PKT_SIGN)) | ||
1418 | { | ||
1419 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); | ||
1420 | al=SSL_AD_ILLEGAL_PARAMETER; | ||
1421 | goto f_err; | ||
1422 | } | ||
1423 | |||
1424 | if (s->s3->change_cipher_spec) | ||
1425 | { | ||
1426 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); | ||
1427 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
1428 | goto f_err; | ||
1429 | } | ||
1430 | |||
1431 | /* we now have a signature that we need to verify */ | ||
1432 | p=(unsigned char *)s->init_buf->data; | ||
1433 | n2s(p,i); | ||
1434 | n-=2; | ||
1435 | if (i > n) | ||
1436 | { | ||
1437 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); | ||
1438 | al=SSL_AD_DECODE_ERROR; | ||
1439 | goto f_err; | ||
1440 | } | ||
1441 | |||
1442 | j=EVP_PKEY_size(pkey); | ||
1443 | if ((i > j) || (n > j) || (n <= 0)) | ||
1444 | { | ||
1445 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); | ||
1446 | al=SSL_AD_DECODE_ERROR; | ||
1447 | goto f_err; | ||
1448 | } | ||
1449 | |||
1450 | #ifndef NO_RSA | ||
1451 | if (pkey->type == EVP_PKEY_RSA) | ||
1452 | { | ||
1453 | i=RSA_public_decrypt(i,p,p,pkey->pkey.rsa,RSA_PKCS1_PADDING); | ||
1454 | if (i < 0) | ||
1455 | { | ||
1456 | al=SSL_AD_DECRYPT_ERROR; | ||
1457 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); | ||
1458 | goto f_err; | ||
1459 | } | ||
1460 | if ((i != (MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH)) || | ||
1461 | memcmp(&(s->s3->tmp.finish_md[0]),p, | ||
1462 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH)) | ||
1463 | { | ||
1464 | al=SSL_AD_DECRYPT_ERROR; | ||
1465 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); | ||
1466 | goto f_err; | ||
1467 | } | ||
1468 | } | ||
1469 | else | ||
1470 | #endif | ||
1471 | #ifndef NO_DSA | ||
1472 | if (pkey->type == EVP_PKEY_DSA) | ||
1473 | { | ||
1474 | j=DSA_verify(pkey->save_type, | ||
1475 | &(s->s3->tmp.finish_md[MD5_DIGEST_LENGTH]), | ||
1476 | SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); | ||
1477 | if (j <= 0) | ||
1478 | { | ||
1479 | /* bad signature */ | ||
1480 | al=SSL_AD_DECRYPT_ERROR; | ||
1481 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); | ||
1482 | goto f_err; | ||
1483 | } | ||
1484 | } | ||
1485 | else | ||
1486 | #endif | ||
1487 | { | ||
1488 | SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR); | ||
1489 | al=SSL_AD_UNSUPPORTED_CERTIFICATE; | ||
1490 | goto f_err; | ||
1491 | } | ||
1492 | |||
1493 | |||
1494 | ret=1; | ||
1495 | if (0) | ||
1496 | { | ||
1497 | f_err: | ||
1498 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
1499 | } | ||
1500 | end: | ||
1501 | EVP_PKEY_free(pkey); | ||
1502 | return(ret); | ||
1503 | } | ||
1504 | |||
1505 | static int ssl3_get_client_certificate(SSL *s) | ||
1506 | { | ||
1507 | int i,ok,al,ret= -1; | ||
1508 | X509 *x=NULL; | ||
1509 | unsigned long l,nc,llen,n; | ||
1510 | unsigned char *p,*d,*q; | ||
1511 | STACK_OF(X509) *sk=NULL; | ||
1512 | |||
1513 | n=ssl3_get_message(s, | ||
1514 | SSL3_ST_SR_CERT_A, | ||
1515 | SSL3_ST_SR_CERT_B, | ||
1516 | -1, | ||
1517 | #if defined(MSDOS) && !defined(WIN32) | ||
1518 | 1024*30, /* 30k max cert list :-) */ | ||
1519 | #else | ||
1520 | 1024*100, /* 100k max cert list :-) */ | ||
1521 | #endif | ||
1522 | &ok); | ||
1523 | |||
1524 | if (!ok) return((int)n); | ||
1525 | |||
1526 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) | ||
1527 | { | ||
1528 | if ( (s->verify_mode & SSL_VERIFY_PEER) && | ||
1529 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | ||
1530 | { | ||
1531 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | ||
1532 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1533 | goto f_err; | ||
1534 | } | ||
1535 | /* If tls asked for a client cert we must return a 0 list */ | ||
1536 | if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) | ||
1537 | { | ||
1538 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); | ||
1539 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
1540 | goto f_err; | ||
1541 | } | ||
1542 | s->s3->tmp.reuse_message=1; | ||
1543 | return(1); | ||
1544 | } | ||
1545 | |||
1546 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) | ||
1547 | { | ||
1548 | al=SSL_AD_UNEXPECTED_MESSAGE; | ||
1549 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); | ||
1550 | goto f_err; | ||
1551 | } | ||
1552 | d=p=(unsigned char *)s->init_buf->data; | ||
1553 | |||
1554 | if ((sk=sk_X509_new_null()) == NULL) | ||
1555 | { | ||
1556 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); | ||
1557 | goto err; | ||
1558 | } | ||
1559 | |||
1560 | n2l3(p,llen); | ||
1561 | if (llen+3 != n) | ||
1562 | { | ||
1563 | al=SSL_AD_DECODE_ERROR; | ||
1564 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); | ||
1565 | goto f_err; | ||
1566 | } | ||
1567 | for (nc=0; nc<llen; ) | ||
1568 | { | ||
1569 | n2l3(p,l); | ||
1570 | if ((l+nc+3) > llen) | ||
1571 | { | ||
1572 | al=SSL_AD_DECODE_ERROR; | ||
1573 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); | ||
1574 | goto f_err; | ||
1575 | } | ||
1576 | |||
1577 | q=p; | ||
1578 | x=d2i_X509(NULL,&p,l); | ||
1579 | if (x == NULL) | ||
1580 | { | ||
1581 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); | ||
1582 | goto err; | ||
1583 | } | ||
1584 | if (p != (q+l)) | ||
1585 | { | ||
1586 | al=SSL_AD_DECODE_ERROR; | ||
1587 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); | ||
1588 | goto f_err; | ||
1589 | } | ||
1590 | if (!sk_X509_push(sk,x)) | ||
1591 | { | ||
1592 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); | ||
1593 | goto err; | ||
1594 | } | ||
1595 | x=NULL; | ||
1596 | nc+=l+3; | ||
1597 | } | ||
1598 | |||
1599 | if (sk_X509_num(sk) <= 0) | ||
1600 | { | ||
1601 | /* TLS does not mind 0 certs returned */ | ||
1602 | if (s->version == SSL3_VERSION) | ||
1603 | { | ||
1604 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1605 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED); | ||
1606 | goto f_err; | ||
1607 | } | ||
1608 | /* Fail for TLS only if we required a certificate */ | ||
1609 | else if ((s->verify_mode & SSL_VERIFY_PEER) && | ||
1610 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) | ||
1611 | { | ||
1612 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | ||
1613 | al=SSL_AD_HANDSHAKE_FAILURE; | ||
1614 | goto f_err; | ||
1615 | } | ||
1616 | } | ||
1617 | else | ||
1618 | { | ||
1619 | i=ssl_verify_cert_chain(s,sk); | ||
1620 | if (!i) | ||
1621 | { | ||
1622 | al=ssl_verify_alarm_type(s->verify_result); | ||
1623 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); | ||
1624 | goto f_err; | ||
1625 | } | ||
1626 | } | ||
1627 | |||
1628 | if (s->session->peer != NULL) /* This should not be needed */ | ||
1629 | X509_free(s->session->peer); | ||
1630 | s->session->peer=sk_X509_shift(sk); | ||
1631 | |||
1632 | /* With the current implementation, sess_cert will always be NULL | ||
1633 | * when we arrive here. */ | ||
1634 | if (s->session->sess_cert == NULL) | ||
1635 | { | ||
1636 | s->session->sess_cert = ssl_sess_cert_new(); | ||
1637 | if (s->session->sess_cert == NULL) | ||
1638 | { | ||
1639 | SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); | ||
1640 | goto err; | ||
1641 | } | ||
1642 | } | ||
1643 | if (s->session->sess_cert->cert_chain != NULL) | ||
1644 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); | ||
1645 | s->session->sess_cert->cert_chain=sk; | ||
1646 | |||
1647 | sk=NULL; | ||
1648 | |||
1649 | ret=1; | ||
1650 | if (0) | ||
1651 | { | ||
1652 | f_err: | ||
1653 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | ||
1654 | } | ||
1655 | err: | ||
1656 | if (x != NULL) X509_free(x); | ||
1657 | if (sk != NULL) sk_X509_pop_free(sk,X509_free); | ||
1658 | return(ret); | ||
1659 | } | ||
1660 | |||
1661 | int ssl3_send_server_certificate(SSL *s) | ||
1662 | { | ||
1663 | unsigned long l; | ||
1664 | X509 *x; | ||
1665 | |||
1666 | if (s->state == SSL3_ST_SW_CERT_A) | ||
1667 | { | ||
1668 | x=ssl_get_server_send_cert(s); | ||
1669 | if (x == NULL) | ||
1670 | { | ||
1671 | SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR); | ||
1672 | return(0); | ||
1673 | } | ||
1674 | |||
1675 | l=ssl3_output_cert_chain(s,x); | ||
1676 | s->state=SSL3_ST_SW_CERT_B; | ||
1677 | s->init_num=(int)l; | ||
1678 | s->init_off=0; | ||
1679 | } | ||
1680 | |||
1681 | /* SSL3_ST_SW_CERT_B */ | ||
1682 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | ||
1683 | } | ||