Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSLv3/TLSv1 shared functions |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | /* |
| 23 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 24 | * and became an IETF standard in 1999. |
| 25 | * |
| 26 | * http://wp.netscape.com/eng/ssl3/ |
| 27 | * http://www.ietf.org/rfc/rfc2246.txt |
| 28 | * http://www.ietf.org/rfc/rfc4346.txt |
| 29 | */ |
| 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 32 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 34 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 35 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 39 | #include "mbedtls/debug.h" |
| 40 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 41 | #include "mbedtls/ssl_internal.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 42 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 43 | #include <string.h> |
| 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 46 | defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 51 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 52 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 53 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 54 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #define mbedtls_free free |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 58 | /* Implementation that should never be optimized out by the compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | static void mbedtls_zeroize( void *v, size_t n ) { |
Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 60 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 61 | } |
| 62 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 63 | /* Length of the "epoch" field in the record header */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 65 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 67 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 68 | return( 2 ); |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 69 | #else |
| 70 | ((void) ssl); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 71 | #endif |
| 72 | return( 0 ); |
| 73 | } |
| 74 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Start a timer. |
| 77 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 78 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 80 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 81 | if( ssl->f_set_timer == NULL ) |
| 82 | return; |
| 83 | |
| 84 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 85 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Return -1 is timer is expired, 0 if it isn't. |
| 90 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | static int ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 92 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 93 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 94 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 95 | |
| 96 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 97 | { |
| 98 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 99 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 100 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 101 | |
| 102 | return( 0 ); |
| 103 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 105 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 106 | /* |
| 107 | * Double the retransmit timeout value, within the allowed range, |
| 108 | * returning -1 if the maximum value has already been reached. |
| 109 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 111 | { |
| 112 | uint32_t new_timeout; |
| 113 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 114 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 115 | return( -1 ); |
| 116 | |
| 117 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 118 | |
| 119 | /* Avoid arithmetic overflow and range overflow */ |
| 120 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 121 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 122 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 123 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ssl->handshake->retransmit_timeout = new_timeout; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs", |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 128 | ssl->handshake->retransmit_timeout ) ); |
| 129 | |
| 130 | return( 0 ); |
| 131 | } |
| 132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 134 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 135 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs", |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 137 | ssl->handshake->retransmit_timeout ) ); |
| 138 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Convert max_fragment_length codes to length. |
| 144 | * RFC 6066 says: |
| 145 | * enum{ |
| 146 | * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255) |
| 147 | * } MaxFragmentLength; |
| 148 | * and we add 0 -> extension unused |
| 149 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | static unsigned int mfl_code_to_length[MBEDTLS_SSL_MAX_FRAG_LEN_INVALID] = |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | MBEDTLS_SSL_MAX_CONTENT_LEN, /* MBEDTLS_SSL_MAX_FRAG_LEN_NONE */ |
| 153 | 512, /* MBEDTLS_SSL_MAX_FRAG_LEN_512 */ |
| 154 | 1024, /* MBEDTLS_SSL_MAX_FRAG_LEN_1024 */ |
| 155 | 2048, /* MBEDTLS_SSL_MAX_FRAG_LEN_2048 */ |
| 156 | 4096, /* MBEDTLS_SSL_MAX_FRAG_LEN_4096 */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 157 | }; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 159 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 160 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | static int ssl_session_copy( mbedtls_ssl_session *dst, const mbedtls_ssl_session *src ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_ssl_session_free( dst ); |
| 164 | memcpy( dst, src, sizeof( mbedtls_ssl_session ) ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 165 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 167 | if( src->peer_cert != NULL ) |
| 168 | { |
Paul Bakker | 2292d1f | 2013-09-15 17:06:49 +0200 | [diff] [blame] | 169 | int ret; |
| 170 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 171 | dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 172 | if( dst->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 173 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_x509_crt_init( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p, |
Manuel Pégourié-Gonnard | 4d2a8eb | 2014-06-13 20:33:27 +0200 | [diff] [blame] | 178 | src->peer_cert->raw.len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 179 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | mbedtls_free( dst->peer_cert ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 181 | dst->peer_cert = NULL; |
| 182 | return( ret ); |
| 183 | } |
| 184 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 186 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 187 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 188 | if( src->ticket != NULL ) |
| 189 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 190 | dst->ticket = mbedtls_calloc( 1, src->ticket_len ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 191 | if( dst->ticket == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 192 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 193 | |
| 194 | memcpy( dst->ticket, src->ticket, src->ticket_len ); |
| 195 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 196 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 197 | |
| 198 | return( 0 ); |
| 199 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 200 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 203 | int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl, |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 204 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 205 | size_t keylen, |
| 206 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 207 | size_t ivlen, |
| 208 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 209 | size_t maclen ) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL; |
| 211 | int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL; |
| 212 | int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL; |
| 213 | int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL; |
| 214 | int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL; |
| 215 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 216 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 217 | /* |
| 218 | * Key material generation |
| 219 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 221 | static int ssl3_prf( const unsigned char *secret, size_t slen, |
| 222 | const char *label, |
| 223 | const unsigned char *random, size_t rlen, |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 224 | unsigned char *dstbuf, size_t dlen ) |
| 225 | { |
| 226 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | mbedtls_md5_context mbedtls_md5; |
| 228 | mbedtls_sha1_context mbedtls_sha1; |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 229 | unsigned char padding[16]; |
| 230 | unsigned char sha1sum[20]; |
| 231 | ((void)label); |
| 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | mbedtls_md5_init( &mbedtls_md5 ); |
| 234 | mbedtls_sha1_init( &mbedtls_sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 235 | |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 236 | /* |
| 237 | * SSLv3: |
| 238 | * block = |
| 239 | * MD5( secret + SHA1( 'A' + secret + random ) ) + |
| 240 | * MD5( secret + SHA1( 'BB' + secret + random ) ) + |
| 241 | * MD5( secret + SHA1( 'CCC' + secret + random ) ) + |
| 242 | * ... |
| 243 | */ |
| 244 | for( i = 0; i < dlen / 16; i++ ) |
| 245 | { |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 246 | memset( padding, (unsigned char) ('A' + i), 1 + i ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | mbedtls_sha1_starts( &mbedtls_sha1 ); |
| 249 | mbedtls_sha1_update( &mbedtls_sha1, padding, 1 + i ); |
| 250 | mbedtls_sha1_update( &mbedtls_sha1, secret, slen ); |
| 251 | mbedtls_sha1_update( &mbedtls_sha1, random, rlen ); |
| 252 | mbedtls_sha1_finish( &mbedtls_sha1, sha1sum ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_md5_starts( &mbedtls_md5 ); |
| 255 | mbedtls_md5_update( &mbedtls_md5, secret, slen ); |
| 256 | mbedtls_md5_update( &mbedtls_md5, sha1sum, 20 ); |
| 257 | mbedtls_md5_finish( &mbedtls_md5, dstbuf + i * 16 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | mbedtls_md5_free( &mbedtls_md5 ); |
| 261 | mbedtls_sha1_free( &mbedtls_sha1 ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | mbedtls_zeroize( padding, sizeof( padding ) ); |
| 264 | mbedtls_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 265 | |
| 266 | return( 0 ); |
| 267 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 271 | static int tls1_prf( const unsigned char *secret, size_t slen, |
| 272 | const char *label, |
| 273 | const unsigned char *random, size_t rlen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 274 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 276 | size_t nb, hs; |
| 277 | size_t i, j, k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 278 | const unsigned char *S1, *S2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | unsigned char tmp[128]; |
| 280 | unsigned char h_i[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | const mbedtls_md_info_t *md_info; |
| 282 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 283 | int ret; |
| 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 286 | |
| 287 | if( sizeof( tmp ) < 20 + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 289 | |
| 290 | hs = ( slen + 1 ) / 2; |
| 291 | S1 = secret; |
| 292 | S2 = secret + slen - hs; |
| 293 | |
| 294 | nb = strlen( label ); |
| 295 | memcpy( tmp + 20, label, nb ); |
| 296 | memcpy( tmp + 20 + nb, random, rlen ); |
| 297 | nb += rlen; |
| 298 | |
| 299 | /* |
| 300 | * First compute P_md5(secret,label+random)[0..dlen] |
| 301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL ) |
| 303 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 306 | return( ret ); |
| 307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_md_hmac_starts( &md_ctx, S1, hs ); |
| 309 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 310 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 311 | |
| 312 | for( i = 0; i < dlen; i += 16 ) |
| 313 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 315 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb ); |
| 316 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 319 | mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 ); |
| 320 | mbedtls_md_hmac_finish( &md_ctx, 4 + tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | |
| 322 | k = ( i + 16 > dlen ) ? dlen % 16 : 16; |
| 323 | |
| 324 | for( j = 0; j < k; j++ ) |
| 325 | dstbuf[i + j] = h_i[j]; |
| 326 | } |
| 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 329 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | /* |
| 331 | * XOR out with P_sha1(secret,label+random)[0..dlen] |
| 332 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL ) |
| 334 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 337 | return( ret ); |
| 338 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | mbedtls_md_hmac_starts( &md_ctx, S2, hs ); |
| 340 | mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb ); |
| 341 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | |
| 343 | for( i = 0; i < dlen; i += 20 ) |
| 344 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 346 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb ); |
| 347 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 348 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 350 | mbedtls_md_hmac_update( &md_ctx, tmp, 20 ); |
| 351 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | |
| 353 | k = ( i + 20 > dlen ) ? dlen % 20 : 20; |
| 354 | |
| 355 | for( j = 0; j < k; j++ ) |
| 356 | dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] ); |
| 357 | } |
| 358 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 359 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | mbedtls_zeroize( tmp, sizeof( tmp ) ); |
| 362 | mbedtls_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | |
| 364 | return( 0 ); |
| 365 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 369 | static int tls_prf_generic( mbedtls_md_type_t md_type, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 370 | const unsigned char *secret, size_t slen, |
| 371 | const char *label, |
| 372 | const unsigned char *random, size_t rlen, |
| 373 | unsigned char *dstbuf, size_t dlen ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 374 | { |
| 375 | size_t nb; |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 376 | size_t i, j, k, md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 377 | unsigned char tmp[128]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 378 | unsigned char h_i[MBEDTLS_MD_MAX_SIZE]; |
| 379 | const mbedtls_md_info_t *md_info; |
| 380 | mbedtls_md_context_t md_ctx; |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 381 | int ret; |
| 382 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL ) |
| 386 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 387 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | md_len = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 389 | |
| 390 | if( sizeof( tmp ) < md_len + strlen( label ) + rlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 392 | |
| 393 | nb = strlen( label ); |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 394 | memcpy( tmp + md_len, label, nb ); |
| 395 | memcpy( tmp + md_len + nb, random, rlen ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 396 | nb += rlen; |
| 397 | |
| 398 | /* |
| 399 | * Compute P_<hash>(secret, label + random)[0..dlen] |
| 400 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 401 | if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 402 | return( ret ); |
| 403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | mbedtls_md_hmac_starts( &md_ctx, secret, slen ); |
| 405 | mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb ); |
| 406 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Manuel Pégourié-Gonnard | 7da726b | 2015-03-24 18:08:19 +0100 | [diff] [blame] | 407 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 408 | for( i = 0; i < dlen; i += md_len ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 409 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 410 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 411 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb ); |
| 412 | mbedtls_md_hmac_finish( &md_ctx, h_i ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 413 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 414 | mbedtls_md_hmac_reset ( &md_ctx ); |
| 415 | mbedtls_md_hmac_update( &md_ctx, tmp, md_len ); |
| 416 | mbedtls_md_hmac_finish( &md_ctx, tmp ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 417 | |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 418 | k = ( i + md_len > dlen ) ? dlen % md_len : md_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 419 | |
| 420 | for( j = 0; j < k; j++ ) |
| 421 | dstbuf[i + j] = h_i[j]; |
| 422 | } |
| 423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | b7fcca3 | 2015-03-26 11:41:28 +0100 | [diff] [blame] | 425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | mbedtls_zeroize( tmp, sizeof( tmp ) ); |
| 427 | mbedtls_zeroize( h_i, sizeof( h_i ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 428 | |
| 429 | return( 0 ); |
| 430 | } |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | #if defined(MBEDTLS_SHA256_C) |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 433 | static int tls_prf_sha256( const unsigned char *secret, size_t slen, |
| 434 | const char *label, |
| 435 | const unsigned char *random, size_t rlen, |
| 436 | unsigned char *dstbuf, size_t dlen ) |
| 437 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 439 | label, random, rlen, dstbuf, dlen ) ); |
| 440 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 441 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 442 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 444 | static int tls_prf_sha384( const unsigned char *secret, size_t slen, |
| 445 | const char *label, |
| 446 | const unsigned char *random, size_t rlen, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 447 | unsigned char *dstbuf, size_t dlen ) |
| 448 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen, |
Manuel Pégourié-Gonnard | 6890c6b | 2015-03-26 11:11:49 +0100 | [diff] [blame] | 450 | label, random, rlen, dstbuf, dlen ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 451 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | #endif /* MBEDTLS_SHA512_C */ |
| 453 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 454 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 456 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 458 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 459 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 460 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 461 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 462 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 463 | static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * ); |
| 464 | static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 465 | #endif |
| 466 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 467 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 468 | static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * ); |
| 469 | static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 470 | #endif |
| 471 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 473 | #if defined(MBEDTLS_SHA256_C) |
| 474 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 475 | static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * ); |
| 476 | static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 477 | #endif |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | #if defined(MBEDTLS_SHA512_C) |
| 480 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t ); |
| 481 | static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * ); |
| 482 | static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 483 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 488 | int ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 489 | unsigned char tmp[64]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 490 | unsigned char keyblk[256]; |
| 491 | unsigned char *key1; |
| 492 | unsigned char *key2; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 493 | unsigned char *mac_enc; |
| 494 | unsigned char *mac_dec; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 495 | size_t iv_copy_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | const mbedtls_cipher_info_t *cipher_info; |
| 497 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | mbedtls_ssl_session *session = ssl->session_negotiate; |
| 500 | mbedtls_ssl_transform *transform = ssl->transform_negotiate; |
| 501 | mbedtls_ssl_handshake_params *handshake = ssl->handshake; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 502 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 503 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 505 | cipher_info = mbedtls_cipher_info_from_type( transform->ciphersuite_info->cipher ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 506 | if( cipher_info == NULL ) |
| 507 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found", |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 509 | transform->ciphersuite_info->cipher ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | md_info = mbedtls_md_info_from_type( transform->ciphersuite_info->mac ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 514 | if( md_info == NULL ) |
| 515 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 516 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found", |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 517 | transform->ciphersuite_info->mac ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | /* |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 522 | * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 523 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 525 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 526 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 527 | handshake->tls_prf = ssl3_prf; |
| 528 | handshake->calc_verify = ssl_calc_verify_ssl; |
| 529 | handshake->calc_finished = ssl_calc_finished_ssl; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 530 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 531 | else |
| 532 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 533 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 534 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 535 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 536 | handshake->tls_prf = tls1_prf; |
| 537 | handshake->calc_verify = ssl_calc_verify_tls; |
| 538 | handshake->calc_finished = ssl_calc_finished_tls; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 539 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 540 | else |
| 541 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 543 | #if defined(MBEDTLS_SHA512_C) |
| 544 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 545 | transform->ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 546 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 547 | handshake->tls_prf = tls_prf_sha384; |
| 548 | handshake->calc_verify = ssl_calc_verify_tls_sha384; |
| 549 | handshake->calc_finished = ssl_calc_finished_tls_sha384; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 550 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 551 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 552 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | #if defined(MBEDTLS_SHA256_C) |
| 554 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 555 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 556 | handshake->tls_prf = tls_prf_sha256; |
| 557 | handshake->calc_verify = ssl_calc_verify_tls_sha256; |
| 558 | handshake->calc_finished = ssl_calc_finished_tls_sha256; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 559 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 560 | else |
| 561 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 563 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 564 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 565 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 566 | } |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 567 | |
| 568 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 569 | * SSLv3: |
| 570 | * master = |
| 571 | * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) + |
| 572 | * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) + |
| 573 | * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) ) |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 574 | * |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 575 | * TLSv1+: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 576 | * master = PRF( premaster, "master secret", randbytes )[0..47] |
| 577 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 578 | if( handshake->resume == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 581 | handshake->pmslen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 584 | if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 585 | { |
| 586 | unsigned char session_hash[48]; |
| 587 | size_t hash_len; |
| 588 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 589 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 590 | |
| 591 | ssl->handshake->calc_verify( ssl, session_hash ); |
| 592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 594 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 595 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | #if defined(MBEDTLS_SHA512_C) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 597 | if( ssl->transform_negotiate->ciphersuite_info->mac == |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | MBEDTLS_MD_SHA384 ) |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 599 | { |
| 600 | hash_len = 48; |
| 601 | } |
| 602 | else |
| 603 | #endif |
| 604 | hash_len = 32; |
| 605 | } |
| 606 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 608 | hash_len = 36; |
| 609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len ); |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 611 | |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 612 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 613 | "extended master secret", |
| 614 | session_hash, hash_len, |
| 615 | session->master, 48 ); |
| 616 | if( ret != 0 ) |
| 617 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 618 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 619 | return( ret ); |
| 620 | } |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 621 | |
| 622 | } |
| 623 | else |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 624 | #endif |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 625 | ret = handshake->tls_prf( handshake->premaster, handshake->pmslen, |
| 626 | "master secret", |
| 627 | handshake->randbytes, 64, |
| 628 | session->master, 48 ); |
| 629 | if( ret != 0 ) |
| 630 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 632 | return( ret ); |
| 633 | } |
Manuel Pégourié-Gonnard | ada3030 | 2014-10-20 20:33:10 +0200 | [diff] [blame] | 634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | mbedtls_zeroize( handshake->premaster, sizeof(handshake->premaster) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 636 | } |
| 637 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * Swap the client and server random values. |
| 642 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 643 | memcpy( tmp, handshake->randbytes, 64 ); |
| 644 | memcpy( handshake->randbytes, tmp + 32, 32 ); |
| 645 | memcpy( handshake->randbytes + 32, tmp, 32 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | mbedtls_zeroize( tmp, sizeof( tmp ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 647 | |
| 648 | /* |
| 649 | * SSLv3: |
| 650 | * key block = |
| 651 | * MD5( master + SHA1( 'A' + master + randbytes ) ) + |
| 652 | * MD5( master + SHA1( 'BB' + master + randbytes ) ) + |
| 653 | * MD5( master + SHA1( 'CCC' + master + randbytes ) ) + |
| 654 | * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) + |
| 655 | * ... |
| 656 | * |
| 657 | * TLSv1: |
| 658 | * key block = PRF( master, "key expansion", randbytes ) |
| 659 | */ |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 660 | ret = handshake->tls_prf( session->master, 48, "key expansion", |
| 661 | handshake->randbytes, 64, keyblk, 256 ); |
| 662 | if( ret != 0 ) |
| 663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret ); |
Manuel Pégourié-Gonnard | e960818 | 2015-03-26 11:47:47 +0100 | [diff] [blame] | 665 | return( ret ); |
| 666 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s", |
| 669 | mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) ); |
| 670 | MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 ); |
| 671 | MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 ); |
| 672 | MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | mbedtls_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 675 | |
| 676 | /* |
| 677 | * Determine the appropriate key, IV and MAC length. |
| 678 | */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 679 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 680 | transform->keylen = cipher_info->key_bitlen / 8; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | if( cipher_info->mode == MBEDTLS_MODE_GCM || |
| 683 | cipher_info->mode == MBEDTLS_MODE_CCM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 684 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 685 | transform->maclen = 0; |
| 686 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 687 | transform->ivlen = 12; |
| 688 | transform->fixed_ivlen = 4; |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 689 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 690 | /* Minimum length is expicit IV + tag */ |
| 691 | transform->minlen = transform->ivlen - transform->fixed_ivlen |
| 692 | + ( transform->ciphersuite_info->flags & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 694 | } |
| 695 | else |
| 696 | { |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 697 | int ret; |
| 698 | |
| 699 | /* Initialize HMAC contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 || |
| 701 | ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 704 | return( ret ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 705 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 706 | |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 707 | /* Get MAC length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | transform->maclen = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 711 | /* |
| 712 | * If HMAC is to be truncated, we shall keep the leftmost bytes, |
| 713 | * (rfc 6066 page 13 or rfc 2104 section 4), |
| 714 | * so we only need to adjust the length here. |
| 715 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 716 | if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED ) |
| 717 | transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN; |
| 718 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e800cd8 | 2014-06-18 15:34:40 +0200 | [diff] [blame] | 719 | |
| 720 | /* IV length */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 721 | transform->ivlen = cipher_info->iv_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 722 | |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 723 | /* Minimum length */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | if( cipher_info->mode == MBEDTLS_MODE_STREAM ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 725 | transform->minlen = transform->maclen; |
| 726 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 727 | { |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 728 | /* |
| 729 | * GenericBlockCipher: |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 730 | * 1. if EtM is in use: one block plus MAC |
| 731 | * otherwise: * first multiple of blocklen greater than maclen |
| 732 | * 2. IV except for SSL3 and TLS 1.0 |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 733 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 735 | if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 169dd6a | 2014-11-04 16:15:39 +0100 | [diff] [blame] | 736 | { |
| 737 | transform->minlen = transform->maclen |
| 738 | + cipher_info->block_size; |
| 739 | } |
| 740 | else |
| 741 | #endif |
| 742 | { |
| 743 | transform->minlen = transform->maclen |
| 744 | + cipher_info->block_size |
| 745 | - transform->maclen % cipher_info->block_size; |
| 746 | } |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 749 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 750 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 751 | ; /* No need to adjust minlen */ |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 752 | else |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 753 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 755 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 || |
| 756 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 ) |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 757 | { |
| 758 | transform->minlen += transform->ivlen; |
| 759 | } |
| 760 | else |
| 761 | #endif |
| 762 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 763 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 764 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | eaa76f7 | 2014-06-18 16:06:02 +0200 | [diff] [blame] | 765 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 766 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 769 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 770 | transform->keylen, transform->minlen, transform->ivlen, |
| 771 | transform->maclen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 772 | |
| 773 | /* |
| 774 | * Finally setup the cipher contexts, IVs and MAC secrets. |
| 775 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 776 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 777 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 778 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 779 | key1 = keyblk + transform->maclen * 2; |
| 780 | key2 = keyblk + transform->maclen * 2 + transform->keylen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 781 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 782 | mac_enc = keyblk; |
| 783 | mac_dec = keyblk + transform->maclen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 784 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 785 | /* |
| 786 | * This is not used in TLS v1.1. |
| 787 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 788 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 789 | transform->fixed_ivlen : transform->ivlen; |
| 790 | memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len ); |
| 791 | memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 792 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 793 | } |
| 794 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 796 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 797 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 798 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 799 | key1 = keyblk + transform->maclen * 2 + transform->keylen; |
| 800 | key2 = keyblk + transform->maclen * 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 801 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 802 | mac_enc = keyblk + transform->maclen; |
| 803 | mac_dec = keyblk; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 804 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 805 | /* |
| 806 | * This is not used in TLS v1.1. |
| 807 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 808 | iv_copy_len = ( transform->fixed_ivlen ) ? |
| 809 | transform->fixed_ivlen : transform->ivlen; |
| 810 | memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len ); |
| 811 | memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len, |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 812 | iv_copy_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 813 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 814 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 816 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 817 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 818 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 819 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 820 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 822 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 823 | { |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 824 | if( transform->maclen > sizeof transform->mac_enc ) |
| 825 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 826 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 827 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7cfdcb8 | 2014-01-18 18:22:55 +0100 | [diff] [blame] | 828 | } |
| 829 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 830 | memcpy( transform->mac_enc, mac_enc, transform->maclen ); |
| 831 | memcpy( transform->mac_dec, mac_dec, transform->maclen ); |
| 832 | } |
| 833 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 835 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 836 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 837 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 838 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen ); |
| 840 | mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 841 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 842 | else |
| 843 | #endif |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 845 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 846 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 847 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 848 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 849 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 850 | if( mbedtls_ssl_hw_record_init != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 851 | { |
| 852 | int ret = 0; |
| 853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 854 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 855 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 856 | if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, transform->keylen, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 857 | transform->iv_enc, transform->iv_dec, |
| 858 | iv_copy_len, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 859 | mac_enc, mac_dec, |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 860 | transform->maclen ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 861 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 862 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret ); |
| 863 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 867 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 868 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 869 | cipher_info ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 870 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 871 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 872 | return( ret ); |
| 873 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 874 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 875 | if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 876 | cipher_info ) ) != 0 ) |
| 877 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 878 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 879 | return( ret ); |
| 880 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 883 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 884 | MBEDTLS_ENCRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 885 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 887 | return( ret ); |
| 888 | } |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 889 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 890 | if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2, |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 891 | cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | MBEDTLS_DECRYPT ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 893 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 894 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 895 | return( ret ); |
| 896 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 898 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 899 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 900 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc, |
| 902 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 903 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 905 | return( ret ); |
Manuel Pégourié-Gonnard | 126a66f | 2013-10-25 18:33:32 +0200 | [diff] [blame] | 906 | } |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 908 | if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec, |
| 909 | MBEDTLS_PADDING_NONE ) ) != 0 ) |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret ); |
Manuel Pégourié-Gonnard | 8866591 | 2013-10-25 18:42:44 +0200 | [diff] [blame] | 912 | return( ret ); |
| 913 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 914 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 917 | mbedtls_zeroize( keyblk, sizeof( keyblk ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 919 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 920 | // Initialize compression |
| 921 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 922 | if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 923 | { |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 924 | if( ssl->compress_buf == NULL ) |
| 925 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 926 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) ); |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 927 | ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_BUFFER_LEN ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 928 | if( ssl->compress_buf == NULL ) |
| 929 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 930 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | MBEDTLS_SSL_BUFFER_LEN ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 932 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 936 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 937 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 938 | memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) ); |
| 939 | memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 940 | |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 941 | if( deflateInit( &transform->ctx_deflate, |
| 942 | Z_DEFAULT_COMPRESSION ) != Z_OK || |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 943 | inflateInit( &transform->ctx_inflate ) != Z_OK ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 944 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 945 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) ); |
| 946 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 949 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 950 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 951 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 952 | |
| 953 | return( 0 ); |
| 954 | } |
| 955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 956 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 957 | void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 958 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | mbedtls_md5_context mbedtls_md5; |
| 960 | mbedtls_sha1_context mbedtls_sha1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 961 | unsigned char pad_1[48]; |
| 962 | unsigned char pad_2[48]; |
| 963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | memcpy( &mbedtls_md5 , &ssl->handshake->fin_md5 , sizeof(mbedtls_md5_context) ); |
| 967 | memcpy( &mbedtls_sha1, &ssl->handshake->fin_sha1, sizeof(mbedtls_sha1_context) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 968 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 969 | memset( pad_1, 0x36, 48 ); |
| 970 | memset( pad_2, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | mbedtls_md5_update( &mbedtls_md5, ssl->session_negotiate->master, 48 ); |
| 973 | mbedtls_md5_update( &mbedtls_md5, pad_1, 48 ); |
| 974 | mbedtls_md5_finish( &mbedtls_md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 975 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | mbedtls_md5_starts( &mbedtls_md5 ); |
| 977 | mbedtls_md5_update( &mbedtls_md5, ssl->session_negotiate->master, 48 ); |
| 978 | mbedtls_md5_update( &mbedtls_md5, pad_2, 48 ); |
| 979 | mbedtls_md5_update( &mbedtls_md5, hash, 16 ); |
| 980 | mbedtls_md5_finish( &mbedtls_md5, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 982 | mbedtls_sha1_update( &mbedtls_sha1, ssl->session_negotiate->master, 48 ); |
| 983 | mbedtls_sha1_update( &mbedtls_sha1, pad_1, 40 ); |
| 984 | mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 985 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 986 | mbedtls_sha1_starts( &mbedtls_sha1 ); |
| 987 | mbedtls_sha1_update( &mbedtls_sha1, ssl->session_negotiate->master, 48 ); |
| 988 | mbedtls_sha1_update( &mbedtls_sha1, pad_2, 40 ); |
| 989 | mbedtls_sha1_update( &mbedtls_sha1, hash + 16, 20 ); |
| 990 | mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 991 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); |
| 993 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | mbedtls_md5_free( &mbedtls_md5 ); |
| 996 | mbedtls_sha1_free( &mbedtls_sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 997 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 998 | return; |
| 999 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1000 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1001 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1002 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 1003 | void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1004 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1005 | mbedtls_md5_context mbedtls_md5; |
| 1006 | mbedtls_sha1_context mbedtls_sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1009 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1010 | memcpy( &mbedtls_md5 , &ssl->handshake->fin_md5 , sizeof(mbedtls_md5_context) ); |
| 1011 | memcpy( &mbedtls_sha1, &ssl->handshake->fin_sha1, sizeof(mbedtls_sha1_context) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1013 | mbedtls_md5_finish( &mbedtls_md5, hash ); |
| 1014 | mbedtls_sha1_finish( &mbedtls_sha1, hash + 16 ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1015 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1016 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); |
| 1017 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1018 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1019 | mbedtls_md5_free( &mbedtls_md5 ); |
| 1020 | mbedtls_sha1_free( &mbedtls_sha1 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1021 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1022 | return; |
| 1023 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1024 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1025 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1026 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1027 | #if defined(MBEDTLS_SHA256_C) |
| 1028 | void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1029 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1030 | mbedtls_sha256_context mbedtls_sha256; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1031 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify mbedtls_sha256" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1033 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | memcpy( &mbedtls_sha256, &ssl->handshake->fin_sha256, sizeof(mbedtls_sha256_context) ); |
| 1035 | mbedtls_sha256_finish( &mbedtls_sha256, hash ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1037 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 ); |
| 1038 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | mbedtls_sha256_free( &mbedtls_sha256 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1041 | |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1042 | return; |
| 1043 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1046 | #if defined(MBEDTLS_SHA512_C) |
| 1047 | void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1049 | mbedtls_sha512_context mbedtls_sha512; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1051 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 1052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | memcpy( &mbedtls_sha512, &ssl->handshake->fin_sha512, sizeof(mbedtls_sha512_context) ); |
| 1054 | mbedtls_sha512_finish( &mbedtls_sha512, hash ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1056 | MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 ); |
| 1057 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | mbedtls_sha512_free( &mbedtls_sha512 ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 1060 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1061 | return; |
| 1062 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | #endif /* MBEDTLS_SHA512_C */ |
| 1064 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1065 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 1067 | int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1068 | { |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1069 | unsigned char *p = ssl->handshake->premaster; |
| 1070 | unsigned char *end = p + sizeof( ssl->handshake->premaster ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1071 | const unsigned char *psk = ssl->conf->psk; |
| 1072 | size_t psk_len = ssl->conf->psk_len; |
| 1073 | |
| 1074 | /* If the psk callback was called, use its result */ |
| 1075 | if( ssl->handshake->psk != NULL ) |
| 1076 | { |
| 1077 | psk = ssl->handshake->psk; |
| 1078 | psk_len = ssl->handshake->psk_len; |
| 1079 | } |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1080 | |
| 1081 | /* |
| 1082 | * PMS = struct { |
| 1083 | * opaque other_secret<0..2^16-1>; |
| 1084 | * opaque psk<0..2^16-1>; |
| 1085 | * }; |
| 1086 | * with "other_secret" depending on the particular key exchange |
| 1087 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1088 | #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) |
| 1089 | if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1090 | { |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1091 | if( end - p < 2 + (int) psk_len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1093 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1094 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1095 | *(p++) = (unsigned char)( psk_len ); |
| 1096 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1097 | } |
| 1098 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1099 | #endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */ |
| 1100 | #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) |
| 1101 | if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
Manuel Pégourié-Gonnard | 0fae60b | 2013-10-14 17:39:48 +0200 | [diff] [blame] | 1102 | { |
| 1103 | /* |
| 1104 | * other_secret already set by the ClientKeyExchange message, |
| 1105 | * and is 48 bytes long |
| 1106 | */ |
| 1107 | *p++ = 0; |
| 1108 | *p++ = 48; |
| 1109 | p += 48; |
| 1110 | } |
| 1111 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1112 | #endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */ |
| 1113 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED) |
| 1114 | if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1115 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1116 | int ret; |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1117 | size_t len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1118 | |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1119 | /* Write length only when we know the actual value */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1120 | if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 1121 | p + 2, end - ( p + 2 ), &len, |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1122 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1123 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1124 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1125 | return( ret ); |
| 1126 | } |
Manuel Pégourié-Gonnard | 8df6863 | 2014-06-23 17:56:08 +0200 | [diff] [blame] | 1127 | *(p++) = (unsigned char)( len >> 8 ); |
| 1128 | *(p++) = (unsigned char)( len ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1129 | p += len; |
| 1130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1132 | } |
| 1133 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1134 | #endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */ |
| 1135 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) |
| 1136 | if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1137 | { |
Manuel Pégourié-Gonnard | 1b62c7f | 2013-10-14 14:02:19 +0200 | [diff] [blame] | 1138 | int ret; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1139 | size_t zlen; |
| 1140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen, |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1142 | p + 2, end - ( p + 2 ), |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1143 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1145 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1146 | return( ret ); |
| 1147 | } |
| 1148 | |
| 1149 | *(p++) = (unsigned char)( zlen >> 8 ); |
| 1150 | *(p++) = (unsigned char)( zlen ); |
| 1151 | p += zlen; |
| 1152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | MBEDTLS_SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1154 | } |
| 1155 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1156 | #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1158 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1159 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* opaque psk<0..2^16-1>; */ |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1163 | if( end - p < 2 + (int) psk_len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 1165 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 1166 | *(p++) = (unsigned char)( psk_len >> 8 ); |
| 1167 | *(p++) = (unsigned char)( psk_len ); |
| 1168 | memcpy( p, psk, psk_len ); |
| 1169 | p += psk_len; |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1170 | |
| 1171 | ssl->handshake->pmslen = p - ssl->handshake->premaster; |
| 1172 | |
| 1173 | return( 0 ); |
| 1174 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Manuel Pégourié-Gonnard | bd1ae24 | 2013-10-14 13:09:25 +0200 | [diff] [blame] | 1176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1178 | /* |
| 1179 | * SSLv3.0 MAC functions |
| 1180 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1181 | static void ssl_mac( mbedtls_md_context_t *md_ctx, unsigned char *secret, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1182 | unsigned char *buf, size_t len, |
| 1183 | unsigned char *ctr, int type ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1184 | { |
| 1185 | unsigned char header[11]; |
| 1186 | unsigned char padding[48]; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1187 | int padlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | int md_size = mbedtls_md_get_size( md_ctx->md_info ); |
| 1189 | int md_type = mbedtls_md_get_type( md_ctx->md_info ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1190 | |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1191 | /* Only MD5 and SHA-1 supported */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1192 | if( md_type == MBEDTLS_MD_MD5 ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1193 | padlen = 48; |
Manuel Pégourié-Gonnard | 8d4ad07 | 2014-07-13 14:43:28 +0200 | [diff] [blame] | 1194 | else |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1195 | padlen = 40; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1196 | |
| 1197 | memcpy( header, ctr, 8 ); |
| 1198 | header[ 8] = (unsigned char) type; |
| 1199 | header[ 9] = (unsigned char)( len >> 8 ); |
| 1200 | header[10] = (unsigned char)( len ); |
| 1201 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1202 | memset( padding, 0x36, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | mbedtls_md_starts( md_ctx ); |
| 1204 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1205 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1206 | mbedtls_md_update( md_ctx, header, 11 ); |
| 1207 | mbedtls_md_update( md_ctx, buf, len ); |
| 1208 | mbedtls_md_finish( md_ctx, buf + len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1209 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1210 | memset( padding, 0x5C, padlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | mbedtls_md_starts( md_ctx ); |
| 1212 | mbedtls_md_update( md_ctx, secret, md_size ); |
| 1213 | mbedtls_md_update( md_ctx, padding, padlen ); |
| 1214 | mbedtls_md_update( md_ctx, buf + len, md_size ); |
| 1215 | mbedtls_md_finish( md_ctx, buf + len ); |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1216 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1217 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5f70b25 | 2012-09-13 14:23:06 +0000 | [diff] [blame] | 1218 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1219 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \ |
| 1220 | ( defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 1221 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) ) |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1222 | #define SSL_SOME_MODES_USE_MAC |
Manuel Pégourié-Gonnard | 8e4b337 | 2014-11-17 15:06:13 +0100 | [diff] [blame] | 1223 | #endif |
| 1224 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1225 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1226 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 1227 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1228 | static int ssl_encrypt_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1229 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1230 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1231 | int auth_done = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1233 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1234 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1235 | if( ssl->session_out == NULL || ssl->transform_out == NULL ) |
| 1236 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1237 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1238 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1239 | } |
| 1240 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1241 | mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1243 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 1244 | ssl->out_msg, ssl->out_msglen ); |
| 1245 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1246 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1247 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1248 | */ |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1249 | #if defined(SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | if( mode == MBEDTLS_MODE_STREAM || |
| 1251 | ( mode == MBEDTLS_MODE_CBC |
| 1252 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1253 | && ssl->session_out->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1254 | #endif |
| 1255 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1256 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1257 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1258 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1259 | { |
| 1260 | ssl_mac( &ssl->transform_out->md_ctx_enc, |
| 1261 | ssl->transform_out->mac_enc, |
| 1262 | ssl->out_msg, ssl->out_msglen, |
| 1263 | ssl->out_ctr, ssl->out_msgtype ); |
| 1264 | } |
| 1265 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1266 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1267 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1268 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1269 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1270 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1271 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 ); |
| 1272 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 ); |
| 1273 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 ); |
| 1274 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1275 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1276 | mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1277 | ssl->out_msg + ssl->out_msglen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1278 | mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1279 | } |
| 1280 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1281 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1282 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1283 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1284 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1287 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1288 | ssl->out_msg + ssl->out_msglen, |
| 1289 | ssl->transform_out->maclen ); |
| 1290 | |
| 1291 | ssl->out_msglen += ssl->transform_out->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1292 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1293 | } |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1294 | #endif /* AEAD not the only option */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1295 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1296 | /* |
| 1297 | * Encrypt |
| 1298 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1299 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 1300 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1301 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1302 | int ret; |
| 1303 | size_t olen = 0; |
| 1304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1305 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1306 | "including %d bytes of padding", |
| 1307 | ssl->out_msglen, 0 ) ); |
| 1308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1310 | ssl->transform_out->iv_enc, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1311 | ssl->transform_out->ivlen, |
| 1312 | ssl->out_msg, ssl->out_msglen, |
| 1313 | ssl->out_msg, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1314 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1316 | return( ret ); |
| 1317 | } |
| 1318 | |
| 1319 | if( ssl->out_msglen != olen ) |
| 1320 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1321 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1322 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1323 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1324 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1325 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
| 1327 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
| 1328 | if( mode == MBEDTLS_MODE_GCM || |
| 1329 | mode == MBEDTLS_MODE_CCM ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1330 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1331 | int ret; |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1332 | size_t enc_msglen, olen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1333 | unsigned char *enc_msg; |
| 1334 | unsigned char add_data[13]; |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1335 | unsigned char taglen = ssl->transform_out->ciphersuite_info->flags & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1336 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1337 | |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1338 | memcpy( add_data, ssl->out_ctr, 8 ); |
| 1339 | add_data[8] = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1340 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1341 | ssl->conf->transport, add_data + 9 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1342 | add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF; |
| 1343 | add_data[12] = ssl->out_msglen & 0xFF; |
| 1344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1345 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1346 | add_data, 13 ); |
| 1347 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1348 | /* |
| 1349 | * Generate IV |
| 1350 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1351 | #if defined(MBEDTLS_SSL_AEAD_RANDOM_IV) |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1352 | ret = ssl->conf->f_rng( ssl->conf->p_rng, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1353 | ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen, |
| 1354 | ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1355 | if( ret != 0 ) |
| 1356 | return( ret ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1357 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1358 | memcpy( ssl->out_iv, |
| 1359 | ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen, |
| 1360 | ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1361 | #else |
| 1362 | if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 ) |
| 1363 | { |
| 1364 | /* Reminder if we ever add an AEAD mode with a different size */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1366 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen, |
| 1370 | ssl->out_ctr, 8 ); |
| 1371 | memcpy( ssl->out_iv, ssl->out_ctr, 8 ); |
| 1372 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1373 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1375 | ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen ); |
Manuel Pégourié-Gonnard | 226d5da | 2013-09-05 13:19:22 +0200 | [diff] [blame] | 1376 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1377 | /* |
| 1378 | * Fix pointer positions and message length with added IV |
| 1379 | */ |
| 1380 | enc_msg = ssl->out_msg; |
| 1381 | enc_msglen = ssl->out_msglen; |
| 1382 | ssl->out_msglen += ssl->transform_out->ivlen - |
| 1383 | ssl->transform_out->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1385 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1386 | "including %d bytes of padding", |
| 1387 | ssl->out_msglen, 0 ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1388 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1389 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1390 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1391 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | if( ( ret = mbedtls_cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc, |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1393 | ssl->transform_out->iv_enc, |
| 1394 | ssl->transform_out->ivlen, |
| 1395 | add_data, 13, |
| 1396 | enc_msg, enc_msglen, |
| 1397 | enc_msg, &olen, |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1398 | enc_msg + enc_msglen, taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1399 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1400 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1401 | return( ret ); |
| 1402 | } |
| 1403 | |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1404 | if( olen != enc_msglen ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1405 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1406 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1407 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1410 | ssl->out_msglen += taglen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1411 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1413 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1414 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1415 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1416 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 1417 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 1418 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) |
| 1419 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1420 | { |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1421 | int ret; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1422 | unsigned char *enc_msg; |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 1423 | size_t enc_msglen, padlen, olen = 0, i; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1424 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1425 | padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) % |
| 1426 | ssl->transform_out->ivlen; |
| 1427 | if( padlen == ssl->transform_out->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1428 | padlen = 0; |
| 1429 | |
| 1430 | for( i = 0; i <= padlen; i++ ) |
| 1431 | ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen; |
| 1432 | |
| 1433 | ssl->out_msglen += padlen + 1; |
| 1434 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1435 | enc_msglen = ssl->out_msglen; |
| 1436 | enc_msg = ssl->out_msg; |
| 1437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1439 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1440 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 1441 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1442 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1443 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1444 | { |
| 1445 | /* |
| 1446 | * Generate IV |
| 1447 | */ |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 1448 | int ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->transform_out->iv_enc, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1449 | ssl->transform_out->ivlen ); |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1450 | if( ret != 0 ) |
| 1451 | return( ret ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1452 | |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1453 | memcpy( ssl->out_iv, ssl->transform_out->iv_enc, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1454 | ssl->transform_out->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1455 | |
| 1456 | /* |
| 1457 | * Fix pointer positions and message length with added IV |
| 1458 | */ |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1459 | enc_msg = ssl->out_msg; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1460 | enc_msglen = ssl->out_msglen; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1461 | ssl->out_msglen += ssl->transform_out->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1462 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1463 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1465 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, " |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1466 | "including %d bytes of IV and %d bytes of padding", |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1467 | ssl->out_msglen, ssl->transform_out->ivlen, |
| 1468 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1470 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_out->cipher_ctx_enc, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1471 | ssl->transform_out->iv_enc, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1472 | ssl->transform_out->ivlen, |
| 1473 | enc_msg, enc_msglen, |
| 1474 | enc_msg, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1475 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1477 | return( ret ); |
| 1478 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1479 | |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1480 | if( enc_msglen != olen ) |
| 1481 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1482 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1483 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1484 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1486 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 1487 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1488 | { |
| 1489 | /* |
| 1490 | * Save IV in SSL3 and TLS1 |
| 1491 | */ |
| 1492 | memcpy( ssl->transform_out->iv_enc, |
| 1493 | ssl->transform_out->cipher_ctx_enc.iv, |
| 1494 | ssl->transform_out->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1495 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1496 | #endif |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1498 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1499 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1500 | { |
| 1501 | /* |
| 1502 | * MAC(MAC_write_key, seq_num + |
| 1503 | * TLSCipherText.type + |
| 1504 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1505 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1506 | * IV + // except for TLS 1.0 |
| 1507 | * ENC(content + padding + padding_length)); |
| 1508 | */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1509 | unsigned char pseudo_hdr[13]; |
| 1510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1512 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1513 | memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 ); |
| 1514 | memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1515 | pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF ); |
| 1516 | pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF ); |
| 1517 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1518 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 ); |
| 1521 | mbedtls_md_hmac_update( &ssl->transform_out->md_ctx_enc, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1522 | ssl->out_iv, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1523 | mbedtls_md_hmac_finish( &ssl->transform_out->md_ctx_enc, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1524 | ssl->out_iv + ssl->out_msglen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1525 | mbedtls_md_hmac_reset( &ssl->transform_out->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1526 | |
| 1527 | ssl->out_msglen += ssl->transform_out->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1528 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1529 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1530 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1531 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1532 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1533 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
| 1534 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1535 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1537 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1538 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1539 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1540 | /* Make extra sure authentication was performed, exactly once */ |
| 1541 | if( auth_done != 1 ) |
| 1542 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1544 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1548 | |
| 1549 | return( 0 ); |
| 1550 | } |
| 1551 | |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1552 | #define SSL_MAX_MAC_SIZE 48 |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 1553 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1554 | static int ssl_decrypt_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1555 | { |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1556 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1558 | int auth_done = 0; |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1559 | #if defined(SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1560 | size_t padlen = 0, correct = 1; |
| 1561 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1562 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1563 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1564 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1565 | if( ssl->session_in == NULL || ssl->transform_in == NULL ) |
| 1566 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1567 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1568 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1569 | } |
| 1570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | mode = mbedtls_cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1572 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1573 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1574 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1575 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1576 | ssl->in_msglen, ssl->transform_in->minlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1580 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
| 1581 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1582 | { |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1583 | int ret; |
| 1584 | size_t olen = 0; |
| 1585 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1586 | padlen = 0; |
| 1587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1588 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1589 | ssl->transform_in->iv_dec, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1590 | ssl->transform_in->ivlen, |
| 1591 | ssl->in_msg, ssl->in_msglen, |
| 1592 | ssl->in_msg, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1593 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1595 | return( ret ); |
| 1596 | } |
| 1597 | |
| 1598 | if( ssl->in_msglen != olen ) |
| 1599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1600 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1601 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1602 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1603 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1604 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1605 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
| 1606 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) |
| 1607 | if( mode == MBEDTLS_MODE_GCM || |
| 1608 | mode == MBEDTLS_MODE_CCM ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1609 | { |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1610 | int ret; |
| 1611 | size_t dec_msglen, olen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1612 | unsigned char *dec_msg; |
| 1613 | unsigned char *dec_msg_result; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1614 | unsigned char add_data[13]; |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1615 | unsigned char taglen = ssl->transform_in->ciphersuite_info->flags & |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1616 | MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16; |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1617 | unsigned char explicit_iv_len = ssl->transform_in->ivlen - |
| 1618 | ssl->transform_in->fixed_ivlen; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1619 | |
Manuel Pégourié-Gonnard | 06d7519 | 2015-02-11 14:54:11 +0000 | [diff] [blame] | 1620 | if( ssl->in_msglen < (size_t) explicit_iv_len + taglen ) |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1622 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) " |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1623 | "+ taglen (%d)", ssl->in_msglen, |
| 1624 | explicit_iv_len, taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1626 | } |
| 1627 | dec_msglen = ssl->in_msglen - explicit_iv_len - taglen; |
| 1628 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1629 | dec_msg = ssl->in_msg; |
| 1630 | dec_msg_result = ssl->in_msg; |
| 1631 | ssl->in_msglen = dec_msglen; |
| 1632 | |
| 1633 | memcpy( add_data, ssl->in_ctr, 8 ); |
| 1634 | add_data[8] = ssl->in_msgtype; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1635 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1636 | ssl->conf->transport, add_data + 9 ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1637 | add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF; |
| 1638 | add_data[12] = ssl->in_msglen & 0xFF; |
| 1639 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1641 | add_data, 13 ); |
| 1642 | |
| 1643 | memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen, |
| 1644 | ssl->in_iv, |
| 1645 | ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen ); |
| 1646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec, |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1648 | ssl->transform_in->ivlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1649 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1650 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1651 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1652 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1653 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1654 | if( ( ret = mbedtls_cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec, |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1655 | ssl->transform_in->iv_dec, |
| 1656 | ssl->transform_in->ivlen, |
| 1657 | add_data, 13, |
| 1658 | dec_msg, dec_msglen, |
| 1659 | dec_msg_result, &olen, |
Manuel Pégourié-Gonnard | 2e5ee32 | 2014-05-14 13:09:22 +0200 | [diff] [blame] | 1660 | dec_msg + dec_msglen, taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1661 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1662 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 1665 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1666 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1667 | return( ret ); |
| 1668 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1669 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1670 | |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1671 | if( olen != dec_msglen ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1672 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1673 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1674 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1675 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1676 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1677 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1678 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 1679 | #if defined(MBEDTLS_CIPHER_MODE_CBC) && \ |
| 1680 | ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) ) |
| 1681 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1682 | { |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1683 | /* |
| 1684 | * Decrypt and check the padding |
| 1685 | */ |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1686 | int ret; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1687 | unsigned char *dec_msg; |
| 1688 | unsigned char *dec_msg_result; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1689 | size_t dec_msglen; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1690 | size_t minlen = 0; |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1691 | size_t olen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1692 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1693 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1694 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1695 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1696 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1697 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1698 | minlen += ssl->transform_in->ivlen; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1699 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1700 | |
| 1701 | if( ssl->in_msglen < minlen + ssl->transform_in->ivlen || |
| 1702 | ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 ) |
| 1703 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1704 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) " |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 1705 | "+ 1 ) ( + expl IV )", ssl->in_msglen, |
| 1706 | ssl->transform_in->ivlen, |
| 1707 | ssl->transform_in->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1709 | } |
| 1710 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1711 | dec_msglen = ssl->in_msglen; |
| 1712 | dec_msg = ssl->in_msg; |
| 1713 | dec_msg_result = ssl->in_msg; |
| 1714 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1715 | /* |
| 1716 | * Authenticate before decrypt if enabled |
| 1717 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1718 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1719 | if( ssl->session_in->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1720 | { |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1721 | unsigned char computed_mac[SSL_MAX_MAC_SIZE]; |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1722 | unsigned char pseudo_hdr[13]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1725 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1726 | dec_msglen -= ssl->transform_in->maclen; |
| 1727 | ssl->in_msglen -= ssl->transform_in->maclen; |
| 1728 | |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1729 | memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 ); |
| 1730 | memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 ); |
| 1731 | pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF ); |
| 1732 | pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF ); |
| 1733 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1734 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1736 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 ); |
| 1737 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1738 | ssl->in_iv, ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1739 | mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac ); |
| 1740 | mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1741 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1743 | ssl->transform_in->maclen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1744 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", computed_mac, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1745 | ssl->transform_in->maclen ); |
| 1746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1747 | if( mbedtls_ssl_safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac, |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1748 | ssl->transform_in->maclen ) != 0 ) |
| 1749 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1750 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1751 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1752 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1753 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1754 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1755 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1756 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1757 | |
| 1758 | /* |
| 1759 | * Check length sanity |
| 1760 | */ |
| 1761 | if( ssl->in_msglen % ssl->transform_in->ivlen != 0 ) |
| 1762 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1763 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0", |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1764 | ssl->in_msglen, ssl->transform_in->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1766 | } |
| 1767 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1768 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1769 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1770 | * Initialize for prepended IV for block cipher in TLS v1.1 and up |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1771 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1772 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1773 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1774 | dec_msglen -= ssl->transform_in->ivlen; |
| 1775 | ssl->in_msglen -= ssl->transform_in->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1776 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1777 | for( i = 0; i < ssl->transform_in->ivlen; i++ ) |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 1778 | ssl->transform_in->iv_dec[i] = ssl->in_iv[i]; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1779 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1780 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1781 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1782 | if( ( ret = mbedtls_cipher_crypt( &ssl->transform_in->cipher_ctx_dec, |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1783 | ssl->transform_in->iv_dec, |
Manuel Pégourié-Gonnard | 8764d27 | 2014-05-13 11:52:02 +0200 | [diff] [blame] | 1784 | ssl->transform_in->ivlen, |
| 1785 | dec_msg, dec_msglen, |
| 1786 | dec_msg_result, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1787 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1788 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1789 | return( ret ); |
| 1790 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1791 | |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1792 | if( dec_msglen != olen ) |
| 1793 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1794 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1795 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1796 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1797 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1798 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
| 1799 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1800 | { |
| 1801 | /* |
| 1802 | * Save IV in SSL3 and TLS1 |
| 1803 | */ |
| 1804 | memcpy( ssl->transform_in->iv_dec, |
| 1805 | ssl->transform_in->cipher_ctx_dec.iv, |
| 1806 | ssl->transform_in->ivlen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1807 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1808 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1809 | |
| 1810 | padlen = 1 + ssl->in_msg[ssl->in_msglen - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1811 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1812 | if( ssl->in_msglen < ssl->transform_in->maclen + padlen && |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1813 | auth_done == 0 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1814 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1815 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1816 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)", |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1817 | ssl->in_msglen, ssl->transform_in->maclen, padlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1818 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1819 | padlen = 0; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1820 | correct = 0; |
| 1821 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1822 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1823 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1824 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1825 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1826 | if( padlen > ssl->transform_in->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1828 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1829 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1830 | "should be no more than %d", |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1831 | padlen, ssl->transform_in->ivlen ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1832 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1833 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1837 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1838 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1839 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1840 | if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1841 | { |
| 1842 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1843 | * TLSv1+: always check the padding up to the first failure |
| 1844 | * and fake check up to 256 bytes of padding |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1845 | */ |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 1846 | size_t pad_count = 0, real_count = 1; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1847 | size_t padding_idx = ssl->in_msglen - padlen - 1; |
| 1848 | |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 1849 | /* |
| 1850 | * Padding is guaranteed to be incorrect if: |
Paul Bakker | 91c61bc | 2014-03-26 14:06:55 +0100 | [diff] [blame] | 1851 | * 1. padlen >= ssl->in_msglen |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 1852 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1853 | * 2. padding_idx >= MBEDTLS_SSL_MAX_CONTENT_LEN + |
Paul Bakker | 61885c7 | 2014-04-25 12:59:03 +0200 | [diff] [blame] | 1854 | * ssl->transform_in->maclen |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 1855 | * |
| 1856 | * In both cases we reset padding_idx to a safe value (0) to |
| 1857 | * prevent out-of-buffer reads. |
| 1858 | */ |
Paul Bakker | 91c61bc | 2014-03-26 14:06:55 +0100 | [diff] [blame] | 1859 | correct &= ( ssl->in_msglen >= padlen + 1 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1860 | correct &= ( padding_idx < MBEDTLS_SSL_MAX_CONTENT_LEN + |
Paul Bakker | 61885c7 | 2014-04-25 12:59:03 +0200 | [diff] [blame] | 1861 | ssl->transform_in->maclen ); |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 1862 | |
| 1863 | padding_idx *= correct; |
| 1864 | |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 1865 | for( i = 1; i <= 256; i++ ) |
| 1866 | { |
| 1867 | real_count &= ( i <= padlen ); |
| 1868 | pad_count += real_count * |
| 1869 | ( ssl->in_msg[padding_idx + i] == padlen - 1 ); |
| 1870 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1871 | |
| 1872 | correct &= ( pad_count == padlen ); /* Only 1 on correct padding */ |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1873 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1874 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1875 | if( padlen > 0 && correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1876 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1877 | #endif |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1878 | padlen &= correct * 0x1FF; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1879 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1880 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1881 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 1882 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1883 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1884 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1885 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1886 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1887 | |
| 1888 | ssl->in_msglen -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1889 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1890 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1891 | #endif /* MBEDTLS_CIPHER_MODE_CBC && |
| 1892 | ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C ) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1893 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1894 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1895 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1896 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1897 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1898 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1899 | ssl->in_msg, ssl->in_msglen ); |
| 1900 | |
| 1901 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1902 | * Authenticate if not done yet. |
| 1903 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1904 | */ |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1905 | #if defined(SSL_SOME_MODES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1906 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1907 | { |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1908 | unsigned char tmp[SSL_MAX_MAC_SIZE]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1909 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1910 | ssl->in_msglen -= ssl->transform_in->maclen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1911 | |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 1912 | ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 ); |
| 1913 | ssl->in_len[1] = (unsigned char)( ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1914 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1915 | memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1918 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1919 | { |
| 1920 | ssl_mac( &ssl->transform_in->md_ctx_dec, |
| 1921 | ssl->transform_in->mac_dec, |
| 1922 | ssl->in_msg, ssl->in_msglen, |
| 1923 | ssl->in_ctr, ssl->in_msgtype ); |
| 1924 | } |
| 1925 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1926 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1927 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1928 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1929 | if( ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1930 | { |
| 1931 | /* |
| 1932 | * Process MAC and always update for padlen afterwards to make |
| 1933 | * total time independent of padlen |
| 1934 | * |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1935 | * extra_run compensates MAC check for padlen |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1936 | * |
| 1937 | * Known timing attacks: |
| 1938 | * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf) |
| 1939 | * |
| 1940 | * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values |
| 1941 | * correctly. (We round down instead of up, so -56 is the correct |
| 1942 | * value for our calculations instead of -55) |
| 1943 | */ |
| 1944 | size_t j, extra_run = 0; |
| 1945 | extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 - |
| 1946 | ( 13 + ssl->in_msglen + 8 ) / 64; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1947 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1948 | extra_run &= correct * 0xFF; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1950 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 ); |
| 1951 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 ); |
| 1952 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 ); |
| 1953 | mbedtls_md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1954 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1955 | mbedtls_md_hmac_finish( &ssl->transform_in->md_ctx_dec, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1956 | ssl->in_msg + ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 1957 | /* Call mbedtls_md_process at least once due to cache attacks */ |
Manuel Pégourié-Gonnard | 47fede0 | 2015-04-29 01:35:48 +0200 | [diff] [blame] | 1958 | for( j = 0; j < extra_run + 1; j++ ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1959 | mbedtls_md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1961 | mbedtls_md_hmac_reset( &ssl->transform_in->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1962 | } |
| 1963 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1964 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 1965 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1966 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1967 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1968 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1969 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1971 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen ); |
| 1972 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1973 | ssl->transform_in->maclen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1974 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1975 | if( mbedtls_ssl_safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen, |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1976 | ssl->transform_in->maclen ) != 0 ) |
| 1977 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1978 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1979 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1980 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1981 | correct = 0; |
| 1982 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1983 | auth_done++; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1984 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1985 | /* |
| 1986 | * Finally check the correct flag |
| 1987 | */ |
| 1988 | if( correct == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1989 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1990 | } |
Manuel Pégourié-Gonnard | 8408a94 | 2015-04-09 12:14:31 +0200 | [diff] [blame] | 1991 | #endif /* SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1992 | |
| 1993 | /* Make extra sure authentication was performed, exactly once */ |
| 1994 | if( auth_done != 1 ) |
| 1995 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1996 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1997 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1998 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1999 | |
| 2000 | if( ssl->in_msglen == 0 ) |
| 2001 | { |
| 2002 | ssl->nb_zero++; |
| 2003 | |
| 2004 | /* |
| 2005 | * Three or more empty messages may be a DoS attack |
| 2006 | * (excessive CPU consumption). |
| 2007 | */ |
| 2008 | if( ssl->nb_zero > 3 ) |
| 2009 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2010 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2011 | "messages, possible DoS attack" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2012 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | else |
| 2016 | ssl->nb_zero = 0; |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 2017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2018 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2019 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2020 | { |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 2021 | ; /* in_ctr read from peer, not maintained internally */ |
Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2022 | } |
| 2023 | else |
| 2024 | #endif |
| 2025 | { |
| 2026 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
| 2027 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 2028 | break; |
| 2029 | |
| 2030 | /* The loop goes to its end iff the counter is wrapping */ |
| 2031 | if( i == ssl_ep_len( ssl ) ) |
| 2032 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2033 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 2034 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | ea22ce5 | 2014-09-24 09:46:10 +0200 | [diff] [blame] | 2035 | } |
Manuel Pégourié-Gonnard | 83cdffc | 2014-03-10 21:20:29 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2038 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2039 | |
| 2040 | return( 0 ); |
| 2041 | } |
| 2042 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 2043 | #undef MAC_NONE |
| 2044 | #undef MAC_PLAINTEXT |
| 2045 | #undef MAC_CIPHERTEXT |
| 2046 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2047 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2048 | /* |
| 2049 | * Compression/decompression functions |
| 2050 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2051 | static int ssl_compress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2052 | { |
| 2053 | int ret; |
| 2054 | unsigned char *msg_post = ssl->out_msg; |
| 2055 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2056 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2058 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2059 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2060 | if( len_pre == 0 ) |
| 2061 | return( 0 ); |
| 2062 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2063 | memcpy( msg_pre, ssl->out_msg, len_pre ); |
| 2064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2065 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2066 | ssl->out_msglen ) ); |
| 2067 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2068 | MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2069 | ssl->out_msg, ssl->out_msglen ); |
| 2070 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2071 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 2072 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 2073 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2074 | ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_BUFFER_LEN; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2075 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2076 | ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2077 | if( ret != Z_OK ) |
| 2078 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2079 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) ); |
| 2080 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2083 | ssl->out_msglen = MBEDTLS_SSL_BUFFER_LEN - |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2084 | ssl->transform_out->ctx_deflate.avail_out; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2085 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2086 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2087 | ssl->out_msglen ) ); |
| 2088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2089 | MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2090 | ssl->out_msg, ssl->out_msglen ); |
| 2091 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2092 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2093 | |
| 2094 | return( 0 ); |
| 2095 | } |
| 2096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2097 | static int ssl_decompress_buf( mbedtls_ssl_context *ssl ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2098 | { |
| 2099 | int ret; |
| 2100 | unsigned char *msg_post = ssl->in_msg; |
| 2101 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 2102 | unsigned char *msg_pre = ssl->compress_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2104 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2105 | |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 2106 | if( len_pre == 0 ) |
| 2107 | return( 0 ); |
| 2108 | |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2109 | memcpy( msg_pre, ssl->in_msg, len_pre ); |
| 2110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2111 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2112 | ssl->in_msglen ) ); |
| 2113 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2114 | MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2115 | ssl->in_msg, ssl->in_msglen ); |
| 2116 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2117 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 2118 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 2119 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2120 | ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_MAX_CONTENT_LEN; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2121 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2122 | ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2123 | if( ret != Z_OK ) |
| 2124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2125 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) ); |
| 2126 | return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2129 | ssl->in_msglen = MBEDTLS_SSL_MAX_CONTENT_LEN - |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 2130 | ssl->transform_in->ctx_inflate.avail_out; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2132 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2133 | ssl->in_msglen ) ); |
| 2134 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2135 | MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2136 | ssl->in_msg, ssl->in_msglen ); |
| 2137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2138 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2139 | |
| 2140 | return( 0 ); |
| 2141 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2144 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2145 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2147 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2148 | static int ssl_resend_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2149 | { |
| 2150 | /* If renegotiation is not enforced, retransmit until we would reach max |
| 2151 | * timeout if we were using the usual handshake doubling scheme */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2152 | if( ssl->conf->renego_max_records < 0 ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2153 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2154 | uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2155 | unsigned char doublings = 1; |
| 2156 | |
| 2157 | while( ratio != 0 ) |
| 2158 | { |
| 2159 | ++doublings; |
| 2160 | ratio >>= 1; |
| 2161 | } |
| 2162 | |
| 2163 | if( ++ssl->renego_records_seen > doublings ) |
| 2164 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2165 | MBEDTLS_SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2166 | return( 0 ); |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | return( ssl_write_hello_request( ssl ) ); |
| 2171 | } |
| 2172 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2173 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2174 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2175 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2176 | * Fill the input message buffer by appending data to it. |
| 2177 | * The amount of data already fetched is in ssl->in_left. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2178 | * |
| 2179 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 2180 | * available (from this read and/or a previous one). Otherwise, an error code |
| 2181 | * is returned (possibly EOF or WANT_READ). |
| 2182 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2183 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 2184 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 2185 | * since we always read a whole datagram at once. |
| 2186 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 2187 | * For DTLS, it is up to the caller to set ssl->next_record_offset when |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2188 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2189 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2190 | int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2192 | int ret; |
| 2193 | size_t len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2195 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2196 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2197 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 2198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2199 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 2200 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2201 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2202 | } |
| 2203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2204 | if( nb_want > MBEDTLS_SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 2205 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2206 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 2207 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 2208 | } |
| 2209 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2210 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2211 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2212 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2213 | uint32_t timeout; |
| 2214 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 2215 | /* Just to be sure */ |
| 2216 | if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL ) |
| 2217 | { |
| 2218 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use " |
| 2219 | "mbedtls_ssl_set_timer_cb() for DTLS" ) ); |
| 2220 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2221 | } |
| 2222 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2223 | /* |
| 2224 | * The point is, we need to always read a full datagram at once, so we |
| 2225 | * sometimes read more then requested, and handle the additional data. |
| 2226 | * It could be the rest of the current record (while fetching the |
| 2227 | * header) and/or some other records in the same datagram. |
| 2228 | */ |
| 2229 | |
| 2230 | /* |
| 2231 | * Move to the next record in the already read datagram if applicable |
| 2232 | */ |
| 2233 | if( ssl->next_record_offset != 0 ) |
| 2234 | { |
| 2235 | if( ssl->in_left < ssl->next_record_offset ) |
| 2236 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2237 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2238 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | ssl->in_left -= ssl->next_record_offset; |
| 2242 | |
| 2243 | if( ssl->in_left != 0 ) |
| 2244 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2245 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d", |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2246 | ssl->next_record_offset ) ); |
| 2247 | memmove( ssl->in_hdr, |
| 2248 | ssl->in_hdr + ssl->next_record_offset, |
| 2249 | ssl->in_left ); |
| 2250 | } |
| 2251 | |
| 2252 | ssl->next_record_offset = 0; |
| 2253 | } |
| 2254 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2255 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2256 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2257 | |
| 2258 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2259 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2260 | */ |
| 2261 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2262 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2263 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2264 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2265 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2266 | |
| 2267 | /* |
| 2268 | * A record can't be split accross datagrams. If we need to read but |
| 2269 | * are not at the beginning of a new record, the caller did something |
| 2270 | * wrong. |
| 2271 | */ |
| 2272 | if( ssl->in_left != 0 ) |
| 2273 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2274 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2275 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2276 | } |
| 2277 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2278 | /* |
| 2279 | * Don't even try to read if time's out already. |
| 2280 | * This avoids by-passing the timer when repeatedly receiving messages |
| 2281 | * that will end up being dropped. |
| 2282 | */ |
| 2283 | if( ssl_check_timer( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2284 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2285 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2287 | len = MBEDTLS_SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2288 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2289 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2290 | timeout = ssl->handshake->retransmit_timeout; |
| 2291 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2292 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2294 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2295 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame^] | 2296 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2297 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 2298 | timeout ); |
| 2299 | else |
| 2300 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 2301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2302 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2303 | |
| 2304 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2306 | } |
| 2307 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2308 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2309 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2310 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2311 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2313 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2314 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2315 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 2316 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2317 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2318 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2321 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2322 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2323 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 2324 | return( ret ); |
| 2325 | } |
| 2326 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2327 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2328 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2329 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2330 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2331 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2332 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 2333 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2334 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2335 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2336 | return( ret ); |
| 2337 | } |
| 2338 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 2339 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 2340 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2341 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2342 | } |
| 2343 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2344 | if( ret < 0 ) |
| 2345 | return( ret ); |
| 2346 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2347 | ssl->in_left = ret; |
| 2348 | } |
| 2349 | else |
| 2350 | #endif |
| 2351 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2352 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 2353 | ssl->in_left, nb_want ) ); |
| 2354 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2355 | while( ssl->in_left < nb_want ) |
| 2356 | { |
| 2357 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 2358 | |
| 2359 | if( ssl_check_timer( ssl ) != 0 ) |
| 2360 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 2361 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame^] | 2362 | { |
| 2363 | if( ssl->f_recv_timeout != NULL ) |
| 2364 | { |
| 2365 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 2366 | ssl->in_hdr + ssl->in_left, len, |
| 2367 | ssl->conf->read_timeout ); |
| 2368 | } |
| 2369 | else |
| 2370 | { |
| 2371 | ret = ssl->f_recv( ssl->p_bio, |
| 2372 | ssl->in_hdr + ssl->in_left, len ); |
| 2373 | } |
| 2374 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2376 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d", |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame^] | 2377 | ssl->in_left, nb_want ) ); |
| 2378 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2379 | |
| 2380 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2381 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2382 | |
| 2383 | if( ret < 0 ) |
| 2384 | return( ret ); |
| 2385 | |
| 2386 | ssl->in_left += ret; |
| 2387 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2390 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2391 | |
| 2392 | return( 0 ); |
| 2393 | } |
| 2394 | |
| 2395 | /* |
| 2396 | * Flush any data not yet written |
| 2397 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2398 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2399 | { |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2400 | int ret; |
| 2401 | unsigned char *buf, i; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2403 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2404 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2405 | if( ssl->f_send == NULL ) |
| 2406 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2407 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 2408 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2410 | } |
| 2411 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2412 | /* Avoid incrementing counter if data is flushed */ |
| 2413 | if( ssl->out_left == 0 ) |
| 2414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2415 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2416 | return( 0 ); |
| 2417 | } |
| 2418 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2419 | while( ssl->out_left > 0 ) |
| 2420 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2421 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d", |
| 2422 | mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2424 | buf = ssl->out_hdr + mbedtls_ssl_hdr_len( ssl ) + |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2425 | ssl->out_msglen - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2426 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 2427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2428 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2429 | |
| 2430 | if( ret <= 0 ) |
| 2431 | return( ret ); |
| 2432 | |
| 2433 | ssl->out_left -= ret; |
| 2434 | } |
| 2435 | |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2436 | for( i = 8; i > ssl_ep_len( ssl ); i-- ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2437 | if( ++ssl->out_ctr[i - 1] != 0 ) |
| 2438 | break; |
| 2439 | |
| 2440 | /* The loop goes to its end iff the counter is wrapping */ |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 2441 | if( i == ssl_ep_len( ssl ) ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2443 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 2444 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2445 | } |
| 2446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2448 | |
| 2449 | return( 0 ); |
| 2450 | } |
| 2451 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2452 | /* |
| 2453 | * Functions to handle the DTLS retransmission state machine |
| 2454 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2456 | /* |
| 2457 | * Append current handshake message to current outgoing flight |
| 2458 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2459 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2460 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2461 | mbedtls_ssl_flight_item *msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2462 | |
| 2463 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2464 | if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2465 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2466 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2467 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2468 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2471 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2472 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2473 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2474 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2475 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | /* Copy current handshake message with headers */ |
| 2479 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 2480 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2481 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2482 | msg->next = NULL; |
| 2483 | |
| 2484 | /* Append to the current flight */ |
| 2485 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2486 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2487 | else |
| 2488 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2489 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2490 | while( cur->next != NULL ) |
| 2491 | cur = cur->next; |
| 2492 | cur->next = msg; |
| 2493 | } |
| 2494 | |
| 2495 | return( 0 ); |
| 2496 | } |
| 2497 | |
| 2498 | /* |
| 2499 | * Free the current flight of handshake messages |
| 2500 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2501 | static void ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2502 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2503 | mbedtls_ssl_flight_item *cur = flight; |
| 2504 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2505 | |
| 2506 | while( cur != NULL ) |
| 2507 | { |
| 2508 | next = cur->next; |
| 2509 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2510 | mbedtls_free( cur->p ); |
| 2511 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2512 | |
| 2513 | cur = next; |
| 2514 | } |
| 2515 | } |
| 2516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2517 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 2518 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 2519 | #endif |
| 2520 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2521 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2522 | * Swap transform_out and out_ctr with the alternative ones |
| 2523 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2524 | static void ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2525 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2526 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2527 | unsigned char tmp_out_ctr[8]; |
| 2528 | |
| 2529 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 2530 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2532 | return; |
| 2533 | } |
| 2534 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2535 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2536 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2537 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2538 | tmp_transform = ssl->transform_out; |
| 2539 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 2540 | ssl->handshake->alt_transform_out = tmp_transform; |
| 2541 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2542 | /* Swap epoch + sequence_number */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2543 | memcpy( tmp_out_ctr, ssl->out_ctr, 8 ); |
| 2544 | memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 ); |
| 2545 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2546 | |
| 2547 | /* Adjust to the newly activated transform */ |
| 2548 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2549 | ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2550 | { |
| 2551 | ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen - |
| 2552 | ssl->transform_out->fixed_ivlen; |
| 2553 | } |
| 2554 | else |
| 2555 | ssl->out_msg = ssl->out_iv; |
| 2556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2557 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 2558 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2560 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2561 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2562 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 2563 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | #endif |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | /* |
| 2570 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2571 | * |
| 2572 | * Need to remember the current message in case flush_output returns |
| 2573 | * WANT_WRITE, causing us to exit this function and come back later. |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2574 | * This function must be called until state is no longer SENDING. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2575 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2576 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2577 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2578 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2580 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2581 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2582 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise resending" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2583 | |
| 2584 | ssl->handshake->cur_msg = ssl->handshake->flight; |
| 2585 | ssl_swap_epochs( ssl ); |
| 2586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2587 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2588 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2589 | |
| 2590 | while( ssl->handshake->cur_msg != NULL ) |
| 2591 | { |
| 2592 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2593 | mbedtls_ssl_flight_item *cur = ssl->handshake->cur_msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2594 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2595 | /* Swap epochs before sending Finished: we can't do it after |
| 2596 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 2597 | * Must be done before copying, may change out_msg pointer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2598 | if( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2599 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2600 | { |
| 2601 | ssl_swap_epochs( ssl ); |
| 2602 | } |
| 2603 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2604 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2605 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2606 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2607 | |
| 2608 | ssl->handshake->cur_msg = cur->next; |
| 2609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2610 | MBEDTLS_SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2613 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2614 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2615 | return( ret ); |
| 2616 | } |
| 2617 | } |
| 2618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2619 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 2620 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 2621 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2622 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2623 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2624 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
| 2625 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2626 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2627 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2628 | |
| 2629 | return( 0 ); |
| 2630 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2631 | |
| 2632 | /* |
| 2633 | * To be called when the last message of an incoming flight is received. |
| 2634 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2635 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2636 | { |
| 2637 | /* We won't need to resend that one any more */ |
| 2638 | ssl_flight_free( ssl->handshake->flight ); |
| 2639 | ssl->handshake->flight = NULL; |
| 2640 | ssl->handshake->cur_msg = NULL; |
| 2641 | |
| 2642 | /* The next incoming flight will start with this msg_seq */ |
| 2643 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 2644 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2645 | /* Cancel timer */ |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2646 | ssl_set_timer( ssl, 0 ); |
| 2647 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2648 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2649 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2650 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2651 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2652 | } |
| 2653 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2654 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2655 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2656 | |
| 2657 | /* |
| 2658 | * To be called when the last message of an outgoing flight is send. |
| 2659 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2660 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2661 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2662 | ssl_reset_retransmit_timeout( ssl ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 2663 | ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2664 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2665 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2666 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2667 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2668 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2669 | } |
| 2670 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2671 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2672 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2674 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2675 | /* |
| 2676 | * Record layer functions |
| 2677 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2678 | |
| 2679 | /* |
| 2680 | * Write current record. |
| 2681 | * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg. |
| 2682 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2683 | int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2684 | { |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2685 | int ret, done = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2686 | size_t len = ssl->out_msglen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2688 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2690 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2691 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2692 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2693 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2694 | { |
| 2695 | ; /* Skip special handshake treatment when resending */ |
| 2696 | } |
| 2697 | else |
| 2698 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2699 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2700 | { |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 2701 | if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST && |
| 2702 | ssl->handshake == NULL ) |
| 2703 | { |
| 2704 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2705 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2706 | } |
| 2707 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2708 | ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 ); |
| 2709 | ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 ); |
| 2710 | ssl->out_msg[3] = (unsigned char)( ( len - 4 ) ); |
| 2711 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2712 | /* |
| 2713 | * DTLS has additional fields in the Handshake layer, |
| 2714 | * between the length field and the actual payload: |
| 2715 | * uint16 message_seq; |
| 2716 | * uint24 fragment_offset; |
| 2717 | * uint24 fragment_length; |
| 2718 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2719 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2720 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2721 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2722 | /* Make room for the additional DTLS fields */ |
| 2723 | memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2724 | ssl->out_msglen += 8; |
| 2725 | len += 8; |
| 2726 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2727 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2728 | if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2729 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 2730 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 2731 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 2732 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2733 | } |
| 2734 | else |
| 2735 | { |
| 2736 | ssl->out_msg[4] = 0; |
| 2737 | ssl->out_msg[5] = 0; |
| 2738 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2739 | |
| 2740 | /* We don't fragment, so frag_offset = 0 and frag_len = len */ |
| 2741 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 2742 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2743 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2744 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2745 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2746 | if( ssl->out_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | f3dc2f6 | 2013-10-29 18:17:41 +0100 | [diff] [blame] | 2747 | ssl->handshake->update_checksum( ssl, ssl->out_msg, len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2750 | /* Save handshake and CCS messages for resending */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2751 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2752 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2753 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2754 | ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING && |
| 2755 | ( ssl->out_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC || |
| 2756 | ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2757 | { |
| 2758 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 2759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2760 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2761 | return( ret ); |
| 2762 | } |
| 2763 | } |
| 2764 | #endif |
| 2765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2767 | if( ssl->transform_out != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2768 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2769 | { |
| 2770 | if( ( ret = ssl_compress_buf( ssl ) ) != 0 ) |
| 2771 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2773 | return( ret ); |
| 2774 | } |
| 2775 | |
| 2776 | len = ssl->out_msglen; |
| 2777 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2778 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 2781 | if( mbedtls_ssl_hw_record_write != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2783 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2785 | ret = mbedtls_ssl_hw_record_write( ssl ); |
| 2786 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2787 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2788 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret ); |
| 2789 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2790 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 2791 | |
| 2792 | if( ret == 0 ) |
| 2793 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2794 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2795 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2796 | if( !done ) |
| 2797 | { |
| 2798 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2799 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2800 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2801 | |
| 2802 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 2803 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2804 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2805 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2806 | { |
| 2807 | if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 ) |
| 2808 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2809 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2810 | return( ret ); |
| 2811 | } |
| 2812 | |
| 2813 | len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2814 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 2815 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2818 | ssl->out_left = mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, " |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2821 | "version = [%d:%d], msglen = %d", |
| 2822 | ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2823 | ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2824 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2825 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
| 2826 | ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2829 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2830 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2831 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2832 | return( ret ); |
| 2833 | } |
| 2834 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2835 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2836 | |
| 2837 | return( 0 ); |
| 2838 | } |
| 2839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2840 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2841 | /* |
| 2842 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 2843 | */ |
| 2844 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 2845 | { |
| 2846 | unsigned int start_bits, end_bits; |
| 2847 | |
| 2848 | start_bits = 8 - ( offset % 8 ); |
| 2849 | if( start_bits != 8 ) |
| 2850 | { |
| 2851 | size_t first_byte_idx = offset / 8; |
| 2852 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2853 | /* Special case */ |
| 2854 | if( len <= start_bits ) |
| 2855 | { |
| 2856 | for( ; len != 0; len-- ) |
| 2857 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 2858 | |
| 2859 | /* Avoid potential issues with offset or len becoming invalid */ |
| 2860 | return; |
| 2861 | } |
| 2862 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2863 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 2864 | len -= start_bits; |
| 2865 | |
| 2866 | for( ; start_bits != 0; start_bits-- ) |
| 2867 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 2868 | } |
| 2869 | |
| 2870 | end_bits = len % 8; |
| 2871 | if( end_bits != 0 ) |
| 2872 | { |
| 2873 | size_t last_byte_idx = ( offset + len ) / 8; |
| 2874 | |
| 2875 | len -= end_bits; /* Now len % 8 == 0 */ |
| 2876 | |
| 2877 | for( ; end_bits != 0; end_bits-- ) |
| 2878 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 2879 | } |
| 2880 | |
| 2881 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 2882 | } |
| 2883 | |
| 2884 | /* |
| 2885 | * Check that bitmask is full |
| 2886 | */ |
| 2887 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 2888 | { |
| 2889 | size_t i; |
| 2890 | |
| 2891 | for( i = 0; i < len / 8; i++ ) |
| 2892 | if( mask[i] != 0xFF ) |
| 2893 | return( -1 ); |
| 2894 | |
| 2895 | for( i = 0; i < len % 8; i++ ) |
| 2896 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 2897 | return( -1 ); |
| 2898 | |
| 2899 | return( 0 ); |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Reassemble fragmented DTLS handshake messages. |
| 2904 | * |
| 2905 | * Use a temporary buffer for reassembly, divided in two parts: |
| 2906 | * - the first holds the reassembled message (including handshake header), |
| 2907 | * - the second holds a bitmask indicating which parts of the message |
| 2908 | * (excluding headers) have been received so far. |
| 2909 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2910 | static int ssl_reassemble_dtls_handshake( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2911 | { |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2912 | unsigned char *msg, *bitmask; |
| 2913 | size_t frag_len, frag_off; |
| 2914 | size_t msg_len = ssl->in_hslen - 12; /* Without headers */ |
| 2915 | |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2916 | if( ssl->handshake == NULL ) |
| 2917 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) ); |
| 2919 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2922 | /* |
| 2923 | * For first fragment, check size and allocate buffer |
| 2924 | */ |
| 2925 | if( ssl->handshake->hs_msg == NULL ) |
| 2926 | { |
| 2927 | size_t alloc_len; |
| 2928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2929 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d", |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2930 | msg_len ) ); |
| 2931 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2932 | if( ssl->in_hslen > MBEDTLS_SSL_MAX_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2933 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2934 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too large" ) ); |
| 2935 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | /* The bitmask needs one bit per byte of message excluding header */ |
| 2939 | alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 ); |
| 2940 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 2941 | ssl->handshake->hs_msg = mbedtls_calloc( 1, alloc_len ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2942 | if( ssl->handshake->hs_msg == NULL ) |
| 2943 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 2944 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc failed (%d bytes)", alloc_len ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2945 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2948 | /* Prepare final header: copy msg_type, length and message_seq, |
| 2949 | * then add standardised fragment_offset and fragment_length */ |
| 2950 | memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 ); |
| 2951 | memset( ssl->handshake->hs_msg + 6, 0, 3 ); |
| 2952 | memcpy( ssl->handshake->hs_msg + 9, |
| 2953 | ssl->handshake->hs_msg + 1, 3 ); |
| 2954 | } |
| 2955 | else |
| 2956 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2957 | /* Make sure msg_type and length are consistent */ |
| 2958 | if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 ) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2959 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2960 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) ); |
| 2961 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | msg = ssl->handshake->hs_msg + 12; |
| 2966 | bitmask = msg + msg_len; |
| 2967 | |
| 2968 | /* |
| 2969 | * Check and copy current fragment |
| 2970 | */ |
| 2971 | frag_off = ( ssl->in_msg[6] << 16 ) | |
| 2972 | ( ssl->in_msg[7] << 8 ) | |
| 2973 | ssl->in_msg[8]; |
| 2974 | frag_len = ( ssl->in_msg[9] << 16 ) | |
| 2975 | ( ssl->in_msg[10] << 8 ) | |
| 2976 | ssl->in_msg[11]; |
| 2977 | |
| 2978 | if( frag_off + frag_len > msg_len ) |
| 2979 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2980 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d", |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2981 | frag_off, frag_len, msg_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2982 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | if( frag_len + 12 > ssl->in_msglen ) |
| 2986 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d", |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2988 | frag_len, ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2989 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2990 | } |
| 2991 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2992 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d", |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2993 | frag_off, frag_len ) ); |
| 2994 | |
| 2995 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 2996 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 2997 | |
| 2998 | /* |
| 2999 | * Do we have the complete message by now? |
| 3000 | * If yes, finalize it, else ask to read the next record. |
| 3001 | */ |
| 3002 | if( ssl_bitmask_check( bitmask, msg_len ) != 0 ) |
| 3003 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3004 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3005 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3006 | } |
| 3007 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3008 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake message completed" ) ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3009 | |
Manuel Pégourié-Gonnard | 23cad33 | 2014-10-13 17:06:41 +0200 | [diff] [blame] | 3010 | if( frag_len + 12 < ssl->in_msglen ) |
| 3011 | { |
| 3012 | /* |
| 3013 | * We'got more handshake messages in the same record. |
| 3014 | * This case is not handled now because no know implementation does |
| 3015 | * that and it's hard to test, so we prefer to fail cleanly for now. |
| 3016 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3017 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) ); |
| 3018 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 23cad33 | 2014-10-13 17:06:41 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3021 | if( ssl->in_left > ssl->next_record_offset ) |
| 3022 | { |
| 3023 | /* |
| 3024 | * We've got more data in the buffer after the current record, |
| 3025 | * that we don't want to overwrite. Move it before writing the |
| 3026 | * reassembled message, and adjust in_left and next_record_offset. |
| 3027 | */ |
| 3028 | unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset; |
| 3029 | unsigned char *new_remain = ssl->in_msg + ssl->in_hslen; |
| 3030 | size_t remain_len = ssl->in_left - ssl->next_record_offset; |
| 3031 | |
| 3032 | /* First compute and check new lengths */ |
| 3033 | ssl->next_record_offset = new_remain - ssl->in_hdr; |
| 3034 | ssl->in_left = ssl->next_record_offset + remain_len; |
| 3035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3036 | if( ssl->in_left > MBEDTLS_SSL_BUFFER_LEN - |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3037 | (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
| 3038 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3039 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) ); |
| 3040 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3041 | } |
| 3042 | |
| 3043 | memmove( new_remain, cur_remain, remain_len ); |
| 3044 | } |
| 3045 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3046 | memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen ); |
| 3047 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3048 | mbedtls_free( ssl->handshake->hs_msg ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3049 | ssl->handshake->hs_msg = NULL; |
| 3050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3051 | MBEDTLS_SSL_DEBUG_BUF( 3, "reassembled handshake message", |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3052 | ssl->in_msg, ssl->in_hslen ); |
| 3053 | |
| 3054 | return( 0 ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3055 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3056 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3057 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3058 | static int ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3059 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3060 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3061 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3062 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d", |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3063 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3064 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3065 | } |
| 3066 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3067 | ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ( |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 3068 | ( ssl->in_msg[1] << 16 ) | |
| 3069 | ( ssl->in_msg[2] << 8 ) | |
| 3070 | ssl->in_msg[3] ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3071 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3072 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3073 | " %d, type = %d, hslen = %d", |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 3074 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3076 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3077 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3078 | { |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3079 | int ret; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3080 | unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3081 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3082 | /* ssl->handshake is NULL when receiving ClientHello for renego */ |
| 3083 | if( ssl->handshake != NULL && |
| 3084 | recv_msg_seq != ssl->handshake->in_msg_seq ) |
| 3085 | { |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 3086 | /* Retransmit only on last message from previous flight, to avoid |
| 3087 | * too many retransmissions. |
| 3088 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 3089 | if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3090 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3092 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3093 | "message_seq = %d, start_of_flight = %d", |
| 3094 | recv_msg_seq, |
| 3095 | ssl->handshake->in_flight_start_seq ) ); |
| 3096 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3097 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3098 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3099 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3100 | return( ret ); |
| 3101 | } |
| 3102 | } |
| 3103 | else |
| 3104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3105 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 3106 | "message_seq = %d, expected = %d", |
| 3107 | recv_msg_seq, |
| 3108 | ssl->handshake->in_msg_seq ) ); |
| 3109 | } |
| 3110 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3111 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3112 | } |
| 3113 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3114 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3115 | /* Reassemble if current message is fragmented or reassembly is |
| 3116 | * already in progress */ |
| 3117 | if( ssl->in_msglen < ssl->in_hslen || |
| 3118 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 3119 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 || |
| 3120 | ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3121 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3122 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 3123 | |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3124 | if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 ) |
| 3125 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3126 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3127 | return( ret ); |
| 3128 | } |
| 3129 | } |
| 3130 | } |
| 3131 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3132 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 3133 | /* With TLS we don't handle fragmentation (for now) */ |
| 3134 | if( ssl->in_msglen < ssl->in_hslen ) |
| 3135 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3136 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 3137 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3138 | } |
| 3139 | |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 3140 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 3141 | ssl->handshake != NULL ) |
| 3142 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3143 | ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen ); |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 3144 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3145 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3146 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3147 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3148 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3149 | ssl->handshake != NULL ) |
| 3150 | { |
| 3151 | ssl->handshake->in_msg_seq++; |
| 3152 | } |
| 3153 | #endif |
| 3154 | |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3155 | return( 0 ); |
| 3156 | } |
| 3157 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3158 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3159 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 3160 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3161 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 3162 | * Bit n is set iff record number in_window_top - n has been seen. |
| 3163 | * |
| 3164 | * Usually, in_window_top is the last record number seen and the lsb of |
| 3165 | * in_window is set. The only exception is the initial state (record number 0 |
| 3166 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3167 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3168 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3169 | static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3170 | { |
| 3171 | ssl->in_window_top = 0; |
| 3172 | ssl->in_window = 0; |
| 3173 | } |
| 3174 | |
| 3175 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 3176 | { |
| 3177 | return( ( (uint64_t) buf[0] << 40 ) | |
| 3178 | ( (uint64_t) buf[1] << 32 ) | |
| 3179 | ( (uint64_t) buf[2] << 24 ) | |
| 3180 | ( (uint64_t) buf[3] << 16 ) | |
| 3181 | ( (uint64_t) buf[4] << 8 ) | |
| 3182 | ( (uint64_t) buf[5] ) ); |
| 3183 | } |
| 3184 | |
| 3185 | /* |
| 3186 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 3187 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3188 | int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3189 | { |
| 3190 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 3191 | uint64_t bit; |
| 3192 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3193 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3194 | return( 0 ); |
| 3195 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3196 | if( rec_seqnum > ssl->in_window_top ) |
| 3197 | return( 0 ); |
| 3198 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3199 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3200 | |
| 3201 | if( bit >= 64 ) |
| 3202 | return( -1 ); |
| 3203 | |
| 3204 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 3205 | return( -1 ); |
| 3206 | |
| 3207 | return( 0 ); |
| 3208 | } |
| 3209 | |
| 3210 | /* |
| 3211 | * Update replay window on new validated record |
| 3212 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3213 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3214 | { |
| 3215 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 3216 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3217 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3218 | return; |
| 3219 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3220 | if( rec_seqnum > ssl->in_window_top ) |
| 3221 | { |
| 3222 | /* Update window_top and the contents of the window */ |
| 3223 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 3224 | |
| 3225 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3226 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3227 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3228 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3229 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3230 | ssl->in_window |= 1; |
| 3231 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3232 | |
| 3233 | ssl->in_window_top = rec_seqnum; |
| 3234 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3235 | else |
| 3236 | { |
| 3237 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3238 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3239 | |
| 3240 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 3241 | ssl->in_window |= (uint64_t) 1 << bit; |
| 3242 | } |
| 3243 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3244 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3245 | |
| 3246 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3247 | * ContentType type; |
| 3248 | * ProtocolVersion version; |
| 3249 | * uint16 epoch; // DTLS only |
| 3250 | * uint48 sequence_number; // DTLS only |
| 3251 | * uint16 length; |
| 3252 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3253 | static int ssl_parse_record_header( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3254 | { |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3255 | int ret; |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3256 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3258 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) ); |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3259 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3260 | ssl->in_msgtype = ssl->in_hdr[0]; |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3261 | ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1]; |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3262 | mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3264 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3265 | "version = [%d:%d], msglen = %d", |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3266 | ssl->in_msgtype, |
| 3267 | major_ver, minor_ver, ssl->in_msglen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3268 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3269 | /* Check record type */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3270 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3271 | ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT && |
| 3272 | ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 3273 | ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3275 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3277 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 3278 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3279 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 ) |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3280 | { |
| 3281 | return( ret ); |
| 3282 | } |
| 3283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3284 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3285 | } |
| 3286 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3287 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3288 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | df9a0a8 | 2014-10-02 14:17:18 +0200 | [diff] [blame] | 3289 | { |
| 3290 | /* Drop unexpected ChangeCipherSpec messages */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3291 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 3292 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 3293 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
Manuel Pégourié-Gonnard | df9a0a8 | 2014-10-02 14:17:18 +0200 | [diff] [blame] | 3294 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) ); |
| 3296 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | df9a0a8 | 2014-10-02 14:17:18 +0200 | [diff] [blame] | 3297 | } |
| 3298 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 3299 | /* Drop unexpected ApplicationData records, |
| 3300 | * except at the beginning of renegotiations */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3301 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 3302 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 3303 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 3304 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 3305 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Manuel Pégourié-Gonnard | 69849f8 | 2015-03-10 11:54:02 +0000 | [diff] [blame] | 3306 | #endif |
| 3307 | ) |
Manuel Pégourié-Gonnard | df9a0a8 | 2014-10-02 14:17:18 +0200 | [diff] [blame] | 3308 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3309 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 3310 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | df9a0a8 | 2014-10-02 14:17:18 +0200 | [diff] [blame] | 3311 | } |
| 3312 | } |
| 3313 | #endif |
| 3314 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3315 | /* Check version */ |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3316 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3317 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3318 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 3319 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3322 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3323 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3324 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 3325 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3328 | /* Check epoch (and sequence number) with DTLS */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3329 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3330 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3331 | { |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3332 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3333 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3334 | if( rec_epoch != ssl->in_epoch ) |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3335 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3336 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3337 | "expected %d, received %d", |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 3338 | ssl->in_epoch, rec_epoch ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3339 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3340 | } |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3342 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3343 | if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3344 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3345 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 3346 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3347 | } |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3348 | #endif |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3349 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3350 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 60ca5af | 2014-09-03 16:02:42 +0200 | [diff] [blame] | 3351 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3352 | /* Check length against the size of our buffer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3353 | if( ssl->in_msglen > MBEDTLS_SSL_BUFFER_LEN |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3354 | - (size_t)( ssl->in_msg - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3355 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3356 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3357 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3360 | /* Check length against bounds of the current transform and version */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3361 | if( ssl->transform_in == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3362 | { |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3363 | if( ssl->in_msglen < 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3364 | ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3365 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3366 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3367 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | else |
| 3371 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3372 | if( ssl->in_msglen < ssl->transform_in->minlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3373 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3374 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3375 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3378 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 3379 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 3380 | ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_MAX_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3381 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3382 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3383 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3384 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3385 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3386 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3387 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3388 | /* |
| 3389 | * TLS encrypted messages can have up to 256 bytes of padding |
| 3390 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3391 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 && |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 3392 | ssl->in_msglen > ssl->transform_in->minlen + |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3393 | MBEDTLS_SSL_MAX_CONTENT_LEN + 256 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3395 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3396 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3397 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3398 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3401 | return( 0 ); |
| 3402 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3403 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3404 | /* |
| 3405 | * If applicable, decrypt (and decompress) record content |
| 3406 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3407 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3408 | { |
| 3409 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3411 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
| 3412 | ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3413 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3414 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 3415 | if( mbedtls_ssl_hw_record_read != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3417 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3419 | ret = mbedtls_ssl_hw_record_read( ssl ); |
| 3420 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3421 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3422 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret ); |
| 3423 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3424 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3425 | |
| 3426 | if( ret == 0 ) |
| 3427 | done = 1; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3428 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3429 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3430 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3431 | { |
| 3432 | if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 ) |
| 3433 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3434 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3435 | return( ret ); |
| 3436 | } |
| 3437 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3438 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3439 | ssl->in_msg, ssl->in_msglen ); |
| 3440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3441 | if( ssl->in_msglen > MBEDTLS_SSL_MAX_CONTENT_LEN ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3443 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3444 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3445 | } |
| 3446 | } |
| 3447 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3448 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3449 | if( ssl->transform_in != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3451 | { |
| 3452 | if( ( ret = ssl_decompress_buf( ssl ) ) != 0 ) |
| 3453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3454 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3455 | return( ret ); |
| 3456 | } |
| 3457 | |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 3458 | // TODO: what's the purpose of these lines? is in_len used? |
| 3459 | ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 ); |
| 3460 | ssl->in_len[1] = (unsigned char)( ssl->in_msglen ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3461 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3462 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3463 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3464 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3465 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3466 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3467 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3468 | } |
| 3469 | #endif |
| 3470 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3471 | return( 0 ); |
| 3472 | } |
| 3473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3474 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3475 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3476 | /* |
| 3477 | * Read a record. |
| 3478 | * |
| 3479 | * For DTLS, silently ignore invalid records (RFC 4.1.2.7.) |
| 3480 | * and continue reading until a valid record is found. |
| 3481 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3482 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3483 | { |
| 3484 | int ret; |
| 3485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3487 | |
Manuel Pégourié-Gonnard | 624bcb5 | 2014-09-10 21:56:38 +0200 | [diff] [blame] | 3488 | if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3489 | { |
| 3490 | /* |
| 3491 | * Get next Handshake message in the current record |
| 3492 | */ |
| 3493 | ssl->in_msglen -= ssl->in_hslen; |
| 3494 | |
| 3495 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 3496 | ssl->in_msglen ); |
| 3497 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3498 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 3499 | ssl->in_msg, ssl->in_msglen ); |
| 3500 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3501 | if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 3502 | return( ret ); |
| 3503 | |
| 3504 | return( 0 ); |
| 3505 | } |
| 3506 | |
| 3507 | ssl->in_hslen = 0; |
| 3508 | |
| 3509 | /* |
| 3510 | * Read the record header and parse it |
| 3511 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3512 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3513 | read_record_header: |
| 3514 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3515 | if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3516 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3517 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3518 | return( ret ); |
| 3519 | } |
| 3520 | |
| 3521 | if( ( ret = ssl_parse_record_header( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3522 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3523 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3524 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3525 | { |
| 3526 | /* Ignore bad record and get next one; drop the whole datagram |
| 3527 | * since current header cannot be trusted to find the next record |
| 3528 | * in current datagram */ |
| 3529 | ssl->next_record_offset = 0; |
| 3530 | ssl->in_left = 0; |
| 3531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3532 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3533 | goto read_record_header; |
| 3534 | } |
| 3535 | #endif |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3536 | return( ret ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3537 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3538 | |
| 3539 | /* |
| 3540 | * Read and optionally decrypt the message contents |
| 3541 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3542 | if( ( ret = mbedtls_ssl_fetch_input( ssl, |
| 3543 | mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3544 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3545 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3546 | return( ret ); |
| 3547 | } |
| 3548 | |
| 3549 | /* Done reading this record, get ready for the next one */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3550 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3551 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3552 | ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3553 | else |
| 3554 | #endif |
| 3555 | ssl->in_left = 0; |
| 3556 | |
| 3557 | if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3559 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3560 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3561 | { |
| 3562 | /* Silently discard invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3563 | if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD || |
| 3564 | ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3565 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3566 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3567 | if( ssl->conf->badmac_limit != 0 && |
| 3568 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3569 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3570 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 3571 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 3572 | } |
| 3573 | #endif |
| 3574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3575 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3576 | goto read_record_header; |
| 3577 | } |
| 3578 | |
| 3579 | return( ret ); |
| 3580 | } |
| 3581 | else |
| 3582 | #endif |
| 3583 | { |
| 3584 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3585 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 3586 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3588 | mbedtls_ssl_send_alert_message( ssl, |
| 3589 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3590 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3591 | } |
| 3592 | #endif |
| 3593 | return( ret ); |
| 3594 | } |
| 3595 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3596 | |
| 3597 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3598 | * When we sent the last flight of the handshake, we MUST respond to a |
| 3599 | * retransmit of the peer's previous flight with a retransmit. (In |
| 3600 | * practice, only the Finished message will make it, other messages |
| 3601 | * including CCS use the old transform so they're dropped as invalid.) |
| 3602 | * |
| 3603 | * If the record we received is not a handshake message, however, it |
| 3604 | * means the peer received our last flight so we can clean up |
| 3605 | * handshake info. |
| 3606 | * |
| 3607 | * This check needs to be done before prepare_handshake() due to an edge |
| 3608 | * case: if the client immediately requests renegotiation, this |
| 3609 | * finishes the current handshake first, avoiding the new ClientHello |
| 3610 | * being mistaken for an ancient message in the current handshake. |
| 3611 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3612 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3613 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3614 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3615 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3616 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3617 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3618 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3619 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3620 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3621 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3622 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3623 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3624 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3625 | return( ret ); |
| 3626 | } |
| 3627 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 3628 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 3629 | } |
| 3630 | else |
| 3631 | { |
| 3632 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 3633 | } |
| 3634 | } |
| 3635 | #endif |
| 3636 | |
| 3637 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3638 | * Handle particular types of records |
| 3639 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3640 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3641 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3642 | if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 3643 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3646 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3647 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3648 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3649 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 3650 | |
| 3651 | /* |
| 3652 | * Ignore non-fatal alerts, except close_notify |
| 3653 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3655 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3656 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 3657 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3658 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3661 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 3662 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3663 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3664 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 3665 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3666 | } |
| 3667 | } |
| 3668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3669 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3670 | |
| 3671 | return( 0 ); |
| 3672 | } |
| 3673 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3674 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 3675 | { |
| 3676 | int ret; |
| 3677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3678 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 3679 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 3680 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 3681 | { |
| 3682 | return( ret ); |
| 3683 | } |
| 3684 | |
| 3685 | return( 0 ); |
| 3686 | } |
| 3687 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3688 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 3689 | unsigned char level, |
| 3690 | unsigned char message ) |
| 3691 | { |
| 3692 | int ret; |
| 3693 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3694 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 3695 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3696 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 3697 | ssl->out_msglen = 2; |
| 3698 | ssl->out_msg[0] = level; |
| 3699 | ssl->out_msg[1] = message; |
| 3700 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3701 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 3702 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3703 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 3704 | return( ret ); |
| 3705 | } |
| 3706 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3707 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 3708 | |
| 3709 | return( 0 ); |
| 3710 | } |
| 3711 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3712 | /* |
| 3713 | * Handshake functions |
| 3714 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3715 | #if !defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) && \ |
| 3716 | !defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED) && \ |
| 3717 | !defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) && \ |
| 3718 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \ |
| 3719 | !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \ |
| 3720 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \ |
| 3721 | !defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED) |
| 3722 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3723 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3724 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3725 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3726 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3727 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3728 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3729 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3730 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3731 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3732 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3733 | ssl->state++; |
| 3734 | return( 0 ); |
| 3735 | } |
| 3736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3738 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3739 | } |
| 3740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3741 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3742 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3743 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3744 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3746 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3747 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3748 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3749 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3750 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3751 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3752 | ssl->state++; |
| 3753 | return( 0 ); |
| 3754 | } |
| 3755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3756 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3757 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3758 | } |
| 3759 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3760 | int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3761 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3762 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3763 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3764 | const mbedtls_x509_crt *crt; |
| 3765 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3767 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3768 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3769 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3770 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3771 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3772 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3773 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 48f7a5d | 2013-04-19 14:30:58 +0200 | [diff] [blame] | 3774 | ssl->state++; |
| 3775 | return( 0 ); |
| 3776 | } |
| 3777 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3778 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3779 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3780 | { |
| 3781 | if( ssl->client_auth == 0 ) |
| 3782 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3783 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3784 | ssl->state++; |
| 3785 | return( 0 ); |
| 3786 | } |
| 3787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3788 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3789 | /* |
| 3790 | * If using SSLv3 and got no cert, send an Alert message |
| 3791 | * (otherwise an empty Certificate message will be sent). |
| 3792 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3793 | if( mbedtls_ssl_own_cert( ssl ) == NULL && |
| 3794 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3795 | { |
| 3796 | ssl->out_msglen = 2; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3797 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
| 3798 | ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING; |
| 3799 | ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3801 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3802 | goto write_msg; |
| 3803 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3804 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3805 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3806 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 3807 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3808 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3809 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3810 | if( mbedtls_ssl_own_cert( ssl ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3812 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) ); |
| 3813 | return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3814 | } |
| 3815 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3816 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3818 | MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3819 | |
| 3820 | /* |
| 3821 | * 0 . 0 handshake type |
| 3822 | * 1 . 3 handshake length |
| 3823 | * 4 . 6 length of all certs |
| 3824 | * 7 . 9 length of cert. 1 |
| 3825 | * 10 . n-1 peer certificate |
| 3826 | * n . n+2 length of cert. 2 |
| 3827 | * n+3 . ... upper level cert, etc. |
| 3828 | */ |
| 3829 | i = 7; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3830 | crt = mbedtls_ssl_own_cert( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3831 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 3832 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3833 | { |
| 3834 | n = crt->raw.len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3835 | if( n > MBEDTLS_SSL_MAX_CONTENT_LEN - 3 - i ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3836 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3837 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d", |
| 3838 | i + 3 + n, MBEDTLS_SSL_MAX_CONTENT_LEN ) ); |
| 3839 | return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | ssl->out_msg[i ] = (unsigned char)( n >> 16 ); |
| 3843 | ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); |
| 3844 | ssl->out_msg[i + 2] = (unsigned char)( n ); |
| 3845 | |
| 3846 | i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); |
| 3847 | i += n; crt = crt->next; |
| 3848 | } |
| 3849 | |
| 3850 | ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); |
| 3851 | ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); |
| 3852 | ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); |
| 3853 | |
| 3854 | ssl->out_msglen = i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3855 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3856 | ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3857 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 3858 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3859 | write_msg: |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 3860 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3861 | |
| 3862 | ssl->state++; |
| 3863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3864 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3865 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3866 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3867 | return( ret ); |
| 3868 | } |
| 3869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3870 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3871 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 3872 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3875 | int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3876 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3877 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 3878 | size_t i, n; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3879 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3880 | int authmode = ssl->conf->authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3881 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3882 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3884 | if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK || |
| 3885 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_DHE_PSK || |
| 3886 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3887 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3888 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 3889 | ssl->state++; |
| 3890 | return( 0 ); |
| 3891 | } |
| 3892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3893 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3894 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3895 | ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK ) |
| 3896 | { |
| 3897 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
| 3898 | ssl->state++; |
| 3899 | return( 0 ); |
| 3900 | } |
| 3901 | |
| 3902 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 3903 | if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET ) |
| 3904 | authmode = ssl->handshake->sni_authmode; |
| 3905 | #endif |
| 3906 | |
| 3907 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 3908 | authmode == MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3909 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 3910 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3911 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3912 | ssl->state++; |
| 3913 | return( 0 ); |
| 3914 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 3915 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3916 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3917 | if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3918 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3919 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3920 | return( ret ); |
| 3921 | } |
| 3922 | |
| 3923 | ssl->state++; |
| 3924 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3925 | #if defined(MBEDTLS_SSL_SRV_C) |
| 3926 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3927 | /* |
| 3928 | * Check if the client sent an empty certificate |
| 3929 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3930 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3931 | ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3932 | { |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 3933 | if( ssl->in_msglen == 2 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3934 | ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 3935 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 3936 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3937 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3938 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3939 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 3940 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3941 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3942 | return( 0 ); |
| 3943 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3944 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3947 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3949 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 3950 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3951 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3952 | ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3953 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3954 | if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) && |
| 3955 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3956 | ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE && |
| 3957 | memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3958 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3959 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3960 | |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 3961 | ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING; |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3962 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3963 | return( 0 ); |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 3964 | else |
| 3965 | return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3966 | } |
| 3967 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3968 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 3969 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3970 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3972 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3973 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3974 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 3975 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3978 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE || |
| 3979 | ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3980 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3981 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 3982 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3985 | i = mbedtls_ssl_hs_hdr_len( ssl ); |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 3986 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3987 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3988 | * Same message structure as in mbedtls_ssl_write_certificate() |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3989 | */ |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 3990 | n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3991 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 3992 | if( ssl->in_msg[i] != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3993 | ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3994 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3995 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 3996 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 3999 | /* In case we tried to reuse a session but it failed */ |
| 4000 | if( ssl->session_negotiate->peer_cert != NULL ) |
| 4001 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4002 | mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert ); |
| 4003 | mbedtls_free( ssl->session_negotiate->peer_cert ); |
Manuel Pégourié-Gonnard | bfb355c | 2013-09-07 17:27:43 +0200 | [diff] [blame] | 4004 | } |
| 4005 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4006 | if( ( ssl->session_negotiate->peer_cert = mbedtls_calloc( 1, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4007 | sizeof( mbedtls_x509_crt ) ) ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4008 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4009 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4010 | sizeof( mbedtls_x509_crt ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4011 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4014 | mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4015 | |
Manuel Pégourié-Gonnard | f49a7da | 2014-09-10 13:30:43 +0000 | [diff] [blame] | 4016 | i += 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4017 | |
| 4018 | while( i < ssl->in_hslen ) |
| 4019 | { |
| 4020 | if( ssl->in_msg[i] != 0 ) |
| 4021 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4022 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 4023 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
| 4026 | n = ( (unsigned int) ssl->in_msg[i + 1] << 8 ) |
| 4027 | | (unsigned int) ssl->in_msg[i + 2]; |
| 4028 | i += 3; |
| 4029 | |
| 4030 | if( n < 128 || i + n > ssl->in_hslen ) |
| 4031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4032 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) ); |
| 4033 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4036 | ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 4037 | ssl->in_msg + i, n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4038 | if( ret != 0 ) |
| 4039 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4040 | MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4041 | return( ret ); |
| 4042 | } |
| 4043 | |
| 4044 | i += n; |
| 4045 | } |
| 4046 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4047 | MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4048 | |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 4049 | /* |
| 4050 | * On client, make sure the server cert doesn't change during renego to |
| 4051 | * avoid "triple handshake" attack: https://secure-resumption.com/ |
| 4052 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4053 | #if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4054 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4055 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 4056 | { |
| 4057 | if( ssl->session->peer_cert == NULL ) |
| 4058 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4059 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) ); |
| 4060 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | if( ssl->session->peer_cert->raw.len != |
| 4064 | ssl->session_negotiate->peer_cert->raw.len || |
| 4065 | memcmp( ssl->session->peer_cert->raw.p, |
| 4066 | ssl->session_negotiate->peer_cert->raw.p, |
| 4067 | ssl->session->peer_cert->raw.len ) != 0 ) |
| 4068 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4069 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) ); |
| 4070 | return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ); |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 4071 | } |
| 4072 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4073 | #endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 796c6f3 | 2014-03-10 09:34:49 +0100 | [diff] [blame] | 4074 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 4075 | if( authmode != MBEDTLS_SSL_VERIFY_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4076 | { |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 4077 | mbedtls_x509_crt *ca_chain; |
| 4078 | mbedtls_x509_crl *ca_crl; |
| 4079 | |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4080 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 4081 | if( ssl->handshake->sni_ca_chain != NULL ) |
| 4082 | { |
| 4083 | ca_chain = ssl->handshake->sni_ca_chain; |
| 4084 | ca_crl = ssl->handshake->sni_ca_crl; |
| 4085 | } |
| 4086 | else |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 4087 | #endif |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 4088 | { |
| 4089 | ca_chain = ssl->conf->ca_chain; |
| 4090 | ca_crl = ssl->conf->ca_crl; |
| 4091 | } |
| 4092 | |
| 4093 | if( ca_chain == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4094 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4095 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) ); |
| 4096 | return( MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4099 | /* |
| 4100 | * Main check: verify certificate |
| 4101 | */ |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 4102 | ret = mbedtls_x509_crt_verify_with_profile( |
| 4103 | ssl->session_negotiate->peer_cert, |
| 4104 | ca_chain, ca_crl, |
| 4105 | ssl->conf->cert_profile, |
| 4106 | ssl->hostname, |
| 4107 | &ssl->session_negotiate->verify_result, |
| 4108 | ssl->conf->f_vrfy, ssl->conf->p_vrfy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4109 | |
| 4110 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4111 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4112 | MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4113 | } |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4114 | |
| 4115 | /* |
| 4116 | * Secondary checks: always done, but change 'ret' only if it was 0 |
| 4117 | */ |
| 4118 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4119 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4120 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4121 | const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4122 | |
| 4123 | /* If certificate uses an EC key, make sure the curve is OK */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4124 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) && |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 4125 | mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4126 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4127 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4128 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4129 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 4130 | } |
| 4131 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 4132 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4133 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4134 | if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert, |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4135 | ciphersuite_info, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4136 | ! ssl->conf->endpoint, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 4137 | &ssl->session_negotiate->verify_result ) != 0 ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4138 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4139 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4140 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4141 | ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 4144 | if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4145 | ret = 0; |
| 4146 | } |
| 4147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4148 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4149 | |
| 4150 | return( ret ); |
| 4151 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4152 | #endif /* !MBEDTLS_KEY_EXCHANGE_RSA_ENABLED |
| 4153 | !MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED |
| 4154 | !MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED |
| 4155 | !MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED |
| 4156 | !MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED |
| 4157 | !MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED |
| 4158 | !MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4160 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4161 | { |
| 4162 | int ret; |
| 4163 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4164 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4165 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4166 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4167 | ssl->out_msglen = 1; |
| 4168 | ssl->out_msg[0] = 1; |
| 4169 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4170 | ssl->state++; |
| 4171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4172 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4173 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4174 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4175 | return( ret ); |
| 4176 | } |
| 4177 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4178 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4179 | |
| 4180 | return( 0 ); |
| 4181 | } |
| 4182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4183 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4184 | { |
| 4185 | int ret; |
| 4186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4187 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4189 | if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4190 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4191 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4192 | return( ret ); |
| 4193 | } |
| 4194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4195 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4197 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
| 4198 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 ) |
| 4202 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4203 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
| 4204 | return( MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4207 | /* |
| 4208 | * Switch to our negotiated transform and session parameters for inbound |
| 4209 | * data. |
| 4210 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4211 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4212 | ssl->transform_in = ssl->transform_negotiate; |
| 4213 | ssl->session_in = ssl->session_negotiate; |
| 4214 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4215 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4216 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4217 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4218 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4219 | ssl_dtls_replay_reset( ssl ); |
| 4220 | #endif |
| 4221 | |
| 4222 | /* Increment epoch */ |
| 4223 | if( ++ssl->in_epoch == 0 ) |
| 4224 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4225 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 4226 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4227 | } |
| 4228 | } |
| 4229 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4230 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4231 | memset( ssl->in_ctr, 0, 8 ); |
| 4232 | |
| 4233 | /* |
| 4234 | * Set the in_msg pointer to the correct location based on IV length |
| 4235 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4236 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4237 | { |
| 4238 | ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen - |
| 4239 | ssl->transform_negotiate->fixed_ivlen; |
| 4240 | } |
| 4241 | else |
| 4242 | ssl->in_msg = ssl->in_iv; |
| 4243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4244 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4245 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4246 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4247 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4248 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4249 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 4250 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4251 | } |
| 4252 | } |
| 4253 | #endif |
| 4254 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4255 | ssl->state++; |
| 4256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4257 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4258 | |
| 4259 | return( 0 ); |
| 4260 | } |
| 4261 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4262 | void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl, |
| 4263 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4264 | { |
Paul Bakker | fb08fd2 | 2013-08-27 15:06:26 +0200 | [diff] [blame] | 4265 | ((void) ciphersuite_info); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 4266 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4267 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4268 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4269 | if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4270 | ssl->handshake->update_checksum = ssl_update_checksum_md5sha1; |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4271 | else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4272 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4273 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4274 | #if defined(MBEDTLS_SHA512_C) |
| 4275 | if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 ) |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4276 | ssl->handshake->update_checksum = ssl_update_checksum_sha384; |
| 4277 | else |
| 4278 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4279 | #if defined(MBEDTLS_SHA256_C) |
| 4280 | if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4281 | ssl->handshake->update_checksum = ssl_update_checksum_sha256; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4282 | else |
| 4283 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4284 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 4285 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4286 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4287 | return; |
Manuel Pégourié-Gonnard | 61edffe | 2014-04-11 17:07:31 +0200 | [diff] [blame] | 4288 | } |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4289 | } |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 4290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4291 | void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 4292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4293 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4294 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4295 | mbedtls_md5_starts( &ssl->handshake->fin_md5 ); |
| 4296 | mbedtls_sha1_starts( &ssl->handshake->fin_sha1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 4297 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4298 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4299 | #if defined(MBEDTLS_SHA256_C) |
| 4300 | mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 4301 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4302 | #if defined(MBEDTLS_SHA512_C) |
| 4303 | mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 ); |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 4304 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4305 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 67427c0 | 2014-07-11 13:45:34 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4308 | static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4309 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4310 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4311 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4312 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4313 | mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len ); |
| 4314 | mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4315 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4316 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4317 | #if defined(MBEDTLS_SHA256_C) |
| 4318 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4319 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4320 | #if defined(MBEDTLS_SHA512_C) |
| 4321 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 4322 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4323 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4326 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4327 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4328 | static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4329 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4330 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4331 | mbedtls_md5_update( &ssl->handshake->fin_md5 , buf, len ); |
| 4332 | mbedtls_sha1_update( &ssl->handshake->fin_sha1, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4333 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4334 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4337 | #if defined(MBEDTLS_SHA256_C) |
| 4338 | static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4339 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4340 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4341 | mbedtls_sha256_update( &ssl->handshake->fin_sha256, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4342 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 4343 | #endif |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4345 | #if defined(MBEDTLS_SHA512_C) |
| 4346 | static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4347 | const unsigned char *buf, size_t len ) |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4348 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4349 | mbedtls_sha512_update( &ssl->handshake->fin_sha512, buf, len ); |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4350 | } |
Paul Bakker | 769075d | 2012-11-24 11:26:46 +0100 | [diff] [blame] | 4351 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4352 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 380da53 | 2012-04-18 16:10:25 +0000 | [diff] [blame] | 4353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4354 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4355 | static void ssl_calc_finished_ssl( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4356 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4357 | { |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4358 | const char *sender; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4359 | mbedtls_md5_context mbedtls_md5; |
| 4360 | mbedtls_sha1_context mbedtls_sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4361 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4362 | unsigned char padbuf[48]; |
| 4363 | unsigned char md5sum[16]; |
| 4364 | unsigned char sha1sum[20]; |
| 4365 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4366 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4367 | if( !session ) |
| 4368 | session = ssl->session; |
| 4369 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4370 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4372 | memcpy( &mbedtls_md5 , &ssl->handshake->fin_md5 , sizeof(mbedtls_md5_context) ); |
| 4373 | memcpy( &mbedtls_sha1, &ssl->handshake->fin_sha1, sizeof(mbedtls_sha1_context) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4374 | |
| 4375 | /* |
| 4376 | * SSLv3: |
| 4377 | * hash = |
| 4378 | * MD5( master + pad2 + |
| 4379 | * MD5( handshake + sender + master + pad1 ) ) |
| 4380 | * + SHA1( master + pad2 + |
| 4381 | * SHA1( handshake + sender + master + pad1 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4382 | */ |
| 4383 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4384 | #if !defined(MBEDTLS_MD5_ALT) |
| 4385 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished mbedtls_md5 state", (unsigned char *) |
| 4386 | mbedtls_md5.state, sizeof( mbedtls_md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4387 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4389 | #if !defined(MBEDTLS_SHA1_ALT) |
| 4390 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished mbedtls_sha1 state", (unsigned char *) |
| 4391 | mbedtls_sha1.state, sizeof( mbedtls_sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4392 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4393 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4394 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT" |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4395 | : "SRVR"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4396 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4397 | memset( padbuf, 0x36, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4399 | mbedtls_md5_update( &mbedtls_md5, (const unsigned char *) sender, 4 ); |
| 4400 | mbedtls_md5_update( &mbedtls_md5, session->master, 48 ); |
| 4401 | mbedtls_md5_update( &mbedtls_md5, padbuf, 48 ); |
| 4402 | mbedtls_md5_finish( &mbedtls_md5, md5sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4404 | mbedtls_sha1_update( &mbedtls_sha1, (const unsigned char *) sender, 4 ); |
| 4405 | mbedtls_sha1_update( &mbedtls_sha1, session->master, 48 ); |
| 4406 | mbedtls_sha1_update( &mbedtls_sha1, padbuf, 40 ); |
| 4407 | mbedtls_sha1_finish( &mbedtls_sha1, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4408 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4409 | memset( padbuf, 0x5C, 48 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4410 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4411 | mbedtls_md5_starts( &mbedtls_md5 ); |
| 4412 | mbedtls_md5_update( &mbedtls_md5, session->master, 48 ); |
| 4413 | mbedtls_md5_update( &mbedtls_md5, padbuf, 48 ); |
| 4414 | mbedtls_md5_update( &mbedtls_md5, md5sum, 16 ); |
| 4415 | mbedtls_md5_finish( &mbedtls_md5, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4417 | mbedtls_sha1_starts( &mbedtls_sha1 ); |
| 4418 | mbedtls_sha1_update( &mbedtls_sha1, session->master, 48 ); |
| 4419 | mbedtls_sha1_update( &mbedtls_sha1, padbuf , 40 ); |
| 4420 | mbedtls_sha1_update( &mbedtls_sha1, sha1sum, 20 ); |
| 4421 | mbedtls_sha1_finish( &mbedtls_sha1, buf + 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4423 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4425 | mbedtls_md5_free( &mbedtls_md5 ); |
| 4426 | mbedtls_sha1_free( &mbedtls_sha1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4428 | mbedtls_zeroize( padbuf, sizeof( padbuf ) ); |
| 4429 | mbedtls_zeroize( md5sum, sizeof( md5sum ) ); |
| 4430 | mbedtls_zeroize( sha1sum, sizeof( sha1sum ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4432 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4433 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4434 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4436 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4437 | static void ssl_calc_finished_tls( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4438 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4439 | { |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4440 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4441 | const char *sender; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4442 | mbedtls_md5_context mbedtls_md5; |
| 4443 | mbedtls_sha1_context mbedtls_sha1; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4444 | unsigned char padbuf[36]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4446 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4447 | if( !session ) |
| 4448 | session = ssl->session; |
| 4449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4450 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4451 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4452 | memcpy( &mbedtls_md5 , &ssl->handshake->fin_md5 , sizeof(mbedtls_md5_context) ); |
| 4453 | memcpy( &mbedtls_sha1, &ssl->handshake->fin_sha1, sizeof(mbedtls_sha1_context) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4454 | |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4455 | /* |
| 4456 | * TLSv1: |
| 4457 | * hash = PRF( master, finished_label, |
| 4458 | * MD5( handshake ) + SHA1( handshake ) )[0..11] |
| 4459 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4461 | #if !defined(MBEDTLS_MD5_ALT) |
| 4462 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished mbedtls_md5 state", (unsigned char *) |
| 4463 | mbedtls_md5.state, sizeof( mbedtls_md5.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4464 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4466 | #if !defined(MBEDTLS_SHA1_ALT) |
| 4467 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished mbedtls_sha1 state", (unsigned char *) |
| 4468 | mbedtls_sha1.state, sizeof( mbedtls_sha1.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4469 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4471 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4472 | ? "client finished" |
| 4473 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4475 | mbedtls_md5_finish( &mbedtls_md5, padbuf ); |
| 4476 | mbedtls_sha1_finish( &mbedtls_sha1, padbuf + 16 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4477 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4478 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4479 | padbuf, 36, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4481 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4483 | mbedtls_md5_free( &mbedtls_md5 ); |
| 4484 | mbedtls_sha1_free( &mbedtls_sha1 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4486 | mbedtls_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4488 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4489 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4490 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4492 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4493 | #if defined(MBEDTLS_SHA256_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4494 | static void ssl_calc_finished_tls_sha256( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4495 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4496 | { |
| 4497 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4498 | const char *sender; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4499 | mbedtls_sha256_context mbedtls_sha256; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4500 | unsigned char padbuf[32]; |
| 4501 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4502 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4503 | if( !session ) |
| 4504 | session = ssl->session; |
| 4505 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4506 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls mbedtls_sha256" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4508 | memcpy( &mbedtls_sha256, &ssl->handshake->fin_sha256, sizeof(mbedtls_sha256_context) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4509 | |
| 4510 | /* |
| 4511 | * TLSv1.2: |
| 4512 | * hash = PRF( master, finished_label, |
| 4513 | * Hash( handshake ) )[0.11] |
| 4514 | */ |
| 4515 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4516 | #if !defined(MBEDTLS_SHA256_ALT) |
| 4517 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *) |
| 4518 | mbedtls_sha256.state, sizeof( mbedtls_sha256.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4519 | #endif |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4521 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4522 | ? "client finished" |
| 4523 | : "server finished"; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4524 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4525 | mbedtls_sha256_finish( &mbedtls_sha256, padbuf ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4526 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4527 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4528 | padbuf, 32, buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4530 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4532 | mbedtls_sha256_free( &mbedtls_sha256 ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4534 | mbedtls_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4536 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4537 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4538 | #endif /* MBEDTLS_SHA256_C */ |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4539 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4540 | #if defined(MBEDTLS_SHA512_C) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4541 | static void ssl_calc_finished_tls_sha384( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4542 | mbedtls_ssl_context *ssl, unsigned char *buf, int from ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4543 | { |
| 4544 | int len = 12; |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4545 | const char *sender; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4546 | mbedtls_sha512_context mbedtls_sha512; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4547 | unsigned char padbuf[48]; |
| 4548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4549 | mbedtls_ssl_session *session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4550 | if( !session ) |
| 4551 | session = ssl->session; |
| 4552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4553 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4555 | memcpy( &mbedtls_sha512, &ssl->handshake->fin_sha512, sizeof(mbedtls_sha512_context) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4556 | |
| 4557 | /* |
| 4558 | * TLSv1.2: |
| 4559 | * hash = PRF( master, finished_label, |
| 4560 | * Hash( handshake ) )[0.11] |
| 4561 | */ |
| 4562 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4563 | #if !defined(MBEDTLS_SHA512_ALT) |
| 4564 | MBEDTLS_SSL_DEBUG_BUF( 4, "finished mbedtls_sha512 state", (unsigned char *) |
| 4565 | mbedtls_sha512.state, sizeof( mbedtls_sha512.state ) ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 4566 | #endif |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4568 | sender = ( from == MBEDTLS_SSL_IS_CLIENT ) |
Paul Bakker | 3c2122f | 2013-06-24 19:03:14 +0200 | [diff] [blame] | 4569 | ? "client finished" |
| 4570 | : "server finished"; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4571 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4572 | mbedtls_sha512_finish( &mbedtls_sha512, padbuf ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4573 | |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 4574 | ssl->handshake->tls_prf( session->master, 48, sender, |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4575 | padbuf, 48, buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4577 | MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4578 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4579 | mbedtls_sha512_free( &mbedtls_sha512 ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4581 | mbedtls_zeroize( padbuf, sizeof( padbuf ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4582 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4583 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4584 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4585 | #endif /* MBEDTLS_SHA512_C */ |
| 4586 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 4587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4588 | static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4589 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4590 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4591 | |
| 4592 | /* |
| 4593 | * Free our handshake params |
| 4594 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4595 | mbedtls_ssl_handshake_free( ssl->handshake ); |
| 4596 | mbedtls_free( ssl->handshake ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4597 | ssl->handshake = NULL; |
| 4598 | |
| 4599 | /* |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4600 | * Free the previous transform and swith in the current one |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4601 | */ |
| 4602 | if( ssl->transform ) |
| 4603 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4604 | mbedtls_ssl_transform_free( ssl->transform ); |
| 4605 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4606 | } |
| 4607 | ssl->transform = ssl->transform_negotiate; |
| 4608 | ssl->transform_negotiate = NULL; |
| 4609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4610 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4611 | } |
| 4612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4613 | void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4614 | { |
| 4615 | int resume = ssl->handshake->resume; |
| 4616 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4617 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4619 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4620 | if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4621 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4622 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4623 | ssl->renego_records_seen = 0; |
| 4624 | } |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4625 | #endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4626 | |
| 4627 | /* |
| 4628 | * Free the previous session and switch in the current one |
| 4629 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4630 | if( ssl->session ) |
| 4631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4632 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 1a03473 | 2014-11-04 17:36:18 +0100 | [diff] [blame] | 4633 | /* RFC 7366 3.1: keep the EtM state */ |
| 4634 | ssl->session_negotiate->encrypt_then_mac = |
| 4635 | ssl->session->encrypt_then_mac; |
| 4636 | #endif |
| 4637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4638 | mbedtls_ssl_session_free( ssl->session ); |
| 4639 | mbedtls_free( ssl->session ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4640 | } |
| 4641 | ssl->session = ssl->session_negotiate; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4642 | ssl->session_negotiate = NULL; |
| 4643 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4644 | /* |
| 4645 | * Add cache entry |
| 4646 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4647 | if( ssl->conf->f_set_cache != NULL && |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 4648 | ssl->session->id_len != 0 && |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 4649 | resume == 0 ) |
| 4650 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 4651 | if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4652 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) ); |
Manuel Pégourié-Gonnard | c086cce | 2013-08-02 14:13:02 +0200 | [diff] [blame] | 4653 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4655 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4656 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4657 | ssl->handshake->flight != NULL ) |
| 4658 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 4659 | /* Cancel handshake timer */ |
| 4660 | ssl_set_timer( ssl, 0 ); |
| 4661 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4662 | /* Keep last flight around in case we need to resend it: |
| 4663 | * we need the handshake and transform structures for that */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4664 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) ); |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4665 | } |
| 4666 | else |
| 4667 | #endif |
| 4668 | ssl_handshake_wrapup_free_hs_transform( ssl ); |
| 4669 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4670 | ssl->state++; |
| 4671 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4672 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4675 | int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4676 | { |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 4677 | int ret, hash_len; |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4680 | |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 4681 | /* |
| 4682 | * Set the out_msg pointer to the correct location based on IV length |
| 4683 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4684 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 ) |
Paul Bakker | 92be97b | 2013-01-02 17:30:03 +0100 | [diff] [blame] | 4685 | { |
| 4686 | ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen - |
| 4687 | ssl->transform_negotiate->fixed_ivlen; |
| 4688 | } |
| 4689 | else |
| 4690 | ssl->out_msg = ssl->out_iv; |
| 4691 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4692 | ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint ); |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 4693 | |
| 4694 | // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4695 | hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4697 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4698 | ssl->verify_data_len = hash_len; |
| 4699 | memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4700 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4701 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4702 | ssl->out_msglen = 4 + hash_len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4703 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 4704 | ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4705 | |
| 4706 | /* |
| 4707 | * In case of session resuming, invert the client and server |
| 4708 | * ChangeCipherSpec messages order. |
| 4709 | */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4710 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4712 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4713 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4714 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 4715 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4716 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4717 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4718 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 4719 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4720 | } |
| 4721 | else |
| 4722 | ssl->state++; |
| 4723 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4724 | /* |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 4725 | * Switch to our negotiated transform and session parameters for outbound |
| 4726 | * data. |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4727 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4728 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) ); |
Manuel Pégourié-Gonnard | 5afb167 | 2014-02-16 18:33:22 +0100 | [diff] [blame] | 4729 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4730 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4731 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 4732 | { |
| 4733 | unsigned char i; |
| 4734 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4735 | /* Remember current epoch settings for resending */ |
| 4736 | ssl->handshake->alt_transform_out = ssl->transform_out; |
| 4737 | memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 ); |
| 4738 | |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 4739 | /* Set sequence_number to zero */ |
| 4740 | memset( ssl->out_ctr + 2, 0, 6 ); |
| 4741 | |
| 4742 | /* Increment epoch */ |
| 4743 | for( i = 2; i > 0; i-- ) |
| 4744 | if( ++ssl->out_ctr[i - 1] != 0 ) |
| 4745 | break; |
| 4746 | |
| 4747 | /* The loop goes to its end iff the counter is wrapping */ |
| 4748 | if( i == 0 ) |
| 4749 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4750 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
| 4751 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 4752 | } |
| 4753 | } |
| 4754 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4755 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 4756 | memset( ssl->out_ctr, 0, 8 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4757 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4758 | ssl->transform_out = ssl->transform_negotiate; |
| 4759 | ssl->session_out = ssl->session_negotiate; |
| 4760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4761 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 4762 | if( mbedtls_ssl_hw_record_activate != NULL ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 4763 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4764 | if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 ) |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 4765 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4766 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret ); |
| 4767 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 07eb38b | 2012-12-19 14:42:06 +0100 | [diff] [blame] | 4768 | } |
| 4769 | } |
| 4770 | #endif |
| 4771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4772 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4773 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4774 | mbedtls_ssl_send_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 4775 | #endif |
| 4776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4777 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4778 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4779 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4780 | return( ret ); |
| 4781 | } |
| 4782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4783 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4784 | |
| 4785 | return( 0 ); |
| 4786 | } |
| 4787 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4788 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 4789 | #define SSL_MAX_HASH_LEN 36 |
| 4790 | #else |
| 4791 | #define SSL_MAX_HASH_LEN 12 |
| 4792 | #endif |
| 4793 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4794 | int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4795 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 4796 | int ret; |
Manuel Pégourié-Gonnard | 879a4f9 | 2014-07-11 22:31:12 +0200 | [diff] [blame] | 4797 | unsigned int hash_len; |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 4798 | unsigned char buf[SSL_MAX_HASH_LEN]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4800 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4801 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4802 | ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4803 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4804 | if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4805 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4806 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4807 | return( ret ); |
| 4808 | } |
| 4809 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4810 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4811 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4812 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 4813 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 4816 | /* There is currently no ciphersuite using another length with TLS 1.2 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4817 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 4818 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Manuel Pégourié-Gonnard | ca6440b | 2014-09-10 12:39:54 +0000 | [diff] [blame] | 4819 | hash_len = 36; |
| 4820 | else |
| 4821 | #endif |
| 4822 | hash_len = 12; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4824 | if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED || |
| 4825 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4826 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4827 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 4828 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4831 | if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), |
Manuel Pégourié-Gonnard | 4abc327 | 2014-09-10 12:02:46 +0000 | [diff] [blame] | 4832 | buf, hash_len ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4833 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) ); |
| 4835 | return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4838 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4839 | ssl->verify_data_len = hash_len; |
| 4840 | memcpy( ssl->peer_verify_data, buf, hash_len ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 4841 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4842 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 4843 | if( ssl->handshake->resume != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4844 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4845 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4846 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4847 | ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 4848 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4849 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4850 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4851 | ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP; |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 4852 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4853 | } |
| 4854 | else |
| 4855 | ssl->state++; |
| 4856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4857 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4858 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4859 | mbedtls_ssl_recv_flight_completed( ssl ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4860 | #endif |
| 4861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4862 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4863 | |
| 4864 | return( 0 ); |
| 4865 | } |
| 4866 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4867 | static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4868 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4869 | memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4870 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4871 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \ |
| 4872 | defined(MBEDTLS_SSL_PROTO_TLS1_1) |
| 4873 | mbedtls_md5_init( &handshake->fin_md5 ); |
| 4874 | mbedtls_sha1_init( &handshake->fin_sha1 ); |
| 4875 | mbedtls_md5_starts( &handshake->fin_md5 ); |
| 4876 | mbedtls_sha1_starts( &handshake->fin_sha1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4877 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4878 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4879 | #if defined(MBEDTLS_SHA256_C) |
| 4880 | mbedtls_sha256_init( &handshake->fin_sha256 ); |
| 4881 | mbedtls_sha256_starts( &handshake->fin_sha256, 0 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4882 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4883 | #if defined(MBEDTLS_SHA512_C) |
| 4884 | mbedtls_sha512_init( &handshake->fin_sha512 ); |
| 4885 | mbedtls_sha512_starts( &handshake->fin_sha512, 1 ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4886 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4887 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4888 | |
| 4889 | handshake->update_checksum = ssl_update_checksum_start; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4890 | handshake->sig_alg = MBEDTLS_SSL_HASH_SHA1; |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4891 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4892 | #if defined(MBEDTLS_DHM_C) |
| 4893 | mbedtls_dhm_init( &handshake->dhm_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4894 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4895 | #if defined(MBEDTLS_ECDH_C) |
| 4896 | mbedtls_ecdh_init( &handshake->ecdh_ctx ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4897 | #endif |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 4898 | |
| 4899 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 4900 | handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET; |
| 4901 | #endif |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4902 | } |
| 4903 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4904 | static void ssl_transform_init( mbedtls_ssl_transform *transform ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4905 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4906 | memset( transform, 0, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 4907 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4908 | mbedtls_cipher_init( &transform->cipher_ctx_enc ); |
| 4909 | mbedtls_cipher_init( &transform->cipher_ctx_dec ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 4910 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4911 | mbedtls_md_init( &transform->md_ctx_enc ); |
| 4912 | mbedtls_md_init( &transform->md_ctx_dec ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4913 | } |
| 4914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4915 | void mbedtls_ssl_session_init( mbedtls_ssl_session *session ) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4916 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4917 | memset( session, 0, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4918 | } |
| 4919 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4920 | static int ssl_handshake_init( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4921 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4922 | /* Clear old handshake information if present */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4923 | if( ssl->transform_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4924 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4925 | if( ssl->session_negotiate ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4926 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4927 | if( ssl->handshake ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4928 | mbedtls_ssl_handshake_free( ssl->handshake ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4929 | |
| 4930 | /* |
| 4931 | * Either the pointers are now NULL or cleared properly and can be freed. |
| 4932 | * Now allocate missing structures. |
| 4933 | */ |
| 4934 | if( ssl->transform_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 4935 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4936 | ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 4937 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4938 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4939 | if( ssl->session_negotiate == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 4940 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4941 | ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 4942 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4943 | |
Paul Bakker | 82788fb | 2014-10-20 13:59:19 +0200 | [diff] [blame] | 4944 | if( ssl->handshake == NULL ) |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 4945 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 4946 | ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) ); |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 4947 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4948 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4949 | /* All pointers should exist and can be directly freed without issue */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4950 | if( ssl->handshake == NULL || |
| 4951 | ssl->transform_negotiate == NULL || |
| 4952 | ssl->session_negotiate == NULL ) |
| 4953 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 4954 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4956 | mbedtls_free( ssl->handshake ); |
| 4957 | mbedtls_free( ssl->transform_negotiate ); |
| 4958 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4959 | |
| 4960 | ssl->handshake = NULL; |
| 4961 | ssl->transform_negotiate = NULL; |
| 4962 | ssl->session_negotiate = NULL; |
| 4963 | |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 4964 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4967 | /* Initialize structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4968 | mbedtls_ssl_session_init( ssl->session_negotiate ); |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 4969 | ssl_transform_init( ssl->transform_negotiate ); |
Paul Bakker | 968afaa | 2014-07-09 11:09:24 +0200 | [diff] [blame] | 4970 | ssl_handshake_params_init( ssl->handshake ); |
| 4971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4972 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 4973 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4974 | { |
| 4975 | ssl->handshake->alt_transform_out = ssl->transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4976 | |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 4977 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4978 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
| 4979 | else |
| 4980 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 4981 | |
| 4982 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 06939ce | 2015-05-11 11:25:46 +0200 | [diff] [blame] | 4983 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 4984 | #endif |
| 4985 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4986 | return( 0 ); |
| 4987 | } |
| 4988 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 4989 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 4990 | /* Dummy cookie callbacks for defaults */ |
| 4991 | static int ssl_cookie_write_dummy( void *ctx, |
| 4992 | unsigned char **p, unsigned char *end, |
| 4993 | const unsigned char *cli_id, size_t cli_id_len ) |
| 4994 | { |
| 4995 | ((void) ctx); |
| 4996 | ((void) p); |
| 4997 | ((void) end); |
| 4998 | ((void) cli_id); |
| 4999 | ((void) cli_id_len); |
| 5000 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5001 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | static int ssl_cookie_check_dummy( void *ctx, |
| 5005 | const unsigned char *cookie, size_t cookie_len, |
| 5006 | const unsigned char *cli_id, size_t cli_id_len ) |
| 5007 | { |
| 5008 | ((void) ctx); |
| 5009 | ((void) cookie); |
| 5010 | ((void) cookie_len); |
| 5011 | ((void) cli_id); |
| 5012 | ((void) cli_id_len); |
| 5013 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5014 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 5015 | } |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5016 | #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 7d38d21 | 2014-07-23 17:52:09 +0200 | [diff] [blame] | 5017 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5018 | /* |
| 5019 | * Initialize an SSL context |
| 5020 | */ |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 5021 | void mbedtls_ssl_init( mbedtls_ssl_context *ssl ) |
| 5022 | { |
| 5023 | memset( ssl, 0, sizeof( mbedtls_ssl_context ) ); |
| 5024 | } |
| 5025 | |
| 5026 | /* |
| 5027 | * Setup an SSL context |
| 5028 | */ |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 5029 | int mbedtls_ssl_setup( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 1897af9 | 2015-05-10 23:27:38 +0200 | [diff] [blame] | 5030 | const mbedtls_ssl_config *conf ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5031 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5032 | int ret; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 5033 | const size_t len = MBEDTLS_SSL_BUFFER_LEN; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5034 | |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 5035 | ssl->conf = conf; |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 5036 | |
| 5037 | /* |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 5038 | * Prepare base structures |
Paul Bakker | 62f2dee | 2012-09-28 07:31:51 +0000 | [diff] [blame] | 5039 | */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5040 | if( ( ssl-> in_buf = mbedtls_calloc( 1, len ) ) == NULL || |
| 5041 | ( ssl->out_buf = mbedtls_calloc( 1, len ) ) == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5042 | { |
Manuel Pégourié-Gonnard | b2a18a2 | 2015-05-27 16:29:56 +0200 | [diff] [blame] | 5043 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5044 | mbedtls_free( ssl->in_buf ); |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 5045 | ssl->in_buf = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5046 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 5049 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5050 | if( conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5051 | { |
| 5052 | ssl->out_hdr = ssl->out_buf; |
| 5053 | ssl->out_ctr = ssl->out_buf + 3; |
| 5054 | ssl->out_len = ssl->out_buf + 11; |
| 5055 | ssl->out_iv = ssl->out_buf + 13; |
| 5056 | ssl->out_msg = ssl->out_buf + 13; |
| 5057 | |
| 5058 | ssl->in_hdr = ssl->in_buf; |
| 5059 | ssl->in_ctr = ssl->in_buf + 3; |
| 5060 | ssl->in_len = ssl->in_buf + 11; |
| 5061 | ssl->in_iv = ssl->in_buf + 13; |
| 5062 | ssl->in_msg = ssl->in_buf + 13; |
| 5063 | } |
| 5064 | else |
| 5065 | #endif |
| 5066 | { |
| 5067 | ssl->out_ctr = ssl->out_buf; |
| 5068 | ssl->out_hdr = ssl->out_buf + 8; |
| 5069 | ssl->out_len = ssl->out_buf + 11; |
| 5070 | ssl->out_iv = ssl->out_buf + 13; |
| 5071 | ssl->out_msg = ssl->out_buf + 13; |
| 5072 | |
| 5073 | ssl->in_ctr = ssl->in_buf; |
| 5074 | ssl->in_hdr = ssl->in_buf + 8; |
| 5075 | ssl->in_len = ssl->in_buf + 11; |
| 5076 | ssl->in_iv = ssl->in_buf + 13; |
| 5077 | ssl->in_msg = ssl->in_buf + 13; |
| 5078 | } |
| 5079 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5080 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5081 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5082 | |
| 5083 | return( 0 ); |
| 5084 | } |
| 5085 | |
| 5086 | /* |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5087 | * Reset an initialized and used SSL context for re-use while retaining |
| 5088 | * all application-set variables, function pointers and data. |
| 5089 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5090 | int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl ) |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5091 | { |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5092 | int ret; |
| 5093 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5094 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 5095 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 5096 | /* Cancel any possibly running timer */ |
| 5097 | ssl_set_timer( ssl, 0 ); |
| 5098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5099 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5100 | ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 5101 | ssl->renego_records_seen = 0; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5102 | |
| 5103 | ssl->verify_data_len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5104 | memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
| 5105 | memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN ); |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 5106 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5107 | ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5108 | |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5109 | ssl->in_offt = NULL; |
| 5110 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 5111 | ssl->in_msg = ssl->in_buf + 13; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5112 | ssl->in_msgtype = 0; |
| 5113 | ssl->in_msglen = 0; |
| 5114 | ssl->in_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5115 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5116 | ssl->next_record_offset = 0; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 5117 | ssl->in_epoch = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 5118 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5119 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 5120 | ssl_dtls_replay_reset( ssl ); |
| 5121 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5122 | |
| 5123 | ssl->in_hslen = 0; |
| 5124 | ssl->nb_zero = 0; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5125 | ssl->record_read = 0; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5126 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 5127 | ssl->out_msg = ssl->out_buf + 13; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5128 | ssl->out_msgtype = 0; |
| 5129 | ssl->out_msglen = 0; |
| 5130 | ssl->out_left = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5131 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 5132 | if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 5133 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5134 | #endif |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5135 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5136 | ssl->transform_in = NULL; |
| 5137 | ssl->transform_out = NULL; |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5139 | memset( ssl->out_buf, 0, MBEDTLS_SSL_BUFFER_LEN ); |
| 5140 | memset( ssl->in_buf, 0, MBEDTLS_SSL_BUFFER_LEN ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5142 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 5143 | if( mbedtls_ssl_hw_record_reset != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5144 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5145 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) ); |
| 5146 | if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5147 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5148 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret ); |
| 5149 | return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5150 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 5151 | } |
| 5152 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5153 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5154 | if( ssl->transform ) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5156 | mbedtls_ssl_transform_free( ssl->transform ); |
| 5157 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5158 | ssl->transform = NULL; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5159 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5160 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 5161 | if( ssl->session ) |
| 5162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5163 | mbedtls_ssl_session_free( ssl->session ); |
| 5164 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 5165 | ssl->session = NULL; |
| 5166 | } |
| 5167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5168 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 5169 | ssl->alpn_chosen = NULL; |
| 5170 | #endif |
| 5171 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 5172 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5173 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 5174 | ssl->cli_id = NULL; |
| 5175 | ssl->cli_id_len = 0; |
| 5176 | #endif |
| 5177 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5178 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5179 | return( ret ); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 5180 | |
| 5181 | return( 0 ); |
Paul Bakker | 7eb013f | 2011-10-06 12:37:39 +0000 | [diff] [blame] | 5182 | } |
| 5183 | |
Manuel Pégourié-Gonnard | 779e429 | 2013-08-03 13:50:48 +0200 | [diff] [blame] | 5184 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5185 | * SSL set accessors |
| 5186 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5187 | void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5188 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5189 | conf->endpoint = endpoint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 5192 | void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport ) |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 5193 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5194 | conf->transport = transport; |
Manuel Pégourié-Gonnard | 0b1ff29 | 2014-02-06 13:04:16 +0100 | [diff] [blame] | 5195 | } |
| 5196 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5197 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5198 | void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 5199 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5200 | conf->anti_replay = mode; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 5201 | } |
| 5202 | #endif |
| 5203 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5204 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5205 | void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5206 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5207 | conf->badmac_limit = limit; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 5208 | } |
| 5209 | #endif |
| 5210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5211 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5212 | void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max ) |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 5213 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5214 | conf->hs_timeout_min = min; |
| 5215 | conf->hs_timeout_max = max; |
Manuel Pégourié-Gonnard | 905dd24 | 2014-10-01 12:03:55 +0200 | [diff] [blame] | 5216 | } |
| 5217 | #endif |
| 5218 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5219 | void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5220 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5221 | conf->authmode = authmode; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5224 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5225 | void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5226 | int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *), |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 5227 | void *p_vrfy ) |
| 5228 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5229 | conf->f_vrfy = f_vrfy; |
| 5230 | conf->p_vrfy = p_vrfy; |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 5231 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5232 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b63b0af | 2011-01-13 17:54:59 +0000 | [diff] [blame] | 5233 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5234 | void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 5235 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5236 | void *p_rng ) |
| 5237 | { |
Manuel Pégourié-Gonnard | 750e4d7 | 2015-05-07 12:35:38 +0100 | [diff] [blame] | 5238 | conf->f_rng = f_rng; |
| 5239 | conf->p_rng = p_rng; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5240 | } |
| 5241 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5242 | void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 5243 | void (*f_dbg)(void *, int, const char *, int, const char *), |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5244 | void *p_dbg ) |
| 5245 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5246 | conf->f_dbg = f_dbg; |
| 5247 | conf->p_dbg = p_dbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5250 | void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 5251 | void *p_bio, |
| 5252 | int (*f_send)(void *, const unsigned char *, size_t), |
| 5253 | int (*f_recv)(void *, unsigned char *, size_t), |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 5254 | int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t) ) |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 5255 | { |
| 5256 | ssl->p_bio = p_bio; |
| 5257 | ssl->f_send = f_send; |
| 5258 | ssl->f_recv = f_recv; |
| 5259 | ssl->f_recv_timeout = f_recv_timeout; |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 5260 | } |
| 5261 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5262 | void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout ) |
Manuel Pégourié-Gonnard | 97fd52c | 2015-05-06 15:38:52 +0100 | [diff] [blame] | 5263 | { |
| 5264 | conf->read_timeout = timeout; |
Manuel Pégourié-Gonnard | 8fa6dfd | 2014-09-17 10:47:43 +0200 | [diff] [blame] | 5265 | } |
| 5266 | |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 5267 | void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl, |
| 5268 | void *p_timer, |
| 5269 | void (*f_set_timer)(void *, uint32_t int_ms, uint32_t fin_ms), |
| 5270 | int (*f_get_timer)(void *) ) |
| 5271 | { |
| 5272 | ssl->p_timer = p_timer; |
| 5273 | ssl->f_set_timer = f_set_timer; |
| 5274 | ssl->f_get_timer = f_get_timer; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 5275 | |
| 5276 | /* Make sure we start with no timer running */ |
| 5277 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 5278 | } |
| 5279 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5280 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5281 | void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 5282 | void *p_cache, |
| 5283 | int (*f_get_cache)(void *, mbedtls_ssl_session *), |
| 5284 | int (*f_set_cache)(void *, const mbedtls_ssl_session *) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5285 | { |
Manuel Pégourié-Gonnard | 5cb3308 | 2015-05-06 18:06:26 +0100 | [diff] [blame] | 5286 | conf->p_cache = p_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5287 | conf->f_get_cache = f_get_cache; |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5288 | conf->f_set_cache = f_set_cache; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5289 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5290 | #endif /* MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5292 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5293 | int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5294 | { |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 5295 | int ret; |
| 5296 | |
| 5297 | if( ssl == NULL || |
| 5298 | session == NULL || |
| 5299 | ssl->session_negotiate == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5300 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 5301 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5302 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 ) |
| 5306 | return( ret ); |
| 5307 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 5308 | ssl->handshake->resume = 1; |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 5309 | |
| 5310 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5311 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5312 | #endif /* MBEDTLS_SSL_CLI_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5313 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5314 | void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5315 | const int *ciphersuites ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5316 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5317 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites; |
| 5318 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites; |
| 5319 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites; |
| 5320 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites; |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 5321 | } |
| 5322 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5323 | void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 5324 | const int *ciphersuites, |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 5325 | int major, int minor ) |
| 5326 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5327 | if( major != MBEDTLS_SSL_MAJOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 5328 | return; |
| 5329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5330 | if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 ) |
Paul Bakker | 8f4ddae | 2013-04-15 15:09:54 +0200 | [diff] [blame] | 5331 | return; |
| 5332 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5333 | conf->ciphersuite_list[minor] = ciphersuites; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5336 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 6e3ee3a | 2015-06-17 10:58:20 +0200 | [diff] [blame] | 5337 | void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, |
| 5338 | mbedtls_x509_crt_profile *profile ) |
| 5339 | { |
| 5340 | conf->cert_profile = profile; |
| 5341 | } |
| 5342 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5343 | /* Append a new keycert entry to a (possibly empty) list */ |
| 5344 | static int ssl_append_key_cert( mbedtls_ssl_key_cert **head, |
| 5345 | mbedtls_x509_crt *cert, |
| 5346 | mbedtls_pk_context *key ) |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5347 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5348 | mbedtls_ssl_key_cert *new; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5349 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5350 | new = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) ); |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5351 | if( new == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5352 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5353 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5354 | new->cert = cert; |
| 5355 | new->key = key; |
| 5356 | new->next = NULL; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5357 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5358 | /* Update head is the list was null, else add to the end */ |
| 5359 | if( *head == NULL ) |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 5360 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5361 | *head = new; |
Paul Bakker | 0333b97 | 2013-11-04 17:08:28 +0100 | [diff] [blame] | 5362 | } |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5363 | else |
| 5364 | { |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5365 | mbedtls_ssl_key_cert *cur = *head; |
| 5366 | while( cur->next != NULL ) |
| 5367 | cur = cur->next; |
| 5368 | cur->next = new; |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5369 | } |
| 5370 | |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5371 | return( 0 ); |
| 5372 | } |
| 5373 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5374 | int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 8f618a8 | 2015-05-10 21:13:36 +0200 | [diff] [blame] | 5375 | mbedtls_x509_crt *own_cert, |
| 5376 | mbedtls_pk_context *pk_key ) |
| 5377 | { |
Manuel Pégourié-Gonnard | 17a40cd | 2015-05-10 23:17:17 +0200 | [diff] [blame] | 5378 | return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) ); |
Manuel Pégourié-Gonnard | 834ea85 | 2013-09-23 14:46:13 +0200 | [diff] [blame] | 5379 | } |
| 5380 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5381 | void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 5382 | mbedtls_x509_crt *ca_chain, |
| 5383 | mbedtls_x509_crl *ca_crl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5384 | { |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 5385 | conf->ca_chain = ca_chain; |
| 5386 | conf->ca_crl = ca_crl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5387 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5388 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | eb2c658 | 2012-09-27 19:15:01 +0000 | [diff] [blame] | 5389 | |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 5390 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
| 5391 | int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl, |
| 5392 | mbedtls_x509_crt *own_cert, |
| 5393 | mbedtls_pk_context *pk_key ) |
| 5394 | { |
| 5395 | return( ssl_append_key_cert( &ssl->handshake->sni_key_cert, |
| 5396 | own_cert, pk_key ) ); |
| 5397 | } |
Manuel Pégourié-Gonnard | 22bfa4b | 2015-05-11 08:46:37 +0200 | [diff] [blame] | 5398 | |
| 5399 | void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl, |
| 5400 | mbedtls_x509_crt *ca_chain, |
| 5401 | mbedtls_x509_crl *ca_crl ) |
| 5402 | { |
| 5403 | ssl->handshake->sni_ca_chain = ca_chain; |
| 5404 | ssl->handshake->sni_ca_crl = ca_crl; |
| 5405 | } |
Manuel Pégourié-Gonnard | cdc26ae | 2015-06-19 12:16:31 +0200 | [diff] [blame] | 5406 | |
| 5407 | void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl, |
| 5408 | int authmode ) |
| 5409 | { |
| 5410 | ssl->handshake->sni_authmode = authmode; |
| 5411 | } |
Manuel Pégourié-Gonnard | 1af6c85 | 2015-05-10 23:10:37 +0200 | [diff] [blame] | 5412 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
| 5413 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5414 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5415 | int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5416 | const unsigned char *psk, size_t psk_len, |
| 5417 | const unsigned char *psk_identity, size_t psk_identity_len ) |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5418 | { |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5419 | if( psk == NULL || psk_identity == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5420 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5422 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 5423 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | b2bf5a1 | 2014-03-25 16:28:12 +0100 | [diff] [blame] | 5424 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 5425 | if( conf->psk != NULL || conf->psk_identity != NULL ) |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5426 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 5427 | mbedtls_free( conf->psk ); |
| 5428 | mbedtls_free( conf->psk_identity ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5429 | } |
| 5430 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5431 | if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL || |
| 5432 | ( conf->psk_identity = mbedtls_calloc( 1, psk_identity_len ) ) == NULL ) |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 5433 | { |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 5434 | mbedtls_free( conf->psk ); |
| 5435 | conf->psk = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5436 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Mansour Moufid | f81088b | 2015-02-17 13:10:21 -0500 | [diff] [blame] | 5437 | } |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5438 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 5439 | conf->psk_len = psk_len; |
| 5440 | conf->psk_identity_len = psk_identity_len; |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5441 | |
Manuel Pégourié-Gonnard | 120fdbd | 2015-05-07 17:07:50 +0100 | [diff] [blame] | 5442 | memcpy( conf->psk, psk, conf->psk_len ); |
| 5443 | memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len ); |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5444 | |
| 5445 | return( 0 ); |
| 5446 | } |
| 5447 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5448 | int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl, |
| 5449 | const unsigned char *psk, size_t psk_len ) |
| 5450 | { |
| 5451 | if( psk == NULL || ssl->handshake == NULL ) |
| 5452 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5453 | |
| 5454 | if( psk_len > MBEDTLS_PSK_MAX_LEN ) |
| 5455 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5456 | |
| 5457 | if( ssl->handshake->psk != NULL ) |
| 5458 | mbedtls_free( ssl->conf->psk ); |
| 5459 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5460 | if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL ) |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5461 | { |
| 5462 | mbedtls_free( ssl->handshake->psk ); |
| 5463 | ssl->handshake->psk = NULL; |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5464 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | ssl->handshake->psk_len = psk_len; |
| 5468 | memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len ); |
| 5469 | |
| 5470 | return( 0 ); |
| 5471 | } |
| 5472 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5473 | void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5474 | int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, |
Paul Bakker | 6db455e | 2013-09-18 17:29:31 +0200 | [diff] [blame] | 5475 | size_t), |
| 5476 | void *p_psk ) |
| 5477 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5478 | conf->f_psk = f_psk; |
| 5479 | conf->p_psk = p_psk; |
Paul Bakker | d4a56ec | 2013-04-16 18:05:29 +0200 | [diff] [blame] | 5480 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5481 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ |
Paul Bakker | 43b7e35 | 2011-01-18 15:27:19 +0000 | [diff] [blame] | 5482 | |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 5483 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5484 | int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5485 | { |
| 5486 | int ret; |
| 5487 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 5488 | if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 || |
| 5489 | ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 ) |
| 5490 | { |
| 5491 | mbedtls_mpi_free( &conf->dhm_P ); |
| 5492 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5493 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 5494 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5495 | |
| 5496 | return( 0 ); |
| 5497 | } |
| 5498 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5499 | int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx ) |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 5500 | { |
| 5501 | int ret; |
| 5502 | |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 5503 | if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 || |
| 5504 | ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 ) |
| 5505 | { |
| 5506 | mbedtls_mpi_free( &conf->dhm_P ); |
| 5507 | mbedtls_mpi_free( &conf->dhm_G ); |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 5508 | return( ret ); |
Manuel Pégourié-Gonnard | 1028b74 | 2015-05-06 17:33:07 +0100 | [diff] [blame] | 5509 | } |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 5510 | |
| 5511 | return( 0 ); |
| 5512 | } |
Manuel Pégourié-Gonnard | cf141ca | 2015-05-20 10:35:51 +0200 | [diff] [blame] | 5513 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 5514 | |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 5515 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 5516 | /* |
| 5517 | * Set the minimum length for Diffie-Hellman parameters |
| 5518 | */ |
| 5519 | void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf, |
| 5520 | unsigned int bitlen ) |
| 5521 | { |
| 5522 | conf->dhm_min_bitlen = bitlen; |
| 5523 | } |
| 5524 | #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ |
| 5525 | |
Manuel Pégourié-Gonnard | 36a8b57 | 2015-06-17 12:43:26 +0200 | [diff] [blame] | 5526 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED) |
| 5527 | /* |
| 5528 | * Set allowed/preferred hashes for handshake signatures |
| 5529 | */ |
| 5530 | void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf, |
| 5531 | const int *hashes ) |
| 5532 | { |
| 5533 | conf->sig_hashes = hashes; |
| 5534 | } |
| 5535 | #endif |
| 5536 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 5537 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 5538 | /* |
| 5539 | * Set the allowed elliptic curves |
| 5540 | */ |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5541 | void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5542 | const mbedtls_ecp_group_id *curve_list ) |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 5543 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5544 | conf->curve_list = curve_list; |
Manuel Pégourié-Gonnard | 7f38ed0 | 2014-02-04 15:52:33 +0100 | [diff] [blame] | 5545 | } |
| 5546 | #endif |
| 5547 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 5548 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5549 | int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5550 | { |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 5551 | size_t hostname_len; |
| 5552 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5553 | if( hostname == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5554 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5555 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 5556 | hostname_len = strlen( hostname ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 5557 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 5558 | if( hostname_len + 1 == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5559 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 75c1a6f | 2013-08-19 14:25:29 +0200 | [diff] [blame] | 5560 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 5561 | ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5562 | |
Paul Bakker | b15b851 | 2012-01-13 13:44:06 +0000 | [diff] [blame] | 5563 | if( ssl->hostname == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 5564 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Paul Bakker | b15b851 | 2012-01-13 13:44:06 +0000 | [diff] [blame] | 5565 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 5566 | memcpy( ssl->hostname, hostname, hostname_len ); |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 5567 | |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 5568 | ssl->hostname[hostname_len] = '\0'; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5569 | |
| 5570 | return( 0 ); |
| 5571 | } |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 5572 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5573 | |
Manuel Pégourié-Gonnard | bc2b771 | 2015-05-06 11:14:19 +0100 | [diff] [blame] | 5574 | #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5575 | void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5576 | int (*f_sni)(void *, mbedtls_ssl_context *, |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 5577 | const unsigned char *, size_t), |
| 5578 | void *p_sni ) |
| 5579 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5580 | conf->f_sni = f_sni; |
| 5581 | conf->p_sni = p_sni; |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 5582 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5583 | #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Paul Bakker | 5701cdc | 2012-09-27 21:49:42 +0000 | [diff] [blame] | 5584 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5585 | #if defined(MBEDTLS_SSL_ALPN) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5586 | int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos ) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 5587 | { |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 5588 | size_t cur_len, tot_len; |
| 5589 | const char **p; |
| 5590 | |
| 5591 | /* |
| 5592 | * "Empty strings MUST NOT be included and byte strings MUST NOT be |
| 5593 | * truncated". Check lengths now rather than later. |
| 5594 | */ |
| 5595 | tot_len = 0; |
| 5596 | for( p = protos; *p != NULL; p++ ) |
| 5597 | { |
| 5598 | cur_len = strlen( *p ); |
| 5599 | tot_len += cur_len; |
| 5600 | |
| 5601 | if( cur_len == 0 || cur_len > 255 || tot_len > 65535 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5602 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 5603 | } |
| 5604 | |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5605 | conf->alpn_list = protos; |
Manuel Pégourié-Gonnard | 0b874dc | 2014-04-07 10:57:45 +0200 | [diff] [blame] | 5606 | |
| 5607 | return( 0 ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 5608 | } |
| 5609 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5610 | const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 5611 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5612 | return( ssl->alpn_chosen ); |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 5613 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5614 | #endif /* MBEDTLS_SSL_ALPN */ |
Manuel Pégourié-Gonnard | 7e250d4 | 2014-04-04 16:08:41 +0200 | [diff] [blame] | 5615 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 5616 | void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 5617 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5618 | conf->max_major_ver = major; |
| 5619 | conf->max_minor_ver = minor; |
Paul Bakker | 490ecc8 | 2011-10-06 13:04:09 +0000 | [diff] [blame] | 5620 | } |
| 5621 | |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 5622 | void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor ) |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 5623 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5624 | conf->min_major_ver = major; |
| 5625 | conf->min_minor_ver = minor; |
Paul Bakker | 1d29fb5 | 2012-09-28 13:28:45 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5628 | #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5629 | void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback ) |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 5630 | { |
Manuel Pégourié-Gonnard | 684b059 | 2015-05-06 09:27:31 +0100 | [diff] [blame] | 5631 | conf->fallback = fallback; |
Manuel Pégourié-Gonnard | 1cbd39d | 2014-10-20 13:34:59 +0200 | [diff] [blame] | 5632 | } |
| 5633 | #endif |
| 5634 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5635 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5636 | void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm ) |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 5637 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5638 | conf->encrypt_then_mac = etm; |
Manuel Pégourié-Gonnard | 699cafa | 2014-10-27 13:57:03 +0100 | [diff] [blame] | 5639 | } |
| 5640 | #endif |
| 5641 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5642 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5643 | void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems ) |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 5644 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5645 | conf->extended_ms = ems; |
Manuel Pégourié-Gonnard | 367381f | 2014-10-20 18:40:56 +0200 | [diff] [blame] | 5646 | } |
| 5647 | #endif |
| 5648 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 5649 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5650 | void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 ) |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 5651 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5652 | conf->arc4_disabled = arc4; |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 5653 | } |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 5654 | #endif |
Manuel Pégourié-Gonnard | bd47a58 | 2015-01-12 13:43:29 +0100 | [diff] [blame] | 5655 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5656 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5657 | int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code ) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 5658 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5659 | if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID || |
| 5660 | mfl_code_to_length[mfl_code] > MBEDTLS_SSL_MAX_CONTENT_LEN ) |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 5661 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5662 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 5663 | } |
| 5664 | |
Manuel Pégourié-Gonnard | 6bf89d6 | 2015-05-05 17:01:57 +0100 | [diff] [blame] | 5665 | conf->mfl_code = mfl_code; |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 5666 | |
| 5667 | return( 0 ); |
| 5668 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5669 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Manuel Pégourié-Gonnard | 8b46459 | 2013-07-16 12:45:26 +0200 | [diff] [blame] | 5670 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5671 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Manuel Pégourié-Gonnard | 01e5e8c | 2015-05-11 10:11:56 +0200 | [diff] [blame] | 5672 | void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate ) |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 5673 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5674 | conf->trunc_hmac = truncate; |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 5675 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5676 | #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */ |
Manuel Pégourié-Gonnard | e980a99 | 2013-07-19 11:08:52 +0200 | [diff] [blame] | 5677 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5678 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5679 | void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split ) |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 5680 | { |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 5681 | conf->cbc_record_splitting = split; |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 5682 | } |
| 5683 | #endif |
| 5684 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5685 | void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5686 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5687 | conf->allow_legacy_renegotiation = allow_legacy; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5690 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5691 | void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation ) |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 5692 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5693 | conf->disable_renegotiation = renegotiation; |
Manuel Pégourié-Gonnard | 615e677 | 2014-11-03 08:23:14 +0100 | [diff] [blame] | 5694 | } |
| 5695 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5696 | void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5697 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5698 | conf->renego_max_records = max_records; |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5699 | } |
| 5700 | |
Manuel Pégourié-Gonnard | 6729e79 | 2015-05-11 09:50:24 +0200 | [diff] [blame] | 5701 | void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 5702 | const unsigned char period[8] ) |
| 5703 | { |
Manuel Pégourié-Gonnard | d36e33f | 2015-05-05 10:45:39 +0200 | [diff] [blame] | 5704 | memcpy( conf->renego_period, period, 8 ); |
Manuel Pégourié-Gonnard | 837f0fe | 2014-11-05 13:58:53 +0100 | [diff] [blame] | 5705 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5706 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5708 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5709 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5710 | void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets ) |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 5711 | { |
Manuel Pégourié-Gonnard | 2b49445 | 2015-05-06 10:05:11 +0100 | [diff] [blame] | 5712 | conf->session_tickets = use_tickets; |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 5713 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5714 | #endif |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 5715 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5716 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 5717 | void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf, |
| 5718 | mbedtls_ssl_ticket_write_t *f_ticket_write, |
| 5719 | mbedtls_ssl_ticket_parse_t *f_ticket_parse, |
| 5720 | void *p_ticket ) |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 5721 | { |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 5722 | conf->f_ticket_write = f_ticket_write; |
| 5723 | conf->f_ticket_parse = f_ticket_parse; |
| 5724 | conf->p_ticket = p_ticket; |
Paul Bakker | 606b4ba | 2013-08-14 16:52:14 +0200 | [diff] [blame] | 5725 | } |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 5726 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5727 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
Manuel Pégourié-Gonnard | aa0d4d1 | 2013-08-03 13:02:31 +0200 | [diff] [blame] | 5728 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5729 | /* |
| 5730 | * SSL get accessors |
| 5731 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5732 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5733 | { |
| 5734 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 5735 | } |
| 5736 | |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 5737 | uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5738 | { |
Manuel Pégourié-Gonnard | e89163c | 2015-01-23 14:30:57 +0000 | [diff] [blame] | 5739 | if( ssl->session != NULL ) |
| 5740 | return( ssl->session->verify_result ); |
| 5741 | |
| 5742 | if( ssl->session_negotiate != NULL ) |
| 5743 | return( ssl->session_negotiate->verify_result ); |
| 5744 | |
Manuel Pégourié-Gonnard | 6ab9b00 | 2015-05-14 11:25:04 +0200 | [diff] [blame] | 5745 | return( 0xFFFFFFFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5748 | const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 5749 | { |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 5750 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5751 | return( NULL ); |
Paul Bakker | 926c8e4 | 2013-03-06 10:23:34 +0100 | [diff] [blame] | 5752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5753 | return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5756 | const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5757 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5758 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5759 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5760 | { |
| 5761 | switch( ssl->minor_ver ) |
| 5762 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5763 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5764 | return( "DTLSv1.0" ); |
| 5765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5766 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5767 | return( "DTLSv1.2" ); |
| 5768 | |
| 5769 | default: |
| 5770 | return( "unknown (DTLS)" ); |
| 5771 | } |
| 5772 | } |
| 5773 | #endif |
| 5774 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5775 | switch( ssl->minor_ver ) |
| 5776 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5777 | case MBEDTLS_SSL_MINOR_VERSION_0: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5778 | return( "SSLv3.0" ); |
| 5779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5780 | case MBEDTLS_SSL_MINOR_VERSION_1: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5781 | return( "TLSv1.0" ); |
| 5782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5783 | case MBEDTLS_SSL_MINOR_VERSION_2: |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5784 | return( "TLSv1.1" ); |
| 5785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5786 | case MBEDTLS_SSL_MINOR_VERSION_3: |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 5787 | return( "TLSv1.2" ); |
| 5788 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5789 | default: |
Manuel Pégourié-Gonnard | b21ca2a | 2014-02-10 13:43:33 +0100 | [diff] [blame] | 5790 | return( "unknown" ); |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5791 | } |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5794 | int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5795 | { |
| 5796 | int transform_expansion; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5797 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5798 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5799 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
| 5800 | if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL ) |
| 5801 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5802 | #endif |
| 5803 | |
| 5804 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5805 | return( mbedtls_ssl_hdr_len( ssl ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5807 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5808 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5809 | case MBEDTLS_MODE_GCM: |
| 5810 | case MBEDTLS_MODE_CCM: |
| 5811 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5812 | transform_expansion = transform->minlen; |
| 5813 | break; |
| 5814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5815 | case MBEDTLS_MODE_CBC: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5816 | transform_expansion = transform->maclen |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5817 | + mbedtls_cipher_get_block_size( &transform->cipher_ctx_enc ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5818 | break; |
| 5819 | |
| 5820 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5821 | MBEDTLS_SSL_DEBUG_MSG( 0, ( "should never happen" ) ); |
| 5822 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5823 | } |
| 5824 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5825 | return( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5826 | } |
| 5827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5828 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 5829 | const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl ) |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5830 | { |
| 5831 | if( ssl == NULL || ssl->session == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5832 | return( NULL ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5833 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 5834 | return( ssl->session->peer_cert ); |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5835 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5836 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | b0550d9 | 2012-10-30 07:51:03 +0000 | [diff] [blame] | 5837 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5838 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5839 | int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *dst ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5840 | { |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5841 | if( ssl == NULL || |
| 5842 | dst == NULL || |
| 5843 | ssl->session == NULL || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5844 | ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5845 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5846 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5847 | } |
| 5848 | |
Manuel Pégourié-Gonnard | 06650f6 | 2013-08-02 15:34:52 +0200 | [diff] [blame] | 5849 | return( ssl_session_copy( dst, ssl->session ) ); |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5850 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5851 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 7471803 | 2013-07-30 12:41:56 +0200 | [diff] [blame] | 5852 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5853 | /* |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5854 | * Perform a single step of the SSL handshake |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5855 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5856 | int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5857 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5858 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5860 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5861 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5862 | ret = mbedtls_ssl_handshake_client_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5863 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5864 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5865 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5866 | ret = mbedtls_ssl_handshake_server_step( ssl ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5867 | #endif |
| 5868 | |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5869 | return( ret ); |
| 5870 | } |
| 5871 | |
| 5872 | /* |
| 5873 | * Perform the SSL handshake |
| 5874 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5875 | int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5876 | { |
| 5877 | int ret = 0; |
| 5878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5879 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5880 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5881 | while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5882 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5883 | ret = mbedtls_ssl_handshake_step( ssl ); |
Paul Bakker | 1961b70 | 2013-01-25 14:49:24 +0100 | [diff] [blame] | 5884 | |
| 5885 | if( ret != 0 ) |
| 5886 | break; |
| 5887 | } |
| 5888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5889 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5890 | |
| 5891 | return( ret ); |
| 5892 | } |
| 5893 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5894 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5895 | #if defined(MBEDTLS_SSL_SRV_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5896 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5897 | * Write HelloRequest to request renegotiation on server |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5898 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5899 | static int ssl_write_hello_request( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5900 | { |
| 5901 | int ret; |
| 5902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5903 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5904 | |
| 5905 | ssl->out_msglen = 4; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5906 | ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 5907 | ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST; |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5908 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5909 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5910 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5911 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5912 | return( ret ); |
| 5913 | } |
| 5914 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5915 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5916 | |
| 5917 | return( 0 ); |
| 5918 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5919 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5920 | |
| 5921 | /* |
| 5922 | * Actually renegotiate current connection, triggered by either: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5923 | * - any side: calling mbedtls_ssl_renegotiate(), |
| 5924 | * - client: receiving a HelloRequest during mbedtls_ssl_read(), |
| 5925 | * - server: receiving any handshake message on server during mbedtls_ssl_read() after |
Manuel Pégourié-Gonnard | 55e4ff2 | 2014-08-19 11:16:35 +0200 | [diff] [blame] | 5926 | * the initial handshake is completed. |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5927 | * If the handshake doesn't complete due to waiting for I/O, it will continue |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5928 | * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively. |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5929 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5930 | static int ssl_start_renegotiation( mbedtls_ssl_context *ssl ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5931 | { |
| 5932 | int ret; |
| 5933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5934 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5935 | |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5936 | if( ( ret = ssl_handshake_init( ssl ) ) != 0 ) |
| 5937 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5938 | |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5939 | /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and |
| 5940 | * the ServerHello will have message_seq = 1" */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5941 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5942 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5943 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5944 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5945 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 5946 | ssl->handshake->out_msg_seq = 1; |
| 5947 | else |
| 5948 | ssl->handshake->in_msg_seq = 1; |
Manuel Pégourié-Gonnard | 0557bd5 | 2014-08-19 19:18:39 +0200 | [diff] [blame] | 5949 | } |
| 5950 | #endif |
| 5951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5952 | ssl->state = MBEDTLS_SSL_HELLO_REQUEST; |
| 5953 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5954 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5955 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5956 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5957 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5958 | return( ret ); |
| 5959 | } |
| 5960 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5961 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5962 | |
| 5963 | return( 0 ); |
| 5964 | } |
| 5965 | |
| 5966 | /* |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5967 | * Renegotiate current connection on client, |
| 5968 | * or request renegotiation on server |
| 5969 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5970 | int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5971 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5972 | int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5973 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5974 | #if defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5975 | /* On server, just send the request */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5976 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5977 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5978 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5979 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5980 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5981 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5982 | |
| 5983 | /* Did we already try/start sending HelloRequest? */ |
| 5984 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5985 | return( mbedtls_ssl_flush_output( ssl ) ); |
Manuel Pégourié-Gonnard | f07f421 | 2014-08-15 19:04:47 +0200 | [diff] [blame] | 5986 | |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5987 | return( ssl_write_hello_request( ssl ) ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5988 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5989 | #endif /* MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5990 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5991 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5992 | /* |
| 5993 | * On client, either start the renegotiation process or, |
| 5994 | * if already in progress, continue the handshake |
| 5995 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5996 | if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 5997 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5998 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 5999 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 6000 | |
| 6001 | if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 ) |
| 6002 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6003 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 6004 | return( ret ); |
| 6005 | } |
| 6006 | } |
| 6007 | else |
| 6008 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6009 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 6010 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6011 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 6012 | return( ret ); |
| 6013 | } |
| 6014 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6015 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 9c1e189 | 2013-10-30 16:41:21 +0100 | [diff] [blame] | 6016 | |
Paul Bakker | 37ce0ff | 2013-10-31 14:32:04 +0100 | [diff] [blame] | 6017 | return( ret ); |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6021 | * Check record counters and renegotiate if they're above the limit. |
| 6022 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6023 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6024 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6025 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 6026 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6027 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6028 | { |
| 6029 | return( 0 ); |
| 6030 | } |
| 6031 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6032 | if( memcmp( ssl->in_ctr, ssl->conf->renego_period, 8 ) <= 0 && |
| 6033 | memcmp( ssl->out_ctr, ssl->conf->renego_period, 8 ) <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6034 | { |
| 6035 | return( 0 ); |
| 6036 | } |
| 6037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6038 | MBEDTLS_SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) ); |
| 6039 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6040 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6041 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6042 | |
| 6043 | /* |
| 6044 | * Receive application data decrypted from the SSL layer |
| 6045 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6046 | int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6047 | { |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6048 | int ret, record_read = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6049 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6051 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6052 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6053 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6054 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6055 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6056 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6057 | return( ret ); |
| 6058 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6059 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6060 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6061 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6062 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6063 | return( ret ); |
| 6064 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 6065 | } |
| 6066 | #endif |
| 6067 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6068 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6069 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 6070 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6071 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 6072 | return( ret ); |
| 6073 | } |
| 6074 | #endif |
| 6075 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6076 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6077 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6078 | ret = mbedtls_ssl_handshake( ssl ); |
| 6079 | if( ret == MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6080 | { |
| 6081 | record_read = 1; |
| 6082 | } |
| 6083 | else if( ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6084 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6085 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6086 | return( ret ); |
| 6087 | } |
| 6088 | } |
| 6089 | |
| 6090 | if( ssl->in_offt == NULL ) |
| 6091 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 6092 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 6093 | if( ssl->f_get_timer != NULL && |
| 6094 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 6095 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6096 | ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 6097 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 6098 | |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6099 | if( ! record_read ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6100 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6101 | if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6103 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6104 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 6105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6106 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6107 | return( ret ); |
| 6108 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6112 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6113 | { |
| 6114 | /* |
| 6115 | * OpenSSL sends empty messages to randomize the IV |
| 6116 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6117 | if( ( ret = mbedtls_ssl_read_record( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6118 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6119 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 6120 | return( 0 ); |
| 6121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6122 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6123 | return( ret ); |
| 6124 | } |
| 6125 | } |
| 6126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6127 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6128 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6129 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6130 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6132 | #if defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6133 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6134 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
| 6135 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6136 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6137 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 6138 | |
| 6139 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6140 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6141 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 6142 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 6143 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6144 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 6145 | } |
| 6146 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6147 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6148 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 6149 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6150 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 6151 | |
| 6152 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6153 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6154 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 6155 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 6156 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6157 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6158 | } |
Manuel Pégourié-Gonnard | d16d1cb | 2014-11-20 18:15:05 +0100 | [diff] [blame] | 6159 | #endif |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6160 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6161 | if( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6162 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6163 | ssl->conf->allow_legacy_renegotiation == |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6164 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6166 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6168 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 6169 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6170 | { |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6171 | /* |
| 6172 | * SSLv3 does not have a "no_renegotiation" alert |
| 6173 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6174 | if( ( ret = mbedtls_ssl_send_fatal_handshake_failure( ssl ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6175 | return( ret ); |
| 6176 | } |
| 6177 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6178 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 6179 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 6180 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 6181 | if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6183 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6184 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 6185 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 6186 | { |
| 6187 | return( ret ); |
| 6188 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6189 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 6190 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6191 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 6192 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 6193 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6194 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 6195 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 6196 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6197 | } |
| 6198 | else |
| 6199 | { |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 6200 | /* DTLS clients need to know renego is server-initiated */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6201 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6202 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 6203 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 6204 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6205 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 6206 | } |
| 6207 | #endif |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6208 | ret = ssl_start_renegotiation( ssl ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6209 | if( ret == MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO ) |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6210 | { |
| 6211 | record_read = 1; |
| 6212 | } |
| 6213 | else if( ret != 0 ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6214 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6215 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6216 | return( ret ); |
| 6217 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6218 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 6219 | |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6220 | /* If a non-handshake record was read during renego, fallthrough, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6221 | * else tell the user they should call mbedtls_ssl_read() again */ |
Manuel Pégourié-Gonnard | 6591962 | 2014-08-19 12:50:30 +0200 | [diff] [blame] | 6222 | if( ! record_read ) |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 6223 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6224 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6225 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 6226 | { |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 6227 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6228 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 6229 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6230 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 6231 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6232 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 6233 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6234 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 6235 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 6236 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 6237 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6238 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 6239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6240 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 6241 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 6242 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6243 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 6244 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 6245 | } |
| 6246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6247 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6248 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6249 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 6250 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 6254 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 6255 | /* We're going to return something now, cancel timer, |
| 6256 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6257 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 6258 | ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6259 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 6260 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6261 | /* If we requested renego but received AppData, resend HelloRequest. |
| 6262 | * Do it now, after setting in_offt, to avoid taking this branch |
| 6263 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6264 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6265 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6266 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6267 | { |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 6268 | if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6269 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6270 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 6271 | return( ret ); |
| 6272 | } |
| 6273 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6274 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f1e9b09 | 2014-10-02 18:08:53 +0200 | [diff] [blame] | 6275 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | n = ( len < ssl->in_msglen ) |
| 6279 | ? len : ssl->in_msglen; |
| 6280 | |
| 6281 | memcpy( buf, ssl->in_offt, n ); |
| 6282 | ssl->in_msglen -= n; |
| 6283 | |
| 6284 | if( ssl->in_msglen == 0 ) |
| 6285 | /* all bytes consumed */ |
| 6286 | ssl->in_offt = NULL; |
| 6287 | else |
| 6288 | /* more data available */ |
| 6289 | ssl->in_offt += n; |
| 6290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6291 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6292 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6293 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6297 | * Send application data to be encrypted by the SSL layer, |
| 6298 | * taking care of max fragment length and buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6299 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6300 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6301 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6302 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 6303 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6304 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6305 | unsigned int max_len; |
| 6306 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6307 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6308 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 6309 | /* |
| 6310 | * Assume mfl_code is correct since it was checked when set |
| 6311 | */ |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6312 | max_len = mfl_code_to_length[ssl->conf->mfl_code]; |
Manuel Pégourié-Gonnard | 581e6b6 | 2013-07-18 12:32:27 +0200 | [diff] [blame] | 6313 | |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 6314 | /* |
Paul Bakker | 05decb2 | 2013-08-15 13:33:48 +0200 | [diff] [blame] | 6315 | * Check if a smaller max length was negotiated |
Manuel Pégourié-Gonnard | ed4af8b | 2013-07-18 14:07:09 +0200 | [diff] [blame] | 6316 | */ |
| 6317 | if( ssl->session_out != NULL && |
| 6318 | mfl_code_to_length[ssl->session_out->mfl_code] < max_len ) |
| 6319 | { |
| 6320 | max_len = mfl_code_to_length[ssl->session_out->mfl_code]; |
| 6321 | } |
| 6322 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6323 | if( len > max_len ) |
| 6324 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6325 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6326 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6327 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6328 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6329 | "maximum fragment length: %d > %d", |
| 6330 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6331 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6332 | } |
| 6333 | else |
| 6334 | #endif |
| 6335 | len = max_len; |
| 6336 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6337 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6338 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6339 | if( ssl->out_left != 0 ) |
| 6340 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6341 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6342 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6343 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6344 | return( ret ); |
| 6345 | } |
| 6346 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6347 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 6348 | { |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6349 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6350 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6351 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6352 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6353 | if( ( ret = mbedtls_ssl_write_record( ssl ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6354 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6355 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 6356 | return( ret ); |
| 6357 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6358 | } |
| 6359 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 6360 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
| 6363 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6364 | * Write application data, doing 1/n-1 splitting if necessary. |
| 6365 | * |
| 6366 | * With non-blocking I/O, ssl_write_real() may return WANT_WRITE, |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 6367 | * then the caller will call us again with the same arguments, so |
| 6368 | * remember wether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6369 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6370 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6371 | static int ssl_write_split( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6372 | const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6373 | { |
| 6374 | int ret; |
| 6375 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 6376 | if( ssl->conf->cbc_record_splitting == |
| 6377 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 6378 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6379 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
| 6380 | mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc ) |
| 6381 | != MBEDTLS_MODE_CBC ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6382 | { |
| 6383 | return( ssl_write_real( ssl, buf, len ) ); |
| 6384 | } |
| 6385 | |
| 6386 | if( ssl->split_done == 0 ) |
| 6387 | { |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 6388 | if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 ) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6389 | return( ret ); |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 6390 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6391 | } |
| 6392 | |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 6393 | if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 ) |
| 6394 | return( ret ); |
| 6395 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6396 | |
| 6397 | return( ret + 1 ); |
| 6398 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6399 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 6400 | |
| 6401 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6402 | * Write application data (public-facing wrapper) |
| 6403 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6404 | int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6405 | { |
| 6406 | int ret; |
| 6407 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6408 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6409 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6410 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6411 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 6412 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6413 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6414 | return( ret ); |
| 6415 | } |
| 6416 | #endif |
| 6417 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6418 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6419 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6420 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6421 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 6422 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6423 | return( ret ); |
| 6424 | } |
| 6425 | } |
| 6426 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6427 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6428 | ret = ssl_write_split( ssl, buf, len ); |
| 6429 | #else |
| 6430 | ret = ssl_write_real( ssl, buf, len ); |
| 6431 | #endif |
| 6432 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 6433 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 6434 | |
| 6435 | return( ret ); |
| 6436 | } |
| 6437 | |
| 6438 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6439 | * Notify the peer that the connection is being closed |
| 6440 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6441 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6442 | { |
| 6443 | int ret; |
| 6444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6445 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6446 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 6447 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6448 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6450 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6451 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6452 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 6453 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 6454 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6455 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6456 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6457 | return( ret ); |
| 6458 | } |
| 6459 | } |
| 6460 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6461 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6462 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 6463 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6464 | } |
| 6465 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6466 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6467 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6468 | if( transform == NULL ) |
| 6469 | return; |
| 6470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6471 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6472 | deflateEnd( &transform->ctx_deflate ); |
| 6473 | inflateEnd( &transform->ctx_inflate ); |
| 6474 | #endif |
| 6475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6476 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 6477 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 6478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6479 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 6480 | mbedtls_md_free( &transform->md_ctx_dec ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6482 | mbedtls_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6483 | } |
| 6484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6485 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6486 | static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert ) |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 6487 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6488 | mbedtls_ssl_key_cert *cur = key_cert, *next; |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 6489 | |
| 6490 | while( cur != NULL ) |
| 6491 | { |
| 6492 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6493 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 6494 | cur = next; |
| 6495 | } |
| 6496 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6497 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 6498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6499 | void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6500 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6501 | if( handshake == NULL ) |
| 6502 | return; |
| 6503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6504 | #if defined(MBEDTLS_DHM_C) |
| 6505 | mbedtls_dhm_free( &handshake->dhm_ctx ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6506 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6507 | #if defined(MBEDTLS_ECDH_C) |
| 6508 | mbedtls_ecdh_free( &handshake->ecdh_ctx ); |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 6509 | #endif |
| 6510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6511 | #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 6512 | /* explicit void pointer cast for buggy MS compiler */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6513 | mbedtls_free( (void *) handshake->curves ); |
Manuel Pégourié-Gonnard | d09453c | 2013-09-23 19:11:32 +0200 | [diff] [blame] | 6514 | #endif |
| 6515 | |
Manuel Pégourié-Gonnard | 4b68296 | 2015-05-07 15:59:54 +0100 | [diff] [blame] | 6516 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 6517 | if( handshake->psk != NULL ) |
| 6518 | { |
| 6519 | mbedtls_zeroize( handshake->psk, handshake->psk_len ); |
| 6520 | mbedtls_free( handshake->psk ); |
| 6521 | } |
| 6522 | #endif |
| 6523 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6524 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 6525 | defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6526 | /* |
| 6527 | * Free only the linked list wrapper, not the keys themselves |
| 6528 | * since the belong to the SNI callback |
| 6529 | */ |
| 6530 | if( handshake->sni_key_cert != NULL ) |
| 6531 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6532 | mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next; |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6533 | |
| 6534 | while( cur != NULL ) |
| 6535 | { |
| 6536 | next = cur->next; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6537 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | 8372454 | 2013-09-24 22:30:56 +0200 | [diff] [blame] | 6538 | cur = next; |
| 6539 | } |
| 6540 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6541 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */ |
Manuel Pégourié-Gonnard | 705fcca | 2013-09-23 20:04:20 +0200 | [diff] [blame] | 6542 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6543 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6544 | mbedtls_free( handshake->verify_cookie ); |
| 6545 | mbedtls_free( handshake->hs_msg ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 6546 | ssl_flight_free( handshake->flight ); |
Manuel Pégourié-Gonnard | 7484881 | 2014-07-11 02:43:49 +0200 | [diff] [blame] | 6547 | #endif |
| 6548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6549 | mbedtls_zeroize( handshake, sizeof( mbedtls_ssl_handshake_params ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6550 | } |
| 6551 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6552 | void mbedtls_ssl_session_free( mbedtls_ssl_session *session ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6553 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6554 | if( session == NULL ) |
| 6555 | return; |
| 6556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6557 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6558 | if( session->peer_cert != NULL ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6560 | mbedtls_x509_crt_free( session->peer_cert ); |
| 6561 | mbedtls_free( session->peer_cert ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6562 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 6563 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6564 | |
Manuel Pégourié-Gonnard | b596abf | 2015-05-20 10:45:29 +0200 | [diff] [blame] | 6565 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6566 | mbedtls_free( session->ticket ); |
Paul Bakker | a503a63 | 2013-08-14 13:48:06 +0200 | [diff] [blame] | 6567 | #endif |
Manuel Pégourié-Gonnard | 75d4401 | 2013-08-02 14:44:04 +0200 | [diff] [blame] | 6568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6569 | mbedtls_zeroize( session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6570 | } |
| 6571 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6572 | /* |
| 6573 | * Free an SSL context |
| 6574 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6575 | void mbedtls_ssl_free( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6576 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 6577 | if( ssl == NULL ) |
| 6578 | return; |
| 6579 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6581 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6582 | if( ssl->out_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6583 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6584 | mbedtls_zeroize( ssl->out_buf, MBEDTLS_SSL_BUFFER_LEN ); |
| 6585 | mbedtls_free( ssl->out_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
Manuel Pégourié-Gonnard | 7ee6f0e | 2014-02-13 10:54:07 +0100 | [diff] [blame] | 6588 | if( ssl->in_buf != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6589 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6590 | mbedtls_zeroize( ssl->in_buf, MBEDTLS_SSL_BUFFER_LEN ); |
| 6591 | mbedtls_free( ssl->in_buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6594 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6595 | if( ssl->compress_buf != NULL ) |
| 6596 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6597 | mbedtls_zeroize( ssl->compress_buf, MBEDTLS_SSL_BUFFER_LEN ); |
| 6598 | mbedtls_free( ssl->compress_buf ); |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 6599 | } |
| 6600 | #endif |
| 6601 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6602 | if( ssl->transform ) |
| 6603 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6604 | mbedtls_ssl_transform_free( ssl->transform ); |
| 6605 | mbedtls_free( ssl->transform ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | if( ssl->handshake ) |
| 6609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6610 | mbedtls_ssl_handshake_free( ssl->handshake ); |
| 6611 | mbedtls_ssl_transform_free( ssl->transform_negotiate ); |
| 6612 | mbedtls_ssl_session_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6614 | mbedtls_free( ssl->handshake ); |
| 6615 | mbedtls_free( ssl->transform_negotiate ); |
| 6616 | mbedtls_free( ssl->session_negotiate ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 6617 | } |
| 6618 | |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6619 | if( ssl->session ) |
| 6620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6621 | mbedtls_ssl_session_free( ssl->session ); |
| 6622 | mbedtls_free( ssl->session ); |
Paul Bakker | c046350 | 2013-02-14 11:19:38 +0100 | [diff] [blame] | 6623 | } |
| 6624 | |
Manuel Pégourié-Gonnard | 55fab2d | 2015-05-11 16:15:19 +0200 | [diff] [blame] | 6625 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 6626 | if( ssl->hostname != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6627 | { |
Manuel Pégourié-Gonnard | ba26c24 | 2015-05-06 10:47:06 +0100 | [diff] [blame] | 6628 | mbedtls_zeroize( ssl->hostname, strlen( ssl->hostname ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6629 | mbedtls_free( ssl->hostname ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6630 | } |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 6631 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6633 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
| 6634 | if( mbedtls_ssl_hw_record_finish != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6635 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6636 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) ); |
| 6637 | mbedtls_ssl_hw_record_finish( ssl ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 6638 | } |
| 6639 | #endif |
| 6640 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6641 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6642 | mbedtls_free( ssl->cli_id ); |
Manuel Pégourié-Gonnard | 43c0218 | 2014-07-22 17:32:01 +0200 | [diff] [blame] | 6643 | #endif |
| 6644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6645 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) ); |
Paul Bakker | 2da561c | 2009-02-05 18:00:28 +0000 | [diff] [blame] | 6646 | |
Paul Bakker | 86f04f4 | 2013-02-14 11:20:09 +0100 | [diff] [blame] | 6647 | /* Actually clear after last debug message */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6648 | mbedtls_zeroize( ssl, sizeof( mbedtls_ssl_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6649 | } |
| 6650 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6651 | /* |
| 6652 | * Initialze mbedtls_ssl_config |
| 6653 | */ |
| 6654 | void mbedtls_ssl_config_init( mbedtls_ssl_config *conf ) |
| 6655 | { |
| 6656 | memset( conf, 0, sizeof( mbedtls_ssl_config ) ); |
| 6657 | } |
| 6658 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6659 | static int ssl_preset_suiteb_ciphersuites[] = { |
| 6660 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, |
| 6661 | MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, |
| 6662 | 0 |
| 6663 | }; |
| 6664 | |
| 6665 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED) |
| 6666 | static int ssl_preset_suiteb_hashes[] = { |
| 6667 | MBEDTLS_MD_SHA256, |
| 6668 | MBEDTLS_MD_SHA384, |
| 6669 | MBEDTLS_MD_NONE |
| 6670 | }; |
| 6671 | #endif |
| 6672 | |
| 6673 | #if defined(MBEDTLS_ECP_C) |
| 6674 | static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = { |
| 6675 | MBEDTLS_ECP_DP_SECP256R1, |
| 6676 | MBEDTLS_ECP_DP_SECP384R1, |
| 6677 | MBEDTLS_ECP_DP_NONE |
| 6678 | }; |
| 6679 | #endif |
| 6680 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6681 | /* |
| 6682 | * Load default in mbetls_ssl_config |
| 6683 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6684 | int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6685 | int endpoint, int transport, int preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6686 | { |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6687 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6688 | int ret; |
Manuel Pégourié-Gonnard | 8b431fb | 2015-05-11 12:54:52 +0200 | [diff] [blame] | 6689 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6690 | |
Manuel Pégourié-Gonnard | 0de074f | 2015-05-14 12:58:01 +0200 | [diff] [blame] | 6691 | /* Use the functions here so that they are covered in tests, |
| 6692 | * but otherwise access member directly for efficiency */ |
| 6693 | mbedtls_ssl_conf_endpoint( conf, endpoint ); |
| 6694 | mbedtls_ssl_conf_transport( conf, transport ); |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6695 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6696 | /* |
| 6697 | * Things that are common to all presets |
| 6698 | */ |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 6699 | #if defined(MBEDTLS_SSL_CLI_C) |
| 6700 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 6701 | { |
| 6702 | conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 6703 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 6704 | conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED; |
| 6705 | #endif |
| 6706 | } |
| 6707 | #endif |
| 6708 | |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6709 | #if defined(MBEDTLS_ARC4_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6710 | conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED; |
Manuel Pégourié-Gonnard | 66dc555 | 2015-05-14 12:28:21 +0200 | [diff] [blame] | 6711 | #endif |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6712 | |
| 6713 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 6714 | conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED; |
| 6715 | #endif |
| 6716 | |
| 6717 | #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) |
| 6718 | conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED; |
| 6719 | #endif |
| 6720 | |
Manuel Pégourié-Gonnard | 17eab2b | 2015-05-05 16:34:53 +0100 | [diff] [blame] | 6721 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
| 6722 | conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED; |
| 6723 | #endif |
| 6724 | |
Manuel Pégourié-Gonnard | e057d3b | 2015-05-20 10:59:43 +0200 | [diff] [blame] | 6725 | #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6726 | conf->f_cookie_write = ssl_cookie_write_dummy; |
| 6727 | conf->f_cookie_check = ssl_cookie_check_dummy; |
| 6728 | #endif |
| 6729 | |
| 6730 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 6731 | conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED; |
| 6732 | #endif |
| 6733 | |
| 6734 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6735 | conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN; |
| 6736 | conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX; |
| 6737 | #endif |
| 6738 | |
| 6739 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 6740 | conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT; |
| 6741 | memset( conf->renego_period, 0xFF, 7 ); |
| 6742 | conf->renego_period[7] = 0x00; |
| 6743 | #endif |
| 6744 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6745 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) |
| 6746 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 6747 | { |
| 6748 | if( ( ret = mbedtls_ssl_conf_dh_param( conf, |
| 6749 | MBEDTLS_DHM_RFC5114_MODP_2048_P, |
| 6750 | MBEDTLS_DHM_RFC5114_MODP_2048_G ) ) != 0 ) |
| 6751 | { |
| 6752 | return( ret ); |
| 6753 | } |
| 6754 | } |
Manuel Pégourié-Gonnard | bd990d6 | 2015-06-11 14:49:42 +0200 | [diff] [blame] | 6755 | #endif |
| 6756 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6757 | /* |
| 6758 | * Preset-specific defaults |
| 6759 | */ |
| 6760 | switch( preset ) |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6761 | { |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6762 | /* |
| 6763 | * NSA Suite B |
| 6764 | */ |
| 6765 | case MBEDTLS_SSL_PRESET_SUITEB: |
| 6766 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6767 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 6768 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6769 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6770 | |
| 6771 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 6772 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 6773 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 6774 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 6775 | ssl_preset_suiteb_ciphersuites; |
| 6776 | |
| 6777 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6778 | conf->cert_profile = &mbedtls_x509_crt_profile_suiteb; |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6779 | #endif |
| 6780 | |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 6781 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED) |
| 6782 | conf->sig_hashes = ssl_preset_suiteb_hashes; |
| 6783 | #endif |
| 6784 | |
| 6785 | #if defined(MBEDTLS_ECP_C) |
| 6786 | conf->curve_list = ssl_preset_suiteb_curves; |
| 6787 | #endif |
| 6788 | |
| 6789 | /* |
| 6790 | * Default |
| 6791 | */ |
| 6792 | default: |
| 6793 | conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 6794 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_1; /* TLS 1.0 */ |
| 6795 | conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION; |
| 6796 | conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION; |
| 6797 | |
| 6798 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 6799 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 6800 | conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 6801 | #endif |
| 6802 | |
| 6803 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = |
| 6804 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = |
| 6805 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = |
| 6806 | conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = |
| 6807 | mbedtls_ssl_list_ciphersuites(); |
| 6808 | |
| 6809 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6810 | conf->cert_profile = &mbedtls_x509_crt_profile_default; |
| 6811 | #endif |
| 6812 | |
| 6813 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED) |
| 6814 | conf->sig_hashes = mbedtls_md_list(); |
| 6815 | #endif |
| 6816 | |
| 6817 | #if defined(MBEDTLS_ECP_C) |
| 6818 | conf->curve_list = mbedtls_ecp_grp_id_list(); |
| 6819 | #endif |
| 6820 | |
| 6821 | #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) |
| 6822 | conf->dhm_min_bitlen = 1024; |
| 6823 | #endif |
| 6824 | } |
| 6825 | |
Manuel Pégourié-Gonnard | cd523e2 | 2015-05-04 13:35:39 +0200 | [diff] [blame] | 6826 | return( 0 ); |
| 6827 | } |
| 6828 | |
| 6829 | /* |
| 6830 | * Free mbedtls_ssl_config |
| 6831 | */ |
| 6832 | void mbedtls_ssl_config_free( mbedtls_ssl_config *conf ) |
| 6833 | { |
| 6834 | #if defined(MBEDTLS_DHM_C) |
| 6835 | mbedtls_mpi_free( &conf->dhm_P ); |
| 6836 | mbedtls_mpi_free( &conf->dhm_G ); |
| 6837 | #endif |
| 6838 | |
| 6839 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED) |
| 6840 | if( conf->psk != NULL ) |
| 6841 | { |
| 6842 | mbedtls_zeroize( conf->psk, conf->psk_len ); |
| 6843 | mbedtls_zeroize( conf->psk_identity, conf->psk_identity_len ); |
| 6844 | mbedtls_free( conf->psk ); |
| 6845 | mbedtls_free( conf->psk_identity ); |
| 6846 | conf->psk_len = 0; |
| 6847 | conf->psk_identity_len = 0; |
| 6848 | } |
| 6849 | #endif |
| 6850 | |
| 6851 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6852 | ssl_key_cert_free( conf->key_cert ); |
| 6853 | #endif |
| 6854 | |
| 6855 | mbedtls_zeroize( conf, sizeof( mbedtls_ssl_config ) ); |
| 6856 | } |
| 6857 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6858 | #if defined(MBEDTLS_PK_C) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6859 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6860 | * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6861 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6862 | unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk ) |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6863 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6864 | #if defined(MBEDTLS_RSA_C) |
| 6865 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) ) |
| 6866 | return( MBEDTLS_SSL_SIG_RSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6867 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6868 | #if defined(MBEDTLS_ECDSA_C) |
| 6869 | if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) ) |
| 6870 | return( MBEDTLS_SSL_SIG_ECDSA ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6871 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6872 | return( MBEDTLS_SSL_SIG_ANON ); |
Manuel Pégourié-Gonnard | 0d42049 | 2013-08-21 16:14:26 +0200 | [diff] [blame] | 6873 | } |
| 6874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6875 | mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig ) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6876 | { |
| 6877 | switch( sig ) |
| 6878 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6879 | #if defined(MBEDTLS_RSA_C) |
| 6880 | case MBEDTLS_SSL_SIG_RSA: |
| 6881 | return( MBEDTLS_PK_RSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6882 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6883 | #if defined(MBEDTLS_ECDSA_C) |
| 6884 | case MBEDTLS_SSL_SIG_ECDSA: |
| 6885 | return( MBEDTLS_PK_ECDSA ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6886 | #endif |
| 6887 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6888 | return( MBEDTLS_PK_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6889 | } |
| 6890 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6891 | #endif /* MBEDTLS_PK_C */ |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6892 | |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6893 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6894 | * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX |
Manuel Pégourié-Gonnard | 1a48383 | 2013-09-20 12:29:15 +0200 | [diff] [blame] | 6895 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6896 | mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash ) |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6897 | { |
| 6898 | switch( hash ) |
| 6899 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6900 | #if defined(MBEDTLS_MD5_C) |
| 6901 | case MBEDTLS_SSL_HASH_MD5: |
| 6902 | return( MBEDTLS_MD_MD5 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6903 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6904 | #if defined(MBEDTLS_SHA1_C) |
| 6905 | case MBEDTLS_SSL_HASH_SHA1: |
| 6906 | return( MBEDTLS_MD_SHA1 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6907 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6908 | #if defined(MBEDTLS_SHA256_C) |
| 6909 | case MBEDTLS_SSL_HASH_SHA224: |
| 6910 | return( MBEDTLS_MD_SHA224 ); |
| 6911 | case MBEDTLS_SSL_HASH_SHA256: |
| 6912 | return( MBEDTLS_MD_SHA256 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6913 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6914 | #if defined(MBEDTLS_SHA512_C) |
| 6915 | case MBEDTLS_SSL_HASH_SHA384: |
| 6916 | return( MBEDTLS_MD_SHA384 ); |
| 6917 | case MBEDTLS_SSL_HASH_SHA512: |
| 6918 | return( MBEDTLS_MD_SHA512 ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6919 | #endif |
| 6920 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6921 | return( MBEDTLS_MD_NONE ); |
Manuel Pégourié-Gonnard | a20c58c | 2013-08-22 13:52:48 +0200 | [diff] [blame] | 6922 | } |
| 6923 | } |
| 6924 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6925 | /* |
| 6926 | * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX |
| 6927 | */ |
| 6928 | unsigned char mbedtls_ssl_hash_from_md_alg( int md ) |
| 6929 | { |
| 6930 | switch( md ) |
| 6931 | { |
| 6932 | #if defined(MBEDTLS_MD5_C) |
| 6933 | case MBEDTLS_MD_MD5: |
| 6934 | return( MBEDTLS_SSL_HASH_MD5 ); |
| 6935 | #endif |
| 6936 | #if defined(MBEDTLS_SHA1_C) |
| 6937 | case MBEDTLS_MD_SHA1: |
| 6938 | return( MBEDTLS_SSL_HASH_SHA1 ); |
| 6939 | #endif |
| 6940 | #if defined(MBEDTLS_SHA256_C) |
| 6941 | case MBEDTLS_MD_SHA224: |
| 6942 | return( MBEDTLS_SSL_HASH_SHA224 ); |
| 6943 | case MBEDTLS_MD_SHA256: |
| 6944 | return( MBEDTLS_SSL_HASH_SHA256 ); |
| 6945 | #endif |
| 6946 | #if defined(MBEDTLS_SHA512_C) |
| 6947 | case MBEDTLS_MD_SHA384: |
| 6948 | return( MBEDTLS_SSL_HASH_SHA384 ); |
| 6949 | case MBEDTLS_MD_SHA512: |
| 6950 | return( MBEDTLS_SSL_HASH_SHA512 ); |
| 6951 | #endif |
| 6952 | default: |
| 6953 | return( MBEDTLS_SSL_HASH_NONE ); |
| 6954 | } |
| 6955 | } |
| 6956 | |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 6957 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6958 | /* |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6959 | * Check if a curve proposed by the peer is in our list. |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6960 | * Return 0 if we're willing to use it, -1 otherwise. |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6961 | */ |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6962 | int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6963 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6964 | const mbedtls_ecp_group_id *gid; |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6965 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6966 | if( ssl->conf->curve_list == NULL ) |
| 6967 | return( -1 ); |
| 6968 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 6969 | for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ ) |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6970 | if( *gid == grp_id ) |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6971 | return( 0 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6972 | |
Manuel Pégourié-Gonnard | 9d412d8 | 2015-06-17 12:10:46 +0200 | [diff] [blame] | 6973 | return( -1 ); |
Manuel Pégourié-Gonnard | ab24010 | 2014-02-04 16:18:07 +0100 | [diff] [blame] | 6974 | } |
Manuel Pégourié-Gonnard | b541da6 | 2015-06-17 11:43:30 +0200 | [diff] [blame] | 6975 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 6976 | |
Manuel Pégourié-Gonnard | 7bfc122 | 2015-06-17 14:34:48 +0200 | [diff] [blame] | 6977 | #if defined(MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED) |
| 6978 | /* |
| 6979 | * Check if a hash proposed by the peer is in our list. |
| 6980 | * Return 0 if we're willing to use it, -1 otherwise. |
| 6981 | */ |
| 6982 | int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl, |
| 6983 | mbedtls_md_type_t md ) |
| 6984 | { |
| 6985 | const int *cur; |
| 6986 | |
| 6987 | if( ssl->conf->sig_hashes == NULL ) |
| 6988 | return( -1 ); |
| 6989 | |
| 6990 | for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ ) |
| 6991 | if( *cur == (int) md ) |
| 6992 | return( 0 ); |
| 6993 | |
| 6994 | return( -1 ); |
| 6995 | } |
| 6996 | #endif /* MBEDTLS_KEY_EXCHANGE__SOME__SIGNATURE_ENABLED */ |
| 6997 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6998 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 6999 | int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert, |
| 7000 | const mbedtls_ssl_ciphersuite_t *ciphersuite, |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7001 | int cert_endpoint, |
Manuel Pégourié-Gonnard | e6ef16f | 2015-05-11 19:54:43 +0200 | [diff] [blame] | 7002 | uint32_t *flags ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7003 | { |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7004 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7005 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7006 | int usage = 0; |
| 7007 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7008 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7009 | const char *ext_oid; |
| 7010 | size_t ext_len; |
| 7011 | #endif |
| 7012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7013 | #if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \ |
| 7014 | !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7015 | ((void) cert); |
| 7016 | ((void) cert_endpoint); |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7017 | ((void) flags); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7018 | #endif |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7019 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7020 | #if defined(MBEDTLS_X509_CHECK_KEY_USAGE) |
| 7021 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7022 | { |
| 7023 | /* Server part of the key exchange */ |
| 7024 | switch( ciphersuite->key_exchange ) |
| 7025 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7026 | case MBEDTLS_KEY_EXCHANGE_RSA: |
| 7027 | case MBEDTLS_KEY_EXCHANGE_RSA_PSK: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7028 | usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7029 | break; |
| 7030 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7031 | case MBEDTLS_KEY_EXCHANGE_DHE_RSA: |
| 7032 | case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA: |
| 7033 | case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA: |
| 7034 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7035 | break; |
| 7036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7037 | case MBEDTLS_KEY_EXCHANGE_ECDH_RSA: |
| 7038 | case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA: |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7039 | usage = MBEDTLS_X509_KU_KEY_AGREEMENT; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7040 | break; |
| 7041 | |
| 7042 | /* Don't use default: we want warnings when adding new values */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7043 | case MBEDTLS_KEY_EXCHANGE_NONE: |
| 7044 | case MBEDTLS_KEY_EXCHANGE_PSK: |
| 7045 | case MBEDTLS_KEY_EXCHANGE_DHE_PSK: |
| 7046 | case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK: |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7047 | usage = 0; |
| 7048 | } |
| 7049 | } |
| 7050 | else |
| 7051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7052 | /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */ |
| 7053 | usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE; |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7054 | } |
| 7055 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7056 | if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7057 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7058 | *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7059 | ret = -1; |
| 7060 | } |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7061 | #else |
| 7062 | ((void) ciphersuite); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7063 | #endif /* MBEDTLS_X509_CHECK_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7065 | #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE) |
| 7066 | if( cert_endpoint == MBEDTLS_SSL_IS_SERVER ) |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7067 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7068 | ext_oid = MBEDTLS_OID_SERVER_AUTH; |
| 7069 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7070 | } |
| 7071 | else |
| 7072 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7073 | ext_oid = MBEDTLS_OID_CLIENT_AUTH; |
| 7074 | ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH ); |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7075 | } |
| 7076 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7077 | if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 ) |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7078 | { |
Manuel Pégourié-Gonnard | e6028c9 | 2015-04-20 12:19:02 +0100 | [diff] [blame] | 7079 | *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE; |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7080 | ret = -1; |
| 7081 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7082 | #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */ |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 7083 | |
Manuel Pégourié-Gonnard | e6efa6f | 2015-04-20 11:01:48 +0100 | [diff] [blame] | 7084 | return( ret ); |
Manuel Pégourié-Gonnard | 7f2a07d | 2014-04-09 09:50:57 +0200 | [diff] [blame] | 7085 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7086 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | 3a306b9 | 2014-04-29 15:11:17 +0200 | [diff] [blame] | 7087 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7088 | /* |
| 7089 | * Convert version numbers to/from wire format |
| 7090 | * and, for DTLS, to/from TLS equivalent. |
| 7091 | * |
| 7092 | * For TLS this is the identity. |
| 7093 | * For DTLS, use one complement (v -> 255 - v, and then map as follows: |
| 7094 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 7095 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 7096 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7097 | void mbedtls_ssl_write_version( int major, int minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7098 | unsigned char ver[2] ) |
| 7099 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7100 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7101 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7103 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7104 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 7105 | |
| 7106 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 7107 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 7108 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 7109 | else |
| 7110 | #else |
| 7111 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7112 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 7113 | { |
| 7114 | ver[0] = (unsigned char) major; |
| 7115 | ver[1] = (unsigned char) minor; |
| 7116 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7117 | } |
| 7118 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7119 | void mbedtls_ssl_read_version( int *major, int *minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7120 | const unsigned char ver[2] ) |
| 7121 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7122 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 7123 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7124 | { |
| 7125 | *major = 255 - ver[0] + 2; |
| 7126 | *minor = 255 - ver[1] + 1; |
| 7127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7128 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7129 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 7130 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 7131 | else |
| 7132 | #else |
| 7133 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7134 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 7135 | { |
| 7136 | *major = ver[0]; |
| 7137 | *minor = ver[1]; |
| 7138 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 7139 | } |
| 7140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 7141 | #endif /* MBEDTLS_SSL_TLS_C */ |