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