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