]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* ssl/s3_clnt.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
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 | #include <stdio.h> | |
ec577822 BM |
60 | #include <openssl/buffer.h> |
61 | #include <openssl/rand.h> | |
62 | #include <openssl/objects.h> | |
d02f751c UM |
63 | #include <openssl/md5.h> |
64 | #include <openssl/sha.h> | |
ec577822 | 65 | #include <openssl/evp.h> |
d02b48c6 RE |
66 | #include "ssl_locl.h" |
67 | ||
f9b3bff6 RL |
68 | #ifndef NO_KRB5 |
69 | #include "kssl.h" | |
70 | #endif | |
71 | ||
9b3086fe | 72 | static SSL_METHOD *ssl3_get_client_method(int ver); |
d02b48c6 RE |
73 | static int ssl3_client_hello(SSL *s); |
74 | static int ssl3_get_server_hello(SSL *s); | |
75 | static int ssl3_get_certificate_request(SSL *s); | |
ccd86b68 | 76 | static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b); |
d02b48c6 RE |
77 | static int ssl3_get_server_done(SSL *s); |
78 | static int ssl3_send_client_verify(SSL *s); | |
79 | static int ssl3_send_client_certificate(SSL *s); | |
80 | static int ssl3_send_client_key_exchange(SSL *s); | |
81 | static int ssl3_get_key_exchange(SSL *s); | |
82 | static int ssl3_get_server_certificate(SSL *s); | |
83 | static int ssl3_check_cert_and_algorithm(SSL *s); | |
6b691a5c | 84 | static SSL_METHOD *ssl3_get_client_method(int ver) |
d02b48c6 | 85 | { |
58964a49 | 86 | if (ver == SSL3_VERSION) |
d02b48c6 RE |
87 | return(SSLv3_client_method()); |
88 | else | |
89 | return(NULL); | |
90 | } | |
91 | ||
6b691a5c | 92 | SSL_METHOD *SSLv3_client_method(void) |
d02b48c6 RE |
93 | { |
94 | static int init=1; | |
95 | static SSL_METHOD SSLv3_client_data; | |
96 | ||
97 | if (init) | |
98 | { | |
99 | init=0; | |
100 | memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(), | |
101 | sizeof(SSL_METHOD)); | |
102 | SSLv3_client_data.ssl_connect=ssl3_connect; | |
103 | SSLv3_client_data.get_ssl_method=ssl3_get_client_method; | |
104 | } | |
105 | return(&SSLv3_client_data); | |
106 | } | |
107 | ||
6b691a5c | 108 | int ssl3_connect(SSL *s) |
d02b48c6 RE |
109 | { |
110 | BUF_MEM *buf; | |
111 | unsigned long Time=time(NULL),l; | |
112 | long num1; | |
113 | void (*cb)()=NULL; | |
114 | int ret= -1; | |
d02b48c6 RE |
115 | int new_state,state,skip=0;; |
116 | ||
eb952088 | 117 | RAND_add(&Time,sizeof(Time),0); |
d02b48c6 | 118 | ERR_clear_error(); |
58964a49 | 119 | clear_sys_error(); |
d02b48c6 RE |
120 | |
121 | if (s->info_callback != NULL) | |
122 | cb=s->info_callback; | |
123 | else if (s->ctx->info_callback != NULL) | |
124 | cb=s->ctx->info_callback; | |
125 | ||
126 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); | |
127 | s->in_handshake++; | |
128 | ||
129 | for (;;) | |
130 | { | |
131 | state=s->state; | |
132 | ||
133 | switch(s->state) | |
134 | { | |
135 | case SSL_ST_RENEGOTIATE: | |
136 | s->new_session=1; | |
137 | s->state=SSL_ST_CONNECT; | |
413c4f45 | 138 | s->ctx->stats.sess_connect_renegotiate++; |
d02b48c6 RE |
139 | /* break */ |
140 | case SSL_ST_BEFORE: | |
141 | case SSL_ST_CONNECT: | |
142 | case SSL_ST_BEFORE|SSL_ST_CONNECT: | |
143 | case SSL_ST_OK|SSL_ST_CONNECT: | |
144 | ||
413c4f45 | 145 | s->server=0; |
d02b48c6 RE |
146 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
147 | ||
58964a49 | 148 | if ((s->version & 0xff00 ) != 0x0300) |
bbb8de09 BM |
149 | { |
150 | SSLerr(SSL_F_SSL3_CONNECT, SSL_R_INTERNAL_ERROR); | |
151 | ret = -1; | |
152 | goto end; | |
153 | } | |
154 | ||
58964a49 | 155 | /* s->version=SSL3_VERSION; */ |
d02b48c6 RE |
156 | s->type=SSL_ST_CONNECT; |
157 | ||
158 | if (s->init_buf == NULL) | |
159 | { | |
160 | if ((buf=BUF_MEM_new()) == NULL) | |
161 | { | |
162 | ret= -1; | |
163 | goto end; | |
164 | } | |
165 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) | |
166 | { | |
167 | ret= -1; | |
168 | goto end; | |
169 | } | |
170 | s->init_buf=buf; | |
171 | } | |
172 | ||
173 | if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } | |
174 | ||
175 | /* setup buffing BIO */ | |
58964a49 | 176 | if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } |
d02b48c6 RE |
177 | |
178 | /* don't push the buffering BIO quite yet */ | |
179 | ||
180 | ssl3_init_finished_mac(s); | |
181 | ||
182 | s->state=SSL3_ST_CW_CLNT_HELLO_A; | |
413c4f45 | 183 | s->ctx->stats.sess_connect++; |
d02b48c6 RE |
184 | s->init_num=0; |
185 | break; | |
186 | ||
187 | case SSL3_ST_CW_CLNT_HELLO_A: | |
188 | case SSL3_ST_CW_CLNT_HELLO_B: | |
189 | ||
190 | s->shutdown=0; | |
191 | ret=ssl3_client_hello(s); | |
192 | if (ret <= 0) goto end; | |
193 | s->state=SSL3_ST_CR_SRVR_HELLO_A; | |
194 | s->init_num=0; | |
195 | ||
196 | /* turn on buffering for the next lot of output */ | |
58964a49 RE |
197 | if (s->bbio != s->wbio) |
198 | s->wbio=BIO_push(s->bbio,s->wbio); | |
d02b48c6 RE |
199 | |
200 | break; | |
201 | ||
202 | case SSL3_ST_CR_SRVR_HELLO_A: | |
203 | case SSL3_ST_CR_SRVR_HELLO_B: | |
204 | ret=ssl3_get_server_hello(s); | |
205 | if (ret <= 0) goto end; | |
206 | if (s->hit) | |
207 | s->state=SSL3_ST_CR_FINISHED_A; | |
208 | else | |
209 | s->state=SSL3_ST_CR_CERT_A; | |
210 | s->init_num=0; | |
211 | break; | |
212 | ||
213 | case SSL3_ST_CR_CERT_A: | |
214 | case SSL3_ST_CR_CERT_B: | |
215 | /* Check if it is anon DH */ | |
216 | if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) | |
217 | { | |
218 | ret=ssl3_get_server_certificate(s); | |
219 | if (ret <= 0) goto end; | |
220 | } | |
221 | else | |
222 | skip=1; | |
223 | s->state=SSL3_ST_CR_KEY_EXCH_A; | |
224 | s->init_num=0; | |
225 | break; | |
226 | ||
227 | case SSL3_ST_CR_KEY_EXCH_A: | |
228 | case SSL3_ST_CR_KEY_EXCH_B: | |
229 | ret=ssl3_get_key_exchange(s); | |
230 | if (ret <= 0) goto end; | |
231 | s->state=SSL3_ST_CR_CERT_REQ_A; | |
232 | s->init_num=0; | |
233 | ||
234 | /* at this point we check that we have the | |
235 | * required stuff from the server */ | |
236 | if (!ssl3_check_cert_and_algorithm(s)) | |
237 | { | |
238 | ret= -1; | |
239 | goto end; | |
240 | } | |
241 | break; | |
242 | ||
243 | case SSL3_ST_CR_CERT_REQ_A: | |
244 | case SSL3_ST_CR_CERT_REQ_B: | |
245 | ret=ssl3_get_certificate_request(s); | |
246 | if (ret <= 0) goto end; | |
247 | s->state=SSL3_ST_CR_SRVR_DONE_A; | |
248 | s->init_num=0; | |
249 | break; | |
250 | ||
251 | case SSL3_ST_CR_SRVR_DONE_A: | |
252 | case SSL3_ST_CR_SRVR_DONE_B: | |
253 | ret=ssl3_get_server_done(s); | |
254 | if (ret <= 0) goto end; | |
255 | if (s->s3->tmp.cert_req) | |
256 | s->state=SSL3_ST_CW_CERT_A; | |
257 | else | |
258 | s->state=SSL3_ST_CW_KEY_EXCH_A; | |
259 | s->init_num=0; | |
260 | ||
261 | break; | |
262 | ||
263 | case SSL3_ST_CW_CERT_A: | |
264 | case SSL3_ST_CW_CERT_B: | |
265 | case SSL3_ST_CW_CERT_C: | |
95d29597 | 266 | case SSL3_ST_CW_CERT_D: |
d02b48c6 RE |
267 | ret=ssl3_send_client_certificate(s); |
268 | if (ret <= 0) goto end; | |
269 | s->state=SSL3_ST_CW_KEY_EXCH_A; | |
270 | s->init_num=0; | |
271 | break; | |
272 | ||
273 | case SSL3_ST_CW_KEY_EXCH_A: | |
274 | case SSL3_ST_CW_KEY_EXCH_B: | |
275 | ret=ssl3_send_client_key_exchange(s); | |
276 | if (ret <= 0) goto end; | |
277 | l=s->s3->tmp.new_cipher->algorithms; | |
278 | /* EAY EAY EAY need to check for DH fix cert | |
279 | * sent back */ | |
58964a49 RE |
280 | /* For TLS, cert_req is set to 2, so a cert chain |
281 | * of nothing is sent, but no verify packet is sent */ | |
282 | if (s->s3->tmp.cert_req == 1) | |
d02b48c6 RE |
283 | { |
284 | s->state=SSL3_ST_CW_CERT_VRFY_A; | |
285 | } | |
286 | else | |
287 | { | |
288 | s->state=SSL3_ST_CW_CHANGE_A; | |
289 | s->s3->change_cipher_spec=0; | |
290 | } | |
291 | ||
292 | s->init_num=0; | |
293 | break; | |
294 | ||
295 | case SSL3_ST_CW_CERT_VRFY_A: | |
296 | case SSL3_ST_CW_CERT_VRFY_B: | |
297 | ret=ssl3_send_client_verify(s); | |
298 | if (ret <= 0) goto end; | |
299 | s->state=SSL3_ST_CW_CHANGE_A; | |
300 | s->init_num=0; | |
301 | s->s3->change_cipher_spec=0; | |
302 | break; | |
303 | ||
304 | case SSL3_ST_CW_CHANGE_A: | |
305 | case SSL3_ST_CW_CHANGE_B: | |
306 | ret=ssl3_send_change_cipher_spec(s, | |
307 | SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); | |
308 | if (ret <= 0) goto end; | |
309 | s->state=SSL3_ST_CW_FINISHED_A; | |
310 | s->init_num=0; | |
311 | ||
312 | s->session->cipher=s->s3->tmp.new_cipher; | |
413c4f45 MC |
313 | if (s->s3->tmp.new_compression == NULL) |
314 | s->session->compress_meth=0; | |
315 | else | |
316 | s->session->compress_meth= | |
317 | s->s3->tmp.new_compression->id; | |
58964a49 | 318 | if (!s->method->ssl3_enc->setup_key_block(s)) |
d02b48c6 RE |
319 | { |
320 | ret= -1; | |
321 | goto end; | |
322 | } | |
323 | ||
58964a49 | 324 | if (!s->method->ssl3_enc->change_cipher_state(s, |
d02b48c6 RE |
325 | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
326 | { | |
327 | ret= -1; | |
328 | goto end; | |
329 | } | |
330 | ||
331 | break; | |
332 | ||
333 | case SSL3_ST_CW_FINISHED_A: | |
334 | case SSL3_ST_CW_FINISHED_B: | |
335 | ret=ssl3_send_finished(s, | |
336 | SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, | |
c44f7540 BM |
337 | s->method->ssl3_enc->client_finished_label, |
338 | s->method->ssl3_enc->client_finished_label_len); | |
d02b48c6 RE |
339 | if (ret <= 0) goto end; |
340 | s->state=SSL3_ST_CW_FLUSH; | |
341 | ||
342 | /* clear flags */ | |
343 | s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; | |
344 | if (s->hit) | |
345 | { | |
346 | s->s3->tmp.next_state=SSL_ST_OK; | |
347 | if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) | |
348 | { | |
349 | s->state=SSL_ST_OK; | |
350 | s->s3->flags|=SSL3_FLAGS_POP_BUFFER; | |
351 | s->s3->delay_buf_pop_ret=0; | |
352 | } | |
353 | } | |
354 | else | |
355 | { | |
356 | s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; | |
357 | } | |
358 | s->init_num=0; | |
359 | break; | |
360 | ||
361 | case SSL3_ST_CR_FINISHED_A: | |
362 | case SSL3_ST_CR_FINISHED_B: | |
363 | ||
364 | ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, | |
58964a49 | 365 | SSL3_ST_CR_FINISHED_B); |
d02b48c6 RE |
366 | if (ret <= 0) goto end; |
367 | ||
368 | if (s->hit) | |
369 | s->state=SSL3_ST_CW_CHANGE_A; | |
370 | else | |
371 | s->state=SSL_ST_OK; | |
372 | s->init_num=0; | |
373 | break; | |
374 | ||
375 | case SSL3_ST_CW_FLUSH: | |
376 | /* number of bytes to be flushed */ | |
377 | num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); | |
378 | if (num1 > 0) | |
379 | { | |
380 | s->rwstate=SSL_WRITING; | |
381 | num1=BIO_flush(s->wbio); | |
382 | if (num1 <= 0) { ret= -1; goto end; } | |
383 | s->rwstate=SSL_NOTHING; | |
384 | } | |
385 | ||
386 | s->state=s->s3->tmp.next_state; | |
387 | break; | |
388 | ||
389 | case SSL_ST_OK: | |
390 | /* clean a few things up */ | |
391 | ssl3_cleanup_key_block(s); | |
392 | ||
413c4f45 | 393 | if (s->init_buf != NULL) |
d02b48c6 | 394 | { |
413c4f45 MC |
395 | BUF_MEM_free(s->init_buf); |
396 | s->init_buf=NULL; | |
d02b48c6 | 397 | } |
413c4f45 MC |
398 | |
399 | /* If we are not 'joining' the last two packets, | |
400 | * remove the buffering now */ | |
401 | if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) | |
402 | ssl_free_wbio_buffer(s); | |
403 | /* else do it later in ssl3_write */ | |
d02b48c6 RE |
404 | |
405 | s->init_num=0; | |
406 | s->new_session=0; | |
407 | ||
408 | ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); | |
413c4f45 | 409 | if (s->hit) s->ctx->stats.sess_hit++; |
d02b48c6 RE |
410 | |
411 | ret=1; | |
412 | /* s->server=0; */ | |
413 | s->handshake_func=ssl3_connect; | |
413c4f45 | 414 | s->ctx->stats.sess_connect_good++; |
d02b48c6 RE |
415 | |
416 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); | |
417 | ||
418 | goto end; | |
dfeab068 | 419 | /* break; */ |
d02b48c6 RE |
420 | |
421 | default: | |
422 | SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE); | |
423 | ret= -1; | |
424 | goto end; | |
425 | /* break; */ | |
426 | } | |
427 | ||
428 | /* did we do anything */ | |
429 | if (!s->s3->tmp.reuse_message && !skip) | |
430 | { | |
58964a49 RE |
431 | if (s->debug) |
432 | { | |
433 | if ((ret=BIO_flush(s->wbio)) <= 0) | |
434 | goto end; | |
435 | } | |
d02b48c6 RE |
436 | |
437 | if ((cb != NULL) && (s->state != state)) | |
438 | { | |
439 | new_state=s->state; | |
440 | s->state=state; | |
441 | cb(s,SSL_CB_CONNECT_LOOP,1); | |
442 | s->state=new_state; | |
443 | } | |
444 | } | |
445 | skip=0; | |
446 | } | |
447 | end: | |
448 | if (cb != NULL) | |
449 | cb(s,SSL_CB_CONNECT_EXIT,ret); | |
450 | s->in_handshake--; | |
451 | return(ret); | |
452 | } | |
453 | ||
454 | ||
6b691a5c | 455 | static int ssl3_client_hello(SSL *s) |
d02b48c6 RE |
456 | { |
457 | unsigned char *buf; | |
458 | unsigned char *p,*d; | |
413c4f45 | 459 | int i,j; |
d02b48c6 | 460 | unsigned long Time,l; |
413c4f45 | 461 | SSL_COMP *comp; |
d02b48c6 RE |
462 | |
463 | buf=(unsigned char *)s->init_buf->data; | |
464 | if (s->state == SSL3_ST_CW_CLNT_HELLO_A) | |
465 | { | |
466 | if ((s->session == NULL) || | |
dfeab068 RE |
467 | (s->session->ssl_version != s->version) || |
468 | (s->session->not_resumable)) | |
d02b48c6 RE |
469 | { |
470 | if (!ssl_get_new_session(s,0)) | |
471 | goto err; | |
472 | } | |
473 | /* else use the pre-loaded session */ | |
474 | ||
475 | p=s->s3->client_random; | |
476 | Time=time(NULL); /* Time */ | |
477 | l2n(Time,p); | |
e7f97e2d | 478 | RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); |
d02b48c6 RE |
479 | |
480 | /* Do the message type and length last */ | |
481 | d=p= &(buf[4]); | |
482 | ||
58964a49 RE |
483 | *(p++)=s->version>>8; |
484 | *(p++)=s->version&0xff; | |
413c4f45 | 485 | s->client_version=s->version; |
d02b48c6 RE |
486 | |
487 | /* Random stuff */ | |
488 | memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); | |
489 | p+=SSL3_RANDOM_SIZE; | |
490 | ||
491 | /* Session ID */ | |
492 | if (s->new_session) | |
493 | i=0; | |
494 | else | |
495 | i=s->session->session_id_length; | |
496 | *(p++)=i; | |
497 | if (i != 0) | |
498 | { | |
499 | memcpy(p,s->session->session_id,i); | |
500 | p+=i; | |
501 | } | |
502 | ||
503 | /* Ciphers supported */ | |
504 | i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2])); | |
505 | if (i == 0) | |
506 | { | |
507 | SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); | |
508 | goto err; | |
509 | } | |
510 | s2n(i,p); | |
511 | p+=i; | |
512 | ||
dfeab068 | 513 | /* COMPRESSION */ |
413c4f45 MC |
514 | if (s->ctx->comp_methods == NULL) |
515 | j=0; | |
516 | else | |
f73e07cf | 517 | j=sk_SSL_COMP_num(s->ctx->comp_methods); |
413c4f45 MC |
518 | *(p++)=1+j; |
519 | for (i=0; i<j; i++) | |
520 | { | |
f73e07cf | 521 | comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); |
413c4f45 MC |
522 | *(p++)=comp->id; |
523 | } | |
524 | *(p++)=0; /* Add the NULL method */ | |
d02b48c6 RE |
525 | |
526 | l=(p-d); | |
527 | d=buf; | |
528 | *(d++)=SSL3_MT_CLIENT_HELLO; | |
529 | l2n3(l,d); | |
530 | ||
531 | s->state=SSL3_ST_CW_CLNT_HELLO_B; | |
532 | /* number of bytes to write */ | |
533 | s->init_num=p-buf; | |
534 | s->init_off=0; | |
535 | } | |
536 | ||
537 | /* SSL3_ST_CW_CLNT_HELLO_B */ | |
538 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
539 | err: | |
540 | return(-1); | |
541 | } | |
542 | ||
6b691a5c | 543 | static int ssl3_get_server_hello(SSL *s) |
d02b48c6 | 544 | { |
f73e07cf | 545 | STACK_OF(SSL_CIPHER) *sk; |
d02b48c6 RE |
546 | SSL_CIPHER *c; |
547 | unsigned char *p,*d; | |
548 | int i,al,ok; | |
549 | unsigned int j; | |
550 | long n; | |
413c4f45 | 551 | SSL_COMP *comp; |
d02b48c6 RE |
552 | |
553 | n=ssl3_get_message(s, | |
554 | SSL3_ST_CR_SRVR_HELLO_A, | |
555 | SSL3_ST_CR_SRVR_HELLO_B, | |
556 | SSL3_MT_SERVER_HELLO, | |
557 | 300, /* ?? */ | |
558 | &ok); | |
559 | ||
560 | if (!ok) return((int)n); | |
561 | d=p=(unsigned char *)s->init_buf->data; | |
562 | ||
58964a49 | 563 | if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) |
d02b48c6 RE |
564 | { |
565 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION); | |
58964a49 RE |
566 | s->version=(s->version&0xff00)|p[1]; |
567 | al=SSL_AD_PROTOCOL_VERSION; | |
568 | goto f_err; | |
d02b48c6 RE |
569 | } |
570 | p+=2; | |
571 | ||
572 | /* load the server hello data */ | |
573 | /* load the server random */ | |
574 | memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE); | |
575 | p+=SSL3_RANDOM_SIZE; | |
576 | ||
577 | /* get the session-id */ | |
578 | j= *(p++); | |
579 | ||
580 | if ((j != 0) && (j != SSL3_SESSION_ID_SIZE)) | |
581 | { | |
582 | /* SSLref returns 16 :-( */ | |
583 | if (j < SSL2_SSL_SESSION_ID_LENGTH) | |
584 | { | |
58964a49 | 585 | al=SSL_AD_ILLEGAL_PARAMETER; |
d02b48c6 RE |
586 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT); |
587 | goto f_err; | |
588 | } | |
589 | } | |
b4cadc6e BL |
590 | if (j != 0 && j == s->session->session_id_length |
591 | && memcmp(p,s->session->session_id,j) == 0) | |
592 | { | |
593 | if(s->sid_ctx_length != s->session->sid_ctx_length | |
594 | || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length)) | |
595 | { | |
596 | al=SSL_AD_ILLEGAL_PARAMETER; | |
597 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); | |
598 | goto f_err; | |
599 | } | |
600 | s->hit=1; | |
601 | } | |
58964a49 | 602 | else /* a miss or crap from the other end */ |
d02b48c6 | 603 | { |
58964a49 RE |
604 | /* If we were trying for session-id reuse, make a new |
605 | * SSL_SESSION so we don't stuff up other people */ | |
d02b48c6 | 606 | s->hit=0; |
58964a49 RE |
607 | if (s->session->session_id_length > 0) |
608 | { | |
609 | if (!ssl_get_new_session(s,0)) | |
610 | { | |
611 | al=SSL_AD_INTERNAL_ERROR; | |
612 | goto f_err; | |
613 | } | |
614 | } | |
615 | s->session->session_id_length=j; | |
616 | memcpy(s->session->session_id,p,j); /* j could be 0 */ | |
d02b48c6 RE |
617 | } |
618 | p+=j; | |
619 | c=ssl_get_cipher_by_char(s,p); | |
620 | if (c == NULL) | |
621 | { | |
622 | /* unknown cipher */ | |
58964a49 | 623 | al=SSL_AD_ILLEGAL_PARAMETER; |
d02b48c6 RE |
624 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED); |
625 | goto f_err; | |
626 | } | |
627 | p+=ssl_put_cipher_by_char(s,NULL,NULL); | |
628 | ||
629 | sk=ssl_get_ciphers_by_id(s); | |
f73e07cf | 630 | i=sk_SSL_CIPHER_find(sk,c); |
d02b48c6 RE |
631 | if (i < 0) |
632 | { | |
633 | /* we did not say we would use this cipher */ | |
58964a49 | 634 | al=SSL_AD_ILLEGAL_PARAMETER; |
d02b48c6 RE |
635 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED); |
636 | goto f_err; | |
637 | } | |
638 | ||
639 | if (s->hit && (s->session->cipher != c)) | |
640 | { | |
58964a49 | 641 | if (!(s->options & |
d02b48c6 RE |
642 | SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) |
643 | { | |
58964a49 | 644 | al=SSL_AD_ILLEGAL_PARAMETER; |
d02b48c6 RE |
645 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
646 | goto f_err; | |
647 | } | |
648 | } | |
649 | s->s3->tmp.new_cipher=c; | |
650 | ||
651 | /* lets get the compression algorithm */ | |
dfeab068 | 652 | /* COMPRESSION */ |
d02b48c6 | 653 | j= *(p++); |
413c4f45 MC |
654 | if (j == 0) |
655 | comp=NULL; | |
656 | else | |
657 | comp=ssl3_comp_find(s->ctx->comp_methods,j); | |
658 | ||
659 | if ((j != 0) && (comp == NULL)) | |
d02b48c6 | 660 | { |
58964a49 | 661 | al=SSL_AD_ILLEGAL_PARAMETER; |
d02b48c6 RE |
662 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
663 | goto f_err; | |
664 | } | |
413c4f45 MC |
665 | else |
666 | { | |
667 | s->s3->tmp.new_compression=comp; | |
668 | } | |
d02b48c6 RE |
669 | |
670 | if (p != (d+n)) | |
671 | { | |
672 | /* wrong packet length */ | |
58964a49 | 673 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
674 | SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH); |
675 | goto err; | |
676 | } | |
677 | ||
678 | return(1); | |
679 | f_err: | |
680 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
681 | err: | |
682 | return(-1); | |
683 | } | |
684 | ||
6b691a5c | 685 | static int ssl3_get_server_certificate(SSL *s) |
d02b48c6 RE |
686 | { |
687 | int al,i,ok,ret= -1; | |
688 | unsigned long n,nc,llen,l; | |
689 | X509 *x=NULL; | |
690 | unsigned char *p,*d,*q; | |
f73e07cf | 691 | STACK_OF(X509) *sk=NULL; |
b56bce4f | 692 | SESS_CERT *sc; |
d02b48c6 | 693 | EVP_PKEY *pkey=NULL; |
f9b3bff6 | 694 | int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */ |
d02b48c6 RE |
695 | |
696 | n=ssl3_get_message(s, | |
697 | SSL3_ST_CR_CERT_A, | |
698 | SSL3_ST_CR_CERT_B, | |
699 | -1, | |
700 | #if defined(MSDOS) && !defined(WIN32) | |
701 | 1024*30, /* 30k max cert list :-) */ | |
702 | #else | |
703 | 1024*100, /* 100k max cert list :-) */ | |
704 | #endif | |
705 | &ok); | |
706 | ||
707 | if (!ok) return((int)n); | |
708 | ||
709 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) | |
710 | { | |
711 | s->s3->tmp.reuse_message=1; | |
712 | return(1); | |
713 | } | |
714 | ||
715 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) | |
716 | { | |
58964a49 | 717 | al=SSL_AD_UNEXPECTED_MESSAGE; |
d02b48c6 RE |
718 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE); |
719 | goto f_err; | |
720 | } | |
721 | d=p=(unsigned char *)s->init_buf->data; | |
722 | ||
f73e07cf | 723 | if ((sk=sk_X509_new_null()) == NULL) |
d02b48c6 RE |
724 | { |
725 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); | |
726 | goto err; | |
727 | } | |
728 | ||
729 | n2l3(p,llen); | |
730 | if (llen+3 != n) | |
731 | { | |
58964a49 | 732 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
733 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH); |
734 | goto f_err; | |
735 | } | |
736 | for (nc=0; nc<llen; ) | |
737 | { | |
738 | n2l3(p,l); | |
739 | if ((l+nc+3) > llen) | |
740 | { | |
58964a49 | 741 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
742 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); |
743 | goto f_err; | |
744 | } | |
745 | ||
746 | q=p; | |
747 | x=d2i_X509(NULL,&q,l); | |
748 | if (x == NULL) | |
749 | { | |
58964a49 | 750 | al=SSL_AD_BAD_CERTIFICATE; |
d02b48c6 RE |
751 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB); |
752 | goto f_err; | |
753 | } | |
754 | if (q != (p+l)) | |
755 | { | |
58964a49 | 756 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
757 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); |
758 | goto f_err; | |
759 | } | |
f73e07cf | 760 | if (!sk_X509_push(sk,x)) |
d02b48c6 RE |
761 | { |
762 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); | |
763 | goto err; | |
764 | } | |
765 | x=NULL; | |
766 | nc+=l+3; | |
767 | p=q; | |
768 | } | |
769 | ||
770 | i=ssl_verify_cert_chain(s,sk); | |
b1c4fe36 | 771 | if ((s->verify_mode != SSL_VERIFY_NONE) && (!i)) |
d02b48c6 RE |
772 | { |
773 | al=ssl_verify_alarm_type(s->verify_result); | |
774 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); | |
775 | goto f_err; | |
776 | } | |
1fab73ac | 777 | ERR_clear_error(); /* but we keep s->verify_result */ |
d02b48c6 | 778 | |
b56bce4f BM |
779 | sc=ssl_sess_cert_new(); |
780 | if (sc == NULL) goto err; | |
d02b48c6 | 781 | |
b56bce4f BM |
782 | if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); |
783 | s->session->sess_cert=sc; | |
d02b48c6 | 784 | |
b56bce4f | 785 | sc->cert_chain=sk; |
98e04f9e BM |
786 | /* Inconsistency alert: cert_chain does include the peer's |
787 | * certificate, which we don't include in s3_srvr.c */ | |
f73e07cf | 788 | x=sk_X509_value(sk,0); |
d02b48c6 | 789 | sk=NULL; |
f9b3bff6 | 790 | /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end */ |
d02b48c6 RE |
791 | |
792 | pkey=X509_get_pubkey(x); | |
793 | ||
f9b3bff6 RL |
794 | /* VRS: allow null cert if auth == KRB5 */ |
795 | need_cert = | |
796 | ((s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK)) | |
797 | == (SSL_aKRB5|SSL_kKRB5))? 0: 1; | |
798 | ||
799 | #ifdef KSSL_DEBUG | |
800 | printf("pkey,x = %p, %p\n", pkey,x); | |
801 | printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); | |
802 | printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name, | |
803 | s->s3->tmp.new_cipher->algorithms, need_cert); | |
804 | #endif /* KSSL_DEBUG */ | |
805 | ||
806 | if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) | |
d02b48c6 RE |
807 | { |
808 | x=NULL; | |
809 | al=SSL3_AL_FATAL; | |
810 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); | |
811 | goto f_err; | |
812 | } | |
813 | ||
814 | i=ssl_cert_type(x,pkey); | |
f9b3bff6 | 815 | if (need_cert && i < 0) |
d02b48c6 RE |
816 | { |
817 | x=NULL; | |
818 | al=SSL3_AL_FATAL; | |
819 | SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE); | |
820 | goto f_err; | |
821 | } | |
822 | ||
f9b3bff6 RL |
823 | if (need_cert) |
824 | { | |
825 | sc->peer_cert_type=i; | |
826 | CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); | |
827 | /* Why would the following ever happen? | |
828 | * We just created sc a couple of lines ago. */ | |
829 | if (sc->peer_pkeys[i].x509 != NULL) | |
830 | X509_free(sc->peer_pkeys[i].x509); | |
831 | sc->peer_pkeys[i].x509=x; | |
832 | sc->peer_key= &(sc->peer_pkeys[i]); | |
833 | ||
834 | if (s->session->peer != NULL) | |
835 | X509_free(s->session->peer); | |
836 | CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); | |
837 | s->session->peer=x; | |
838 | } | |
839 | else | |
840 | { | |
841 | sc->peer_cert_type=i; | |
842 | sc->peer_key= NULL; | |
843 | ||
844 | if (s->session->peer != NULL) | |
845 | X509_free(s->session->peer); | |
846 | s->session->peer=NULL; | |
847 | } | |
0dd2254d | 848 | s->session->verify_result = s->verify_result; |
d02b48c6 RE |
849 | |
850 | x=NULL; | |
851 | ret=1; | |
852 | ||
853 | if (0) | |
854 | { | |
855 | f_err: | |
856 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
857 | } | |
858 | err: | |
a8236c8c DSH |
859 | EVP_PKEY_free(pkey); |
860 | X509_free(x); | |
f73e07cf | 861 | sk_X509_pop_free(sk,X509_free); |
d02b48c6 RE |
862 | return(ret); |
863 | } | |
864 | ||
6b691a5c | 865 | static int ssl3_get_key_exchange(SSL *s) |
d02b48c6 RE |
866 | { |
867 | #ifndef NO_RSA | |
868 | unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; | |
869 | #endif | |
870 | EVP_MD_CTX md_ctx; | |
871 | unsigned char *param,*p; | |
872 | int al,i,j,param_len,ok; | |
873 | long n,alg; | |
874 | EVP_PKEY *pkey=NULL; | |
79df9d62 | 875 | #ifndef NO_RSA |
d02b48c6 | 876 | RSA *rsa=NULL; |
79df9d62 | 877 | #endif |
58964a49 | 878 | #ifndef NO_DH |
d02b48c6 | 879 | DH *dh=NULL; |
58964a49 | 880 | #endif |
d02b48c6 RE |
881 | |
882 | n=ssl3_get_message(s, | |
883 | SSL3_ST_CR_KEY_EXCH_A, | |
884 | SSL3_ST_CR_KEY_EXCH_B, | |
885 | -1, | |
886 | 1024*8, /* ?? */ | |
887 | &ok); | |
888 | ||
889 | if (!ok) return((int)n); | |
890 | ||
891 | if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) | |
892 | { | |
893 | s->s3->tmp.reuse_message=1; | |
894 | return(1); | |
895 | } | |
896 | ||
897 | param=p=(unsigned char *)s->init_buf->data; | |
898 | ||
9d5cceac | 899 | if (s->session->sess_cert != NULL) |
d02b48c6 RE |
900 | { |
901 | #ifndef NO_RSA | |
b56bce4f | 902 | if (s->session->sess_cert->peer_rsa_tmp != NULL) |
d02b48c6 | 903 | { |
b56bce4f BM |
904 | RSA_free(s->session->sess_cert->peer_rsa_tmp); |
905 | s->session->sess_cert->peer_rsa_tmp=NULL; | |
d02b48c6 RE |
906 | } |
907 | #endif | |
908 | #ifndef NO_DH | |
b56bce4f | 909 | if (s->session->sess_cert->peer_dh_tmp) |
d02b48c6 | 910 | { |
b56bce4f BM |
911 | DH_free(s->session->sess_cert->peer_dh_tmp); |
912 | s->session->sess_cert->peer_dh_tmp=NULL; | |
d02b48c6 RE |
913 | } |
914 | #endif | |
915 | } | |
916 | else | |
917 | { | |
b56bce4f | 918 | s->session->sess_cert=ssl_sess_cert_new(); |
d02b48c6 RE |
919 | } |
920 | ||
921 | param_len=0; | |
922 | alg=s->s3->tmp.new_cipher->algorithms; | |
923 | ||
924 | #ifndef NO_RSA | |
925 | if (alg & SSL_kRSA) | |
926 | { | |
927 | if ((rsa=RSA_new()) == NULL) | |
928 | { | |
929 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); | |
930 | goto err; | |
931 | } | |
932 | n2s(p,i); | |
933 | param_len=i+2; | |
934 | if (param_len > n) | |
935 | { | |
58964a49 | 936 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
937 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH); |
938 | goto f_err; | |
939 | } | |
940 | if (!(rsa->n=BN_bin2bn(p,i,rsa->n))) | |
941 | { | |
942 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); | |
943 | goto err; | |
944 | } | |
945 | p+=i; | |
946 | ||
947 | n2s(p,i); | |
948 | param_len+=i+2; | |
949 | if (param_len > n) | |
950 | { | |
58964a49 | 951 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
952 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH); |
953 | goto f_err; | |
954 | } | |
955 | if (!(rsa->e=BN_bin2bn(p,i,rsa->e))) | |
956 | { | |
957 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); | |
958 | goto err; | |
959 | } | |
960 | p+=i; | |
961 | n-=param_len; | |
962 | ||
d02b48c6 RE |
963 | /* this should be because we are using an export cipher */ |
964 | if (alg & SSL_aRSA) | |
b56bce4f | 965 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
d02b48c6 RE |
966 | else |
967 | { | |
968 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); | |
969 | goto err; | |
970 | } | |
b56bce4f | 971 | s->session->sess_cert->peer_rsa_tmp=rsa; |
6b521df3 | 972 | rsa=NULL; |
d02b48c6 | 973 | } |
3f2599d9 BM |
974 | #else /* NO_RSA */ |
975 | if (0) | |
976 | ; | |
d02b48c6 RE |
977 | #endif |
978 | #ifndef NO_DH | |
3f2599d9 | 979 | else if (alg & SSL_kEDH) |
d02b48c6 RE |
980 | { |
981 | if ((dh=DH_new()) == NULL) | |
982 | { | |
983 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB); | |
984 | goto err; | |
985 | } | |
986 | n2s(p,i); | |
987 | param_len=i+2; | |
988 | if (param_len > n) | |
989 | { | |
58964a49 | 990 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
991 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH); |
992 | goto f_err; | |
993 | } | |
994 | if (!(dh->p=BN_bin2bn(p,i,NULL))) | |
995 | { | |
996 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); | |
997 | goto err; | |
998 | } | |
999 | p+=i; | |
1000 | ||
1001 | n2s(p,i); | |
1002 | param_len+=i+2; | |
1003 | if (param_len > n) | |
1004 | { | |
58964a49 | 1005 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
1006 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH); |
1007 | goto f_err; | |
1008 | } | |
1009 | if (!(dh->g=BN_bin2bn(p,i,NULL))) | |
1010 | { | |
1011 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); | |
1012 | goto err; | |
1013 | } | |
1014 | p+=i; | |
1015 | ||
1016 | n2s(p,i); | |
1017 | param_len+=i+2; | |
1018 | if (param_len > n) | |
1019 | { | |
58964a49 | 1020 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
1021 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH); |
1022 | goto f_err; | |
1023 | } | |
1024 | if (!(dh->pub_key=BN_bin2bn(p,i,NULL))) | |
1025 | { | |
1026 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); | |
1027 | goto err; | |
1028 | } | |
1029 | p+=i; | |
1030 | n-=param_len; | |
1031 | ||
1032 | #ifndef NO_RSA | |
1033 | if (alg & SSL_aRSA) | |
b56bce4f | 1034 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
3f2599d9 BM |
1035 | #else |
1036 | if (0) | |
1037 | ; | |
d02b48c6 RE |
1038 | #endif |
1039 | #ifndef NO_DSA | |
3f2599d9 | 1040 | else if (alg & SSL_aDSS) |
b56bce4f | 1041 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509); |
d02b48c6 RE |
1042 | #endif |
1043 | /* else anonymous DH, so no certificate or pkey. */ | |
1044 | ||
b56bce4f | 1045 | s->session->sess_cert->peer_dh_tmp=dh; |
413c4f45 | 1046 | dh=NULL; |
d02b48c6 RE |
1047 | } |
1048 | else if ((alg & SSL_kDHr) || (alg & SSL_kDHd)) | |
1049 | { | |
58964a49 | 1050 | al=SSL_AD_ILLEGAL_PARAMETER; |
d02b48c6 RE |
1051 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); |
1052 | goto f_err; | |
1053 | } | |
3f2599d9 | 1054 | #endif /* !NO_DH */ |
dfeab068 RE |
1055 | if (alg & SSL_aFZA) |
1056 | { | |
1057 | al=SSL_AD_HANDSHAKE_FAILURE; | |
1058 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); | |
1059 | goto f_err; | |
1060 | } | |
1061 | ||
d02b48c6 RE |
1062 | |
1063 | /* p points to the next byte, there are 'n' bytes left */ | |
1064 | ||
1065 | ||
1066 | /* if it was signed, check the signature */ | |
1067 | if (pkey != NULL) | |
1068 | { | |
1069 | n2s(p,i); | |
1070 | n-=2; | |
1071 | j=EVP_PKEY_size(pkey); | |
1072 | ||
1073 | if ((i != n) || (n > j) || (n <= 0)) | |
1074 | { | |
1075 | /* wrong packet length */ | |
58964a49 | 1076 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 | 1077 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH); |
dfeab068 | 1078 | goto f_err; |
d02b48c6 RE |
1079 | } |
1080 | ||
1081 | #ifndef NO_RSA | |
1082 | if (pkey->type == EVP_PKEY_RSA) | |
1083 | { | |
1084 | int num; | |
1085 | ||
1086 | j=0; | |
1087 | q=md_buf; | |
1088 | for (num=2; num > 0; num--) | |
1089 | { | |
58964a49 RE |
1090 | EVP_DigestInit(&md_ctx,(num == 2) |
1091 | ?s->ctx->md5:s->ctx->sha1); | |
d02b48c6 RE |
1092 | EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); |
1093 | EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | |
1094 | EVP_DigestUpdate(&md_ctx,param,param_len); | |
1095 | EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i); | |
1096 | q+=i; | |
1097 | j+=i; | |
1098 | } | |
1c80019a DSH |
1099 | i=RSA_verify(NID_md5_sha1, md_buf, j, p, n, |
1100 | pkey->pkey.rsa); | |
1101 | if (i < 0) | |
d02b48c6 | 1102 | { |
58964a49 | 1103 | al=SSL_AD_DECRYPT_ERROR; |
d02b48c6 RE |
1104 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); |
1105 | goto f_err; | |
1106 | } | |
1c80019a | 1107 | if (i == 0) |
d02b48c6 RE |
1108 | { |
1109 | /* bad signature */ | |
58964a49 | 1110 | al=SSL_AD_DECRYPT_ERROR; |
d02b48c6 RE |
1111 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); |
1112 | goto f_err; | |
1113 | } | |
1114 | } | |
1115 | else | |
1116 | #endif | |
1117 | #ifndef NO_DSA | |
1118 | if (pkey->type == EVP_PKEY_DSA) | |
1119 | { | |
1120 | /* lets do DSS */ | |
1121 | EVP_VerifyInit(&md_ctx,EVP_dss1()); | |
1122 | EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); | |
1123 | EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); | |
1124 | EVP_VerifyUpdate(&md_ctx,param,param_len); | |
1125 | if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey)) | |
1126 | { | |
1127 | /* bad signature */ | |
58964a49 | 1128 | al=SSL_AD_DECRYPT_ERROR; |
d02b48c6 RE |
1129 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); |
1130 | goto f_err; | |
1131 | } | |
1132 | } | |
1133 | else | |
1134 | #endif | |
1135 | { | |
1136 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); | |
1137 | goto err; | |
1138 | } | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | /* still data left over */ | |
1143 | if (!(alg & SSL_aNULL)) | |
1144 | { | |
1145 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); | |
1146 | goto err; | |
1147 | } | |
1148 | if (n != 0) | |
1149 | { | |
58964a49 | 1150 | al=SSL_AD_DECODE_ERROR; |
d02b48c6 RE |
1151 | SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE); |
1152 | goto f_err; | |
1153 | } | |
1154 | } | |
a8236c8c | 1155 | EVP_PKEY_free(pkey); |
d02b48c6 RE |
1156 | return(1); |
1157 | f_err: | |
1158 | ssl3_send_alert(s,SSL3_AL_FATAL,al); | |
1159 | err: | |
a8236c8c | 1160 | EVP_PKEY_free(pkey); |
6b521df3 BM |
1161 | #ifndef NO_RSA |
1162 | if (rsa != NULL) | |
1163 | RSA_free(rsa); | |
1164 | #endif | |
1165 | #ifndef NO_DH | |
1166 | if (dh != NULL) | |
1167 | DH_free(dh); | |
1168 | #endif | |
d02b48c6 RE |
1169 | return(-1); |
1170 | } | |
1171 | ||
6b691a5c | 1172 | static int ssl3_get_certificate_request(SSL *s) |
d02b48c6 RE |
1173 | { |
1174 | int ok,ret=0; | |
58964a49 RE |
1175 | unsigned long n,nc,l; |
1176 | unsigned int llen,ctype_num,i; | |
d02b48c6 RE |
1177 | X509_NAME *xn=NULL; |
1178 | unsigned char *p,*d,*q; | |
f73e07cf | 1179 | STACK_OF(X509_NAME) *ca_sk=NULL; |
d02b48c6 RE |
1180 | |
1181 | n=ssl3_get_message(s, | |
1182 | SSL3_ST_CR_CERT_REQ_A, | |
1183 | SSL3_ST_CR_CERT_REQ_B, | |
1184 | -1, | |
1185 | #if defined(MSDOS) && !defined(WIN32) | |
1186 | 1024*30, /* 30k max cert list :-) */ | |
1187 | #else | |
1188 | 1024*100, /* 100k max cert list :-) */ | |
1189 | #endif | |
1190 | &ok); | |
1191 | ||
1192 | if (!ok) return((int)n); | |
1193 | ||
1194 | s->s3->tmp.cert_req=0; | |
1195 | ||
1196 | if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) | |
1197 | { | |
1198 | s->s3->tmp.reuse_message=1; | |
1199 | return(1); | |
1200 | } | |
1201 | ||
1202 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) | |
1203 | { | |
58964a49 | 1204 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); |
d02b48c6 RE |
1205 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE); |
1206 | goto err; | |
1207 | } | |
1208 | ||
58964a49 RE |
1209 | /* TLS does not like anon-DH with client cert */ |
1210 | if (s->version > SSL3_VERSION) | |
1211 | { | |
1212 | l=s->s3->tmp.new_cipher->algorithms; | |
1213 | if (l & SSL_aNULL) | |
1214 | { | |
1215 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); | |
1216 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); | |
1217 | goto err; | |
1218 | } | |
1219 | } | |
1220 | ||
d02b48c6 RE |
1221 | d=p=(unsigned char *)s->init_buf->data; |
1222 | ||
f73e07cf | 1223 | if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL) |
d02b48c6 RE |
1224 | { |
1225 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); | |
1226 | goto err; | |
1227 | } | |
1228 | ||
1229 | /* get the certificate types */ | |
1230 | ctype_num= *(p++); | |
1231 | if (ctype_num > SSL3_CT_NUMBER) | |
1232 | ctype_num=SSL3_CT_NUMBER; | |
1233 | for (i=0; i<ctype_num; i++) | |
1234 | s->s3->tmp.ctype[i]= p[i]; | |
1235 | p+=ctype_num; | |
1236 | ||
1237 | /* get the CA RDNs */ | |
1238 | n2s(p,llen); | |
dfeab068 RE |
1239 | #if 0 |
1240 | { | |
1241 | FILE *out; | |
1242 | out=fopen("/tmp/vsign.der","w"); | |
1243 | fwrite(p,1,llen,out); | |
1244 | fclose(out); | |
1245 | } | |
1246 | #endif | |
1247 | ||
d02b48c6 RE |
1248 | if ((llen+ctype_num+2+1) != n) |
1249 | { | |
58964a49 | 1250 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
d02b48c6 RE |
1251 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH); |
1252 | goto err; | |
1253 | } | |
1254 | ||
1255 | for (nc=0; nc<llen; ) | |
1256 | { | |
1257 | n2s(p,l); | |
1258 | if ((l+nc+2) > llen) | |
1259 | { | |
58964a49 | 1260 | if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) |
d02b48c6 | 1261 | goto cont; /* netscape bugs */ |
58964a49 | 1262 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
d02b48c6 RE |
1263 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG); |
1264 | goto err; | |
1265 | } | |
1266 | ||
1267 | q=p; | |
1268 | ||
1269 | if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) | |
1270 | { | |
657e60fa | 1271 | /* If netscape tolerance is on, ignore errors */ |
58964a49 | 1272 | if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) |
d02b48c6 RE |
1273 | goto cont; |
1274 | else | |
1275 | { | |
58964a49 | 1276 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
d02b48c6 RE |
1277 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB); |
1278 | goto err; | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | if (q != (p+l)) | |
1283 | { | |
58964a49 | 1284 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
d02b48c6 RE |
1285 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH); |
1286 | goto err; | |
1287 | } | |
f73e07cf | 1288 | if (!sk_X509_NAME_push(ca_sk,xn)) |
d02b48c6 RE |
1289 | { |
1290 | SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); | |
1291 | goto err; | |
1292 | } | |
1293 | ||
1294 | p+=l; | |
1295 | nc+=l+2; | |
1296 | } | |
1297 | ||
1298 | if (0) | |
1299 | { | |
1300 | cont: | |
1301 | ERR_clear_error(); | |
1302 | } | |
1303 | ||
657e60fa | 1304 | /* we should setup a certificate to return.... */ |
d02b48c6 RE |
1305 | s->s3->tmp.cert_req=1; |
1306 | s->s3->tmp.ctype_num=ctype_num; | |
1307 | if (s->s3->tmp.ca_names != NULL) | |
f73e07cf | 1308 | sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); |
d02b48c6 RE |
1309 | s->s3->tmp.ca_names=ca_sk; |
1310 | ca_sk=NULL; | |
1311 | ||
1312 | ret=1; | |
1313 | err: | |
f73e07cf | 1314 | if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); |
d02b48c6 RE |
1315 | return(ret); |
1316 | } | |
1317 | ||
ccd86b68 | 1318 | static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) |
d02b48c6 RE |
1319 | { |
1320 | return(X509_NAME_cmp(*a,*b)); | |
1321 | } | |
1322 | ||
6b691a5c | 1323 | static int ssl3_get_server_done(SSL *s) |
d02b48c6 RE |
1324 | { |
1325 | int ok,ret=0; | |
1326 | long n; | |
1327 | ||
1328 | n=ssl3_get_message(s, | |
1329 | SSL3_ST_CR_SRVR_DONE_A, | |
1330 | SSL3_ST_CR_SRVR_DONE_B, | |
1331 | SSL3_MT_SERVER_DONE, | |
1332 | 30, /* should be very small, like 0 :-) */ | |
1333 | &ok); | |
1334 | ||
1335 | if (!ok) return((int)n); | |
1336 | if (n > 0) | |
1337 | { | |
1338 | /* should contain no data */ | |
58964a49 | 1339 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); |
d02b48c6 RE |
1340 | SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH); |
1341 | } | |
1342 | ret=1; | |
1343 | return(ret); | |
1344 | } | |
1345 | ||
6b691a5c | 1346 | static int ssl3_send_client_key_exchange(SSL *s) |
d02b48c6 | 1347 | { |
79df9d62 | 1348 | unsigned char *p,*d; |
d02b48c6 RE |
1349 | int n; |
1350 | unsigned long l; | |
79df9d62 UM |
1351 | #ifndef NO_RSA |
1352 | unsigned char *q; | |
d02b48c6 | 1353 | EVP_PKEY *pkey=NULL; |
79df9d62 | 1354 | #endif |
f9b3bff6 RL |
1355 | #ifndef NO_KRB5 |
1356 | KSSL_ERR kssl_err; | |
1357 | #endif /* NO_KRB5 */ | |
d02b48c6 RE |
1358 | |
1359 | if (s->state == SSL3_ST_CW_KEY_EXCH_A) | |
1360 | { | |
1361 | d=(unsigned char *)s->init_buf->data; | |
1362 | p= &(d[4]); | |
1363 | ||
1364 | l=s->s3->tmp.new_cipher->algorithms; | |
1365 | ||
f9b3bff6 RL |
1366 | /* Fool emacs indentation */ |
1367 | if (0) {} | |
d02b48c6 | 1368 | #ifndef NO_RSA |
f9b3bff6 | 1369 | else if (l & SSL_kRSA) |
d02b48c6 RE |
1370 | { |
1371 | RSA *rsa; | |
dfeab068 | 1372 | unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; |
d02b48c6 | 1373 | |
b56bce4f BM |
1374 | if (s->session->sess_cert->peer_rsa_tmp != NULL) |
1375 | rsa=s->session->sess_cert->peer_rsa_tmp; | |
d02b48c6 RE |
1376 | else |
1377 | { | |
b56bce4f | 1378 | pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); |
d02b48c6 RE |
1379 | if ((pkey == NULL) || |
1380 | (pkey->type != EVP_PKEY_RSA) || | |
1381 | (pkey->pkey.rsa == NULL)) | |
1382 | { | |
1383 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); | |
1384 | goto err; | |
1385 | } | |
1386 | rsa=pkey->pkey.rsa; | |
50596582 | 1387 | EVP_PKEY_free(pkey); |
d02b48c6 RE |
1388 | } |
1389 | ||
413c4f45 MC |
1390 | tmp_buf[0]=s->client_version>>8; |
1391 | tmp_buf[1]=s->client_version&0xff; | |
e7f97e2d UM |
1392 | if (RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2) <= 0) |
1393 | goto err; | |
d02b48c6 RE |
1394 | |
1395 | s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH; | |
1396 | ||
58964a49 RE |
1397 | q=p; |
1398 | /* Fix buf for TLS and beyond */ | |
1399 | if (s->version > SSL3_VERSION) | |
1400 | p+=2; | |
1401 | n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH, | |
1402 | tmp_buf,p,rsa,RSA_PKCS1_PADDING); | |
dfeab068 RE |
1403 | #ifdef PKCS1_CHECK |
1404 | if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; | |
1405 | if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; | |
1406 | #endif | |
d02b48c6 RE |
1407 | if (n <= 0) |
1408 | { | |
1409 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); | |
1410 | goto err; | |
1411 | } | |
1412 | ||
58964a49 RE |
1413 | /* Fix buf for TLS and beyond */ |
1414 | if (s->version > SSL3_VERSION) | |
1415 | { | |
1416 | s2n(n,q); | |
1417 | n+=2; | |
1418 | } | |
1419 | ||
d02b48c6 | 1420 | s->session->master_key_length= |
58964a49 | 1421 | s->method->ssl3_enc->generate_master_secret(s, |
d02b48c6 | 1422 | s->session->master_key, |
dfeab068 RE |
1423 | tmp_buf,SSL_MAX_MASTER_KEY_LENGTH); |
1424 | memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH); | |
d02b48c6 | 1425 | } |
f9b3bff6 RL |
1426 | #endif |
1427 | #ifndef NO_KRB5 | |
1428 | else if (l & SSL_kKRB5) | |
1429 | { | |
1430 | krb5_error_code krb5rc; | |
1431 | KSSL_CTX *kssl_ctx = s->kssl_ctx; | |
1432 | krb5_data krb5_ap_req; | |
1433 | ||
1434 | #ifdef KSSL_DEBUG | |
1435 | printf("ssl3_send_client_key_exchange(%lx & %lx)\n", | |
1436 | l, SSL_kKRB5); | |
1437 | #endif /* KSSL_DEBUG */ | |
1438 | ||
1439 | /* | |
1440 | ** Tried to send random tmp_buf[] as PMS in Kerberos ticket | |
1441 | ** by passing krb5_mk_req_extended(ctx,authctx,opts, tmp_buf, ...) | |
1442 | ** but: I can't retrieve the PMS on the other side! There is | |
1443 | ** some indication in the krb5 source that this is only used | |
1444 | ** to generate a checksum. OTOH, the Tung book shows data | |
1445 | ** ("GET widget01.txt") being passed in krb5_mk_req_extended() | |
1446 | ** by way of krb5_sendauth(). I don't get it. | |
1447 | ** Until Kerberos goes 3DES, the big PMS secret would only be | |
1448 | ** encrypted in 1-DES anyway. So losing the PMS shouldn't be | |
1449 | ** a big deal. | |
1450 | */ | |
1451 | krb5rc = kssl_cget_tkt(kssl_ctx, &krb5_ap_req, | |
1452 | &kssl_err); | |
1453 | #ifdef KSSL_DEBUG | |
1454 | { | |
1455 | printf("kssl_cget_tkt rtn %d\n", krb5rc); | |
1456 | kssl_ctx_show(kssl_ctx); | |
1457 | if (krb5rc && kssl_err.text) | |
1458 | printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); | |
1459 | } | |
1460 | #endif /* KSSL_DEBUG */ | |
1461 | ||
1462 | if (krb5rc) | |
1463 | { | |
1464 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); | |
1465 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason); | |
1466 | goto err; | |
1467 | } | |
1468 | ||
1469 | /* Send ticket (copy to *p, set n = length) | |
1470 | */ | |
1471 | n = krb5_ap_req.length; | |
1472 | memcpy(p, krb5_ap_req.data, krb5_ap_req.length); | |
1473 | if (krb5_ap_req.data) | |
1474 | kssl_krb5_free_data_contents(NULL,&krb5_ap_req); | |
1475 | ||
1476 | /* 19991013 VRS - 3DES is kind of bogus here, | |
1477 | ** at least until Kerberos supports 3DES. The only | |
1478 | ** real secret is the 8-byte Kerberos session key; | |
1479 | ** the other key material ((s->) client_random, server_random) | |
1480 | ** could be sniffed. Mixing in these nonces should help | |
1481 | ** protect against replay attacks, however. | |
1482 | ** | |
1483 | ** Alternate code for Kerberos Purists: | |
1484 | ** | |
1485 | ** memcpy(s->session->master_key, kssl_ctx->key, kssl_ctx->length); | |
1486 | ** s->session->master_key_length = kssl_ctx->length; | |
1487 | */ | |
1488 | s->session->master_key_length= | |
1489 | s->method->ssl3_enc->generate_master_secret(s, | |
1490 | s->session->master_key, kssl_ctx->key,kssl_ctx->length); | |
1491 | } | |
d02b48c6 RE |
1492 | #endif |
1493 | #ifndef NO_DH | |
f9b3bff6 | 1494 | else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) |
d02b48c6 RE |
1495 | { |
1496 | DH *dh_srvr,*dh_clnt; | |
1497 | ||
b56bce4f BM |
1498 | if (s->session->sess_cert->peer_dh_tmp != NULL) |
1499 | dh_srvr=s->session->sess_cert->peer_dh_tmp; | |
d02b48c6 RE |
1500 | else |
1501 | { | |
1502 | /* we get them from the cert */ | |
58964a49 | 1503 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
d02b48c6 RE |
1504 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); |
1505 | goto err; | |
1506 | } | |
1507 | ||
1508 | /* generate a new random key */ | |
1509 | if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) | |
1510 | { | |
1511 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); | |
1512 | goto err; | |
1513 | } | |
1514 | if (!DH_generate_key(dh_clnt)) | |
1515 | { | |
1516 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); | |
1517 | goto err; | |
1518 | } | |
1519 | ||
1520 | /* use the 'p' output buffer for the DH key, but | |
1521 | * make sure to clear it out afterwards */ | |
58964a49 | 1522 | |
d02b48c6 | 1523 | n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); |
58964a49 | 1524 | |
d02b48c6 RE |
1525 | if (n <= 0) |
1526 | { | |
1527 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); | |
1528 | goto err; | |
1529 | } | |
1530 | ||
1531 | /* generate master key from the result */ | |
1532 | s->session->master_key_length= | |
58964a49 | 1533 | s->method->ssl3_enc->generate_master_secret(s, |
d02b48c6 RE |
1534 | s->session->master_key,p,n); |
1535 | /* clean up */ | |
1536 | memset(p,0,n); | |
1537 | ||
1538 | /* send off the data */ | |
1539 | n=BN_num_bytes(dh_clnt->pub_key); | |
1540 | s2n(n,p); | |
1541 | BN_bn2bin(dh_clnt->pub_key,p); | |
1542 | n+=2; | |
1543 | ||
1544 | DH_free(dh_clnt); | |
1545 | ||
1546 | /* perhaps clean things up a bit EAY EAY EAY EAY*/ | |
1547 | } | |
d02b48c6 | 1548 | #endif |
f9b3bff6 | 1549 | else |
d02b48c6 | 1550 | { |
58964a49 | 1551 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
d02b48c6 RE |
1552 | SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR); |
1553 | goto err; | |
1554 | } | |
1555 | ||
1556 | *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; | |
1557 | l2n3(n,d); | |
1558 | ||
1559 | s->state=SSL3_ST_CW_KEY_EXCH_B; | |
1560 | /* number of bytes to write */ | |
1561 | s->init_num=n+4; | |
1562 | s->init_off=0; | |
1563 | } | |
1564 | ||
1565 | /* SSL3_ST_CW_KEY_EXCH_B */ | |
1566 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
1567 | err: | |
1568 | return(-1); | |
1569 | } | |
1570 | ||
6b691a5c | 1571 | static int ssl3_send_client_verify(SSL *s) |
d02b48c6 RE |
1572 | { |
1573 | unsigned char *p,*d; | |
1574 | unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; | |
1575 | EVP_PKEY *pkey; | |
79df9d62 | 1576 | #ifndef NO_RSA |
ca7fea96 | 1577 | unsigned u=0; |
79df9d62 | 1578 | #endif |
d02b48c6 | 1579 | unsigned long n; |
58964a49 RE |
1580 | #ifndef NO_DSA |
1581 | int j; | |
1582 | #endif | |
d02b48c6 RE |
1583 | |
1584 | if (s->state == SSL3_ST_CW_CERT_VRFY_A) | |
1585 | { | |
1586 | d=(unsigned char *)s->init_buf->data; | |
1587 | p= &(d[4]); | |
1588 | pkey=s->cert->key->privatekey; | |
1589 | ||
58964a49 RE |
1590 | s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), |
1591 | &(data[MD5_DIGEST_LENGTH])); | |
d02b48c6 RE |
1592 | |
1593 | #ifndef NO_RSA | |
1594 | if (pkey->type == EVP_PKEY_RSA) | |
1595 | { | |
58964a49 RE |
1596 | s->method->ssl3_enc->cert_verify_mac(s, |
1597 | &(s->s3->finish_dgst1),&(data[0])); | |
1c80019a DSH |
1598 | if (RSA_sign(NID_md5_sha1, data, |
1599 | MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, | |
ca7fea96 | 1600 | &(p[2]), &u, pkey->pkey.rsa) <= 0 ) |
d02b48c6 RE |
1601 | { |
1602 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); | |
1603 | goto err; | |
1604 | } | |
ca7fea96 BL |
1605 | s2n(u,p); |
1606 | n=u+2; | |
d02b48c6 RE |
1607 | } |
1608 | else | |
1609 | #endif | |
1610 | #ifndef NO_DSA | |
1611 | if (pkey->type == EVP_PKEY_DSA) | |
1612 | { | |
1613 | if (!DSA_sign(pkey->save_type, | |
1614 | &(data[MD5_DIGEST_LENGTH]), | |
1615 | SHA_DIGEST_LENGTH,&(p[2]), | |
1616 | (unsigned int *)&j,pkey->pkey.dsa)) | |
1617 | { | |
1618 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); | |
1619 | goto err; | |
1620 | } | |
1621 | s2n(j,p); | |
1622 | n=j+2; | |
1623 | } | |
1624 | else | |
1625 | #endif | |
1626 | { | |
1627 | SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR); | |
1628 | goto err; | |
1629 | } | |
1630 | *(d++)=SSL3_MT_CERTIFICATE_VERIFY; | |
1631 | l2n3(n,d); | |
1632 | ||
1633 | s->init_num=(int)n+4; | |
1634 | s->init_off=0; | |
1635 | } | |
1636 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
1637 | err: | |
1638 | return(-1); | |
1639 | } | |
1640 | ||
6b691a5c | 1641 | static int ssl3_send_client_certificate(SSL *s) |
d02b48c6 RE |
1642 | { |
1643 | X509 *x509=NULL; | |
1644 | EVP_PKEY *pkey=NULL; | |
1645 | int i; | |
1646 | unsigned long l; | |
1647 | ||
1648 | if (s->state == SSL3_ST_CW_CERT_A) | |
1649 | { | |
1650 | if ((s->cert == NULL) || | |
1651 | (s->cert->key->x509 == NULL) || | |
1652 | (s->cert->key->privatekey == NULL)) | |
1653 | s->state=SSL3_ST_CW_CERT_B; | |
1654 | else | |
1655 | s->state=SSL3_ST_CW_CERT_C; | |
1656 | } | |
1657 | ||
1658 | /* We need to get a client cert */ | |
1659 | if (s->state == SSL3_ST_CW_CERT_B) | |
1660 | { | |
1661 | /* If we get an error, we need to | |
1662 | * ssl->rwstate=SSL_X509_LOOKUP; return(-1); | |
1663 | * We then get retied later */ | |
1664 | i=0; | |
1665 | if (s->ctx->client_cert_cb != NULL) | |
1666 | i=s->ctx->client_cert_cb(s,&(x509),&(pkey)); | |
1667 | if (i < 0) | |
1668 | { | |
1669 | s->rwstate=SSL_X509_LOOKUP; | |
1670 | return(-1); | |
1671 | } | |
1672 | s->rwstate=SSL_NOTHING; | |
1673 | if ((i == 1) && (pkey != NULL) && (x509 != NULL)) | |
1674 | { | |
1675 | s->state=SSL3_ST_CW_CERT_B; | |
1676 | if ( !SSL_use_certificate(s,x509) || | |
1677 | !SSL_use_PrivateKey(s,pkey)) | |
1678 | i=0; | |
1679 | } | |
1680 | else if (i == 1) | |
1681 | { | |
1682 | i=0; | |
1683 | SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); | |
1684 | } | |
1685 | ||
1686 | if (x509 != NULL) X509_free(x509); | |
1687 | if (pkey != NULL) EVP_PKEY_free(pkey); | |
1688 | if (i == 0) | |
1689 | { | |
58964a49 RE |
1690 | if (s->version == SSL3_VERSION) |
1691 | { | |
1692 | s->s3->tmp.cert_req=0; | |
1693 | ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); | |
1694 | return(1); | |
1695 | } | |
1696 | else | |
1697 | { | |
1698 | s->s3->tmp.cert_req=2; | |
1699 | } | |
d02b48c6 RE |
1700 | } |
1701 | ||
1702 | /* Ok, we have a cert */ | |
1703 | s->state=SSL3_ST_CW_CERT_C; | |
1704 | } | |
1705 | ||
1706 | if (s->state == SSL3_ST_CW_CERT_C) | |
1707 | { | |
1708 | s->state=SSL3_ST_CW_CERT_D; | |
58964a49 RE |
1709 | l=ssl3_output_cert_chain(s, |
1710 | (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); | |
d02b48c6 RE |
1711 | s->init_num=(int)l; |
1712 | s->init_off=0; | |
1713 | } | |
1714 | /* SSL3_ST_CW_CERT_D */ | |
1715 | return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); | |
1716 | } | |
1717 | ||
1718 | #define has_bits(i,m) (((i)&(m)) == (m)) | |
1719 | ||
6b691a5c | 1720 | static int ssl3_check_cert_and_algorithm(SSL *s) |
d02b48c6 RE |
1721 | { |
1722 | int i,idx; | |
1723 | long algs; | |
1724 | EVP_PKEY *pkey=NULL; | |
b56bce4f | 1725 | SESS_CERT *sc; |
79df9d62 | 1726 | #ifndef NO_RSA |
d02b48c6 | 1727 | RSA *rsa; |
79df9d62 UM |
1728 | #endif |
1729 | #ifndef NO_DH | |
d02b48c6 | 1730 | DH *dh; |
79df9d62 | 1731 | #endif |
d02b48c6 | 1732 | |
b56bce4f | 1733 | sc=s->session->sess_cert; |
d02b48c6 | 1734 | |
b56bce4f | 1735 | if (sc == NULL) |
d02b48c6 RE |
1736 | { |
1737 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR); | |
1738 | goto err; | |
1739 | } | |
1740 | ||
1741 | algs=s->s3->tmp.new_cipher->algorithms; | |
1742 | ||
1743 | /* we don't have a certificate */ | |
f9b3bff6 | 1744 | if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) |
d02b48c6 RE |
1745 | return(1); |
1746 | ||
79df9d62 | 1747 | #ifndef NO_RSA |
b56bce4f | 1748 | rsa=s->session->sess_cert->peer_rsa_tmp; |
79df9d62 UM |
1749 | #endif |
1750 | #ifndef NO_DH | |
b56bce4f | 1751 | dh=s->session->sess_cert->peer_dh_tmp; |
79df9d62 | 1752 | #endif |
d02b48c6 RE |
1753 | |
1754 | /* This is the passed certificate */ | |
1755 | ||
b56bce4f BM |
1756 | idx=sc->peer_cert_type; |
1757 | pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); | |
1758 | i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); | |
a8236c8c | 1759 | EVP_PKEY_free(pkey); |
d02b48c6 RE |
1760 | |
1761 | ||
1762 | /* Check that we have a certificate if we require one */ | |
1763 | if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) | |
1764 | { | |
1765 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT); | |
1766 | goto f_err; | |
1767 | } | |
1768 | #ifndef NO_DSA | |
1769 | else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) | |
1770 | { | |
1771 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT); | |
1772 | goto f_err; | |
1773 | } | |
1774 | #endif | |
79df9d62 | 1775 | #ifndef NO_RSA |
d02b48c6 RE |
1776 | if ((algs & SSL_kRSA) && |
1777 | !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) | |
1778 | { | |
1779 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT); | |
1780 | goto f_err; | |
1781 | } | |
79df9d62 | 1782 | #endif |
d02b48c6 | 1783 | #ifndef NO_DH |
79df9d62 | 1784 | if ((algs & SSL_kEDH) && |
d02b48c6 RE |
1785 | !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) |
1786 | { | |
1787 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY); | |
1788 | goto f_err; | |
1789 | } | |
1790 | else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA)) | |
1791 | { | |
1792 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT); | |
1793 | goto f_err; | |
1794 | } | |
1795 | #ifndef NO_DSA | |
1796 | else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) | |
1797 | { | |
1798 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT); | |
1799 | goto f_err; | |
1800 | } | |
1801 | #endif | |
1802 | #endif | |
1803 | ||
018e57c7 | 1804 | if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP)) |
d02b48c6 RE |
1805 | { |
1806 | #ifndef NO_RSA | |
1807 | if (algs & SSL_kRSA) | |
1808 | { | |
06ab81f9 | 1809 | if (rsa == NULL |
018e57c7 | 1810 | || RSA_size(rsa) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) |
d02b48c6 RE |
1811 | { |
1812 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY); | |
1813 | goto f_err; | |
1814 | } | |
1815 | } | |
1816 | else | |
1817 | #endif | |
1818 | #ifndef NO_DH | |
1819 | if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) | |
06ab81f9 BL |
1820 | { |
1821 | if (dh == NULL | |
018e57c7 | 1822 | || DH_size(dh) > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) |
d02b48c6 RE |
1823 | { |
1824 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY); | |
1825 | goto f_err; | |
1826 | } | |
1827 | } | |
1828 | else | |
1829 | #endif | |
1830 | { | |
1831 | SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); | |
1832 | goto f_err; | |
1833 | } | |
1834 | } | |
1835 | return(1); | |
1836 | f_err: | |
58964a49 | 1837 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
d02b48c6 RE |
1838 | err: |
1839 | return(0); | |
1840 | } | |
1841 |