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