blob: de85b676cb8b5345ab0d5008dcd8c8bdbc64532e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081
82/*
83 * Timers (WIP)
84 */
85#if defined(POLARSSL_TIMING_C)
86/*
87 * Start a timer.
88 * Passing millisecs = 0 cancels a running timer.
89 * The timer is already running iff time_limit != 0.
90 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020091static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092{
93 ssl->time_limit = millisecs;
94 get_timer( &ssl->time_info, 1 );
95}
96
97/*
98 * Return -1 is timer is expired, 0 if it isn't.
99 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +0200100static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101{
102 if( ssl->time_limit != 0 &&
103 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
104 {
105 return( -1 );
106 }
107
108 return( 0 );
109}
110#endif
111
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200112#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200113/*
114 * Double the retransmit timeout value, within the allowed range,
115 * returning -1 if the maximum value has already been reached.
116 */
117static int ssl_double_retransmit_timeout( ssl_context *ssl )
118{
119 uint32_t new_timeout;
120
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200121 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200122 return( -1 );
123
124 new_timeout = 2 * ssl->handshake->retransmit_timeout;
125
126 /* Avoid arithmetic overflow and range overflow */
127 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200128 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200129 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200130 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200131 }
132
133 ssl->handshake->retransmit_timeout = new_timeout;
134 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
135 ssl->handshake->retransmit_timeout ) );
136
137 return( 0 );
138}
139
140static void ssl_reset_retransmit_timeout( ssl_context *ssl )
141{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200142 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200143 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
144 ssl->handshake->retransmit_timeout ) );
145}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200146#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200147
Paul Bakker05decb22013-08-15 13:33:48 +0200148#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200149/*
150 * Convert max_fragment_length codes to length.
151 * RFC 6066 says:
152 * enum{
153 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
154 * } MaxFragmentLength;
155 * and we add 0 -> extension unused
156 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200157static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200158{
159 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
160 512, /* SSL_MAX_FRAG_LEN_512 */
161 1024, /* SSL_MAX_FRAG_LEN_1024 */
162 2048, /* SSL_MAX_FRAG_LEN_2048 */
163 4096, /* SSL_MAX_FRAG_LEN_4096 */
164};
Paul Bakker05decb22013-08-15 13:33:48 +0200165#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200166
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200167static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
168{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200169 ssl_session_free( dst );
170 memcpy( dst, src, sizeof( ssl_session ) );
171
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200173 if( src->peer_cert != NULL )
174 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200175 int ret;
176
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200177 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
178 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200179 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
180
Paul Bakkerb6b09562013-09-18 14:17:41 +0200181 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200182
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200183 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
184 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200185 {
186 polarssl_free( dst->peer_cert );
187 dst->peer_cert = NULL;
188 return( ret );
189 }
190 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192
Paul Bakkera503a632013-08-14 13:48:06 +0200193#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200194 if( src->ticket != NULL )
195 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200196 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
197 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200198 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
199
200 memcpy( dst->ticket, src->ticket, src->ticket_len );
201 }
Paul Bakkera503a632013-08-14 13:48:06 +0200202#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200203
204 return( 0 );
205}
206
Paul Bakker05ef8352012-05-08 09:17:57 +0000207#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200208int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200209 const unsigned char *key_enc, const unsigned char *key_dec,
210 size_t keylen,
211 const unsigned char *iv_enc, const unsigned char *iv_dec,
212 size_t ivlen,
213 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200214 size_t maclen ) = NULL;
215int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
216int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
217int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
218int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
219int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200220#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222/*
223 * Key material generation
224 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200225#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200226static int ssl3_prf( const unsigned char *secret, size_t slen,
227 const char *label,
228 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000229 unsigned char *dstbuf, size_t dlen )
230{
231 size_t i;
232 md5_context md5;
233 sha1_context sha1;
234 unsigned char padding[16];
235 unsigned char sha1sum[20];
236 ((void)label);
237
Paul Bakker5b4af392014-06-26 12:09:34 +0200238 md5_init( &md5 );
239 sha1_init( &sha1 );
240
Paul Bakker5f70b252012-09-13 14:23:06 +0000241 /*
242 * SSLv3:
243 * block =
244 * MD5( secret + SHA1( 'A' + secret + random ) ) +
245 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
246 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
247 * ...
248 */
249 for( i = 0; i < dlen / 16; i++ )
250 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200251 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000252
253 sha1_starts( &sha1 );
254 sha1_update( &sha1, padding, 1 + i );
255 sha1_update( &sha1, secret, slen );
256 sha1_update( &sha1, random, rlen );
257 sha1_finish( &sha1, sha1sum );
258
259 md5_starts( &md5 );
260 md5_update( &md5, secret, slen );
261 md5_update( &md5, sha1sum, 20 );
262 md5_finish( &md5, dstbuf + i * 16 );
263 }
264
Paul Bakker5b4af392014-06-26 12:09:34 +0200265 md5_free( &md5 );
266 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000267
Paul Bakker34617722014-06-13 17:20:13 +0200268 polarssl_zeroize( padding, sizeof( padding ) );
269 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000270
271 return( 0 );
272}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200273#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000274
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200275#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200276static int tls1_prf( const unsigned char *secret, size_t slen,
277 const char *label,
278 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000279 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 size_t nb, hs;
282 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200283 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 unsigned char tmp[128];
285 unsigned char h_i[20];
286
287 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000288 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290 hs = ( slen + 1 ) / 2;
291 S1 = secret;
292 S2 = secret + slen - hs;
293
294 nb = strlen( label );
295 memcpy( tmp + 20, label, nb );
296 memcpy( tmp + 20 + nb, random, rlen );
297 nb += rlen;
298
299 /*
300 * First compute P_md5(secret,label+random)[0..dlen]
301 */
302 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
303
304 for( i = 0; i < dlen; i += 16 )
305 {
306 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
307 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
308
309 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
310
311 for( j = 0; j < k; j++ )
312 dstbuf[i + j] = h_i[j];
313 }
314
315 /*
316 * XOR out with P_sha1(secret,label+random)[0..dlen]
317 */
318 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
319
320 for( i = 0; i < dlen; i += 20 )
321 {
322 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
323 sha1_hmac( S2, hs, tmp, 20, tmp );
324
325 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
326
327 for( j = 0; j < k; j++ )
328 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
329 }
330
Paul Bakker34617722014-06-13 17:20:13 +0200331 polarssl_zeroize( tmp, sizeof( tmp ) );
332 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
334 return( 0 );
335}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200336#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200338#if defined(POLARSSL_SSL_PROTO_TLS1_2)
339#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200340static int tls_prf_sha256( const unsigned char *secret, size_t slen,
341 const char *label,
342 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000343 unsigned char *dstbuf, size_t dlen )
344{
345 size_t nb;
346 size_t i, j, k;
347 unsigned char tmp[128];
348 unsigned char h_i[32];
349
350 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
351 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
352
353 nb = strlen( label );
354 memcpy( tmp + 32, label, nb );
355 memcpy( tmp + 32 + nb, random, rlen );
356 nb += rlen;
357
358 /*
359 * Compute P_<hash>(secret, label + random)[0..dlen]
360 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200361 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000362
363 for( i = 0; i < dlen; i += 32 )
364 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200365 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
366 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000367
368 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
369
370 for( j = 0; j < k; j++ )
371 dstbuf[i + j] = h_i[j];
372 }
373
Paul Bakker34617722014-06-13 17:20:13 +0200374 polarssl_zeroize( tmp, sizeof( tmp ) );
375 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000376
377 return( 0 );
378}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200379#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000380
Paul Bakker9e36f042013-06-30 14:34:05 +0200381#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200382static int tls_prf_sha384( const unsigned char *secret, size_t slen,
383 const char *label,
384 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000385 unsigned char *dstbuf, size_t dlen )
386{
387 size_t nb;
388 size_t i, j, k;
389 unsigned char tmp[128];
390 unsigned char h_i[48];
391
392 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
393 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
394
395 nb = strlen( label );
396 memcpy( tmp + 48, label, nb );
397 memcpy( tmp + 48 + nb, random, rlen );
398 nb += rlen;
399
400 /*
401 * Compute P_<hash>(secret, label + random)[0..dlen]
402 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200403 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404
405 for( i = 0; i < dlen; i += 48 )
406 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200407 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
408 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000409
410 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
411
412 for( j = 0; j < k; j++ )
413 dstbuf[i + j] = h_i[j];
414 }
415
Paul Bakker34617722014-06-13 17:20:13 +0200416 polarssl_zeroize( tmp, sizeof( tmp ) );
417 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418
419 return( 0 );
420}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421#endif /* POLARSSL_SHA512_C */
422#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423
Paul Bakker66d5d072014-06-17 16:39:18 +0200424static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200425
426#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
427 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200428static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#endif
Paul Bakker380da532012-04-18 16:10:25 +0000430
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200432static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
433static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435
436#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200437static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
438static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200439#endif
440
441#if defined(POLARSSL_SSL_PROTO_TLS1_2)
442#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200443static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
444static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
445static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200446#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100447
Paul Bakker9e36f042013-06-30 14:34:05 +0200448#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200449static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
450static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
451static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100452#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200453#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000454
Paul Bakker5121ce52009-01-03 21:22:43 +0000455int ssl_derive_keys( ssl_context *ssl )
456{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200457 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 unsigned char keyblk[256];
460 unsigned char *key1;
461 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100462 unsigned char *mac_enc;
463 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200464 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100465 const cipher_info_t *cipher_info;
466 const md_info_t *md_info;
467
Paul Bakker48916f92012-09-16 19:57:18 +0000468 ssl_session *session = ssl->session_negotiate;
469 ssl_transform *transform = ssl->transform_negotiate;
470 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
472 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
473
Paul Bakker68884e32013-01-07 18:20:04 +0100474 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
475 if( cipher_info == NULL )
476 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200477 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100478 transform->ciphersuite_info->cipher ) );
479 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
480 }
481
482 md_info = md_info_from_type( transform->ciphersuite_info->mac );
483 if( md_info == NULL )
484 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200485 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100486 transform->ciphersuite_info->mac ) );
487 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
488 }
489
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000491 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000492 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200493#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000494 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000495 {
Paul Bakker48916f92012-09-16 19:57:18 +0000496 handshake->tls_prf = ssl3_prf;
497 handshake->calc_verify = ssl_calc_verify_ssl;
498 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000499 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200500 else
501#endif
502#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
503 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000504 {
Paul Bakker48916f92012-09-16 19:57:18 +0000505 handshake->tls_prf = tls1_prf;
506 handshake->calc_verify = ssl_calc_verify_tls;
507 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000508 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200509 else
510#endif
511#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200512#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200513 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
514 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000515 {
Paul Bakker48916f92012-09-16 19:57:18 +0000516 handshake->tls_prf = tls_prf_sha384;
517 handshake->calc_verify = ssl_calc_verify_tls_sha384;
518 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000519 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000520 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200521#endif
522#if defined(POLARSSL_SHA256_C)
523 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000524 {
Paul Bakker48916f92012-09-16 19:57:18 +0000525 handshake->tls_prf = tls_prf_sha256;
526 handshake->calc_verify = ssl_calc_verify_tls_sha256;
527 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000528 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200529 else
530#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200531#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200532 {
533 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200534 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200535 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000536
537 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * SSLv3:
539 * master =
540 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
541 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
542 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200543 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200544 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000545 * master = PRF( premaster, "master secret", randbytes )[0..47]
546 */
Paul Bakker0a597072012-09-25 21:55:46 +0000547 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 {
Paul Bakker48916f92012-09-16 19:57:18 +0000549 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
550 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000551
Paul Bakker48916f92012-09-16 19:57:18 +0000552 handshake->tls_prf( handshake->premaster, handshake->pmslen,
553 "master secret",
554 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Paul Bakker34617722014-06-13 17:20:13 +0200556 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 }
558 else
559 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
560
561 /*
562 * Swap the client and server random values.
563 */
Paul Bakker48916f92012-09-16 19:57:18 +0000564 memcpy( tmp, handshake->randbytes, 64 );
565 memcpy( handshake->randbytes, tmp + 32, 32 );
566 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200567 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 /*
570 * SSLv3:
571 * key block =
572 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
573 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
574 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
575 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
576 * ...
577 *
578 * TLSv1:
579 * key block = PRF( master, "key expansion", randbytes )
580 */
Paul Bakker48916f92012-09-16 19:57:18 +0000581 handshake->tls_prf( session->master, 48, "key expansion",
582 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Paul Bakker48916f92012-09-16 19:57:18 +0000584 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
585 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
586 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
587 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
589
Paul Bakker34617722014-06-13 17:20:13 +0200590 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
592 /*
593 * Determine the appropriate key, IV and MAC length.
594 */
Paul Bakker68884e32013-01-07 18:20:04 +0100595
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200596 transform->keylen = cipher_info->key_length / 8;
597
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200598 if( cipher_info->mode == POLARSSL_MODE_GCM ||
599 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200601 transform->maclen = 0;
602
Paul Bakker68884e32013-01-07 18:20:04 +0100603 transform->ivlen = 12;
604 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200605
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200606 /* Minimum length is expicit IV + tag */
607 transform->minlen = transform->ivlen - transform->fixed_ivlen
608 + ( transform->ciphersuite_info->flags &
609 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100610 }
611 else
612 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200613 int ret;
614
615 /* Initialize HMAC contexts */
616 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
617 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100618 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200619 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
620 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100621 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200623 /* Get MAC length */
624 transform->maclen = md_get_size( md_info );
625
626#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
627 /*
628 * If HMAC is to be truncated, we shall keep the leftmost bytes,
629 * (rfc 6066 page 13 or rfc 2104 section 4),
630 * so we only need to adjust the length here.
631 */
632 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
633 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
634#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
635
636 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100637 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200639 /* Minimum length */
640 if( cipher_info->mode == POLARSSL_MODE_STREAM )
641 transform->minlen = transform->maclen;
642 else
Paul Bakker68884e32013-01-07 18:20:04 +0100643 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200644 /*
645 * GenericBlockCipher:
646 * first multiple of blocklen greater than maclen
647 * + IV except for SSL3 and TLS 1.0
648 */
649 transform->minlen = transform->maclen
650 + cipher_info->block_size
651 - transform->maclen % cipher_info->block_size;
652
653#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
654 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
655 ssl->minor_ver == SSL_MINOR_VERSION_1 )
656 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100657 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200658#endif
659#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
660 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
661 ssl->minor_ver == SSL_MINOR_VERSION_3 )
662 {
663 transform->minlen += transform->ivlen;
664 }
665 else
666#endif
667 {
668 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
669 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
670 }
Paul Bakker68884e32013-01-07 18:20:04 +0100671 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
673
674 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000675 transform->keylen, transform->minlen, transform->ivlen,
676 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677
678 /*
679 * Finally setup the cipher contexts, IVs and MAC secrets.
680 */
681 if( ssl->endpoint == SSL_IS_CLIENT )
682 {
Paul Bakker48916f92012-09-16 19:57:18 +0000683 key1 = keyblk + transform->maclen * 2;
684 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
Paul Bakker68884e32013-01-07 18:20:04 +0100686 mac_enc = keyblk;
687 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000688
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000689 /*
690 * This is not used in TLS v1.1.
691 */
Paul Bakker48916f92012-09-16 19:57:18 +0000692 iv_copy_len = ( transform->fixed_ivlen ) ?
693 transform->fixed_ivlen : transform->ivlen;
694 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
695 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000696 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 }
698 else
699 {
Paul Bakker48916f92012-09-16 19:57:18 +0000700 key1 = keyblk + transform->maclen * 2 + transform->keylen;
701 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
Paul Bakker68884e32013-01-07 18:20:04 +0100703 mac_enc = keyblk + transform->maclen;
704 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000706 /*
707 * This is not used in TLS v1.1.
708 */
Paul Bakker48916f92012-09-16 19:57:18 +0000709 iv_copy_len = ( transform->fixed_ivlen ) ?
710 transform->fixed_ivlen : transform->ivlen;
711 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
712 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000713 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 }
715
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100717 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
718 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100719 if( transform->maclen > sizeof transform->mac_enc )
720 {
721 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200722 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100723 }
724
Paul Bakker68884e32013-01-07 18:20:04 +0100725 memcpy( transform->mac_enc, mac_enc, transform->maclen );
726 memcpy( transform->mac_dec, mac_dec, transform->maclen );
727 }
728 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200729#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200730#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
731 defined(POLARSSL_SSL_PROTO_TLS1_2)
732 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100733 {
734 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
735 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
736 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200737 else
738#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200739 {
740 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200741 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200742 }
Paul Bakker68884e32013-01-07 18:20:04 +0100743
Paul Bakker05ef8352012-05-08 09:17:57 +0000744#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200745 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000746 {
747 int ret = 0;
748
749 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
750
Paul Bakker07eb38b2012-12-19 14:42:06 +0100751 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
752 transform->iv_enc, transform->iv_dec,
753 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100754 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100755 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000756 {
757 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200758 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000759 }
760 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200761#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000762
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200763 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
764 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000765 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200766 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
767 return( ret );
768 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200770 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
771 cipher_info ) ) != 0 )
772 {
773 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
774 return( ret );
775 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200776
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200777 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
778 cipher_info->key_length,
779 POLARSSL_ENCRYPT ) ) != 0 )
780 {
781 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
782 return( ret );
783 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200784
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200785 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
786 cipher_info->key_length,
787 POLARSSL_DECRYPT ) ) != 0 )
788 {
789 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
790 return( ret );
791 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200792
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200793#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200794 if( cipher_info->mode == POLARSSL_MODE_CBC )
795 {
796 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
797 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200798 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200799 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
800 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200801 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200802
803 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
804 POLARSSL_PADDING_NONE ) ) != 0 )
805 {
806 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
807 return( ret );
808 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000809 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200810#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000811
Paul Bakker34617722014-06-13 17:20:13 +0200812 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000813
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814#if defined(POLARSSL_ZLIB_SUPPORT)
815 // Initialize compression
816 //
Paul Bakker48916f92012-09-16 19:57:18 +0000817 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000818 {
Paul Bakker16770332013-10-11 09:59:44 +0200819 if( ssl->compress_buf == NULL )
820 {
821 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
822 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
823 if( ssl->compress_buf == NULL )
824 {
825 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
826 SSL_BUFFER_LEN ) );
827 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
828 }
829 }
830
Paul Bakker2770fbd2012-07-03 13:30:23 +0000831 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
832
Paul Bakker48916f92012-09-16 19:57:18 +0000833 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
834 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000835
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200836 if( deflateInit( &transform->ctx_deflate,
837 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000838 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000839 {
840 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
841 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
842 }
843 }
844#endif /* POLARSSL_ZLIB_SUPPORT */
845
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
847
848 return( 0 );
849}
850
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200851#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000852void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000853{
854 md5_context md5;
855 sha1_context sha1;
856 unsigned char pad_1[48];
857 unsigned char pad_2[48];
858
Paul Bakker380da532012-04-18 16:10:25 +0000859 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
Paul Bakker48916f92012-09-16 19:57:18 +0000861 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
862 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863
Paul Bakker380da532012-04-18 16:10:25 +0000864 memset( pad_1, 0x36, 48 );
865 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
Paul Bakker48916f92012-09-16 19:57:18 +0000867 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000868 md5_update( &md5, pad_1, 48 );
869 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000870
Paul Bakker380da532012-04-18 16:10:25 +0000871 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000872 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000873 md5_update( &md5, pad_2, 48 );
874 md5_update( &md5, hash, 16 );
875 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
Paul Bakker48916f92012-09-16 19:57:18 +0000877 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_update( &sha1, pad_1, 40 );
879 sha1_finish( &sha1, hash + 16 );
880
881 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000882 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000883 sha1_update( &sha1, pad_2, 40 );
884 sha1_update( &sha1, hash + 16, 20 );
885 sha1_finish( &sha1, hash + 16 );
886
887 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
888 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
889
Paul Bakker5b4af392014-06-26 12:09:34 +0200890 md5_free( &md5 );
891 sha1_free( &sha1 );
892
Paul Bakker380da532012-04-18 16:10:25 +0000893 return;
894}
Paul Bakker9af723c2014-05-01 13:03:14 +0200895#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000896
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000898void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
899{
900 md5_context md5;
901 sha1_context sha1;
902
903 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
904
Paul Bakker48916f92012-09-16 19:57:18 +0000905 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
906 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000907
Paul Bakker48916f92012-09-16 19:57:18 +0000908 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000909 sha1_finish( &sha1, hash + 16 );
910
911 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
912 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
913
Paul Bakker5b4af392014-06-26 12:09:34 +0200914 md5_free( &md5 );
915 sha1_free( &sha1 );
916
Paul Bakker380da532012-04-18 16:10:25 +0000917 return;
918}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200919#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000920
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
922#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000923void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
924{
Paul Bakker9e36f042013-06-30 14:34:05 +0200925 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000926
927 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
928
Paul Bakker9e36f042013-06-30 14:34:05 +0200929 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
930 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000931
932 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
933 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
934
Paul Bakker5b4af392014-06-26 12:09:34 +0200935 sha256_free( &sha256 );
936
Paul Bakker380da532012-04-18 16:10:25 +0000937 return;
938}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200939#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000940
Paul Bakker9e36f042013-06-30 14:34:05 +0200941#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000942void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
943{
Paul Bakker9e36f042013-06-30 14:34:05 +0200944 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000945
946 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
947
Paul Bakker9e36f042013-06-30 14:34:05 +0200948 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
949 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000950
Paul Bakkerca4ab492012-04-18 14:23:57 +0000951 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
953
Paul Bakker5b4af392014-06-26 12:09:34 +0200954 sha512_free( &sha512 );
955
Paul Bakker5121ce52009-01-03 21:22:43 +0000956 return;
957}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200958#endif /* POLARSSL_SHA512_C */
959#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000960
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200961#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
963{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 unsigned char *p = ssl->handshake->premaster;
965 unsigned char *end = p + sizeof( ssl->handshake->premaster );
966
967 /*
968 * PMS = struct {
969 * opaque other_secret<0..2^16-1>;
970 * opaque psk<0..2^16-1>;
971 * };
972 * with "other_secret" depending on the particular key exchange
973 */
974#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
975 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
976 {
977 if( end - p < 2 + (int) ssl->psk_len )
978 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
979
980 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
981 *(p++) = (unsigned char)( ssl->psk_len );
982 p += ssl->psk_len;
983 }
984 else
985#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200986#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
987 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
988 {
989 /*
990 * other_secret already set by the ClientKeyExchange message,
991 * and is 48 bytes long
992 */
993 *p++ = 0;
994 *p++ = 48;
995 p += 48;
996 }
997 else
998#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200999#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1000 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1001 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001002 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001003 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001005 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001006 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001007 p + 2, &len,
1008 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001009 {
1010 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1011 return( ret );
1012 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001013 *(p++) = (unsigned char)( len >> 8 );
1014 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015 p += len;
1016
1017 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1018 }
1019 else
1020#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1021#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1022 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1023 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001024 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001025 size_t zlen;
1026
1027 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001028 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001029 ssl->f_rng, ssl->p_rng ) ) != 0 )
1030 {
1031 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1032 return( ret );
1033 }
1034
1035 *(p++) = (unsigned char)( zlen >> 8 );
1036 *(p++) = (unsigned char)( zlen );
1037 p += zlen;
1038
1039 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1040 }
1041 else
1042#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1043 {
1044 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001045 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001046 }
1047
1048 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001049 if( end - p < 2 + (int) ssl->psk_len )
1050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1051
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001052 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1053 *(p++) = (unsigned char)( ssl->psk_len );
1054 memcpy( p, ssl->psk, ssl->psk_len );
1055 p += ssl->psk_len;
1056
1057 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1058
1059 return( 0 );
1060}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001061#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001062
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001063#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001064/*
1065 * SSLv3.0 MAC functions
1066 */
Paul Bakker68884e32013-01-07 18:20:04 +01001067static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1068 unsigned char *buf, size_t len,
1069 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001070{
1071 unsigned char header[11];
1072 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001073 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001074 int md_size = md_get_size( md_ctx->md_info );
1075 int md_type = md_get_type( md_ctx->md_info );
1076
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001077 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001078 if( md_type == POLARSSL_MD_MD5 )
1079 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001080 else
Paul Bakker68884e32013-01-07 18:20:04 +01001081 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
1083 memcpy( header, ctr, 8 );
1084 header[ 8] = (unsigned char) type;
1085 header[ 9] = (unsigned char)( len >> 8 );
1086 header[10] = (unsigned char)( len );
1087
Paul Bakker68884e32013-01-07 18:20:04 +01001088 memset( padding, 0x36, padlen );
1089 md_starts( md_ctx );
1090 md_update( md_ctx, secret, md_size );
1091 md_update( md_ctx, padding, padlen );
1092 md_update( md_ctx, header, 11 );
1093 md_update( md_ctx, buf, len );
1094 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakker68884e32013-01-07 18:20:04 +01001096 memset( padding, 0x5C, padlen );
1097 md_starts( md_ctx );
1098 md_update( md_ctx, secret, md_size );
1099 md_update( md_ctx, padding, padlen );
1100 md_update( md_ctx, buf + len, md_size );
1101 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001102}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001103#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001104
Paul Bakker5121ce52009-01-03 21:22:43 +00001105/*
1106 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001107 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001108static int ssl_encrypt_buf( ssl_context *ssl )
1109{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001110 const cipher_mode_t mode = cipher_get_cipher_mode(
1111 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1114
1115 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001116 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001118#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1119 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1120 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001121 if( mode != POLARSSL_MODE_GCM &&
1122 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001124#if defined(POLARSSL_SSL_PROTO_SSL3)
1125 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1126 {
1127 ssl_mac( &ssl->transform_out->md_ctx_enc,
1128 ssl->transform_out->mac_enc,
1129 ssl->out_msg, ssl->out_msglen,
1130 ssl->out_ctr, ssl->out_msgtype );
1131 }
1132 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001133#endif
1134#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001135 defined(POLARSSL_SSL_PROTO_TLS1_2)
1136 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1137 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001138 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1139 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1140 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001141 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1142 ssl->out_msg, ssl->out_msglen );
1143 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1144 ssl->out_msg + ssl->out_msglen );
1145 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1146 }
1147 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001148#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001149 {
1150 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001151 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001152 }
1153
1154 SSL_DEBUG_BUF( 4, "computed mac",
1155 ssl->out_msg + ssl->out_msglen,
1156 ssl->transform_out->maclen );
1157
1158 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001159 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001160#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001161
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001162 /*
1163 * Encrypt
1164 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001165#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001166 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001168 int ret;
1169 size_t olen = 0;
1170
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1172 "including %d bytes of padding",
1173 ssl->out_msglen, 0 ) );
1174
1175 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1176 ssl->out_msg, ssl->out_msglen );
1177
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001179 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001180 ssl->transform_out->ivlen,
1181 ssl->out_msg, ssl->out_msglen,
1182 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001183 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001184 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001185 return( ret );
1186 }
1187
1188 if( ssl->out_msglen != olen )
1189 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001190 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001191 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 }
Paul Bakker68884e32013-01-07 18:20:04 +01001194 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001195#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001196#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1197 if( mode == POLARSSL_MODE_GCM ||
1198 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001200 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001201 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 unsigned char *enc_msg;
1203 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1205 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206
Paul Bakkerca4ab492012-04-18 14:23:57 +00001207 memcpy( add_data, ssl->out_ctr, 8 );
1208 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001209 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1210 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001211 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1212 add_data[12] = ssl->out_msglen & 0xFF;
1213
1214 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1215 add_data, 13 );
1216
Paul Bakker68884e32013-01-07 18:20:04 +01001217 /*
1218 * Generate IV
1219 */
1220 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001221 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1222 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001223 if( ret != 0 )
1224 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001225
Paul Bakker68884e32013-01-07 18:20:04 +01001226 memcpy( ssl->out_iv,
1227 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1228 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001229
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001230 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001231 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001232
Paul Bakker68884e32013-01-07 18:20:04 +01001233 /*
1234 * Fix pointer positions and message length with added IV
1235 */
1236 enc_msg = ssl->out_msg;
1237 enc_msglen = ssl->out_msglen;
1238 ssl->out_msglen += ssl->transform_out->ivlen -
1239 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001240
Paul Bakker68884e32013-01-07 18:20:04 +01001241 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1242 "including %d bytes of padding",
1243 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001244
Paul Bakker68884e32013-01-07 18:20:04 +01001245 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1246 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001247
Paul Bakker68884e32013-01-07 18:20:04 +01001248 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001249 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001250 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001251 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1252 ssl->transform_out->iv_enc,
1253 ssl->transform_out->ivlen,
1254 add_data, 13,
1255 enc_msg, enc_msglen,
1256 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001257 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001259 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001260 return( ret );
1261 }
1262
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001263 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001264 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001265 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001266 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001267 }
1268
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001270
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001271 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001272 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001275#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1276 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001277 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001280 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001281 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001282
Paul Bakker48916f92012-09-16 19:57:18 +00001283 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1284 ssl->transform_out->ivlen;
1285 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 padlen = 0;
1287
1288 for( i = 0; i <= padlen; i++ )
1289 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1290
1291 ssl->out_msglen += padlen + 1;
1292
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001293 enc_msglen = ssl->out_msglen;
1294 enc_msg = ssl->out_msg;
1295
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001296#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001297 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001298 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1299 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001300 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001301 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001302 {
1303 /*
1304 * Generate IV
1305 */
Paul Bakker48916f92012-09-16 19:57:18 +00001306 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1307 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001308 if( ret != 0 )
1309 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001310
Paul Bakker92be97b2013-01-02 17:30:03 +01001311 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001312 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313
1314 /*
1315 * Fix pointer positions and message length with added IV
1316 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001317 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001319 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001320 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001321#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001322
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001325 ssl->out_msglen, ssl->transform_out->ivlen,
1326 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
1328 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001329 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001331 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001332 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001333 ssl->transform_out->ivlen,
1334 enc_msg, enc_msglen,
1335 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001336 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001337 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001338 return( ret );
1339 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001340
Paul Bakkercca5b812013-08-31 17:40:26 +02001341 if( enc_msglen != olen )
1342 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001343 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001344 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001345 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001346
1347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001348 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1349 {
1350 /*
1351 * Save IV in SSL3 and TLS1
1352 */
1353 memcpy( ssl->transform_out->iv_enc,
1354 ssl->transform_out->cipher_ctx_enc.iv,
1355 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001357#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001359 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001360#endif /* POLARSSL_CIPHER_MODE_CBC &&
1361 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001362 {
1363 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001364 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001365 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001366
1367 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1368
1369 return( 0 );
1370}
1371
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001372#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001373
Paul Bakker5121ce52009-01-03 21:22:43 +00001374static int ssl_decrypt_buf( ssl_context *ssl )
1375{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001376 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001377 const cipher_mode_t mode = cipher_get_cipher_mode(
1378 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001379#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1380 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1381 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1382 size_t padlen = 0, correct = 1;
1383#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
1385 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1386
Paul Bakker48916f92012-09-16 19:57:18 +00001387 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001388 {
1389 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001390 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001391 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001392 }
1393
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001394#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001395 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001396 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001397 int ret;
1398 size_t olen = 0;
1399
Paul Bakker68884e32013-01-07 18:20:04 +01001400 padlen = 0;
1401
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001402 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001403 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001404 ssl->transform_in->ivlen,
1405 ssl->in_msg, ssl->in_msglen,
1406 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001407 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001408 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001409 return( ret );
1410 }
1411
1412 if( ssl->in_msglen != olen )
1413 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001414 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001415 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001416 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 }
Paul Bakker68884e32013-01-07 18:20:04 +01001418 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001419#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001420#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1421 if( mode == POLARSSL_MODE_GCM ||
1422 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424 int ret;
1425 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001426 unsigned char *dec_msg;
1427 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001428 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001429 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1430 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001431 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1432 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001433
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001434 if( ssl->in_msglen < explicit_iv_len + taglen )
1435 {
1436 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1437 "+ taglen (%d)", ssl->in_msglen,
1438 explicit_iv_len, taglen ) );
1439 return( POLARSSL_ERR_SSL_INVALID_MAC );
1440 }
1441 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1442
Paul Bakker68884e32013-01-07 18:20:04 +01001443 dec_msg = ssl->in_msg;
1444 dec_msg_result = ssl->in_msg;
1445 ssl->in_msglen = dec_msglen;
1446
1447 memcpy( add_data, ssl->in_ctr, 8 );
1448 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001449 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1450 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001451 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1452 add_data[12] = ssl->in_msglen & 0xFF;
1453
1454 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1455 add_data, 13 );
1456
1457 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1458 ssl->in_iv,
1459 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1460
1461 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1462 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001463 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001464
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001465 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001466 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001467 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001468 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1469 ssl->transform_in->iv_dec,
1470 ssl->transform_in->ivlen,
1471 add_data, 13,
1472 dec_msg, dec_msglen,
1473 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001474 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001475 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001476 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1477
1478 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1479 return( POLARSSL_ERR_SSL_INVALID_MAC );
1480
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001481 return( ret );
1482 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001483
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001484 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001485 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001486 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001487 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001488 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001491#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001492#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1493 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001494 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001495 {
Paul Bakker45829992013-01-03 14:52:21 +01001496 /*
1497 * Decrypt and check the padding
1498 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001499 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500 unsigned char *dec_msg;
1501 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001502 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001503 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001504 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001505
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 /*
Paul Bakker45829992013-01-03 14:52:21 +01001507 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 */
Paul Bakker48916f92012-09-16 19:57:18 +00001509 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 {
1511 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001512 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001513 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001514 }
1515
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001516#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001517 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1518 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001519#endif
Paul Bakker45829992013-01-03 14:52:21 +01001520
1521 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1522 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1523 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001524 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1525 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1526 ssl->transform_in->ivlen,
1527 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001528 return( POLARSSL_ERR_SSL_INVALID_MAC );
1529 }
1530
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001531 dec_msglen = ssl->in_msglen;
1532 dec_msg = ssl->in_msg;
1533 dec_msg_result = ssl->in_msg;
1534
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001535#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001536 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001537 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001539 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001540 {
Paul Bakker48916f92012-09-16 19:57:18 +00001541 dec_msglen -= ssl->transform_in->ivlen;
1542 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543
Paul Bakker48916f92012-09-16 19:57:18 +00001544 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001545 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001546 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001547#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001548
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001549 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001550 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001551 ssl->transform_in->ivlen,
1552 dec_msg, dec_msglen,
1553 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001554 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001555 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001556 return( ret );
1557 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558
Paul Bakkercca5b812013-08-31 17:40:26 +02001559 if( dec_msglen != olen )
1560 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001561 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001562 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001563 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001564
1565#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001566 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1567 {
1568 /*
1569 * Save IV in SSL3 and TLS1
1570 */
1571 memcpy( ssl->transform_in->iv_dec,
1572 ssl->transform_in->cipher_ctx_dec.iv,
1573 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001574 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001575#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001576
1577 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001578
1579 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1580 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001581#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001582 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1583 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001584#endif
Paul Bakker45829992013-01-03 14:52:21 +01001585 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001586 correct = 0;
1587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001589#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1591 {
Paul Bakker48916f92012-09-16 19:57:18 +00001592 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001594#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1596 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001597 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001598#endif
Paul Bakker45829992013-01-03 14:52:21 +01001599 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 }
1601 }
1602 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001603#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001604#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1605 defined(POLARSSL_SSL_PROTO_TLS1_2)
1606 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 {
1608 /*
Paul Bakker45829992013-01-03 14:52:21 +01001609 * TLSv1+: always check the padding up to the first failure
1610 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001612 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001613 size_t padding_idx = ssl->in_msglen - padlen - 1;
1614
Paul Bakker956c9e02013-12-19 14:42:28 +01001615 /*
1616 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001617 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001618 *
Paul Bakker61885c72014-04-25 12:59:03 +02001619 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1620 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001621 *
1622 * In both cases we reset padding_idx to a safe value (0) to
1623 * prevent out-of-buffer reads.
1624 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001625 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001626 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1627 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001628
1629 padding_idx *= correct;
1630
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001631 for( i = 1; i <= 256; i++ )
1632 {
1633 real_count &= ( i <= padlen );
1634 pad_count += real_count *
1635 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1636 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001637
1638 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001639
Paul Bakkerd66f0702013-01-31 16:57:45 +01001640#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001641 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001642 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001643#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001644 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001646 else
1647#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1648 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001649 {
1650 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001651 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001652 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001654 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001655#endif /* POLARSSL_CIPHER_MODE_CBC &&
1656 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001657 {
1658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
1662 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1663 ssl->in_msg, ssl->in_msglen );
1664
1665 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001666 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001667 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1669 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1670 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001671 if( mode != POLARSSL_MODE_GCM &&
1672 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001673 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001674 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1675
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001678 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1679 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001681 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001683#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001684 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1685 {
1686 ssl_mac( &ssl->transform_in->md_ctx_dec,
1687 ssl->transform_in->mac_dec,
1688 ssl->in_msg, ssl->in_msglen,
1689 ssl->in_ctr, ssl->in_msgtype );
1690 }
1691 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001692#endif /* POLARSSL_SSL_PROTO_SSL3 */
1693#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 defined(POLARSSL_SSL_PROTO_TLS1_2)
1695 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1696 {
1697 /*
1698 * Process MAC and always update for padlen afterwards to make
1699 * total time independent of padlen
1700 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001701 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702 *
1703 * Known timing attacks:
1704 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1705 *
1706 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1707 * correctly. (We round down instead of up, so -56 is the correct
1708 * value for our calculations instead of -55)
1709 */
1710 size_t j, extra_run = 0;
1711 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1712 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001713
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001715
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001716 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1717 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1718 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001719 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1720 ssl->in_msglen );
1721 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1722 ssl->in_msg + ssl->in_msglen );
1723 for( j = 0; j < extra_run; j++ )
1724 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001725
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001726 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1727 }
1728 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001729#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001730 POLARSSL_SSL_PROTO_TLS1_2 */
1731 {
1732 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001733 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001734 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1737 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1738 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001740 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 ssl->transform_in->maclen ) != 0 )
1742 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001745#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001746 correct = 0;
1747 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 /*
1750 * Finally check the correct flag
1751 */
1752 if( correct == 0 )
1753 return( POLARSSL_ERR_SSL_INVALID_MAC );
1754 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001755#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
1757 if( ssl->in_msglen == 0 )
1758 {
1759 ssl->nb_zero++;
1760
1761 /*
1762 * Three or more empty messages may be a DoS attack
1763 * (excessive CPU consumption).
1764 */
1765 if( ssl->nb_zero > 3 )
1766 {
1767 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1768 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001769 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001770 }
1771 }
1772 else
1773 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001774
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001775#if defined(POLARSSL_SSL_PROTO_DTLS)
1776 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001777 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001778 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001779 }
1780 else
1781#endif
1782 {
1783 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1784 if( ++ssl->in_ctr[i - 1] != 0 )
1785 break;
1786
1787 /* The loop goes to its end iff the counter is wrapping */
1788 if( i == ssl_ep_len( ssl ) )
1789 {
1790 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1791 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1792 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001793 }
1794
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1796
1797 return( 0 );
1798}
1799
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800#if defined(POLARSSL_ZLIB_SUPPORT)
1801/*
1802 * Compression/decompression functions
1803 */
1804static int ssl_compress_buf( ssl_context *ssl )
1805{
1806 int ret;
1807 unsigned char *msg_post = ssl->out_msg;
1808 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001809 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810
1811 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1812
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001813 if( len_pre == 0 )
1814 return( 0 );
1815
Paul Bakker2770fbd2012-07-03 13:30:23 +00001816 memcpy( msg_pre, ssl->out_msg, len_pre );
1817
1818 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1819 ssl->out_msglen ) );
1820
1821 SSL_DEBUG_BUF( 4, "before compression: output payload",
1822 ssl->out_msg, ssl->out_msglen );
1823
Paul Bakker48916f92012-09-16 19:57:18 +00001824 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1825 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1826 ssl->transform_out->ctx_deflate.next_out = msg_post;
1827 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001828
Paul Bakker48916f92012-09-16 19:57:18 +00001829 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001830 if( ret != Z_OK )
1831 {
1832 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1833 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1834 }
1835
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001836 ssl->out_msglen = SSL_BUFFER_LEN -
1837 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838
Paul Bakker2770fbd2012-07-03 13:30:23 +00001839 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1840 ssl->out_msglen ) );
1841
1842 SSL_DEBUG_BUF( 4, "after compression: output payload",
1843 ssl->out_msg, ssl->out_msglen );
1844
1845 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1846
1847 return( 0 );
1848}
1849
1850static int ssl_decompress_buf( ssl_context *ssl )
1851{
1852 int ret;
1853 unsigned char *msg_post = ssl->in_msg;
1854 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001855 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001856
1857 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1858
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001859 if( len_pre == 0 )
1860 return( 0 );
1861
Paul Bakker2770fbd2012-07-03 13:30:23 +00001862 memcpy( msg_pre, ssl->in_msg, len_pre );
1863
1864 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1865 ssl->in_msglen ) );
1866
1867 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1868 ssl->in_msg, ssl->in_msglen );
1869
Paul Bakker48916f92012-09-16 19:57:18 +00001870 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1871 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1872 ssl->transform_in->ctx_inflate.next_out = msg_post;
1873 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001874
Paul Bakker48916f92012-09-16 19:57:18 +00001875 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001876 if( ret != Z_OK )
1877 {
1878 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1879 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1880 }
1881
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001882 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1883 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001884
Paul Bakker2770fbd2012-07-03 13:30:23 +00001885 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1886 ssl->in_msglen ) );
1887
1888 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1889 ssl->in_msg, ssl->in_msglen );
1890
1891 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1892
1893 return( 0 );
1894}
1895#endif /* POLARSSL_ZLIB_SUPPORT */
1896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001898 * Fill the input message buffer by appending data to it.
1899 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001900 *
1901 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1902 * available (from this read and/or a previous one). Otherwise, an error code
1903 * is returned (possibly EOF or WANT_READ).
1904 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001905 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1906 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1907 * since we always read a whole datagram at once.
1908 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001909 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001910 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 */
Paul Bakker23986e52011-04-24 08:57:21 +00001912int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001913{
Paul Bakker23986e52011-04-24 08:57:21 +00001914 int ret;
1915 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
1917 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1918
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001919 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1920 {
1921 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1922 "or ssl_set_bio_timeout()" ) );
1923 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1924 }
1925
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001926 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001927 {
1928 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1929 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1930 }
1931
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001932#if defined(POLARSSL_SSL_PROTO_DTLS)
1933 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001935 uint32_t timeout;
1936
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001937 /*
1938 * The point is, we need to always read a full datagram at once, so we
1939 * sometimes read more then requested, and handle the additional data.
1940 * It could be the rest of the current record (while fetching the
1941 * header) and/or some other records in the same datagram.
1942 */
1943
1944 /*
1945 * Move to the next record in the already read datagram if applicable
1946 */
1947 if( ssl->next_record_offset != 0 )
1948 {
1949 if( ssl->in_left < ssl->next_record_offset )
1950 {
1951 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1952 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1953 }
1954
1955 ssl->in_left -= ssl->next_record_offset;
1956
1957 if( ssl->in_left != 0 )
1958 {
1959 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1960 ssl->next_record_offset ) );
1961 memmove( ssl->in_hdr,
1962 ssl->in_hdr + ssl->next_record_offset,
1963 ssl->in_left );
1964 }
1965
1966 ssl->next_record_offset = 0;
1967 }
1968
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1970 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001971
1972 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001973 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001974 */
1975 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001976 {
1977 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001978 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001979 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001980
1981 /*
1982 * A record can't be split accross datagrams. If we need to read but
1983 * are not at the beginning of a new record, the caller did something
1984 * wrong.
1985 */
1986 if( ssl->in_left != 0 )
1987 {
1988 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1989 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1990 }
1991
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001992 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001993
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001994 /*
1995 * Don't even try to read if time's out already.
1996 * This avoids by-passing the timer when repeatedly receiving messages
1997 * that will end up being dropped.
1998 */
1999 if( ssl_check_timer( ssl ) != 0 )
2000 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002001 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002002 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002003 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2004
2005 if( ssl->state != SSL_HANDSHAKE_OVER )
2006 timeout = ssl->handshake->retransmit_timeout;
2007 else
2008 timeout = ssl->read_timeout;
2009
2010 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2011
2012 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2013 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2014 timeout );
2015 else
2016 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2017
2018 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2019
2020 if( ret == 0 )
2021 return( POLARSSL_ERR_SSL_CONN_EOF );
2022 }
2023
2024 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2025 {
2026 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2027 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002028
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002029 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002030 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002031 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2032 {
2033 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2034 return( POLARSSL_ERR_NET_TIMEOUT );
2035 }
2036
2037 if( ( ret = ssl_resend( ssl ) ) != 0 )
2038 {
2039 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2040 return( ret );
2041 }
2042
2043 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002044 }
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002045 }
2046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 if( ret < 0 )
2048 return( ret );
2049
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002050 ssl->in_left = ret;
2051 }
2052 else
2053#endif
2054 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002055 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2056 ssl->in_left, nb_want ) );
2057
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002058 while( ssl->in_left < nb_want )
2059 {
2060 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002061 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002062
2063 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2064 ssl->in_left, nb_want ) );
2065 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2066
2067 if( ret == 0 )
2068 return( POLARSSL_ERR_SSL_CONN_EOF );
2069
2070 if( ret < 0 )
2071 return( ret );
2072
2073 ssl->in_left += ret;
2074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 }
2076
2077 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2078
2079 return( 0 );
2080}
2081
2082/*
2083 * Flush any data not yet written
2084 */
2085int ssl_flush_output( ssl_context *ssl )
2086{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002087 int ret;
2088 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
2090 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2091
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002092 if( ssl->f_send == NULL )
2093 {
2094 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2095 "or ssl_set_bio_timeout()" ) );
2096 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2097 }
2098
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002099 /* Avoid incrementing counter if data is flushed */
2100 if( ssl->out_left == 0 )
2101 {
2102 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2103 return( 0 );
2104 }
2105
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 while( ssl->out_left > 0 )
2107 {
2108 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002109 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002111 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2112 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002113 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002114
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2116
2117 if( ret <= 0 )
2118 return( ret );
2119
2120 ssl->out_left -= ret;
2121 }
2122
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002123 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002124 if( ++ssl->out_ctr[i - 1] != 0 )
2125 break;
2126
2127 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002128 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002129 {
2130 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2131 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2132 }
2133
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2135
2136 return( 0 );
2137}
2138
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002139/*
2140 * Functions to handle the DTLS retransmission state machine
2141 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002142#if defined(POLARSSL_SSL_PROTO_DTLS)
2143/*
2144 * Append current handshake message to current outgoing flight
2145 */
2146static int ssl_flight_append( ssl_context *ssl )
2147{
2148 ssl_flight_item *msg;
2149
2150 /* Allocate space for current message */
2151 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2152 {
2153 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2154 sizeof( ssl_flight_item ) ) );
2155 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2156 }
2157
2158 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2159 {
2160 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2161 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2162 }
2163
2164 /* Copy current handshake message with headers */
2165 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2166 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002167 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002168 msg->next = NULL;
2169
2170 /* Append to the current flight */
2171 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002172 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002173 else
2174 {
2175 ssl_flight_item *cur = ssl->handshake->flight;
2176 while( cur->next != NULL )
2177 cur = cur->next;
2178 cur->next = msg;
2179 }
2180
2181 return( 0 );
2182}
2183
2184/*
2185 * Free the current flight of handshake messages
2186 */
2187static void ssl_flight_free( ssl_flight_item *flight )
2188{
2189 ssl_flight_item *cur = flight;
2190 ssl_flight_item *next;
2191
2192 while( cur != NULL )
2193 {
2194 next = cur->next;
2195
2196 polarssl_free( cur->p );
2197 polarssl_free( cur );
2198
2199 cur = next;
2200 }
2201}
2202
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002203#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2204static void ssl_dtls_replay_reset( ssl_context *ssl );
2205#endif
2206
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002207/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002208 * Swap transform_out and out_ctr with the alternative ones
2209 */
2210static void ssl_swap_epochs( ssl_context *ssl )
2211{
2212 ssl_transform *tmp_transform;
2213 unsigned char tmp_out_ctr[8];
2214
2215 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2216 {
2217 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2218 return;
2219 }
2220
2221 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2222
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002223 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002224 tmp_transform = ssl->transform_out;
2225 ssl->transform_out = ssl->handshake->alt_transform_out;
2226 ssl->handshake->alt_transform_out = tmp_transform;
2227
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002228 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002229 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2230 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2231 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002232
2233 /* Adjust to the newly activated transform */
2234 if( ssl->transform_out != NULL &&
2235 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2236 {
2237 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2238 ssl->transform_out->fixed_ivlen;
2239 }
2240 else
2241 ssl->out_msg = ssl->out_iv;
2242
2243#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2244 if( ssl_hw_record_activate != NULL )
2245 {
2246 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2247 {
2248 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2249 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2250 }
2251 }
2252#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002253}
2254
2255/*
2256 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002257 *
2258 * Need to remember the current message in case flush_output returns
2259 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002260 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002261 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002262int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002263{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002264 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002265
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002266 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2267 {
2268 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2269
2270 ssl->handshake->cur_msg = ssl->handshake->flight;
2271 ssl_swap_epochs( ssl );
2272
2273 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2274 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002275
2276 while( ssl->handshake->cur_msg != NULL )
2277 {
2278 int ret;
2279 ssl_flight_item *cur = ssl->handshake->cur_msg;
2280
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002281 /* Swap epochs before sending Finished: we can't do it after
2282 * sending ChangeCipherSpec, in case write returns WANT_READ.
2283 * Must be done before copying, may change out_msg pointer */
2284 if( cur->type == SSL_MSG_HANDSHAKE &&
2285 cur->p[0] == SSL_HS_FINISHED )
2286 {
2287 ssl_swap_epochs( ssl );
2288 }
2289
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002290 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002291 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002292 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002293
2294 ssl->handshake->cur_msg = cur->next;
2295
2296 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2297
2298 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2299 {
2300 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2301 return( ret );
2302 }
2303 }
2304
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002305 if( ssl->state == SSL_HANDSHAKE_OVER )
2306 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2307 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002308 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002309 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002310 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2311 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002312
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002313 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002314
2315 return( 0 );
2316}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002317
2318/*
2319 * To be called when the last message of an incoming flight is received.
2320 */
2321void ssl_recv_flight_completed( ssl_context *ssl )
2322{
2323 /* We won't need to resend that one any more */
2324 ssl_flight_free( ssl->handshake->flight );
2325 ssl->handshake->flight = NULL;
2326 ssl->handshake->cur_msg = NULL;
2327
2328 /* The next incoming flight will start with this msg_seq */
2329 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2330
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002331 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002332 ssl_set_timer( ssl, 0 );
2333
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002334 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2335 ssl->in_msg[0] == SSL_HS_FINISHED )
2336 {
2337 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2338 }
2339 else
2340 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2341}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002342
2343/*
2344 * To be called when the last message of an outgoing flight is send.
2345 */
2346void ssl_send_flight_completed( ssl_context *ssl )
2347{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002348 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002349 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002350
2351 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2352 ssl->in_msg[0] == SSL_HS_FINISHED )
2353 {
2354 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2355 }
2356 else
2357 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2358}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002359#endif /* POLARSSL_SSL_PROTO_DTLS */
2360
Paul Bakker5121ce52009-01-03 21:22:43 +00002361/*
2362 * Record layer functions
2363 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002364
2365/*
2366 * Write current record.
2367 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2368 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002369int ssl_write_record( ssl_context *ssl )
2370{
Paul Bakker05ef8352012-05-08 09:17:57 +00002371 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002372 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
2374 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2375
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002376#if defined(POLARSSL_SSL_PROTO_DTLS)
2377 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2378 ssl->handshake != NULL &&
2379 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2380 {
2381 ; /* Skip special handshake treatment when resending */
2382 }
2383 else
2384#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2386 {
2387 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2388 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2389 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2390
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002391 /*
2392 * DTLS has additional fields in the Handshake layer,
2393 * between the length field and the actual payload:
2394 * uint16 message_seq;
2395 * uint24 fragment_offset;
2396 * uint24 fragment_length;
2397 */
2398#if defined(POLARSSL_SSL_PROTO_DTLS)
2399 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2400 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002401 /* Make room for the additional DTLS fields */
2402 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002403 ssl->out_msglen += 8;
2404 len += 8;
2405
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002406 /* Write message_seq and update it, except for HelloRequest */
2407 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2408 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002409 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2410 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2411 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002412 }
2413 else
2414 {
2415 ssl->out_msg[4] = 0;
2416 ssl->out_msg[5] = 0;
2417 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002418
2419 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2420 memset( ssl->out_msg + 6, 0x00, 3 );
2421 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002422 }
2423#endif /* POLARSSL_SSL_PROTO_DTLS */
2424
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002425 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2426 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002429 /* Save handshake and CCS messages for resending */
2430#if defined(POLARSSL_SSL_PROTO_DTLS)
2431 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2432 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002433 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002434 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2435 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2436 {
2437 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2438 {
2439 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2440 return( ret );
2441 }
2442 }
2443#endif
2444
Paul Bakker2770fbd2012-07-03 13:30:23 +00002445#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002446 if( ssl->transform_out != NULL &&
2447 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002448 {
2449 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2450 {
2451 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2452 return( ret );
2453 }
2454
2455 len = ssl->out_msglen;
2456 }
2457#endif /*POLARSSL_ZLIB_SUPPORT */
2458
Paul Bakker05ef8352012-05-08 09:17:57 +00002459#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002460 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002462 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
Paul Bakker05ef8352012-05-08 09:17:57 +00002464 ret = ssl_hw_record_write( ssl );
2465 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2466 {
2467 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002468 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002469 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002470
2471 if( ret == 0 )
2472 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002473 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002474#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002475 if( !done )
2476 {
2477 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002478 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2479 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002480
2481 ssl->out_len[0] = (unsigned char)( len >> 8 );
2482 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002483
Paul Bakker48916f92012-09-16 19:57:18 +00002484 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002485 {
2486 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2487 {
2488 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2489 return( ret );
2490 }
2491
2492 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002493 ssl->out_len[0] = (unsigned char)( len >> 8 );
2494 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002495 }
2496
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002497 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002498
2499 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2500 "version = [%d:%d], msglen = %d",
2501 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002502 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002503
2504 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002505 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
2507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2509 {
2510 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2511 return( ret );
2512 }
2513
2514 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2515
2516 return( 0 );
2517}
2518
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002519#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002520/*
2521 * Mark bits in bitmask (used for DTLS HS reassembly)
2522 */
2523static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2524{
2525 unsigned int start_bits, end_bits;
2526
2527 start_bits = 8 - ( offset % 8 );
2528 if( start_bits != 8 )
2529 {
2530 size_t first_byte_idx = offset / 8;
2531
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002532 /* Special case */
2533 if( len <= start_bits )
2534 {
2535 for( ; len != 0; len-- )
2536 mask[first_byte_idx] |= 1 << ( start_bits - len );
2537
2538 /* Avoid potential issues with offset or len becoming invalid */
2539 return;
2540 }
2541
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002542 offset += start_bits; /* Now offset % 8 == 0 */
2543 len -= start_bits;
2544
2545 for( ; start_bits != 0; start_bits-- )
2546 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2547 }
2548
2549 end_bits = len % 8;
2550 if( end_bits != 0 )
2551 {
2552 size_t last_byte_idx = ( offset + len ) / 8;
2553
2554 len -= end_bits; /* Now len % 8 == 0 */
2555
2556 for( ; end_bits != 0; end_bits-- )
2557 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2558 }
2559
2560 memset( mask + offset / 8, 0xFF, len / 8 );
2561}
2562
2563/*
2564 * Check that bitmask is full
2565 */
2566static int ssl_bitmask_check( unsigned char *mask, size_t len )
2567{
2568 size_t i;
2569
2570 for( i = 0; i < len / 8; i++ )
2571 if( mask[i] != 0xFF )
2572 return( -1 );
2573
2574 for( i = 0; i < len % 8; i++ )
2575 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2576 return( -1 );
2577
2578 return( 0 );
2579}
2580
2581/*
2582 * Reassemble fragmented DTLS handshake messages.
2583 *
2584 * Use a temporary buffer for reassembly, divided in two parts:
2585 * - the first holds the reassembled message (including handshake header),
2586 * - the second holds a bitmask indicating which parts of the message
2587 * (excluding headers) have been received so far.
2588 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002589static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2590{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002591 unsigned char *msg, *bitmask;
2592 size_t frag_len, frag_off;
2593 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2594
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002595 if( ssl->handshake == NULL )
2596 {
2597 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2598 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2599 }
2600
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002601 /*
2602 * For first fragment, check size and allocate buffer
2603 */
2604 if( ssl->handshake->hs_msg == NULL )
2605 {
2606 size_t alloc_len;
2607
2608 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2609 msg_len ) );
2610
2611 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2612 {
2613 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2614 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2615 }
2616
2617 /* The bitmask needs one bit per byte of message excluding header */
2618 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2619
2620 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2621 if( ssl->handshake->hs_msg == NULL )
2622 {
2623 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2624 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2625 }
2626
2627 memset( ssl->handshake->hs_msg, 0, alloc_len );
2628
2629 /* Prepare final header: copy msg_type, length and message_seq,
2630 * then add standardised fragment_offset and fragment_length */
2631 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2632 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2633 memcpy( ssl->handshake->hs_msg + 9,
2634 ssl->handshake->hs_msg + 1, 3 );
2635 }
2636 else
2637 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002638 /* Make sure msg_type and length are consistent */
2639 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002640 {
2641 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2642 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2643 }
2644 }
2645
2646 msg = ssl->handshake->hs_msg + 12;
2647 bitmask = msg + msg_len;
2648
2649 /*
2650 * Check and copy current fragment
2651 */
2652 frag_off = ( ssl->in_msg[6] << 16 ) |
2653 ( ssl->in_msg[7] << 8 ) |
2654 ssl->in_msg[8];
2655 frag_len = ( ssl->in_msg[9] << 16 ) |
2656 ( ssl->in_msg[10] << 8 ) |
2657 ssl->in_msg[11];
2658
2659 if( frag_off + frag_len > msg_len )
2660 {
2661 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2662 frag_off, frag_len, msg_len ) );
2663 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2664 }
2665
2666 if( frag_len + 12 > ssl->in_msglen )
2667 {
2668 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2669 frag_len, ssl->in_msglen ) );
2670 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2671 }
2672
2673 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2674 frag_off, frag_len ) );
2675
2676 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2677 ssl_bitmask_set( bitmask, frag_off, frag_len );
2678
2679 /*
2680 * Do we have the complete message by now?
2681 * If yes, finalize it, else ask to read the next record.
2682 */
2683 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2684 {
2685 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2686 return( POLARSSL_ERR_NET_WANT_READ );
2687 }
2688
2689 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2690
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002691 if( ssl->in_left > ssl->next_record_offset )
2692 {
2693 /*
2694 * We've got more data in the buffer after the current record,
2695 * that we don't want to overwrite. Move it before writing the
2696 * reassembled message, and adjust in_left and next_record_offset.
2697 */
2698 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2699 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2700 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2701
2702 /* First compute and check new lengths */
2703 ssl->next_record_offset = new_remain - ssl->in_hdr;
2704 ssl->in_left = ssl->next_record_offset + remain_len;
2705
2706 if( ssl->in_left > SSL_BUFFER_LEN -
2707 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2708 {
2709 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2710 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2711 }
2712
2713 memmove( new_remain, cur_remain, remain_len );
2714 }
2715
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002716 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2717
2718 polarssl_free( ssl->handshake->hs_msg );
2719 ssl->handshake->hs_msg = NULL;
2720
2721 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2722 ssl->in_msg, ssl->in_hslen );
2723
2724 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002725}
2726#endif /* POLARSSL_SSL_PROTO_DTLS */
2727
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002728static int ssl_prepare_handshake_record( ssl_context *ssl )
2729{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002730 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2731 {
2732 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2733 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002734 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002735 }
2736
2737 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2738 ( ssl->in_msg[1] << 16 ) |
2739 ( ssl->in_msg[2] << 8 ) |
2740 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002741
2742 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2743 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002744 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002745
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002746#if defined(POLARSSL_SSL_PROTO_DTLS)
2747 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002748 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002749 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002750 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002751
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002752 /* ssl->handshake is NULL when receiving ClientHello for renego */
2753 if( ssl->handshake != NULL &&
2754 recv_msg_seq != ssl->handshake->in_msg_seq )
2755 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002756 /* No sane server ever retransmits HelloVerifyRequest */
2757 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2758 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002759 {
2760 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2761 "message_seq = %d, start_of_flight = %d",
2762 recv_msg_seq,
2763 ssl->handshake->in_flight_start_seq ) );
2764
2765 if( ( ret = ssl_resend( ssl ) ) != 0 )
2766 {
2767 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2768 return( ret );
2769 }
2770 }
2771 else
2772 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002773 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002774 "message_seq = %d, expected = %d",
2775 recv_msg_seq,
2776 ssl->handshake->in_msg_seq ) );
2777 }
2778
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002779 return( POLARSSL_ERR_NET_WANT_READ );
2780 }
2781 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002782
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002783 /* Reassemble if current message is fragmented or reassembly is
2784 * already in progress */
2785 if( ssl->in_msglen < ssl->in_hslen ||
2786 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2787 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2788 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002789 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002790 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2791
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002792 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2793 {
2794 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2795 return( ret );
2796 }
2797 }
2798 }
2799 else
2800#endif /* POLARSSL_SSL_PROTO_DTLS */
2801 /* With TLS we don't handle fragmentation (for now) */
2802 if( ssl->in_msglen < ssl->in_hslen )
2803 {
2804 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002805 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002806 }
2807
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002808 if( ssl->state != SSL_HANDSHAKE_OVER )
2809 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2810
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002811 /* Handshake message is complete, increment counter */
2812#if defined(POLARSSL_SSL_PROTO_DTLS)
2813 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2814 ssl->handshake != NULL )
2815 {
2816 ssl->handshake->in_msg_seq++;
2817 }
2818#endif
2819
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002820 return( 0 );
2821}
2822
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002823/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002824 * DTLS anti-replay: RFC 6347 4.1.2.6
2825 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002826 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2827 * Bit n is set iff record number in_window_top - n has been seen.
2828 *
2829 * Usually, in_window_top is the last record number seen and the lsb of
2830 * in_window is set. The only exception is the initial state (record number 0
2831 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002832 */
2833#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2834static void ssl_dtls_replay_reset( ssl_context *ssl )
2835{
2836 ssl->in_window_top = 0;
2837 ssl->in_window = 0;
2838}
2839
2840static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2841{
2842 return( ( (uint64_t) buf[0] << 40 ) |
2843 ( (uint64_t) buf[1] << 32 ) |
2844 ( (uint64_t) buf[2] << 24 ) |
2845 ( (uint64_t) buf[3] << 16 ) |
2846 ( (uint64_t) buf[4] << 8 ) |
2847 ( (uint64_t) buf[5] ) );
2848}
2849
2850/*
2851 * Return 0 if sequence number is acceptable, -1 otherwise
2852 */
2853int ssl_dtls_replay_check( ssl_context *ssl )
2854{
2855 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2856 uint64_t bit;
2857
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002858 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2859 return( 0 );
2860
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002861 if( rec_seqnum > ssl->in_window_top )
2862 return( 0 );
2863
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002864 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002865
2866 if( bit >= 64 )
2867 return( -1 );
2868
2869 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2870 return( -1 );
2871
2872 return( 0 );
2873}
2874
2875/*
2876 * Update replay window on new validated record
2877 */
2878void ssl_dtls_replay_update( ssl_context *ssl )
2879{
2880 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2881
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002882 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2883 return;
2884
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002885 if( rec_seqnum > ssl->in_window_top )
2886 {
2887 /* Update window_top and the contents of the window */
2888 uint64_t shift = rec_seqnum - ssl->in_window_top;
2889
2890 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002891 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002892 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002893 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002894 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002895 ssl->in_window |= 1;
2896 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002897
2898 ssl->in_window_top = rec_seqnum;
2899 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002900 else
2901 {
2902 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002903 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002904
2905 if( bit < 64 ) /* Always true, but be extra sure */
2906 ssl->in_window |= (uint64_t) 1 << bit;
2907 }
2908}
2909#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2910
2911/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002912 * ContentType type;
2913 * ProtocolVersion version;
2914 * uint16 epoch; // DTLS only
2915 * uint48 sequence_number; // DTLS only
2916 * uint16 length;
2917 */
2918static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002920 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002921 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002923 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2924
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002926 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002927 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002928
2929 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2930 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002931 ssl->in_msgtype,
2932 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002934 /* Check record type */
2935 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2936 ssl->in_msgtype != SSL_MSG_ALERT &&
2937 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2938 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2939 {
2940 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002941
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002942 if( ( ret = ssl_send_alert_message( ssl,
2943 SSL_ALERT_LEVEL_FATAL,
2944 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2945 {
2946 return( ret );
2947 }
2948
2949 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2950 }
2951
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002952#if defined(POLARSSL_SSL_PROTO_DTLS)
2953 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2954 {
2955 /* Drop unexpected ChangeCipherSpec messages */
2956 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2957 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2958 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2959 {
2960 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2961 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2962 }
2963
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002964 /* Drop unexpected ApplicationData records,
2965 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002966 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002967 ssl->state != SSL_HANDSHAKE_OVER &&
2968 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
2969 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002970 {
2971 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2972 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2973 }
2974 }
2975#endif
2976
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002977 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002978 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 {
2980 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002981 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 }
2983
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002984 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 {
2986 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002987 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 }
2989
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002990 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002991#if defined(POLARSSL_SSL_PROTO_DTLS)
2992 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2993 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002994 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002995
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002996 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002997 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002998 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002999 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003000 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003001 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003002 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003003
3004#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3005 if( ssl_dtls_replay_check( ssl ) != 0 )
3006 {
3007 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3008 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3009 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003010#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003011 }
3012#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003013
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003014 /* Check length against the size of our buffer */
3015 if( ssl->in_msglen > SSL_BUFFER_LEN
3016 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003017 {
3018 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3019 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3020 }
3021
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003022 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003023 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003025 if( ssl->in_msglen < 1 ||
3026 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 {
3028 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003029 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 }
3031 }
3032 else
3033 {
Paul Bakker48916f92012-09-16 19:57:18 +00003034 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 {
3036 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003037 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 }
3039
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003040#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003042 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 {
3044 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003045 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003047#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003048#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3049 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 /*
3051 * TLS encrypted messages can have up to 256 bytes of padding
3052 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003054 ssl->in_msglen > ssl->transform_in->minlen +
3055 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 {
3057 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003058 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003060#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003061 }
3062
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003063 return( 0 );
3064}
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003066/*
3067 * If applicable, decrypt (and decompress) record content
3068 */
3069static int ssl_prepare_record_content( ssl_context *ssl )
3070{
3071 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003072
Paul Bakker5121ce52009-01-03 21:22:43 +00003073 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003074 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003075
Paul Bakker05ef8352012-05-08 09:17:57 +00003076#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003077 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003078 {
3079 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3080
3081 ret = ssl_hw_record_read( ssl );
3082 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3083 {
3084 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003085 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003086 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003087
3088 if( ret == 0 )
3089 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003090 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003091#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003092 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 {
3094 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3095 {
3096 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3097 return( ret );
3098 }
3099
3100 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3101 ssl->in_msg, ssl->in_msglen );
3102
3103 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3104 {
3105 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003106 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003107 }
3108 }
3109
Paul Bakker2770fbd2012-07-03 13:30:23 +00003110#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003111 if( ssl->transform_in != NULL &&
3112 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003113 {
3114 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3115 {
3116 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3117 return( ret );
3118 }
3119
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003120 // TODO: what's the purpose of these lines? is in_len used?
3121 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3122 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003123 }
3124#endif /* POLARSSL_ZLIB_SUPPORT */
3125
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003126#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003127 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3128 {
3129 ssl_dtls_replay_update( ssl );
3130 }
3131#endif
3132
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003133 return( 0 );
3134}
3135
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003136static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3137
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003138/*
3139 * Read a record.
3140 *
3141 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3142 * and continue reading until a valid record is found.
3143 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003144int ssl_read_record( ssl_context *ssl )
3145{
3146 int ret;
3147
3148 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3149
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003150 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003151 {
3152 /*
3153 * Get next Handshake message in the current record
3154 */
3155 ssl->in_msglen -= ssl->in_hslen;
3156
3157 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3158 ssl->in_msglen );
3159
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003160 SSL_DEBUG_BUF( 4, "remaining content in record",
3161 ssl->in_msg, ssl->in_msglen );
3162
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003163 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3164 return( ret );
3165
3166 return( 0 );
3167 }
3168
3169 ssl->in_hslen = 0;
3170
3171 /*
3172 * Read the record header and parse it
3173 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003174#if defined(POLARSSL_SSL_PROTO_DTLS)
3175read_record_header:
3176#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003177 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3178 {
3179 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3180 return( ret );
3181 }
3182
3183 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003184 {
3185#if defined(POLARSSL_SSL_PROTO_DTLS)
3186 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3187 {
3188 /* Ignore bad record and get next one; drop the whole datagram
3189 * since current header cannot be trusted to find the next record
3190 * in current datagram */
3191 ssl->next_record_offset = 0;
3192 ssl->in_left = 0;
3193
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003194 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003195 goto read_record_header;
3196 }
3197#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003198 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003199 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003200
3201 /*
3202 * Read and optionally decrypt the message contents
3203 */
3204 if( ( ret = ssl_fetch_input( ssl,
3205 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3208 return( ret );
3209 }
3210
3211 /* Done reading this record, get ready for the next one */
3212#if defined(POLARSSL_SSL_PROTO_DTLS)
3213 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3214 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3215 else
3216#endif
3217 ssl->in_left = 0;
3218
3219 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003220 {
3221#if defined(POLARSSL_SSL_PROTO_DTLS)
3222 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3223 {
3224 /* Silently discard invalid records */
3225 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3226 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3227 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003228 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003229 goto read_record_header;
3230 }
3231
3232 return( ret );
3233 }
3234 else
3235#endif
3236 {
3237 /* Error out (and send alert) on invalid records */
3238#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3239 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3240 {
3241 ssl_send_alert_message( ssl,
3242 SSL_ALERT_LEVEL_FATAL,
3243 SSL_ALERT_MSG_BAD_RECORD_MAC );
3244 }
3245#endif
3246 return( ret );
3247 }
3248 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003249
3250 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003251 * When we sent the last flight of the handshake, we MUST respond to a
3252 * retransmit of the peer's previous flight with a retransmit. (In
3253 * practice, only the Finished message will make it, other messages
3254 * including CCS use the old transform so they're dropped as invalid.)
3255 *
3256 * If the record we received is not a handshake message, however, it
3257 * means the peer received our last flight so we can clean up
3258 * handshake info.
3259 *
3260 * This check needs to be done before prepare_handshake() due to an edge
3261 * case: if the client immediately requests renegotiation, this
3262 * finishes the current handshake first, avoiding the new ClientHello
3263 * being mistaken for an ancient message in the current handshake.
3264 */
3265#if defined(POLARSSL_SSL_PROTO_DTLS)
3266 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3267 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003268 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003269 {
3270 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3271 ssl->in_msg[0] == SSL_HS_FINISHED )
3272 {
3273 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3274
3275 if( ( ret = ssl_resend( ssl ) ) != 0 )
3276 {
3277 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3278 return( ret );
3279 }
3280
3281 return( POLARSSL_ERR_NET_WANT_READ );
3282 }
3283 else
3284 {
3285 ssl_handshake_wrapup_free_hs_transform( ssl );
3286 }
3287 }
3288#endif
3289
3290 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003291 * Handle particular types of records
3292 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3294 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003295 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3296 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 }
3298
3299 if( ssl->in_msgtype == SSL_MSG_ALERT )
3300 {
3301 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3302 ssl->in_msg[0], ssl->in_msg[1] ) );
3303
3304 /*
3305 * Ignore non-fatal alerts, except close_notify
3306 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003307 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003309 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3310 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003311 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 }
3313
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003314 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3315 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 {
3317 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003318 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 }
3320 }
3321
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3323
3324 return( 0 );
3325}
3326
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003327int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3328{
3329 int ret;
3330
3331 if( ( ret = ssl_send_alert_message( ssl,
3332 SSL_ALERT_LEVEL_FATAL,
3333 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3334 {
3335 return( ret );
3336 }
3337
3338 return( 0 );
3339}
3340
Paul Bakker0a925182012-04-16 06:46:41 +00003341int ssl_send_alert_message( ssl_context *ssl,
3342 unsigned char level,
3343 unsigned char message )
3344{
3345 int ret;
3346
3347 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3348
3349 ssl->out_msgtype = SSL_MSG_ALERT;
3350 ssl->out_msglen = 2;
3351 ssl->out_msg[0] = level;
3352 ssl->out_msg[1] = message;
3353
3354 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3355 {
3356 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3357 return( ret );
3358 }
3359
3360 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3361
3362 return( 0 );
3363}
3364
Paul Bakker5121ce52009-01-03 21:22:43 +00003365/*
3366 * Handshake functions
3367 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003368#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3369 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3370 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3371 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3372 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3373 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3374 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003375int ssl_write_certificate( ssl_context *ssl )
3376{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003377 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
3379 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3380
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003381 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003382 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3383 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003384 {
3385 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3386 ssl->state++;
3387 return( 0 );
3388 }
3389
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003390 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3391 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003392}
3393
3394int ssl_parse_certificate( ssl_context *ssl )
3395{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003396 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3397
3398 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3399
3400 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003401 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003403 {
3404 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3405 ssl->state++;
3406 return( 0 );
3407 }
3408
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003409 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3410 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003411}
3412#else
3413int ssl_write_certificate( ssl_context *ssl )
3414{
3415 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3416 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003417 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003418 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3419
3420 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3421
3422 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3424 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003425 {
3426 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3427 ssl->state++;
3428 return( 0 );
3429 }
3430
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 if( ssl->endpoint == SSL_IS_CLIENT )
3432 {
3433 if( ssl->client_auth == 0 )
3434 {
3435 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3436 ssl->state++;
3437 return( 0 );
3438 }
3439
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003440#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 /*
3442 * If using SSLv3 and got no cert, send an Alert message
3443 * (otherwise an empty Certificate message will be sent).
3444 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003445 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3447 {
3448 ssl->out_msglen = 2;
3449 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003450 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3451 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003452
3453 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3454 goto write_msg;
3455 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003456#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 }
3458 else /* SSL_IS_SERVER */
3459 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003460 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 {
3462 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003463 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 }
3465 }
3466
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003467 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003468
3469 /*
3470 * 0 . 0 handshake type
3471 * 1 . 3 handshake length
3472 * 4 . 6 length of all certs
3473 * 7 . 9 length of cert. 1
3474 * 10 . n-1 peer certificate
3475 * n . n+2 length of cert. 2
3476 * n+3 . ... upper level cert, etc.
3477 */
3478 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003479 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
Paul Bakker29087132010-03-21 21:03:34 +00003481 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 {
3483 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003484 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 {
3486 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3487 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003488 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 }
3490
3491 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3492 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3493 ssl->out_msg[i + 2] = (unsigned char)( n );
3494
3495 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3496 i += n; crt = crt->next;
3497 }
3498
3499 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3500 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3501 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3502
3503 ssl->out_msglen = i;
3504 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3505 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3506
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003507#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003508write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003509#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003510
3511 ssl->state++;
3512
3513 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3514 {
3515 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3516 return( ret );
3517 }
3518
3519 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3520
Paul Bakkered27a042013-04-18 22:46:23 +02003521 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522}
3523
3524int ssl_parse_certificate( ssl_context *ssl )
3525{
Paul Bakkered27a042013-04-18 22:46:23 +02003526 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003527 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003528 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
3530 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3531
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003532 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003535 {
3536 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3537 ssl->state++;
3538 return( 0 );
3539 }
3540
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003542 ( ssl->authmode == SSL_VERIFY_NONE ||
3543 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003545 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3547 ssl->state++;
3548 return( 0 );
3549 }
3550
3551 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3552 {
3553 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3554 return( ret );
3555 }
3556
3557 ssl->state++;
3558
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003559#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003560 /*
3561 * Check if the client sent an empty certificate
3562 */
3563 if( ssl->endpoint == SSL_IS_SERVER &&
3564 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3565 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003566 if( ssl->in_msglen == 2 &&
3567 ssl->in_msgtype == SSL_MSG_ALERT &&
3568 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3569 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003570 {
3571 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3572
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003573 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3575 return( 0 );
3576 else
Paul Bakker40e46942009-01-03 21:51:57 +00003577 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 }
3579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003580#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003582#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3583 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 if( ssl->endpoint == SSL_IS_SERVER &&
3585 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3586 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003587 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3589 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003590 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 {
3592 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3593
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003594 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003596 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 else
3598 return( 0 );
3599 }
3600 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003601#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3602 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
3604 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3605 {
3606 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003607 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 }
3609
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003610 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3611 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 {
3613 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003614 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003615 }
3616
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003617 i = ssl_hs_hdr_len( ssl );
3618
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 /*
3620 * Same message structure as in ssl_write_certificate()
3621 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003622 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003624 if( ssl->in_msg[i] != 0 ||
3625 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 {
3627 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003628 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 }
3630
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003631 /* In case we tried to reuse a session but it failed */
3632 if( ssl->session_negotiate->peer_cert != NULL )
3633 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003634 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003635 polarssl_free( ssl->session_negotiate->peer_cert );
3636 }
3637
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003638 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3639 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003640 {
3641 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003642 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003643 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003644 }
3645
Paul Bakkerb6b09562013-09-18 14:17:41 +02003646 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003648 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
3650 while( i < ssl->in_hslen )
3651 {
3652 if( ssl->in_msg[i] != 0 )
3653 {
3654 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003655 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003656 }
3657
3658 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3659 | (unsigned int) ssl->in_msg[i + 2];
3660 i += 3;
3661
3662 if( n < 128 || i + n > ssl->in_hslen )
3663 {
3664 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003665 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 }
3667
Paul Bakkerddf26b42013-09-18 13:46:23 +02003668 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3669 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 if( ret != 0 )
3671 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003672 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 return( ret );
3674 }
3675
3676 i += n;
3677 }
3678
Paul Bakker48916f92012-09-16 19:57:18 +00003679 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003680
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003681 /*
3682 * On client, make sure the server cert doesn't change during renego to
3683 * avoid "triple handshake" attack: https://secure-resumption.com/
3684 */
3685 if( ssl->endpoint == SSL_IS_CLIENT &&
3686 ssl->renegotiation == SSL_RENEGOTIATION )
3687 {
3688 if( ssl->session->peer_cert == NULL )
3689 {
3690 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3691 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3692 }
3693
3694 if( ssl->session->peer_cert->raw.len !=
3695 ssl->session_negotiate->peer_cert->raw.len ||
3696 memcmp( ssl->session->peer_cert->raw.p,
3697 ssl->session_negotiate->peer_cert->raw.p,
3698 ssl->session->peer_cert->raw.len ) != 0 )
3699 {
3700 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3701 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3702 }
3703 }
3704
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 if( ssl->authmode != SSL_VERIFY_NONE )
3706 {
3707 if( ssl->ca_chain == NULL )
3708 {
3709 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003710 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003711 }
3712
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003713 /*
3714 * Main check: verify certificate
3715 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003716 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3717 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3718 &ssl->session_negotiate->verify_result,
3719 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003720
3721 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003722 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003723 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003724 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003725
3726 /*
3727 * Secondary checks: always done, but change 'ret' only if it was 0
3728 */
3729
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003730#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003731 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003732 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003733
3734 /* If certificate uses an EC key, make sure the curve is OK */
3735 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3736 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3737 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003738 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003739 if( ret == 0 )
3740 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003741 }
3742 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003743#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003744
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003745 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3746 ciphersuite_info,
3747 ! ssl->endpoint ) != 0 )
3748 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003749 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003750 if( ret == 0 )
3751 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3752 }
3753
Paul Bakker5121ce52009-01-03 21:22:43 +00003754 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3755 ret = 0;
3756 }
3757
3758 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3759
3760 return( ret );
3761}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003762#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3763 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3764 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3765 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3766 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3767 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3768 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003769
3770int ssl_write_change_cipher_spec( ssl_context *ssl )
3771{
3772 int ret;
3773
3774 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3775
3776 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3777 ssl->out_msglen = 1;
3778 ssl->out_msg[0] = 1;
3779
Paul Bakker5121ce52009-01-03 21:22:43 +00003780 ssl->state++;
3781
3782 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3783 {
3784 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3785 return( ret );
3786 }
3787
3788 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3789
3790 return( 0 );
3791}
3792
3793int ssl_parse_change_cipher_spec( ssl_context *ssl )
3794{
3795 int ret;
3796
3797 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3798
Paul Bakker5121ce52009-01-03 21:22:43 +00003799 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3800 {
3801 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3802 return( ret );
3803 }
3804
3805 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3806 {
3807 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003808 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 }
3810
3811 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3812 {
3813 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003814 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003815 }
3816
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003817 /*
3818 * Switch to our negotiated transform and session parameters for inbound
3819 * data.
3820 */
3821 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3822 ssl->transform_in = ssl->transform_negotiate;
3823 ssl->session_in = ssl->session_negotiate;
3824
3825#if defined(POLARSSL_SSL_PROTO_DTLS)
3826 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3827 {
3828#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3829 ssl_dtls_replay_reset( ssl );
3830#endif
3831
3832 /* Increment epoch */
3833 if( ++ssl->in_epoch == 0 )
3834 {
3835 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3836 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3837 }
3838 }
3839 else
3840#endif /* POLARSSL_SSL_PROTO_DTLS */
3841 memset( ssl->in_ctr, 0, 8 );
3842
3843 /*
3844 * Set the in_msg pointer to the correct location based on IV length
3845 */
3846 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3847 {
3848 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3849 ssl->transform_negotiate->fixed_ivlen;
3850 }
3851 else
3852 ssl->in_msg = ssl->in_iv;
3853
3854#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3855 if( ssl_hw_record_activate != NULL )
3856 {
3857 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3858 {
3859 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3860 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3861 }
3862 }
3863#endif
3864
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 ssl->state++;
3866
3867 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3868
3869 return( 0 );
3870}
3871
Paul Bakker41c83d32013-03-20 14:39:14 +01003872void ssl_optimize_checksum( ssl_context *ssl,
3873 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003874{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003875 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003876
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003877#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3878 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003879 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003880 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003881 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003882#endif
3883#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3884#if defined(POLARSSL_SHA512_C)
3885 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3886 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3887 else
3888#endif
3889#if defined(POLARSSL_SHA256_C)
3890 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003891 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003892 else
3893#endif
3894#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003895 {
3896 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003897 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003898 }
Paul Bakker380da532012-04-18 16:10:25 +00003899}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003900
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003901void ssl_reset_checksum( ssl_context *ssl )
3902{
3903#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3904 defined(POLARSSL_SSL_PROTO_TLS1_1)
3905 md5_starts( &ssl->handshake->fin_md5 );
3906 sha1_starts( &ssl->handshake->fin_sha1 );
3907#endif
3908#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3909#if defined(POLARSSL_SHA256_C)
3910 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3911#endif
3912#if defined(POLARSSL_SHA512_C)
3913 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3914#endif
3915#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3916}
3917
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003918static void ssl_update_checksum_start( ssl_context *ssl,
3919 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003920{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003921#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3922 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003923 md5_update( &ssl->handshake->fin_md5 , buf, len );
3924 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003925#endif
3926#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3927#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003928 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003929#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003930#if defined(POLARSSL_SHA512_C)
3931 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003932#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003933#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003934}
3935
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003936#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3937 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003938static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3939 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003940{
Paul Bakker48916f92012-09-16 19:57:18 +00003941 md5_update( &ssl->handshake->fin_md5 , buf, len );
3942 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003943}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003944#endif
Paul Bakker380da532012-04-18 16:10:25 +00003945
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003946#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3947#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003948static void ssl_update_checksum_sha256( ssl_context *ssl,
3949 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003950{
Paul Bakker9e36f042013-06-30 14:34:05 +02003951 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003952}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003953#endif
Paul Bakker380da532012-04-18 16:10:25 +00003954
Paul Bakker9e36f042013-06-30 14:34:05 +02003955#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003956static void ssl_update_checksum_sha384( ssl_context *ssl,
3957 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003958{
Paul Bakker9e36f042013-06-30 14:34:05 +02003959 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003960}
Paul Bakker769075d2012-11-24 11:26:46 +01003961#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003962#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003963
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003964#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003965static void ssl_calc_finished_ssl(
3966 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003967{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003968 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003969 md5_context md5;
3970 sha1_context sha1;
3971
Paul Bakker5121ce52009-01-03 21:22:43 +00003972 unsigned char padbuf[48];
3973 unsigned char md5sum[16];
3974 unsigned char sha1sum[20];
3975
Paul Bakker48916f92012-09-16 19:57:18 +00003976 ssl_session *session = ssl->session_negotiate;
3977 if( !session )
3978 session = ssl->session;
3979
Paul Bakker1ef83d62012-04-11 12:09:53 +00003980 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3981
Paul Bakker48916f92012-09-16 19:57:18 +00003982 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3983 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003984
3985 /*
3986 * SSLv3:
3987 * hash =
3988 * MD5( master + pad2 +
3989 * MD5( handshake + sender + master + pad1 ) )
3990 * + SHA1( master + pad2 +
3991 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003992 */
3993
Paul Bakker90995b52013-06-24 19:20:35 +02003994#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003995 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003996 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003997#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
Paul Bakker90995b52013-06-24 19:20:35 +02003999#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004000 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004001 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004002#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
Paul Bakker3c2122f2013-06-24 19:03:14 +02004004 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4005 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
Paul Bakker1ef83d62012-04-11 12:09:53 +00004007 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
Paul Bakker3c2122f2013-06-24 19:03:14 +02004009 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004010 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004011 md5_update( &md5, padbuf, 48 );
4012 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Paul Bakker3c2122f2013-06-24 19:03:14 +02004014 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004015 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004016 sha1_update( &sha1, padbuf, 40 );
4017 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004018
Paul Bakker1ef83d62012-04-11 12:09:53 +00004019 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Paul Bakker1ef83d62012-04-11 12:09:53 +00004021 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004022 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004023 md5_update( &md5, padbuf, 48 );
4024 md5_update( &md5, md5sum, 16 );
4025 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
Paul Bakker1ef83d62012-04-11 12:09:53 +00004027 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004028 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004029 sha1_update( &sha1, padbuf , 40 );
4030 sha1_update( &sha1, sha1sum, 20 );
4031 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Paul Bakker1ef83d62012-04-11 12:09:53 +00004033 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004034
Paul Bakker5b4af392014-06-26 12:09:34 +02004035 md5_free( &md5 );
4036 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004037
Paul Bakker34617722014-06-13 17:20:13 +02004038 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4039 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4040 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004041
4042 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4043}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004044#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004045
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004046#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004047static void ssl_calc_finished_tls(
4048 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004049{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004050 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004051 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004052 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004054 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004055
Paul Bakker48916f92012-09-16 19:57:18 +00004056 ssl_session *session = ssl->session_negotiate;
4057 if( !session )
4058 session = ssl->session;
4059
Paul Bakker1ef83d62012-04-11 12:09:53 +00004060 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004061
Paul Bakker48916f92012-09-16 19:57:18 +00004062 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4063 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004064
Paul Bakker1ef83d62012-04-11 12:09:53 +00004065 /*
4066 * TLSv1:
4067 * hash = PRF( master, finished_label,
4068 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4069 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004070
Paul Bakker90995b52013-06-24 19:20:35 +02004071#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004072 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4073 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004074#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004075
Paul Bakker90995b52013-06-24 19:20:35 +02004076#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004077 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4078 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004079#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004080
4081 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004082 ? "client finished"
4083 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004084
4085 md5_finish( &md5, padbuf );
4086 sha1_finish( &sha1, padbuf + 16 );
4087
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004088 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004089 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004090
4091 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4092
Paul Bakker5b4af392014-06-26 12:09:34 +02004093 md5_free( &md5 );
4094 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004095
Paul Bakker34617722014-06-13 17:20:13 +02004096 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004097
4098 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4099}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004100#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004101
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004102#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4103#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004104static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004105 ssl_context *ssl, unsigned char *buf, int from )
4106{
4107 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004108 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004109 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004110 unsigned char padbuf[32];
4111
Paul Bakker48916f92012-09-16 19:57:18 +00004112 ssl_session *session = ssl->session_negotiate;
4113 if( !session )
4114 session = ssl->session;
4115
Paul Bakker380da532012-04-18 16:10:25 +00004116 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004117
Paul Bakker9e36f042013-06-30 14:34:05 +02004118 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004119
4120 /*
4121 * TLSv1.2:
4122 * hash = PRF( master, finished_label,
4123 * Hash( handshake ) )[0.11]
4124 */
4125
Paul Bakker9e36f042013-06-30 14:34:05 +02004126#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004127 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004128 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004129#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004130
4131 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004132 ? "client finished"
4133 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004134
Paul Bakker9e36f042013-06-30 14:34:05 +02004135 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004137 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004138 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004139
4140 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4141
Paul Bakker5b4af392014-06-26 12:09:34 +02004142 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004143
Paul Bakker34617722014-06-13 17:20:13 +02004144 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004145
4146 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4147}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004148#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004149
Paul Bakker9e36f042013-06-30 14:34:05 +02004150#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004151static void ssl_calc_finished_tls_sha384(
4152 ssl_context *ssl, unsigned char *buf, int from )
4153{
4154 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004155 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004156 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004157 unsigned char padbuf[48];
4158
Paul Bakker48916f92012-09-16 19:57:18 +00004159 ssl_session *session = ssl->session_negotiate;
4160 if( !session )
4161 session = ssl->session;
4162
Paul Bakker380da532012-04-18 16:10:25 +00004163 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004164
Paul Bakker9e36f042013-06-30 14:34:05 +02004165 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004166
4167 /*
4168 * TLSv1.2:
4169 * hash = PRF( master, finished_label,
4170 * Hash( handshake ) )[0.11]
4171 */
4172
Paul Bakker9e36f042013-06-30 14:34:05 +02004173#if !defined(POLARSSL_SHA512_ALT)
4174 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4175 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004176#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004177
4178 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004179 ? "client finished"
4180 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004181
Paul Bakker9e36f042013-06-30 14:34:05 +02004182 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004183
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004184 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004185 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004186
4187 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4188
Paul Bakker5b4af392014-06-26 12:09:34 +02004189 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004190
Paul Bakker34617722014-06-13 17:20:13 +02004191 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004192
4193 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4194}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004195#endif /* POLARSSL_SHA512_C */
4196#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004197
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004198static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004199{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004200 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004201
4202 /*
4203 * Free our handshake params
4204 */
4205 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004206 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004207 ssl->handshake = NULL;
4208
4209 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004210 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004211 */
4212 if( ssl->transform )
4213 {
4214 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004215 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004216 }
4217 ssl->transform = ssl->transform_negotiate;
4218 ssl->transform_negotiate = NULL;
4219
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004220 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4221}
4222
4223void ssl_handshake_wrapup( ssl_context *ssl )
4224{
4225 int resume = ssl->handshake->resume;
4226
4227 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4228
4229 if( ssl->renegotiation == SSL_RENEGOTIATION )
4230 {
4231 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4232 ssl->renego_records_seen = 0;
4233 }
4234
4235 /*
4236 * Free the previous session and switch in the current one
4237 */
Paul Bakker0a597072012-09-25 21:55:46 +00004238 if( ssl->session )
4239 {
4240 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004241 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004242 }
4243 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004244 ssl->session_negotiate = NULL;
4245
Paul Bakker0a597072012-09-25 21:55:46 +00004246 /*
4247 * Add cache entry
4248 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004249 if( ssl->f_set_cache != NULL &&
4250 ssl->session->length != 0 &&
4251 resume == 0 )
4252 {
Paul Bakker0a597072012-09-25 21:55:46 +00004253 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4254 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004255 }
Paul Bakker0a597072012-09-25 21:55:46 +00004256
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004257#if defined(POLARSSL_SSL_PROTO_DTLS)
4258 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4259 ssl->handshake->flight != NULL )
4260 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004261 /* Cancel handshake timer */
4262 ssl_set_timer( ssl, 0 );
4263
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004264 /* Keep last flight around in case we need to resend it:
4265 * we need the handshake and transform structures for that */
4266 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4267 }
4268 else
4269#endif
4270 ssl_handshake_wrapup_free_hs_transform( ssl );
4271
Paul Bakker48916f92012-09-16 19:57:18 +00004272 ssl->state++;
4273
4274 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4275}
4276
Paul Bakker1ef83d62012-04-11 12:09:53 +00004277int ssl_write_finished( ssl_context *ssl )
4278{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004279 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004280
4281 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4282
Paul Bakker92be97b2013-01-02 17:30:03 +01004283 /*
4284 * Set the out_msg pointer to the correct location based on IV length
4285 */
4286 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4287 {
4288 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4289 ssl->transform_negotiate->fixed_ivlen;
4290 }
4291 else
4292 ssl->out_msg = ssl->out_iv;
4293
Paul Bakker48916f92012-09-16 19:57:18 +00004294 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004295
4296 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004297 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4298
Paul Bakker48916f92012-09-16 19:57:18 +00004299 ssl->verify_data_len = hash_len;
4300 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4301
Paul Bakker5121ce52009-01-03 21:22:43 +00004302 ssl->out_msglen = 4 + hash_len;
4303 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4304 ssl->out_msg[0] = SSL_HS_FINISHED;
4305
4306 /*
4307 * In case of session resuming, invert the client and server
4308 * ChangeCipherSpec messages order.
4309 */
Paul Bakker0a597072012-09-25 21:55:46 +00004310 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 {
4312 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004313 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 else
4315 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4316 }
4317 else
4318 ssl->state++;
4319
Paul Bakker48916f92012-09-16 19:57:18 +00004320 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004321 * Switch to our negotiated transform and session parameters for outbound
4322 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004323 */
4324 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004325
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004326#if defined(POLARSSL_SSL_PROTO_DTLS)
4327 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4328 {
4329 unsigned char i;
4330
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004331 /* Remember current epoch settings for resending */
4332 ssl->handshake->alt_transform_out = ssl->transform_out;
4333 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4334
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004335 /* Set sequence_number to zero */
4336 memset( ssl->out_ctr + 2, 0, 6 );
4337
4338 /* Increment epoch */
4339 for( i = 2; i > 0; i-- )
4340 if( ++ssl->out_ctr[i - 1] != 0 )
4341 break;
4342
4343 /* The loop goes to its end iff the counter is wrapping */
4344 if( i == 0 )
4345 {
4346 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4347 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4348 }
4349 }
4350 else
4351#endif /* POLARSSL_SSL_PROTO_DTLS */
4352 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004353
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004354 ssl->transform_out = ssl->transform_negotiate;
4355 ssl->session_out = ssl->session_negotiate;
4356
Paul Bakker07eb38b2012-12-19 14:42:06 +01004357#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004358 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004359 {
4360 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4361 {
4362 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4363 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4364 }
4365 }
4366#endif
4367
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004368#if defined(POLARSSL_SSL_PROTO_DTLS)
4369 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4370 ssl_send_flight_completed( ssl );
4371#endif
4372
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4374 {
4375 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4376 return( ret );
4377 }
4378
4379 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4380
4381 return( 0 );
4382}
4383
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004384#if defined(POLARSSL_SSL_PROTO_SSL3)
4385#define SSL_MAX_HASH_LEN 36
4386#else
4387#define SSL_MAX_HASH_LEN 12
4388#endif
4389
Paul Bakker5121ce52009-01-03 21:22:43 +00004390int ssl_parse_finished( ssl_context *ssl )
4391{
Paul Bakker23986e52011-04-24 08:57:21 +00004392 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004393 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004394 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004395
4396 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4397
Paul Bakker48916f92012-09-16 19:57:18 +00004398 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
Paul Bakker5121ce52009-01-03 21:22:43 +00004400 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4401 {
4402 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4403 return( ret );
4404 }
4405
4406 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4407 {
4408 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004409 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004410 }
4411
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004412 /* There is currently no ciphersuite using another length with TLS 1.2 */
4413#if defined(POLARSSL_SSL_PROTO_SSL3)
4414 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4415 hash_len = 36;
4416 else
4417#endif
4418 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004419
4420 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004421 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 {
4423 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004424 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 }
4426
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004427 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4428 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 {
4430 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004431 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 }
4433
Paul Bakker48916f92012-09-16 19:57:18 +00004434 ssl->verify_data_len = hash_len;
4435 memcpy( ssl->peer_verify_data, buf, hash_len );
4436
Paul Bakker0a597072012-09-25 21:55:46 +00004437 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004438 {
4439 if( ssl->endpoint == SSL_IS_CLIENT )
4440 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4441
4442 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004443 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 }
4445 else
4446 ssl->state++;
4447
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004448#if defined(POLARSSL_SSL_PROTO_DTLS)
4449 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4450 ssl_recv_flight_completed( ssl );
4451#endif
4452
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4454
4455 return( 0 );
4456}
4457
Paul Bakker968afaa2014-07-09 11:09:24 +02004458static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004459{
4460 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4461
4462#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4463 defined(POLARSSL_SSL_PROTO_TLS1_1)
4464 md5_init( &handshake->fin_md5 );
4465 sha1_init( &handshake->fin_sha1 );
4466 md5_starts( &handshake->fin_md5 );
4467 sha1_starts( &handshake->fin_sha1 );
4468#endif
4469#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4470#if defined(POLARSSL_SHA256_C)
4471 sha256_init( &handshake->fin_sha256 );
4472 sha256_starts( &handshake->fin_sha256, 0 );
4473#endif
4474#if defined(POLARSSL_SHA512_C)
4475 sha512_init( &handshake->fin_sha512 );
4476 sha512_starts( &handshake->fin_sha512, 1 );
4477#endif
4478#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4479
4480 handshake->update_checksum = ssl_update_checksum_start;
4481 handshake->sig_alg = SSL_HASH_SHA1;
4482
4483#if defined(POLARSSL_DHM_C)
4484 dhm_init( &handshake->dhm_ctx );
4485#endif
4486#if defined(POLARSSL_ECDH_C)
4487 ecdh_init( &handshake->ecdh_ctx );
4488#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004489}
4490
4491static void ssl_transform_init( ssl_transform *transform )
4492{
4493 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004494
4495 cipher_init( &transform->cipher_ctx_enc );
4496 cipher_init( &transform->cipher_ctx_dec );
4497
4498 md_init( &transform->md_ctx_enc );
4499 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004500}
4501
4502void ssl_session_init( ssl_session *session )
4503{
4504 memset( session, 0, sizeof(ssl_session) );
4505}
4506
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004507static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004508{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004509 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004510 if( ssl->transform_negotiate )
4511 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004512 if( ssl->session_negotiate )
4513 ssl_session_free( ssl->session_negotiate );
4514 if( ssl->handshake )
4515 ssl_handshake_free( ssl->handshake );
4516
4517 /*
4518 * Either the pointers are now NULL or cleared properly and can be freed.
4519 * Now allocate missing structures.
4520 */
4521 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004522 {
4523 ssl->transform_negotiate =
4524 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4525 }
Paul Bakker48916f92012-09-16 19:57:18 +00004526
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004527 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004528 {
4529 ssl->session_negotiate =
4530 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4531 }
Paul Bakker48916f92012-09-16 19:57:18 +00004532
Paul Bakker82788fb2014-10-20 13:59:19 +02004533 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004534 {
4535 ssl->handshake = (ssl_handshake_params *)
4536 polarssl_malloc( sizeof(ssl_handshake_params) );
4537 }
Paul Bakker48916f92012-09-16 19:57:18 +00004538
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004539 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004540 if( ssl->handshake == NULL ||
4541 ssl->transform_negotiate == NULL ||
4542 ssl->session_negotiate == NULL )
4543 {
4544 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004545
4546 polarssl_free( ssl->handshake );
4547 polarssl_free( ssl->transform_negotiate );
4548 polarssl_free( ssl->session_negotiate );
4549
4550 ssl->handshake = NULL;
4551 ssl->transform_negotiate = NULL;
4552 ssl->session_negotiate = NULL;
4553
Paul Bakker48916f92012-09-16 19:57:18 +00004554 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4555 }
4556
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004557 /* Initialize structures */
4558 ssl_session_init( ssl->session_negotiate );
4559 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004560 ssl_handshake_params_init( ssl->handshake );
4561
4562#if defined(POLARSSL_X509_CRT_PARSE_C)
4563 ssl->handshake->key_cert = ssl->key_cert;
4564#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004565
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004566 /*
4567 * We may not know yet if we're using DTLS,
4568 * so always initiliase DTLS-specific fields.
4569 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004570#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004571 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004572
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004573 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004574 if( ssl->endpoint == SSL_IS_CLIENT )
4575 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4576 else
4577 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004578#endif
4579
Paul Bakker48916f92012-09-16 19:57:18 +00004580 return( 0 );
4581}
4582
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004583#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4584/* Dummy cookie callbacks for defaults */
4585static int ssl_cookie_write_dummy( void *ctx,
4586 unsigned char **p, unsigned char *end,
4587 const unsigned char *cli_id, size_t cli_id_len )
4588{
4589 ((void) ctx);
4590 ((void) p);
4591 ((void) end);
4592 ((void) cli_id);
4593 ((void) cli_id_len);
4594
4595 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4596}
4597
4598static int ssl_cookie_check_dummy( void *ctx,
4599 const unsigned char *cookie, size_t cookie_len,
4600 const unsigned char *cli_id, size_t cli_id_len )
4601{
4602 ((void) ctx);
4603 ((void) cookie);
4604 ((void) cookie_len);
4605 ((void) cli_id);
4606 ((void) cli_id_len);
4607
4608 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4609}
4610#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4611
Paul Bakker5121ce52009-01-03 21:22:43 +00004612/*
4613 * Initialize an SSL context
4614 */
4615int ssl_init( ssl_context *ssl )
4616{
Paul Bakker48916f92012-09-16 19:57:18 +00004617 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 int len = SSL_BUFFER_LEN;
4619
4620 memset( ssl, 0, sizeof( ssl_context ) );
4621
Paul Bakker62f2dee2012-09-28 07:31:51 +00004622 /*
4623 * Sane defaults
4624 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004625 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4626 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4627 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4628 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004629
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004630 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004631
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004632 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4633
Paul Bakker62f2dee2012-09-28 07:31:51 +00004634#if defined(POLARSSL_DHM_C)
4635 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4636 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4637 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4638 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4639 {
4640 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4641 return( ret );
4642 }
4643#endif
4644
4645 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004646 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004647 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004648 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004649 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004650
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004651 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004652 {
4653 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004654 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004655 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004656 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004657 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004658 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004659 }
4660
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004661 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4662 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004664 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4665 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4666
Paul Bakker606b4ba2013-08-14 16:52:14 +02004667#if defined(POLARSSL_SSL_SESSION_TICKETS)
4668 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4669#endif
4670
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004671#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004672 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004673#endif
4674
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004675#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4676 ssl->f_cookie_write = ssl_cookie_write_dummy;
4677 ssl->f_cookie_check = ssl_cookie_check_dummy;
4678#endif
4679
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004680#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4681 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4682#endif
4683
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004684#if defined(POLARSSL_SSL_PROTO_DTLS)
4685 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4686 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4687#endif
4688
Paul Bakker48916f92012-09-16 19:57:18 +00004689 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4690 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004691
4692 return( 0 );
4693}
4694
4695/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004696 * Reset an initialized and used SSL context for re-use while retaining
4697 * all application-set variables, function pointers and data.
4698 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004699int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004700{
Paul Bakker48916f92012-09-16 19:57:18 +00004701 int ret;
4702
Paul Bakker7eb013f2011-10-06 12:37:39 +00004703 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004704 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4705 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4706
4707 ssl->verify_data_len = 0;
4708 memset( ssl->own_verify_data, 0, 36 );
4709 memset( ssl->peer_verify_data, 0, 36 );
4710
Paul Bakker7eb013f2011-10-06 12:37:39 +00004711 ssl->in_offt = NULL;
4712
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004713 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004714 ssl->in_msgtype = 0;
4715 ssl->in_msglen = 0;
4716 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004717#if defined(POLARSSL_SSL_PROTO_DTLS)
4718 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004719 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004720#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004721#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4722 ssl_dtls_replay_reset( ssl );
4723#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004724
4725 ssl->in_hslen = 0;
4726 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004727 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004728
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004729 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004730 ssl->out_msgtype = 0;
4731 ssl->out_msglen = 0;
4732 ssl->out_left = 0;
4733
Paul Bakker48916f92012-09-16 19:57:18 +00004734 ssl->transform_in = NULL;
4735 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004736
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004737 ssl->renego_records_seen = 0;
4738
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004739 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4740 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004741
4742#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004743 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004744 {
4745 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004746 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004747 {
4748 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4749 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4750 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004751 }
4752#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004753
Paul Bakker48916f92012-09-16 19:57:18 +00004754 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004755 {
Paul Bakker48916f92012-09-16 19:57:18 +00004756 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004757 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004758 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004759 }
Paul Bakker48916f92012-09-16 19:57:18 +00004760
Paul Bakkerc0463502013-02-14 11:19:38 +01004761 if( ssl->session )
4762 {
4763 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004764 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004765 ssl->session = NULL;
4766 }
4767
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004768#if defined(POLARSSL_SSL_ALPN)
4769 ssl->alpn_chosen = NULL;
4770#endif
4771
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004772#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004773 polarssl_free( ssl->cli_id );
4774 ssl->cli_id = NULL;
4775 ssl->cli_id_len = 0;
4776#endif
4777
Paul Bakker48916f92012-09-16 19:57:18 +00004778 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4779 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004780
4781 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004782}
4783
Paul Bakkera503a632013-08-14 13:48:06 +02004784#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004785static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4786{
4787 aes_free( &tkeys->enc );
4788 aes_free( &tkeys->dec );
4789
4790 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4791}
4792
Paul Bakker7eb013f2011-10-06 12:37:39 +00004793/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004794 * Allocate and initialize ticket keys
4795 */
4796static int ssl_ticket_keys_init( ssl_context *ssl )
4797{
4798 int ret;
4799 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004800 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004801
4802 if( ssl->ticket_keys != NULL )
4803 return( 0 );
4804
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004805 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4806 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004807 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4808
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004809 aes_init( &tkeys->enc );
4810 aes_init( &tkeys->dec );
4811
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004812 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004813 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004814 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004815 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004816 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004817 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004818
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004819 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4820 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4821 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4822 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004823 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004824 polarssl_free( tkeys );
4825 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004826 }
4827
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004828 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004829 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004830 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004831 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004832 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004833 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004834
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004835 ssl->ticket_keys = tkeys;
4836
4837 return( 0 );
4838}
Paul Bakkera503a632013-08-14 13:48:06 +02004839#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004840
4841/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 * SSL set accessors
4843 */
4844void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4845{
4846 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004847
Paul Bakker606b4ba2013-08-14 16:52:14 +02004848#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004849 if( endpoint == SSL_IS_CLIENT )
4850 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004852}
4853
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004854int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004855{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004856#if defined(POLARSSL_SSL_PROTO_DTLS)
4857 if( transport == SSL_TRANSPORT_DATAGRAM )
4858 {
4859 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004860
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004861 ssl->out_hdr = ssl->out_buf;
4862 ssl->out_ctr = ssl->out_buf + 3;
4863 ssl->out_len = ssl->out_buf + 11;
4864 ssl->out_iv = ssl->out_buf + 13;
4865 ssl->out_msg = ssl->out_buf + 13;
4866
4867 ssl->in_hdr = ssl->in_buf;
4868 ssl->in_ctr = ssl->in_buf + 3;
4869 ssl->in_len = ssl->in_buf + 11;
4870 ssl->in_iv = ssl->in_buf + 13;
4871 ssl->in_msg = ssl->in_buf + 13;
4872
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004873 /* DTLS starts with TLS1.1 */
4874 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4875 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004876
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004877 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4878 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4879
4880 return( 0 );
4881 }
4882#endif
4883
4884 if( transport == SSL_TRANSPORT_STREAM )
4885 {
4886 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004887
4888 ssl->out_ctr = ssl->out_buf;
4889 ssl->out_hdr = ssl->out_buf + 8;
4890 ssl->out_len = ssl->out_buf + 11;
4891 ssl->out_iv = ssl->out_buf + 13;
4892 ssl->out_msg = ssl->out_buf + 13;
4893
4894 ssl->in_ctr = ssl->in_buf;
4895 ssl->in_hdr = ssl->in_buf + 8;
4896 ssl->in_len = ssl->in_buf + 11;
4897 ssl->in_iv = ssl->in_buf + 13;
4898 ssl->in_msg = ssl->in_buf + 13;
4899
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004900 return( 0 );
4901 }
4902
4903 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004904}
4905
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004906#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4907void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4908{
4909 ssl->anti_replay = mode;
4910}
4911#endif
4912
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004913#if defined(POLARSSL_SSL_PROTO_DTLS)
4914void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4915{
4916 ssl->hs_timeout_min = min;
4917 ssl->hs_timeout_max = max;
4918}
4919#endif
4920
Paul Bakker5121ce52009-01-03 21:22:43 +00004921void ssl_set_authmode( ssl_context *ssl, int authmode )
4922{
4923 ssl->authmode = authmode;
4924}
4925
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004926#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004927void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004928 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004929 void *p_vrfy )
4930{
4931 ssl->f_vrfy = f_vrfy;
4932 ssl->p_vrfy = p_vrfy;
4933}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004934#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004935
Paul Bakker5121ce52009-01-03 21:22:43 +00004936void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004937 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004938 void *p_rng )
4939{
4940 ssl->f_rng = f_rng;
4941 ssl->p_rng = p_rng;
4942}
4943
4944void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004945 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004946 void *p_dbg )
4947{
4948 ssl->f_dbg = f_dbg;
4949 ssl->p_dbg = p_dbg;
4950}
4951
4952void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004953 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004954 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004955{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004956 if( p_recv != p_send )
4957 {
4958 ssl->f_recv = NULL;
4959 ssl->f_send = NULL;
4960 ssl->p_bio = NULL;
4961 return;
4962 }
4963
Paul Bakker5121ce52009-01-03 21:22:43 +00004964 ssl->f_recv = f_recv;
4965 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004966 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004967}
4968
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004969void ssl_set_bio_timeout( ssl_context *ssl,
4970 void *p_bio,
4971 int (*f_send)(void *, const unsigned char *, size_t),
4972 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004973 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4974 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004975{
4976 ssl->p_bio = p_bio;
4977 ssl->f_send = f_send;
4978 ssl->f_recv = f_recv;
4979 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004980 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004981}
4982
Paul Bakker0a597072012-09-25 21:55:46 +00004983void ssl_set_session_cache( ssl_context *ssl,
4984 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4985 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004986{
Paul Bakker0a597072012-09-25 21:55:46 +00004987 ssl->f_get_cache = f_get_cache;
4988 ssl->p_get_cache = p_get_cache;
4989 ssl->f_set_cache = f_set_cache;
4990 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004991}
4992
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004993int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004994{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004995 int ret;
4996
4997 if( ssl == NULL ||
4998 session == NULL ||
4999 ssl->session_negotiate == NULL ||
5000 ssl->endpoint != SSL_IS_CLIENT )
5001 {
5002 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5003 }
5004
5005 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5006 return( ret );
5007
Paul Bakker0a597072012-09-25 21:55:46 +00005008 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005009
5010 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005011}
5012
Paul Bakkerb68cad62012-08-23 08:34:18 +00005013void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005014{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005015 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5016 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5017 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5018 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5019}
5020
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005021void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5022 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005023 int major, int minor )
5024{
5025 if( major != SSL_MAJOR_VERSION_3 )
5026 return;
5027
5028 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5029 return;
5030
5031 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005032}
5033
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005034#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005035/* Add a new (empty) key_cert entry an return a pointer to it */
5036static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5037{
5038 ssl_key_cert *key_cert, *last;
5039
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005040 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5041 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005042 return( NULL );
5043
5044 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5045
5046 /* Append the new key_cert to the (possibly empty) current list */
5047 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005048 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005049 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005050 if( ssl->handshake != NULL )
5051 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005052 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005053 else
5054 {
5055 last = ssl->key_cert;
5056 while( last->next != NULL )
5057 last = last->next;
5058 last->next = key_cert;
5059 }
5060
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005061 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005062}
5063
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005064void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005065 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005066{
5067 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005068 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 ssl->peer_cn = peer_cn;
5070}
5071
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005072int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005073 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005074{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005075 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5076
5077 if( key_cert == NULL )
5078 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5079
5080 key_cert->cert = own_cert;
5081 key_cert->key = pk_key;
5082
5083 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005084}
5085
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005086#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005087int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005088 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005089{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005090 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005091 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005092
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005093 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005094 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5095
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005096 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5097 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005099
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005100 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005101
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005102 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005103 if( ret != 0 )
5104 return( ret );
5105
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005106 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005107 return( ret );
5108
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005109 key_cert->cert = own_cert;
5110 key_cert->key_own_alloc = 1;
5111
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005112 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005113}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005114#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005115
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005116int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005117 void *rsa_key,
5118 rsa_decrypt_func rsa_decrypt,
5119 rsa_sign_func rsa_sign,
5120 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005121{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005122 int ret;
5123 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005124
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005125 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005126 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5127
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005128 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5129 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005130 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005131
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005132 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005133
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005134 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5135 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5136 return( ret );
5137
5138 key_cert->cert = own_cert;
5139 key_cert->key_own_alloc = 1;
5140
5141 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005142}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005143#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005144
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005145#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005146int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5147 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005148{
Paul Bakker6db455e2013-09-18 17:29:31 +02005149 if( psk == NULL || psk_identity == NULL )
5150 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5151
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005152 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005153 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5154
Paul Bakker6db455e2013-09-18 17:29:31 +02005155 if( ssl->psk != NULL )
5156 {
5157 polarssl_free( ssl->psk );
5158 polarssl_free( ssl->psk_identity );
5159 }
5160
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005161 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005162 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005163
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005164 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005165 ssl->psk_identity = (unsigned char *)
5166 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005167
5168 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5169 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5170
5171 memcpy( ssl->psk, psk, ssl->psk_len );
5172 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005173
5174 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005175}
5176
5177void ssl_set_psk_cb( ssl_context *ssl,
5178 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5179 size_t),
5180 void *p_psk )
5181{
5182 ssl->f_psk = f_psk;
5183 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005184}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005185#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005186
Paul Bakker48916f92012-09-16 19:57:18 +00005187#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005188int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005189{
5190 int ret;
5191
Paul Bakker48916f92012-09-16 19:57:18 +00005192 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005193 {
5194 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5195 return( ret );
5196 }
5197
Paul Bakker48916f92012-09-16 19:57:18 +00005198 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005199 {
5200 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5201 return( ret );
5202 }
5203
5204 return( 0 );
5205}
5206
Paul Bakker1b57b062011-01-06 15:48:19 +00005207int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5208{
5209 int ret;
5210
Paul Bakker66d5d072014-06-17 16:39:18 +02005211 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005212 {
5213 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5214 return( ret );
5215 }
5216
Paul Bakker66d5d072014-06-17 16:39:18 +02005217 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005218 {
5219 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5220 return( ret );
5221 }
5222
5223 return( 0 );
5224}
Paul Bakker48916f92012-09-16 19:57:18 +00005225#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005226
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005227#if defined(POLARSSL_SSL_SET_CURVES)
5228/*
5229 * Set the allowed elliptic curves
5230 */
5231void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5232{
5233 ssl->curve_list = curve_list;
5234}
5235#endif
5236
Paul Bakker0be444a2013-08-27 21:55:01 +02005237#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005238int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005239{
5240 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005241 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005242
5243 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005244
5245 if( ssl->hostname_len + 1 == 0 )
5246 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5247
Paul Bakker6e339b52013-07-03 13:37:05 +02005248 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005249
Paul Bakkerb15b8512012-01-13 13:44:06 +00005250 if( ssl->hostname == NULL )
5251 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5252
Paul Bakker3c2122f2013-06-24 19:03:14 +02005253 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005254 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005255
Paul Bakker40ea7de2009-05-03 10:18:48 +00005256 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005257
5258 return( 0 );
5259}
5260
Paul Bakker5701cdc2012-09-27 21:49:42 +00005261void ssl_set_sni( ssl_context *ssl,
5262 int (*f_sni)(void *, ssl_context *,
5263 const unsigned char *, size_t),
5264 void *p_sni )
5265{
5266 ssl->f_sni = f_sni;
5267 ssl->p_sni = p_sni;
5268}
Paul Bakker0be444a2013-08-27 21:55:01 +02005269#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005270
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005271#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005272int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005273{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005274 size_t cur_len, tot_len;
5275 const char **p;
5276
5277 /*
5278 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5279 * truncated". Check lengths now rather than later.
5280 */
5281 tot_len = 0;
5282 for( p = protos; *p != NULL; p++ )
5283 {
5284 cur_len = strlen( *p );
5285 tot_len += cur_len;
5286
5287 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5288 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5289 }
5290
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005291 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005292
5293 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005294}
5295
5296const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5297{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005298 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005299}
5300#endif /* POLARSSL_SSL_ALPN */
5301
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005302static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005303{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005304 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005305 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005306 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005307 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005308 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005309
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005310#if defined(POLARSSL_SSL_PROTO_DTLS)
5311 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5312 minor < SSL_MINOR_VERSION_2 )
5313 {
5314 return( -1 );
5315 }
5316#else
5317 ((void) ssl);
5318#endif
5319
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005320 return( 0 );
5321}
5322
5323int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5324{
5325 if( ssl_check_version( ssl, major, minor ) != 0 )
5326 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5327
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005328 ssl->max_major_ver = major;
5329 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005330
5331 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005332}
5333
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005334int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005335{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005336 if( ssl_check_version( ssl, major, minor ) != 0 )
5337 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005338
5339 ssl->min_major_ver = major;
5340 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005341
5342 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005343}
5344
Paul Bakker05decb22013-08-15 13:33:48 +02005345#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005346int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5347{
Paul Bakker77e257e2013-12-16 15:29:52 +01005348 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005349 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005350 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005351 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005352 }
5353
5354 ssl->mfl_code = mfl_code;
5355
5356 return( 0 );
5357}
Paul Bakker05decb22013-08-15 13:33:48 +02005358#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005359
Paul Bakker1f2bc622013-08-15 13:45:55 +02005360#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005361int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005362{
5363 if( ssl->endpoint != SSL_IS_CLIENT )
5364 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5365
Paul Bakker8c1ede62013-07-19 14:14:37 +02005366 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005367
5368 return( 0 );
5369}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005370#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005371
Paul Bakker48916f92012-09-16 19:57:18 +00005372void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5373{
5374 ssl->disable_renegotiation = renegotiation;
5375}
5376
5377void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5378{
5379 ssl->allow_legacy_renegotiation = allow_legacy;
5380}
5381
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005382void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5383{
5384 ssl->renego_max_records = max_records;
5385}
5386
Paul Bakkera503a632013-08-14 13:48:06 +02005387#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005388int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5389{
5390 ssl->session_tickets = use_tickets;
5391
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005392 if( ssl->endpoint == SSL_IS_CLIENT )
5393 return( 0 );
5394
5395 if( ssl->f_rng == NULL )
5396 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5397
5398 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005399}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005400
5401void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5402{
5403 ssl->ticket_lifetime = lifetime;
5404}
Paul Bakkera503a632013-08-14 13:48:06 +02005405#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005406
Paul Bakker5121ce52009-01-03 21:22:43 +00005407/*
5408 * SSL get accessors
5409 */
Paul Bakker23986e52011-04-24 08:57:21 +00005410size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005411{
5412 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5413}
5414
Paul Bakkerff60ee62010-03-16 21:09:09 +00005415int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005416{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005417 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005418}
5419
Paul Bakkere3166ce2011-01-27 17:40:50 +00005420const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005421{
Paul Bakker926c8e42013-03-06 10:23:34 +01005422 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005423 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005424
Paul Bakkere3166ce2011-01-27 17:40:50 +00005425 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005426}
5427
Paul Bakker43ca69c2011-01-15 17:35:19 +00005428const char *ssl_get_version( const ssl_context *ssl )
5429{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005430#if defined(POLARSSL_SSL_PROTO_DTLS)
5431 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5432 {
5433 switch( ssl->minor_ver )
5434 {
5435 case SSL_MINOR_VERSION_2:
5436 return( "DTLSv1.0" );
5437
5438 case SSL_MINOR_VERSION_3:
5439 return( "DTLSv1.2" );
5440
5441 default:
5442 return( "unknown (DTLS)" );
5443 }
5444 }
5445#endif
5446
Paul Bakker43ca69c2011-01-15 17:35:19 +00005447 switch( ssl->minor_ver )
5448 {
5449 case SSL_MINOR_VERSION_0:
5450 return( "SSLv3.0" );
5451
5452 case SSL_MINOR_VERSION_1:
5453 return( "TLSv1.0" );
5454
5455 case SSL_MINOR_VERSION_2:
5456 return( "TLSv1.1" );
5457
Paul Bakker1ef83d62012-04-11 12:09:53 +00005458 case SSL_MINOR_VERSION_3:
5459 return( "TLSv1.2" );
5460
Paul Bakker43ca69c2011-01-15 17:35:19 +00005461 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005462 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005463 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005464}
5465
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005466#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005467const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005468{
5469 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005470 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005471
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005472 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005473}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005474#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005475
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005476int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5477{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005478 if( ssl == NULL ||
5479 dst == NULL ||
5480 ssl->session == NULL ||
5481 ssl->endpoint != SSL_IS_CLIENT )
5482 {
5483 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5484 }
5485
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005486 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005487}
5488
Paul Bakker5121ce52009-01-03 21:22:43 +00005489/*
Paul Bakker1961b702013-01-25 14:49:24 +01005490 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 */
Paul Bakker1961b702013-01-25 14:49:24 +01005492int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005493{
Paul Bakker40e46942009-01-03 21:51:57 +00005494 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005495
Paul Bakker40e46942009-01-03 21:51:57 +00005496#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005497 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005498 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005499#endif
5500
Paul Bakker40e46942009-01-03 21:51:57 +00005501#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005502 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005503 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005504#endif
5505
Paul Bakker1961b702013-01-25 14:49:24 +01005506 return( ret );
5507}
5508
5509/*
5510 * Perform the SSL handshake
5511 */
5512int ssl_handshake( ssl_context *ssl )
5513{
5514 int ret = 0;
5515
5516 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5517
5518 while( ssl->state != SSL_HANDSHAKE_OVER )
5519 {
5520 ret = ssl_handshake_step( ssl );
5521
5522 if( ret != 0 )
5523 break;
5524 }
5525
Paul Bakker5121ce52009-01-03 21:22:43 +00005526 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5527
5528 return( ret );
5529}
5530
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005531#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005532/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005533 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005534 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005535static int ssl_write_hello_request( ssl_context *ssl )
5536{
5537 int ret;
5538
5539 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5540
5541 ssl->out_msglen = 4;
5542 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5543 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5544
5545 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5546 {
5547 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5548 return( ret );
5549 }
5550
5551 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5552
5553 return( 0 );
5554}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005555#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005556
5557/*
5558 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005559 * - any side: calling ssl_renegotiate(),
5560 * - client: receiving a HelloRequest during ssl_read(),
5561 * - server: receiving any handshake message on server during ssl_read() after
5562 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005563 * If the handshake doesn't complete due to waiting for I/O, it will continue
5564 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005565 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005566static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005567{
5568 int ret;
5569
5570 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5571
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005572 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5573 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005574
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005575 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5576 * the ServerHello will have message_seq = 1" */
5577#if defined(POLARSSL_SSL_PROTO_DTLS)
5578 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005579 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5580 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005581 if( ssl->endpoint == SSL_IS_SERVER )
5582 ssl->handshake->out_msg_seq = 1;
5583 else
5584 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005585 }
5586#endif
5587
Paul Bakker48916f92012-09-16 19:57:18 +00005588 ssl->state = SSL_HELLO_REQUEST;
5589 ssl->renegotiation = SSL_RENEGOTIATION;
5590
Paul Bakker48916f92012-09-16 19:57:18 +00005591 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5592 {
5593 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5594 return( ret );
5595 }
5596
5597 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5598
5599 return( 0 );
5600}
5601
5602/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005603 * Renegotiate current connection on client,
5604 * or request renegotiation on server
5605 */
5606int ssl_renegotiate( ssl_context *ssl )
5607{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005608 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005609
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005610#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005611 /* On server, just send the request */
5612 if( ssl->endpoint == SSL_IS_SERVER )
5613 {
5614 if( ssl->state != SSL_HANDSHAKE_OVER )
5615 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5616
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005617 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5618
5619 /* Did we already try/start sending HelloRequest? */
5620 if( ssl->out_left != 0 )
5621 return( ssl_flush_output( ssl ) );
5622
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005623 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005624 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005625#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005626
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005627#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005628 /*
5629 * On client, either start the renegotiation process or,
5630 * if already in progress, continue the handshake
5631 */
5632 if( ssl->renegotiation != SSL_RENEGOTIATION )
5633 {
5634 if( ssl->state != SSL_HANDSHAKE_OVER )
5635 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5636
5637 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5638 {
5639 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5640 return( ret );
5641 }
5642 }
5643 else
5644 {
5645 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5646 {
5647 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5648 return( ret );
5649 }
5650 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005651#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005652
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005653 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005654}
5655
5656/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005657 * Receive application data decrypted from the SSL layer
5658 */
Paul Bakker23986e52011-04-24 08:57:21 +00005659int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005660{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005661 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005662 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005663
5664 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5665
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005666#if defined(POLARSSL_SSL_PROTO_DTLS)
5667 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5668 ssl->handshake != NULL &&
5669 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5670 {
5671 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5672 return( ret );
5673
5674 if( ( ret = ssl_resend( ssl ) ) != 0 )
5675 return( ret );
5676 }
5677#endif
5678
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 if( ssl->state != SSL_HANDSHAKE_OVER )
5680 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005681 ret = ssl_handshake( ssl );
5682 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5683 {
5684 record_read = 1;
5685 }
5686 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005687 {
5688 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5689 return( ret );
5690 }
5691 }
5692
5693 if( ssl->in_offt == NULL )
5694 {
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005695#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005696 /* Start timer if not already running */
5697 if( ssl->time_limit == 0 )
5698 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005699#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005700
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005701 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005702 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005703 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5704 {
5705 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5706 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005707
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005708 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5709 return( ret );
5710 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005711 }
5712
5713 if( ssl->in_msglen == 0 &&
5714 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5715 {
5716 /*
5717 * OpenSSL sends empty messages to randomize the IV
5718 */
5719 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5720 {
Paul Bakker831a7552011-05-18 13:32:51 +00005721 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5722 return( 0 );
5723
Paul Bakker5121ce52009-01-03 21:22:43 +00005724 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5725 return( ret );
5726 }
5727 }
5728
Paul Bakker48916f92012-09-16 19:57:18 +00005729 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5730 {
5731 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5732
5733 if( ssl->endpoint == SSL_IS_CLIENT &&
5734 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005735 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005736 {
5737 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005738
5739 /* With DTLS, drop the packet (probably from last handshake) */
5740#if defined(POLARSSL_SSL_PROTO_DTLS)
5741 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5742 return( POLARSSL_ERR_NET_WANT_READ );
5743#endif
5744 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5745 }
5746
5747 if( ssl->endpoint == SSL_IS_SERVER &&
5748 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5749 {
5750 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5751
5752 /* With DTLS, drop the packet (probably from last handshake) */
5753#if defined(POLARSSL_SSL_PROTO_DTLS)
5754 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5755 return( POLARSSL_ERR_NET_WANT_READ );
5756#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005757 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5758 }
5759
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005760 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5761 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005762 ssl->allow_legacy_renegotiation ==
5763 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005764 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005765 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005766
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005767#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005768 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005769 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005770 /*
5771 * SSLv3 does not have a "no_renegotiation" alert
5772 */
5773 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5774 return( ret );
5775 }
5776 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005777#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005778#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5779 defined(POLARSSL_SSL_PROTO_TLS1_2)
5780 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005781 {
5782 if( ( ret = ssl_send_alert_message( ssl,
5783 SSL_ALERT_LEVEL_WARNING,
5784 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5785 {
5786 return( ret );
5787 }
Paul Bakker48916f92012-09-16 19:57:18 +00005788 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005789 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005790#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5791 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005792 {
5793 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005794 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005795 }
Paul Bakker48916f92012-09-16 19:57:18 +00005796 }
5797 else
5798 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005799 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005800#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005801 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5802 ssl->endpoint == SSL_IS_CLIENT )
5803 {
5804 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5805 }
5806#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005807 ret = ssl_start_renegotiation( ssl );
5808 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5809 {
5810 record_read = 1;
5811 }
5812 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005813 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005814 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005815 return( ret );
5816 }
Paul Bakker48916f92012-09-16 19:57:18 +00005817 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005818
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005819 /* If a non-handshake record was read during renego, fallthrough,
5820 * else tell the user they should call ssl_read() again */
5821 if( ! record_read )
5822 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005823 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005824 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5825 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005826 ssl->renego_records_seen++;
5827
5828 if( ssl->renego_max_records >= 0 &&
5829 ssl->renego_records_seen > ssl->renego_max_records )
5830 {
5831 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5832 "but not honored by client" ) );
5833 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5834 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005835 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005836
5837 /* Fatal and closure alerts handled by ssl_read_record() */
5838 if( ssl->in_msgtype == SSL_MSG_ALERT )
5839 {
5840 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5841 return( POLARSSL_ERR_NET_WANT_READ );
5842 }
5843
5844 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 {
5846 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005847 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005848 }
5849
5850 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005851
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005852#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005853 /* We're going to return something now, cancel timer,
5854 * except if handshake (renegotiation) is in progress */
5855 if( ssl->state == SSL_HANDSHAKE_OVER )
5856 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005857#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 }
5859
5860 n = ( len < ssl->in_msglen )
5861 ? len : ssl->in_msglen;
5862
5863 memcpy( buf, ssl->in_offt, n );
5864 ssl->in_msglen -= n;
5865
5866 if( ssl->in_msglen == 0 )
5867 /* all bytes consumed */
5868 ssl->in_offt = NULL;
5869 else
5870 /* more data available */
5871 ssl->in_offt += n;
5872
5873 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5874
Paul Bakker23986e52011-04-24 08:57:21 +00005875 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005876}
5877
5878/*
5879 * Send application data to be encrypted by the SSL layer
5880 */
Paul Bakker23986e52011-04-24 08:57:21 +00005881int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005882{
Paul Bakker23986e52011-04-24 08:57:21 +00005883 int ret;
5884 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005885 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005886
5887 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5888
5889 if( ssl->state != SSL_HANDSHAKE_OVER )
5890 {
5891 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5892 {
5893 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5894 return( ret );
5895 }
5896 }
5897
Paul Bakker05decb22013-08-15 13:33:48 +02005898#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005899 /*
5900 * Assume mfl_code is correct since it was checked when set
5901 */
5902 max_len = mfl_code_to_length[ssl->mfl_code];
5903
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005904 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005905 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005906 */
5907 if( ssl->session_out != NULL &&
5908 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5909 {
5910 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5911 }
Paul Bakker05decb22013-08-15 13:33:48 +02005912#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005913
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005914 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005915
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 if( ssl->out_left != 0 )
5917 {
5918 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5919 {
5920 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5921 return( ret );
5922 }
5923 }
Paul Bakker887bd502011-06-08 13:10:54 +00005924 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005925 {
Paul Bakker887bd502011-06-08 13:10:54 +00005926 ssl->out_msglen = n;
5927 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5928 memcpy( ssl->out_msg, buf, n );
5929
5930 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5931 {
5932 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5933 return( ret );
5934 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005935 }
5936
5937 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5938
Paul Bakker23986e52011-04-24 08:57:21 +00005939 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005940}
5941
5942/*
5943 * Notify the peer that the connection is being closed
5944 */
5945int ssl_close_notify( ssl_context *ssl )
5946{
5947 int ret;
5948
5949 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5950
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005951 if( ssl->out_left != 0 )
5952 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005953
5954 if( ssl->state == SSL_HANDSHAKE_OVER )
5955 {
Paul Bakker48916f92012-09-16 19:57:18 +00005956 if( ( ret = ssl_send_alert_message( ssl,
5957 SSL_ALERT_LEVEL_WARNING,
5958 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005959 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005960 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005961 return( ret );
5962 }
5963 }
5964
5965 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5966
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005967 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005968}
5969
Paul Bakker48916f92012-09-16 19:57:18 +00005970void ssl_transform_free( ssl_transform *transform )
5971{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005972 if( transform == NULL )
5973 return;
5974
Paul Bakker48916f92012-09-16 19:57:18 +00005975#if defined(POLARSSL_ZLIB_SUPPORT)
5976 deflateEnd( &transform->ctx_deflate );
5977 inflateEnd( &transform->ctx_inflate );
5978#endif
5979
Paul Bakker84bbeb52014-07-01 14:53:22 +02005980 cipher_free( &transform->cipher_ctx_enc );
5981 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005982
Paul Bakker84bbeb52014-07-01 14:53:22 +02005983 md_free( &transform->md_ctx_enc );
5984 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005985
Paul Bakker34617722014-06-13 17:20:13 +02005986 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005987}
5988
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005989#if defined(POLARSSL_X509_CRT_PARSE_C)
5990static void ssl_key_cert_free( ssl_key_cert *key_cert )
5991{
5992 ssl_key_cert *cur = key_cert, *next;
5993
5994 while( cur != NULL )
5995 {
5996 next = cur->next;
5997
5998 if( cur->key_own_alloc )
5999 {
6000 pk_free( cur->key );
6001 polarssl_free( cur->key );
6002 }
6003 polarssl_free( cur );
6004
6005 cur = next;
6006 }
6007}
6008#endif /* POLARSSL_X509_CRT_PARSE_C */
6009
Paul Bakker48916f92012-09-16 19:57:18 +00006010void ssl_handshake_free( ssl_handshake_params *handshake )
6011{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006012 if( handshake == NULL )
6013 return;
6014
Paul Bakker48916f92012-09-16 19:57:18 +00006015#if defined(POLARSSL_DHM_C)
6016 dhm_free( &handshake->dhm_ctx );
6017#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006018#if defined(POLARSSL_ECDH_C)
6019 ecdh_free( &handshake->ecdh_ctx );
6020#endif
6021
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006022#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006023 /* explicit void pointer cast for buggy MS compiler */
6024 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006025#endif
6026
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006027#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6028 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6029 /*
6030 * Free only the linked list wrapper, not the keys themselves
6031 * since the belong to the SNI callback
6032 */
6033 if( handshake->sni_key_cert != NULL )
6034 {
6035 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6036
6037 while( cur != NULL )
6038 {
6039 next = cur->next;
6040 polarssl_free( cur );
6041 cur = next;
6042 }
6043 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006044#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006045
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006046#if defined(POLARSSL_SSL_PROTO_DTLS)
6047 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006048 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006049 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006050#endif
6051
Paul Bakker34617722014-06-13 17:20:13 +02006052 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006053}
6054
6055void ssl_session_free( ssl_session *session )
6056{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006057 if( session == NULL )
6058 return;
6059
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006060#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006061 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006062 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006063 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006064 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006065 }
Paul Bakkered27a042013-04-18 22:46:23 +02006066#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006067
Paul Bakkera503a632013-08-14 13:48:06 +02006068#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006069 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006070#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006071
Paul Bakker34617722014-06-13 17:20:13 +02006072 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006073}
6074
Paul Bakker5121ce52009-01-03 21:22:43 +00006075/*
6076 * Free an SSL context
6077 */
6078void ssl_free( ssl_context *ssl )
6079{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006080 if( ssl == NULL )
6081 return;
6082
Paul Bakker5121ce52009-01-03 21:22:43 +00006083 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6084
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006085 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006086 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006087 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6088 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006089 }
6090
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006091 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006092 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006093 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6094 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 }
6096
Paul Bakker16770332013-10-11 09:59:44 +02006097#if defined(POLARSSL_ZLIB_SUPPORT)
6098 if( ssl->compress_buf != NULL )
6099 {
Paul Bakker34617722014-06-13 17:20:13 +02006100 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006101 polarssl_free( ssl->compress_buf );
6102 }
6103#endif
6104
Paul Bakker40e46942009-01-03 21:51:57 +00006105#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006106 mpi_free( &ssl->dhm_P );
6107 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006108#endif
6109
Paul Bakker48916f92012-09-16 19:57:18 +00006110 if( ssl->transform )
6111 {
6112 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006113 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006114 }
6115
6116 if( ssl->handshake )
6117 {
6118 ssl_handshake_free( ssl->handshake );
6119 ssl_transform_free( ssl->transform_negotiate );
6120 ssl_session_free( ssl->session_negotiate );
6121
Paul Bakker6e339b52013-07-03 13:37:05 +02006122 polarssl_free( ssl->handshake );
6123 polarssl_free( ssl->transform_negotiate );
6124 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006125 }
6126
Paul Bakkerc0463502013-02-14 11:19:38 +01006127 if( ssl->session )
6128 {
6129 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006130 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006131 }
6132
Paul Bakkera503a632013-08-14 13:48:06 +02006133#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006134 if( ssl->ticket_keys )
6135 {
6136 ssl_ticket_keys_free( ssl->ticket_keys );
6137 polarssl_free( ssl->ticket_keys );
6138 }
Paul Bakkera503a632013-08-14 13:48:06 +02006139#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006140
Paul Bakker0be444a2013-08-27 21:55:01 +02006141#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006142 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 {
Paul Bakker34617722014-06-13 17:20:13 +02006144 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006145 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006146 ssl->hostname_len = 0;
6147 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006148#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006149
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006150#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006151 if( ssl->psk != NULL )
6152 {
Paul Bakker34617722014-06-13 17:20:13 +02006153 polarssl_zeroize( ssl->psk, ssl->psk_len );
6154 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006155 polarssl_free( ssl->psk );
6156 polarssl_free( ssl->psk_identity );
6157 ssl->psk_len = 0;
6158 ssl->psk_identity_len = 0;
6159 }
6160#endif
6161
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006162#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006163 ssl_key_cert_free( ssl->key_cert );
6164#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006165
Paul Bakker05ef8352012-05-08 09:17:57 +00006166#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6167 if( ssl_hw_record_finish != NULL )
6168 {
6169 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6170 ssl_hw_record_finish( ssl );
6171 }
6172#endif
6173
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006174#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006175 polarssl_free( ssl->cli_id );
6176#endif
6177
Paul Bakker5121ce52009-01-03 21:22:43 +00006178 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006179
Paul Bakker86f04f42013-02-14 11:20:09 +01006180 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006181 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006182}
6183
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006184#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006185/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006186 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006187 */
6188unsigned char ssl_sig_from_pk( pk_context *pk )
6189{
6190#if defined(POLARSSL_RSA_C)
6191 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6192 return( SSL_SIG_RSA );
6193#endif
6194#if defined(POLARSSL_ECDSA_C)
6195 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6196 return( SSL_SIG_ECDSA );
6197#endif
6198 return( SSL_SIG_ANON );
6199}
6200
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006201pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6202{
6203 switch( sig )
6204 {
6205#if defined(POLARSSL_RSA_C)
6206 case SSL_SIG_RSA:
6207 return( POLARSSL_PK_RSA );
6208#endif
6209#if defined(POLARSSL_ECDSA_C)
6210 case SSL_SIG_ECDSA:
6211 return( POLARSSL_PK_ECDSA );
6212#endif
6213 default:
6214 return( POLARSSL_PK_NONE );
6215 }
6216}
Paul Bakker9af723c2014-05-01 13:03:14 +02006217#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006218
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006219/*
6220 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6221 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006222md_type_t ssl_md_alg_from_hash( unsigned char hash )
6223{
6224 switch( hash )
6225 {
6226#if defined(POLARSSL_MD5_C)
6227 case SSL_HASH_MD5:
6228 return( POLARSSL_MD_MD5 );
6229#endif
6230#if defined(POLARSSL_SHA1_C)
6231 case SSL_HASH_SHA1:
6232 return( POLARSSL_MD_SHA1 );
6233#endif
6234#if defined(POLARSSL_SHA256_C)
6235 case SSL_HASH_SHA224:
6236 return( POLARSSL_MD_SHA224 );
6237 case SSL_HASH_SHA256:
6238 return( POLARSSL_MD_SHA256 );
6239#endif
6240#if defined(POLARSSL_SHA512_C)
6241 case SSL_HASH_SHA384:
6242 return( POLARSSL_MD_SHA384 );
6243 case SSL_HASH_SHA512:
6244 return( POLARSSL_MD_SHA512 );
6245#endif
6246 default:
6247 return( POLARSSL_MD_NONE );
6248 }
6249}
6250
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006251#if defined(POLARSSL_SSL_SET_CURVES)
6252/*
6253 * Check is a curve proposed by the peer is in our list.
6254 * Return 1 if we're willing to use it, 0 otherwise.
6255 */
6256int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6257{
6258 const ecp_group_id *gid;
6259
6260 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6261 if( *gid == grp_id )
6262 return( 1 );
6263
6264 return( 0 );
6265}
Paul Bakker9af723c2014-05-01 13:03:14 +02006266#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006267
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006268#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006269int ssl_check_cert_usage( const x509_crt *cert,
6270 const ssl_ciphersuite_t *ciphersuite,
6271 int cert_endpoint )
6272{
6273#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6274 int usage = 0;
6275#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006276#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6277 const char *ext_oid;
6278 size_t ext_len;
6279#endif
6280
6281#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6282 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6283 ((void) cert);
6284 ((void) cert_endpoint);
6285#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006286
6287#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6288 if( cert_endpoint == SSL_IS_SERVER )
6289 {
6290 /* Server part of the key exchange */
6291 switch( ciphersuite->key_exchange )
6292 {
6293 case POLARSSL_KEY_EXCHANGE_RSA:
6294 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6295 usage = KU_KEY_ENCIPHERMENT;
6296 break;
6297
6298 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6299 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6300 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6301 usage = KU_DIGITAL_SIGNATURE;
6302 break;
6303
6304 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6305 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6306 usage = KU_KEY_AGREEMENT;
6307 break;
6308
6309 /* Don't use default: we want warnings when adding new values */
6310 case POLARSSL_KEY_EXCHANGE_NONE:
6311 case POLARSSL_KEY_EXCHANGE_PSK:
6312 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6313 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6314 usage = 0;
6315 }
6316 }
6317 else
6318 {
6319 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6320 usage = KU_DIGITAL_SIGNATURE;
6321 }
6322
6323 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6324 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006325#else
6326 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006327#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6328
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006329#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6330 if( cert_endpoint == SSL_IS_SERVER )
6331 {
6332 ext_oid = OID_SERVER_AUTH;
6333 ext_len = OID_SIZE( OID_SERVER_AUTH );
6334 }
6335 else
6336 {
6337 ext_oid = OID_CLIENT_AUTH;
6338 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6339 }
6340
6341 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6342 return( -1 );
6343#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6344
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006345 return( 0 );
6346}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006347#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006348
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006349/*
6350 * Convert version numbers to/from wire format
6351 * and, for DTLS, to/from TLS equivalent.
6352 *
6353 * For TLS this is the identity.
6354 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6355 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6356 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6357 */
6358void ssl_write_version( int major, int minor, int transport,
6359 unsigned char ver[2] )
6360{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006361#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006362 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006363 {
6364 if( minor == SSL_MINOR_VERSION_2 )
6365 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6366
6367 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6368 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6369 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006370 else
6371#else
6372 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006373#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006374 {
6375 ver[0] = (unsigned char) major;
6376 ver[1] = (unsigned char) minor;
6377 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006378}
6379
6380void ssl_read_version( int *major, int *minor, int transport,
6381 const unsigned char ver[2] )
6382{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006383#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006384 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006385 {
6386 *major = 255 - ver[0] + 2;
6387 *minor = 255 - ver[1] + 1;
6388
6389 if( *minor == SSL_MINOR_VERSION_1 )
6390 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6391 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006392 else
6393#else
6394 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006395#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006396 {
6397 *major = ver[0];
6398 *minor = ver[1];
6399 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006400}
6401
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006402#endif /* POLARSSL_SSL_TLS_C */