blob: bdc9c534b67b373bfcaf34e7fb0022ad4ade58b2 [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/debug.h"
40#include "mbedtls/ssl.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020041
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020047#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067/* Length of the "epoch" field in the record header */
68static inline size_t ssl_ep_len( const ssl_context *ssl )
69{
70#if defined(POLARSSL_SSL_PROTO_DTLS)
71 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
72 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010073#else
74 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010075#endif
76 return( 0 );
77}
78
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020079
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020080#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
84 * The timer is already running iff time_limit != 0.
85 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020086static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020087{
88 ssl->time_limit = millisecs;
89 get_timer( &ssl->time_info, 1 );
90}
91
92/*
93 * Return -1 is timer is expired, 0 if it isn't.
94 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020095static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020096{
97 if( ssl->time_limit != 0 &&
98 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
99 {
100 return( -1 );
101 }
102
103 return( 0 );
104}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200106/*
107 * Double the retransmit timeout value, within the allowed range,
108 * returning -1 if the maximum value has already been reached.
109 */
110static int ssl_double_retransmit_timeout( ssl_context *ssl )
111{
112 uint32_t new_timeout;
113
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200114 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200115 return( -1 );
116
117 new_timeout = 2 * ssl->handshake->retransmit_timeout;
118
119 /* Avoid arithmetic overflow and range overflow */
120 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200121 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200122 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 }
125
126 ssl->handshake->retransmit_timeout = new_timeout;
127 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
128 ssl->handshake->retransmit_timeout ) );
129
130 return( 0 );
131}
132
133static void ssl_reset_retransmit_timeout( ssl_context *ssl )
134{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200135 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200136 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
137 ssl->handshake->retransmit_timeout ) );
138}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200139#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200140
Paul Bakker05decb22013-08-15 13:33:48 +0200141#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200142/*
143 * Convert max_fragment_length codes to length.
144 * RFC 6066 says:
145 * enum{
146 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
147 * } MaxFragmentLength;
148 * and we add 0 -> extension unused
149 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200150static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200151{
152 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
153 512, /* SSL_MAX_FRAG_LEN_512 */
154 1024, /* SSL_MAX_FRAG_LEN_1024 */
155 2048, /* SSL_MAX_FRAG_LEN_2048 */
156 4096, /* SSL_MAX_FRAG_LEN_4096 */
157};
Paul Bakker05decb22013-08-15 13:33:48 +0200158#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200159
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200160static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
161{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162 ssl_session_free( dst );
163 memcpy( dst, src, sizeof( ssl_session ) );
164
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200166 if( src->peer_cert != NULL )
167 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200168 int ret;
169
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500170 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200171 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200172 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
173
Paul Bakkerb6b09562013-09-18 14:17:41 +0200174 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200175
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200176 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
177 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200178 {
179 polarssl_free( dst->peer_cert );
180 dst->peer_cert = NULL;
181 return( ret );
182 }
183 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185
Paul Bakkera503a632013-08-14 13:48:06 +0200186#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187 if( src->ticket != NULL )
188 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500189 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200190 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200191 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
192
193 memcpy( dst->ticket, src->ticket, src->ticket_len );
194 }
Paul Bakkera503a632013-08-14 13:48:06 +0200195#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200196
197 return( 0 );
198}
199
Paul Bakker05ef8352012-05-08 09:17:57 +0000200#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200201int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200202 const unsigned char *key_enc, const unsigned char *key_dec,
203 size_t keylen,
204 const unsigned char *iv_enc, const unsigned char *iv_dec,
205 size_t ivlen,
206 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200207 size_t maclen ) = NULL;
208int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
209int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
210int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
211int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200213#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215/*
216 * Key material generation
217 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200218#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200219static int ssl3_prf( const unsigned char *secret, size_t slen,
220 const char *label,
221 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000222 unsigned char *dstbuf, size_t dlen )
223{
224 size_t i;
225 md5_context md5;
226 sha1_context sha1;
227 unsigned char padding[16];
228 unsigned char sha1sum[20];
229 ((void)label);
230
Paul Bakker5b4af392014-06-26 12:09:34 +0200231 md5_init( &md5 );
232 sha1_init( &sha1 );
233
Paul Bakker5f70b252012-09-13 14:23:06 +0000234 /*
235 * SSLv3:
236 * block =
237 * MD5( secret + SHA1( 'A' + secret + random ) ) +
238 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
239 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
240 * ...
241 */
242 for( i = 0; i < dlen / 16; i++ )
243 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000245
246 sha1_starts( &sha1 );
247 sha1_update( &sha1, padding, 1 + i );
248 sha1_update( &sha1, secret, slen );
249 sha1_update( &sha1, random, rlen );
250 sha1_finish( &sha1, sha1sum );
251
252 md5_starts( &md5 );
253 md5_update( &md5, secret, slen );
254 md5_update( &md5, sha1sum, 20 );
255 md5_finish( &md5, dstbuf + i * 16 );
256 }
257
Paul Bakker5b4af392014-06-26 12:09:34 +0200258 md5_free( &md5 );
259 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000260
Paul Bakker34617722014-06-13 17:20:13 +0200261 polarssl_zeroize( padding, sizeof( padding ) );
262 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000263
264 return( 0 );
265}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200266#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000267
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200269static int tls1_prf( const unsigned char *secret, size_t slen,
270 const char *label,
271 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000272 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273{
Paul Bakker23986e52011-04-24 08:57:21 +0000274 size_t nb, hs;
275 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200276 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 unsigned char tmp[128];
278 unsigned char h_i[20];
279
280 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000281 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
283 hs = ( slen + 1 ) / 2;
284 S1 = secret;
285 S2 = secret + slen - hs;
286
287 nb = strlen( label );
288 memcpy( tmp + 20, label, nb );
289 memcpy( tmp + 20 + nb, random, rlen );
290 nb += rlen;
291
292 /*
293 * First compute P_md5(secret,label+random)[0..dlen]
294 */
295 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
296
297 for( i = 0; i < dlen; i += 16 )
298 {
299 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
300 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
301
302 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
303
304 for( j = 0; j < k; j++ )
305 dstbuf[i + j] = h_i[j];
306 }
307
308 /*
309 * XOR out with P_sha1(secret,label+random)[0..dlen]
310 */
311 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
312
313 for( i = 0; i < dlen; i += 20 )
314 {
315 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
316 sha1_hmac( S2, hs, tmp, 20, tmp );
317
318 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
319
320 for( j = 0; j < k; j++ )
321 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
322 }
323
Paul Bakker34617722014-06-13 17:20:13 +0200324 polarssl_zeroize( tmp, sizeof( tmp ) );
325 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
327 return( 0 );
328}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200329#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200331#if defined(POLARSSL_SSL_PROTO_TLS1_2)
332#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200333static int tls_prf_sha256( const unsigned char *secret, size_t slen,
334 const char *label,
335 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000336 unsigned char *dstbuf, size_t dlen )
337{
338 size_t nb;
339 size_t i, j, k;
340 unsigned char tmp[128];
341 unsigned char h_i[32];
342
343 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
344 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
345
346 nb = strlen( label );
347 memcpy( tmp + 32, label, nb );
348 memcpy( tmp + 32 + nb, random, rlen );
349 nb += rlen;
350
351 /*
352 * Compute P_<hash>(secret, label + random)[0..dlen]
353 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200354 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000355
356 for( i = 0; i < dlen; i += 32 )
357 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200358 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
359 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000360
361 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
362
363 for( j = 0; j < k; j++ )
364 dstbuf[i + j] = h_i[j];
365 }
366
Paul Bakker34617722014-06-13 17:20:13 +0200367 polarssl_zeroize( tmp, sizeof( tmp ) );
368 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000369
370 return( 0 );
371}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200372#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker9e36f042013-06-30 14:34:05 +0200374#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200375static int tls_prf_sha384( const unsigned char *secret, size_t slen,
376 const char *label,
377 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000378 unsigned char *dstbuf, size_t dlen )
379{
380 size_t nb;
381 size_t i, j, k;
382 unsigned char tmp[128];
383 unsigned char h_i[48];
384
385 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
386 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
387
388 nb = strlen( label );
389 memcpy( tmp + 48, label, nb );
390 memcpy( tmp + 48 + nb, random, rlen );
391 nb += rlen;
392
393 /*
394 * Compute P_<hash>(secret, label + random)[0..dlen]
395 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200396 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000397
398 for( i = 0; i < dlen; i += 48 )
399 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200400 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
401 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000402
403 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
404
405 for( j = 0; j < k; j++ )
406 dstbuf[i + j] = h_i[j];
407 }
408
Paul Bakker34617722014-06-13 17:20:13 +0200409 polarssl_zeroize( tmp, sizeof( tmp ) );
410 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000411
412 return( 0 );
413}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#endif /* POLARSSL_SHA512_C */
415#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416
Paul Bakker66d5d072014-06-17 16:39:18 +0200417static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200418
419#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
420 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200421static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200422#endif
Paul Bakker380da532012-04-18 16:10:25 +0000423
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200425static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
426static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427#endif
428
429#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200430static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
431static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432#endif
433
434#if defined(POLARSSL_SSL_PROTO_TLS1_2)
435#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200436static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
437static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
438static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100440
Paul Bakker9e36f042013-06-30 14:34:05 +0200441#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200442static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
443static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
444static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100445#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200446#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000447
Paul Bakker5121ce52009-01-03 21:22:43 +0000448int ssl_derive_keys( ssl_context *ssl )
449{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200450 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000452 unsigned char keyblk[256];
453 unsigned char *key1;
454 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100455 unsigned char *mac_enc;
456 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200457 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100458 const cipher_info_t *cipher_info;
459 const md_info_t *md_info;
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 ssl_session *session = ssl->session_negotiate;
462 ssl_transform *transform = ssl->transform_negotiate;
463 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
466
Paul Bakker68884e32013-01-07 18:20:04 +0100467 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
468 if( cipher_info == NULL )
469 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200470 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100471 transform->ciphersuite_info->cipher ) );
472 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
473 }
474
475 md_info = md_info_from_type( transform->ciphersuite_info->mac );
476 if( md_info == NULL )
477 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200478 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100479 transform->ciphersuite_info->mac ) );
480 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
481 }
482
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000484 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000485 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200486#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000488 {
Paul Bakker48916f92012-09-16 19:57:18 +0000489 handshake->tls_prf = ssl3_prf;
490 handshake->calc_verify = ssl_calc_verify_ssl;
491 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000492 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200493 else
494#endif
495#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
496 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000497 {
Paul Bakker48916f92012-09-16 19:57:18 +0000498 handshake->tls_prf = tls1_prf;
499 handshake->calc_verify = ssl_calc_verify_tls;
500 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000501 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200502 else
503#endif
504#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200505#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200506 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
507 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000508 {
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf = tls_prf_sha384;
510 handshake->calc_verify = ssl_calc_verify_tls_sha384;
511 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000512 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000513 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200514#endif
515#if defined(POLARSSL_SHA256_C)
516 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000517 {
Paul Bakker48916f92012-09-16 19:57:18 +0000518 handshake->tls_prf = tls_prf_sha256;
519 handshake->calc_verify = ssl_calc_verify_tls_sha256;
520 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000521 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200522 else
523#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200524#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200525 {
526 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200527 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200528 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000529
530 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * SSLv3:
532 * master =
533 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
534 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
535 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200536 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200537 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * master = PRF( premaster, "master secret", randbytes )[0..47]
539 */
Paul Bakker0a597072012-09-25 21:55:46 +0000540 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 {
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
543 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200545#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
546 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200547 {
548 unsigned char session_hash[48];
549 size_t hash_len;
550
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200551 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200552
553 ssl->handshake->calc_verify( ssl, session_hash );
554
555#if defined(POLARSSL_SSL_PROTO_TLS1_2)
556 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
557 {
558#if defined(POLARSSL_SHA512_C)
559 if( ssl->transform_negotiate->ciphersuite_info->mac ==
560 POLARSSL_MD_SHA384 )
561 {
562 hash_len = 48;
563 }
564 else
565#endif
566 hash_len = 32;
567 }
568 else
569#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
570 hash_len = 36;
571
572 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
573
574 handshake->tls_prf( handshake->premaster, handshake->pmslen,
575 "extended master secret",
576 session_hash, hash_len, session->master, 48 );
577
578 }
579 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200580#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000581 handshake->tls_prf( handshake->premaster, handshake->pmslen,
582 "master secret",
583 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200585
Paul Bakker34617722014-06-13 17:20:13 +0200586 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 }
588 else
589 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
590
591 /*
592 * Swap the client and server random values.
593 */
Paul Bakker48916f92012-09-16 19:57:18 +0000594 memcpy( tmp, handshake->randbytes, 64 );
595 memcpy( handshake->randbytes, tmp + 32, 32 );
596 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200597 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * SSLv3:
601 * key block =
602 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
603 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
604 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
605 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
606 * ...
607 *
608 * TLSv1:
609 * key block = PRF( master, "key expansion", randbytes )
610 */
Paul Bakker48916f92012-09-16 19:57:18 +0000611 handshake->tls_prf( session->master, 48, "key expansion",
612 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
Paul Bakker48916f92012-09-16 19:57:18 +0000614 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
615 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
616 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
617 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
619
Paul Bakker34617722014-06-13 17:20:13 +0200620 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 /*
623 * Determine the appropriate key, IV and MAC length.
624 */
Paul Bakker68884e32013-01-07 18:20:04 +0100625
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200626 transform->keylen = cipher_info->key_length / 8;
627
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200628 if( cipher_info->mode == POLARSSL_MODE_GCM ||
629 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200631 transform->maclen = 0;
632
Paul Bakker68884e32013-01-07 18:20:04 +0100633 transform->ivlen = 12;
634 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200635
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200636 /* Minimum length is expicit IV + tag */
637 transform->minlen = transform->ivlen - transform->fixed_ivlen
638 + ( transform->ciphersuite_info->flags &
639 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
641 else
642 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200643 int ret;
644
645 /* Initialize HMAC contexts */
646 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
647 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100648 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200649 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
650 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100651 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200653 /* Get MAC length */
654 transform->maclen = md_get_size( md_info );
655
656#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
657 /*
658 * If HMAC is to be truncated, we shall keep the leftmost bytes,
659 * (rfc 6066 page 13 or rfc 2104 section 4),
660 * so we only need to adjust the length here.
661 */
662 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
663 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
664#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
665
666 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100667 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200669 /* Minimum length */
670 if( cipher_info->mode == POLARSSL_MODE_STREAM )
671 transform->minlen = transform->maclen;
672 else
Paul Bakker68884e32013-01-07 18:20:04 +0100673 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200674 /*
675 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100676 * 1. if EtM is in use: one block plus MAC
677 * otherwise: * first multiple of blocklen greater than maclen
678 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200679 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100680#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
681 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
682 {
683 transform->minlen = transform->maclen
684 + cipher_info->block_size;
685 }
686 else
687#endif
688 {
689 transform->minlen = transform->maclen
690 + cipher_info->block_size
691 - transform->maclen % cipher_info->block_size;
692 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200693
694#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
695 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
696 ssl->minor_ver == SSL_MINOR_VERSION_1 )
697 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100698 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200699#endif
700#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
701 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
702 ssl->minor_ver == SSL_MINOR_VERSION_3 )
703 {
704 transform->minlen += transform->ivlen;
705 }
706 else
707#endif
708 {
709 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
710 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
711 }
Paul Bakker68884e32013-01-07 18:20:04 +0100712 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 }
714
715 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000716 transform->keylen, transform->minlen, transform->ivlen,
717 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719 /*
720 * Finally setup the cipher contexts, IVs and MAC secrets.
721 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100722#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 if( ssl->endpoint == SSL_IS_CLIENT )
724 {
Paul Bakker48916f92012-09-16 19:57:18 +0000725 key1 = keyblk + transform->maclen * 2;
726 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000727
Paul Bakker68884e32013-01-07 18:20:04 +0100728 mac_enc = keyblk;
729 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000731 /*
732 * This is not used in TLS v1.1.
733 */
Paul Bakker48916f92012-09-16 19:57:18 +0000734 iv_copy_len = ( transform->fixed_ivlen ) ?
735 transform->fixed_ivlen : transform->ivlen;
736 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
737 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000738 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 }
740 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100741#endif /* POLARSSL_SSL_CLI_C */
742#if defined(POLARSSL_SSL_SRV_C)
743 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 {
Paul Bakker48916f92012-09-16 19:57:18 +0000745 key1 = keyblk + transform->maclen * 2 + transform->keylen;
746 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000747
Paul Bakker68884e32013-01-07 18:20:04 +0100748 mac_enc = keyblk + transform->maclen;
749 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000751 /*
752 * This is not used in TLS v1.1.
753 */
Paul Bakker48916f92012-09-16 19:57:18 +0000754 iv_copy_len = ( transform->fixed_ivlen ) ?
755 transform->fixed_ivlen : transform->ivlen;
756 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
757 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000758 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000759 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100760 else
761#endif /* POLARSSL_SSL_SRV_C */
762 {
763 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
764 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
765 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200767#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100768 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
769 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100770 if( transform->maclen > sizeof transform->mac_enc )
771 {
772 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200773 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100774 }
775
Paul Bakker68884e32013-01-07 18:20:04 +0100776 memcpy( transform->mac_enc, mac_enc, transform->maclen );
777 memcpy( transform->mac_dec, mac_dec, transform->maclen );
778 }
779 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200780#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200781#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
782 defined(POLARSSL_SSL_PROTO_TLS1_2)
783 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100784 {
785 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
786 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200788 else
789#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200790 {
791 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200792 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200793 }
Paul Bakker68884e32013-01-07 18:20:04 +0100794
Paul Bakker05ef8352012-05-08 09:17:57 +0000795#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200796 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000797 {
798 int ret = 0;
799
800 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
801
Paul Bakker07eb38b2012-12-19 14:42:06 +0100802 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
803 transform->iv_enc, transform->iv_dec,
804 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100805 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100806 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000807 {
808 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200809 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000810 }
811 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200812#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000813
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200814 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
815 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000816 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200817 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
818 return( ret );
819 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200820
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200821 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
822 cipher_info ) ) != 0 )
823 {
824 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
825 return( ret );
826 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200827
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200828 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
829 cipher_info->key_length,
830 POLARSSL_ENCRYPT ) ) != 0 )
831 {
832 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
833 return( ret );
834 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200835
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200836 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
837 cipher_info->key_length,
838 POLARSSL_DECRYPT ) ) != 0 )
839 {
840 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
841 return( ret );
842 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200843
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200844#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200845 if( cipher_info->mode == POLARSSL_MODE_CBC )
846 {
847 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
848 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200849 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200850 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
851 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200852 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200853
854 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
855 POLARSSL_PADDING_NONE ) ) != 0 )
856 {
857 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
858 return( ret );
859 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200861#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000862
Paul Bakker34617722014-06-13 17:20:13 +0200863 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Paul Bakker2770fbd2012-07-03 13:30:23 +0000865#if defined(POLARSSL_ZLIB_SUPPORT)
866 // Initialize compression
867 //
Paul Bakker48916f92012-09-16 19:57:18 +0000868 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000869 {
Paul Bakker16770332013-10-11 09:59:44 +0200870 if( ssl->compress_buf == NULL )
871 {
872 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
873 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
874 if( ssl->compress_buf == NULL )
875 {
876 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
877 SSL_BUFFER_LEN ) );
878 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
879 }
880 }
881
Paul Bakker2770fbd2012-07-03 13:30:23 +0000882 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
883
Paul Bakker48916f92012-09-16 19:57:18 +0000884 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
885 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000886
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200887 if( deflateInit( &transform->ctx_deflate,
888 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000889 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000890 {
891 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
892 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
893 }
894 }
895#endif /* POLARSSL_ZLIB_SUPPORT */
896
Paul Bakker5121ce52009-01-03 21:22:43 +0000897 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
898
899 return( 0 );
900}
901
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200902#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000903void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000904{
905 md5_context md5;
906 sha1_context sha1;
907 unsigned char pad_1[48];
908 unsigned char pad_2[48];
909
Paul Bakker380da532012-04-18 16:10:25 +0000910 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Paul Bakker48916f92012-09-16 19:57:18 +0000912 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
913 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Paul Bakker380da532012-04-18 16:10:25 +0000915 memset( pad_1, 0x36, 48 );
916 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
Paul Bakker48916f92012-09-16 19:57:18 +0000918 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000919 md5_update( &md5, pad_1, 48 );
920 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921
Paul Bakker380da532012-04-18 16:10:25 +0000922 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000923 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000924 md5_update( &md5, pad_2, 48 );
925 md5_update( &md5, hash, 16 );
926 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakker48916f92012-09-16 19:57:18 +0000928 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000929 sha1_update( &sha1, pad_1, 40 );
930 sha1_finish( &sha1, hash + 16 );
931
932 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000933 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000934 sha1_update( &sha1, pad_2, 40 );
935 sha1_update( &sha1, hash + 16, 20 );
936 sha1_finish( &sha1, hash + 16 );
937
938 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
939 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
940
Paul Bakker5b4af392014-06-26 12:09:34 +0200941 md5_free( &md5 );
942 sha1_free( &sha1 );
943
Paul Bakker380da532012-04-18 16:10:25 +0000944 return;
945}
Paul Bakker9af723c2014-05-01 13:03:14 +0200946#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000947
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200948#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000949void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
950{
951 md5_context md5;
952 sha1_context sha1;
953
954 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
955
Paul Bakker48916f92012-09-16 19:57:18 +0000956 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
957 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000958
Paul Bakker48916f92012-09-16 19:57:18 +0000959 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000960 sha1_finish( &sha1, hash + 16 );
961
962 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
963 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
964
Paul Bakker5b4af392014-06-26 12:09:34 +0200965 md5_free( &md5 );
966 sha1_free( &sha1 );
967
Paul Bakker380da532012-04-18 16:10:25 +0000968 return;
969}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200970#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000971
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200972#if defined(POLARSSL_SSL_PROTO_TLS1_2)
973#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000974void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
975{
Paul Bakker9e36f042013-06-30 14:34:05 +0200976 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000977
978 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
979
Paul Bakker9e36f042013-06-30 14:34:05 +0200980 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
981 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000982
983 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
984 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
985
Paul Bakker5b4af392014-06-26 12:09:34 +0200986 sha256_free( &sha256 );
987
Paul Bakker380da532012-04-18 16:10:25 +0000988 return;
989}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200990#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000991
Paul Bakker9e36f042013-06-30 14:34:05 +0200992#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000993void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
994{
Paul Bakker9e36f042013-06-30 14:34:05 +0200995 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000996
997 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
998
Paul Bakker9e36f042013-06-30 14:34:05 +0200999 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
1000 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001001
Paul Bakkerca4ab492012-04-18 14:23:57 +00001002 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1004
Paul Bakker5b4af392014-06-26 12:09:34 +02001005 sha512_free( &sha512 );
1006
Paul Bakker5121ce52009-01-03 21:22:43 +00001007 return;
1008}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009#endif /* POLARSSL_SHA512_C */
1010#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001011
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001012#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001013int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1014{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015 unsigned char *p = ssl->handshake->premaster;
1016 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1017
1018 /*
1019 * PMS = struct {
1020 * opaque other_secret<0..2^16-1>;
1021 * opaque psk<0..2^16-1>;
1022 * };
1023 * with "other_secret" depending on the particular key exchange
1024 */
1025#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1026 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1027 {
1028 if( end - p < 2 + (int) ssl->psk_len )
1029 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1030
1031 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1032 *(p++) = (unsigned char)( ssl->psk_len );
1033 p += ssl->psk_len;
1034 }
1035 else
1036#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001037#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1038 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1039 {
1040 /*
1041 * other_secret already set by the ClientKeyExchange message,
1042 * and is 48 bytes long
1043 */
1044 *p++ = 0;
1045 *p++ = 48;
1046 p += 48;
1047 }
1048 else
1049#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001050#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1051 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1052 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001053 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001054 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001055
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001056 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001057 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001058 p + 2, &len,
1059 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001060 {
1061 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1062 return( ret );
1063 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001064 *(p++) = (unsigned char)( len >> 8 );
1065 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001066 p += len;
1067
1068 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1069 }
1070 else
1071#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1072#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1073 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1074 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001075 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001076 size_t zlen;
1077
1078 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001079 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001080 ssl->f_rng, ssl->p_rng ) ) != 0 )
1081 {
1082 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1083 return( ret );
1084 }
1085
1086 *(p++) = (unsigned char)( zlen >> 8 );
1087 *(p++) = (unsigned char)( zlen );
1088 p += zlen;
1089
1090 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1091 }
1092 else
1093#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1094 {
1095 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001096 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001097 }
1098
1099 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001100 if( end - p < 2 + (int) ssl->psk_len )
1101 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1102
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001103 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1104 *(p++) = (unsigned char)( ssl->psk_len );
1105 memcpy( p, ssl->psk, ssl->psk_len );
1106 p += ssl->psk_len;
1107
1108 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1109
1110 return( 0 );
1111}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001112#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001113
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001114#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001115/*
1116 * SSLv3.0 MAC functions
1117 */
Paul Bakker68884e32013-01-07 18:20:04 +01001118static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1119 unsigned char *buf, size_t len,
1120 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121{
1122 unsigned char header[11];
1123 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001124 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001125 int md_size = md_get_size( md_ctx->md_info );
1126 int md_type = md_get_type( md_ctx->md_info );
1127
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001128 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001129 if( md_type == POLARSSL_MD_MD5 )
1130 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001131 else
Paul Bakker68884e32013-01-07 18:20:04 +01001132 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001133
1134 memcpy( header, ctr, 8 );
1135 header[ 8] = (unsigned char) type;
1136 header[ 9] = (unsigned char)( len >> 8 );
1137 header[10] = (unsigned char)( len );
1138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 memset( padding, 0x36, padlen );
1140 md_starts( md_ctx );
1141 md_update( md_ctx, secret, md_size );
1142 md_update( md_ctx, padding, padlen );
1143 md_update( md_ctx, header, 11 );
1144 md_update( md_ctx, buf, len );
1145 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 memset( padding, 0x5C, padlen );
1148 md_starts( md_ctx );
1149 md_update( md_ctx, secret, md_size );
1150 md_update( md_ctx, padding, padlen );
1151 md_update( md_ctx, buf + len, md_size );
1152 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001153}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001154#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001155
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001156#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1157 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1158 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1159#define POLARSSL_SOME_MODES_USE_MAC
1160#endif
1161
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001162/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001164 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001165static int ssl_encrypt_buf( ssl_context *ssl )
1166{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001167 cipher_mode_t mode;
1168 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
1170 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1171
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001172 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1173 {
1174 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1175 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1176 }
1177
1178 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1179
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001180 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1181 ssl->out_msg, ssl->out_msglen );
1182
Paul Bakker5121ce52009-01-03 21:22:43 +00001183 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001184 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001186#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001187 if( mode == POLARSSL_MODE_STREAM ||
1188 ( mode == POLARSSL_MODE_CBC
1189#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1190 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1191#endif
1192 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001194#if defined(POLARSSL_SSL_PROTO_SSL3)
1195 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1196 {
1197 ssl_mac( &ssl->transform_out->md_ctx_enc,
1198 ssl->transform_out->mac_enc,
1199 ssl->out_msg, ssl->out_msglen,
1200 ssl->out_ctr, ssl->out_msgtype );
1201 }
1202 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001203#endif
1204#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001205 defined(POLARSSL_SSL_PROTO_TLS1_2)
1206 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1207 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001208 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1209 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1210 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001211 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1212 ssl->out_msg, ssl->out_msglen );
1213 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1214 ssl->out_msg + ssl->out_msglen );
1215 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1216 }
1217 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001218#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001219 {
1220 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001221 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001222 }
1223
1224 SSL_DEBUG_BUF( 4, "computed mac",
1225 ssl->out_msg + ssl->out_msglen,
1226 ssl->transform_out->maclen );
1227
1228 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001229 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001230 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001231#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001232
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001233 /*
1234 * Encrypt
1235 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001236#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001237 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001239 int ret;
1240 size_t olen = 0;
1241
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1243 "including %d bytes of padding",
1244 ssl->out_msglen, 0 ) );
1245
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001246 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001247 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001248 ssl->transform_out->ivlen,
1249 ssl->out_msg, ssl->out_msglen,
1250 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001251 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001252 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001253 return( ret );
1254 }
1255
1256 if( ssl->out_msglen != olen )
1257 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001258 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001259 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001260 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 }
Paul Bakker68884e32013-01-07 18:20:04 +01001262 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001263#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1265 if( mode == POLARSSL_MODE_GCM ||
1266 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001268 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001269 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001270 unsigned char *enc_msg;
1271 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001272 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1273 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001274
Paul Bakkerca4ab492012-04-18 14:23:57 +00001275 memcpy( add_data, ssl->out_ctr, 8 );
1276 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001277 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1278 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001279 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1280 add_data[12] = ssl->out_msglen & 0xFF;
1281
1282 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1283 add_data, 13 );
1284
Paul Bakker68884e32013-01-07 18:20:04 +01001285 /*
1286 * Generate IV
1287 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001288#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001289 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001290 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1291 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001292 if( ret != 0 )
1293 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001294
Paul Bakker68884e32013-01-07 18:20:04 +01001295 memcpy( ssl->out_iv,
1296 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1297 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001298#else
1299 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1300 {
1301 /* Reminder if we ever add an AEAD mode with a different size */
1302 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1303 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1304 }
1305
1306 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1307 ssl->out_ctr, 8 );
1308 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1309#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001310
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001311 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001312 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001313
Paul Bakker68884e32013-01-07 18:20:04 +01001314 /*
1315 * Fix pointer positions and message length with added IV
1316 */
1317 enc_msg = ssl->out_msg;
1318 enc_msglen = ssl->out_msglen;
1319 ssl->out_msglen += ssl->transform_out->ivlen -
1320 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001321
Paul Bakker68884e32013-01-07 18:20:04 +01001322 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1323 "including %d bytes of padding",
1324 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001325
Paul Bakker68884e32013-01-07 18:20:04 +01001326 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001327 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001328 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001329 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1330 ssl->transform_out->iv_enc,
1331 ssl->transform_out->ivlen,
1332 add_data, 13,
1333 enc_msg, enc_msglen,
1334 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001335 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001336 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001337 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001338 return( ret );
1339 }
1340
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001341 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001342 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001343 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001344 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001345 }
1346
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001347 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001348 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001349
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001350 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001353#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001354#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1355 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001356 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001358 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001359 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001360 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001361
Paul Bakker48916f92012-09-16 19:57:18 +00001362 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1363 ssl->transform_out->ivlen;
1364 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 padlen = 0;
1366
1367 for( i = 0; i <= padlen; i++ )
1368 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1369
1370 ssl->out_msglen += padlen + 1;
1371
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001372 enc_msglen = ssl->out_msglen;
1373 enc_msg = ssl->out_msg;
1374
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001375#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001376 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001377 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1378 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001379 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001380 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001381 {
1382 /*
1383 * Generate IV
1384 */
Paul Bakker48916f92012-09-16 19:57:18 +00001385 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1386 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001387 if( ret != 0 )
1388 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001389
Paul Bakker92be97b2013-01-02 17:30:03 +01001390 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001391 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001392
1393 /*
1394 * Fix pointer positions and message length with added IV
1395 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001396 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001397 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001398 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001399 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001400#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001401
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001403 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001404 ssl->out_msglen, ssl->transform_out->ivlen,
1405 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001407 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001408 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001409 ssl->transform_out->ivlen,
1410 enc_msg, enc_msglen,
1411 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001412 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001413 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001414 return( ret );
1415 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001416
Paul Bakkercca5b812013-08-31 17:40:26 +02001417 if( enc_msglen != olen )
1418 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001419 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001420 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001421 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001422
1423#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001424 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1425 {
1426 /*
1427 * Save IV in SSL3 and TLS1
1428 */
1429 memcpy( ssl->transform_out->iv_enc,
1430 ssl->transform_out->cipher_ctx_enc.iv,
1431 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001433#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001434
1435#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001436 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001437 {
1438 /*
1439 * MAC(MAC_write_key, seq_num +
1440 * TLSCipherText.type +
1441 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001442 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001443 * IV + // except for TLS 1.0
1444 * ENC(content + padding + padding_length));
1445 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001446 unsigned char pseudo_hdr[13];
1447
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001448 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1449
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001450 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1451 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001452 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1453 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1454
1455 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001456
1457 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1458 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1459 ssl->out_iv, ssl->out_msglen );
1460 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1461 ssl->out_iv + ssl->out_msglen );
1462 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1463
1464 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001465 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001466 }
1467#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001468 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001469 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001470#endif /* POLARSSL_CIPHER_MODE_CBC &&
1471 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001472 {
1473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001474 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001477 /* Make extra sure authentication was performed, exactly once */
1478 if( auth_done != 1 )
1479 {
1480 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1481 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1482 }
1483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1485
1486 return( 0 );
1487}
1488
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001489#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001490
Paul Bakker5121ce52009-01-03 21:22:43 +00001491static int ssl_decrypt_buf( ssl_context *ssl )
1492{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001493 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001494 cipher_mode_t mode;
1495 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001496#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001497 size_t padlen = 0, correct = 1;
1498#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001499
1500 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1501
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001502 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1503 {
1504 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1505 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1506 }
1507
1508 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1509
Paul Bakker48916f92012-09-16 19:57:18 +00001510 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001511 {
1512 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001513 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001514 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 }
1516
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001517#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001518 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001519 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001520 int ret;
1521 size_t olen = 0;
1522
Paul Bakker68884e32013-01-07 18:20:04 +01001523 padlen = 0;
1524
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001525 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001526 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001527 ssl->transform_in->ivlen,
1528 ssl->in_msg, ssl->in_msglen,
1529 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001530 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001531 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001532 return( ret );
1533 }
1534
1535 if( ssl->in_msglen != olen )
1536 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001537 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001538 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 }
Paul Bakker68884e32013-01-07 18:20:04 +01001541 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001542#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001543#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1544 if( mode == POLARSSL_MODE_GCM ||
1545 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001546 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001547 int ret;
1548 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001549 unsigned char *dec_msg;
1550 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001552 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1553 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001554 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1555 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001556
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001557 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001558 {
1559 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1560 "+ taglen (%d)", ssl->in_msglen,
1561 explicit_iv_len, taglen ) );
1562 return( POLARSSL_ERR_SSL_INVALID_MAC );
1563 }
1564 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1565
Paul Bakker68884e32013-01-07 18:20:04 +01001566 dec_msg = ssl->in_msg;
1567 dec_msg_result = ssl->in_msg;
1568 ssl->in_msglen = dec_msglen;
1569
1570 memcpy( add_data, ssl->in_ctr, 8 );
1571 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001572 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1573 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001574 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1575 add_data[12] = ssl->in_msglen & 0xFF;
1576
1577 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1578 add_data, 13 );
1579
1580 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1581 ssl->in_iv,
1582 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1583
1584 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1585 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001586 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001587
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001588 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001589 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001590 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1592 ssl->transform_in->iv_dec,
1593 ssl->transform_in->ivlen,
1594 add_data, 13,
1595 dec_msg, dec_msglen,
1596 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001597 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001598 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001599 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1600
1601 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1602 return( POLARSSL_ERR_SSL_INVALID_MAC );
1603
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001604 return( ret );
1605 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001606 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001607
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001608 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001609 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001610 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001611 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001612 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001613 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001615#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001616#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1617 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001618 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 {
Paul Bakker45829992013-01-03 14:52:21 +01001620 /*
1621 * Decrypt and check the padding
1622 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001623 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001624 unsigned char *dec_msg;
1625 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001626 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001627 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001628 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001629
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 /*
Paul Bakker45829992013-01-03 14:52:21 +01001631 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001633#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001634 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1635 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636#endif
Paul Bakker45829992013-01-03 14:52:21 +01001637
1638 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1639 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1640 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001641 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1642 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1643 ssl->transform_in->ivlen,
1644 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001645 return( POLARSSL_ERR_SSL_INVALID_MAC );
1646 }
1647
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 dec_msglen = ssl->in_msglen;
1649 dec_msg = ssl->in_msg;
1650 dec_msg_result = ssl->in_msg;
1651
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001652 /*
1653 * Authenticate before decrypt if enabled
1654 */
1655#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001656 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001657 {
1658 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001659 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001660
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001661 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1662
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001663 dec_msglen -= ssl->transform_in->maclen;
1664 ssl->in_msglen -= ssl->transform_in->maclen;
1665
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001666 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1667 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1668 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1669 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1670
1671 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1672
1673 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001674 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1675 ssl->in_iv, ssl->in_msglen );
1676 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1677 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1678
1679 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1680 ssl->transform_in->maclen );
1681 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1682 ssl->transform_in->maclen );
1683
1684 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1685 ssl->transform_in->maclen ) != 0 )
1686 {
1687 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1688
1689 return( POLARSSL_ERR_SSL_INVALID_MAC );
1690 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001691 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001692 }
1693#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1694
1695 /*
1696 * Check length sanity
1697 */
1698 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1699 {
1700 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1701 ssl->in_msglen, ssl->transform_in->ivlen ) );
1702 return( POLARSSL_ERR_SSL_INVALID_MAC );
1703 }
1704
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001705#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001706 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001707 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001709 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001710 {
Paul Bakker48916f92012-09-16 19:57:18 +00001711 dec_msglen -= ssl->transform_in->ivlen;
1712 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001713
Paul Bakker48916f92012-09-16 19:57:18 +00001714 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001715 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001716 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001717#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001718
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001719 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001720 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001721 ssl->transform_in->ivlen,
1722 dec_msg, dec_msglen,
1723 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001724 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001725 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001726 return( ret );
1727 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001728
Paul Bakkercca5b812013-08-31 17:40:26 +02001729 if( dec_msglen != olen )
1730 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001731 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001732 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001734
1735#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001736 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1737 {
1738 /*
1739 * Save IV in SSL3 and TLS1
1740 */
1741 memcpy( ssl->transform_in->iv_dec,
1742 ssl->transform_in->cipher_ctx_dec.iv,
1743 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001745#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
1747 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001748
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001749 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001750 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001751 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001752#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001753 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1754 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001755#endif
Paul Bakker45829992013-01-03 14:52:21 +01001756 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001757 correct = 0;
1758 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001760#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1762 {
Paul Bakker48916f92012-09-16 19:57:18 +00001763 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001765#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1767 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001768 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001769#endif
Paul Bakker45829992013-01-03 14:52:21 +01001770 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 }
1772 }
1773 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001774#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001775#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1776 defined(POLARSSL_SSL_PROTO_TLS1_2)
1777 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 {
1779 /*
Paul Bakker45829992013-01-03 14:52:21 +01001780 * TLSv1+: always check the padding up to the first failure
1781 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001782 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001783 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001784 size_t padding_idx = ssl->in_msglen - padlen - 1;
1785
Paul Bakker956c9e02013-12-19 14:42:28 +01001786 /*
1787 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001788 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001789 *
Paul Bakker61885c72014-04-25 12:59:03 +02001790 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1791 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001792 *
1793 * In both cases we reset padding_idx to a safe value (0) to
1794 * prevent out-of-buffer reads.
1795 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001796 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001797 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1798 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001799
1800 padding_idx *= correct;
1801
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001802 for( i = 1; i <= 256; i++ )
1803 {
1804 real_count &= ( i <= padlen );
1805 pad_count += real_count *
1806 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1807 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001808
1809 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001810
Paul Bakkerd66f0702013-01-31 16:57:45 +01001811#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001812 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001813 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001814#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001815 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001817 else
1818#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1819 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001820 {
1821 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001822 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001823 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001824
1825 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001827 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001828#endif /* POLARSSL_CIPHER_MODE_CBC &&
1829 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001830 {
1831 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001832 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001833 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001834
1835 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1836 ssl->in_msg, ssl->in_msglen );
1837
1838 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001839 * Authenticate if not done yet.
1840 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001841 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001842#if defined(POLARSSL_SOME_MODES_USE_MAC)
1843 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001845 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1846
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001847 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001849 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1850 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001852 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001854#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001855 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1856 {
1857 ssl_mac( &ssl->transform_in->md_ctx_dec,
1858 ssl->transform_in->mac_dec,
1859 ssl->in_msg, ssl->in_msglen,
1860 ssl->in_ctr, ssl->in_msgtype );
1861 }
1862 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001863#endif /* POLARSSL_SSL_PROTO_SSL3 */
1864#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001865 defined(POLARSSL_SSL_PROTO_TLS1_2)
1866 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1867 {
1868 /*
1869 * Process MAC and always update for padlen afterwards to make
1870 * total time independent of padlen
1871 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001872 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001873 *
1874 * Known timing attacks:
1875 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1876 *
1877 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1878 * correctly. (We round down instead of up, so -56 is the correct
1879 * value for our calculations instead of -55)
1880 */
1881 size_t j, extra_run = 0;
1882 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1883 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001884
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001885 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001886
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001887 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1888 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1889 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001890 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1891 ssl->in_msglen );
1892 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1893 ssl->in_msg + ssl->in_msglen );
1894 for( j = 0; j < extra_run; j++ )
1895 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001896
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001897 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1898 }
1899 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001900#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001901 POLARSSL_SSL_PROTO_TLS1_2 */
1902 {
1903 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001904 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001905 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001906
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001907 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1908 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1909 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001911 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001912 ssl->transform_in->maclen ) != 0 )
1913 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001914#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001915 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001916#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001917 correct = 0;
1918 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001919 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001921 /*
1922 * Finally check the correct flag
1923 */
1924 if( correct == 0 )
1925 return( POLARSSL_ERR_SSL_INVALID_MAC );
1926 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001927#endif /* POLARSSL_SOME_MODES_USE_MAC */
1928
1929 /* Make extra sure authentication was performed, exactly once */
1930 if( auth_done != 1 )
1931 {
1932 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1933 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
1936 if( ssl->in_msglen == 0 )
1937 {
1938 ssl->nb_zero++;
1939
1940 /*
1941 * Three or more empty messages may be a DoS attack
1942 * (excessive CPU consumption).
1943 */
1944 if( ssl->nb_zero > 3 )
1945 {
1946 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1947 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001948 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 }
1950 }
1951 else
1952 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001953
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001954#if defined(POLARSSL_SSL_PROTO_DTLS)
1955 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001956 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001957 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001958 }
1959 else
1960#endif
1961 {
1962 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1963 if( ++ssl->in_ctr[i - 1] != 0 )
1964 break;
1965
1966 /* The loop goes to its end iff the counter is wrapping */
1967 if( i == ssl_ep_len( ssl ) )
1968 {
1969 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1970 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1971 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001972 }
1973
Paul Bakker5121ce52009-01-03 21:22:43 +00001974 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1975
1976 return( 0 );
1977}
1978
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001979#undef MAC_NONE
1980#undef MAC_PLAINTEXT
1981#undef MAC_CIPHERTEXT
1982
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983#if defined(POLARSSL_ZLIB_SUPPORT)
1984/*
1985 * Compression/decompression functions
1986 */
1987static int ssl_compress_buf( ssl_context *ssl )
1988{
1989 int ret;
1990 unsigned char *msg_post = ssl->out_msg;
1991 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001992 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001993
1994 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1995
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001996 if( len_pre == 0 )
1997 return( 0 );
1998
Paul Bakker2770fbd2012-07-03 13:30:23 +00001999 memcpy( msg_pre, ssl->out_msg, len_pre );
2000
2001 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2002 ssl->out_msglen ) );
2003
2004 SSL_DEBUG_BUF( 4, "before compression: output payload",
2005 ssl->out_msg, ssl->out_msglen );
2006
Paul Bakker48916f92012-09-16 19:57:18 +00002007 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2008 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2009 ssl->transform_out->ctx_deflate.next_out = msg_post;
2010 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002011
Paul Bakker48916f92012-09-16 19:57:18 +00002012 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002013 if( ret != Z_OK )
2014 {
2015 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2016 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2017 }
2018
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002019 ssl->out_msglen = SSL_BUFFER_LEN -
2020 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002021
Paul Bakker2770fbd2012-07-03 13:30:23 +00002022 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2023 ssl->out_msglen ) );
2024
2025 SSL_DEBUG_BUF( 4, "after compression: output payload",
2026 ssl->out_msg, ssl->out_msglen );
2027
2028 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2029
2030 return( 0 );
2031}
2032
2033static int ssl_decompress_buf( ssl_context *ssl )
2034{
2035 int ret;
2036 unsigned char *msg_post = ssl->in_msg;
2037 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002038 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002039
2040 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2041
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002042 if( len_pre == 0 )
2043 return( 0 );
2044
Paul Bakker2770fbd2012-07-03 13:30:23 +00002045 memcpy( msg_pre, ssl->in_msg, len_pre );
2046
2047 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2048 ssl->in_msglen ) );
2049
2050 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2051 ssl->in_msg, ssl->in_msglen );
2052
Paul Bakker48916f92012-09-16 19:57:18 +00002053 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2054 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2055 ssl->transform_in->ctx_inflate.next_out = msg_post;
2056 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002057
Paul Bakker48916f92012-09-16 19:57:18 +00002058 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002059 if( ret != Z_OK )
2060 {
2061 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2062 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2063 }
2064
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002065 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2066 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002067
Paul Bakker2770fbd2012-07-03 13:30:23 +00002068 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2069 ssl->in_msglen ) );
2070
2071 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2072 ssl->in_msg, ssl->in_msglen );
2073
2074 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2075
2076 return( 0 );
2077}
2078#endif /* POLARSSL_ZLIB_SUPPORT */
2079
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002080#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002081static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002082
2083#if defined(POLARSSL_SSL_PROTO_DTLS)
2084static int ssl_resend_hello_request( ssl_context *ssl )
2085{
2086 /* If renegotiation is not enforced, retransmit until we would reach max
2087 * timeout if we were using the usual handshake doubling scheme */
2088 if( ssl->renego_max_records < 0 )
2089 {
2090 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2091 unsigned char doublings = 1;
2092
2093 while( ratio != 0 )
2094 {
2095 ++doublings;
2096 ratio >>= 1;
2097 }
2098
2099 if( ++ssl->renego_records_seen > doublings )
2100 {
2101 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2102 return( 0 );
2103 }
2104 }
2105
2106 return( ssl_write_hello_request( ssl ) );
2107}
2108#endif
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002109#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002110
Paul Bakker5121ce52009-01-03 21:22:43 +00002111/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002112 * Fill the input message buffer by appending data to it.
2113 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002114 *
2115 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2116 * available (from this read and/or a previous one). Otherwise, an error code
2117 * is returned (possibly EOF or WANT_READ).
2118 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002119 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2120 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2121 * since we always read a whole datagram at once.
2122 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002123 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002124 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002125 */
Paul Bakker23986e52011-04-24 08:57:21 +00002126int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127{
Paul Bakker23986e52011-04-24 08:57:21 +00002128 int ret;
2129 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130
2131 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2132
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002133 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2134 {
2135 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2136 "or ssl_set_bio_timeout()" ) );
2137 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2138 }
2139
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002140 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002141 {
2142 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2144 }
2145
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002146#if defined(POLARSSL_SSL_PROTO_DTLS)
2147 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002149 uint32_t timeout;
2150
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002151 /*
2152 * The point is, we need to always read a full datagram at once, so we
2153 * sometimes read more then requested, and handle the additional data.
2154 * It could be the rest of the current record (while fetching the
2155 * header) and/or some other records in the same datagram.
2156 */
2157
2158 /*
2159 * Move to the next record in the already read datagram if applicable
2160 */
2161 if( ssl->next_record_offset != 0 )
2162 {
2163 if( ssl->in_left < ssl->next_record_offset )
2164 {
2165 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2166 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2167 }
2168
2169 ssl->in_left -= ssl->next_record_offset;
2170
2171 if( ssl->in_left != 0 )
2172 {
2173 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2174 ssl->next_record_offset ) );
2175 memmove( ssl->in_hdr,
2176 ssl->in_hdr + ssl->next_record_offset,
2177 ssl->in_left );
2178 }
2179
2180 ssl->next_record_offset = 0;
2181 }
2182
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2184 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002185
2186 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002187 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002188 */
2189 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002190 {
2191 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002192 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002193 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002194
2195 /*
2196 * A record can't be split accross datagrams. If we need to read but
2197 * are not at the beginning of a new record, the caller did something
2198 * wrong.
2199 */
2200 if( ssl->in_left != 0 )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2203 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2204 }
2205
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002206 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002207
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002208 /*
2209 * Don't even try to read if time's out already.
2210 * This avoids by-passing the timer when repeatedly receiving messages
2211 * that will end up being dropped.
2212 */
2213 if( ssl_check_timer( ssl ) != 0 )
2214 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002215 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002216 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002217 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2218
2219 if( ssl->state != SSL_HANDSHAKE_OVER )
2220 timeout = ssl->handshake->retransmit_timeout;
2221 else
2222 timeout = ssl->read_timeout;
2223
2224 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2225
2226 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2227 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2228 timeout );
2229 else
2230 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2231
2232 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2233
2234 if( ret == 0 )
2235 return( POLARSSL_ERR_SSL_CONN_EOF );
2236 }
2237
2238 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2239 {
2240 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2241 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002242
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002243 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002244 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002245 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2246 {
2247 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2248 return( POLARSSL_ERR_NET_TIMEOUT );
2249 }
2250
2251 if( ( ret = ssl_resend( ssl ) ) != 0 )
2252 {
2253 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2254 return( ret );
2255 }
2256
2257 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002258 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002259#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002260 else if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002261 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002262 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002263 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002264 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002265 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002266 return( ret );
2267 }
2268
2269 return( POLARSSL_ERR_NET_WANT_READ );
2270 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00002271#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002272 }
2273
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 if( ret < 0 )
2275 return( ret );
2276
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002277 ssl->in_left = ret;
2278 }
2279 else
2280#endif
2281 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002282 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2283 ssl->in_left, nb_want ) );
2284
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002285 while( ssl->in_left < nb_want )
2286 {
2287 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002288 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002289
2290 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2291 ssl->in_left, nb_want ) );
2292 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2293
2294 if( ret == 0 )
2295 return( POLARSSL_ERR_SSL_CONN_EOF );
2296
2297 if( ret < 0 )
2298 return( ret );
2299
2300 ssl->in_left += ret;
2301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002302 }
2303
2304 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2305
2306 return( 0 );
2307}
2308
2309/*
2310 * Flush any data not yet written
2311 */
2312int ssl_flush_output( ssl_context *ssl )
2313{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002314 int ret;
2315 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2318
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002319 if( ssl->f_send == NULL )
2320 {
2321 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2322 "or ssl_set_bio_timeout()" ) );
2323 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2324 }
2325
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002326 /* Avoid incrementing counter if data is flushed */
2327 if( ssl->out_left == 0 )
2328 {
2329 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2330 return( 0 );
2331 }
2332
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 while( ssl->out_left > 0 )
2334 {
2335 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002336 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002338 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2339 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002340 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002341
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2343
2344 if( ret <= 0 )
2345 return( ret );
2346
2347 ssl->out_left -= ret;
2348 }
2349
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002350 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002351 if( ++ssl->out_ctr[i - 1] != 0 )
2352 break;
2353
2354 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002355 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002356 {
2357 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2358 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2359 }
2360
Paul Bakker5121ce52009-01-03 21:22:43 +00002361 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2362
2363 return( 0 );
2364}
2365
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002366/*
2367 * Functions to handle the DTLS retransmission state machine
2368 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002369#if defined(POLARSSL_SSL_PROTO_DTLS)
2370/*
2371 * Append current handshake message to current outgoing flight
2372 */
2373static int ssl_flight_append( ssl_context *ssl )
2374{
2375 ssl_flight_item *msg;
2376
2377 /* Allocate space for current message */
2378 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2379 {
2380 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2381 sizeof( ssl_flight_item ) ) );
2382 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2383 }
2384
2385 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2386 {
2387 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002388 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002389 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2390 }
2391
2392 /* Copy current handshake message with headers */
2393 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2394 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002395 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002396 msg->next = NULL;
2397
2398 /* Append to the current flight */
2399 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002400 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002401 else
2402 {
2403 ssl_flight_item *cur = ssl->handshake->flight;
2404 while( cur->next != NULL )
2405 cur = cur->next;
2406 cur->next = msg;
2407 }
2408
2409 return( 0 );
2410}
2411
2412/*
2413 * Free the current flight of handshake messages
2414 */
2415static void ssl_flight_free( ssl_flight_item *flight )
2416{
2417 ssl_flight_item *cur = flight;
2418 ssl_flight_item *next;
2419
2420 while( cur != NULL )
2421 {
2422 next = cur->next;
2423
2424 polarssl_free( cur->p );
2425 polarssl_free( cur );
2426
2427 cur = next;
2428 }
2429}
2430
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002431#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2432static void ssl_dtls_replay_reset( ssl_context *ssl );
2433#endif
2434
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002435/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002436 * Swap transform_out and out_ctr with the alternative ones
2437 */
2438static void ssl_swap_epochs( ssl_context *ssl )
2439{
2440 ssl_transform *tmp_transform;
2441 unsigned char tmp_out_ctr[8];
2442
2443 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2444 {
2445 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2446 return;
2447 }
2448
2449 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2450
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002451 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002452 tmp_transform = ssl->transform_out;
2453 ssl->transform_out = ssl->handshake->alt_transform_out;
2454 ssl->handshake->alt_transform_out = tmp_transform;
2455
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002456 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002457 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2458 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2459 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002460
2461 /* Adjust to the newly activated transform */
2462 if( ssl->transform_out != NULL &&
2463 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2464 {
2465 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2466 ssl->transform_out->fixed_ivlen;
2467 }
2468 else
2469 ssl->out_msg = ssl->out_iv;
2470
2471#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2472 if( ssl_hw_record_activate != NULL )
2473 {
2474 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2475 {
2476 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2477 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2478 }
2479 }
2480#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002481}
2482
2483/*
2484 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002485 *
2486 * Need to remember the current message in case flush_output returns
2487 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002488 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002489 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002490int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002491{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002492 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002493
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002494 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2495 {
2496 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2497
2498 ssl->handshake->cur_msg = ssl->handshake->flight;
2499 ssl_swap_epochs( ssl );
2500
2501 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2502 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002503
2504 while( ssl->handshake->cur_msg != NULL )
2505 {
2506 int ret;
2507 ssl_flight_item *cur = ssl->handshake->cur_msg;
2508
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002509 /* Swap epochs before sending Finished: we can't do it after
2510 * sending ChangeCipherSpec, in case write returns WANT_READ.
2511 * Must be done before copying, may change out_msg pointer */
2512 if( cur->type == SSL_MSG_HANDSHAKE &&
2513 cur->p[0] == SSL_HS_FINISHED )
2514 {
2515 ssl_swap_epochs( ssl );
2516 }
2517
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002518 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002519 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002520 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002521
2522 ssl->handshake->cur_msg = cur->next;
2523
2524 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2525
2526 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2527 {
2528 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2529 return( ret );
2530 }
2531 }
2532
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002533 if( ssl->state == SSL_HANDSHAKE_OVER )
2534 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2535 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002536 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002537 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002538 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2539 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002540
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002541 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002542
2543 return( 0 );
2544}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002545
2546/*
2547 * To be called when the last message of an incoming flight is received.
2548 */
2549void ssl_recv_flight_completed( ssl_context *ssl )
2550{
2551 /* We won't need to resend that one any more */
2552 ssl_flight_free( ssl->handshake->flight );
2553 ssl->handshake->flight = NULL;
2554 ssl->handshake->cur_msg = NULL;
2555
2556 /* The next incoming flight will start with this msg_seq */
2557 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2558
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002559 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002560 ssl_set_timer( ssl, 0 );
2561
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002562 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2563 ssl->in_msg[0] == SSL_HS_FINISHED )
2564 {
2565 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2566 }
2567 else
2568 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2569}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002570
2571/*
2572 * To be called when the last message of an outgoing flight is send.
2573 */
2574void ssl_send_flight_completed( ssl_context *ssl )
2575{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002576 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002577 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002578
2579 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2580 ssl->in_msg[0] == SSL_HS_FINISHED )
2581 {
2582 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2583 }
2584 else
2585 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2586}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002587#endif /* POLARSSL_SSL_PROTO_DTLS */
2588
Paul Bakker5121ce52009-01-03 21:22:43 +00002589/*
2590 * Record layer functions
2591 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002592
2593/*
2594 * Write current record.
2595 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2596 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002597int ssl_write_record( ssl_context *ssl )
2598{
Paul Bakker05ef8352012-05-08 09:17:57 +00002599 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002600 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2603
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002604#if defined(POLARSSL_SSL_PROTO_DTLS)
2605 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2606 ssl->handshake != NULL &&
2607 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2608 {
2609 ; /* Skip special handshake treatment when resending */
2610 }
2611 else
2612#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2614 {
2615 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2616 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2617 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2618
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002619 /*
2620 * DTLS has additional fields in the Handshake layer,
2621 * between the length field and the actual payload:
2622 * uint16 message_seq;
2623 * uint24 fragment_offset;
2624 * uint24 fragment_length;
2625 */
2626#if defined(POLARSSL_SSL_PROTO_DTLS)
2627 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2628 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002629 /* Make room for the additional DTLS fields */
2630 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002631 ssl->out_msglen += 8;
2632 len += 8;
2633
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002634 /* Write message_seq and update it, except for HelloRequest */
2635 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2636 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002637 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2638 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2639 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002640 }
2641 else
2642 {
2643 ssl->out_msg[4] = 0;
2644 ssl->out_msg[5] = 0;
2645 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002646
2647 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2648 memset( ssl->out_msg + 6, 0x00, 3 );
2649 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002650 }
2651#endif /* POLARSSL_SSL_PROTO_DTLS */
2652
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002653 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2654 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002657 /* Save handshake and CCS messages for resending */
2658#if defined(POLARSSL_SSL_PROTO_DTLS)
2659 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2660 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002661 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002662 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2663 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2664 {
2665 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2666 {
2667 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2668 return( ret );
2669 }
2670 }
2671#endif
2672
Paul Bakker2770fbd2012-07-03 13:30:23 +00002673#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002674 if( ssl->transform_out != NULL &&
2675 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002676 {
2677 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2678 {
2679 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2680 return( ret );
2681 }
2682
2683 len = ssl->out_msglen;
2684 }
2685#endif /*POLARSSL_ZLIB_SUPPORT */
2686
Paul Bakker05ef8352012-05-08 09:17:57 +00002687#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002688 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002690 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691
Paul Bakker05ef8352012-05-08 09:17:57 +00002692 ret = ssl_hw_record_write( ssl );
2693 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2694 {
2695 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002696 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002697 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002698
2699 if( ret == 0 )
2700 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002701 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002702#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002703 if( !done )
2704 {
2705 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002706 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2707 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002708
2709 ssl->out_len[0] = (unsigned char)( len >> 8 );
2710 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002711
Paul Bakker48916f92012-09-16 19:57:18 +00002712 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002713 {
2714 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2715 {
2716 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2717 return( ret );
2718 }
2719
2720 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002721 ssl->out_len[0] = (unsigned char)( len >> 8 );
2722 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002723 }
2724
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002725 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002726
2727 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2728 "version = [%d:%d], msglen = %d",
2729 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002730 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002731
2732 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002733 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2737 {
2738 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2739 return( ret );
2740 }
2741
2742 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2743
2744 return( 0 );
2745}
2746
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002747#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002748/*
2749 * Mark bits in bitmask (used for DTLS HS reassembly)
2750 */
2751static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2752{
2753 unsigned int start_bits, end_bits;
2754
2755 start_bits = 8 - ( offset % 8 );
2756 if( start_bits != 8 )
2757 {
2758 size_t first_byte_idx = offset / 8;
2759
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002760 /* Special case */
2761 if( len <= start_bits )
2762 {
2763 for( ; len != 0; len-- )
2764 mask[first_byte_idx] |= 1 << ( start_bits - len );
2765
2766 /* Avoid potential issues with offset or len becoming invalid */
2767 return;
2768 }
2769
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002770 offset += start_bits; /* Now offset % 8 == 0 */
2771 len -= start_bits;
2772
2773 for( ; start_bits != 0; start_bits-- )
2774 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2775 }
2776
2777 end_bits = len % 8;
2778 if( end_bits != 0 )
2779 {
2780 size_t last_byte_idx = ( offset + len ) / 8;
2781
2782 len -= end_bits; /* Now len % 8 == 0 */
2783
2784 for( ; end_bits != 0; end_bits-- )
2785 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2786 }
2787
2788 memset( mask + offset / 8, 0xFF, len / 8 );
2789}
2790
2791/*
2792 * Check that bitmask is full
2793 */
2794static int ssl_bitmask_check( unsigned char *mask, size_t len )
2795{
2796 size_t i;
2797
2798 for( i = 0; i < len / 8; i++ )
2799 if( mask[i] != 0xFF )
2800 return( -1 );
2801
2802 for( i = 0; i < len % 8; i++ )
2803 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2804 return( -1 );
2805
2806 return( 0 );
2807}
2808
2809/*
2810 * Reassemble fragmented DTLS handshake messages.
2811 *
2812 * Use a temporary buffer for reassembly, divided in two parts:
2813 * - the first holds the reassembled message (including handshake header),
2814 * - the second holds a bitmask indicating which parts of the message
2815 * (excluding headers) have been received so far.
2816 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002817static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2818{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002819 unsigned char *msg, *bitmask;
2820 size_t frag_len, frag_off;
2821 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2822
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002823 if( ssl->handshake == NULL )
2824 {
2825 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2826 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2827 }
2828
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002829 /*
2830 * For first fragment, check size and allocate buffer
2831 */
2832 if( ssl->handshake->hs_msg == NULL )
2833 {
2834 size_t alloc_len;
2835
2836 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2837 msg_len ) );
2838
2839 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2840 {
2841 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2842 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2843 }
2844
2845 /* The bitmask needs one bit per byte of message excluding header */
2846 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2847
2848 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2849 if( ssl->handshake->hs_msg == NULL )
2850 {
2851 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2852 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2853 }
2854
2855 memset( ssl->handshake->hs_msg, 0, alloc_len );
2856
2857 /* Prepare final header: copy msg_type, length and message_seq,
2858 * then add standardised fragment_offset and fragment_length */
2859 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2860 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2861 memcpy( ssl->handshake->hs_msg + 9,
2862 ssl->handshake->hs_msg + 1, 3 );
2863 }
2864 else
2865 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002866 /* Make sure msg_type and length are consistent */
2867 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002868 {
2869 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2870 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2871 }
2872 }
2873
2874 msg = ssl->handshake->hs_msg + 12;
2875 bitmask = msg + msg_len;
2876
2877 /*
2878 * Check and copy current fragment
2879 */
2880 frag_off = ( ssl->in_msg[6] << 16 ) |
2881 ( ssl->in_msg[7] << 8 ) |
2882 ssl->in_msg[8];
2883 frag_len = ( ssl->in_msg[9] << 16 ) |
2884 ( ssl->in_msg[10] << 8 ) |
2885 ssl->in_msg[11];
2886
2887 if( frag_off + frag_len > msg_len )
2888 {
2889 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2890 frag_off, frag_len, msg_len ) );
2891 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2892 }
2893
2894 if( frag_len + 12 > ssl->in_msglen )
2895 {
2896 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2897 frag_len, ssl->in_msglen ) );
2898 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2899 }
2900
2901 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2902 frag_off, frag_len ) );
2903
2904 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2905 ssl_bitmask_set( bitmask, frag_off, frag_len );
2906
2907 /*
2908 * Do we have the complete message by now?
2909 * If yes, finalize it, else ask to read the next record.
2910 */
2911 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2912 {
2913 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2914 return( POLARSSL_ERR_NET_WANT_READ );
2915 }
2916
2917 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2918
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002919 if( frag_len + 12 < ssl->in_msglen )
2920 {
2921 /*
2922 * We'got more handshake messages in the same record.
2923 * This case is not handled now because no know implementation does
2924 * that and it's hard to test, so we prefer to fail cleanly for now.
2925 */
2926 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2927 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2928 }
2929
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002930 if( ssl->in_left > ssl->next_record_offset )
2931 {
2932 /*
2933 * We've got more data in the buffer after the current record,
2934 * that we don't want to overwrite. Move it before writing the
2935 * reassembled message, and adjust in_left and next_record_offset.
2936 */
2937 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2938 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2939 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2940
2941 /* First compute and check new lengths */
2942 ssl->next_record_offset = new_remain - ssl->in_hdr;
2943 ssl->in_left = ssl->next_record_offset + remain_len;
2944
2945 if( ssl->in_left > SSL_BUFFER_LEN -
2946 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2947 {
2948 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2949 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2950 }
2951
2952 memmove( new_remain, cur_remain, remain_len );
2953 }
2954
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002955 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2956
2957 polarssl_free( ssl->handshake->hs_msg );
2958 ssl->handshake->hs_msg = NULL;
2959
2960 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2961 ssl->in_msg, ssl->in_hslen );
2962
2963 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002964}
2965#endif /* POLARSSL_SSL_PROTO_DTLS */
2966
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002967static int ssl_prepare_handshake_record( ssl_context *ssl )
2968{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002969 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2970 {
2971 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2972 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002973 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002974 }
2975
2976 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2977 ( ssl->in_msg[1] << 16 ) |
2978 ( ssl->in_msg[2] << 8 ) |
2979 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002980
2981 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2982 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002983 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002984
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002985#if defined(POLARSSL_SSL_PROTO_DTLS)
2986 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002987 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002988 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002989 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002990
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002991 /* ssl->handshake is NULL when receiving ClientHello for renego */
2992 if( ssl->handshake != NULL &&
2993 recv_msg_seq != ssl->handshake->in_msg_seq )
2994 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002995 /* Retransmit only on last message from previous flight, to avoid
2996 * too many retransmissions.
2997 * Besides, No sane server ever retransmits HelloVerifyRequest */
2998 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002999 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003000 {
3001 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3002 "message_seq = %d, start_of_flight = %d",
3003 recv_msg_seq,
3004 ssl->handshake->in_flight_start_seq ) );
3005
3006 if( ( ret = ssl_resend( ssl ) ) != 0 )
3007 {
3008 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3009 return( ret );
3010 }
3011 }
3012 else
3013 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003014 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003015 "message_seq = %d, expected = %d",
3016 recv_msg_seq,
3017 ssl->handshake->in_msg_seq ) );
3018 }
3019
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003020 return( POLARSSL_ERR_NET_WANT_READ );
3021 }
3022 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003023
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003024 /* Reassemble if current message is fragmented or reassembly is
3025 * already in progress */
3026 if( ssl->in_msglen < ssl->in_hslen ||
3027 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3028 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3029 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003030 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003031 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3032
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003033 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3034 {
3035 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3036 return( ret );
3037 }
3038 }
3039 }
3040 else
3041#endif /* POLARSSL_SSL_PROTO_DTLS */
3042 /* With TLS we don't handle fragmentation (for now) */
3043 if( ssl->in_msglen < ssl->in_hslen )
3044 {
3045 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003046 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003047 }
3048
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003049 if( ssl->state != SSL_HANDSHAKE_OVER )
3050 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3051
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003052 /* Handshake message is complete, increment counter */
3053#if defined(POLARSSL_SSL_PROTO_DTLS)
3054 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3055 ssl->handshake != NULL )
3056 {
3057 ssl->handshake->in_msg_seq++;
3058 }
3059#endif
3060
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003061 return( 0 );
3062}
3063
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003064/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003065 * DTLS anti-replay: RFC 6347 4.1.2.6
3066 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003067 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3068 * Bit n is set iff record number in_window_top - n has been seen.
3069 *
3070 * Usually, in_window_top is the last record number seen and the lsb of
3071 * in_window is set. The only exception is the initial state (record number 0
3072 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003073 */
3074#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3075static void ssl_dtls_replay_reset( ssl_context *ssl )
3076{
3077 ssl->in_window_top = 0;
3078 ssl->in_window = 0;
3079}
3080
3081static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3082{
3083 return( ( (uint64_t) buf[0] << 40 ) |
3084 ( (uint64_t) buf[1] << 32 ) |
3085 ( (uint64_t) buf[2] << 24 ) |
3086 ( (uint64_t) buf[3] << 16 ) |
3087 ( (uint64_t) buf[4] << 8 ) |
3088 ( (uint64_t) buf[5] ) );
3089}
3090
3091/*
3092 * Return 0 if sequence number is acceptable, -1 otherwise
3093 */
3094int ssl_dtls_replay_check( ssl_context *ssl )
3095{
3096 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3097 uint64_t bit;
3098
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003099 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3100 return( 0 );
3101
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003102 if( rec_seqnum > ssl->in_window_top )
3103 return( 0 );
3104
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003105 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003106
3107 if( bit >= 64 )
3108 return( -1 );
3109
3110 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3111 return( -1 );
3112
3113 return( 0 );
3114}
3115
3116/*
3117 * Update replay window on new validated record
3118 */
3119void ssl_dtls_replay_update( ssl_context *ssl )
3120{
3121 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3122
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003123 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3124 return;
3125
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003126 if( rec_seqnum > ssl->in_window_top )
3127 {
3128 /* Update window_top and the contents of the window */
3129 uint64_t shift = rec_seqnum - ssl->in_window_top;
3130
3131 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003132 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003133 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003134 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003135 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003136 ssl->in_window |= 1;
3137 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003138
3139 ssl->in_window_top = rec_seqnum;
3140 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003141 else
3142 {
3143 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003144 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003145
3146 if( bit < 64 ) /* Always true, but be extra sure */
3147 ssl->in_window |= (uint64_t) 1 << bit;
3148 }
3149}
3150#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3151
3152/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003153 * ContentType type;
3154 * ProtocolVersion version;
3155 * uint16 epoch; // DTLS only
3156 * uint48 sequence_number; // DTLS only
3157 * uint16 length;
3158 */
3159static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003160{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003161 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003162 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003164 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3165
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003167 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003168 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003169
3170 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3171 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003172 ssl->in_msgtype,
3173 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003175 /* Check record type */
3176 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3177 ssl->in_msgtype != SSL_MSG_ALERT &&
3178 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3179 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3180 {
3181 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003182
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003183 if( ( ret = ssl_send_alert_message( ssl,
3184 SSL_ALERT_LEVEL_FATAL,
3185 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3186 {
3187 return( ret );
3188 }
3189
3190 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3191 }
3192
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003193#if defined(POLARSSL_SSL_PROTO_DTLS)
3194 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3195 {
3196 /* Drop unexpected ChangeCipherSpec messages */
3197 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3198 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3199 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3200 {
3201 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3203 }
3204
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003205 /* Drop unexpected ApplicationData records,
3206 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003207 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003208 ssl->state != SSL_HANDSHAKE_OVER
3209#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003210 && ! ( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00003211 ssl->state == SSL_SERVER_HELLO )
3212#endif
3213 )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003214 {
3215 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3216 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3217 }
3218 }
3219#endif
3220
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003221 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003222 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
3224 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003225 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003226 }
3227
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003228 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
3230 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 }
3233
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003234 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003235#if defined(POLARSSL_SSL_PROTO_DTLS)
3236 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3237 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003238 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003239
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003240 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003241 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003242 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003243 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003244 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003245 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003246 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003247
3248#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3249 if( ssl_dtls_replay_check( ssl ) != 0 )
3250 {
3251 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3252 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3253 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003254#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003255 }
3256#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003257
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003258 /* Check length against the size of our buffer */
3259 if( ssl->in_msglen > SSL_BUFFER_LEN
3260 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003261 {
3262 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3263 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3264 }
3265
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003266 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003267 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003268 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003269 if( ssl->in_msglen < 1 ||
3270 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003271 {
3272 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 }
3275 }
3276 else
3277 {
Paul Bakker48916f92012-09-16 19:57:18 +00003278 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 {
3280 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003281 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
3283
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003284#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003286 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 {
3288 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003289 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003291#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003292#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3293 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 /*
3295 * TLS encrypted messages can have up to 256 bytes of padding
3296 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003297 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003298 ssl->in_msglen > ssl->transform_in->minlen +
3299 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 {
3301 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003302 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003304#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 }
3306
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003307 return( 0 );
3308}
Paul Bakker5121ce52009-01-03 21:22:43 +00003309
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003310/*
3311 * If applicable, decrypt (and decompress) record content
3312 */
3313static int ssl_prepare_record_content( ssl_context *ssl )
3314{
3315 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003316
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003318 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003319
Paul Bakker05ef8352012-05-08 09:17:57 +00003320#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003321 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003322 {
3323 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3324
3325 ret = ssl_hw_record_read( ssl );
3326 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3327 {
3328 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003329 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003330 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003331
3332 if( ret == 0 )
3333 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003334 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003335#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003336 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 {
3338 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3339 {
3340 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3341 return( ret );
3342 }
3343
3344 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3345 ssl->in_msg, ssl->in_msglen );
3346
3347 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3348 {
3349 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003350 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 }
3352 }
3353
Paul Bakker2770fbd2012-07-03 13:30:23 +00003354#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003355 if( ssl->transform_in != NULL &&
3356 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003357 {
3358 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3359 {
3360 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3361 return( ret );
3362 }
3363
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003364 // TODO: what's the purpose of these lines? is in_len used?
3365 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3366 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003367 }
3368#endif /* POLARSSL_ZLIB_SUPPORT */
3369
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003370#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003371 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3372 {
3373 ssl_dtls_replay_update( ssl );
3374 }
3375#endif
3376
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003377 return( 0 );
3378}
3379
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003380static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3381
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003382/*
3383 * Read a record.
3384 *
3385 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3386 * and continue reading until a valid record is found.
3387 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003388int ssl_read_record( ssl_context *ssl )
3389{
3390 int ret;
3391
3392 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3393
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003394 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003395 {
3396 /*
3397 * Get next Handshake message in the current record
3398 */
3399 ssl->in_msglen -= ssl->in_hslen;
3400
3401 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3402 ssl->in_msglen );
3403
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003404 SSL_DEBUG_BUF( 4, "remaining content in record",
3405 ssl->in_msg, ssl->in_msglen );
3406
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003407 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3408 return( ret );
3409
3410 return( 0 );
3411 }
3412
3413 ssl->in_hslen = 0;
3414
3415 /*
3416 * Read the record header and parse it
3417 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003418#if defined(POLARSSL_SSL_PROTO_DTLS)
3419read_record_header:
3420#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003421 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3422 {
3423 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3424 return( ret );
3425 }
3426
3427 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003428 {
3429#if defined(POLARSSL_SSL_PROTO_DTLS)
3430 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3431 {
3432 /* Ignore bad record and get next one; drop the whole datagram
3433 * since current header cannot be trusted to find the next record
3434 * in current datagram */
3435 ssl->next_record_offset = 0;
3436 ssl->in_left = 0;
3437
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003438 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003439 goto read_record_header;
3440 }
3441#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003442 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003443 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003444
3445 /*
3446 * Read and optionally decrypt the message contents
3447 */
3448 if( ( ret = ssl_fetch_input( ssl,
3449 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3450 {
3451 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3452 return( ret );
3453 }
3454
3455 /* Done reading this record, get ready for the next one */
3456#if defined(POLARSSL_SSL_PROTO_DTLS)
3457 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3458 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3459 else
3460#endif
3461 ssl->in_left = 0;
3462
3463 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003464 {
3465#if defined(POLARSSL_SSL_PROTO_DTLS)
3466 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3467 {
3468 /* Silently discard invalid records */
3469 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3470 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3471 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003472#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3473 if( ssl->badmac_limit != 0 &&
3474 ++ssl->badmac_seen >= ssl->badmac_limit )
3475 {
3476 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3477 return( POLARSSL_ERR_SSL_INVALID_MAC );
3478 }
3479#endif
3480
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003481 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003482 goto read_record_header;
3483 }
3484
3485 return( ret );
3486 }
3487 else
3488#endif
3489 {
3490 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard9b669902015-03-06 16:52:46 +00003491#if defined(POLARSSL_SSL_ALL_ALERT_MESSAGES)
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003492 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3493 {
3494 ssl_send_alert_message( ssl,
3495 SSL_ALERT_LEVEL_FATAL,
3496 SSL_ALERT_MSG_BAD_RECORD_MAC );
3497 }
3498#endif
3499 return( ret );
3500 }
3501 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003502
3503 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003504 * When we sent the last flight of the handshake, we MUST respond to a
3505 * retransmit of the peer's previous flight with a retransmit. (In
3506 * practice, only the Finished message will make it, other messages
3507 * including CCS use the old transform so they're dropped as invalid.)
3508 *
3509 * If the record we received is not a handshake message, however, it
3510 * means the peer received our last flight so we can clean up
3511 * handshake info.
3512 *
3513 * This check needs to be done before prepare_handshake() due to an edge
3514 * case: if the client immediately requests renegotiation, this
3515 * finishes the current handshake first, avoiding the new ClientHello
3516 * being mistaken for an ancient message in the current handshake.
3517 */
3518#if defined(POLARSSL_SSL_PROTO_DTLS)
3519 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3520 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003521 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003522 {
3523 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3524 ssl->in_msg[0] == SSL_HS_FINISHED )
3525 {
3526 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3527
3528 if( ( ret = ssl_resend( ssl ) ) != 0 )
3529 {
3530 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3531 return( ret );
3532 }
3533
3534 return( POLARSSL_ERR_NET_WANT_READ );
3535 }
3536 else
3537 {
3538 ssl_handshake_wrapup_free_hs_transform( ssl );
3539 }
3540 }
3541#endif
3542
3543 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003544 * Handle particular types of records
3545 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3547 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003548 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3549 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003550 }
3551
3552 if( ssl->in_msgtype == SSL_MSG_ALERT )
3553 {
3554 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3555 ssl->in_msg[0], ssl->in_msg[1] ) );
3556
3557 /*
3558 * Ignore non-fatal alerts, except close_notify
3559 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003560 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003562 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3563 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003564 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 }
3566
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003567 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3568 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 {
3570 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003571 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 }
3573 }
3574
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3576
3577 return( 0 );
3578}
3579
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003580int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3581{
3582 int ret;
3583
3584 if( ( ret = ssl_send_alert_message( ssl,
3585 SSL_ALERT_LEVEL_FATAL,
3586 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3587 {
3588 return( ret );
3589 }
3590
3591 return( 0 );
3592}
3593
Paul Bakker0a925182012-04-16 06:46:41 +00003594int ssl_send_alert_message( ssl_context *ssl,
3595 unsigned char level,
3596 unsigned char message )
3597{
3598 int ret;
3599
3600 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3601
3602 ssl->out_msgtype = SSL_MSG_ALERT;
3603 ssl->out_msglen = 2;
3604 ssl->out_msg[0] = level;
3605 ssl->out_msg[1] = message;
3606
3607 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3608 {
3609 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3610 return( ret );
3611 }
3612
3613 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3614
3615 return( 0 );
3616}
3617
Paul Bakker5121ce52009-01-03 21:22:43 +00003618/*
3619 * Handshake functions
3620 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003621#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3622 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3623 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3624 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3625 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3626 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3627 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003628int ssl_write_certificate( ssl_context *ssl )
3629{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003630 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
3632 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3633
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003634 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003635 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003637 {
3638 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3639 ssl->state++;
3640 return( 0 );
3641 }
3642
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003643 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3644 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003645}
3646
3647int ssl_parse_certificate( ssl_context *ssl )
3648{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003649 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3650
3651 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3652
3653 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003654 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3655 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003656 {
3657 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3658 ssl->state++;
3659 return( 0 );
3660 }
3661
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003662 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3663 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003664}
3665#else
3666int ssl_write_certificate( ssl_context *ssl )
3667{
3668 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3669 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003670 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003671 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3672
3673 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3674
3675 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003676 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3677 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003678 {
3679 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3680 ssl->state++;
3681 return( 0 );
3682 }
3683
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003684#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003685 if( ssl->endpoint == SSL_IS_CLIENT )
3686 {
3687 if( ssl->client_auth == 0 )
3688 {
3689 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3690 ssl->state++;
3691 return( 0 );
3692 }
3693
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003694#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 /*
3696 * If using SSLv3 and got no cert, send an Alert message
3697 * (otherwise an empty Certificate message will be sent).
3698 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003699 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3701 {
3702 ssl->out_msglen = 2;
3703 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003704 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3705 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003706
3707 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3708 goto write_msg;
3709 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003710#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003712#endif /* POLARSSL_SSL_CLI_C */
3713#if defined(POLARSSL_SSL_SRV_C)
3714 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003715 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003716 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 {
3718 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003719 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 }
3721 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003724 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003725
3726 /*
3727 * 0 . 0 handshake type
3728 * 1 . 3 handshake length
3729 * 4 . 6 length of all certs
3730 * 7 . 9 length of cert. 1
3731 * 10 . n-1 peer certificate
3732 * n . n+2 length of cert. 2
3733 * n+3 . ... upper level cert, etc.
3734 */
3735 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003736 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
Paul Bakker29087132010-03-21 21:03:34 +00003738 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 {
3740 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003741 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003742 {
3743 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3744 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003745 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003746 }
3747
3748 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3749 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3750 ssl->out_msg[i + 2] = (unsigned char)( n );
3751
3752 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3753 i += n; crt = crt->next;
3754 }
3755
3756 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3757 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3758 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3759
3760 ssl->out_msglen = i;
3761 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3762 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3763
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003764#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003765write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003766#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003767
3768 ssl->state++;
3769
3770 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3771 {
3772 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3773 return( ret );
3774 }
3775
3776 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3777
Paul Bakkered27a042013-04-18 22:46:23 +02003778 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003779}
3780
3781int ssl_parse_certificate( ssl_context *ssl )
3782{
Paul Bakkered27a042013-04-18 22:46:23 +02003783 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003784 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003785 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
3787 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3788
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003789 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3791 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003792 {
3793 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3794 ssl->state++;
3795 return( 0 );
3796 }
3797
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003798#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003800 ( ssl->authmode == SSL_VERIFY_NONE ||
3801 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003803 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3805 ssl->state++;
3806 return( 0 );
3807 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003808#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003809
3810 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3811 {
3812 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3813 return( ret );
3814 }
3815
3816 ssl->state++;
3817
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003818#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003819#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003820 /*
3821 * Check if the client sent an empty certificate
3822 */
3823 if( ssl->endpoint == SSL_IS_SERVER &&
3824 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3825 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003826 if( ssl->in_msglen == 2 &&
3827 ssl->in_msgtype == SSL_MSG_ALERT &&
3828 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3829 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 {
3831 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3832
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003833 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3835 return( 0 );
3836 else
Paul Bakker40e46942009-01-03 21:51:57 +00003837 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003838 }
3839 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003840#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003841
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003842#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3843 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 if( ssl->endpoint == SSL_IS_SERVER &&
3845 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3846 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003847 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003848 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3849 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003850 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003851 {
3852 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3853
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003854 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003855 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003856 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 else
3858 return( 0 );
3859 }
3860 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003861#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3862 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003863#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003864
3865 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3866 {
3867 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003868 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003869 }
3870
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003871 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3872 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 {
3874 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003875 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003876 }
3877
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003878 i = ssl_hs_hdr_len( ssl );
3879
Paul Bakker5121ce52009-01-03 21:22:43 +00003880 /*
3881 * Same message structure as in ssl_write_certificate()
3882 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003883 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003884
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003885 if( ssl->in_msg[i] != 0 ||
3886 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 {
3888 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003889 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003890 }
3891
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003892 /* In case we tried to reuse a session but it failed */
3893 if( ssl->session_negotiate->peer_cert != NULL )
3894 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003895 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003896 polarssl_free( ssl->session_negotiate->peer_cert );
3897 }
3898
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003899 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003900 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 {
3902 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003903 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003904 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 }
3906
Paul Bakkerb6b09562013-09-18 14:17:41 +02003907 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003908
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003909 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003910
3911 while( i < ssl->in_hslen )
3912 {
3913 if( ssl->in_msg[i] != 0 )
3914 {
3915 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003916 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003917 }
3918
3919 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3920 | (unsigned int) ssl->in_msg[i + 2];
3921 i += 3;
3922
3923 if( n < 128 || i + n > ssl->in_hslen )
3924 {
3925 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003926 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 }
3928
Paul Bakkerddf26b42013-09-18 13:46:23 +02003929 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3930 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003931 if( ret != 0 )
3932 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003933 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003934 return( ret );
3935 }
3936
3937 i += n;
3938 }
3939
Paul Bakker48916f92012-09-16 19:57:18 +00003940 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003942 /*
3943 * On client, make sure the server cert doesn't change during renego to
3944 * avoid "triple handshake" attack: https://secure-resumption.com/
3945 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003946#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003947 if( ssl->endpoint == SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00003948 ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003949 {
3950 if( ssl->session->peer_cert == NULL )
3951 {
3952 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3953 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3954 }
3955
3956 if( ssl->session->peer_cert->raw.len !=
3957 ssl->session_negotiate->peer_cert->raw.len ||
3958 memcmp( ssl->session->peer_cert->raw.p,
3959 ssl->session_negotiate->peer_cert->raw.p,
3960 ssl->session->peer_cert->raw.len ) != 0 )
3961 {
3962 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3963 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3964 }
3965 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003966#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 if( ssl->authmode != SSL_VERIFY_NONE )
3969 {
3970 if( ssl->ca_chain == NULL )
3971 {
3972 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003973 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 }
3975
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003976 /*
3977 * Main check: verify certificate
3978 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003979 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3980 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3981 &ssl->session_negotiate->verify_result,
3982 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003983
3984 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003985 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003986 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003987 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003988
3989 /*
3990 * Secondary checks: always done, but change 'ret' only if it was 0
3991 */
3992
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003993#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003994 {
Manuel Pégourié-Gonnard0db107e2015-03-19 14:01:57 +00003995 const pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003996
3997 /* If certificate uses an EC key, make sure the curve is OK */
3998 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3999 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
4000 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004001 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004002 if( ret == 0 )
4003 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004004 }
4005 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004006#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004007
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004008 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4009 ciphersuite_info,
4010 ! ssl->endpoint ) != 0 )
4011 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004012 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004013 if( ret == 0 )
4014 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4015 }
4016
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4018 ret = 0;
4019 }
4020
4021 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4022
4023 return( ret );
4024}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004025#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4026 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4027 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4028 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4029 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4030 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4031 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
4033int ssl_write_change_cipher_spec( ssl_context *ssl )
4034{
4035 int ret;
4036
4037 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4038
4039 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4040 ssl->out_msglen = 1;
4041 ssl->out_msg[0] = 1;
4042
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 ssl->state++;
4044
4045 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4046 {
4047 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4048 return( ret );
4049 }
4050
4051 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4052
4053 return( 0 );
4054}
4055
4056int ssl_parse_change_cipher_spec( ssl_context *ssl )
4057{
4058 int ret;
4059
4060 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4061
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4063 {
4064 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4065 return( ret );
4066 }
4067
4068 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4069 {
4070 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004071 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004072 }
4073
4074 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4075 {
4076 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004077 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004078 }
4079
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004080 /*
4081 * Switch to our negotiated transform and session parameters for inbound
4082 * data.
4083 */
4084 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4085 ssl->transform_in = ssl->transform_negotiate;
4086 ssl->session_in = ssl->session_negotiate;
4087
4088#if defined(POLARSSL_SSL_PROTO_DTLS)
4089 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4090 {
4091#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4092 ssl_dtls_replay_reset( ssl );
4093#endif
4094
4095 /* Increment epoch */
4096 if( ++ssl->in_epoch == 0 )
4097 {
4098 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4099 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4100 }
4101 }
4102 else
4103#endif /* POLARSSL_SSL_PROTO_DTLS */
4104 memset( ssl->in_ctr, 0, 8 );
4105
4106 /*
4107 * Set the in_msg pointer to the correct location based on IV length
4108 */
4109 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4110 {
4111 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4112 ssl->transform_negotiate->fixed_ivlen;
4113 }
4114 else
4115 ssl->in_msg = ssl->in_iv;
4116
4117#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4118 if( ssl_hw_record_activate != NULL )
4119 {
4120 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4121 {
4122 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4123 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4124 }
4125 }
4126#endif
4127
Paul Bakker5121ce52009-01-03 21:22:43 +00004128 ssl->state++;
4129
4130 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4131
4132 return( 0 );
4133}
4134
Paul Bakker41c83d32013-03-20 14:39:14 +01004135void ssl_optimize_checksum( ssl_context *ssl,
4136 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004137{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004138 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004139
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004140#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4141 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004142 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004143 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004144 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004145#endif
4146#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4147#if defined(POLARSSL_SHA512_C)
4148 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4149 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4150 else
4151#endif
4152#if defined(POLARSSL_SHA256_C)
4153 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004154 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004155 else
4156#endif
4157#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004158 {
4159 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004161 }
Paul Bakker380da532012-04-18 16:10:25 +00004162}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004163
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004164void ssl_reset_checksum( ssl_context *ssl )
4165{
4166#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4167 defined(POLARSSL_SSL_PROTO_TLS1_1)
4168 md5_starts( &ssl->handshake->fin_md5 );
4169 sha1_starts( &ssl->handshake->fin_sha1 );
4170#endif
4171#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4172#if defined(POLARSSL_SHA256_C)
4173 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4174#endif
4175#if defined(POLARSSL_SHA512_C)
4176 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4177#endif
4178#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4179}
4180
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004181static void ssl_update_checksum_start( ssl_context *ssl,
4182 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004183{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004184#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4185 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004186 md5_update( &ssl->handshake->fin_md5 , buf, len );
4187 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004188#endif
4189#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4190#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004191 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004192#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004193#if defined(POLARSSL_SHA512_C)
4194 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004195#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004196#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004197}
4198
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004199#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4200 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004201static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4202 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004203{
Paul Bakker48916f92012-09-16 19:57:18 +00004204 md5_update( &ssl->handshake->fin_md5 , buf, len );
4205 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004206}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004207#endif
Paul Bakker380da532012-04-18 16:10:25 +00004208
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004209#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4210#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004211static void ssl_update_checksum_sha256( ssl_context *ssl,
4212 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004213{
Paul Bakker9e36f042013-06-30 14:34:05 +02004214 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004215}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004216#endif
Paul Bakker380da532012-04-18 16:10:25 +00004217
Paul Bakker9e36f042013-06-30 14:34:05 +02004218#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004219static void ssl_update_checksum_sha384( ssl_context *ssl,
4220 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004221{
Paul Bakker9e36f042013-06-30 14:34:05 +02004222 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004223}
Paul Bakker769075d2012-11-24 11:26:46 +01004224#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004225#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004226
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004227#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004228static void ssl_calc_finished_ssl(
4229 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004230{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004231 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004232 md5_context md5;
4233 sha1_context sha1;
4234
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 unsigned char padbuf[48];
4236 unsigned char md5sum[16];
4237 unsigned char sha1sum[20];
4238
Paul Bakker48916f92012-09-16 19:57:18 +00004239 ssl_session *session = ssl->session_negotiate;
4240 if( !session )
4241 session = ssl->session;
4242
Paul Bakker1ef83d62012-04-11 12:09:53 +00004243 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4244
Paul Bakker48916f92012-09-16 19:57:18 +00004245 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4246 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
4248 /*
4249 * SSLv3:
4250 * hash =
4251 * MD5( master + pad2 +
4252 * MD5( handshake + sender + master + pad1 ) )
4253 * + SHA1( master + pad2 +
4254 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004255 */
4256
Paul Bakker90995b52013-06-24 19:20:35 +02004257#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004259 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004261
Paul Bakker90995b52013-06-24 19:20:35 +02004262#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004263 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004264 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004265#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004266
Paul Bakker3c2122f2013-06-24 19:03:14 +02004267 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4268 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004269
Paul Bakker1ef83d62012-04-11 12:09:53 +00004270 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271
Paul Bakker3c2122f2013-06-24 19:03:14 +02004272 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004273 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274 md5_update( &md5, padbuf, 48 );
4275 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004276
Paul Bakker3c2122f2013-06-24 19:03:14 +02004277 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004278 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004279 sha1_update( &sha1, padbuf, 40 );
4280 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004283
Paul Bakker1ef83d62012-04-11 12:09:53 +00004284 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004285 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004286 md5_update( &md5, padbuf, 48 );
4287 md5_update( &md5, md5sum, 16 );
4288 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004289
Paul Bakker1ef83d62012-04-11 12:09:53 +00004290 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004291 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004292 sha1_update( &sha1, padbuf , 40 );
4293 sha1_update( &sha1, sha1sum, 20 );
4294 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295
Paul Bakker1ef83d62012-04-11 12:09:53 +00004296 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004297
Paul Bakker5b4af392014-06-26 12:09:34 +02004298 md5_free( &md5 );
4299 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004300
Paul Bakker34617722014-06-13 17:20:13 +02004301 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4302 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4303 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
4305 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4306}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004307#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004308
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004309#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004310static void ssl_calc_finished_tls(
4311 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004312{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004313 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004314 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004315 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004317 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Paul Bakker48916f92012-09-16 19:57:18 +00004319 ssl_session *session = ssl->session_negotiate;
4320 if( !session )
4321 session = ssl->session;
4322
Paul Bakker1ef83d62012-04-11 12:09:53 +00004323 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004324
Paul Bakker48916f92012-09-16 19:57:18 +00004325 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4326 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004327
Paul Bakker1ef83d62012-04-11 12:09:53 +00004328 /*
4329 * TLSv1:
4330 * hash = PRF( master, finished_label,
4331 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4332 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004333
Paul Bakker90995b52013-06-24 19:20:35 +02004334#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004335 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4336 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004337#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004338
Paul Bakker90995b52013-06-24 19:20:35 +02004339#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004340 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4341 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004342#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004343
4344 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004345 ? "client finished"
4346 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004347
4348 md5_finish( &md5, padbuf );
4349 sha1_finish( &sha1, padbuf + 16 );
4350
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004351 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004352 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004353
4354 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4355
Paul Bakker5b4af392014-06-26 12:09:34 +02004356 md5_free( &md5 );
4357 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004358
Paul Bakker34617722014-06-13 17:20:13 +02004359 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004360
4361 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4362}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004363#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004364
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004365#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4366#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004367static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004368 ssl_context *ssl, unsigned char *buf, int from )
4369{
4370 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004371 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004372 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004373 unsigned char padbuf[32];
4374
Paul Bakker48916f92012-09-16 19:57:18 +00004375 ssl_session *session = ssl->session_negotiate;
4376 if( !session )
4377 session = ssl->session;
4378
Paul Bakker380da532012-04-18 16:10:25 +00004379 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004380
Paul Bakker9e36f042013-06-30 14:34:05 +02004381 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004382
4383 /*
4384 * TLSv1.2:
4385 * hash = PRF( master, finished_label,
4386 * Hash( handshake ) )[0.11]
4387 */
4388
Paul Bakker9e36f042013-06-30 14:34:05 +02004389#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004390 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004391 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004392#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004393
4394 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004395 ? "client finished"
4396 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004397
Paul Bakker9e36f042013-06-30 14:34:05 +02004398 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004399
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004400 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004401 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004402
4403 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4404
Paul Bakker5b4af392014-06-26 12:09:34 +02004405 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004406
Paul Bakker34617722014-06-13 17:20:13 +02004407 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004408
4409 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4410}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004411#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004412
Paul Bakker9e36f042013-06-30 14:34:05 +02004413#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004414static void ssl_calc_finished_tls_sha384(
4415 ssl_context *ssl, unsigned char *buf, int from )
4416{
4417 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004418 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004419 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004420 unsigned char padbuf[48];
4421
Paul Bakker48916f92012-09-16 19:57:18 +00004422 ssl_session *session = ssl->session_negotiate;
4423 if( !session )
4424 session = ssl->session;
4425
Paul Bakker380da532012-04-18 16:10:25 +00004426 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004427
Paul Bakker9e36f042013-06-30 14:34:05 +02004428 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004429
4430 /*
4431 * TLSv1.2:
4432 * hash = PRF( master, finished_label,
4433 * Hash( handshake ) )[0.11]
4434 */
4435
Paul Bakker9e36f042013-06-30 14:34:05 +02004436#if !defined(POLARSSL_SHA512_ALT)
4437 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4438 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004439#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004440
4441 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004442 ? "client finished"
4443 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004444
Paul Bakker9e36f042013-06-30 14:34:05 +02004445 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004446
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004447 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004448 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004449
4450 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4451
Paul Bakker5b4af392014-06-26 12:09:34 +02004452 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004453
Paul Bakker34617722014-06-13 17:20:13 +02004454 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004455
4456 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4457}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004458#endif /* POLARSSL_SHA512_C */
4459#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004460
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004461static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004462{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004463 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004464
4465 /*
4466 * Free our handshake params
4467 */
4468 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004469 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004470 ssl->handshake = NULL;
4471
4472 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004473 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004474 */
4475 if( ssl->transform )
4476 {
4477 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004478 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004479 }
4480 ssl->transform = ssl->transform_negotiate;
4481 ssl->transform_negotiate = NULL;
4482
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004483 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4484}
4485
4486void ssl_handshake_wrapup( ssl_context *ssl )
4487{
4488 int resume = ssl->handshake->resume;
4489
4490 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4491
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004492#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004493 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004494 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004495 ssl->renego_status = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004496 ssl->renego_records_seen = 0;
4497 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004498#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004499
4500 /*
4501 * Free the previous session and switch in the current one
4502 */
Paul Bakker0a597072012-09-25 21:55:46 +00004503 if( ssl->session )
4504 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004505#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4506 /* RFC 7366 3.1: keep the EtM state */
4507 ssl->session_negotiate->encrypt_then_mac =
4508 ssl->session->encrypt_then_mac;
4509#endif
4510
Paul Bakker0a597072012-09-25 21:55:46 +00004511 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004512 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004513 }
4514 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004515 ssl->session_negotiate = NULL;
4516
Paul Bakker0a597072012-09-25 21:55:46 +00004517 /*
4518 * Add cache entry
4519 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004520 if( ssl->f_set_cache != NULL &&
4521 ssl->session->length != 0 &&
4522 resume == 0 )
4523 {
Paul Bakker0a597072012-09-25 21:55:46 +00004524 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4525 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004526 }
Paul Bakker0a597072012-09-25 21:55:46 +00004527
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004528#if defined(POLARSSL_SSL_PROTO_DTLS)
4529 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4530 ssl->handshake->flight != NULL )
4531 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004532 /* Cancel handshake timer */
4533 ssl_set_timer( ssl, 0 );
4534
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004535 /* Keep last flight around in case we need to resend it:
4536 * we need the handshake and transform structures for that */
4537 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4538 }
4539 else
4540#endif
4541 ssl_handshake_wrapup_free_hs_transform( ssl );
4542
Paul Bakker48916f92012-09-16 19:57:18 +00004543 ssl->state++;
4544
4545 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4546}
4547
Paul Bakker1ef83d62012-04-11 12:09:53 +00004548int ssl_write_finished( ssl_context *ssl )
4549{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004550 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004551
4552 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4553
Paul Bakker92be97b2013-01-02 17:30:03 +01004554 /*
4555 * Set the out_msg pointer to the correct location based on IV length
4556 */
4557 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4558 {
4559 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4560 ssl->transform_negotiate->fixed_ivlen;
4561 }
4562 else
4563 ssl->out_msg = ssl->out_iv;
4564
Paul Bakker48916f92012-09-16 19:57:18 +00004565 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004566
4567 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004568 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4569
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004570#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004571 ssl->verify_data_len = hash_len;
4572 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004573#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004574
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 ssl->out_msglen = 4 + hash_len;
4576 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4577 ssl->out_msg[0] = SSL_HS_FINISHED;
4578
4579 /*
4580 * In case of session resuming, invert the client and server
4581 * ChangeCipherSpec messages order.
4582 */
Paul Bakker0a597072012-09-25 21:55:46 +00004583 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004584 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004585#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004586 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004587 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004588#endif
4589#if defined(POLARSSL_SSL_SRV_C)
4590 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004591 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004592#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004593 }
4594 else
4595 ssl->state++;
4596
Paul Bakker48916f92012-09-16 19:57:18 +00004597 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004598 * Switch to our negotiated transform and session parameters for outbound
4599 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004600 */
4601 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004602
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004603#if defined(POLARSSL_SSL_PROTO_DTLS)
4604 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4605 {
4606 unsigned char i;
4607
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004608 /* Remember current epoch settings for resending */
4609 ssl->handshake->alt_transform_out = ssl->transform_out;
4610 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4611
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004612 /* Set sequence_number to zero */
4613 memset( ssl->out_ctr + 2, 0, 6 );
4614
4615 /* Increment epoch */
4616 for( i = 2; i > 0; i-- )
4617 if( ++ssl->out_ctr[i - 1] != 0 )
4618 break;
4619
4620 /* The loop goes to its end iff the counter is wrapping */
4621 if( i == 0 )
4622 {
4623 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4624 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4625 }
4626 }
4627 else
4628#endif /* POLARSSL_SSL_PROTO_DTLS */
4629 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004630
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004631 ssl->transform_out = ssl->transform_negotiate;
4632 ssl->session_out = ssl->session_negotiate;
4633
Paul Bakker07eb38b2012-12-19 14:42:06 +01004634#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004635 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004636 {
4637 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4638 {
4639 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4640 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4641 }
4642 }
4643#endif
4644
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004645#if defined(POLARSSL_SSL_PROTO_DTLS)
4646 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4647 ssl_send_flight_completed( ssl );
4648#endif
4649
Paul Bakker5121ce52009-01-03 21:22:43 +00004650 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4651 {
4652 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4653 return( ret );
4654 }
4655
4656 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4657
4658 return( 0 );
4659}
4660
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004661#if defined(POLARSSL_SSL_PROTO_SSL3)
4662#define SSL_MAX_HASH_LEN 36
4663#else
4664#define SSL_MAX_HASH_LEN 12
4665#endif
4666
Paul Bakker5121ce52009-01-03 21:22:43 +00004667int ssl_parse_finished( ssl_context *ssl )
4668{
Paul Bakker23986e52011-04-24 08:57:21 +00004669 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004670 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004671 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004672
4673 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4674
Paul Bakker48916f92012-09-16 19:57:18 +00004675 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4678 {
4679 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4680 return( ret );
4681 }
4682
4683 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4684 {
4685 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004686 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 }
4688
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004689 /* There is currently no ciphersuite using another length with TLS 1.2 */
4690#if defined(POLARSSL_SSL_PROTO_SSL3)
4691 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4692 hash_len = 36;
4693 else
4694#endif
4695 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004696
4697 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004698 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004699 {
4700 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004701 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702 }
4703
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004704 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4705 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 {
4707 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004708 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004709 }
4710
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004711#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004712 ssl->verify_data_len = hash_len;
4713 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004714#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004715
Paul Bakker0a597072012-09-25 21:55:46 +00004716 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004718#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 if( ssl->endpoint == SSL_IS_CLIENT )
4720 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004721#endif
4722#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004723 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004724 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004725#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004726 }
4727 else
4728 ssl->state++;
4729
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004730#if defined(POLARSSL_SSL_PROTO_DTLS)
4731 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4732 ssl_recv_flight_completed( ssl );
4733#endif
4734
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4736
4737 return( 0 );
4738}
4739
Paul Bakker968afaa2014-07-09 11:09:24 +02004740static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004741{
4742 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4743
4744#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4745 defined(POLARSSL_SSL_PROTO_TLS1_1)
4746 md5_init( &handshake->fin_md5 );
4747 sha1_init( &handshake->fin_sha1 );
4748 md5_starts( &handshake->fin_md5 );
4749 sha1_starts( &handshake->fin_sha1 );
4750#endif
4751#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4752#if defined(POLARSSL_SHA256_C)
4753 sha256_init( &handshake->fin_sha256 );
4754 sha256_starts( &handshake->fin_sha256, 0 );
4755#endif
4756#if defined(POLARSSL_SHA512_C)
4757 sha512_init( &handshake->fin_sha512 );
4758 sha512_starts( &handshake->fin_sha512, 1 );
4759#endif
4760#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4761
4762 handshake->update_checksum = ssl_update_checksum_start;
4763 handshake->sig_alg = SSL_HASH_SHA1;
4764
4765#if defined(POLARSSL_DHM_C)
4766 dhm_init( &handshake->dhm_ctx );
4767#endif
4768#if defined(POLARSSL_ECDH_C)
4769 ecdh_init( &handshake->ecdh_ctx );
4770#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004771}
4772
4773static void ssl_transform_init( ssl_transform *transform )
4774{
4775 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004776
4777 cipher_init( &transform->cipher_ctx_enc );
4778 cipher_init( &transform->cipher_ctx_dec );
4779
4780 md_init( &transform->md_ctx_enc );
4781 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004782}
4783
4784void ssl_session_init( ssl_session *session )
4785{
4786 memset( session, 0, sizeof(ssl_session) );
4787}
4788
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004789static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004790{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004791 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004792 if( ssl->transform_negotiate )
4793 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004794 if( ssl->session_negotiate )
4795 ssl_session_free( ssl->session_negotiate );
4796 if( ssl->handshake )
4797 ssl_handshake_free( ssl->handshake );
4798
4799 /*
4800 * Either the pointers are now NULL or cleared properly and can be freed.
4801 * Now allocate missing structures.
4802 */
4803 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004804 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004805 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004806 }
Paul Bakker48916f92012-09-16 19:57:18 +00004807
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004808 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004809 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004810 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004811 }
Paul Bakker48916f92012-09-16 19:57:18 +00004812
Paul Bakker82788fb2014-10-20 13:59:19 +02004813 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004814 {
Mansour Moufid99b92592015-02-15 17:46:32 -05004815 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004816 }
Paul Bakker48916f92012-09-16 19:57:18 +00004817
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004818 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004819 if( ssl->handshake == NULL ||
4820 ssl->transform_negotiate == NULL ||
4821 ssl->session_negotiate == NULL )
4822 {
4823 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004824
4825 polarssl_free( ssl->handshake );
4826 polarssl_free( ssl->transform_negotiate );
4827 polarssl_free( ssl->session_negotiate );
4828
4829 ssl->handshake = NULL;
4830 ssl->transform_negotiate = NULL;
4831 ssl->session_negotiate = NULL;
4832
Paul Bakker48916f92012-09-16 19:57:18 +00004833 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4834 }
4835
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004836 /* Initialize structures */
4837 ssl_session_init( ssl->session_negotiate );
4838 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004839 ssl_handshake_params_init( ssl->handshake );
4840
4841#if defined(POLARSSL_X509_CRT_PARSE_C)
4842 ssl->handshake->key_cert = ssl->key_cert;
4843#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004844
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004845 /*
4846 * We may not know yet if we're using DTLS,
4847 * so always initiliase DTLS-specific fields.
4848 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004849#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004850 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004851
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004852 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004853 if( ssl->endpoint == SSL_IS_CLIENT )
4854 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4855 else
4856 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004857#endif
4858
Paul Bakker48916f92012-09-16 19:57:18 +00004859 return( 0 );
4860}
4861
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004862#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4863/* Dummy cookie callbacks for defaults */
4864static int ssl_cookie_write_dummy( void *ctx,
4865 unsigned char **p, unsigned char *end,
4866 const unsigned char *cli_id, size_t cli_id_len )
4867{
4868 ((void) ctx);
4869 ((void) p);
4870 ((void) end);
4871 ((void) cli_id);
4872 ((void) cli_id_len);
4873
4874 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4875}
4876
4877static int ssl_cookie_check_dummy( void *ctx,
4878 const unsigned char *cookie, size_t cookie_len,
4879 const unsigned char *cli_id, size_t cli_id_len )
4880{
4881 ((void) ctx);
4882 ((void) cookie);
4883 ((void) cookie_len);
4884 ((void) cli_id);
4885 ((void) cli_id_len);
4886
4887 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4888}
4889#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4890
Paul Bakker5121ce52009-01-03 21:22:43 +00004891/*
4892 * Initialize an SSL context
4893 */
4894int ssl_init( ssl_context *ssl )
4895{
Paul Bakker48916f92012-09-16 19:57:18 +00004896 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 int len = SSL_BUFFER_LEN;
4898
4899 memset( ssl, 0, sizeof( ssl_context ) );
4900
Paul Bakker62f2dee2012-09-28 07:31:51 +00004901 /*
4902 * Sane defaults
4903 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004904 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4905 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4906 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4907 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004908
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004909 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004910
Manuel Pégourié-Gonnard849b1742015-03-20 19:13:22 +00004911 ssl_set_arc4_support( ssl, SSL_ARC4_DISABLED );
4912
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004913#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004914 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004915 memset( ssl->renego_period, 0xFF, 7 );
4916 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004917#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004918
Paul Bakker62f2dee2012-09-28 07:31:51 +00004919#if defined(POLARSSL_DHM_C)
4920 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4921 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4922 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4923 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4924 {
4925 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4926 return( ret );
4927 }
4928#endif
4929
4930 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004931 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004932 */
Manuel Pégourié-Gonnard4e41c992015-02-18 10:39:49 +00004933 if( ( ssl->in_buf = polarssl_malloc( len ) ) == NULL ||
4934 ( ssl->out_buf = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004935 {
4936 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004937 polarssl_free( ssl->in_buf );
4938 ssl->in_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004939 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 }
4941
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004942 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4943 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004944
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004945 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4946 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4947
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004948#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4949 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4950#endif
4951
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004952#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4953 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4954#endif
4955
Paul Bakker606b4ba2013-08-14 16:52:14 +02004956#if defined(POLARSSL_SSL_SESSION_TICKETS)
4957 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4958#endif
4959
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004960#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004961 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004962#endif
4963
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004964#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4965 ssl->f_cookie_write = ssl_cookie_write_dummy;
4966 ssl->f_cookie_check = ssl_cookie_check_dummy;
4967#endif
4968
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004969#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4970 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4971#endif
4972
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004973#if defined(POLARSSL_SSL_PROTO_DTLS)
4974 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4975 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4976#endif
4977
Paul Bakker48916f92012-09-16 19:57:18 +00004978 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4979 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004980
4981 return( 0 );
4982}
4983
4984/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004985 * Reset an initialized and used SSL context for re-use while retaining
4986 * all application-set variables, function pointers and data.
4987 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004988int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004989{
Paul Bakker48916f92012-09-16 19:57:18 +00004990 int ret;
4991
Paul Bakker7eb013f2011-10-06 12:37:39 +00004992 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004993
4994#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00004995 ssl->renego_status = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004996 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004997
4998 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01004999 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
5000 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005001#endif
5002 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005003
Paul Bakker7eb013f2011-10-06 12:37:39 +00005004 ssl->in_offt = NULL;
5005
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005006 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005007 ssl->in_msgtype = 0;
5008 ssl->in_msglen = 0;
5009 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005010#if defined(POLARSSL_SSL_PROTO_DTLS)
5011 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005012 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005013#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005014#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5015 ssl_dtls_replay_reset( ssl );
5016#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005017
5018 ssl->in_hslen = 0;
5019 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005020 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005021
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005022 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005023 ssl->out_msgtype = 0;
5024 ssl->out_msglen = 0;
5025 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005026#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005027 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
5028 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01005029#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005030
Paul Bakker48916f92012-09-16 19:57:18 +00005031 ssl->transform_in = NULL;
5032 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005033
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005034 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5035 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005036
5037#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005038 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005039 {
5040 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005041 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005042 {
5043 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5044 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5045 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005046 }
5047#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005048
Paul Bakker48916f92012-09-16 19:57:18 +00005049 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005050 {
Paul Bakker48916f92012-09-16 19:57:18 +00005051 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005052 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005053 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005054 }
Paul Bakker48916f92012-09-16 19:57:18 +00005055
Paul Bakkerc0463502013-02-14 11:19:38 +01005056 if( ssl->session )
5057 {
5058 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005059 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005060 ssl->session = NULL;
5061 }
5062
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005063#if defined(POLARSSL_SSL_ALPN)
5064 ssl->alpn_chosen = NULL;
5065#endif
5066
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005067#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005068 polarssl_free( ssl->cli_id );
5069 ssl->cli_id = NULL;
5070 ssl->cli_id_len = 0;
5071#endif
5072
Paul Bakker48916f92012-09-16 19:57:18 +00005073 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5074 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005075
5076 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005077}
5078
Paul Bakkera503a632013-08-14 13:48:06 +02005079#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005080static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5081{
5082 aes_free( &tkeys->enc );
5083 aes_free( &tkeys->dec );
5084
5085 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5086}
5087
Paul Bakker7eb013f2011-10-06 12:37:39 +00005088/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005089 * Allocate and initialize ticket keys
5090 */
5091static int ssl_ticket_keys_init( ssl_context *ssl )
5092{
5093 int ret;
5094 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005095 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005096
5097 if( ssl->ticket_keys != NULL )
5098 return( 0 );
5099
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005100 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005101 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005102 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5103
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005104 aes_init( &tkeys->enc );
5105 aes_init( &tkeys->dec );
5106
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005107 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005108 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005109 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005110 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005111 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005112 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005113
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005114 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5115 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5116 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5117 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005118 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005119 polarssl_free( tkeys );
5120 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005121 }
5122
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005123 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005124 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005125 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005126 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005127 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005128 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005129
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005130 ssl->ticket_keys = tkeys;
5131
5132 return( 0 );
5133}
Paul Bakkera503a632013-08-14 13:48:06 +02005134#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005135
5136/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005137 * SSL set accessors
5138 */
5139void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5140{
5141 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005142
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005143#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5144 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005145 if( endpoint == SSL_IS_CLIENT )
5146 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005147#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01005148
5149#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
5150 if( endpoint == SSL_IS_SERVER )
5151 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
5152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005153}
5154
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005155int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005156{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005157#if defined(POLARSSL_SSL_PROTO_DTLS)
5158 if( transport == SSL_TRANSPORT_DATAGRAM )
5159 {
5160 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005161
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005162 ssl->out_hdr = ssl->out_buf;
5163 ssl->out_ctr = ssl->out_buf + 3;
5164 ssl->out_len = ssl->out_buf + 11;
5165 ssl->out_iv = ssl->out_buf + 13;
5166 ssl->out_msg = ssl->out_buf + 13;
5167
5168 ssl->in_hdr = ssl->in_buf;
5169 ssl->in_ctr = ssl->in_buf + 3;
5170 ssl->in_len = ssl->in_buf + 11;
5171 ssl->in_iv = ssl->in_buf + 13;
5172 ssl->in_msg = ssl->in_buf + 13;
5173
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005174 /* DTLS starts with TLS1.1 */
5175 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5176 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005177
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005178 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5179 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5180
5181 return( 0 );
5182 }
5183#endif
5184
5185 if( transport == SSL_TRANSPORT_STREAM )
5186 {
5187 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005188
5189 ssl->out_ctr = ssl->out_buf;
5190 ssl->out_hdr = ssl->out_buf + 8;
5191 ssl->out_len = ssl->out_buf + 11;
5192 ssl->out_iv = ssl->out_buf + 13;
5193 ssl->out_msg = ssl->out_buf + 13;
5194
5195 ssl->in_ctr = ssl->in_buf;
5196 ssl->in_hdr = ssl->in_buf + 8;
5197 ssl->in_len = ssl->in_buf + 11;
5198 ssl->in_iv = ssl->in_buf + 13;
5199 ssl->in_msg = ssl->in_buf + 13;
5200
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005201 return( 0 );
5202 }
5203
5204 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005205}
5206
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005207#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5208void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5209{
5210 ssl->anti_replay = mode;
5211}
5212#endif
5213
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005214#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5215void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5216{
5217 ssl->badmac_limit = limit;
5218}
5219#endif
5220
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005221#if defined(POLARSSL_SSL_PROTO_DTLS)
5222void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5223{
5224 ssl->hs_timeout_min = min;
5225 ssl->hs_timeout_max = max;
5226}
5227#endif
5228
Paul Bakker5121ce52009-01-03 21:22:43 +00005229void ssl_set_authmode( ssl_context *ssl, int authmode )
5230{
5231 ssl->authmode = authmode;
5232}
5233
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005234#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005235void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005236 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005237 void *p_vrfy )
5238{
5239 ssl->f_vrfy = f_vrfy;
5240 ssl->p_vrfy = p_vrfy;
5241}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005242#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005243
Paul Bakker5121ce52009-01-03 21:22:43 +00005244void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005245 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 void *p_rng )
5247{
5248 ssl->f_rng = f_rng;
5249 ssl->p_rng = p_rng;
5250}
5251
5252void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005253 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005254 void *p_dbg )
5255{
5256 ssl->f_dbg = f_dbg;
5257 ssl->p_dbg = p_dbg;
5258}
5259
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005260#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Paul Bakker5121ce52009-01-03 21:22:43 +00005261void 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}
Manuel Pégourié-Gonnard9a65e802015-03-25 17:19:12 +01005277#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005278
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005279void ssl_set_bio_timeout( ssl_context *ssl,
5280 void *p_bio,
5281 int (*f_send)(void *, const unsigned char *, size_t),
5282 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005283 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5284 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005285{
5286 ssl->p_bio = p_bio;
5287 ssl->f_send = f_send;
5288 ssl->f_recv = f_recv;
5289 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005290 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005291}
5292
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005293#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005294void ssl_set_session_cache( ssl_context *ssl,
5295 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5296 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005297{
Paul Bakker0a597072012-09-25 21:55:46 +00005298 ssl->f_get_cache = f_get_cache;
5299 ssl->p_get_cache = p_get_cache;
5300 ssl->f_set_cache = f_set_cache;
5301 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005302}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005303#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005304
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005305#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005306int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005307{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005308 int ret;
5309
5310 if( ssl == NULL ||
5311 session == NULL ||
5312 ssl->session_negotiate == NULL ||
5313 ssl->endpoint != SSL_IS_CLIENT )
5314 {
5315 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5316 }
5317
5318 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5319 return( ret );
5320
Paul Bakker0a597072012-09-25 21:55:46 +00005321 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005322
5323 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005325#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005326
Paul Bakkerb68cad62012-08-23 08:34:18 +00005327void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005328{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005329 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5330 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5331 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5332 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5333}
5334
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005335void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5336 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005337 int major, int minor )
5338{
5339 if( major != SSL_MAJOR_VERSION_3 )
5340 return;
5341
5342 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5343 return;
5344
5345 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005346}
5347
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005348#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005349/* Add a new (empty) key_cert entry an return a pointer to it */
5350static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5351{
5352 ssl_key_cert *key_cert, *last;
5353
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005354 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005355 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005356 return( NULL );
5357
5358 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5359
5360 /* Append the new key_cert to the (possibly empty) current list */
5361 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005362 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005363 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005364 if( ssl->handshake != NULL )
5365 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005366 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005367 else
5368 {
5369 last = ssl->key_cert;
5370 while( last->next != NULL )
5371 last = last->next;
5372 last->next = key_cert;
5373 }
5374
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005375 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005376}
5377
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005378void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005379 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005380{
5381 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005382 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005383 ssl->peer_cn = peer_cn;
5384}
5385
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005386int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005387 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005388{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005389 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5390
5391 if( key_cert == NULL )
5392 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5393
5394 key_cert->cert = own_cert;
5395 key_cert->key = pk_key;
5396
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00005397 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005398}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005399#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005400
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005401#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005402int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5403 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005404{
Paul Bakker6db455e2013-09-18 17:29:31 +02005405 if( psk == NULL || psk_identity == NULL )
5406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5407
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005408 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005409 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5410
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005411 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02005412 {
5413 polarssl_free( ssl->psk );
5414 polarssl_free( ssl->psk_identity );
5415 }
5416
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005417 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
5418 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05005419 {
5420 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00005421 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05005422 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5423 }
Paul Bakker6db455e2013-09-18 17:29:31 +02005424
Paul Bakker6db455e2013-09-18 17:29:31 +02005425 ssl->psk_len = psk_len;
5426 ssl->psk_identity_len = psk_identity_len;
5427
Paul Bakker6db455e2013-09-18 17:29:31 +02005428 memcpy( ssl->psk, psk, ssl->psk_len );
5429 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
5430
5431 return( 0 );
5432}
5433
5434void ssl_set_psk_cb( ssl_context *ssl,
5435 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5436 size_t),
5437 void *p_psk )
5438{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005439 ssl->f_psk = f_psk;
5440 ssl->p_psk = p_psk;
Paul Bakker43b7e352011-01-18 15:27:19 +00005441}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005442#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00005443
Paul Bakker48916f92012-09-16 19:57:18 +00005444#if defined(POLARSSL_DHM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005445int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
5446{
5447 int ret;
5448
Paul Bakker48916f92012-09-16 19:57:18 +00005449 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450 {
5451 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5452 return( ret );
5453 }
5454
Paul Bakker48916f92012-09-16 19:57:18 +00005455 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005456 {
5457 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5458 return( ret );
5459 }
5460
5461 return( 0 );
5462}
5463
Paul Bakker1b57b062011-01-06 15:48:19 +00005464int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5465{
5466 int ret;
5467
Paul Bakker66d5d072014-06-17 16:39:18 +02005468 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005469 {
5470 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5471 return( ret );
5472 }
5473
Paul Bakker66d5d072014-06-17 16:39:18 +02005474 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005475 {
5476 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5477 return( ret );
5478 }
5479
5480 return( 0 );
5481}
Paul Bakker48916f92012-09-16 19:57:18 +00005482#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005483
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005484#if defined(POLARSSL_SSL_SET_CURVES)
5485/*
5486 * Set the allowed elliptic curves
5487 */
5488void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5489{
5490 ssl->curve_list = curve_list;
5491}
5492#endif
5493
Paul Bakker0be444a2013-08-27 21:55:01 +02005494#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005495int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496{
5497 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005498 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005499
5500 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005501
5502 if( ssl->hostname_len + 1 == 0 )
5503 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5504
Mansour Moufidc531b4a2015-02-15 17:35:38 -05005505 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506
Paul Bakkerb15b8512012-01-13 13:44:06 +00005507 if( ssl->hostname == NULL )
5508 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5509
Paul Bakker3c2122f2013-06-24 19:03:14 +02005510 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005511 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005512
Paul Bakker40ea7de2009-05-03 10:18:48 +00005513 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005514
5515 return( 0 );
5516}
5517
Paul Bakker5701cdc2012-09-27 21:49:42 +00005518void ssl_set_sni( ssl_context *ssl,
5519 int (*f_sni)(void *, ssl_context *,
5520 const unsigned char *, size_t),
5521 void *p_sni )
5522{
5523 ssl->f_sni = f_sni;
5524 ssl->p_sni = p_sni;
5525}
Paul Bakker0be444a2013-08-27 21:55:01 +02005526#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005527
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005528#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005529int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005530{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005531 size_t cur_len, tot_len;
5532 const char **p;
5533
5534 /*
5535 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5536 * truncated". Check lengths now rather than later.
5537 */
5538 tot_len = 0;
5539 for( p = protos; *p != NULL; p++ )
5540 {
5541 cur_len = strlen( *p );
5542 tot_len += cur_len;
5543
5544 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5545 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5546 }
5547
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005548 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005549
5550 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005551}
5552
5553const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5554{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005555 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005556}
5557#endif /* POLARSSL_SSL_ALPN */
5558
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005559static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005560{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005561 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005562 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005563 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005564 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005565 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005566
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005567#if defined(POLARSSL_SSL_PROTO_DTLS)
5568 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5569 minor < SSL_MINOR_VERSION_2 )
5570 {
5571 return( -1 );
5572 }
5573#else
5574 ((void) ssl);
5575#endif
5576
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005577 return( 0 );
5578}
5579
5580int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5581{
5582 if( ssl_check_version( ssl, major, minor ) != 0 )
5583 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5584
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005585 ssl->max_major_ver = major;
5586 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005587
5588 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005589}
5590
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005591int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005592{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005593 if( ssl_check_version( ssl, major, minor ) != 0 )
5594 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005595
5596 ssl->min_major_ver = major;
5597 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005598
5599 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005600}
5601
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005602#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5603void ssl_set_fallback( ssl_context *ssl, char fallback )
5604{
5605 ssl->fallback = fallback;
5606}
5607#endif
5608
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005609#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5610void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5611{
5612 ssl->encrypt_then_mac = etm;
5613}
5614#endif
5615
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005616#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5617void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5618{
5619 ssl->extended_ms = ems;
5620}
5621#endif
5622
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01005623void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
5624{
5625 ssl->arc4_disabled = arc4;
5626}
5627
Paul Bakker05decb22013-08-15 13:33:48 +02005628#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005629int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5630{
Paul Bakker77e257e2013-12-16 15:29:52 +01005631 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005632 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005633 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005634 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005635 }
5636
5637 ssl->mfl_code = mfl_code;
5638
5639 return( 0 );
5640}
Paul Bakker05decb22013-08-15 13:33:48 +02005641#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005642
Paul Bakker1f2bc622013-08-15 13:45:55 +02005643#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005644int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005645{
Paul Bakker8c1ede62013-07-19 14:14:37 +02005646 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005647
5648 return( 0 );
5649}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005650#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005651
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01005652#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
5653void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
5654{
5655 ssl->split_done = split;
5656}
5657#endif
5658
Paul Bakker48916f92012-09-16 19:57:18 +00005659void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5660{
5661 ssl->allow_legacy_renegotiation = allow_legacy;
5662}
5663
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005664#if defined(POLARSSL_SSL_RENEGOTIATION)
5665void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5666{
5667 ssl->disable_renegotiation = renegotiation;
5668}
5669
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005670void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5671{
5672 ssl->renego_max_records = max_records;
5673}
5674
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005675void ssl_set_renegotiation_period( ssl_context *ssl,
5676 const unsigned char period[8] )
5677{
5678 memcpy( ssl->renego_period, period, 8 );
5679}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005680#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005681
Paul Bakkera503a632013-08-14 13:48:06 +02005682#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005683int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5684{
5685 ssl->session_tickets = use_tickets;
5686
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005687#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005688 if( ssl->endpoint == SSL_IS_CLIENT )
5689 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005690#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005691
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005692 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5693 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005694
5695 if( ssl->f_rng == NULL )
5696 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5697
5698 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005699}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005700
5701void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5702{
5703 ssl->ticket_lifetime = lifetime;
5704}
Paul Bakkera503a632013-08-14 13:48:06 +02005705#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005706
Paul Bakker5121ce52009-01-03 21:22:43 +00005707/*
5708 * SSL get accessors
5709 */
5710size_t ssl_get_bytes_avail( const ssl_context *ssl )
5711{
5712 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5713}
5714
Paul Bakkerff60ee62010-03-16 21:09:09 +00005715int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005716{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00005717 if( ssl->session != NULL )
5718 return( ssl->session->verify_result );
5719
5720 if( ssl->session_negotiate != NULL )
5721 return( ssl->session_negotiate->verify_result );
5722
5723 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005724}
5725
Paul Bakkere3166ce2011-01-27 17:40:50 +00005726const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005727{
Paul Bakker926c8e42013-03-06 10:23:34 +01005728 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005729 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005730
Paul Bakkere3166ce2011-01-27 17:40:50 +00005731 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005732}
5733
Paul Bakker43ca69c2011-01-15 17:35:19 +00005734const char *ssl_get_version( const ssl_context *ssl )
5735{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005736#if defined(POLARSSL_SSL_PROTO_DTLS)
5737 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5738 {
5739 switch( ssl->minor_ver )
5740 {
5741 case SSL_MINOR_VERSION_2:
5742 return( "DTLSv1.0" );
5743
5744 case SSL_MINOR_VERSION_3:
5745 return( "DTLSv1.2" );
5746
5747 default:
5748 return( "unknown (DTLS)" );
5749 }
5750 }
5751#endif
5752
Paul Bakker43ca69c2011-01-15 17:35:19 +00005753 switch( ssl->minor_ver )
5754 {
5755 case SSL_MINOR_VERSION_0:
5756 return( "SSLv3.0" );
5757
5758 case SSL_MINOR_VERSION_1:
5759 return( "TLSv1.0" );
5760
5761 case SSL_MINOR_VERSION_2:
5762 return( "TLSv1.1" );
5763
Paul Bakker1ef83d62012-04-11 12:09:53 +00005764 case SSL_MINOR_VERSION_3:
5765 return( "TLSv1.2" );
5766
Paul Bakker43ca69c2011-01-15 17:35:19 +00005767 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005768 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005769 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005770}
5771
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005772int ssl_get_record_expansion( const ssl_context *ssl )
5773{
5774 int transform_expansion;
5775 const ssl_transform *transform = ssl->transform_out;
5776
5777#if defined(POLARSSL_ZLIB_SUPPORT)
5778 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5779 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5780#endif
5781
5782 if( transform == NULL )
5783 return( ssl_hdr_len( ssl ) );
5784
5785 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5786 {
5787 case POLARSSL_MODE_GCM:
5788 case POLARSSL_MODE_CCM:
5789 case POLARSSL_MODE_STREAM:
5790 transform_expansion = transform->minlen;
5791 break;
5792
5793 case POLARSSL_MODE_CBC:
5794 transform_expansion = transform->maclen
5795 + cipher_get_block_size( &transform->cipher_ctx_enc );
5796 break;
5797
5798 default:
5799 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5800 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5801 }
5802
5803 return( ssl_hdr_len( ssl ) + transform_expansion );
5804}
5805
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005806#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005807const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005808{
5809 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005810 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005811
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005812 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005813}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005814#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005815
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005816#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005817int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5818{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005819 if( ssl == NULL ||
5820 dst == NULL ||
5821 ssl->session == NULL ||
5822 ssl->endpoint != SSL_IS_CLIENT )
5823 {
5824 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5825 }
5826
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005827 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005828}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005829#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005830
Paul Bakker5121ce52009-01-03 21:22:43 +00005831/*
Paul Bakker1961b702013-01-25 14:49:24 +01005832 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 */
Paul Bakker1961b702013-01-25 14:49:24 +01005834int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005835{
Paul Bakker40e46942009-01-03 21:51:57 +00005836 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005837
Paul Bakker40e46942009-01-03 21:51:57 +00005838#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005840 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005842#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005843 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005844 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005845#endif
5846
Paul Bakker1961b702013-01-25 14:49:24 +01005847 return( ret );
5848}
5849
5850/*
5851 * Perform the SSL handshake
5852 */
5853int ssl_handshake( ssl_context *ssl )
5854{
5855 int ret = 0;
5856
5857 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5858
5859 while( ssl->state != SSL_HANDSHAKE_OVER )
5860 {
5861 ret = ssl_handshake_step( ssl );
5862
5863 if( ret != 0 )
5864 break;
5865 }
5866
Paul Bakker5121ce52009-01-03 21:22:43 +00005867 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5868
5869 return( ret );
5870}
5871
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005872#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005873#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005874/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005875 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005876 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005877static int ssl_write_hello_request( ssl_context *ssl )
5878{
5879 int ret;
5880
5881 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5882
5883 ssl->out_msglen = 4;
5884 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5885 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5886
5887 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5888 {
5889 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5890 return( ret );
5891 }
5892
5893 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5894
5895 return( 0 );
5896}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005897#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005898
5899/*
5900 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005901 * - any side: calling ssl_renegotiate(),
5902 * - client: receiving a HelloRequest during ssl_read(),
5903 * - server: receiving any handshake message on server during ssl_read() after
5904 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005905 * If the handshake doesn't complete due to waiting for I/O, it will continue
5906 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005907 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005908static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005909{
5910 int ret;
5911
5912 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5913
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005914 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5915 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005916
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005917 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5918 * the ServerHello will have message_seq = 1" */
5919#if defined(POLARSSL_SSL_PROTO_DTLS)
5920 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005921 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005922 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005923 if( ssl->endpoint == SSL_IS_SERVER )
5924 ssl->handshake->out_msg_seq = 1;
5925 else
5926 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005927 }
5928#endif
5929
Paul Bakker48916f92012-09-16 19:57:18 +00005930 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005931 ssl->renego_status = SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00005932
Paul Bakker48916f92012-09-16 19:57:18 +00005933 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5934 {
5935 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5936 return( ret );
5937 }
5938
5939 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5940
5941 return( 0 );
5942}
5943
5944/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005945 * Renegotiate current connection on client,
5946 * or request renegotiation on server
5947 */
5948int ssl_renegotiate( ssl_context *ssl )
5949{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005950 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005951
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005952#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005953 /* On server, just send the request */
5954 if( ssl->endpoint == SSL_IS_SERVER )
5955 {
5956 if( ssl->state != SSL_HANDSHAKE_OVER )
5957 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5958
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005959 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005960
5961 /* Did we already try/start sending HelloRequest? */
5962 if( ssl->out_left != 0 )
5963 return( ssl_flush_output( ssl ) );
5964
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005965 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005966 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005967#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005968
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005969#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005970 /*
5971 * On client, either start the renegotiation process or,
5972 * if already in progress, continue the handshake
5973 */
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00005974 if( ssl->renego_status != SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005975 {
5976 if( ssl->state != SSL_HANDSHAKE_OVER )
5977 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5978
5979 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5980 {
5981 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5982 return( ret );
5983 }
5984 }
5985 else
5986 {
5987 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5988 {
5989 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5990 return( ret );
5991 }
5992 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005993#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005994
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005995 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005996}
5997
5998/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01005999 * Check record counters and renegotiate if they're above the limit.
6000 */
6001static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6002{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006003 if( ssl->state != SSL_HANDSHAKE_OVER ||
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006004 ssl->renego_status == SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006005 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6006 {
6007 return( 0 );
6008 }
6009
6010 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006011 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6012 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006013 {
6014 return( 0 );
6015 }
6016
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006017 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006018 return( ssl_renegotiate( ssl ) );
6019}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006020#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006021
6022/*
6023 * Receive application data decrypted from the SSL layer
6024 */
Paul Bakker23986e52011-04-24 08:57:21 +00006025int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006026{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006027 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006028 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006029
6030 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6031
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006032#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006033 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006034 {
6035 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6036 return( ret );
6037
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006038 if( ssl->handshake != NULL &&
6039 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6040 {
6041 if( ( ret = ssl_resend( ssl ) ) != 0 )
6042 return( ret );
6043 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006044 }
6045#endif
6046
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006047#if defined(POLARSSL_SSL_RENEGOTIATION)
6048 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6049 {
6050 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6051 return( ret );
6052 }
6053#endif
6054
Paul Bakker5121ce52009-01-03 21:22:43 +00006055 if( ssl->state != SSL_HANDSHAKE_OVER )
6056 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006057 ret = ssl_handshake( ssl );
6058 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6059 {
6060 record_read = 1;
6061 }
6062 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006063 {
6064 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6065 return( ret );
6066 }
6067 }
6068
6069 if( ssl->in_offt == NULL )
6070 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006071#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006072 /* Start timer if not already running */
6073 if( ssl->time_limit == 0 )
6074 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006075#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006076
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006077 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006079 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6080 {
6081 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6082 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006083
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006084 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6085 return( ret );
6086 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006087 }
6088
6089 if( ssl->in_msglen == 0 &&
6090 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6091 {
6092 /*
6093 * OpenSSL sends empty messages to randomize the IV
6094 */
6095 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6096 {
Paul Bakker831a7552011-05-18 13:32:51 +00006097 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6098 return( 0 );
6099
Paul Bakker5121ce52009-01-03 21:22:43 +00006100 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6101 return( ret );
6102 }
6103 }
6104
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006105#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006106 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6107 {
6108 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6109
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006110#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006111 if( ssl->endpoint == SSL_IS_CLIENT &&
6112 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006113 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006114 {
6115 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006116
6117 /* With DTLS, drop the packet (probably from last handshake) */
6118#if defined(POLARSSL_SSL_PROTO_DTLS)
6119 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6120 return( POLARSSL_ERR_NET_WANT_READ );
6121#endif
6122 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6123 }
6124
6125 if( ssl->endpoint == SSL_IS_SERVER &&
6126 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6127 {
6128 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6129
6130 /* With DTLS, drop the packet (probably from last handshake) */
6131#if defined(POLARSSL_SSL_PROTO_DTLS)
6132 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6133 return( POLARSSL_ERR_NET_WANT_READ );
6134#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006135 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6136 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006137#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006138
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006139 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6140 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006141 ssl->allow_legacy_renegotiation ==
6142 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006143 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006144 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006145
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006147 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006148 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006149 /*
6150 * SSLv3 does not have a "no_renegotiation" alert
6151 */
6152 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6153 return( ret );
6154 }
6155 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006156#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006157#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6158 defined(POLARSSL_SSL_PROTO_TLS1_2)
6159 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006160 {
6161 if( ( ret = ssl_send_alert_message( ssl,
6162 SSL_ALERT_LEVEL_WARNING,
6163 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6164 {
6165 return( ret );
6166 }
Paul Bakker48916f92012-09-16 19:57:18 +00006167 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006168 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006169#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6170 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006171 {
6172 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006173 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006174 }
Paul Bakker48916f92012-09-16 19:57:18 +00006175 }
6176 else
6177 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006178 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006179#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006180 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6181 ssl->endpoint == SSL_IS_CLIENT )
6182 {
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006183 ssl->renego_status = SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006184 }
6185#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006186 ret = ssl_start_renegotiation( ssl );
6187 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6188 {
6189 record_read = 1;
6190 }
6191 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006192 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006193 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006194 return( ret );
6195 }
Paul Bakker48916f92012-09-16 19:57:18 +00006196 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006197
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006198 /* If a non-handshake record was read during renego, fallthrough,
6199 * else tell the user they should call ssl_read() again */
6200 if( ! record_read )
6201 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006202 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006203 else if( ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006204 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006205
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006206 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006207 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006208 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6209 {
6210 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6211 "but not honored by client" ) );
6212 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6213 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006214 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006215 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006216#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006217
6218 /* Fatal and closure alerts handled by ssl_read_record() */
6219 if( ssl->in_msgtype == SSL_MSG_ALERT )
6220 {
6221 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6222 return( POLARSSL_ERR_NET_WANT_READ );
6223 }
6224
6225 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006226 {
6227 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006228 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006229 }
6230
6231 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006232
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006233#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006234 /* We're going to return something now, cancel timer,
6235 * except if handshake (renegotiation) is in progress */
6236 if( ssl->state == SSL_HANDSHAKE_OVER )
6237 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006238
6239 /* If we requested renego but received AppData, resend HelloRequest.
6240 * Do it now, after setting in_offt, to avoid taking this branch
6241 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006242#if defined(POLARSSL_SSL_SRV_C) && defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006243 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00006244 ssl->renego_status == SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006245 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006246 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006247 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006248 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006249 return( ret );
6250 }
6251 }
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00006252#endif /* POLARSSL_SSL_SRV_C && POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006253#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006254 }
6255
6256 n = ( len < ssl->in_msglen )
6257 ? len : ssl->in_msglen;
6258
6259 memcpy( buf, ssl->in_offt, n );
6260 ssl->in_msglen -= n;
6261
6262 if( ssl->in_msglen == 0 )
6263 /* all bytes consumed */
6264 ssl->in_offt = NULL;
6265 else
6266 /* more data available */
6267 ssl->in_offt += n;
6268
6269 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6270
Paul Bakker23986e52011-04-24 08:57:21 +00006271 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006272}
6273
6274/*
6275 * Send application data to be encrypted by the SSL layer
6276 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006277#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6278static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
6279#else
Paul Bakker23986e52011-04-24 08:57:21 +00006280int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006281#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006282{
Paul Bakker23986e52011-04-24 08:57:21 +00006283 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006284#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6285 unsigned int max_len;
6286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006287
6288 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6289
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006290#if defined(POLARSSL_SSL_RENEGOTIATION)
6291 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6292 {
6293 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6294 return( ret );
6295 }
6296#endif
6297
Paul Bakker5121ce52009-01-03 21:22:43 +00006298 if( ssl->state != SSL_HANDSHAKE_OVER )
6299 {
6300 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6301 {
6302 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6303 return( ret );
6304 }
6305 }
6306
Paul Bakker05decb22013-08-15 13:33:48 +02006307#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006308 /*
6309 * Assume mfl_code is correct since it was checked when set
6310 */
6311 max_len = mfl_code_to_length[ssl->mfl_code];
6312
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006313 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006314 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006315 */
6316 if( ssl->session_out != NULL &&
6317 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6318 {
6319 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6320 }
6321
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006322 if( len > max_len )
6323 {
6324#if defined(POLARSSL_SSL_PROTO_DTLS)
6325 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6326 {
6327 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6328 "maximum fragment length: %d > %d",
6329 len, max_len ) );
6330 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6331 }
6332 else
6333#endif
6334 len = max_len;
6335 }
6336#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006337
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 if( ssl->out_left != 0 )
6339 {
6340 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6341 {
6342 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6343 return( ret );
6344 }
6345 }
Paul Bakker887bd502011-06-08 13:10:54 +00006346 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006347 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006348 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006349 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006350 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006351
6352 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6353 {
6354 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6355 return( ret );
6356 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 }
6358
6359 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6360
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006361 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006362}
6363
6364/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006365 * Write application data, doing 1/n-1 splitting if necessary.
6366 *
6367 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006368 * then the caller will call us again with the same arguments, so
6369 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006370 */
6371#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
6372int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
6373{
6374 int ret;
6375
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01006376 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
6377 len <= 1 ||
6378 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006379 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
6380 != POLARSSL_MODE_CBC )
6381 {
6382 return( ssl_write_real( ssl, buf, len ) );
6383 }
6384
6385 if( ssl->split_done == 0 )
6386 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006387 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006388 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006389 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006390 }
6391
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01006392 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
6393 return( ret );
6394 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01006395
6396 return( ret + 1 );
6397}
6398#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
6399
6400/*
Paul Bakker5121ce52009-01-03 21:22:43 +00006401 * Notify the peer that the connection is being closed
6402 */
6403int ssl_close_notify( ssl_context *ssl )
6404{
6405 int ret;
6406
6407 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6408
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006409 if( ssl->out_left != 0 )
6410 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006411
6412 if( ssl->state == SSL_HANDSHAKE_OVER )
6413 {
Paul Bakker48916f92012-09-16 19:57:18 +00006414 if( ( ret = ssl_send_alert_message( ssl,
6415 SSL_ALERT_LEVEL_WARNING,
6416 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006418 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419 return( ret );
6420 }
6421 }
6422
6423 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6424
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006425 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006426}
6427
Paul Bakker48916f92012-09-16 19:57:18 +00006428void ssl_transform_free( ssl_transform *transform )
6429{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006430 if( transform == NULL )
6431 return;
6432
Paul Bakker48916f92012-09-16 19:57:18 +00006433#if defined(POLARSSL_ZLIB_SUPPORT)
6434 deflateEnd( &transform->ctx_deflate );
6435 inflateEnd( &transform->ctx_inflate );
6436#endif
6437
Paul Bakker84bbeb52014-07-01 14:53:22 +02006438 cipher_free( &transform->cipher_ctx_enc );
6439 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006440
Paul Bakker84bbeb52014-07-01 14:53:22 +02006441 md_free( &transform->md_ctx_enc );
6442 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006443
Paul Bakker34617722014-06-13 17:20:13 +02006444 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006445}
6446
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006447#if defined(POLARSSL_X509_CRT_PARSE_C)
6448static void ssl_key_cert_free( ssl_key_cert *key_cert )
6449{
6450 ssl_key_cert *cur = key_cert, *next;
6451
6452 while( cur != NULL )
6453 {
6454 next = cur->next;
6455
6456 if( cur->key_own_alloc )
6457 {
6458 pk_free( cur->key );
6459 polarssl_free( cur->key );
6460 }
6461 polarssl_free( cur );
6462
6463 cur = next;
6464 }
6465}
6466#endif /* POLARSSL_X509_CRT_PARSE_C */
6467
Paul Bakker48916f92012-09-16 19:57:18 +00006468void ssl_handshake_free( ssl_handshake_params *handshake )
6469{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006470 if( handshake == NULL )
6471 return;
6472
Paul Bakker48916f92012-09-16 19:57:18 +00006473#if defined(POLARSSL_DHM_C)
6474 dhm_free( &handshake->dhm_ctx );
6475#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006476#if defined(POLARSSL_ECDH_C)
6477 ecdh_free( &handshake->ecdh_ctx );
6478#endif
6479
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006480#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006481 /* explicit void pointer cast for buggy MS compiler */
6482 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006483#endif
6484
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006485#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6486 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6487 /*
6488 * Free only the linked list wrapper, not the keys themselves
6489 * since the belong to the SNI callback
6490 */
6491 if( handshake->sni_key_cert != NULL )
6492 {
6493 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6494
6495 while( cur != NULL )
6496 {
6497 next = cur->next;
6498 polarssl_free( cur );
6499 cur = next;
6500 }
6501 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006502#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006503
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006504#if defined(POLARSSL_SSL_PROTO_DTLS)
6505 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006506 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006507 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006508#endif
6509
Paul Bakker34617722014-06-13 17:20:13 +02006510 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006511}
6512
6513void ssl_session_free( ssl_session *session )
6514{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006515 if( session == NULL )
6516 return;
6517
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006518#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006519 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006520 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006521 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006522 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006523 }
Paul Bakkered27a042013-04-18 22:46:23 +02006524#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006525
Paul Bakkera503a632013-08-14 13:48:06 +02006526#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006527 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006528#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006529
Paul Bakker34617722014-06-13 17:20:13 +02006530 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006531}
6532
Paul Bakker5121ce52009-01-03 21:22:43 +00006533/*
6534 * Free an SSL context
6535 */
6536void ssl_free( ssl_context *ssl )
6537{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006538 if( ssl == NULL )
6539 return;
6540
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6542
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006543 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006545 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6546 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 }
6548
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006549 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006550 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006551 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6552 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 }
6554
Paul Bakker16770332013-10-11 09:59:44 +02006555#if defined(POLARSSL_ZLIB_SUPPORT)
6556 if( ssl->compress_buf != NULL )
6557 {
Paul Bakker34617722014-06-13 17:20:13 +02006558 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006559 polarssl_free( ssl->compress_buf );
6560 }
6561#endif
6562
Paul Bakker40e46942009-01-03 21:51:57 +00006563#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006564 mpi_free( &ssl->dhm_P );
6565 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006566#endif
6567
Paul Bakker48916f92012-09-16 19:57:18 +00006568 if( ssl->transform )
6569 {
6570 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006571 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006572 }
6573
6574 if( ssl->handshake )
6575 {
6576 ssl_handshake_free( ssl->handshake );
6577 ssl_transform_free( ssl->transform_negotiate );
6578 ssl_session_free( ssl->session_negotiate );
6579
Paul Bakker6e339b52013-07-03 13:37:05 +02006580 polarssl_free( ssl->handshake );
6581 polarssl_free( ssl->transform_negotiate );
6582 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006583 }
6584
Paul Bakkerc0463502013-02-14 11:19:38 +01006585 if( ssl->session )
6586 {
6587 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006588 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006589 }
6590
Paul Bakkera503a632013-08-14 13:48:06 +02006591#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006592 if( ssl->ticket_keys )
6593 {
6594 ssl_ticket_keys_free( ssl->ticket_keys );
6595 polarssl_free( ssl->ticket_keys );
6596 }
Paul Bakkera503a632013-08-14 13:48:06 +02006597#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006598
Paul Bakker0be444a2013-08-27 21:55:01 +02006599#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006600 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006601 {
Paul Bakker34617722014-06-13 17:20:13 +02006602 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006603 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006604 ssl->hostname_len = 0;
6605 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006606#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006607
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006608#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006609 if( ssl->psk != NULL )
6610 {
Paul Bakker34617722014-06-13 17:20:13 +02006611 polarssl_zeroize( ssl->psk, ssl->psk_len );
6612 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006613 polarssl_free( ssl->psk );
6614 polarssl_free( ssl->psk_identity );
6615 ssl->psk_len = 0;
6616 ssl->psk_identity_len = 0;
6617 }
6618#endif
6619
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006620#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006621 ssl_key_cert_free( ssl->key_cert );
6622#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006623
Paul Bakker05ef8352012-05-08 09:17:57 +00006624#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6625 if( ssl_hw_record_finish != NULL )
6626 {
6627 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6628 ssl_hw_record_finish( ssl );
6629 }
6630#endif
6631
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006632#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006633 polarssl_free( ssl->cli_id );
6634#endif
6635
Paul Bakker5121ce52009-01-03 21:22:43 +00006636 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006637
Paul Bakker86f04f42013-02-14 11:20:09 +01006638 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006639 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006640}
6641
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006642#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006643/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006644 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006645 */
6646unsigned char ssl_sig_from_pk( pk_context *pk )
6647{
6648#if defined(POLARSSL_RSA_C)
6649 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6650 return( SSL_SIG_RSA );
6651#endif
6652#if defined(POLARSSL_ECDSA_C)
6653 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6654 return( SSL_SIG_ECDSA );
6655#endif
6656 return( SSL_SIG_ANON );
6657}
6658
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006659pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6660{
6661 switch( sig )
6662 {
6663#if defined(POLARSSL_RSA_C)
6664 case SSL_SIG_RSA:
6665 return( POLARSSL_PK_RSA );
6666#endif
6667#if defined(POLARSSL_ECDSA_C)
6668 case SSL_SIG_ECDSA:
6669 return( POLARSSL_PK_ECDSA );
6670#endif
6671 default:
6672 return( POLARSSL_PK_NONE );
6673 }
6674}
Paul Bakker9af723c2014-05-01 13:03:14 +02006675#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006676
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006677/*
6678 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6679 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006680md_type_t ssl_md_alg_from_hash( unsigned char hash )
6681{
6682 switch( hash )
6683 {
6684#if defined(POLARSSL_MD5_C)
6685 case SSL_HASH_MD5:
6686 return( POLARSSL_MD_MD5 );
6687#endif
6688#if defined(POLARSSL_SHA1_C)
6689 case SSL_HASH_SHA1:
6690 return( POLARSSL_MD_SHA1 );
6691#endif
6692#if defined(POLARSSL_SHA256_C)
6693 case SSL_HASH_SHA224:
6694 return( POLARSSL_MD_SHA224 );
6695 case SSL_HASH_SHA256:
6696 return( POLARSSL_MD_SHA256 );
6697#endif
6698#if defined(POLARSSL_SHA512_C)
6699 case SSL_HASH_SHA384:
6700 return( POLARSSL_MD_SHA384 );
6701 case SSL_HASH_SHA512:
6702 return( POLARSSL_MD_SHA512 );
6703#endif
6704 default:
6705 return( POLARSSL_MD_NONE );
6706 }
6707}
6708
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006709#if defined(POLARSSL_SSL_SET_CURVES)
6710/*
6711 * Check is a curve proposed by the peer is in our list.
6712 * Return 1 if we're willing to use it, 0 otherwise.
6713 */
6714int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6715{
6716 const ecp_group_id *gid;
6717
6718 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6719 if( *gid == grp_id )
6720 return( 1 );
6721
6722 return( 0 );
6723}
Paul Bakker9af723c2014-05-01 13:03:14 +02006724#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006725
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006726#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006727int ssl_check_cert_usage( const x509_crt *cert,
6728 const ssl_ciphersuite_t *ciphersuite,
6729 int cert_endpoint )
6730{
6731#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6732 int usage = 0;
6733#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006734#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6735 const char *ext_oid;
6736 size_t ext_len;
6737#endif
6738
6739#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6740 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6741 ((void) cert);
6742 ((void) cert_endpoint);
6743#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006744
6745#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6746 if( cert_endpoint == SSL_IS_SERVER )
6747 {
6748 /* Server part of the key exchange */
6749 switch( ciphersuite->key_exchange )
6750 {
6751 case POLARSSL_KEY_EXCHANGE_RSA:
6752 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6753 usage = KU_KEY_ENCIPHERMENT;
6754 break;
6755
6756 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6757 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6758 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6759 usage = KU_DIGITAL_SIGNATURE;
6760 break;
6761
6762 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6763 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6764 usage = KU_KEY_AGREEMENT;
6765 break;
6766
6767 /* Don't use default: we want warnings when adding new values */
6768 case POLARSSL_KEY_EXCHANGE_NONE:
6769 case POLARSSL_KEY_EXCHANGE_PSK:
6770 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6771 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6772 usage = 0;
6773 }
6774 }
6775 else
6776 {
6777 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6778 usage = KU_DIGITAL_SIGNATURE;
6779 }
6780
6781 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6782 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006783#else
6784 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006785#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6786
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006787#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6788 if( cert_endpoint == SSL_IS_SERVER )
6789 {
6790 ext_oid = OID_SERVER_AUTH;
6791 ext_len = OID_SIZE( OID_SERVER_AUTH );
6792 }
6793 else
6794 {
6795 ext_oid = OID_CLIENT_AUTH;
6796 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6797 }
6798
6799 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6800 return( -1 );
6801#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6802
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006803 return( 0 );
6804}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006805#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006806
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006807/*
6808 * Convert version numbers to/from wire format
6809 * and, for DTLS, to/from TLS equivalent.
6810 *
6811 * For TLS this is the identity.
6812 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6813 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6814 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6815 */
6816void ssl_write_version( int major, int minor, int transport,
6817 unsigned char ver[2] )
6818{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006819#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006820 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006821 {
6822 if( minor == SSL_MINOR_VERSION_2 )
6823 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6824
6825 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6826 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6827 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006828 else
6829#else
6830 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006831#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006832 {
6833 ver[0] = (unsigned char) major;
6834 ver[1] = (unsigned char) minor;
6835 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006836}
6837
6838void ssl_read_version( int *major, int *minor, int transport,
6839 const unsigned char ver[2] )
6840{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006841#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006842 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006843 {
6844 *major = 255 - ver[0] + 2;
6845 *minor = 255 - ver[1] + 1;
6846
6847 if( *minor == SSL_MINOR_VERSION_1 )
6848 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6849 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006850 else
6851#else
6852 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006853#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006854 {
6855 *major = ver[0];
6856 *minor = ver[1];
6857 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006858}
6859
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006860#endif /* POLARSSL_SSL_TLS_C */