]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/statem/statem_lib.c
Allow different protocol version when trying to reuse a session
[thirdparty/openssl.git] / ssl / statem / statem_lib.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57 /* ====================================================================
58 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110 /* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115
116 #include <limits.h>
117 #include <string.h>
118 #include <stdio.h>
119 #include "../ssl_locl.h"
120 #include "statem_locl.h"
121 #include <openssl/buffer.h>
122 #include <openssl/rand.h>
123 #include <openssl/objects.h>
124 #include <openssl/evp.h>
125 #include <openssl/x509.h>
126
127 /*
128 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
129 * SSL3_RT_CHANGE_CIPHER_SPEC)
130 */
131 int ssl3_do_write(SSL *s, int type)
132 {
133 int ret;
134
135 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
136 s->init_num);
137 if (ret < 0)
138 return (-1);
139 if (type == SSL3_RT_HANDSHAKE)
140 /*
141 * should not be done for 'Hello Request's, but in that case we'll
142 * ignore the result anyway
143 */
144 ssl3_finish_mac(s, (unsigned char *)&s->init_buf->data[s->init_off],
145 ret);
146
147 if (ret == s->init_num) {
148 if (s->msg_callback)
149 s->msg_callback(1, s->version, type, s->init_buf->data,
150 (size_t)(s->init_off + s->init_num), s,
151 s->msg_callback_arg);
152 return (1);
153 }
154 s->init_off += ret;
155 s->init_num -= ret;
156 return (0);
157 }
158
159 int tls_construct_finished(SSL *s, const char *sender, int slen)
160 {
161 unsigned char *p;
162 int i;
163 unsigned long l;
164
165 p = ssl_handshake_start(s);
166
167 i = s->method->ssl3_enc->final_finish_mac(s,
168 sender, slen,
169 s->s3->tmp.finish_md);
170 if (i <= 0)
171 return 0;
172 s->s3->tmp.finish_md_len = i;
173 memcpy(p, s->s3->tmp.finish_md, i);
174 l = i;
175
176 /*
177 * Copy the finished so we can use it for renegotiation checks
178 */
179 if (!s->server) {
180 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
181 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md, i);
182 s->s3->previous_client_finished_len = i;
183 } else {
184 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
185 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md, i);
186 s->s3->previous_server_finished_len = i;
187 }
188
189 if (!ssl_set_handshake_header(s, SSL3_MT_FINISHED, l)) {
190 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED, ERR_R_INTERNAL_ERROR);
191 return 0;
192 }
193
194 return 1;
195 }
196
197 #ifndef OPENSSL_NO_NEXTPROTONEG
198 /*
199 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
200 * to far.
201 */
202 static void ssl3_take_mac(SSL *s)
203 {
204 const char *sender;
205 int slen;
206 /*
207 * If no new cipher setup return immediately: other functions will set
208 * the appropriate error.
209 */
210 if (s->s3->tmp.new_cipher == NULL)
211 return;
212 if (!s->server) {
213 sender = s->method->ssl3_enc->server_finished_label;
214 slen = s->method->ssl3_enc->server_finished_label_len;
215 } else {
216 sender = s->method->ssl3_enc->client_finished_label;
217 slen = s->method->ssl3_enc->client_finished_label_len;
218 }
219
220 s->s3->tmp.peer_finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
221 sender,
222 slen,
223 s->s3->tmp.peer_finish_md);
224 }
225 #endif
226
227 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
228 {
229 int al;
230 long remain;
231
232 remain = PACKET_remaining(pkt);
233 /*
234 * 'Change Cipher Spec' is just a single byte, which should already have
235 * been consumed by ssl_get_message() so there should be no bytes left,
236 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
237 */
238 if (SSL_IS_DTLS(s)) {
239 if ((s->version == DTLS1_BAD_VER
240 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
241 || (s->version != DTLS1_BAD_VER
242 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
243 al = SSL_AD_ILLEGAL_PARAMETER;
244 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
245 SSL_R_BAD_CHANGE_CIPHER_SPEC);
246 goto f_err;
247 }
248 } else {
249 if (remain != 0) {
250 al = SSL_AD_ILLEGAL_PARAMETER;
251 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
252 SSL_R_BAD_CHANGE_CIPHER_SPEC);
253 goto f_err;
254 }
255 }
256
257 /* Check we have a cipher to change to */
258 if (s->s3->tmp.new_cipher == NULL) {
259 al = SSL_AD_UNEXPECTED_MESSAGE;
260 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
261 goto f_err;
262 }
263
264 s->s3->change_cipher_spec = 1;
265 if (!ssl3_do_change_cipher_spec(s)) {
266 al = SSL_AD_INTERNAL_ERROR;
267 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
268 goto f_err;
269 }
270
271 if (SSL_IS_DTLS(s)) {
272 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
273
274 if (s->version == DTLS1_BAD_VER)
275 s->d1->handshake_read_seq++;
276
277 #ifndef OPENSSL_NO_SCTP
278 /*
279 * Remember that a CCS has been received, so that an old key of
280 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
281 * SCTP is used
282 */
283 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
284 #endif
285 }
286
287 return MSG_PROCESS_CONTINUE_READING;
288 f_err:
289 ssl3_send_alert(s, SSL3_AL_FATAL, al);
290 ossl_statem_set_error(s);
291 return MSG_PROCESS_ERROR;
292 }
293
294 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
295 {
296 int al, i;
297
298 /* If this occurs, we have missed a message */
299 if (!s->s3->change_cipher_spec) {
300 al = SSL_AD_UNEXPECTED_MESSAGE;
301 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_GOT_A_FIN_BEFORE_A_CCS);
302 goto f_err;
303 }
304 s->s3->change_cipher_spec = 0;
305
306 i = s->s3->tmp.peer_finish_md_len;
307
308 if ((unsigned long)i != PACKET_remaining(pkt)) {
309 al = SSL_AD_DECODE_ERROR;
310 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_BAD_DIGEST_LENGTH);
311 goto f_err;
312 }
313
314 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md, i) != 0) {
315 al = SSL_AD_DECRYPT_ERROR;
316 SSLerr(SSL_F_TLS_PROCESS_FINISHED, SSL_R_DIGEST_CHECK_FAILED);
317 goto f_err;
318 }
319
320 /*
321 * Copy the finished so we can use it for renegotiation checks
322 */
323 if (s->server) {
324 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
325 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md, i);
326 s->s3->previous_client_finished_len = i;
327 } else {
328 OPENSSL_assert(i <= EVP_MAX_MD_SIZE);
329 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md, i);
330 s->s3->previous_server_finished_len = i;
331 }
332
333 return MSG_PROCESS_FINISHED_READING;
334 f_err:
335 ssl3_send_alert(s, SSL3_AL_FATAL, al);
336 ossl_statem_set_error(s);
337 return MSG_PROCESS_ERROR;
338 }
339
340 int tls_construct_change_cipher_spec(SSL *s)
341 {
342 unsigned char *p;
343
344 p = (unsigned char *)s->init_buf->data;
345 *p = SSL3_MT_CCS;
346 s->init_num = 1;
347 s->init_off = 0;
348
349 return 1;
350 }
351
352 unsigned long ssl3_output_cert_chain(SSL *s, CERT_PKEY *cpk)
353 {
354 unsigned char *p;
355 unsigned long l = 3 + SSL_HM_HEADER_LENGTH(s);
356
357 if (!ssl_add_cert_chain(s, cpk, &l))
358 return 0;
359
360 l -= 3 + SSL_HM_HEADER_LENGTH(s);
361 p = ssl_handshake_start(s);
362 l2n3(l, p);
363 l += 3;
364
365 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE, l)) {
366 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN, ERR_R_INTERNAL_ERROR);
367 return 0;
368 }
369 return l + SSL_HM_HEADER_LENGTH(s);
370 }
371
372 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst)
373 {
374 void (*cb) (const SSL *ssl, int type, int val) = NULL;
375
376 #ifndef OPENSSL_NO_SCTP
377 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))) {
378 WORK_STATE ret;
379 ret = dtls_wait_for_dry(s);
380 if (ret != WORK_FINISHED_CONTINUE)
381 return ret;
382 }
383 #endif
384
385 /* clean a few things up */
386 ssl3_cleanup_key_block(s);
387
388 if (!SSL_IS_DTLS(s)) {
389 /*
390 * We don't do this in DTLS because we may still need the init_buf
391 * in case there are any unexpected retransmits
392 */
393 BUF_MEM_free(s->init_buf);
394 s->init_buf = NULL;
395 }
396
397 ssl_free_wbio_buffer(s);
398
399 s->init_num = 0;
400
401 if (!s->server || s->renegotiate == 2) {
402 /* skipped if we just sent a HelloRequest */
403 s->renegotiate = 0;
404 s->new_session = 0;
405
406 if (s->server) {
407 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
408
409 s->ctx->stats.sess_accept_good++;
410 s->handshake_func = ossl_statem_accept;
411 } else {
412 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
413 if (s->hit)
414 s->ctx->stats.sess_hit++;
415
416 s->handshake_func = ossl_statem_connect;
417 s->ctx->stats.sess_connect_good++;
418 }
419
420 if (s->info_callback != NULL)
421 cb = s->info_callback;
422 else if (s->ctx->info_callback != NULL)
423 cb = s->ctx->info_callback;
424
425 if (cb != NULL)
426 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
427
428 if (SSL_IS_DTLS(s)) {
429 /* done with handshaking */
430 s->d1->handshake_read_seq = 0;
431 s->d1->handshake_write_seq = 0;
432 s->d1->next_handshake_write_seq = 0;
433 }
434 }
435
436 return WORK_FINISHED_STOP;
437 }
438
439 int tls_get_message_header(SSL *s, int *mt)
440 {
441 /* s->init_num < SSL3_HM_HEADER_LENGTH */
442 int skip_message, i, recvd_type, al;
443 unsigned char *p;
444 unsigned long l;
445
446 p = (unsigned char *)s->init_buf->data;
447
448 do {
449 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
450 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
451 &p[s->init_num], SSL3_HM_HEADER_LENGTH - s->init_num, 0);
452 if (i <= 0) {
453 s->rwstate = SSL_READING;
454 return 0;
455 }
456 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
457 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
458 s->init_num = i - 1;
459 s->s3->tmp.message_size = i;
460 return 1;
461 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
462 al = SSL_AD_UNEXPECTED_MESSAGE;
463 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_CCS_RECEIVED_EARLY);
464 goto f_err;
465 }
466 s->init_num += i;
467 }
468
469 skip_message = 0;
470 if (!s->server)
471 if (p[0] == SSL3_MT_HELLO_REQUEST)
472 /*
473 * The server may always send 'Hello Request' messages --
474 * we are doing a handshake anyway now, so ignore them if
475 * their format is correct. Does not count for 'Finished'
476 * MAC.
477 */
478 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
479 s->init_num = 0;
480 skip_message = 1;
481
482 if (s->msg_callback)
483 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
484 p, SSL3_HM_HEADER_LENGTH, s,
485 s->msg_callback_arg);
486 }
487 } while (skip_message);
488 /* s->init_num == SSL3_HM_HEADER_LENGTH */
489
490 *mt = *p;
491 s->s3->tmp.message_type = *(p++);
492
493 if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
494 /*
495 * Only happens with SSLv3+ in an SSLv2 backward compatible
496 * ClientHello
497 */
498 /*
499 * Total message size is the remaining record bytes to read
500 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
501 */
502 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
503 + SSL3_HM_HEADER_LENGTH;
504 if (l && !BUF_MEM_grow_clean(s->init_buf, (int)l)) {
505 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
506 goto err;
507 }
508 s->s3->tmp.message_size = l;
509
510 s->init_msg = s->init_buf->data;
511 s->init_num = SSL3_HM_HEADER_LENGTH;
512 } else {
513 n2l3(p, l);
514 /* BUF_MEM_grow takes an 'int' parameter */
515 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
516 al = SSL_AD_ILLEGAL_PARAMETER;
517 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, SSL_R_EXCESSIVE_MESSAGE_SIZE);
518 goto f_err;
519 }
520 if (l && !BUF_MEM_grow_clean(s->init_buf,
521 (int)l + SSL3_HM_HEADER_LENGTH)) {
522 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER, ERR_R_BUF_LIB);
523 goto err;
524 }
525 s->s3->tmp.message_size = l;
526
527 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
528 s->init_num = 0;
529 }
530
531 return 1;
532 f_err:
533 ssl3_send_alert(s, SSL3_AL_FATAL, al);
534 err:
535 return 0;
536 }
537
538 int tls_get_message_body(SSL *s, unsigned long *len)
539 {
540 long n;
541 unsigned char *p;
542 int i;
543
544 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
545 /* We've already read everything in */
546 *len = (unsigned long)s->init_num;
547 return 1;
548 }
549
550 p = s->init_msg;
551 n = s->s3->tmp.message_size - s->init_num;
552 while (n > 0) {
553 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
554 &p[s->init_num], n, 0);
555 if (i <= 0) {
556 s->rwstate = SSL_READING;
557 *len = 0;
558 return 0;
559 }
560 s->init_num += i;
561 n -= i;
562 }
563
564 #ifndef OPENSSL_NO_NEXTPROTONEG
565 /*
566 * If receiving Finished, record MAC of prior handshake messages for
567 * Finished verification.
568 */
569 if (*s->init_buf->data == SSL3_MT_FINISHED)
570 ssl3_take_mac(s);
571 #endif
572
573 /* Feed this message into MAC computation. */
574 if(RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
575 ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, s->init_num);
576 if (s->msg_callback)
577 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
578 (size_t)s->init_num, s, s->msg_callback_arg);
579 } else {
580 ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
581 s->init_num + SSL3_HM_HEADER_LENGTH);
582 if (s->msg_callback)
583 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
584 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
585 s->msg_callback_arg);
586 }
587
588 /*
589 * init_num should never be negative...should probably be declared
590 * unsigned
591 */
592 if (s->init_num < 0) {
593 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY, ERR_R_INTERNAL_ERROR);
594 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
595 *len = 0;
596 return 0;
597 }
598 *len = (unsigned long)s->init_num;
599 return 1;
600 }
601
602 int ssl_cert_type(X509 *x, EVP_PKEY *pk)
603 {
604 if (pk == NULL &&
605 (pk = X509_get0_pubkey(x)) == NULL)
606 return -1;
607
608 switch (EVP_PKEY_id(pk)) {
609 default:
610 return -1;
611 case EVP_PKEY_RSA:
612 return SSL_PKEY_RSA_ENC;
613 case EVP_PKEY_DSA:
614 return SSL_PKEY_DSA_SIGN;
615 #ifndef OPENSSL_NO_EC
616 case EVP_PKEY_EC:
617 return SSL_PKEY_ECC;
618 #endif
619 #ifndef OPENSSL_NO_GOST
620 case NID_id_GostR3410_2001:
621 return SSL_PKEY_GOST01;
622 case NID_id_GostR3410_2012_256:
623 return SSL_PKEY_GOST12_256;
624 case NID_id_GostR3410_2012_512:
625 return SSL_PKEY_GOST12_512;
626 #endif
627 }
628 }
629
630 int ssl_verify_alarm_type(long type)
631 {
632 int al;
633
634 switch (type) {
635 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
636 case X509_V_ERR_UNABLE_TO_GET_CRL:
637 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
638 al = SSL_AD_UNKNOWN_CA;
639 break;
640 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
641 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
642 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
643 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
644 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
645 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
646 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
647 case X509_V_ERR_CERT_NOT_YET_VALID:
648 case X509_V_ERR_CRL_NOT_YET_VALID:
649 case X509_V_ERR_CERT_UNTRUSTED:
650 case X509_V_ERR_CERT_REJECTED:
651 al = SSL_AD_BAD_CERTIFICATE;
652 break;
653 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
654 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
655 al = SSL_AD_DECRYPT_ERROR;
656 break;
657 case X509_V_ERR_CERT_HAS_EXPIRED:
658 case X509_V_ERR_CRL_HAS_EXPIRED:
659 al = SSL_AD_CERTIFICATE_EXPIRED;
660 break;
661 case X509_V_ERR_CERT_REVOKED:
662 al = SSL_AD_CERTIFICATE_REVOKED;
663 break;
664 case X509_V_ERR_OUT_OF_MEM:
665 al = SSL_AD_INTERNAL_ERROR;
666 break;
667 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
668 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
669 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
670 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
671 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
672 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
673 case X509_V_ERR_INVALID_CA:
674 al = SSL_AD_UNKNOWN_CA;
675 break;
676 case X509_V_ERR_APPLICATION_VERIFICATION:
677 al = SSL_AD_HANDSHAKE_FAILURE;
678 break;
679 case X509_V_ERR_INVALID_PURPOSE:
680 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
681 break;
682 default:
683 al = SSL_AD_CERTIFICATE_UNKNOWN;
684 break;
685 }
686 return (al);
687 }
688
689 int ssl_allow_compression(SSL *s)
690 {
691 if (s->options & SSL_OP_NO_COMPRESSION)
692 return 0;
693 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
694 }
695
696 static int version_cmp(const SSL *s, int a, int b)
697 {
698 int dtls = SSL_IS_DTLS(s);
699
700 if (a == b)
701 return 0;
702 if (!dtls)
703 return a < b ? -1 : 1;
704 return DTLS_VERSION_LT(a, b) ? -1 : 1;
705 }
706
707 typedef struct {
708 int version;
709 const SSL_METHOD *(*cmeth)(void);
710 const SSL_METHOD *(*smeth)(void);
711 } version_info;
712
713 #if TLS_MAX_VERSION != TLS1_2_VERSION
714 # error Code needs update for TLS_method() support beyond TLS1_2_VERSION.
715 #endif
716
717 static const version_info tls_version_table[] = {
718 #ifndef OPENSSL_NO_TLS1_2
719 { TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method },
720 #else
721 { TLS1_2_VERSION, NULL, NULL },
722 #endif
723 #ifndef OPENSSL_NO_TLS1_1
724 { TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method },
725 #else
726 { TLS1_1_VERSION, NULL, NULL },
727 #endif
728 #ifndef OPENSSL_NO_TLS1
729 { TLS1_VERSION, tlsv1_client_method, tlsv1_server_method },
730 #else
731 { TLS1_VERSION, NULL, NULL },
732 #endif
733 #ifndef OPENSSL_NO_SSL3
734 { SSL3_VERSION, sslv3_client_method, sslv3_server_method },
735 #else
736 { SSL3_VERSION, NULL, NULL },
737 #endif
738 { 0, NULL, NULL },
739 };
740
741 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
742 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
743 #endif
744
745 static const version_info dtls_version_table[] = {
746 #ifndef OPENSSL_NO_DTLS1_2
747 { DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method },
748 #else
749 { DTLS1_2_VERSION, NULL, NULL },
750 #endif
751 #ifndef OPENSSL_NO_DTLS1
752 { DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method },
753 #else
754 { DTLS1_VERSION, NULL, NULL },
755 #endif
756 { 0, NULL, NULL },
757 };
758
759 /*
760 * ssl_method_error - Check whether an SSL_METHOD is enabled.
761 *
762 * @s: The SSL handle for the candidate method
763 * @method: the intended method.
764 *
765 * Returns 0 on success, or an SSL error reason on failure.
766 */
767 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
768 {
769 int version = method->version;
770
771 if ((s->min_proto_version != 0 &&
772 version_cmp(s, version, s->min_proto_version) < 0) ||
773 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
774 return SSL_R_VERSION_TOO_LOW;
775
776 if (s->max_proto_version != 0 &&
777 version_cmp(s, version, s->max_proto_version) > 0)
778 return SSL_R_VERSION_TOO_HIGH;
779
780 if ((s->options & method->mask) != 0)
781 return SSL_R_UNSUPPORTED_PROTOCOL;
782 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
783 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
784 else if ((method->flags & SSL_METHOD_NO_FIPS) != 0 && FIPS_mode())
785 return SSL_R_AT_LEAST_TLS_1_0_NEEDED_IN_FIPS_MODE;
786
787 return 0;
788 }
789
790 /*
791 * ssl_version_supported - Check that the specified `version` is supported by
792 * `SSL *` instance
793 *
794 * @s: The SSL handle for the candidate method
795 * @version: Protocol version to test against
796 *
797 * Returns 1 when supported, otherwise 0
798 */
799 int ssl_version_supported(const SSL *s, int version)
800 {
801 const version_info *vent;
802 const version_info *table;
803
804 switch (s->method->version) {
805 default:
806 /* Version should match method version for non-ANY method */
807 return version_cmp(s, version, s->version) == 0;
808 case TLS_ANY_VERSION:
809 table = tls_version_table;
810 break;
811 case DTLS_ANY_VERSION:
812 table = dtls_version_table;
813 break;
814 }
815
816 for (vent = table;
817 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
818 ++vent) {
819 if (vent->cmeth != NULL &&
820 version_cmp(s, version, vent->version) == 0 &&
821 ssl_method_error(s, vent->cmeth()) == 0) {
822 return 1;
823 }
824 }
825 return 0;
826 }
827
828 /*
829 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
830 * fallback indication from a client check whether we're using the highest
831 * supported protocol version.
832 *
833 * @s server SSL handle.
834 *
835 * Returns 1 when using the highest enabled version, 0 otherwise.
836 */
837 int ssl_check_version_downgrade(SSL *s)
838 {
839 const version_info *vent;
840 const version_info *table;
841
842 /*
843 * Check that the current protocol is the highest enabled version
844 * (according to s->ctx->method, as version negotiation may have changed
845 * s->method).
846 */
847 if (s->version == s->ctx->method->version)
848 return 1;
849
850 /*
851 * Apparently we're using a version-flexible SSL_METHOD (not at its
852 * highest protocol version).
853 */
854 if (s->ctx->method->version == TLS_method()->version)
855 table = tls_version_table;
856 else if (s->ctx->method->version == DTLS_method()->version)
857 table = dtls_version_table;
858 else {
859 /* Unexpected state; fail closed. */
860 return 0;
861 }
862
863 for (vent = table; vent->version != 0; ++vent) {
864 if (vent->smeth != NULL &&
865 ssl_method_error(s, vent->smeth()) == 0)
866 return s->version == vent->version;
867 }
868 return 0;
869 }
870
871 /*
872 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
873 * protocols, provided the initial (D)TLS method is version-flexible. This
874 * function sanity-checks the proposed value and makes sure the method is
875 * version-flexible, then sets the limit if all is well.
876 *
877 * @method_version: The version of the current SSL_METHOD.
878 * @version: the intended limit.
879 * @bound: pointer to limit to be updated.
880 *
881 * Returns 1 on success, 0 on failure.
882 */
883 int ssl_set_version_bound(int method_version, int version, int *bound)
884 {
885 if (version == 0) {
886 *bound = version;
887 return 1;
888 }
889
890 /*-
891 * Restrict TLS methods to TLS protocol versions.
892 * Restrict DTLS methods to DTLS protocol versions.
893 * Note, DTLS version numbers are decreasing, use comparison macros.
894 *
895 * Note that for both lower-bounds we use explicit versions, not
896 * (D)TLS_MIN_VERSION. This is because we don't want to break user
897 * configurations. If the MIN (supported) version ever rises, the user's
898 * "floor" remains valid even if no longer available. We don't expect the
899 * MAX ceiling to ever get lower, so making that variable makes sense.
900 */
901 switch (method_version) {
902 default:
903 /*
904 * XXX For fixed version methods, should we always fail and not set any
905 * bounds, always succeed and not set any bounds, or set the bounds and
906 * arrange to fail later if they are not met? At present fixed-version
907 * methods are not subject to controls that disable individual protocol
908 * versions.
909 */
910 return 0;
911
912 case TLS_ANY_VERSION:
913 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
914 return 0;
915 break;
916
917 case DTLS_ANY_VERSION:
918 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
919 DTLS_VERSION_LT(version, DTLS1_VERSION))
920 return 0;
921 break;
922 }
923
924 *bound = version;
925 return 1;
926 }
927
928 /*
929 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
930 * client HELLO is received to select the final server protocol version and
931 * the version specific method.
932 *
933 * @s: server SSL handle.
934 *
935 * Returns 0 on success or an SSL error reason number on failure.
936 */
937 int ssl_choose_server_version(SSL *s)
938 {
939 /*-
940 * With version-flexible methods we have an initial state with:
941 *
942 * s->method->version == (D)TLS_ANY_VERSION,
943 * s->version == (D)TLS_MAX_VERSION.
944 *
945 * So we detect version-flexible methods via the method version, not the
946 * handle version.
947 */
948 int server_version = s->method->version;
949 int client_version = s->client_version;
950 const version_info *vent;
951 const version_info *table;
952 int disabled = 0;
953
954 switch (server_version) {
955 default:
956 if (version_cmp(s, client_version, s->version) < 0)
957 return SSL_R_WRONG_SSL_VERSION;
958 /*
959 * If this SSL handle is not from a version flexible method we don't
960 * (and never did) check min/max FIPS or Suite B constraints. Hope
961 * that's OK. It is up to the caller to not choose fixed protocol
962 * versions they don't want. If not, then easy to fix, just return
963 * ssl_method_error(s, s->method)
964 */
965 return 0;
966 case TLS_ANY_VERSION:
967 table = tls_version_table;
968 break;
969 case DTLS_ANY_VERSION:
970 table = dtls_version_table;
971 break;
972 }
973
974 for (vent = table; vent->version != 0; ++vent) {
975 const SSL_METHOD *method;
976
977 if (vent->smeth == NULL ||
978 version_cmp(s, client_version, vent->version) < 0)
979 continue;
980 method = vent->smeth();
981 if (ssl_method_error(s, method) == 0) {
982 s->version = vent->version;
983 s->method = method;
984 return 0;
985 }
986 disabled = 1;
987 }
988 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
989 }
990
991 /*
992 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
993 * server HELLO is received to select the final client protocol version and
994 * the version specific method.
995 *
996 * @s: client SSL handle.
997 * @version: The proposed version from the server's HELLO.
998 *
999 * Returns 0 on success or an SSL error reason number on failure.
1000 */
1001 int ssl_choose_client_version(SSL *s, int version)
1002 {
1003 const version_info *vent;
1004 const version_info *table;
1005
1006 switch (s->method->version) {
1007 default:
1008 if (version != s->version)
1009 return SSL_R_WRONG_SSL_VERSION;
1010 /*
1011 * If this SSL handle is not from a version flexible method we don't
1012 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1013 * that's OK. It is up to the caller to not choose fixed protocol
1014 * versions they don't want. If not, then easy to fix, just return
1015 * ssl_method_error(s, s->method)
1016 */
1017 return 0;
1018 case TLS_ANY_VERSION:
1019 table = tls_version_table;
1020 break;
1021 case DTLS_ANY_VERSION:
1022 table = dtls_version_table;
1023 break;
1024 }
1025
1026 for (vent = table; vent->version != 0; ++vent) {
1027 const SSL_METHOD *method;
1028 int err;
1029
1030 if (version != vent->version)
1031 continue;
1032 if (vent->cmeth == NULL)
1033 break;
1034 method = vent->cmeth();
1035 err = ssl_method_error(s, method);
1036 if (err != 0)
1037 return err;
1038 s->method = method;
1039 s->version = version;
1040 return 0;
1041 }
1042
1043 return SSL_R_UNSUPPORTED_PROTOCOL;
1044 }
1045
1046 /*
1047 * ssl_get_client_min_max_version - get minimum and maximum client version
1048 * @s: The SSL connection
1049 * @min_version: The minimum supported version
1050 * @max_version: The maximum supported version
1051 *
1052 * Work out what version we should be using for the initial ClientHello if the
1053 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1054 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1055 * or FIPS_mode() constraints and any floor imposed by the security level here,
1056 * so we don't advertise the wrong protocol version to only reject the outcome later.
1057 *
1058 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1059 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1060 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1061 *
1062 * Returns 0 on success or an SSL error reason number on failure. On failure
1063 * min_version and max_version will also be set to 0.
1064 */
1065 int ssl_get_client_min_max_version(const SSL *s, int *min_version, int *max_version)
1066 {
1067 int version;
1068 int hole;
1069 const SSL_METHOD *single = NULL;
1070 const SSL_METHOD *method;
1071 const version_info *table;
1072 const version_info *vent;
1073
1074 switch (s->method->version) {
1075 default:
1076 /*
1077 * If this SSL handle is not from a version flexible method we don't
1078 * (and never did) check min/max FIPS or Suite B constraints. Hope
1079 * that's OK. It is up to the caller to not choose fixed protocol
1080 * versions they don't want. If not, then easy to fix, just return
1081 * ssl_method_error(s, s->method)
1082 */
1083 *min_version = *max_version = s->version;
1084 return 0;
1085 case TLS_ANY_VERSION:
1086 table = tls_version_table;
1087 break;
1088 case DTLS_ANY_VERSION:
1089 table = dtls_version_table;
1090 break;
1091 }
1092
1093 /*
1094 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1095 * below X enabled. This is required in order to maintain the "version
1096 * capability" vector contiguous. Any versions with a NULL client method
1097 * (protocol version client is disabled at compile-time) is also a "hole".
1098 *
1099 * Our initial state is hole == 1, version == 0. That is, versions above
1100 * the first version in the method table are disabled (a "hole" above
1101 * the valid protocol entries) and we don't have a selected version yet.
1102 *
1103 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1104 * the selected version, and the method becomes a candidate "single"
1105 * method. We're no longer in a hole, so "hole" becomes 0.
1106 *
1107 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1108 * as we support a contiguous range of at least two methods. If we hit
1109 * a disabled method, then hole becomes true again, but nothing else
1110 * changes yet, because all the remaining methods may be disabled too.
1111 * If we again hit an enabled method after the new hole, it becomes
1112 * selected, as we start from scratch.
1113 */
1114 *min_version = version = 0;
1115 hole = 1;
1116 for (vent = table; vent->version != 0; ++vent) {
1117 /*
1118 * A table entry with a NULL client method is still a hole in the
1119 * "version capability" vector.
1120 */
1121 if (vent->cmeth == NULL) {
1122 hole = 1;
1123 continue;
1124 }
1125 method = vent->cmeth();
1126 if (ssl_method_error(s, method) != 0) {
1127 hole = 1;
1128 } else if (!hole) {
1129 single = NULL;
1130 *min_version = method->version;
1131 } else {
1132 version = (single = method)->version;
1133 *min_version = version;
1134 hole = 0;
1135 }
1136 }
1137
1138 *max_version = version;
1139
1140 /* Fail if everything is disabled */
1141 if (version == 0)
1142 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1143
1144 return 0;
1145 }
1146
1147 /*
1148 * ssl_set_client_hello_version - Work out what version we should be using for
1149 * the initial ClientHello.
1150 *
1151 * @s: client SSL handle.
1152 *
1153 * Returns 0 on success or an SSL error reason number on failure.
1154 */
1155 int ssl_set_client_hello_version(SSL *s)
1156 {
1157 int ver_min, ver_max, ret;
1158
1159 ret = ssl_get_client_min_max_version(s, &ver_min, &ver_max);
1160
1161 if (ret != 0)
1162 return ret;
1163
1164 s->client_version = s->version = ver_max;
1165 return 0;
1166 }