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