blob: 1f062278149103e818626eb820d01966aa137be7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020042#if defined(POLARSSL_X509_CRT_PARSE_C) && \
43 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
44#include "polarssl/oid.h"
45#endif
46
Paul Bakker7dc4c442014-02-01 22:50:26 +010047#if defined(POLARSSL_PLATFORM_C)
48#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#else
50#define polarssl_malloc malloc
51#define polarssl_free free
52#endif
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Paul Bakker6edcd412013-10-29 15:22:54 +010056#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
57 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000058#define strcasecmp _stricmp
59#endif
60
Paul Bakker34617722014-06-13 17:20:13 +020061/* Implementation that should never be optimized out by the compiler */
62static void polarssl_zeroize( void *v, size_t n ) {
63 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
64}
65
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066/* Length of the "epoch" field in the record header */
67static inline size_t ssl_ep_len( const ssl_context *ssl )
68{
69#if defined(POLARSSL_SSL_PROTO_DTLS)
70 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
71 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010072#else
73 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074#endif
75 return( 0 );
76}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020079#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080/*
81 * Start a timer.
82 * Passing millisecs = 0 cancels a running timer.
83 * The timer is already running iff time_limit != 0.
84 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020085static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
87 ssl->time_limit = millisecs;
88 get_timer( &ssl->time_info, 1 );
89}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020094static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
96 if( ssl->time_limit != 0 &&
97 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
98 {
99 return( -1 );
100 }
101
102 return( 0 );
103}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200105/*
106 * Double the retransmit timeout value, within the allowed range,
107 * returning -1 if the maximum value has already been reached.
108 */
109static int ssl_double_retransmit_timeout( ssl_context *ssl )
110{
111 uint32_t new_timeout;
112
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200113 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200114 return( -1 );
115
116 new_timeout = 2 * ssl->handshake->retransmit_timeout;
117
118 /* Avoid arithmetic overflow and range overflow */
119 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200120 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200121 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200122 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200123 }
124
125 ssl->handshake->retransmit_timeout = new_timeout;
126 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
127 ssl->handshake->retransmit_timeout ) );
128
129 return( 0 );
130}
131
132static void ssl_reset_retransmit_timeout( ssl_context *ssl )
133{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200134 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200135 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
136 ssl->handshake->retransmit_timeout ) );
137}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200138#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200139
Paul Bakker05decb22013-08-15 13:33:48 +0200140#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200141/*
142 * Convert max_fragment_length codes to length.
143 * RFC 6066 says:
144 * enum{
145 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
146 * } MaxFragmentLength;
147 * and we add 0 -> extension unused
148 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200149static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200150{
151 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
152 512, /* SSL_MAX_FRAG_LEN_512 */
153 1024, /* SSL_MAX_FRAG_LEN_1024 */
154 2048, /* SSL_MAX_FRAG_LEN_2048 */
155 4096, /* SSL_MAX_FRAG_LEN_4096 */
156};
Paul Bakker05decb22013-08-15 13:33:48 +0200157#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200158
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200159static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
160{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200161 ssl_session_free( dst );
162 memcpy( dst, src, sizeof( ssl_session ) );
163
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200165 if( src->peer_cert != NULL )
166 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200167 int ret;
168
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200169 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
170 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200171 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
172
Paul Bakkerb6b09562013-09-18 14:17:41 +0200173 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200174
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200175 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
176 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200177 {
178 polarssl_free( dst->peer_cert );
179 dst->peer_cert = NULL;
180 return( ret );
181 }
182 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200184
Paul Bakkera503a632013-08-14 13:48:06 +0200185#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200186 if( src->ticket != NULL )
187 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200188 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
189 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
191
192 memcpy( dst->ticket, src->ticket, src->ticket_len );
193 }
Paul Bakkera503a632013-08-14 13:48:06 +0200194#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200195
196 return( 0 );
197}
198
Paul Bakker05ef8352012-05-08 09:17:57 +0000199#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200200int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200201 const unsigned char *key_enc, const unsigned char *key_dec,
202 size_t keylen,
203 const unsigned char *iv_enc, const unsigned char *iv_dec,
204 size_t ivlen,
205 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200206 size_t maclen ) = NULL;
207int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
208int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
209int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
210int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
211int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200212#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214/*
215 * Key material generation
216 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200217#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200218static int ssl3_prf( const unsigned char *secret, size_t slen,
219 const char *label,
220 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000221 unsigned char *dstbuf, size_t dlen )
222{
223 size_t i;
224 md5_context md5;
225 sha1_context sha1;
226 unsigned char padding[16];
227 unsigned char sha1sum[20];
228 ((void)label);
229
Paul Bakker5b4af392014-06-26 12:09:34 +0200230 md5_init( &md5 );
231 sha1_init( &sha1 );
232
Paul Bakker5f70b252012-09-13 14:23:06 +0000233 /*
234 * SSLv3:
235 * block =
236 * MD5( secret + SHA1( 'A' + secret + random ) ) +
237 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
238 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
239 * ...
240 */
241 for( i = 0; i < dlen / 16; i++ )
242 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200243 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000244
245 sha1_starts( &sha1 );
246 sha1_update( &sha1, padding, 1 + i );
247 sha1_update( &sha1, secret, slen );
248 sha1_update( &sha1, random, rlen );
249 sha1_finish( &sha1, sha1sum );
250
251 md5_starts( &md5 );
252 md5_update( &md5, secret, slen );
253 md5_update( &md5, sha1sum, 20 );
254 md5_finish( &md5, dstbuf + i * 16 );
255 }
256
Paul Bakker5b4af392014-06-26 12:09:34 +0200257 md5_free( &md5 );
258 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000259
Paul Bakker34617722014-06-13 17:20:13 +0200260 polarssl_zeroize( padding, sizeof( padding ) );
261 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000262
263 return( 0 );
264}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200265#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000266
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200267#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200268static int tls1_prf( const unsigned char *secret, size_t slen,
269 const char *label,
270 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000271 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000272{
Paul Bakker23986e52011-04-24 08:57:21 +0000273 size_t nb, hs;
274 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200275 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 unsigned char tmp[128];
277 unsigned char h_i[20];
278
279 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000280 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282 hs = ( slen + 1 ) / 2;
283 S1 = secret;
284 S2 = secret + slen - hs;
285
286 nb = strlen( label );
287 memcpy( tmp + 20, label, nb );
288 memcpy( tmp + 20 + nb, random, rlen );
289 nb += rlen;
290
291 /*
292 * First compute P_md5(secret,label+random)[0..dlen]
293 */
294 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
295
296 for( i = 0; i < dlen; i += 16 )
297 {
298 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
299 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
300
301 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
302
303 for( j = 0; j < k; j++ )
304 dstbuf[i + j] = h_i[j];
305 }
306
307 /*
308 * XOR out with P_sha1(secret,label+random)[0..dlen]
309 */
310 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
311
312 for( i = 0; i < dlen; i += 20 )
313 {
314 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
315 sha1_hmac( S2, hs, tmp, 20, tmp );
316
317 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
318
319 for( j = 0; j < k; j++ )
320 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
321 }
322
Paul Bakker34617722014-06-13 17:20:13 +0200323 polarssl_zeroize( tmp, sizeof( tmp ) );
324 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 return( 0 );
327}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200328#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200330#if defined(POLARSSL_SSL_PROTO_TLS1_2)
331#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200332static int tls_prf_sha256( const unsigned char *secret, size_t slen,
333 const char *label,
334 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000335 unsigned char *dstbuf, size_t dlen )
336{
337 size_t nb;
338 size_t i, j, k;
339 unsigned char tmp[128];
340 unsigned char h_i[32];
341
342 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
343 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
344
345 nb = strlen( label );
346 memcpy( tmp + 32, label, nb );
347 memcpy( tmp + 32 + nb, random, rlen );
348 nb += rlen;
349
350 /*
351 * Compute P_<hash>(secret, label + random)[0..dlen]
352 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200353 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000354
355 for( i = 0; i < dlen; i += 32 )
356 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200357 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
358 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000359
360 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
361
362 for( j = 0; j < k; j++ )
363 dstbuf[i + j] = h_i[j];
364 }
365
Paul Bakker34617722014-06-13 17:20:13 +0200366 polarssl_zeroize( tmp, sizeof( tmp ) );
367 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000368
369 return( 0 );
370}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200371#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000372
Paul Bakker9e36f042013-06-30 14:34:05 +0200373#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200374static int tls_prf_sha384( const unsigned char *secret, size_t slen,
375 const char *label,
376 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000377 unsigned char *dstbuf, size_t dlen )
378{
379 size_t nb;
380 size_t i, j, k;
381 unsigned char tmp[128];
382 unsigned char h_i[48];
383
384 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
385 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
386
387 nb = strlen( label );
388 memcpy( tmp + 48, label, nb );
389 memcpy( tmp + 48 + nb, random, rlen );
390 nb += rlen;
391
392 /*
393 * Compute P_<hash>(secret, label + random)[0..dlen]
394 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200395 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000396
397 for( i = 0; i < dlen; i += 48 )
398 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200399 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
400 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000401
402 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
403
404 for( j = 0; j < k; j++ )
405 dstbuf[i + j] = h_i[j];
406 }
407
Paul Bakker34617722014-06-13 17:20:13 +0200408 polarssl_zeroize( tmp, sizeof( tmp ) );
409 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000410
411 return( 0 );
412}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200413#endif /* POLARSSL_SHA512_C */
414#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000415
Paul Bakker66d5d072014-06-17 16:39:18 +0200416static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200417
418#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
419 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200420static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421#endif
Paul Bakker380da532012-04-18 16:10:25 +0000422
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200423#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200424static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
425static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#endif
427
428#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200429static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
430static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431#endif
432
433#if defined(POLARSSL_SSL_PROTO_TLS1_2)
434#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200435static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
436static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
437static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200438#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100439
Paul Bakker9e36f042013-06-30 14:34:05 +0200440#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200441static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
442static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
443static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100444#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200445#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000446
Paul Bakker5121ce52009-01-03 21:22:43 +0000447int ssl_derive_keys( ssl_context *ssl )
448{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200449 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000450 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char keyblk[256];
452 unsigned char *key1;
453 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100454 unsigned char *mac_enc;
455 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200456 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100457 const cipher_info_t *cipher_info;
458 const md_info_t *md_info;
459
Paul Bakker48916f92012-09-16 19:57:18 +0000460 ssl_session *session = ssl->session_negotiate;
461 ssl_transform *transform = ssl->transform_negotiate;
462 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000463
464 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
465
Paul Bakker68884e32013-01-07 18:20:04 +0100466 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
467 if( cipher_info == NULL )
468 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200469 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100470 transform->ciphersuite_info->cipher ) );
471 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
472 }
473
474 md_info = md_info_from_type( transform->ciphersuite_info->mac );
475 if( md_info == NULL )
476 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200477 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100478 transform->ciphersuite_info->mac ) );
479 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
480 }
481
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000483 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000484 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200485#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000486 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000487 {
Paul Bakker48916f92012-09-16 19:57:18 +0000488 handshake->tls_prf = ssl3_prf;
489 handshake->calc_verify = ssl_calc_verify_ssl;
490 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000491 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200492 else
493#endif
494#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
495 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000496 {
Paul Bakker48916f92012-09-16 19:57:18 +0000497 handshake->tls_prf = tls1_prf;
498 handshake->calc_verify = ssl_calc_verify_tls;
499 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000500 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200501 else
502#endif
503#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200504#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200505 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
506 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000507 {
Paul Bakker48916f92012-09-16 19:57:18 +0000508 handshake->tls_prf = tls_prf_sha384;
509 handshake->calc_verify = ssl_calc_verify_tls_sha384;
510 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000511 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000512 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513#endif
514#if defined(POLARSSL_SHA256_C)
515 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000516 {
Paul Bakker48916f92012-09-16 19:57:18 +0000517 handshake->tls_prf = tls_prf_sha256;
518 handshake->calc_verify = ssl_calc_verify_tls_sha256;
519 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000520 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200521 else
522#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200523#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200524 {
525 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200526 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200527 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000528
529 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * SSLv3:
531 * master =
532 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
533 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
534 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200535 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200536 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000537 * master = PRF( premaster, "master secret", randbytes )[0..47]
538 */
Paul Bakker0a597072012-09-25 21:55:46 +0000539 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 {
Paul Bakker48916f92012-09-16 19:57:18 +0000541 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
542 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200544#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
545 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200546 {
547 unsigned char session_hash[48];
548 size_t hash_len;
549
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200550 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200551
552 ssl->handshake->calc_verify( ssl, session_hash );
553
554#if defined(POLARSSL_SSL_PROTO_TLS1_2)
555 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
556 {
557#if defined(POLARSSL_SHA512_C)
558 if( ssl->transform_negotiate->ciphersuite_info->mac ==
559 POLARSSL_MD_SHA384 )
560 {
561 hash_len = 48;
562 }
563 else
564#endif
565 hash_len = 32;
566 }
567 else
568#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
569 hash_len = 36;
570
571 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
572
573 handshake->tls_prf( handshake->premaster, handshake->pmslen,
574 "extended master secret",
575 session_hash, hash_len, session->master, 48 );
576
577 }
578 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200579#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000580 handshake->tls_prf( handshake->premaster, handshake->pmslen,
581 "master secret",
582 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200584
Paul Bakker34617722014-06-13 17:20:13 +0200585 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 }
587 else
588 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
589
590 /*
591 * Swap the client and server random values.
592 */
Paul Bakker48916f92012-09-16 19:57:18 +0000593 memcpy( tmp, handshake->randbytes, 64 );
594 memcpy( handshake->randbytes, tmp + 32, 32 );
595 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200596 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000597
598 /*
599 * SSLv3:
600 * key block =
601 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
602 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
603 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
604 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
605 * ...
606 *
607 * TLSv1:
608 * key block = PRF( master, "key expansion", randbytes )
609 */
Paul Bakker48916f92012-09-16 19:57:18 +0000610 handshake->tls_prf( session->master, 48, "key expansion",
611 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
Paul Bakker48916f92012-09-16 19:57:18 +0000613 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
614 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
615 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
616 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
618
Paul Bakker34617722014-06-13 17:20:13 +0200619 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621 /*
622 * Determine the appropriate key, IV and MAC length.
623 */
Paul Bakker68884e32013-01-07 18:20:04 +0100624
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200625 transform->keylen = cipher_info->key_length / 8;
626
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200627 if( cipher_info->mode == POLARSSL_MODE_GCM ||
628 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200630 transform->maclen = 0;
631
Paul Bakker68884e32013-01-07 18:20:04 +0100632 transform->ivlen = 12;
633 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200634
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200635 /* Minimum length is expicit IV + tag */
636 transform->minlen = transform->ivlen - transform->fixed_ivlen
637 + ( transform->ciphersuite_info->flags &
638 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100639 }
640 else
641 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200642 int ret;
643
644 /* Initialize HMAC contexts */
645 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
646 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100647 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200648 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
649 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100650 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000651
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200652 /* Get MAC length */
653 transform->maclen = md_get_size( md_info );
654
655#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
656 /*
657 * If HMAC is to be truncated, we shall keep the leftmost bytes,
658 * (rfc 6066 page 13 or rfc 2104 section 4),
659 * so we only need to adjust the length here.
660 */
661 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
662 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
663#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
664
665 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100666 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200668 /* Minimum length */
669 if( cipher_info->mode == POLARSSL_MODE_STREAM )
670 transform->minlen = transform->maclen;
671 else
Paul Bakker68884e32013-01-07 18:20:04 +0100672 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200673 /*
674 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100675 * 1. if EtM is in use: one block plus MAC
676 * otherwise: * first multiple of blocklen greater than maclen
677 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200678 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100679#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
680 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
681 {
682 transform->minlen = transform->maclen
683 + cipher_info->block_size;
684 }
685 else
686#endif
687 {
688 transform->minlen = transform->maclen
689 + cipher_info->block_size
690 - transform->maclen % cipher_info->block_size;
691 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200692
693#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
695 ssl->minor_ver == SSL_MINOR_VERSION_1 )
696 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100697 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200698#endif
699#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
700 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
701 ssl->minor_ver == SSL_MINOR_VERSION_3 )
702 {
703 transform->minlen += transform->ivlen;
704 }
705 else
706#endif
707 {
708 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
709 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
710 }
Paul Bakker68884e32013-01-07 18:20:04 +0100711 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 }
713
714 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000715 transform->keylen, transform->minlen, transform->ivlen,
716 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000717
718 /*
719 * Finally setup the cipher contexts, IVs and MAC secrets.
720 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100721#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 if( ssl->endpoint == SSL_IS_CLIENT )
723 {
Paul Bakker48916f92012-09-16 19:57:18 +0000724 key1 = keyblk + transform->maclen * 2;
725 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000726
Paul Bakker68884e32013-01-07 18:20:04 +0100727 mac_enc = keyblk;
728 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000730 /*
731 * This is not used in TLS v1.1.
732 */
Paul Bakker48916f92012-09-16 19:57:18 +0000733 iv_copy_len = ( transform->fixed_ivlen ) ?
734 transform->fixed_ivlen : transform->ivlen;
735 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
736 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000737 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000738 }
739 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100740#endif /* POLARSSL_SSL_CLI_C */
741#if defined(POLARSSL_SSL_SRV_C)
742 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000743 {
Paul Bakker48916f92012-09-16 19:57:18 +0000744 key1 = keyblk + transform->maclen * 2 + transform->keylen;
745 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Paul Bakker68884e32013-01-07 18:20:04 +0100747 mac_enc = keyblk + transform->maclen;
748 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000750 /*
751 * This is not used in TLS v1.1.
752 */
Paul Bakker48916f92012-09-16 19:57:18 +0000753 iv_copy_len = ( transform->fixed_ivlen ) ?
754 transform->fixed_ivlen : transform->ivlen;
755 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
756 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000757 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100759 else
760#endif /* POLARSSL_SSL_SRV_C */
761 {
762 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
763 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
764 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200766#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100767 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
768 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100769 if( transform->maclen > sizeof transform->mac_enc )
770 {
771 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200772 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100773 }
774
Paul Bakker68884e32013-01-07 18:20:04 +0100775 memcpy( transform->mac_enc, mac_enc, transform->maclen );
776 memcpy( transform->mac_dec, mac_dec, transform->maclen );
777 }
778 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200779#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200780#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
781 defined(POLARSSL_SSL_PROTO_TLS1_2)
782 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100783 {
784 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
785 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
786 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200787 else
788#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200789 {
790 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200791 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200792 }
Paul Bakker68884e32013-01-07 18:20:04 +0100793
Paul Bakker05ef8352012-05-08 09:17:57 +0000794#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200795 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000796 {
797 int ret = 0;
798
799 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
800
Paul Bakker07eb38b2012-12-19 14:42:06 +0100801 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
802 transform->iv_enc, transform->iv_dec,
803 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100804 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100805 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000806 {
807 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200808 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000809 }
810 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200811#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000812
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200813 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
814 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200816 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
817 return( ret );
818 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200819
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200820 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
821 cipher_info ) ) != 0 )
822 {
823 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
824 return( ret );
825 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200826
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200827 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
828 cipher_info->key_length,
829 POLARSSL_ENCRYPT ) ) != 0 )
830 {
831 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
832 return( ret );
833 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200834
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200835 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
836 cipher_info->key_length,
837 POLARSSL_DECRYPT ) ) != 0 )
838 {
839 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
840 return( ret );
841 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200842
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200843#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200844 if( cipher_info->mode == POLARSSL_MODE_CBC )
845 {
846 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
847 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200848 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200849 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
850 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200851 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200852
853 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
854 POLARSSL_PADDING_NONE ) ) != 0 )
855 {
856 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
857 return( ret );
858 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000859 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200860#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Paul Bakker34617722014-06-13 17:20:13 +0200862 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Paul Bakker2770fbd2012-07-03 13:30:23 +0000864#if defined(POLARSSL_ZLIB_SUPPORT)
865 // Initialize compression
866 //
Paul Bakker48916f92012-09-16 19:57:18 +0000867 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000868 {
Paul Bakker16770332013-10-11 09:59:44 +0200869 if( ssl->compress_buf == NULL )
870 {
871 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
872 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
873 if( ssl->compress_buf == NULL )
874 {
875 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
876 SSL_BUFFER_LEN ) );
877 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
878 }
879 }
880
Paul Bakker2770fbd2012-07-03 13:30:23 +0000881 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
882
Paul Bakker48916f92012-09-16 19:57:18 +0000883 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
884 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000885
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200886 if( deflateInit( &transform->ctx_deflate,
887 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000888 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000889 {
890 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
891 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
892 }
893 }
894#endif /* POLARSSL_ZLIB_SUPPORT */
895
Paul Bakker5121ce52009-01-03 21:22:43 +0000896 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
897
898 return( 0 );
899}
900
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200901#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000902void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000903{
904 md5_context md5;
905 sha1_context sha1;
906 unsigned char pad_1[48];
907 unsigned char pad_2[48];
908
Paul Bakker380da532012-04-18 16:10:25 +0000909 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910
Paul Bakker48916f92012-09-16 19:57:18 +0000911 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
912 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker380da532012-04-18 16:10:25 +0000914 memset( pad_1, 0x36, 48 );
915 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Paul Bakker48916f92012-09-16 19:57:18 +0000917 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000918 md5_update( &md5, pad_1, 48 );
919 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000920
Paul Bakker380da532012-04-18 16:10:25 +0000921 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000922 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000923 md5_update( &md5, pad_2, 48 );
924 md5_update( &md5, hash, 16 );
925 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000926
Paul Bakker48916f92012-09-16 19:57:18 +0000927 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000928 sha1_update( &sha1, pad_1, 40 );
929 sha1_finish( &sha1, hash + 16 );
930
931 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000932 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000933 sha1_update( &sha1, pad_2, 40 );
934 sha1_update( &sha1, hash + 16, 20 );
935 sha1_finish( &sha1, hash + 16 );
936
937 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
938 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
939
Paul Bakker5b4af392014-06-26 12:09:34 +0200940 md5_free( &md5 );
941 sha1_free( &sha1 );
942
Paul Bakker380da532012-04-18 16:10:25 +0000943 return;
944}
Paul Bakker9af723c2014-05-01 13:03:14 +0200945#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000946
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200947#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000948void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
949{
950 md5_context md5;
951 sha1_context sha1;
952
953 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
954
Paul Bakker48916f92012-09-16 19:57:18 +0000955 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
956 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000957
Paul Bakker48916f92012-09-16 19:57:18 +0000958 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000959 sha1_finish( &sha1, hash + 16 );
960
961 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
962 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
963
Paul Bakker5b4af392014-06-26 12:09:34 +0200964 md5_free( &md5 );
965 sha1_free( &sha1 );
966
Paul Bakker380da532012-04-18 16:10:25 +0000967 return;
968}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200969#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000970
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200971#if defined(POLARSSL_SSL_PROTO_TLS1_2)
972#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000973void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
974{
Paul Bakker9e36f042013-06-30 14:34:05 +0200975 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000976
977 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
978
Paul Bakker9e36f042013-06-30 14:34:05 +0200979 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
980 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000981
982 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
983 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
984
Paul Bakker5b4af392014-06-26 12:09:34 +0200985 sha256_free( &sha256 );
986
Paul Bakker380da532012-04-18 16:10:25 +0000987 return;
988}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200989#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000990
Paul Bakker9e36f042013-06-30 14:34:05 +0200991#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000992void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
993{
Paul Bakker9e36f042013-06-30 14:34:05 +0200994 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000995
996 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
997
Paul Bakker9e36f042013-06-30 14:34:05 +0200998 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
999 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001000
Paul Bakkerca4ab492012-04-18 14:23:57 +00001001 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1003
Paul Bakker5b4af392014-06-26 12:09:34 +02001004 sha512_free( &sha512 );
1005
Paul Bakker5121ce52009-01-03 21:22:43 +00001006 return;
1007}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001008#endif /* POLARSSL_SHA512_C */
1009#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001010
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001011#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001012int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1013{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001014 unsigned char *p = ssl->handshake->premaster;
1015 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1016
1017 /*
1018 * PMS = struct {
1019 * opaque other_secret<0..2^16-1>;
1020 * opaque psk<0..2^16-1>;
1021 * };
1022 * with "other_secret" depending on the particular key exchange
1023 */
1024#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1025 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1026 {
1027 if( end - p < 2 + (int) ssl->psk_len )
1028 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1029
1030 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1031 *(p++) = (unsigned char)( ssl->psk_len );
1032 p += ssl->psk_len;
1033 }
1034 else
1035#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001036#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1037 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1038 {
1039 /*
1040 * other_secret already set by the ClientKeyExchange message,
1041 * and is 48 bytes long
1042 */
1043 *p++ = 0;
1044 *p++ = 48;
1045 p += 48;
1046 }
1047 else
1048#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001049#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1050 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1051 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001052 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001053 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001054
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001055 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001056 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001057 p + 2, &len,
1058 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001059 {
1060 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1061 return( ret );
1062 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001063 *(p++) = (unsigned char)( len >> 8 );
1064 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001065 p += len;
1066
1067 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1068 }
1069 else
1070#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1071#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1072 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1073 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001074 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001075 size_t zlen;
1076
1077 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001078 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001079 ssl->f_rng, ssl->p_rng ) ) != 0 )
1080 {
1081 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1082 return( ret );
1083 }
1084
1085 *(p++) = (unsigned char)( zlen >> 8 );
1086 *(p++) = (unsigned char)( zlen );
1087 p += zlen;
1088
1089 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1090 }
1091 else
1092#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1093 {
1094 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001095 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001096 }
1097
1098 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001099 if( end - p < 2 + (int) ssl->psk_len )
1100 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1101
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001102 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1103 *(p++) = (unsigned char)( ssl->psk_len );
1104 memcpy( p, ssl->psk, ssl->psk_len );
1105 p += ssl->psk_len;
1106
1107 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1108
1109 return( 0 );
1110}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001111#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001112
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001113#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001114/*
1115 * SSLv3.0 MAC functions
1116 */
Paul Bakker68884e32013-01-07 18:20:04 +01001117static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1118 unsigned char *buf, size_t len,
1119 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120{
1121 unsigned char header[11];
1122 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001123 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001124 int md_size = md_get_size( md_ctx->md_info );
1125 int md_type = md_get_type( md_ctx->md_info );
1126
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001127 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001128 if( md_type == POLARSSL_MD_MD5 )
1129 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001130 else
Paul Bakker68884e32013-01-07 18:20:04 +01001131 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001132
1133 memcpy( header, ctr, 8 );
1134 header[ 8] = (unsigned char) type;
1135 header[ 9] = (unsigned char)( len >> 8 );
1136 header[10] = (unsigned char)( len );
1137
Paul Bakker68884e32013-01-07 18:20:04 +01001138 memset( padding, 0x36, padlen );
1139 md_starts( md_ctx );
1140 md_update( md_ctx, secret, md_size );
1141 md_update( md_ctx, padding, padlen );
1142 md_update( md_ctx, header, 11 );
1143 md_update( md_ctx, buf, len );
1144 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001145
Paul Bakker68884e32013-01-07 18:20:04 +01001146 memset( padding, 0x5C, padlen );
1147 md_starts( md_ctx );
1148 md_update( md_ctx, secret, md_size );
1149 md_update( md_ctx, padding, padlen );
1150 md_update( md_ctx, buf + len, md_size );
1151 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001152}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001153#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001154
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001155#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1156 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1157 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1158#define POLARSSL_SOME_MODES_USE_MAC
1159#endif
1160
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001161/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001163 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001164static int ssl_encrypt_buf( ssl_context *ssl )
1165{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001166 cipher_mode_t mode;
1167 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001168
1169 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1170
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001171 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1172 {
1173 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1174 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1175 }
1176
1177 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1178
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001179 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1180 ssl->out_msg, ssl->out_msglen );
1181
Paul Bakker5121ce52009-01-03 21:22:43 +00001182 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001183 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001185#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001186 if( mode == POLARSSL_MODE_STREAM ||
1187 ( mode == POLARSSL_MODE_CBC
1188#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1189 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1190#endif
1191 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001193#if defined(POLARSSL_SSL_PROTO_SSL3)
1194 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1195 {
1196 ssl_mac( &ssl->transform_out->md_ctx_enc,
1197 ssl->transform_out->mac_enc,
1198 ssl->out_msg, ssl->out_msglen,
1199 ssl->out_ctr, ssl->out_msgtype );
1200 }
1201 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001202#endif
1203#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001204 defined(POLARSSL_SSL_PROTO_TLS1_2)
1205 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1206 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001207 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1208 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1209 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001210 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1211 ssl->out_msg, ssl->out_msglen );
1212 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1213 ssl->out_msg + ssl->out_msglen );
1214 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1215 }
1216 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001217#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001218 {
1219 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001220 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001221 }
1222
1223 SSL_DEBUG_BUF( 4, "computed mac",
1224 ssl->out_msg + ssl->out_msglen,
1225 ssl->transform_out->maclen );
1226
1227 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001228 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001229 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001230#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001232 /*
1233 * Encrypt
1234 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001235#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001236 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001238 int ret;
1239 size_t olen = 0;
1240
Paul Bakker5121ce52009-01-03 21:22:43 +00001241 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1242 "including %d bytes of padding",
1243 ssl->out_msglen, 0 ) );
1244
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001245 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001246 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001247 ssl->transform_out->ivlen,
1248 ssl->out_msg, ssl->out_msglen,
1249 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001250 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001251 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001252 return( ret );
1253 }
1254
1255 if( ssl->out_msglen != olen )
1256 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001257 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001258 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 }
Paul Bakker68884e32013-01-07 18:20:04 +01001261 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001262#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001263#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1264 if( mode == POLARSSL_MODE_GCM ||
1265 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001266 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001268 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001269 unsigned char *enc_msg;
1270 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001271 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1272 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001273
Paul Bakkerca4ab492012-04-18 14:23:57 +00001274 memcpy( add_data, ssl->out_ctr, 8 );
1275 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001276 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1277 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001278 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1279 add_data[12] = ssl->out_msglen & 0xFF;
1280
1281 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1282 add_data, 13 );
1283
Paul Bakker68884e32013-01-07 18:20:04 +01001284 /*
1285 * Generate IV
1286 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001287#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001288 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001289 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1290 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001291 if( ret != 0 )
1292 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001293
Paul Bakker68884e32013-01-07 18:20:04 +01001294 memcpy( ssl->out_iv,
1295 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1296 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001297#else
1298 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1299 {
1300 /* Reminder if we ever add an AEAD mode with a different size */
1301 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1302 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1303 }
1304
1305 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1306 ssl->out_ctr, 8 );
1307 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1308#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001309
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001310 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001311 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001312
Paul Bakker68884e32013-01-07 18:20:04 +01001313 /*
1314 * Fix pointer positions and message length with added IV
1315 */
1316 enc_msg = ssl->out_msg;
1317 enc_msglen = ssl->out_msglen;
1318 ssl->out_msglen += ssl->transform_out->ivlen -
1319 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001320
Paul Bakker68884e32013-01-07 18:20:04 +01001321 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1322 "including %d bytes of padding",
1323 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001324
Paul Bakker68884e32013-01-07 18:20:04 +01001325 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001326 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001327 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001328 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1329 ssl->transform_out->iv_enc,
1330 ssl->transform_out->ivlen,
1331 add_data, 13,
1332 enc_msg, enc_msglen,
1333 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001334 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001335 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001336 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001337 return( ret );
1338 }
1339
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001340 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001341 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001342 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001343 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001344 }
1345
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001346 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001347 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001348
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001349 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001350 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001352#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001353#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1354 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001355 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001357 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001358 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001359 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001360
Paul Bakker48916f92012-09-16 19:57:18 +00001361 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1362 ssl->transform_out->ivlen;
1363 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 padlen = 0;
1365
1366 for( i = 0; i <= padlen; i++ )
1367 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1368
1369 ssl->out_msglen += padlen + 1;
1370
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001371 enc_msglen = ssl->out_msglen;
1372 enc_msg = ssl->out_msg;
1373
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001374#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001375 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001376 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1377 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001378 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001379 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001380 {
1381 /*
1382 * Generate IV
1383 */
Paul Bakker48916f92012-09-16 19:57:18 +00001384 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1385 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001386 if( ret != 0 )
1387 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001388
Paul Bakker92be97b2013-01-02 17:30:03 +01001389 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001390 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391
1392 /*
1393 * Fix pointer positions and message length with added IV
1394 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001395 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001396 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001397 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001398 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001399#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001400
Paul Bakker5121ce52009-01-03 21:22:43 +00001401 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001402 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001403 ssl->out_msglen, ssl->transform_out->ivlen,
1404 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001405
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001406 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001407 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001408 ssl->transform_out->ivlen,
1409 enc_msg, enc_msglen,
1410 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001411 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001412 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001413 return( ret );
1414 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001415
Paul Bakkercca5b812013-08-31 17:40:26 +02001416 if( enc_msglen != olen )
1417 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001418 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001419 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001420 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001421
1422#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001423 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1424 {
1425 /*
1426 * Save IV in SSL3 and TLS1
1427 */
1428 memcpy( ssl->transform_out->iv_enc,
1429 ssl->transform_out->cipher_ctx_enc.iv,
1430 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001432#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001433
1434#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001435 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001436 {
1437 /*
1438 * MAC(MAC_write_key, seq_num +
1439 * TLSCipherText.type +
1440 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001441 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001442 * IV + // except for TLS 1.0
1443 * ENC(content + padding + padding_length));
1444 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001445 unsigned char pseudo_hdr[13];
1446
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001447 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1448
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001449 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1450 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001451 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1452 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1453
1454 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001455
1456 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1457 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1458 ssl->out_iv, ssl->out_msglen );
1459 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1460 ssl->out_iv + ssl->out_msglen );
1461 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1462
1463 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001464 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001465 }
1466#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001468 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001469#endif /* POLARSSL_CIPHER_MODE_CBC &&
1470 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001471 {
1472 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001473 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001475
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001476 /* Make extra sure authentication was performed, exactly once */
1477 if( auth_done != 1 )
1478 {
1479 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1480 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1481 }
1482
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1484
1485 return( 0 );
1486}
1487
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001488#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001489
Paul Bakker5121ce52009-01-03 21:22:43 +00001490static int ssl_decrypt_buf( ssl_context *ssl )
1491{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001492 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001493 cipher_mode_t mode;
1494 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001495#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001496 size_t padlen = 0, correct = 1;
1497#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001498
1499 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1500
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001501 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1502 {
1503 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1504 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1505 }
1506
1507 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1508
Paul Bakker48916f92012-09-16 19:57:18 +00001509 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 {
1511 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001512 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001513 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
1515
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001516#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001517 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001518 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001519 int ret;
1520 size_t olen = 0;
1521
Paul Bakker68884e32013-01-07 18:20:04 +01001522 padlen = 0;
1523
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001524 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001525 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001526 ssl->transform_in->ivlen,
1527 ssl->in_msg, ssl->in_msglen,
1528 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001529 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001530 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001531 return( ret );
1532 }
1533
1534 if( ssl->in_msglen != olen )
1535 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001536 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001537 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 }
Paul Bakker68884e32013-01-07 18:20:04 +01001540 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001541#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001542#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1543 if( mode == POLARSSL_MODE_GCM ||
1544 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001545 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001546 int ret;
1547 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001548 unsigned char *dec_msg;
1549 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001550 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001551 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1552 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001553 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1554 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001555
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001556 if( ssl->in_msglen < explicit_iv_len + taglen )
1557 {
1558 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1559 "+ taglen (%d)", ssl->in_msglen,
1560 explicit_iv_len, taglen ) );
1561 return( POLARSSL_ERR_SSL_INVALID_MAC );
1562 }
1563 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1564
Paul Bakker68884e32013-01-07 18:20:04 +01001565 dec_msg = ssl->in_msg;
1566 dec_msg_result = ssl->in_msg;
1567 ssl->in_msglen = dec_msglen;
1568
1569 memcpy( add_data, ssl->in_ctr, 8 );
1570 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001571 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1572 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001573 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1574 add_data[12] = ssl->in_msglen & 0xFF;
1575
1576 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1577 add_data, 13 );
1578
1579 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1580 ssl->in_iv,
1581 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1582
1583 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1584 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001585 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001586
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001587 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001588 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001589 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001590 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1591 ssl->transform_in->iv_dec,
1592 ssl->transform_in->ivlen,
1593 add_data, 13,
1594 dec_msg, dec_msglen,
1595 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001596 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001597 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001598 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1599
1600 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1601 return( POLARSSL_ERR_SSL_INVALID_MAC );
1602
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001603 return( ret );
1604 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001605 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001606
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001607 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001608 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001609 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001610 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001611 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001612 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001614#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001615#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1616 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001617 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001618 {
Paul Bakker45829992013-01-03 14:52:21 +01001619 /*
1620 * Decrypt and check the padding
1621 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001622 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001623 unsigned char *dec_msg;
1624 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001625 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001626 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001627 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001628
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 /*
Paul Bakker45829992013-01-03 14:52:21 +01001630 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001632#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001633 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1634 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001635#endif
Paul Bakker45829992013-01-03 14:52:21 +01001636
1637 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1638 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1639 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001640 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1641 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1642 ssl->transform_in->ivlen,
1643 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001644 return( POLARSSL_ERR_SSL_INVALID_MAC );
1645 }
1646
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001647 dec_msglen = ssl->in_msglen;
1648 dec_msg = ssl->in_msg;
1649 dec_msg_result = ssl->in_msg;
1650
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001651 /*
1652 * Authenticate before decrypt if enabled
1653 */
1654#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001655 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001656 {
1657 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001658 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001659
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001660 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1661
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001662 dec_msglen -= ssl->transform_in->maclen;
1663 ssl->in_msglen -= ssl->transform_in->maclen;
1664
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001665 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1666 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1667 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1668 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1669
1670 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1671
1672 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001673 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1674 ssl->in_iv, ssl->in_msglen );
1675 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1676 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1677
1678 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1679 ssl->transform_in->maclen );
1680 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1681 ssl->transform_in->maclen );
1682
1683 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1684 ssl->transform_in->maclen ) != 0 )
1685 {
1686 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1687
1688 return( POLARSSL_ERR_SSL_INVALID_MAC );
1689 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001690 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001691 }
1692#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1693
1694 /*
1695 * Check length sanity
1696 */
1697 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1698 {
1699 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1700 ssl->in_msglen, ssl->transform_in->ivlen ) );
1701 return( POLARSSL_ERR_SSL_INVALID_MAC );
1702 }
1703
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001704#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001705 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001706 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001707 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001708 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001709 {
Paul Bakker48916f92012-09-16 19:57:18 +00001710 dec_msglen -= ssl->transform_in->ivlen;
1711 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001712
Paul Bakker48916f92012-09-16 19:57:18 +00001713 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001714 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001716#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001717
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001718 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001719 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001720 ssl->transform_in->ivlen,
1721 dec_msg, dec_msglen,
1722 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001723 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001724 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001725 return( ret );
1726 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001727
Paul Bakkercca5b812013-08-31 17:40:26 +02001728 if( dec_msglen != olen )
1729 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001730 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001731 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001732 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001733
1734#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001735 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1736 {
1737 /*
1738 * Save IV in SSL3 and TLS1
1739 */
1740 memcpy( ssl->transform_in->iv_dec,
1741 ssl->transform_in->cipher_ctx_dec.iv,
1742 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001744#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
1746 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001747
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001748 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001749 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001750 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001751#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001752 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1753 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001754#endif
Paul Bakker45829992013-01-03 14:52:21 +01001755 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001756 correct = 0;
1757 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001759#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001760 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1761 {
Paul Bakker48916f92012-09-16 19:57:18 +00001762 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001764#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1766 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001767 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001768#endif
Paul Bakker45829992013-01-03 14:52:21 +01001769 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 }
1771 }
1772 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001773#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001774#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1775 defined(POLARSSL_SSL_PROTO_TLS1_2)
1776 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 {
1778 /*
Paul Bakker45829992013-01-03 14:52:21 +01001779 * TLSv1+: always check the padding up to the first failure
1780 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001781 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001782 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001783 size_t padding_idx = ssl->in_msglen - padlen - 1;
1784
Paul Bakker956c9e02013-12-19 14:42:28 +01001785 /*
1786 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001787 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001788 *
Paul Bakker61885c72014-04-25 12:59:03 +02001789 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1790 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001791 *
1792 * In both cases we reset padding_idx to a safe value (0) to
1793 * prevent out-of-buffer reads.
1794 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001795 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001796 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1797 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001798
1799 padding_idx *= correct;
1800
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001801 for( i = 1; i <= 256; i++ )
1802 {
1803 real_count &= ( i <= padlen );
1804 pad_count += real_count *
1805 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1806 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001807
1808 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001809
Paul Bakkerd66f0702013-01-31 16:57:45 +01001810#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001811 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001812 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001813#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001814 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001815 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001816 else
1817#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1818 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001819 {
1820 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001821 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001822 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001823
1824 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001825 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001826 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001827#endif /* POLARSSL_CIPHER_MODE_CBC &&
1828 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001829 {
1830 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001831 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
1834 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1835 ssl->in_msg, ssl->in_msglen );
1836
1837 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001838 * Authenticate if not done yet.
1839 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001841#if defined(POLARSSL_SOME_MODES_USE_MAC)
1842 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001844 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1845
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001846 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001847
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001848 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1849 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001851 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001853#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1855 {
1856 ssl_mac( &ssl->transform_in->md_ctx_dec,
1857 ssl->transform_in->mac_dec,
1858 ssl->in_msg, ssl->in_msglen,
1859 ssl->in_ctr, ssl->in_msgtype );
1860 }
1861 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001862#endif /* POLARSSL_SSL_PROTO_SSL3 */
1863#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001864 defined(POLARSSL_SSL_PROTO_TLS1_2)
1865 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1866 {
1867 /*
1868 * Process MAC and always update for padlen afterwards to make
1869 * total time independent of padlen
1870 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001871 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001872 *
1873 * Known timing attacks:
1874 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1875 *
1876 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1877 * correctly. (We round down instead of up, so -56 is the correct
1878 * value for our calculations instead of -55)
1879 */
1880 size_t j, extra_run = 0;
1881 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1882 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001883
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001884 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001885
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001886 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1887 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1888 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001889 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1890 ssl->in_msglen );
1891 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1892 ssl->in_msg + ssl->in_msglen );
1893 for( j = 0; j < extra_run; j++ )
1894 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001895
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001896 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1897 }
1898 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001899#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001900 POLARSSL_SSL_PROTO_TLS1_2 */
1901 {
1902 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001903 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001906 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1907 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1908 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001910 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001911 ssl->transform_in->maclen ) != 0 )
1912 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001913#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001914 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001915#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001916 correct = 0;
1917 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001918 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001919
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001920 /*
1921 * Finally check the correct flag
1922 */
1923 if( correct == 0 )
1924 return( POLARSSL_ERR_SSL_INVALID_MAC );
1925 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001926#endif /* POLARSSL_SOME_MODES_USE_MAC */
1927
1928 /* Make extra sure authentication was performed, exactly once */
1929 if( auth_done != 1 )
1930 {
1931 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1932 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1933 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001934
1935 if( ssl->in_msglen == 0 )
1936 {
1937 ssl->nb_zero++;
1938
1939 /*
1940 * Three or more empty messages may be a DoS attack
1941 * (excessive CPU consumption).
1942 */
1943 if( ssl->nb_zero > 3 )
1944 {
1945 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1946 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001947 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 }
1949 }
1950 else
1951 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001952
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001953#if defined(POLARSSL_SSL_PROTO_DTLS)
1954 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001955 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001956 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001957 }
1958 else
1959#endif
1960 {
1961 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1962 if( ++ssl->in_ctr[i - 1] != 0 )
1963 break;
1964
1965 /* The loop goes to its end iff the counter is wrapping */
1966 if( i == ssl_ep_len( ssl ) )
1967 {
1968 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1969 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1970 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001971 }
1972
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1974
1975 return( 0 );
1976}
1977
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001978#undef MAC_NONE
1979#undef MAC_PLAINTEXT
1980#undef MAC_CIPHERTEXT
1981
Paul Bakker2770fbd2012-07-03 13:30:23 +00001982#if defined(POLARSSL_ZLIB_SUPPORT)
1983/*
1984 * Compression/decompression functions
1985 */
1986static int ssl_compress_buf( ssl_context *ssl )
1987{
1988 int ret;
1989 unsigned char *msg_post = ssl->out_msg;
1990 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001991 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992
1993 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1994
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001995 if( len_pre == 0 )
1996 return( 0 );
1997
Paul Bakker2770fbd2012-07-03 13:30:23 +00001998 memcpy( msg_pre, ssl->out_msg, len_pre );
1999
2000 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2001 ssl->out_msglen ) );
2002
2003 SSL_DEBUG_BUF( 4, "before compression: output payload",
2004 ssl->out_msg, ssl->out_msglen );
2005
Paul Bakker48916f92012-09-16 19:57:18 +00002006 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2007 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2008 ssl->transform_out->ctx_deflate.next_out = msg_post;
2009 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002010
Paul Bakker48916f92012-09-16 19:57:18 +00002011 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002012 if( ret != Z_OK )
2013 {
2014 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2015 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2016 }
2017
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002018 ssl->out_msglen = SSL_BUFFER_LEN -
2019 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002020
Paul Bakker2770fbd2012-07-03 13:30:23 +00002021 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2022 ssl->out_msglen ) );
2023
2024 SSL_DEBUG_BUF( 4, "after compression: output payload",
2025 ssl->out_msg, ssl->out_msglen );
2026
2027 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2028
2029 return( 0 );
2030}
2031
2032static int ssl_decompress_buf( ssl_context *ssl )
2033{
2034 int ret;
2035 unsigned char *msg_post = ssl->in_msg;
2036 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002037 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002038
2039 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2040
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002041 if( len_pre == 0 )
2042 return( 0 );
2043
Paul Bakker2770fbd2012-07-03 13:30:23 +00002044 memcpy( msg_pre, ssl->in_msg, len_pre );
2045
2046 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2047 ssl->in_msglen ) );
2048
2049 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2050 ssl->in_msg, ssl->in_msglen );
2051
Paul Bakker48916f92012-09-16 19:57:18 +00002052 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2053 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2054 ssl->transform_in->ctx_inflate.next_out = msg_post;
2055 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002056
Paul Bakker48916f92012-09-16 19:57:18 +00002057 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002058 if( ret != Z_OK )
2059 {
2060 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2061 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2062 }
2063
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002064 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2065 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002066
Paul Bakker2770fbd2012-07-03 13:30:23 +00002067 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2068 ssl->in_msglen ) );
2069
2070 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2071 ssl->in_msg, ssl->in_msglen );
2072
2073 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2074
2075 return( 0 );
2076}
2077#endif /* POLARSSL_ZLIB_SUPPORT */
2078
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002079#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002080static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002081
2082#if defined(POLARSSL_SSL_PROTO_DTLS)
2083static int ssl_resend_hello_request( ssl_context *ssl )
2084{
2085 /* If renegotiation is not enforced, retransmit until we would reach max
2086 * timeout if we were using the usual handshake doubling scheme */
2087 if( ssl->renego_max_records < 0 )
2088 {
2089 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2090 unsigned char doublings = 1;
2091
2092 while( ratio != 0 )
2093 {
2094 ++doublings;
2095 ratio >>= 1;
2096 }
2097
2098 if( ++ssl->renego_records_seen > doublings )
2099 {
2100 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2101 return( 0 );
2102 }
2103 }
2104
2105 return( ssl_write_hello_request( ssl ) );
2106}
2107#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002108#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002109
Paul Bakker5121ce52009-01-03 21:22:43 +00002110/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002111 * Fill the input message buffer by appending data to it.
2112 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002113 *
2114 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2115 * available (from this read and/or a previous one). Otherwise, an error code
2116 * is returned (possibly EOF or WANT_READ).
2117 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002118 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2119 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2120 * since we always read a whole datagram at once.
2121 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002122 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002123 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 */
Paul Bakker23986e52011-04-24 08:57:21 +00002125int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002126{
Paul Bakker23986e52011-04-24 08:57:21 +00002127 int ret;
2128 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002129
2130 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2131
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002132 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2133 {
2134 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2135 "or ssl_set_bio_timeout()" ) );
2136 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2137 }
2138
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002139 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002140 {
2141 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2142 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2143 }
2144
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002145#if defined(POLARSSL_SSL_PROTO_DTLS)
2146 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002148 uint32_t timeout;
2149
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002150 /*
2151 * The point is, we need to always read a full datagram at once, so we
2152 * sometimes read more then requested, and handle the additional data.
2153 * It could be the rest of the current record (while fetching the
2154 * header) and/or some other records in the same datagram.
2155 */
2156
2157 /*
2158 * Move to the next record in the already read datagram if applicable
2159 */
2160 if( ssl->next_record_offset != 0 )
2161 {
2162 if( ssl->in_left < ssl->next_record_offset )
2163 {
2164 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2165 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2166 }
2167
2168 ssl->in_left -= ssl->next_record_offset;
2169
2170 if( ssl->in_left != 0 )
2171 {
2172 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2173 ssl->next_record_offset ) );
2174 memmove( ssl->in_hdr,
2175 ssl->in_hdr + ssl->next_record_offset,
2176 ssl->in_left );
2177 }
2178
2179 ssl->next_record_offset = 0;
2180 }
2181
Paul Bakker5121ce52009-01-03 21:22:43 +00002182 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2183 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002184
2185 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002186 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002187 */
2188 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002189 {
2190 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002191 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002192 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002193
2194 /*
2195 * A record can't be split accross datagrams. If we need to read but
2196 * are not at the beginning of a new record, the caller did something
2197 * wrong.
2198 */
2199 if( ssl->in_left != 0 )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2203 }
2204
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002205 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002206
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002207 /*
2208 * Don't even try to read if time's out already.
2209 * This avoids by-passing the timer when repeatedly receiving messages
2210 * that will end up being dropped.
2211 */
2212 if( ssl_check_timer( ssl ) != 0 )
2213 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002214 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002215 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002216 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2217
2218 if( ssl->state != SSL_HANDSHAKE_OVER )
2219 timeout = ssl->handshake->retransmit_timeout;
2220 else
2221 timeout = ssl->read_timeout;
2222
2223 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2224
2225 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2226 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2227 timeout );
2228 else
2229 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2230
2231 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2232
2233 if( ret == 0 )
2234 return( POLARSSL_ERR_SSL_CONN_EOF );
2235 }
2236
2237 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2238 {
2239 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2240 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002241
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002242 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002243 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002244 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2245 {
2246 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2247 return( POLARSSL_ERR_NET_TIMEOUT );
2248 }
2249
2250 if( ( ret = ssl_resend( ssl ) ) != 0 )
2251 {
2252 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2253 return( ret );
2254 }
2255
2256 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002257 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002258#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002259 else if( ssl->endpoint == SSL_IS_SERVER &&
2260 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2261 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002262 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002263 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002264 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002265 return( ret );
2266 }
2267
2268 return( POLARSSL_ERR_NET_WANT_READ );
2269 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002270#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002271 }
2272
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 if( ret < 0 )
2274 return( ret );
2275
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002276 ssl->in_left = ret;
2277 }
2278 else
2279#endif
2280 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002281 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2282 ssl->in_left, nb_want ) );
2283
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002284 while( ssl->in_left < nb_want )
2285 {
2286 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002287 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002288
2289 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2290 ssl->in_left, nb_want ) );
2291 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2292
2293 if( ret == 0 )
2294 return( POLARSSL_ERR_SSL_CONN_EOF );
2295
2296 if( ret < 0 )
2297 return( ret );
2298
2299 ssl->in_left += ret;
2300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
2302
2303 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2304
2305 return( 0 );
2306}
2307
2308/*
2309 * Flush any data not yet written
2310 */
2311int ssl_flush_output( ssl_context *ssl )
2312{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002313 int ret;
2314 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002315
2316 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2317
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002318 if( ssl->f_send == NULL )
2319 {
2320 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2321 "or ssl_set_bio_timeout()" ) );
2322 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2323 }
2324
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002325 /* Avoid incrementing counter if data is flushed */
2326 if( ssl->out_left == 0 )
2327 {
2328 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2329 return( 0 );
2330 }
2331
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 while( ssl->out_left > 0 )
2333 {
2334 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002335 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002337 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2338 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002339 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002340
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2342
2343 if( ret <= 0 )
2344 return( ret );
2345
2346 ssl->out_left -= ret;
2347 }
2348
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002349 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002350 if( ++ssl->out_ctr[i - 1] != 0 )
2351 break;
2352
2353 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002354 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002355 {
2356 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2357 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2358 }
2359
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2361
2362 return( 0 );
2363}
2364
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002365/*
2366 * Functions to handle the DTLS retransmission state machine
2367 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002368#if defined(POLARSSL_SSL_PROTO_DTLS)
2369/*
2370 * Append current handshake message to current outgoing flight
2371 */
2372static int ssl_flight_append( ssl_context *ssl )
2373{
2374 ssl_flight_item *msg;
2375
2376 /* Allocate space for current message */
2377 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2378 {
2379 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2380 sizeof( ssl_flight_item ) ) );
2381 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2382 }
2383
2384 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2385 {
2386 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002387 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002388 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2389 }
2390
2391 /* Copy current handshake message with headers */
2392 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2393 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002394 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002395 msg->next = NULL;
2396
2397 /* Append to the current flight */
2398 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002399 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002400 else
2401 {
2402 ssl_flight_item *cur = ssl->handshake->flight;
2403 while( cur->next != NULL )
2404 cur = cur->next;
2405 cur->next = msg;
2406 }
2407
2408 return( 0 );
2409}
2410
2411/*
2412 * Free the current flight of handshake messages
2413 */
2414static void ssl_flight_free( ssl_flight_item *flight )
2415{
2416 ssl_flight_item *cur = flight;
2417 ssl_flight_item *next;
2418
2419 while( cur != NULL )
2420 {
2421 next = cur->next;
2422
2423 polarssl_free( cur->p );
2424 polarssl_free( cur );
2425
2426 cur = next;
2427 }
2428}
2429
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002430#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2431static void ssl_dtls_replay_reset( ssl_context *ssl );
2432#endif
2433
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002434/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002435 * Swap transform_out and out_ctr with the alternative ones
2436 */
2437static void ssl_swap_epochs( ssl_context *ssl )
2438{
2439 ssl_transform *tmp_transform;
2440 unsigned char tmp_out_ctr[8];
2441
2442 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2443 {
2444 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2445 return;
2446 }
2447
2448 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2449
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002450 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002451 tmp_transform = ssl->transform_out;
2452 ssl->transform_out = ssl->handshake->alt_transform_out;
2453 ssl->handshake->alt_transform_out = tmp_transform;
2454
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002455 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002456 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2457 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2458 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002459
2460 /* Adjust to the newly activated transform */
2461 if( ssl->transform_out != NULL &&
2462 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2463 {
2464 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2465 ssl->transform_out->fixed_ivlen;
2466 }
2467 else
2468 ssl->out_msg = ssl->out_iv;
2469
2470#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2471 if( ssl_hw_record_activate != NULL )
2472 {
2473 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2474 {
2475 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2476 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2477 }
2478 }
2479#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002480}
2481
2482/*
2483 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002484 *
2485 * Need to remember the current message in case flush_output returns
2486 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002487 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002488 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002489int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002490{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002491 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002492
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002493 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2494 {
2495 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2496
2497 ssl->handshake->cur_msg = ssl->handshake->flight;
2498 ssl_swap_epochs( ssl );
2499
2500 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2501 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002502
2503 while( ssl->handshake->cur_msg != NULL )
2504 {
2505 int ret;
2506 ssl_flight_item *cur = ssl->handshake->cur_msg;
2507
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002508 /* Swap epochs before sending Finished: we can't do it after
2509 * sending ChangeCipherSpec, in case write returns WANT_READ.
2510 * Must be done before copying, may change out_msg pointer */
2511 if( cur->type == SSL_MSG_HANDSHAKE &&
2512 cur->p[0] == SSL_HS_FINISHED )
2513 {
2514 ssl_swap_epochs( ssl );
2515 }
2516
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002517 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002518 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002519 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002520
2521 ssl->handshake->cur_msg = cur->next;
2522
2523 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2524
2525 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2526 {
2527 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2528 return( ret );
2529 }
2530 }
2531
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002532 if( ssl->state == SSL_HANDSHAKE_OVER )
2533 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2534 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002535 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002536 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002537 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2538 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002539
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002540 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002541
2542 return( 0 );
2543}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002544
2545/*
2546 * To be called when the last message of an incoming flight is received.
2547 */
2548void ssl_recv_flight_completed( ssl_context *ssl )
2549{
2550 /* We won't need to resend that one any more */
2551 ssl_flight_free( ssl->handshake->flight );
2552 ssl->handshake->flight = NULL;
2553 ssl->handshake->cur_msg = NULL;
2554
2555 /* The next incoming flight will start with this msg_seq */
2556 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2557
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002558 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002559 ssl_set_timer( ssl, 0 );
2560
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002561 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2562 ssl->in_msg[0] == SSL_HS_FINISHED )
2563 {
2564 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2565 }
2566 else
2567 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2568}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002569
2570/*
2571 * To be called when the last message of an outgoing flight is send.
2572 */
2573void ssl_send_flight_completed( ssl_context *ssl )
2574{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002575 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002576 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002577
2578 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2579 ssl->in_msg[0] == SSL_HS_FINISHED )
2580 {
2581 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2582 }
2583 else
2584 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2585}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002586#endif /* POLARSSL_SSL_PROTO_DTLS */
2587
Paul Bakker5121ce52009-01-03 21:22:43 +00002588/*
2589 * Record layer functions
2590 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002591
2592/*
2593 * Write current record.
2594 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2595 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002596int ssl_write_record( ssl_context *ssl )
2597{
Paul Bakker05ef8352012-05-08 09:17:57 +00002598 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002599 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2602
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002603#if defined(POLARSSL_SSL_PROTO_DTLS)
2604 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2605 ssl->handshake != NULL &&
2606 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2607 {
2608 ; /* Skip special handshake treatment when resending */
2609 }
2610 else
2611#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2613 {
2614 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2615 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2616 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2617
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002618 /*
2619 * DTLS has additional fields in the Handshake layer,
2620 * between the length field and the actual payload:
2621 * uint16 message_seq;
2622 * uint24 fragment_offset;
2623 * uint24 fragment_length;
2624 */
2625#if defined(POLARSSL_SSL_PROTO_DTLS)
2626 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2627 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002628 /* Make room for the additional DTLS fields */
2629 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002630 ssl->out_msglen += 8;
2631 len += 8;
2632
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002633 /* Write message_seq and update it, except for HelloRequest */
2634 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2635 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002636 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2637 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2638 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002639 }
2640 else
2641 {
2642 ssl->out_msg[4] = 0;
2643 ssl->out_msg[5] = 0;
2644 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002645
2646 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2647 memset( ssl->out_msg + 6, 0x00, 3 );
2648 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002649 }
2650#endif /* POLARSSL_SSL_PROTO_DTLS */
2651
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002652 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2653 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002656 /* Save handshake and CCS messages for resending */
2657#if defined(POLARSSL_SSL_PROTO_DTLS)
2658 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2659 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002660 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002661 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2662 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2663 {
2664 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2665 {
2666 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2667 return( ret );
2668 }
2669 }
2670#endif
2671
Paul Bakker2770fbd2012-07-03 13:30:23 +00002672#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002673 if( ssl->transform_out != NULL &&
2674 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002675 {
2676 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2677 {
2678 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2679 return( ret );
2680 }
2681
2682 len = ssl->out_msglen;
2683 }
2684#endif /*POLARSSL_ZLIB_SUPPORT */
2685
Paul Bakker05ef8352012-05-08 09:17:57 +00002686#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002687 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002689 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002690
Paul Bakker05ef8352012-05-08 09:17:57 +00002691 ret = ssl_hw_record_write( ssl );
2692 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2693 {
2694 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002695 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002696 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002697
2698 if( ret == 0 )
2699 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002700 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002701#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002702 if( !done )
2703 {
2704 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002705 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2706 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002707
2708 ssl->out_len[0] = (unsigned char)( len >> 8 );
2709 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002710
Paul Bakker48916f92012-09-16 19:57:18 +00002711 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002712 {
2713 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2714 {
2715 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2716 return( ret );
2717 }
2718
2719 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002720 ssl->out_len[0] = (unsigned char)( len >> 8 );
2721 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002722 }
2723
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002724 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002725
2726 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2727 "version = [%d:%d], msglen = %d",
2728 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002729 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002730
2731 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002732 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2736 {
2737 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2738 return( ret );
2739 }
2740
2741 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2742
2743 return( 0 );
2744}
2745
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002746#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002747/*
2748 * Mark bits in bitmask (used for DTLS HS reassembly)
2749 */
2750static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2751{
2752 unsigned int start_bits, end_bits;
2753
2754 start_bits = 8 - ( offset % 8 );
2755 if( start_bits != 8 )
2756 {
2757 size_t first_byte_idx = offset / 8;
2758
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002759 /* Special case */
2760 if( len <= start_bits )
2761 {
2762 for( ; len != 0; len-- )
2763 mask[first_byte_idx] |= 1 << ( start_bits - len );
2764
2765 /* Avoid potential issues with offset or len becoming invalid */
2766 return;
2767 }
2768
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002769 offset += start_bits; /* Now offset % 8 == 0 */
2770 len -= start_bits;
2771
2772 for( ; start_bits != 0; start_bits-- )
2773 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2774 }
2775
2776 end_bits = len % 8;
2777 if( end_bits != 0 )
2778 {
2779 size_t last_byte_idx = ( offset + len ) / 8;
2780
2781 len -= end_bits; /* Now len % 8 == 0 */
2782
2783 for( ; end_bits != 0; end_bits-- )
2784 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2785 }
2786
2787 memset( mask + offset / 8, 0xFF, len / 8 );
2788}
2789
2790/*
2791 * Check that bitmask is full
2792 */
2793static int ssl_bitmask_check( unsigned char *mask, size_t len )
2794{
2795 size_t i;
2796
2797 for( i = 0; i < len / 8; i++ )
2798 if( mask[i] != 0xFF )
2799 return( -1 );
2800
2801 for( i = 0; i < len % 8; i++ )
2802 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2803 return( -1 );
2804
2805 return( 0 );
2806}
2807
2808/*
2809 * Reassemble fragmented DTLS handshake messages.
2810 *
2811 * Use a temporary buffer for reassembly, divided in two parts:
2812 * - the first holds the reassembled message (including handshake header),
2813 * - the second holds a bitmask indicating which parts of the message
2814 * (excluding headers) have been received so far.
2815 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002816static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2817{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002818 unsigned char *msg, *bitmask;
2819 size_t frag_len, frag_off;
2820 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2821
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002822 if( ssl->handshake == NULL )
2823 {
2824 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2825 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2826 }
2827
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002828 /*
2829 * For first fragment, check size and allocate buffer
2830 */
2831 if( ssl->handshake->hs_msg == NULL )
2832 {
2833 size_t alloc_len;
2834
2835 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2836 msg_len ) );
2837
2838 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2839 {
2840 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2841 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2842 }
2843
2844 /* The bitmask needs one bit per byte of message excluding header */
2845 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2846
2847 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2848 if( ssl->handshake->hs_msg == NULL )
2849 {
2850 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2851 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2852 }
2853
2854 memset( ssl->handshake->hs_msg, 0, alloc_len );
2855
2856 /* Prepare final header: copy msg_type, length and message_seq,
2857 * then add standardised fragment_offset and fragment_length */
2858 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2859 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2860 memcpy( ssl->handshake->hs_msg + 9,
2861 ssl->handshake->hs_msg + 1, 3 );
2862 }
2863 else
2864 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002865 /* Make sure msg_type and length are consistent */
2866 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002867 {
2868 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2869 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2870 }
2871 }
2872
2873 msg = ssl->handshake->hs_msg + 12;
2874 bitmask = msg + msg_len;
2875
2876 /*
2877 * Check and copy current fragment
2878 */
2879 frag_off = ( ssl->in_msg[6] << 16 ) |
2880 ( ssl->in_msg[7] << 8 ) |
2881 ssl->in_msg[8];
2882 frag_len = ( ssl->in_msg[9] << 16 ) |
2883 ( ssl->in_msg[10] << 8 ) |
2884 ssl->in_msg[11];
2885
2886 if( frag_off + frag_len > msg_len )
2887 {
2888 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2889 frag_off, frag_len, msg_len ) );
2890 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2891 }
2892
2893 if( frag_len + 12 > ssl->in_msglen )
2894 {
2895 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2896 frag_len, ssl->in_msglen ) );
2897 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2898 }
2899
2900 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2901 frag_off, frag_len ) );
2902
2903 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2904 ssl_bitmask_set( bitmask, frag_off, frag_len );
2905
2906 /*
2907 * Do we have the complete message by now?
2908 * If yes, finalize it, else ask to read the next record.
2909 */
2910 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2911 {
2912 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2913 return( POLARSSL_ERR_NET_WANT_READ );
2914 }
2915
2916 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2917
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002918 if( frag_len + 12 < ssl->in_msglen )
2919 {
2920 /*
2921 * We'got more handshake messages in the same record.
2922 * This case is not handled now because no know implementation does
2923 * that and it's hard to test, so we prefer to fail cleanly for now.
2924 */
2925 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2926 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2927 }
2928
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002929 if( ssl->in_left > ssl->next_record_offset )
2930 {
2931 /*
2932 * We've got more data in the buffer after the current record,
2933 * that we don't want to overwrite. Move it before writing the
2934 * reassembled message, and adjust in_left and next_record_offset.
2935 */
2936 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2937 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2938 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2939
2940 /* First compute and check new lengths */
2941 ssl->next_record_offset = new_remain - ssl->in_hdr;
2942 ssl->in_left = ssl->next_record_offset + remain_len;
2943
2944 if( ssl->in_left > SSL_BUFFER_LEN -
2945 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2946 {
2947 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2948 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2949 }
2950
2951 memmove( new_remain, cur_remain, remain_len );
2952 }
2953
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002954 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2955
2956 polarssl_free( ssl->handshake->hs_msg );
2957 ssl->handshake->hs_msg = NULL;
2958
2959 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2960 ssl->in_msg, ssl->in_hslen );
2961
2962 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002963}
2964#endif /* POLARSSL_SSL_PROTO_DTLS */
2965
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002966static int ssl_prepare_handshake_record( ssl_context *ssl )
2967{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002968 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2969 {
2970 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2971 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002972 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002973 }
2974
2975 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2976 ( ssl->in_msg[1] << 16 ) |
2977 ( ssl->in_msg[2] << 8 ) |
2978 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002979
2980 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2981 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002982 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002983
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002984#if defined(POLARSSL_SSL_PROTO_DTLS)
2985 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002986 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002987 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002988 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002989
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002990 /* ssl->handshake is NULL when receiving ClientHello for renego */
2991 if( ssl->handshake != NULL &&
2992 recv_msg_seq != ssl->handshake->in_msg_seq )
2993 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002994 /* Retransmit only on last message from previous flight, to avoid
2995 * too many retransmissions.
2996 * Besides, No sane server ever retransmits HelloVerifyRequest */
2997 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002998 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002999 {
3000 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3001 "message_seq = %d, start_of_flight = %d",
3002 recv_msg_seq,
3003 ssl->handshake->in_flight_start_seq ) );
3004
3005 if( ( ret = ssl_resend( ssl ) ) != 0 )
3006 {
3007 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3008 return( ret );
3009 }
3010 }
3011 else
3012 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003013 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003014 "message_seq = %d, expected = %d",
3015 recv_msg_seq,
3016 ssl->handshake->in_msg_seq ) );
3017 }
3018
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003019 return( POLARSSL_ERR_NET_WANT_READ );
3020 }
3021 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003022
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003023 /* Reassemble if current message is fragmented or reassembly is
3024 * already in progress */
3025 if( ssl->in_msglen < ssl->in_hslen ||
3026 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3027 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3028 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003029 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003030 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3031
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003032 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3033 {
3034 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3035 return( ret );
3036 }
3037 }
3038 }
3039 else
3040#endif /* POLARSSL_SSL_PROTO_DTLS */
3041 /* With TLS we don't handle fragmentation (for now) */
3042 if( ssl->in_msglen < ssl->in_hslen )
3043 {
3044 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003045 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003046 }
3047
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003048 if( ssl->state != SSL_HANDSHAKE_OVER )
3049 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3050
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003051 /* Handshake message is complete, increment counter */
3052#if defined(POLARSSL_SSL_PROTO_DTLS)
3053 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3054 ssl->handshake != NULL )
3055 {
3056 ssl->handshake->in_msg_seq++;
3057 }
3058#endif
3059
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003060 return( 0 );
3061}
3062
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003063/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003064 * DTLS anti-replay: RFC 6347 4.1.2.6
3065 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003066 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3067 * Bit n is set iff record number in_window_top - n has been seen.
3068 *
3069 * Usually, in_window_top is the last record number seen and the lsb of
3070 * in_window is set. The only exception is the initial state (record number 0
3071 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003072 */
3073#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3074static void ssl_dtls_replay_reset( ssl_context *ssl )
3075{
3076 ssl->in_window_top = 0;
3077 ssl->in_window = 0;
3078}
3079
3080static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3081{
3082 return( ( (uint64_t) buf[0] << 40 ) |
3083 ( (uint64_t) buf[1] << 32 ) |
3084 ( (uint64_t) buf[2] << 24 ) |
3085 ( (uint64_t) buf[3] << 16 ) |
3086 ( (uint64_t) buf[4] << 8 ) |
3087 ( (uint64_t) buf[5] ) );
3088}
3089
3090/*
3091 * Return 0 if sequence number is acceptable, -1 otherwise
3092 */
3093int ssl_dtls_replay_check( ssl_context *ssl )
3094{
3095 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3096 uint64_t bit;
3097
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003098 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3099 return( 0 );
3100
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003101 if( rec_seqnum > ssl->in_window_top )
3102 return( 0 );
3103
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003104 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003105
3106 if( bit >= 64 )
3107 return( -1 );
3108
3109 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3110 return( -1 );
3111
3112 return( 0 );
3113}
3114
3115/*
3116 * Update replay window on new validated record
3117 */
3118void ssl_dtls_replay_update( ssl_context *ssl )
3119{
3120 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3121
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003122 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3123 return;
3124
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003125 if( rec_seqnum > ssl->in_window_top )
3126 {
3127 /* Update window_top and the contents of the window */
3128 uint64_t shift = rec_seqnum - ssl->in_window_top;
3129
3130 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003131 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003132 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003133 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003134 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003135 ssl->in_window |= 1;
3136 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003137
3138 ssl->in_window_top = rec_seqnum;
3139 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003140 else
3141 {
3142 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003143 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003144
3145 if( bit < 64 ) /* Always true, but be extra sure */
3146 ssl->in_window |= (uint64_t) 1 << bit;
3147 }
3148}
3149#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3150
3151/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003152 * ContentType type;
3153 * ProtocolVersion version;
3154 * uint16 epoch; // DTLS only
3155 * uint48 sequence_number; // DTLS only
3156 * uint16 length;
3157 */
3158static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003159{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003160 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003161 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003162
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003163 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3164
Paul Bakker5121ce52009-01-03 21:22:43 +00003165 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003166 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003167 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
3169 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3170 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003171 ssl->in_msgtype,
3172 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003174 /* Check record type */
3175 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3176 ssl->in_msgtype != SSL_MSG_ALERT &&
3177 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3178 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3179 {
3180 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003181
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003182 if( ( ret = ssl_send_alert_message( ssl,
3183 SSL_ALERT_LEVEL_FATAL,
3184 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3185 {
3186 return( ret );
3187 }
3188
3189 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3190 }
3191
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003192#if defined(POLARSSL_SSL_PROTO_DTLS)
3193 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3194 {
3195 /* Drop unexpected ChangeCipherSpec messages */
3196 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3197 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3198 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3199 {
3200 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3201 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3202 }
3203
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003204 /* Drop unexpected ApplicationData records,
3205 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003206 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003207 ssl->state != SSL_HANDSHAKE_OVER &&
3208 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
3209 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003210 {
3211 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3212 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3213 }
3214 }
3215#endif
3216
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003217 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003218 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 {
3220 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003221 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 }
3223
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003224 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 {
3226 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003227 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 }
3229
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003230 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003231#if defined(POLARSSL_SSL_PROTO_DTLS)
3232 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3233 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003234 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003235
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003236 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003237 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003238 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003239 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003240 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003241 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003242 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003243
3244#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3245 if( ssl_dtls_replay_check( ssl ) != 0 )
3246 {
3247 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3248 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3249 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003250#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003251 }
3252#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003253
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003254 /* Check length against the size of our buffer */
3255 if( ssl->in_msglen > SSL_BUFFER_LEN
3256 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003257 {
3258 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3259 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3260 }
3261
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003262 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003263 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003265 if( ssl->in_msglen < 1 ||
3266 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 {
3268 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003269 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
3271 }
3272 else
3273 {
Paul Bakker48916f92012-09-16 19:57:18 +00003274 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 {
3276 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003277 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 }
3279
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003280#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003282 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 {
3284 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003285 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003287#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003288#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3289 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 /*
3291 * TLS encrypted messages can have up to 256 bytes of padding
3292 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003293 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003294 ssl->in_msglen > ssl->transform_in->minlen +
3295 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 {
3297 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003298 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003300#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 }
3302
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003303 return( 0 );
3304}
Paul Bakker5121ce52009-01-03 21:22:43 +00003305
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003306/*
3307 * If applicable, decrypt (and decompress) record content
3308 */
3309static int ssl_prepare_record_content( ssl_context *ssl )
3310{
3311 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003312
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003314 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
Paul Bakker05ef8352012-05-08 09:17:57 +00003316#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003317 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003318 {
3319 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3320
3321 ret = ssl_hw_record_read( ssl );
3322 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3323 {
3324 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003325 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003326 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003327
3328 if( ret == 0 )
3329 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003330 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003331#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003332 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 {
3334 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3335 {
3336 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3337 return( ret );
3338 }
3339
3340 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3341 ssl->in_msg, ssl->in_msglen );
3342
3343 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3344 {
3345 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003346 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 }
3348 }
3349
Paul Bakker2770fbd2012-07-03 13:30:23 +00003350#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003351 if( ssl->transform_in != NULL &&
3352 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353 {
3354 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3355 {
3356 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3357 return( ret );
3358 }
3359
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003360 // TODO: what's the purpose of these lines? is in_len used?
3361 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3362 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003363 }
3364#endif /* POLARSSL_ZLIB_SUPPORT */
3365
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003366#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003367 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3368 {
3369 ssl_dtls_replay_update( ssl );
3370 }
3371#endif
3372
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003373 return( 0 );
3374}
3375
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003376static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3377
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003378/*
3379 * Read a record.
3380 *
3381 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3382 * and continue reading until a valid record is found.
3383 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003384int ssl_read_record( ssl_context *ssl )
3385{
3386 int ret;
3387
3388 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3389
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003390 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003391 {
3392 /*
3393 * Get next Handshake message in the current record
3394 */
3395 ssl->in_msglen -= ssl->in_hslen;
3396
3397 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3398 ssl->in_msglen );
3399
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003400 SSL_DEBUG_BUF( 4, "remaining content in record",
3401 ssl->in_msg, ssl->in_msglen );
3402
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003403 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3404 return( ret );
3405
3406 return( 0 );
3407 }
3408
3409 ssl->in_hslen = 0;
3410
3411 /*
3412 * Read the record header and parse it
3413 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003414#if defined(POLARSSL_SSL_PROTO_DTLS)
3415read_record_header:
3416#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003417 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3418 {
3419 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3420 return( ret );
3421 }
3422
3423 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003424 {
3425#if defined(POLARSSL_SSL_PROTO_DTLS)
3426 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3427 {
3428 /* Ignore bad record and get next one; drop the whole datagram
3429 * since current header cannot be trusted to find the next record
3430 * in current datagram */
3431 ssl->next_record_offset = 0;
3432 ssl->in_left = 0;
3433
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003434 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003435 goto read_record_header;
3436 }
3437#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003438 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003439 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003440
3441 /*
3442 * Read and optionally decrypt the message contents
3443 */
3444 if( ( ret = ssl_fetch_input( ssl,
3445 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3446 {
3447 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3448 return( ret );
3449 }
3450
3451 /* Done reading this record, get ready for the next one */
3452#if defined(POLARSSL_SSL_PROTO_DTLS)
3453 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3454 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3455 else
3456#endif
3457 ssl->in_left = 0;
3458
3459 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003460 {
3461#if defined(POLARSSL_SSL_PROTO_DTLS)
3462 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3463 {
3464 /* Silently discard invalid records */
3465 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3466 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3467 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003468#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3469 if( ssl->badmac_limit != 0 &&
3470 ++ssl->badmac_seen >= ssl->badmac_limit )
3471 {
3472 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3473 return( POLARSSL_ERR_SSL_INVALID_MAC );
3474 }
3475#endif
3476
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003477 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003478 goto read_record_header;
3479 }
3480
3481 return( ret );
3482 }
3483 else
3484#endif
3485 {
3486 /* Error out (and send alert) on invalid records */
3487#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3488 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3489 {
3490 ssl_send_alert_message( ssl,
3491 SSL_ALERT_LEVEL_FATAL,
3492 SSL_ALERT_MSG_BAD_RECORD_MAC );
3493 }
3494#endif
3495 return( ret );
3496 }
3497 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003498
3499 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003500 * When we sent the last flight of the handshake, we MUST respond to a
3501 * retransmit of the peer's previous flight with a retransmit. (In
3502 * practice, only the Finished message will make it, other messages
3503 * including CCS use the old transform so they're dropped as invalid.)
3504 *
3505 * If the record we received is not a handshake message, however, it
3506 * means the peer received our last flight so we can clean up
3507 * handshake info.
3508 *
3509 * This check needs to be done before prepare_handshake() due to an edge
3510 * case: if the client immediately requests renegotiation, this
3511 * finishes the current handshake first, avoiding the new ClientHello
3512 * being mistaken for an ancient message in the current handshake.
3513 */
3514#if defined(POLARSSL_SSL_PROTO_DTLS)
3515 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3516 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003517 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003518 {
3519 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3520 ssl->in_msg[0] == SSL_HS_FINISHED )
3521 {
3522 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3523
3524 if( ( ret = ssl_resend( ssl ) ) != 0 )
3525 {
3526 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3527 return( ret );
3528 }
3529
3530 return( POLARSSL_ERR_NET_WANT_READ );
3531 }
3532 else
3533 {
3534 ssl_handshake_wrapup_free_hs_transform( ssl );
3535 }
3536 }
3537#endif
3538
3539 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003540 * Handle particular types of records
3541 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3543 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003544 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3545 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 }
3547
3548 if( ssl->in_msgtype == SSL_MSG_ALERT )
3549 {
3550 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3551 ssl->in_msg[0], ssl->in_msg[1] ) );
3552
3553 /*
3554 * Ignore non-fatal alerts, except close_notify
3555 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003556 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003558 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3559 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003560 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 }
3562
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003563 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3564 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 {
3566 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003567 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003568 }
3569 }
3570
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3572
3573 return( 0 );
3574}
3575
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003576int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3577{
3578 int ret;
3579
3580 if( ( ret = ssl_send_alert_message( ssl,
3581 SSL_ALERT_LEVEL_FATAL,
3582 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3583 {
3584 return( ret );
3585 }
3586
3587 return( 0 );
3588}
3589
Paul Bakker0a925182012-04-16 06:46:41 +00003590int ssl_send_alert_message( ssl_context *ssl,
3591 unsigned char level,
3592 unsigned char message )
3593{
3594 int ret;
3595
3596 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3597
3598 ssl->out_msgtype = SSL_MSG_ALERT;
3599 ssl->out_msglen = 2;
3600 ssl->out_msg[0] = level;
3601 ssl->out_msg[1] = message;
3602
3603 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3604 {
3605 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3606 return( ret );
3607 }
3608
3609 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3610
3611 return( 0 );
3612}
3613
Paul Bakker5121ce52009-01-03 21:22:43 +00003614/*
3615 * Handshake functions
3616 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003617#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3618 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3619 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3620 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3621 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3622 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3623 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003624int ssl_write_certificate( ssl_context *ssl )
3625{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003626 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
3628 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3629
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003630 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003631 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3632 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003633 {
3634 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3635 ssl->state++;
3636 return( 0 );
3637 }
3638
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003639 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3640 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003641}
3642
3643int ssl_parse_certificate( ssl_context *ssl )
3644{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003645 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3646
3647 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3648
3649 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003650 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003652 {
3653 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3654 ssl->state++;
3655 return( 0 );
3656 }
3657
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003660}
3661#else
3662int ssl_write_certificate( ssl_context *ssl )
3663{
3664 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3665 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003666 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003667 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3668
3669 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3670
3671 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003672 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3673 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003674 {
3675 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3676 ssl->state++;
3677 return( 0 );
3678 }
3679
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003680#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 if( ssl->endpoint == SSL_IS_CLIENT )
3682 {
3683 if( ssl->client_auth == 0 )
3684 {
3685 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3686 ssl->state++;
3687 return( 0 );
3688 }
3689
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003690#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 /*
3692 * If using SSLv3 and got no cert, send an Alert message
3693 * (otherwise an empty Certificate message will be sent).
3694 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003695 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3697 {
3698 ssl->out_msglen = 2;
3699 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003700 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3701 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
3703 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3704 goto write_msg;
3705 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003706#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003708#endif /* POLARSSL_SSL_CLI_C */
3709#if defined(POLARSSL_SSL_SRV_C)
3710 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003712 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003713 {
3714 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003715 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 }
3717 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003718#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003721
3722 /*
3723 * 0 . 0 handshake type
3724 * 1 . 3 handshake length
3725 * 4 . 6 length of all certs
3726 * 7 . 9 length of cert. 1
3727 * 10 . n-1 peer certificate
3728 * n . n+2 length of cert. 2
3729 * n+3 . ... upper level cert, etc.
3730 */
3731 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003732 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733
Paul Bakker29087132010-03-21 21:03:34 +00003734 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 {
3736 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003737 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 {
3739 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3740 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003741 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003742 }
3743
3744 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3745 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3746 ssl->out_msg[i + 2] = (unsigned char)( n );
3747
3748 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3749 i += n; crt = crt->next;
3750 }
3751
3752 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3753 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3754 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3755
3756 ssl->out_msglen = i;
3757 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3758 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3759
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003760#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003761write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003762#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003763
3764 ssl->state++;
3765
3766 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3767 {
3768 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3769 return( ret );
3770 }
3771
3772 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3773
Paul Bakkered27a042013-04-18 22:46:23 +02003774 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003775}
3776
3777int ssl_parse_certificate( ssl_context *ssl )
3778{
Paul Bakkered27a042013-04-18 22:46:23 +02003779 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003780 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003781 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
3783 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3784
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003785 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003786 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3787 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003788 {
3789 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3790 ssl->state++;
3791 return( 0 );
3792 }
3793
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003794#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003796 ( ssl->authmode == SSL_VERIFY_NONE ||
3797 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003799 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3801 ssl->state++;
3802 return( 0 );
3803 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003804#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003805
3806 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3807 {
3808 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3809 return( ret );
3810 }
3811
3812 ssl->state++;
3813
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003814#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003815#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 /*
3817 * Check if the client sent an empty certificate
3818 */
3819 if( ssl->endpoint == SSL_IS_SERVER &&
3820 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3821 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003822 if( ssl->in_msglen == 2 &&
3823 ssl->in_msgtype == SSL_MSG_ALERT &&
3824 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3825 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 {
3827 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3828
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003829 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3831 return( 0 );
3832 else
Paul Bakker40e46942009-01-03 21:51:57 +00003833 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 }
3835 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003836#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003837
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003838#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3839 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003840 if( ssl->endpoint == SSL_IS_SERVER &&
3841 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3842 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003843 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3845 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003846 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 {
3848 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3849
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003850 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003852 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003853 else
3854 return( 0 );
3855 }
3856 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003857#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3858 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003859#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003860
3861 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3862 {
3863 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003864 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 }
3866
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003867 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3868 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003869 {
3870 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003871 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 }
3873
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003874 i = ssl_hs_hdr_len( ssl );
3875
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 /*
3877 * Same message structure as in ssl_write_certificate()
3878 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003879 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003881 if( ssl->in_msg[i] != 0 ||
3882 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003883 {
3884 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003885 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 }
3887
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003888 /* In case we tried to reuse a session but it failed */
3889 if( ssl->session_negotiate->peer_cert != NULL )
3890 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003891 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003892 polarssl_free( ssl->session_negotiate->peer_cert );
3893 }
3894
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003895 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3896 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003897 {
3898 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003899 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003900 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 }
3902
Paul Bakkerb6b09562013-09-18 14:17:41 +02003903 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003904
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003905 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003906
3907 while( i < ssl->in_hslen )
3908 {
3909 if( ssl->in_msg[i] != 0 )
3910 {
3911 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003912 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913 }
3914
3915 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3916 | (unsigned int) ssl->in_msg[i + 2];
3917 i += 3;
3918
3919 if( n < 128 || i + n > ssl->in_hslen )
3920 {
3921 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003922 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003923 }
3924
Paul Bakkerddf26b42013-09-18 13:46:23 +02003925 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3926 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 if( ret != 0 )
3928 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003929 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003930 return( ret );
3931 }
3932
3933 i += n;
3934 }
3935
Paul Bakker48916f92012-09-16 19:57:18 +00003936 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003938 /*
3939 * On client, make sure the server cert doesn't change during renego to
3940 * avoid "triple handshake" attack: https://secure-resumption.com/
3941 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003942#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003943 if( ssl->endpoint == SSL_IS_CLIENT &&
3944 ssl->renegotiation == SSL_RENEGOTIATION )
3945 {
3946 if( ssl->session->peer_cert == NULL )
3947 {
3948 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3949 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3950 }
3951
3952 if( ssl->session->peer_cert->raw.len !=
3953 ssl->session_negotiate->peer_cert->raw.len ||
3954 memcmp( ssl->session->peer_cert->raw.p,
3955 ssl->session_negotiate->peer_cert->raw.p,
3956 ssl->session->peer_cert->raw.len ) != 0 )
3957 {
3958 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3959 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3960 }
3961 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003962#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003963
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 if( ssl->authmode != SSL_VERIFY_NONE )
3965 {
3966 if( ssl->ca_chain == NULL )
3967 {
3968 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003969 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003970 }
3971
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003972 /*
3973 * Main check: verify certificate
3974 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003975 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3976 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3977 &ssl->session_negotiate->verify_result,
3978 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
3980 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003981 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003983 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003984
3985 /*
3986 * Secondary checks: always done, but change 'ret' only if it was 0
3987 */
3988
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003989#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003990 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003991 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003992
3993 /* If certificate uses an EC key, make sure the curve is OK */
3994 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3995 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3996 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003997 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003998 if( ret == 0 )
3999 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004000 }
4001 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004002#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004004 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4005 ciphersuite_info,
4006 ! ssl->endpoint ) != 0 )
4007 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004008 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004009 if( ret == 0 )
4010 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4011 }
4012
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4014 ret = 0;
4015 }
4016
4017 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4018
4019 return( ret );
4020}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004021#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4022 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4023 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4024 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4025 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4026 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4027 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
4029int ssl_write_change_cipher_spec( ssl_context *ssl )
4030{
4031 int ret;
4032
4033 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4034
4035 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4036 ssl->out_msglen = 1;
4037 ssl->out_msg[0] = 1;
4038
Paul Bakker5121ce52009-01-03 21:22:43 +00004039 ssl->state++;
4040
4041 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4042 {
4043 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4044 return( ret );
4045 }
4046
4047 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4048
4049 return( 0 );
4050}
4051
4052int ssl_parse_change_cipher_spec( ssl_context *ssl )
4053{
4054 int ret;
4055
4056 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4057
Paul Bakker5121ce52009-01-03 21:22:43 +00004058 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4059 {
4060 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4061 return( ret );
4062 }
4063
4064 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4065 {
4066 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004067 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004068 }
4069
4070 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4071 {
4072 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004073 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074 }
4075
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004076 /*
4077 * Switch to our negotiated transform and session parameters for inbound
4078 * data.
4079 */
4080 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4081 ssl->transform_in = ssl->transform_negotiate;
4082 ssl->session_in = ssl->session_negotiate;
4083
4084#if defined(POLARSSL_SSL_PROTO_DTLS)
4085 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4086 {
4087#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4088 ssl_dtls_replay_reset( ssl );
4089#endif
4090
4091 /* Increment epoch */
4092 if( ++ssl->in_epoch == 0 )
4093 {
4094 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4095 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4096 }
4097 }
4098 else
4099#endif /* POLARSSL_SSL_PROTO_DTLS */
4100 memset( ssl->in_ctr, 0, 8 );
4101
4102 /*
4103 * Set the in_msg pointer to the correct location based on IV length
4104 */
4105 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4106 {
4107 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4108 ssl->transform_negotiate->fixed_ivlen;
4109 }
4110 else
4111 ssl->in_msg = ssl->in_iv;
4112
4113#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4114 if( ssl_hw_record_activate != NULL )
4115 {
4116 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4117 {
4118 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4119 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4120 }
4121 }
4122#endif
4123
Paul Bakker5121ce52009-01-03 21:22:43 +00004124 ssl->state++;
4125
4126 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4127
4128 return( 0 );
4129}
4130
Paul Bakker41c83d32013-03-20 14:39:14 +01004131void ssl_optimize_checksum( ssl_context *ssl,
4132 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004133{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004134 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004135
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004136#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4137 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004138 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004139 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004140 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004141#endif
4142#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4143#if defined(POLARSSL_SHA512_C)
4144 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4145 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4146 else
4147#endif
4148#if defined(POLARSSL_SHA256_C)
4149 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004150 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004151 else
4152#endif
4153#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004154 {
4155 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004156 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004157 }
Paul Bakker380da532012-04-18 16:10:25 +00004158}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004159
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004160void ssl_reset_checksum( ssl_context *ssl )
4161{
4162#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4163 defined(POLARSSL_SSL_PROTO_TLS1_1)
4164 md5_starts( &ssl->handshake->fin_md5 );
4165 sha1_starts( &ssl->handshake->fin_sha1 );
4166#endif
4167#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4168#if defined(POLARSSL_SHA256_C)
4169 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4170#endif
4171#if defined(POLARSSL_SHA512_C)
4172 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4173#endif
4174#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4175}
4176
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004177static void ssl_update_checksum_start( ssl_context *ssl,
4178 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004179{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004180#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4181 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004182 md5_update( &ssl->handshake->fin_md5 , buf, len );
4183 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004184#endif
4185#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4186#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004187 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004188#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004189#if defined(POLARSSL_SHA512_C)
4190 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004191#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004192#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004193}
4194
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004195#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4196 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004197static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4198 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004199{
Paul Bakker48916f92012-09-16 19:57:18 +00004200 md5_update( &ssl->handshake->fin_md5 , buf, len );
4201 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004202}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004203#endif
Paul Bakker380da532012-04-18 16:10:25 +00004204
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004205#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4206#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004207static void ssl_update_checksum_sha256( ssl_context *ssl,
4208 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004209{
Paul Bakker9e36f042013-06-30 14:34:05 +02004210 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004211}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004212#endif
Paul Bakker380da532012-04-18 16:10:25 +00004213
Paul Bakker9e36f042013-06-30 14:34:05 +02004214#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004215static void ssl_update_checksum_sha384( ssl_context *ssl,
4216 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004217{
Paul Bakker9e36f042013-06-30 14:34:05 +02004218 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004219}
Paul Bakker769075d2012-11-24 11:26:46 +01004220#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004221#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004222
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004223#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004224static void ssl_calc_finished_ssl(
4225 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004226{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004227 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004228 md5_context md5;
4229 sha1_context sha1;
4230
Paul Bakker5121ce52009-01-03 21:22:43 +00004231 unsigned char padbuf[48];
4232 unsigned char md5sum[16];
4233 unsigned char sha1sum[20];
4234
Paul Bakker48916f92012-09-16 19:57:18 +00004235 ssl_session *session = ssl->session_negotiate;
4236 if( !session )
4237 session = ssl->session;
4238
Paul Bakker1ef83d62012-04-11 12:09:53 +00004239 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4240
Paul Bakker48916f92012-09-16 19:57:18 +00004241 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4242 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004243
4244 /*
4245 * SSLv3:
4246 * hash =
4247 * MD5( master + pad2 +
4248 * MD5( handshake + sender + master + pad1 ) )
4249 * + SHA1( master + pad2 +
4250 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004251 */
4252
Paul Bakker90995b52013-06-24 19:20:35 +02004253#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004255 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004256#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004257
Paul Bakker90995b52013-06-24 19:20:35 +02004258#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004259 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004260 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004262
Paul Bakker3c2122f2013-06-24 19:03:14 +02004263 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4264 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004265
Paul Bakker1ef83d62012-04-11 12:09:53 +00004266 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004267
Paul Bakker3c2122f2013-06-24 19:03:14 +02004268 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004269 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004270 md5_update( &md5, padbuf, 48 );
4271 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
Paul Bakker3c2122f2013-06-24 19:03:14 +02004273 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004274 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004275 sha1_update( &sha1, padbuf, 40 );
4276 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004277
Paul Bakker1ef83d62012-04-11 12:09:53 +00004278 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
Paul Bakker1ef83d62012-04-11 12:09:53 +00004280 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004281 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282 md5_update( &md5, padbuf, 48 );
4283 md5_update( &md5, md5sum, 16 );
4284 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
Paul Bakker1ef83d62012-04-11 12:09:53 +00004286 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004287 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004288 sha1_update( &sha1, padbuf , 40 );
4289 sha1_update( &sha1, sha1sum, 20 );
4290 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004291
Paul Bakker1ef83d62012-04-11 12:09:53 +00004292 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004293
Paul Bakker5b4af392014-06-26 12:09:34 +02004294 md5_free( &md5 );
4295 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296
Paul Bakker34617722014-06-13 17:20:13 +02004297 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4298 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4299 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
4301 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4302}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004303#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004305#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004306static void ssl_calc_finished_tls(
4307 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004308{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004309 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004310 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004311 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004312 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004313 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004314
Paul Bakker48916f92012-09-16 19:57:18 +00004315 ssl_session *session = ssl->session_negotiate;
4316 if( !session )
4317 session = ssl->session;
4318
Paul Bakker1ef83d62012-04-11 12:09:53 +00004319 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004320
Paul Bakker48916f92012-09-16 19:57:18 +00004321 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4322 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Paul Bakker1ef83d62012-04-11 12:09:53 +00004324 /*
4325 * TLSv1:
4326 * hash = PRF( master, finished_label,
4327 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4328 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004329
Paul Bakker90995b52013-06-24 19:20:35 +02004330#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004331 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4332 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004333#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004334
Paul Bakker90995b52013-06-24 19:20:35 +02004335#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004336 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4337 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004338#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004339
4340 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004341 ? "client finished"
4342 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004343
4344 md5_finish( &md5, padbuf );
4345 sha1_finish( &sha1, padbuf + 16 );
4346
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004347 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004348 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004349
4350 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4351
Paul Bakker5b4af392014-06-26 12:09:34 +02004352 md5_free( &md5 );
4353 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004354
Paul Bakker34617722014-06-13 17:20:13 +02004355 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004356
4357 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4358}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004359#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004360
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004361#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4362#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004363static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004364 ssl_context *ssl, unsigned char *buf, int from )
4365{
4366 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004367 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004368 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004369 unsigned char padbuf[32];
4370
Paul Bakker48916f92012-09-16 19:57:18 +00004371 ssl_session *session = ssl->session_negotiate;
4372 if( !session )
4373 session = ssl->session;
4374
Paul Bakker380da532012-04-18 16:10:25 +00004375 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004376
Paul Bakker9e36f042013-06-30 14:34:05 +02004377 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004378
4379 /*
4380 * TLSv1.2:
4381 * hash = PRF( master, finished_label,
4382 * Hash( handshake ) )[0.11]
4383 */
4384
Paul Bakker9e36f042013-06-30 14:34:05 +02004385#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004386 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004387 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004388#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004389
4390 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004391 ? "client finished"
4392 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004393
Paul Bakker9e36f042013-06-30 14:34:05 +02004394 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004395
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004396 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004397 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004398
4399 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4400
Paul Bakker5b4af392014-06-26 12:09:34 +02004401 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004402
Paul Bakker34617722014-06-13 17:20:13 +02004403 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004404
4405 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4406}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004407#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004408
Paul Bakker9e36f042013-06-30 14:34:05 +02004409#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004410static void ssl_calc_finished_tls_sha384(
4411 ssl_context *ssl, unsigned char *buf, int from )
4412{
4413 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004414 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004415 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004416 unsigned char padbuf[48];
4417
Paul Bakker48916f92012-09-16 19:57:18 +00004418 ssl_session *session = ssl->session_negotiate;
4419 if( !session )
4420 session = ssl->session;
4421
Paul Bakker380da532012-04-18 16:10:25 +00004422 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004423
Paul Bakker9e36f042013-06-30 14:34:05 +02004424 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004425
4426 /*
4427 * TLSv1.2:
4428 * hash = PRF( master, finished_label,
4429 * Hash( handshake ) )[0.11]
4430 */
4431
Paul Bakker9e36f042013-06-30 14:34:05 +02004432#if !defined(POLARSSL_SHA512_ALT)
4433 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4434 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004435#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004436
4437 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004438 ? "client finished"
4439 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004440
Paul Bakker9e36f042013-06-30 14:34:05 +02004441 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004442
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004443 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004444 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004445
4446 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4447
Paul Bakker5b4af392014-06-26 12:09:34 +02004448 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004449
Paul Bakker34617722014-06-13 17:20:13 +02004450 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004451
4452 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4453}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004454#endif /* POLARSSL_SHA512_C */
4455#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004456
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004457static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004458{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004459 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004460
4461 /*
4462 * Free our handshake params
4463 */
4464 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004465 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004466 ssl->handshake = NULL;
4467
4468 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004469 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004470 */
4471 if( ssl->transform )
4472 {
4473 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004474 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004475 }
4476 ssl->transform = ssl->transform_negotiate;
4477 ssl->transform_negotiate = NULL;
4478
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004479 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4480}
4481
4482void ssl_handshake_wrapup( ssl_context *ssl )
4483{
4484 int resume = ssl->handshake->resume;
4485
4486 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4487
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004488#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004489 if( ssl->renegotiation == SSL_RENEGOTIATION )
4490 {
4491 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4492 ssl->renego_records_seen = 0;
4493 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004494#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004495
4496 /*
4497 * Free the previous session and switch in the current one
4498 */
Paul Bakker0a597072012-09-25 21:55:46 +00004499 if( ssl->session )
4500 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004501#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4502 /* RFC 7366 3.1: keep the EtM state */
4503 ssl->session_negotiate->encrypt_then_mac =
4504 ssl->session->encrypt_then_mac;
4505#endif
4506
Paul Bakker0a597072012-09-25 21:55:46 +00004507 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004508 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004509 }
4510 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004511 ssl->session_negotiate = NULL;
4512
Paul Bakker0a597072012-09-25 21:55:46 +00004513 /*
4514 * Add cache entry
4515 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004516 if( ssl->f_set_cache != NULL &&
4517 ssl->session->length != 0 &&
4518 resume == 0 )
4519 {
Paul Bakker0a597072012-09-25 21:55:46 +00004520 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4521 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004522 }
Paul Bakker0a597072012-09-25 21:55:46 +00004523
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004524#if defined(POLARSSL_SSL_PROTO_DTLS)
4525 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4526 ssl->handshake->flight != NULL )
4527 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004528 /* Cancel handshake timer */
4529 ssl_set_timer( ssl, 0 );
4530
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004531 /* Keep last flight around in case we need to resend it:
4532 * we need the handshake and transform structures for that */
4533 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4534 }
4535 else
4536#endif
4537 ssl_handshake_wrapup_free_hs_transform( ssl );
4538
Paul Bakker48916f92012-09-16 19:57:18 +00004539 ssl->state++;
4540
4541 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4542}
4543
Paul Bakker1ef83d62012-04-11 12:09:53 +00004544int ssl_write_finished( ssl_context *ssl )
4545{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004546 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004547
4548 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4549
Paul Bakker92be97b2013-01-02 17:30:03 +01004550 /*
4551 * Set the out_msg pointer to the correct location based on IV length
4552 */
4553 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4554 {
4555 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4556 ssl->transform_negotiate->fixed_ivlen;
4557 }
4558 else
4559 ssl->out_msg = ssl->out_iv;
4560
Paul Bakker48916f92012-09-16 19:57:18 +00004561 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004562
4563 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004564 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4565
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004566#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004567 ssl->verify_data_len = hash_len;
4568 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004569#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004570
Paul Bakker5121ce52009-01-03 21:22:43 +00004571 ssl->out_msglen = 4 + hash_len;
4572 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4573 ssl->out_msg[0] = SSL_HS_FINISHED;
4574
4575 /*
4576 * In case of session resuming, invert the client and server
4577 * ChangeCipherSpec messages order.
4578 */
Paul Bakker0a597072012-09-25 21:55:46 +00004579 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004580 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004581#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004583 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004584#endif
4585#if defined(POLARSSL_SSL_SRV_C)
4586 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004589 }
4590 else
4591 ssl->state++;
4592
Paul Bakker48916f92012-09-16 19:57:18 +00004593 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004594 * Switch to our negotiated transform and session parameters for outbound
4595 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004596 */
4597 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004598
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004599#if defined(POLARSSL_SSL_PROTO_DTLS)
4600 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4601 {
4602 unsigned char i;
4603
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004604 /* Remember current epoch settings for resending */
4605 ssl->handshake->alt_transform_out = ssl->transform_out;
4606 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4607
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004608 /* Set sequence_number to zero */
4609 memset( ssl->out_ctr + 2, 0, 6 );
4610
4611 /* Increment epoch */
4612 for( i = 2; i > 0; i-- )
4613 if( ++ssl->out_ctr[i - 1] != 0 )
4614 break;
4615
4616 /* The loop goes to its end iff the counter is wrapping */
4617 if( i == 0 )
4618 {
4619 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4620 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4621 }
4622 }
4623 else
4624#endif /* POLARSSL_SSL_PROTO_DTLS */
4625 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004626
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004627 ssl->transform_out = ssl->transform_negotiate;
4628 ssl->session_out = ssl->session_negotiate;
4629
Paul Bakker07eb38b2012-12-19 14:42:06 +01004630#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004631 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004632 {
4633 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4634 {
4635 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4636 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4637 }
4638 }
4639#endif
4640
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004641#if defined(POLARSSL_SSL_PROTO_DTLS)
4642 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4643 ssl_send_flight_completed( ssl );
4644#endif
4645
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4647 {
4648 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4649 return( ret );
4650 }
4651
4652 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4653
4654 return( 0 );
4655}
4656
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004657#if defined(POLARSSL_SSL_PROTO_SSL3)
4658#define SSL_MAX_HASH_LEN 36
4659#else
4660#define SSL_MAX_HASH_LEN 12
4661#endif
4662
Paul Bakker5121ce52009-01-03 21:22:43 +00004663int ssl_parse_finished( ssl_context *ssl )
4664{
Paul Bakker23986e52011-04-24 08:57:21 +00004665 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004666 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004667 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004668
4669 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4670
Paul Bakker48916f92012-09-16 19:57:18 +00004671 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004672
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4674 {
4675 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4676 return( ret );
4677 }
4678
4679 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4680 {
4681 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004682 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 }
4684
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004685 /* There is currently no ciphersuite using another length with TLS 1.2 */
4686#if defined(POLARSSL_SSL_PROTO_SSL3)
4687 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4688 hash_len = 36;
4689 else
4690#endif
4691 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004692
4693 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004694 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 {
4696 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004697 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 }
4699
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004700 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4701 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 {
4703 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004704 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 }
4706
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004707#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004708 ssl->verify_data_len = hash_len;
4709 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004710#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004711
Paul Bakker0a597072012-09-25 21:55:46 +00004712 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004713 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004714#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004715 if( ssl->endpoint == SSL_IS_CLIENT )
4716 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004717#endif
4718#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004720 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004721#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 }
4723 else
4724 ssl->state++;
4725
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004726#if defined(POLARSSL_SSL_PROTO_DTLS)
4727 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4728 ssl_recv_flight_completed( ssl );
4729#endif
4730
Paul Bakker5121ce52009-01-03 21:22:43 +00004731 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4732
4733 return( 0 );
4734}
4735
Paul Bakker968afaa2014-07-09 11:09:24 +02004736static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004737{
4738 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4739
4740#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4741 defined(POLARSSL_SSL_PROTO_TLS1_1)
4742 md5_init( &handshake->fin_md5 );
4743 sha1_init( &handshake->fin_sha1 );
4744 md5_starts( &handshake->fin_md5 );
4745 sha1_starts( &handshake->fin_sha1 );
4746#endif
4747#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4748#if defined(POLARSSL_SHA256_C)
4749 sha256_init( &handshake->fin_sha256 );
4750 sha256_starts( &handshake->fin_sha256, 0 );
4751#endif
4752#if defined(POLARSSL_SHA512_C)
4753 sha512_init( &handshake->fin_sha512 );
4754 sha512_starts( &handshake->fin_sha512, 1 );
4755#endif
4756#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4757
4758 handshake->update_checksum = ssl_update_checksum_start;
4759 handshake->sig_alg = SSL_HASH_SHA1;
4760
4761#if defined(POLARSSL_DHM_C)
4762 dhm_init( &handshake->dhm_ctx );
4763#endif
4764#if defined(POLARSSL_ECDH_C)
4765 ecdh_init( &handshake->ecdh_ctx );
4766#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004767}
4768
4769static void ssl_transform_init( ssl_transform *transform )
4770{
4771 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004772
4773 cipher_init( &transform->cipher_ctx_enc );
4774 cipher_init( &transform->cipher_ctx_dec );
4775
4776 md_init( &transform->md_ctx_enc );
4777 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004778}
4779
4780void ssl_session_init( ssl_session *session )
4781{
4782 memset( session, 0, sizeof(ssl_session) );
4783}
4784
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004785static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004786{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004787 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004788 if( ssl->transform_negotiate )
4789 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004790 if( ssl->session_negotiate )
4791 ssl_session_free( ssl->session_negotiate );
4792 if( ssl->handshake )
4793 ssl_handshake_free( ssl->handshake );
4794
4795 /*
4796 * Either the pointers are now NULL or cleared properly and can be freed.
4797 * Now allocate missing structures.
4798 */
4799 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004800 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01004801 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
4802 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004803 }
Paul Bakker48916f92012-09-16 19:57:18 +00004804
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004805 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004806 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01004807 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
4808 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004809 }
Paul Bakker48916f92012-09-16 19:57:18 +00004810
Paul Bakker82788fb2014-10-20 13:59:19 +02004811 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004812 {
4813 ssl->handshake = (ssl_handshake_params *)
4814 polarssl_malloc( sizeof(ssl_handshake_params) );
4815 }
Paul Bakker48916f92012-09-16 19:57:18 +00004816
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004817 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004818 if( ssl->handshake == NULL ||
4819 ssl->transform_negotiate == NULL ||
4820 ssl->session_negotiate == NULL )
4821 {
4822 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004823
4824 polarssl_free( ssl->handshake );
4825 polarssl_free( ssl->transform_negotiate );
4826 polarssl_free( ssl->session_negotiate );
4827
4828 ssl->handshake = NULL;
4829 ssl->transform_negotiate = NULL;
4830 ssl->session_negotiate = NULL;
4831
Paul Bakker48916f92012-09-16 19:57:18 +00004832 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4833 }
4834
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004835 /* Initialize structures */
4836 ssl_session_init( ssl->session_negotiate );
4837 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004838 ssl_handshake_params_init( ssl->handshake );
4839
4840#if defined(POLARSSL_X509_CRT_PARSE_C)
4841 ssl->handshake->key_cert = ssl->key_cert;
4842#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004843
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004844 /*
4845 * We may not know yet if we're using DTLS,
4846 * so always initiliase DTLS-specific fields.
4847 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004848#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004849 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004850
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004851 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004852 if( ssl->endpoint == SSL_IS_CLIENT )
4853 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4854 else
4855 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004856#endif
4857
Paul Bakker48916f92012-09-16 19:57:18 +00004858 return( 0 );
4859}
4860
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004861#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4862/* Dummy cookie callbacks for defaults */
4863static int ssl_cookie_write_dummy( void *ctx,
4864 unsigned char **p, unsigned char *end,
4865 const unsigned char *cli_id, size_t cli_id_len )
4866{
4867 ((void) ctx);
4868 ((void) p);
4869 ((void) end);
4870 ((void) cli_id);
4871 ((void) cli_id_len);
4872
4873 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4874}
4875
4876static int ssl_cookie_check_dummy( void *ctx,
4877 const unsigned char *cookie, size_t cookie_len,
4878 const unsigned char *cli_id, size_t cli_id_len )
4879{
4880 ((void) ctx);
4881 ((void) cookie);
4882 ((void) cookie_len);
4883 ((void) cli_id);
4884 ((void) cli_id_len);
4885
4886 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4887}
4888#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4889
Paul Bakker5121ce52009-01-03 21:22:43 +00004890/*
4891 * Initialize an SSL context
4892 */
4893int ssl_init( ssl_context *ssl )
4894{
Paul Bakker48916f92012-09-16 19:57:18 +00004895 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004896 int len = SSL_BUFFER_LEN;
4897
4898 memset( ssl, 0, sizeof( ssl_context ) );
4899
Paul Bakker62f2dee2012-09-28 07:31:51 +00004900 /*
4901 * Sane defaults
4902 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004903 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4904 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4905 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4906 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004907
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004908 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004909
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004910#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004911 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004912 memset( ssl->renego_period, 0xFF, 7 );
4913 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004914#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004915
Paul Bakker62f2dee2012-09-28 07:31:51 +00004916#if defined(POLARSSL_DHM_C)
4917 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4918 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4919 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4920 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4921 {
4922 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4923 return( ret );
4924 }
4925#endif
4926
4927 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004928 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004929 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004930 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004931 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004933 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004934 {
4935 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004936 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004937 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004938 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004939 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004940 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004941 }
4942
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004943 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4944 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004945
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004946 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4947 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4948
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004949#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4950 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4951#endif
4952
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004953#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4954 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4955#endif
4956
Paul Bakker606b4ba2013-08-14 16:52:14 +02004957#if defined(POLARSSL_SSL_SESSION_TICKETS)
4958 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4959#endif
4960
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004961#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004962 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004963#endif
4964
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004965#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4966 ssl->f_cookie_write = ssl_cookie_write_dummy;
4967 ssl->f_cookie_check = ssl_cookie_check_dummy;
4968#endif
4969
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004970#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4971 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4972#endif
4973
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004974#if defined(POLARSSL_SSL_PROTO_DTLS)
4975 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4976 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4977#endif
4978
Paul Bakker48916f92012-09-16 19:57:18 +00004979 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4980 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004981
4982 return( 0 );
4983}
4984
4985/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004986 * Reset an initialized and used SSL context for re-use while retaining
4987 * all application-set variables, function pointers and data.
4988 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004989int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004990{
Paul Bakker48916f92012-09-16 19:57:18 +00004991 int ret;
4992
Paul Bakker7eb013f2011-10-06 12:37:39 +00004993 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004994
4995#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004996 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004997 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004998
4999 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01005000 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
5001 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005002#endif
5003 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005004
Paul Bakker7eb013f2011-10-06 12:37:39 +00005005 ssl->in_offt = NULL;
5006
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005007 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005008 ssl->in_msgtype = 0;
5009 ssl->in_msglen = 0;
5010 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005011#if defined(POLARSSL_SSL_PROTO_DTLS)
5012 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005013 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005014#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005015#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5016 ssl_dtls_replay_reset( ssl );
5017#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005018
5019 ssl->in_hslen = 0;
5020 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005021 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005022
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005023 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005024 ssl->out_msgtype = 0;
5025 ssl->out_msglen = 0;
5026 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005027#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005028 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5029 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005030#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005031
Paul Bakker48916f92012-09-16 19:57:18 +00005032 ssl->transform_in = NULL;
5033 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005034
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005035 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5036 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005037
5038#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005039 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005040 {
5041 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005042 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005043 {
5044 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5045 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5046 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005047 }
5048#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005049
Paul Bakker48916f92012-09-16 19:57:18 +00005050 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005051 {
Paul Bakker48916f92012-09-16 19:57:18 +00005052 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005053 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005054 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005055 }
Paul Bakker48916f92012-09-16 19:57:18 +00005056
Paul Bakkerc0463502013-02-14 11:19:38 +01005057 if( ssl->session )
5058 {
5059 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005060 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005061 ssl->session = NULL;
5062 }
5063
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005064#if defined(POLARSSL_SSL_ALPN)
5065 ssl->alpn_chosen = NULL;
5066#endif
5067
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005068#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005069 polarssl_free( ssl->cli_id );
5070 ssl->cli_id = NULL;
5071 ssl->cli_id_len = 0;
5072#endif
5073
Paul Bakker48916f92012-09-16 19:57:18 +00005074 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5075 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005076
5077 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005078}
5079
Paul Bakkera503a632013-08-14 13:48:06 +02005080#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005081static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5082{
5083 aes_free( &tkeys->enc );
5084 aes_free( &tkeys->dec );
5085
5086 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5087}
5088
Paul Bakker7eb013f2011-10-06 12:37:39 +00005089/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005090 * Allocate and initialize ticket keys
5091 */
5092static int ssl_ticket_keys_init( ssl_context *ssl )
5093{
5094 int ret;
5095 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005096 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005097
5098 if( ssl->ticket_keys != NULL )
5099 return( 0 );
5100
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005101 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
5102 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005103 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5104
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005105 aes_init( &tkeys->enc );
5106 aes_init( &tkeys->dec );
5107
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005108 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005109 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005110 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005111 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005112 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005113 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005114
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005115 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5116 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5117 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5118 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005119 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005120 polarssl_free( tkeys );
5121 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005122 }
5123
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005124 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005125 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005126 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005127 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005128 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005129 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005130
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005131 ssl->ticket_keys = tkeys;
5132
5133 return( 0 );
5134}
Paul Bakkera503a632013-08-14 13:48:06 +02005135#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005136
5137/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 * SSL set accessors
5139 */
5140void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5141{
5142 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005143
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005144#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5145 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005146 if( endpoint == SSL_IS_CLIENT )
5147 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005148#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005149
5150#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5151 if( endpoint == SSL_IS_SERVER )
5152 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005154}
5155
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005156int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005157{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005158#if defined(POLARSSL_SSL_PROTO_DTLS)
5159 if( transport == SSL_TRANSPORT_DATAGRAM )
5160 {
5161 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005162
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005163 ssl->out_hdr = ssl->out_buf;
5164 ssl->out_ctr = ssl->out_buf + 3;
5165 ssl->out_len = ssl->out_buf + 11;
5166 ssl->out_iv = ssl->out_buf + 13;
5167 ssl->out_msg = ssl->out_buf + 13;
5168
5169 ssl->in_hdr = ssl->in_buf;
5170 ssl->in_ctr = ssl->in_buf + 3;
5171 ssl->in_len = ssl->in_buf + 11;
5172 ssl->in_iv = ssl->in_buf + 13;
5173 ssl->in_msg = ssl->in_buf + 13;
5174
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005175 /* DTLS starts with TLS1.1 */
5176 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5177 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005178
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005179 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5180 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5181
5182 return( 0 );
5183 }
5184#endif
5185
5186 if( transport == SSL_TRANSPORT_STREAM )
5187 {
5188 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005189
5190 ssl->out_ctr = ssl->out_buf;
5191 ssl->out_hdr = ssl->out_buf + 8;
5192 ssl->out_len = ssl->out_buf + 11;
5193 ssl->out_iv = ssl->out_buf + 13;
5194 ssl->out_msg = ssl->out_buf + 13;
5195
5196 ssl->in_ctr = ssl->in_buf;
5197 ssl->in_hdr = ssl->in_buf + 8;
5198 ssl->in_len = ssl->in_buf + 11;
5199 ssl->in_iv = ssl->in_buf + 13;
5200 ssl->in_msg = ssl->in_buf + 13;
5201
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005202 return( 0 );
5203 }
5204
5205 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005206}
5207
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005208#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5209void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5210{
5211 ssl->anti_replay = mode;
5212}
5213#endif
5214
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005215#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5216void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5217{
5218 ssl->badmac_limit = limit;
5219}
5220#endif
5221
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005222#if defined(POLARSSL_SSL_PROTO_DTLS)
5223void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5224{
5225 ssl->hs_timeout_min = min;
5226 ssl->hs_timeout_max = max;
5227}
5228#endif
5229
Paul Bakker5121ce52009-01-03 21:22:43 +00005230void ssl_set_authmode( ssl_context *ssl, int authmode )
5231{
5232 ssl->authmode = authmode;
5233}
5234
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005235#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005236void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005237 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005238 void *p_vrfy )
5239{
5240 ssl->f_vrfy = f_vrfy;
5241 ssl->p_vrfy = p_vrfy;
5242}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005243#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005244
Paul Bakker5121ce52009-01-03 21:22:43 +00005245void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005246 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005247 void *p_rng )
5248{
5249 ssl->f_rng = f_rng;
5250 ssl->p_rng = p_rng;
5251}
5252
5253void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005254 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005255 void *p_dbg )
5256{
5257 ssl->f_dbg = f_dbg;
5258 ssl->p_dbg = p_dbg;
5259}
5260
5261void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005262 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005263 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005264{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005265 if( p_recv != p_send )
5266 {
5267 ssl->f_recv = NULL;
5268 ssl->f_send = NULL;
5269 ssl->p_bio = NULL;
5270 return;
5271 }
5272
Paul Bakker5121ce52009-01-03 21:22:43 +00005273 ssl->f_recv = f_recv;
5274 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005275 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005276}
5277
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005278void ssl_set_bio_timeout( ssl_context *ssl,
5279 void *p_bio,
5280 int (*f_send)(void *, const unsigned char *, size_t),
5281 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005282 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5283 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005284{
5285 ssl->p_bio = p_bio;
5286 ssl->f_send = f_send;
5287 ssl->f_recv = f_recv;
5288 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005289 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005290}
5291
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005292#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005293void ssl_set_session_cache( ssl_context *ssl,
5294 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5295 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005296{
Paul Bakker0a597072012-09-25 21:55:46 +00005297 ssl->f_get_cache = f_get_cache;
5298 ssl->p_get_cache = p_get_cache;
5299 ssl->f_set_cache = f_set_cache;
5300 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005301}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005302#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005303
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005304#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005305int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005306{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005307 int ret;
5308
5309 if( ssl == NULL ||
5310 session == NULL ||
5311 ssl->session_negotiate == NULL ||
5312 ssl->endpoint != SSL_IS_CLIENT )
5313 {
5314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5315 }
5316
5317 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5318 return( ret );
5319
Paul Bakker0a597072012-09-25 21:55:46 +00005320 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005321
5322 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005323}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005324#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005325
Paul Bakkerb68cad62012-08-23 08:34:18 +00005326void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005327{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005328 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5329 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5330 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5331 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5332}
5333
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005334void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5335 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005336 int major, int minor )
5337{
5338 if( major != SSL_MAJOR_VERSION_3 )
5339 return;
5340
5341 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5342 return;
5343
5344 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005345}
5346
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005347#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005348/* Add a new (empty) key_cert entry an return a pointer to it */
5349static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5350{
5351 ssl_key_cert *key_cert, *last;
5352
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005353 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5354 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005355 return( NULL );
5356
5357 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5358
5359 /* Append the new key_cert to the (possibly empty) current list */
5360 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005361 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005362 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005363 if( ssl->handshake != NULL )
5364 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005365 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005366 else
5367 {
5368 last = ssl->key_cert;
5369 while( last->next != NULL )
5370 last = last->next;
5371 last->next = key_cert;
5372 }
5373
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005374 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005375}
5376
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005377void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005378 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005379{
5380 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005381 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005382 ssl->peer_cn = peer_cn;
5383}
5384
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005385int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005386 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005387{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005388 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5389
5390 if( key_cert == NULL )
5391 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5392
5393 key_cert->cert = own_cert;
5394 key_cert->key = pk_key;
5395
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005396 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005397}
5398
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005399#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005400int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005401 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005402{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005403 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005404 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005405
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005406 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005407 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5408
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005409 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5410 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005411 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005412
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005413 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005414
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005415 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005416 if( ret != 0 )
5417 return( ret );
5418
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005419 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005420 return( ret );
5421
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005422 key_cert->cert = own_cert;
5423 key_cert->key_own_alloc = 1;
5424
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005425 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005426}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005427#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005428
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005429int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005430 void *rsa_key,
5431 rsa_decrypt_func rsa_decrypt,
5432 rsa_sign_func rsa_sign,
5433 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005434{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005435 int ret;
5436 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005437
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005438 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005439 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5440
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005441 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5442 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005443 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005444
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005445 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005446
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005447 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5448 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5449 return( ret );
5450
5451 key_cert->cert = own_cert;
5452 key_cert->key_own_alloc = 1;
5453
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005454 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00005455}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005456#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005457
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005458#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005459int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5460 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005461{
Paul Bakker6db455e2013-09-18 17:29:31 +02005462 if( psk == NULL || psk_identity == NULL )
5463 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5464
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005465 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005466 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5467
Paul Bakker6db455e2013-09-18 17:29:31 +02005468 if( ssl->psk != NULL )
5469 {
5470 polarssl_free( ssl->psk );
5471 polarssl_free( ssl->psk_identity );
5472 }
5473
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005474 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005475 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005476
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005477 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005478 ssl->psk_identity = (unsigned char *)
5479 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005480
5481 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5482 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5483
5484 memcpy( ssl->psk, psk, ssl->psk_len );
5485 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005486
5487 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005488}
5489
5490void ssl_set_psk_cb( ssl_context *ssl,
5491 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5492 size_t),
5493 void *p_psk )
5494{
5495 ssl->f_psk = f_psk;
5496 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005497}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005498#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005499
Paul Bakker48916f92012-09-16 19:57:18 +00005500#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005501int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005502{
5503 int ret;
5504
Paul Bakker48916f92012-09-16 19:57:18 +00005505 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
5507 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5508 return( ret );
5509 }
5510
Paul Bakker48916f92012-09-16 19:57:18 +00005511 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005512 {
5513 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5514 return( ret );
5515 }
5516
5517 return( 0 );
5518}
5519
Paul Bakker1b57b062011-01-06 15:48:19 +00005520int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5521{
5522 int ret;
5523
Paul Bakker66d5d072014-06-17 16:39:18 +02005524 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005525 {
5526 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5527 return( ret );
5528 }
5529
Paul Bakker66d5d072014-06-17 16:39:18 +02005530 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005531 {
5532 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5533 return( ret );
5534 }
5535
5536 return( 0 );
5537}
Paul Bakker48916f92012-09-16 19:57:18 +00005538#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005539
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005540#if defined(POLARSSL_SSL_SET_CURVES)
5541/*
5542 * Set the allowed elliptic curves
5543 */
5544void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5545{
5546 ssl->curve_list = curve_list;
5547}
5548#endif
5549
Paul Bakker0be444a2013-08-27 21:55:01 +02005550#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005551int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005552{
5553 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005554 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005555
5556 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005557
5558 if( ssl->hostname_len + 1 == 0 )
5559 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5560
Paul Bakker6e339b52013-07-03 13:37:05 +02005561 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562
Paul Bakkerb15b8512012-01-13 13:44:06 +00005563 if( ssl->hostname == NULL )
5564 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5565
Paul Bakker3c2122f2013-06-24 19:03:14 +02005566 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005567 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005568
Paul Bakker40ea7de2009-05-03 10:18:48 +00005569 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005570
5571 return( 0 );
5572}
5573
Paul Bakker5701cdc2012-09-27 21:49:42 +00005574void ssl_set_sni( ssl_context *ssl,
5575 int (*f_sni)(void *, ssl_context *,
5576 const unsigned char *, size_t),
5577 void *p_sni )
5578{
5579 ssl->f_sni = f_sni;
5580 ssl->p_sni = p_sni;
5581}
Paul Bakker0be444a2013-08-27 21:55:01 +02005582#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005583
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005584#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005585int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005586{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005587 size_t cur_len, tot_len;
5588 const char **p;
5589
5590 /*
5591 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5592 * truncated". Check lengths now rather than later.
5593 */
5594 tot_len = 0;
5595 for( p = protos; *p != NULL; p++ )
5596 {
5597 cur_len = strlen( *p );
5598 tot_len += cur_len;
5599
5600 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5601 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5602 }
5603
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005604 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005605
5606 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005607}
5608
5609const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5610{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005611 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005612}
5613#endif /* POLARSSL_SSL_ALPN */
5614
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005615static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005616{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005617 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005618 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005619 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005620 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005621 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005622
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005623#if defined(POLARSSL_SSL_PROTO_DTLS)
5624 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5625 minor < SSL_MINOR_VERSION_2 )
5626 {
5627 return( -1 );
5628 }
5629#else
5630 ((void) ssl);
5631#endif
5632
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005633 return( 0 );
5634}
5635
5636int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5637{
5638 if( ssl_check_version( ssl, major, minor ) != 0 )
5639 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5640
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005641 ssl->max_major_ver = major;
5642 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005643
5644 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005645}
5646
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005647int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005648{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005649 if( ssl_check_version( ssl, major, minor ) != 0 )
5650 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005651
5652 ssl->min_major_ver = major;
5653 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005654
5655 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005656}
5657
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005658#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5659void ssl_set_fallback( ssl_context *ssl, char fallback )
5660{
5661 ssl->fallback = fallback;
5662}
5663#endif
5664
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005665#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5666void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5667{
5668 ssl->encrypt_then_mac = etm;
5669}
5670#endif
5671
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005672#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5673void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5674{
5675 ssl->extended_ms = ems;
5676}
5677#endif
5678
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005679void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5680{
5681 ssl->arc4_disabled = arc4;
5682}
5683
Paul Bakker05decb22013-08-15 13:33:48 +02005684#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005685int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5686{
Paul Bakker77e257e2013-12-16 15:29:52 +01005687 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005688 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005689 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005690 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005691 }
5692
5693 ssl->mfl_code = mfl_code;
5694
5695 return( 0 );
5696}
Paul Bakker05decb22013-08-15 13:33:48 +02005697#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005698
Paul Bakker1f2bc622013-08-15 13:45:55 +02005699#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005700int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005701{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005702 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005703
5704 return( 0 );
5705}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005706#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005707
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005708#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5709void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5710{
5711 ssl->split_done = split;
5712}
5713#endif
5714
Paul Bakker48916f92012-09-16 19:57:18 +00005715void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5716{
5717 ssl->allow_legacy_renegotiation = allow_legacy;
5718}
5719
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005720#if defined(POLARSSL_SSL_RENEGOTIATION)
5721void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5722{
5723 ssl->disable_renegotiation = renegotiation;
5724}
5725
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005726void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5727{
5728 ssl->renego_max_records = max_records;
5729}
5730
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005731void ssl_set_renegotiation_period( ssl_context *ssl,
5732 const unsigned char period[8] )
5733{
5734 memcpy( ssl->renego_period, period, 8 );
5735}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005736#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005737
Paul Bakkera503a632013-08-14 13:48:06 +02005738#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005739int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5740{
5741 ssl->session_tickets = use_tickets;
5742
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005743#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005744 if( ssl->endpoint == SSL_IS_CLIENT )
5745 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005746#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005747
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005748 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5749 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005750
5751 if( ssl->f_rng == NULL )
5752 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5753
5754 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005755}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005756
5757void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5758{
5759 ssl->ticket_lifetime = lifetime;
5760}
Paul Bakkera503a632013-08-14 13:48:06 +02005761#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005762
Paul Bakker5121ce52009-01-03 21:22:43 +00005763/*
5764 * SSL get accessors
5765 */
5766size_t ssl_get_bytes_avail( const ssl_context *ssl )
5767{
5768 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5769}
5770
Paul Bakkerff60ee62010-03-16 21:09:09 +00005771int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005772{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005773 if( ssl->session != NULL )
5774 return( ssl->session->verify_result );
5775
5776 if( ssl->session_negotiate != NULL )
5777 return( ssl->session_negotiate->verify_result );
5778
5779 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005780}
5781
Paul Bakkere3166ce2011-01-27 17:40:50 +00005782const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005783{
Paul Bakker926c8e42013-03-06 10:23:34 +01005784 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005785 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005786
Paul Bakkere3166ce2011-01-27 17:40:50 +00005787 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005788}
5789
Paul Bakker43ca69c2011-01-15 17:35:19 +00005790const char *ssl_get_version( const ssl_context *ssl )
5791{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005792#if defined(POLARSSL_SSL_PROTO_DTLS)
5793 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5794 {
5795 switch( ssl->minor_ver )
5796 {
5797 case SSL_MINOR_VERSION_2:
5798 return( "DTLSv1.0" );
5799
5800 case SSL_MINOR_VERSION_3:
5801 return( "DTLSv1.2" );
5802
5803 default:
5804 return( "unknown (DTLS)" );
5805 }
5806 }
5807#endif
5808
Paul Bakker43ca69c2011-01-15 17:35:19 +00005809 switch( ssl->minor_ver )
5810 {
5811 case SSL_MINOR_VERSION_0:
5812 return( "SSLv3.0" );
5813
5814 case SSL_MINOR_VERSION_1:
5815 return( "TLSv1.0" );
5816
5817 case SSL_MINOR_VERSION_2:
5818 return( "TLSv1.1" );
5819
Paul Bakker1ef83d62012-04-11 12:09:53 +00005820 case SSL_MINOR_VERSION_3:
5821 return( "TLSv1.2" );
5822
Paul Bakker43ca69c2011-01-15 17:35:19 +00005823 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005824 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005825 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005826}
5827
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005828int ssl_get_record_expansion( const ssl_context *ssl )
5829{
5830 int transform_expansion;
5831 const ssl_transform *transform = ssl->transform_out;
5832
5833#if defined(POLARSSL_ZLIB_SUPPORT)
5834 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5835 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5836#endif
5837
5838 if( transform == NULL )
5839 return( ssl_hdr_len( ssl ) );
5840
5841 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5842 {
5843 case POLARSSL_MODE_GCM:
5844 case POLARSSL_MODE_CCM:
5845 case POLARSSL_MODE_STREAM:
5846 transform_expansion = transform->minlen;
5847 break;
5848
5849 case POLARSSL_MODE_CBC:
5850 transform_expansion = transform->maclen
5851 + cipher_get_block_size( &transform->cipher_ctx_enc );
5852 break;
5853
5854 default:
5855 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5856 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5857 }
5858
5859 return( ssl_hdr_len( ssl ) + transform_expansion );
5860}
5861
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005862#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005863const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005864{
5865 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005866 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005867
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005868 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005869}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005870#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005871
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005872#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005873int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5874{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005875 if( ssl == NULL ||
5876 dst == NULL ||
5877 ssl->session == NULL ||
5878 ssl->endpoint != SSL_IS_CLIENT )
5879 {
5880 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5881 }
5882
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005883 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005884}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005885#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005886
Paul Bakker5121ce52009-01-03 21:22:43 +00005887/*
Paul Bakker1961b702013-01-25 14:49:24 +01005888 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 */
Paul Bakker1961b702013-01-25 14:49:24 +01005890int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005891{
Paul Bakker40e46942009-01-03 21:51:57 +00005892 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005893
Paul Bakker40e46942009-01-03 21:51:57 +00005894#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005895 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005896 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005897#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005898#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005900 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005901#endif
5902
Paul Bakker1961b702013-01-25 14:49:24 +01005903 return( ret );
5904}
5905
5906/*
5907 * Perform the SSL handshake
5908 */
5909int ssl_handshake( ssl_context *ssl )
5910{
5911 int ret = 0;
5912
5913 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5914
5915 while( ssl->state != SSL_HANDSHAKE_OVER )
5916 {
5917 ret = ssl_handshake_step( ssl );
5918
5919 if( ret != 0 )
5920 break;
5921 }
5922
Paul Bakker5121ce52009-01-03 21:22:43 +00005923 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5924
5925 return( ret );
5926}
5927
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005928#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005929#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005930/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005931 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005932 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005933static int ssl_write_hello_request( ssl_context *ssl )
5934{
5935 int ret;
5936
5937 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5938
5939 ssl->out_msglen = 4;
5940 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5941 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5942
5943 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5944 {
5945 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5946 return( ret );
5947 }
5948
5949 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5950
5951 return( 0 );
5952}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005953#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005954
5955/*
5956 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005957 * - any side: calling ssl_renegotiate(),
5958 * - client: receiving a HelloRequest during ssl_read(),
5959 * - server: receiving any handshake message on server during ssl_read() after
5960 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005961 * If the handshake doesn't complete due to waiting for I/O, it will continue
5962 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005963 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005964static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005965{
5966 int ret;
5967
5968 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5969
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005970 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5971 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005972
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005973 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5974 * the ServerHello will have message_seq = 1" */
5975#if defined(POLARSSL_SSL_PROTO_DTLS)
5976 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005977 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5978 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005979 if( ssl->endpoint == SSL_IS_SERVER )
5980 ssl->handshake->out_msg_seq = 1;
5981 else
5982 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005983 }
5984#endif
5985
Paul Bakker48916f92012-09-16 19:57:18 +00005986 ssl->state = SSL_HELLO_REQUEST;
5987 ssl->renegotiation = SSL_RENEGOTIATION;
5988
Paul Bakker48916f92012-09-16 19:57:18 +00005989 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5990 {
5991 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5992 return( ret );
5993 }
5994
5995 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5996
5997 return( 0 );
5998}
5999
6000/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006001 * Renegotiate current connection on client,
6002 * or request renegotiation on server
6003 */
6004int ssl_renegotiate( ssl_context *ssl )
6005{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006006 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006007
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006008#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006009 /* On server, just send the request */
6010 if( ssl->endpoint == SSL_IS_SERVER )
6011 {
6012 if( ssl->state != SSL_HANDSHAKE_OVER )
6013 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6014
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02006015 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6016
6017 /* Did we already try/start sending HelloRequest? */
6018 if( ssl->out_left != 0 )
6019 return( ssl_flush_output( ssl ) );
6020
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006021 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006022 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006023#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006024
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006025#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006026 /*
6027 * On client, either start the renegotiation process or,
6028 * if already in progress, continue the handshake
6029 */
6030 if( ssl->renegotiation != SSL_RENEGOTIATION )
6031 {
6032 if( ssl->state != SSL_HANDSHAKE_OVER )
6033 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6034
6035 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6036 {
6037 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6038 return( ret );
6039 }
6040 }
6041 else
6042 {
6043 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6044 {
6045 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6046 return( ret );
6047 }
6048 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006049#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006050
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006051 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006052}
6053
6054/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006055 * Check record counters and renegotiate if they're above the limit.
6056 */
6057static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6058{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006059 if( ssl->state != SSL_HANDSHAKE_OVER ||
6060 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
6061 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6062 {
6063 return( 0 );
6064 }
6065
6066 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006067 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6068 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006069 {
6070 return( 0 );
6071 }
6072
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006073 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006074 return( ssl_renegotiate( ssl ) );
6075}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006076#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006077
6078/*
6079 * Receive application data decrypted from the SSL layer
6080 */
Paul Bakker23986e52011-04-24 08:57:21 +00006081int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006082{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006083 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006084 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006085
6086 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6087
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006088#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006089 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006090 {
6091 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6092 return( ret );
6093
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006094 if( ssl->handshake != NULL &&
6095 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6096 {
6097 if( ( ret = ssl_resend( ssl ) ) != 0 )
6098 return( ret );
6099 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006100 }
6101#endif
6102
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006103#if defined(POLARSSL_SSL_RENEGOTIATION)
6104 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6105 {
6106 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6107 return( ret );
6108 }
6109#endif
6110
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 if( ssl->state != SSL_HANDSHAKE_OVER )
6112 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006113 ret = ssl_handshake( ssl );
6114 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6115 {
6116 record_read = 1;
6117 }
6118 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 {
6120 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6121 return( ret );
6122 }
6123 }
6124
6125 if( ssl->in_offt == NULL )
6126 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006127#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006128 /* Start timer if not already running */
6129 if( ssl->time_limit == 0 )
6130 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006131#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006132
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006133 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006135 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6136 {
6137 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6138 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006139
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006140 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6141 return( ret );
6142 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 }
6144
6145 if( ssl->in_msglen == 0 &&
6146 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6147 {
6148 /*
6149 * OpenSSL sends empty messages to randomize the IV
6150 */
6151 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6152 {
Paul Bakker831a7552011-05-18 13:32:51 +00006153 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6154 return( 0 );
6155
Paul Bakker5121ce52009-01-03 21:22:43 +00006156 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6157 return( ret );
6158 }
6159 }
6160
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006161#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006162 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6163 {
6164 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6165
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006166#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006167 if( ssl->endpoint == SSL_IS_CLIENT &&
6168 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006169 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006170 {
6171 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006172
6173 /* With DTLS, drop the packet (probably from last handshake) */
6174#if defined(POLARSSL_SSL_PROTO_DTLS)
6175 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6176 return( POLARSSL_ERR_NET_WANT_READ );
6177#endif
6178 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6179 }
6180
6181 if( ssl->endpoint == SSL_IS_SERVER &&
6182 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6183 {
6184 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6185
6186 /* With DTLS, drop the packet (probably from last handshake) */
6187#if defined(POLARSSL_SSL_PROTO_DTLS)
6188 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6189 return( POLARSSL_ERR_NET_WANT_READ );
6190#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006191 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6192 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006193#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006194
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006195 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6196 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006197 ssl->allow_legacy_renegotiation ==
6198 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006199 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006200 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006201
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006202#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006203 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006204 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006205 /*
6206 * SSLv3 does not have a "no_renegotiation" alert
6207 */
6208 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6209 return( ret );
6210 }
6211 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006212#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006213#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6214 defined(POLARSSL_SSL_PROTO_TLS1_2)
6215 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006216 {
6217 if( ( ret = ssl_send_alert_message( ssl,
6218 SSL_ALERT_LEVEL_WARNING,
6219 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6220 {
6221 return( ret );
6222 }
Paul Bakker48916f92012-09-16 19:57:18 +00006223 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006224 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006225#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6226 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006227 {
6228 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006229 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006230 }
Paul Bakker48916f92012-09-16 19:57:18 +00006231 }
6232 else
6233 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006234 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006235#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006236 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6237 ssl->endpoint == SSL_IS_CLIENT )
6238 {
6239 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6240 }
6241#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006242 ret = ssl_start_renegotiation( ssl );
6243 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6244 {
6245 record_read = 1;
6246 }
6247 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006248 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006249 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006250 return( ret );
6251 }
Paul Bakker48916f92012-09-16 19:57:18 +00006252 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006253
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006254 /* If a non-handshake record was read during renego, fallthrough,
6255 * else tell the user they should call ssl_read() again */
6256 if( ! record_read )
6257 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006258 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006259 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6260 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006261
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006262 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006263 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006264 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6265 {
6266 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6267 "but not honored by client" ) );
6268 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6269 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006270 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006271 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006272#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006273
6274 /* Fatal and closure alerts handled by ssl_read_record() */
6275 if( ssl->in_msgtype == SSL_MSG_ALERT )
6276 {
6277 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6278 return( POLARSSL_ERR_NET_WANT_READ );
6279 }
6280
6281 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006282 {
6283 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006284 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006285 }
6286
6287 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006288
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006289#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006290 /* We're going to return something now, cancel timer,
6291 * except if handshake (renegotiation) is in progress */
6292 if( ssl->state == SSL_HANDSHAKE_OVER )
6293 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006294
6295 /* If we requested renego but received AppData, resend HelloRequest.
6296 * Do it now, after setting in_offt, to avoid taking this branch
6297 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006298#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006299 if( ssl->endpoint == SSL_IS_SERVER &&
6300 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6301 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006302 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006303 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006304 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006305 return( ret );
6306 }
6307 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006308#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006309#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006310 }
6311
6312 n = ( len < ssl->in_msglen )
6313 ? len : ssl->in_msglen;
6314
6315 memcpy( buf, ssl->in_offt, n );
6316 ssl->in_msglen -= n;
6317
6318 if( ssl->in_msglen == 0 )
6319 /* all bytes consumed */
6320 ssl->in_offt = NULL;
6321 else
6322 /* more data available */
6323 ssl->in_offt += n;
6324
6325 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6326
Paul Bakker23986e52011-04-24 08:57:21 +00006327 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006328}
6329
6330/*
6331 * Send application data to be encrypted by the SSL layer
6332 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006333#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6334static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6335#else
Paul Bakker23986e52011-04-24 08:57:21 +00006336int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006338{
Paul Bakker23986e52011-04-24 08:57:21 +00006339 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006340#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6341 unsigned int max_len;
6342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
6344 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6345
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006346#if defined(POLARSSL_SSL_RENEGOTIATION)
6347 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6348 {
6349 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6350 return( ret );
6351 }
6352#endif
6353
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 if( ssl->state != SSL_HANDSHAKE_OVER )
6355 {
6356 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6357 {
6358 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6359 return( ret );
6360 }
6361 }
6362
Paul Bakker05decb22013-08-15 13:33:48 +02006363#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006364 /*
6365 * Assume mfl_code is correct since it was checked when set
6366 */
6367 max_len = mfl_code_to_length[ssl->mfl_code];
6368
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006369 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006370 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006371 */
6372 if( ssl->session_out != NULL &&
6373 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6374 {
6375 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6376 }
6377
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006378 if( len > max_len )
6379 {
6380#if defined(POLARSSL_SSL_PROTO_DTLS)
6381 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6382 {
6383 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6384 "maximum fragment length: %d > %d",
6385 len, max_len ) );
6386 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6387 }
6388 else
6389#endif
6390 len = max_len;
6391 }
6392#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006393
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 if( ssl->out_left != 0 )
6395 {
6396 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6397 {
6398 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6399 return( ret );
6400 }
6401 }
Paul Bakker887bd502011-06-08 13:10:54 +00006402 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006403 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006404 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006405 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006406 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006407
6408 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6409 {
6410 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6411 return( ret );
6412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 }
6414
6415 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6416
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006417 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418}
6419
6420/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006421 * Write application data, doing 1/n-1 splitting if necessary.
6422 *
6423 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006424 * then the caller will call us again with the same arguments, so
6425 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006426 */
6427#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6428int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6429{
6430 int ret;
6431
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006432 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6433 len <= 1 ||
6434 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006435 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6436 != POLARSSL_MODE_CBC )
6437 {
6438 return( ssl_write_real( ssl, buf, len ) );
6439 }
6440
6441 if( ssl->split_done == 0 )
6442 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006443 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006444 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006445 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006446 }
6447
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006448 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6449 return( ret );
6450 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006451
6452 return( ret + 1 );
6453}
6454#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6455
6456/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 * Notify the peer that the connection is being closed
6458 */
6459int ssl_close_notify( ssl_context *ssl )
6460{
6461 int ret;
6462
6463 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6464
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006465 if( ssl->out_left != 0 )
6466 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006467
6468 if( ssl->state == SSL_HANDSHAKE_OVER )
6469 {
Paul Bakker48916f92012-09-16 19:57:18 +00006470 if( ( ret = ssl_send_alert_message( ssl,
6471 SSL_ALERT_LEVEL_WARNING,
6472 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006474 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006475 return( ret );
6476 }
6477 }
6478
6479 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6480
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006481 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006482}
6483
Paul Bakker48916f92012-09-16 19:57:18 +00006484void ssl_transform_free( ssl_transform *transform )
6485{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006486 if( transform == NULL )
6487 return;
6488
Paul Bakker48916f92012-09-16 19:57:18 +00006489#if defined(POLARSSL_ZLIB_SUPPORT)
6490 deflateEnd( &transform->ctx_deflate );
6491 inflateEnd( &transform->ctx_inflate );
6492#endif
6493
Paul Bakker84bbeb52014-07-01 14:53:22 +02006494 cipher_free( &transform->cipher_ctx_enc );
6495 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006496
Paul Bakker84bbeb52014-07-01 14:53:22 +02006497 md_free( &transform->md_ctx_enc );
6498 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006499
Paul Bakker34617722014-06-13 17:20:13 +02006500 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006501}
6502
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006503#if defined(POLARSSL_X509_CRT_PARSE_C)
6504static void ssl_key_cert_free( ssl_key_cert *key_cert )
6505{
6506 ssl_key_cert *cur = key_cert, *next;
6507
6508 while( cur != NULL )
6509 {
6510 next = cur->next;
6511
6512 if( cur->key_own_alloc )
6513 {
6514 pk_free( cur->key );
6515 polarssl_free( cur->key );
6516 }
6517 polarssl_free( cur );
6518
6519 cur = next;
6520 }
6521}
6522#endif /* POLARSSL_X509_CRT_PARSE_C */
6523
Paul Bakker48916f92012-09-16 19:57:18 +00006524void ssl_handshake_free( ssl_handshake_params *handshake )
6525{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006526 if( handshake == NULL )
6527 return;
6528
Paul Bakker48916f92012-09-16 19:57:18 +00006529#if defined(POLARSSL_DHM_C)
6530 dhm_free( &handshake->dhm_ctx );
6531#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006532#if defined(POLARSSL_ECDH_C)
6533 ecdh_free( &handshake->ecdh_ctx );
6534#endif
6535
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006536#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006537 /* explicit void pointer cast for buggy MS compiler */
6538 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006539#endif
6540
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006541#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6542 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6543 /*
6544 * Free only the linked list wrapper, not the keys themselves
6545 * since the belong to the SNI callback
6546 */
6547 if( handshake->sni_key_cert != NULL )
6548 {
6549 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6550
6551 while( cur != NULL )
6552 {
6553 next = cur->next;
6554 polarssl_free( cur );
6555 cur = next;
6556 }
6557 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006558#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006559
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006560#if defined(POLARSSL_SSL_PROTO_DTLS)
6561 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006562 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006563 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006564#endif
6565
Paul Bakker34617722014-06-13 17:20:13 +02006566 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006567}
6568
6569void ssl_session_free( ssl_session *session )
6570{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006571 if( session == NULL )
6572 return;
6573
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006574#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006575 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006576 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006577 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006578 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006579 }
Paul Bakkered27a042013-04-18 22:46:23 +02006580#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006581
Paul Bakkera503a632013-08-14 13:48:06 +02006582#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006583 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006584#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006585
Paul Bakker34617722014-06-13 17:20:13 +02006586 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006587}
6588
Paul Bakker5121ce52009-01-03 21:22:43 +00006589/*
6590 * Free an SSL context
6591 */
6592void ssl_free( ssl_context *ssl )
6593{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006594 if( ssl == NULL )
6595 return;
6596
Paul Bakker5121ce52009-01-03 21:22:43 +00006597 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6598
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006599 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006600 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006601 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6602 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006603 }
6604
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006605 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006606 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006607 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6608 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 }
6610
Paul Bakker16770332013-10-11 09:59:44 +02006611#if defined(POLARSSL_ZLIB_SUPPORT)
6612 if( ssl->compress_buf != NULL )
6613 {
Paul Bakker34617722014-06-13 17:20:13 +02006614 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006615 polarssl_free( ssl->compress_buf );
6616 }
6617#endif
6618
Paul Bakker40e46942009-01-03 21:51:57 +00006619#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006620 mpi_free( &ssl->dhm_P );
6621 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006622#endif
6623
Paul Bakker48916f92012-09-16 19:57:18 +00006624 if( ssl->transform )
6625 {
6626 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006627 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006628 }
6629
6630 if( ssl->handshake )
6631 {
6632 ssl_handshake_free( ssl->handshake );
6633 ssl_transform_free( ssl->transform_negotiate );
6634 ssl_session_free( ssl->session_negotiate );
6635
Paul Bakker6e339b52013-07-03 13:37:05 +02006636 polarssl_free( ssl->handshake );
6637 polarssl_free( ssl->transform_negotiate );
6638 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006639 }
6640
Paul Bakkerc0463502013-02-14 11:19:38 +01006641 if( ssl->session )
6642 {
6643 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006644 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006645 }
6646
Paul Bakkera503a632013-08-14 13:48:06 +02006647#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006648 if( ssl->ticket_keys )
6649 {
6650 ssl_ticket_keys_free( ssl->ticket_keys );
6651 polarssl_free( ssl->ticket_keys );
6652 }
Paul Bakkera503a632013-08-14 13:48:06 +02006653#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006654
Paul Bakker0be444a2013-08-27 21:55:01 +02006655#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006656 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006657 {
Paul Bakker34617722014-06-13 17:20:13 +02006658 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006659 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006660 ssl->hostname_len = 0;
6661 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006662#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006663
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006664#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006665 if( ssl->psk != NULL )
6666 {
Paul Bakker34617722014-06-13 17:20:13 +02006667 polarssl_zeroize( ssl->psk, ssl->psk_len );
6668 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006669 polarssl_free( ssl->psk );
6670 polarssl_free( ssl->psk_identity );
6671 ssl->psk_len = 0;
6672 ssl->psk_identity_len = 0;
6673 }
6674#endif
6675
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006676#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006677 ssl_key_cert_free( ssl->key_cert );
6678#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006679
Paul Bakker05ef8352012-05-08 09:17:57 +00006680#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6681 if( ssl_hw_record_finish != NULL )
6682 {
6683 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6684 ssl_hw_record_finish( ssl );
6685 }
6686#endif
6687
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006688#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006689 polarssl_free( ssl->cli_id );
6690#endif
6691
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006693
Paul Bakker86f04f42013-02-14 11:20:09 +01006694 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006695 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006696}
6697
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006698#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006699/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006700 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006701 */
6702unsigned char ssl_sig_from_pk( pk_context *pk )
6703{
6704#if defined(POLARSSL_RSA_C)
6705 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6706 return( SSL_SIG_RSA );
6707#endif
6708#if defined(POLARSSL_ECDSA_C)
6709 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6710 return( SSL_SIG_ECDSA );
6711#endif
6712 return( SSL_SIG_ANON );
6713}
6714
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006715pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6716{
6717 switch( sig )
6718 {
6719#if defined(POLARSSL_RSA_C)
6720 case SSL_SIG_RSA:
6721 return( POLARSSL_PK_RSA );
6722#endif
6723#if defined(POLARSSL_ECDSA_C)
6724 case SSL_SIG_ECDSA:
6725 return( POLARSSL_PK_ECDSA );
6726#endif
6727 default:
6728 return( POLARSSL_PK_NONE );
6729 }
6730}
Paul Bakker9af723c2014-05-01 13:03:14 +02006731#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006732
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006733/*
6734 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6735 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006736md_type_t ssl_md_alg_from_hash( unsigned char hash )
6737{
6738 switch( hash )
6739 {
6740#if defined(POLARSSL_MD5_C)
6741 case SSL_HASH_MD5:
6742 return( POLARSSL_MD_MD5 );
6743#endif
6744#if defined(POLARSSL_SHA1_C)
6745 case SSL_HASH_SHA1:
6746 return( POLARSSL_MD_SHA1 );
6747#endif
6748#if defined(POLARSSL_SHA256_C)
6749 case SSL_HASH_SHA224:
6750 return( POLARSSL_MD_SHA224 );
6751 case SSL_HASH_SHA256:
6752 return( POLARSSL_MD_SHA256 );
6753#endif
6754#if defined(POLARSSL_SHA512_C)
6755 case SSL_HASH_SHA384:
6756 return( POLARSSL_MD_SHA384 );
6757 case SSL_HASH_SHA512:
6758 return( POLARSSL_MD_SHA512 );
6759#endif
6760 default:
6761 return( POLARSSL_MD_NONE );
6762 }
6763}
6764
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006765#if defined(POLARSSL_SSL_SET_CURVES)
6766/*
6767 * Check is a curve proposed by the peer is in our list.
6768 * Return 1 if we're willing to use it, 0 otherwise.
6769 */
6770int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6771{
6772 const ecp_group_id *gid;
6773
6774 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6775 if( *gid == grp_id )
6776 return( 1 );
6777
6778 return( 0 );
6779}
Paul Bakker9af723c2014-05-01 13:03:14 +02006780#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006781
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006782#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006783int ssl_check_cert_usage( const x509_crt *cert,
6784 const ssl_ciphersuite_t *ciphersuite,
6785 int cert_endpoint )
6786{
6787#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6788 int usage = 0;
6789#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006790#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6791 const char *ext_oid;
6792 size_t ext_len;
6793#endif
6794
6795#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6796 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6797 ((void) cert);
6798 ((void) cert_endpoint);
6799#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006800
6801#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6802 if( cert_endpoint == SSL_IS_SERVER )
6803 {
6804 /* Server part of the key exchange */
6805 switch( ciphersuite->key_exchange )
6806 {
6807 case POLARSSL_KEY_EXCHANGE_RSA:
6808 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6809 usage = KU_KEY_ENCIPHERMENT;
6810 break;
6811
6812 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6813 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6814 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6815 usage = KU_DIGITAL_SIGNATURE;
6816 break;
6817
6818 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6819 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6820 usage = KU_KEY_AGREEMENT;
6821 break;
6822
6823 /* Don't use default: we want warnings when adding new values */
6824 case POLARSSL_KEY_EXCHANGE_NONE:
6825 case POLARSSL_KEY_EXCHANGE_PSK:
6826 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6827 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6828 usage = 0;
6829 }
6830 }
6831 else
6832 {
6833 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6834 usage = KU_DIGITAL_SIGNATURE;
6835 }
6836
6837 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6838 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006839#else
6840 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006841#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6842
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006843#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6844 if( cert_endpoint == SSL_IS_SERVER )
6845 {
6846 ext_oid = OID_SERVER_AUTH;
6847 ext_len = OID_SIZE( OID_SERVER_AUTH );
6848 }
6849 else
6850 {
6851 ext_oid = OID_CLIENT_AUTH;
6852 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6853 }
6854
6855 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6856 return( -1 );
6857#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6858
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006859 return( 0 );
6860}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006861#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006862
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006863/*
6864 * Convert version numbers to/from wire format
6865 * and, for DTLS, to/from TLS equivalent.
6866 *
6867 * For TLS this is the identity.
6868 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6869 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6870 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6871 */
6872void ssl_write_version( int major, int minor, int transport,
6873 unsigned char ver[2] )
6874{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006875#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006876 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006877 {
6878 if( minor == SSL_MINOR_VERSION_2 )
6879 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6880
6881 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6882 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6883 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006884 else
6885#else
6886 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006887#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006888 {
6889 ver[0] = (unsigned char) major;
6890 ver[1] = (unsigned char) minor;
6891 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006892}
6893
6894void ssl_read_version( int *major, int *minor, int transport,
6895 const unsigned char ver[2] )
6896{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006897#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006898 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006899 {
6900 *major = 255 - ver[0] + 2;
6901 *minor = 255 - ver[1] + 1;
6902
6903 if( *minor == SSL_MINOR_VERSION_1 )
6904 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6905 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006906 else
6907#else
6908 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006909#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006910 {
6911 *major = ver[0];
6912 *minor = ver[1];
6913 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006914}
6915
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006916#endif /* POLARSSL_SSL_TLS_C */