blob: ea8696c041cb04e7f5b9826d418c82869d140920 [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
2964 /* Drop unexpected ApplicationData records */
2965 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
2966 ssl->state != SSL_HANDSHAKE_OVER )
2967 {
2968 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2969 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2970 }
2971 }
2972#endif
2973
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002974 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002975 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 {
2977 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002978 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 }
2980
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002981 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002982 {
2983 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002984 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002985 }
2986
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002987 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002988#if defined(POLARSSL_SSL_PROTO_DTLS)
2989 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2990 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002991 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002992
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002993 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002994 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002995 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002996 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002997 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002998 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002999 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003000
3001#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3002 if( ssl_dtls_replay_check( ssl ) != 0 )
3003 {
3004 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3005 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3006 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003007#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003008 }
3009#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003010
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003011 /* Check length against the size of our buffer */
3012 if( ssl->in_msglen > SSL_BUFFER_LEN
3013 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003014 {
3015 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3016 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3017 }
3018
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003019 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003020 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003022 if( ssl->in_msglen < 1 ||
3023 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 {
3025 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003026 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 }
3028 }
3029 else
3030 {
Paul Bakker48916f92012-09-16 19:57:18 +00003031 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
3033 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003034 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 }
3036
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003037#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003039 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 {
3041 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003042 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003044#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003045#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3046 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 /*
3048 * TLS encrypted messages can have up to 256 bytes of padding
3049 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003050 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003051 ssl->in_msglen > ssl->transform_in->minlen +
3052 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 {
3054 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003055 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 }
3059
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003060 return( 0 );
3061}
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003063/*
3064 * If applicable, decrypt (and decompress) record content
3065 */
3066static int ssl_prepare_record_content( ssl_context *ssl )
3067{
3068 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003069
Paul Bakker5121ce52009-01-03 21:22:43 +00003070 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003071 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakker05ef8352012-05-08 09:17:57 +00003073#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003074 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003075 {
3076 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3077
3078 ret = ssl_hw_record_read( ssl );
3079 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3080 {
3081 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003082 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003083 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003084
3085 if( ret == 0 )
3086 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003087 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003088#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003089 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 {
3091 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3092 {
3093 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3094 return( ret );
3095 }
3096
3097 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3098 ssl->in_msg, ssl->in_msglen );
3099
3100 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3101 {
3102 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003103 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003104 }
3105 }
3106
Paul Bakker2770fbd2012-07-03 13:30:23 +00003107#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003108 if( ssl->transform_in != NULL &&
3109 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003110 {
3111 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3112 {
3113 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3114 return( ret );
3115 }
3116
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003117 // TODO: what's the purpose of these lines? is in_len used?
3118 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3119 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003120 }
3121#endif /* POLARSSL_ZLIB_SUPPORT */
3122
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003123#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003124 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3125 {
3126 ssl_dtls_replay_update( ssl );
3127 }
3128#endif
3129
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003130 return( 0 );
3131}
3132
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003133static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3134
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003135/*
3136 * Read a record.
3137 *
3138 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3139 * and continue reading until a valid record is found.
3140 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003141int ssl_read_record( ssl_context *ssl )
3142{
3143 int ret;
3144
3145 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3146
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003147 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003148 {
3149 /*
3150 * Get next Handshake message in the current record
3151 */
3152 ssl->in_msglen -= ssl->in_hslen;
3153
3154 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3155 ssl->in_msglen );
3156
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003157 SSL_DEBUG_BUF( 4, "remaining content in record",
3158 ssl->in_msg, ssl->in_msglen );
3159
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003160 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3161 return( ret );
3162
3163 return( 0 );
3164 }
3165
3166 ssl->in_hslen = 0;
3167
3168 /*
3169 * Read the record header and parse it
3170 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003171#if defined(POLARSSL_SSL_PROTO_DTLS)
3172read_record_header:
3173#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003174 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3175 {
3176 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3177 return( ret );
3178 }
3179
3180 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003181 {
3182#if defined(POLARSSL_SSL_PROTO_DTLS)
3183 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3184 {
3185 /* Ignore bad record and get next one; drop the whole datagram
3186 * since current header cannot be trusted to find the next record
3187 * in current datagram */
3188 ssl->next_record_offset = 0;
3189 ssl->in_left = 0;
3190
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003191 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003192 goto read_record_header;
3193 }
3194#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003195 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003196 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003197
3198 /*
3199 * Read and optionally decrypt the message contents
3200 */
3201 if( ( ret = ssl_fetch_input( ssl,
3202 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3203 {
3204 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3205 return( ret );
3206 }
3207
3208 /* Done reading this record, get ready for the next one */
3209#if defined(POLARSSL_SSL_PROTO_DTLS)
3210 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3211 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3212 else
3213#endif
3214 ssl->in_left = 0;
3215
3216 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003217 {
3218#if defined(POLARSSL_SSL_PROTO_DTLS)
3219 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3220 {
3221 /* Silently discard invalid records */
3222 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3223 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3224 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003225 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003226 goto read_record_header;
3227 }
3228
3229 return( ret );
3230 }
3231 else
3232#endif
3233 {
3234 /* Error out (and send alert) on invalid records */
3235#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3236 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3237 {
3238 ssl_send_alert_message( ssl,
3239 SSL_ALERT_LEVEL_FATAL,
3240 SSL_ALERT_MSG_BAD_RECORD_MAC );
3241 }
3242#endif
3243 return( ret );
3244 }
3245 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003246
3247 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003248 * When we sent the last flight of the handshake, we MUST respond to a
3249 * retransmit of the peer's previous flight with a retransmit. (In
3250 * practice, only the Finished message will make it, other messages
3251 * including CCS use the old transform so they're dropped as invalid.)
3252 *
3253 * If the record we received is not a handshake message, however, it
3254 * means the peer received our last flight so we can clean up
3255 * handshake info.
3256 *
3257 * This check needs to be done before prepare_handshake() due to an edge
3258 * case: if the client immediately requests renegotiation, this
3259 * finishes the current handshake first, avoiding the new ClientHello
3260 * being mistaken for an ancient message in the current handshake.
3261 */
3262#if defined(POLARSSL_SSL_PROTO_DTLS)
3263 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3264 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003265 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003266 {
3267 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3268 ssl->in_msg[0] == SSL_HS_FINISHED )
3269 {
3270 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3271
3272 if( ( ret = ssl_resend( ssl ) ) != 0 )
3273 {
3274 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3275 return( ret );
3276 }
3277
3278 return( POLARSSL_ERR_NET_WANT_READ );
3279 }
3280 else
3281 {
3282 ssl_handshake_wrapup_free_hs_transform( ssl );
3283 }
3284 }
3285#endif
3286
3287 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003288 * Handle particular types of records
3289 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3291 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003292 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3293 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 }
3295
3296 if( ssl->in_msgtype == SSL_MSG_ALERT )
3297 {
3298 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3299 ssl->in_msg[0], ssl->in_msg[1] ) );
3300
3301 /*
3302 * Ignore non-fatal alerts, except close_notify
3303 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003304 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003306 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3307 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003308 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 }
3310
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003311 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3312 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 {
3314 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003315 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 }
3317 }
3318
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3320
3321 return( 0 );
3322}
3323
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003324int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3325{
3326 int ret;
3327
3328 if( ( ret = ssl_send_alert_message( ssl,
3329 SSL_ALERT_LEVEL_FATAL,
3330 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3331 {
3332 return( ret );
3333 }
3334
3335 return( 0 );
3336}
3337
Paul Bakker0a925182012-04-16 06:46:41 +00003338int ssl_send_alert_message( ssl_context *ssl,
3339 unsigned char level,
3340 unsigned char message )
3341{
3342 int ret;
3343
3344 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3345
3346 ssl->out_msgtype = SSL_MSG_ALERT;
3347 ssl->out_msglen = 2;
3348 ssl->out_msg[0] = level;
3349 ssl->out_msg[1] = message;
3350
3351 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3352 {
3353 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3354 return( ret );
3355 }
3356
3357 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3358
3359 return( 0 );
3360}
3361
Paul Bakker5121ce52009-01-03 21:22:43 +00003362/*
3363 * Handshake functions
3364 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003365#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3366 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3367 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3368 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3369 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3370 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3371 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003372int ssl_write_certificate( ssl_context *ssl )
3373{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003374 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
3376 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3377
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003378 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003379 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3380 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003381 {
3382 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3383 ssl->state++;
3384 return( 0 );
3385 }
3386
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003387 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3388 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003389}
3390
3391int ssl_parse_certificate( ssl_context *ssl )
3392{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003393 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3394
3395 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3396
3397 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003398 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003400 {
3401 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3402 ssl->state++;
3403 return( 0 );
3404 }
3405
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003406 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3407 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003408}
3409#else
3410int ssl_write_certificate( ssl_context *ssl )
3411{
3412 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3413 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003414 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003415 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3416
3417 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3418
3419 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003420 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3421 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003422 {
3423 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3424 ssl->state++;
3425 return( 0 );
3426 }
3427
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 if( ssl->endpoint == SSL_IS_CLIENT )
3429 {
3430 if( ssl->client_auth == 0 )
3431 {
3432 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3433 ssl->state++;
3434 return( 0 );
3435 }
3436
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003437#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 /*
3439 * If using SSLv3 and got no cert, send an Alert message
3440 * (otherwise an empty Certificate message will be sent).
3441 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003442 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3444 {
3445 ssl->out_msglen = 2;
3446 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003447 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3448 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003449
3450 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3451 goto write_msg;
3452 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003453#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 }
3455 else /* SSL_IS_SERVER */
3456 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003457 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 {
3459 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003460 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 }
3462 }
3463
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003464 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
3466 /*
3467 * 0 . 0 handshake type
3468 * 1 . 3 handshake length
3469 * 4 . 6 length of all certs
3470 * 7 . 9 length of cert. 1
3471 * 10 . n-1 peer certificate
3472 * n . n+2 length of cert. 2
3473 * n+3 . ... upper level cert, etc.
3474 */
3475 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003476 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
Paul Bakker29087132010-03-21 21:03:34 +00003478 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003479 {
3480 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003481 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003482 {
3483 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3484 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003485 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 }
3487
3488 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3489 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3490 ssl->out_msg[i + 2] = (unsigned char)( n );
3491
3492 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3493 i += n; crt = crt->next;
3494 }
3495
3496 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3497 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3498 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3499
3500 ssl->out_msglen = i;
3501 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3502 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3503
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003504#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003505write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003507
3508 ssl->state++;
3509
3510 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3511 {
3512 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3513 return( ret );
3514 }
3515
3516 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3517
Paul Bakkered27a042013-04-18 22:46:23 +02003518 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003519}
3520
3521int ssl_parse_certificate( ssl_context *ssl )
3522{
Paul Bakkered27a042013-04-18 22:46:23 +02003523 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003524 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003525 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003526
3527 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3528
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003529 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003530 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003532 {
3533 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3534 ssl->state++;
3535 return( 0 );
3536 }
3537
Paul Bakker5121ce52009-01-03 21:22:43 +00003538 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003539 ( ssl->authmode == SSL_VERIFY_NONE ||
3540 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003542 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3544 ssl->state++;
3545 return( 0 );
3546 }
3547
3548 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3549 {
3550 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3551 return( ret );
3552 }
3553
3554 ssl->state++;
3555
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003556#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 /*
3558 * Check if the client sent an empty certificate
3559 */
3560 if( ssl->endpoint == SSL_IS_SERVER &&
3561 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3562 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003563 if( ssl->in_msglen == 2 &&
3564 ssl->in_msgtype == SSL_MSG_ALERT &&
3565 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3566 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 {
3568 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3569
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003570 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3572 return( 0 );
3573 else
Paul Bakker40e46942009-01-03 21:51:57 +00003574 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 }
3576 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003577#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003579#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3580 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 if( ssl->endpoint == SSL_IS_SERVER &&
3582 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3583 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003584 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3586 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003587 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 {
3589 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3590
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003591 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003593 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 else
3595 return( 0 );
3596 }
3597 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003598#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3599 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003600
3601 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3602 {
3603 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003604 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 }
3606
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003607 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3608 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003609 {
3610 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003611 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 }
3613
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003614 i = ssl_hs_hdr_len( ssl );
3615
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 /*
3617 * Same message structure as in ssl_write_certificate()
3618 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003619 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003621 if( ssl->in_msg[i] != 0 ||
3622 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003623 {
3624 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003625 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003626 }
3627
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003628 /* In case we tried to reuse a session but it failed */
3629 if( ssl->session_negotiate->peer_cert != NULL )
3630 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003631 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003632 polarssl_free( ssl->session_negotiate->peer_cert );
3633 }
3634
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003635 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3636 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 {
3638 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003639 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003640 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 }
3642
Paul Bakkerb6b09562013-09-18 14:17:41 +02003643 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003644
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003645 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003646
3647 while( i < ssl->in_hslen )
3648 {
3649 if( ssl->in_msg[i] != 0 )
3650 {
3651 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003652 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 }
3654
3655 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3656 | (unsigned int) ssl->in_msg[i + 2];
3657 i += 3;
3658
3659 if( n < 128 || i + n > ssl->in_hslen )
3660 {
3661 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003662 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003663 }
3664
Paul Bakkerddf26b42013-09-18 13:46:23 +02003665 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3666 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 if( ret != 0 )
3668 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003669 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 return( ret );
3671 }
3672
3673 i += n;
3674 }
3675
Paul Bakker48916f92012-09-16 19:57:18 +00003676 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003677
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003678 /*
3679 * On client, make sure the server cert doesn't change during renego to
3680 * avoid "triple handshake" attack: https://secure-resumption.com/
3681 */
3682 if( ssl->endpoint == SSL_IS_CLIENT &&
3683 ssl->renegotiation == SSL_RENEGOTIATION )
3684 {
3685 if( ssl->session->peer_cert == NULL )
3686 {
3687 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3688 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3689 }
3690
3691 if( ssl->session->peer_cert->raw.len !=
3692 ssl->session_negotiate->peer_cert->raw.len ||
3693 memcmp( ssl->session->peer_cert->raw.p,
3694 ssl->session_negotiate->peer_cert->raw.p,
3695 ssl->session->peer_cert->raw.len ) != 0 )
3696 {
3697 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3698 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3699 }
3700 }
3701
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 if( ssl->authmode != SSL_VERIFY_NONE )
3703 {
3704 if( ssl->ca_chain == NULL )
3705 {
3706 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003707 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 }
3709
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003710 /*
3711 * Main check: verify certificate
3712 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003713 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3714 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3715 &ssl->session_negotiate->verify_result,
3716 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
3718 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003719 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003721 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003722
3723 /*
3724 * Secondary checks: always done, but change 'ret' only if it was 0
3725 */
3726
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003727#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003728 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003729 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003730
3731 /* If certificate uses an EC key, make sure the curve is OK */
3732 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3733 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3734 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003735 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003736 if( ret == 0 )
3737 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003738 }
3739 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003740#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003741
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003742 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3743 ciphersuite_info,
3744 ! ssl->endpoint ) != 0 )
3745 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003746 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003747 if( ret == 0 )
3748 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3749 }
3750
Paul Bakker5121ce52009-01-03 21:22:43 +00003751 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3752 ret = 0;
3753 }
3754
3755 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3756
3757 return( ret );
3758}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003759#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3760 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3761 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3762 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3763 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3764 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3765 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003766
3767int ssl_write_change_cipher_spec( ssl_context *ssl )
3768{
3769 int ret;
3770
3771 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3772
3773 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3774 ssl->out_msglen = 1;
3775 ssl->out_msg[0] = 1;
3776
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 ssl->state++;
3778
3779 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3780 {
3781 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3782 return( ret );
3783 }
3784
3785 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3786
3787 return( 0 );
3788}
3789
3790int ssl_parse_change_cipher_spec( ssl_context *ssl )
3791{
3792 int ret;
3793
3794 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3795
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3797 {
3798 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3799 return( ret );
3800 }
3801
3802 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3803 {
3804 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003805 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806 }
3807
3808 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3809 {
3810 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003811 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 }
3813
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003814 /*
3815 * Switch to our negotiated transform and session parameters for inbound
3816 * data.
3817 */
3818 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3819 ssl->transform_in = ssl->transform_negotiate;
3820 ssl->session_in = ssl->session_negotiate;
3821
3822#if defined(POLARSSL_SSL_PROTO_DTLS)
3823 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3824 {
3825#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3826 ssl_dtls_replay_reset( ssl );
3827#endif
3828
3829 /* Increment epoch */
3830 if( ++ssl->in_epoch == 0 )
3831 {
3832 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3833 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3834 }
3835 }
3836 else
3837#endif /* POLARSSL_SSL_PROTO_DTLS */
3838 memset( ssl->in_ctr, 0, 8 );
3839
3840 /*
3841 * Set the in_msg pointer to the correct location based on IV length
3842 */
3843 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3844 {
3845 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3846 ssl->transform_negotiate->fixed_ivlen;
3847 }
3848 else
3849 ssl->in_msg = ssl->in_iv;
3850
3851#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3852 if( ssl_hw_record_activate != NULL )
3853 {
3854 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3855 {
3856 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3857 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3858 }
3859 }
3860#endif
3861
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 ssl->state++;
3863
3864 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3865
3866 return( 0 );
3867}
3868
Paul Bakker41c83d32013-03-20 14:39:14 +01003869void ssl_optimize_checksum( ssl_context *ssl,
3870 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003871{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003872 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003873
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003874#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3875 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003876 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003877 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003878 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003879#endif
3880#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3881#if defined(POLARSSL_SHA512_C)
3882 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3883 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3884 else
3885#endif
3886#if defined(POLARSSL_SHA256_C)
3887 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003888 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003889 else
3890#endif
3891#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003892 {
3893 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003894 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003895 }
Paul Bakker380da532012-04-18 16:10:25 +00003896}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003897
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003898void ssl_reset_checksum( ssl_context *ssl )
3899{
3900#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3901 defined(POLARSSL_SSL_PROTO_TLS1_1)
3902 md5_starts( &ssl->handshake->fin_md5 );
3903 sha1_starts( &ssl->handshake->fin_sha1 );
3904#endif
3905#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3906#if defined(POLARSSL_SHA256_C)
3907 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3908#endif
3909#if defined(POLARSSL_SHA512_C)
3910 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3911#endif
3912#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3913}
3914
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003915static void ssl_update_checksum_start( ssl_context *ssl,
3916 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003917{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003918#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3919 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003920 md5_update( &ssl->handshake->fin_md5 , buf, len );
3921 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003922#endif
3923#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3924#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003925 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003926#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003927#if defined(POLARSSL_SHA512_C)
3928 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003929#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003930#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003931}
3932
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003933#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3934 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003935static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3936 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003937{
Paul Bakker48916f92012-09-16 19:57:18 +00003938 md5_update( &ssl->handshake->fin_md5 , buf, len );
3939 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003940}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003941#endif
Paul Bakker380da532012-04-18 16:10:25 +00003942
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003943#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3944#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003945static void ssl_update_checksum_sha256( ssl_context *ssl,
3946 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003947{
Paul Bakker9e36f042013-06-30 14:34:05 +02003948 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003949}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003950#endif
Paul Bakker380da532012-04-18 16:10:25 +00003951
Paul Bakker9e36f042013-06-30 14:34:05 +02003952#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003953static void ssl_update_checksum_sha384( ssl_context *ssl,
3954 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003955{
Paul Bakker9e36f042013-06-30 14:34:05 +02003956 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003957}
Paul Bakker769075d2012-11-24 11:26:46 +01003958#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003959#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003960
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003961#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003962static void ssl_calc_finished_ssl(
3963 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003964{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003965 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003966 md5_context md5;
3967 sha1_context sha1;
3968
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 unsigned char padbuf[48];
3970 unsigned char md5sum[16];
3971 unsigned char sha1sum[20];
3972
Paul Bakker48916f92012-09-16 19:57:18 +00003973 ssl_session *session = ssl->session_negotiate;
3974 if( !session )
3975 session = ssl->session;
3976
Paul Bakker1ef83d62012-04-11 12:09:53 +00003977 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3978
Paul Bakker48916f92012-09-16 19:57:18 +00003979 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3980 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981
3982 /*
3983 * SSLv3:
3984 * hash =
3985 * MD5( master + pad2 +
3986 * MD5( handshake + sender + master + pad1 ) )
3987 * + SHA1( master + pad2 +
3988 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 */
3990
Paul Bakker90995b52013-06-24 19:20:35 +02003991#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003992 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003993 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003994#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
Paul Bakker90995b52013-06-24 19:20:35 +02003996#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003997 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003999#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004000
Paul Bakker3c2122f2013-06-24 19:03:14 +02004001 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4002 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
Paul Bakker1ef83d62012-04-11 12:09:53 +00004004 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
Paul Bakker3c2122f2013-06-24 19:03:14 +02004006 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004007 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004008 md5_update( &md5, padbuf, 48 );
4009 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
Paul Bakker3c2122f2013-06-24 19:03:14 +02004011 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004012 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013 sha1_update( &sha1, padbuf, 40 );
4014 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakker1ef83d62012-04-11 12:09:53 +00004016 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004017
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004019 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004020 md5_update( &md5, padbuf, 48 );
4021 md5_update( &md5, md5sum, 16 );
4022 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004023
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004025 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004026 sha1_update( &sha1, padbuf , 40 );
4027 sha1_update( &sha1, sha1sum, 20 );
4028 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
Paul Bakker1ef83d62012-04-11 12:09:53 +00004030 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031
Paul Bakker5b4af392014-06-26 12:09:34 +02004032 md5_free( &md5 );
4033 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004034
Paul Bakker34617722014-06-13 17:20:13 +02004035 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4036 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4037 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004038
4039 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4040}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004041#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004043#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004044static void ssl_calc_finished_tls(
4045 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004046{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004047 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004048 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004049 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004051 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004052
Paul Bakker48916f92012-09-16 19:57:18 +00004053 ssl_session *session = ssl->session_negotiate;
4054 if( !session )
4055 session = ssl->session;
4056
Paul Bakker1ef83d62012-04-11 12:09:53 +00004057 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
Paul Bakker48916f92012-09-16 19:57:18 +00004059 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4060 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004061
Paul Bakker1ef83d62012-04-11 12:09:53 +00004062 /*
4063 * TLSv1:
4064 * hash = PRF( master, finished_label,
4065 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4066 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004067
Paul Bakker90995b52013-06-24 19:20:35 +02004068#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004069 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4070 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004071#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004072
Paul Bakker90995b52013-06-24 19:20:35 +02004073#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004074 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4075 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004076#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004077
4078 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004079 ? "client finished"
4080 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004081
4082 md5_finish( &md5, padbuf );
4083 sha1_finish( &sha1, padbuf + 16 );
4084
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004085 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004086 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004087
4088 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4089
Paul Bakker5b4af392014-06-26 12:09:34 +02004090 md5_free( &md5 );
4091 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004092
Paul Bakker34617722014-06-13 17:20:13 +02004093 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004094
4095 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4096}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004097#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004098
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004099#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4100#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004101static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004102 ssl_context *ssl, unsigned char *buf, int from )
4103{
4104 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004105 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004106 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004107 unsigned char padbuf[32];
4108
Paul Bakker48916f92012-09-16 19:57:18 +00004109 ssl_session *session = ssl->session_negotiate;
4110 if( !session )
4111 session = ssl->session;
4112
Paul Bakker380da532012-04-18 16:10:25 +00004113 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114
Paul Bakker9e36f042013-06-30 14:34:05 +02004115 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004116
4117 /*
4118 * TLSv1.2:
4119 * hash = PRF( master, finished_label,
4120 * Hash( handshake ) )[0.11]
4121 */
4122
Paul Bakker9e36f042013-06-30 14:34:05 +02004123#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004124 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004125 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004126#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004127
4128 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004129 ? "client finished"
4130 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004131
Paul Bakker9e36f042013-06-30 14:34:05 +02004132 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004133
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004134 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004135 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136
4137 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4138
Paul Bakker5b4af392014-06-26 12:09:34 +02004139 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004140
Paul Bakker34617722014-06-13 17:20:13 +02004141 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004142
4143 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4144}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004145#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004146
Paul Bakker9e36f042013-06-30 14:34:05 +02004147#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004148static void ssl_calc_finished_tls_sha384(
4149 ssl_context *ssl, unsigned char *buf, int from )
4150{
4151 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004152 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004153 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004154 unsigned char padbuf[48];
4155
Paul Bakker48916f92012-09-16 19:57:18 +00004156 ssl_session *session = ssl->session_negotiate;
4157 if( !session )
4158 session = ssl->session;
4159
Paul Bakker380da532012-04-18 16:10:25 +00004160 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004161
Paul Bakker9e36f042013-06-30 14:34:05 +02004162 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004163
4164 /*
4165 * TLSv1.2:
4166 * hash = PRF( master, finished_label,
4167 * Hash( handshake ) )[0.11]
4168 */
4169
Paul Bakker9e36f042013-06-30 14:34:05 +02004170#if !defined(POLARSSL_SHA512_ALT)
4171 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4172 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004173#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004174
4175 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004176 ? "client finished"
4177 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004178
Paul Bakker9e36f042013-06-30 14:34:05 +02004179 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004180
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004181 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004182 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004183
4184 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4185
Paul Bakker5b4af392014-06-26 12:09:34 +02004186 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004187
Paul Bakker34617722014-06-13 17:20:13 +02004188 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004189
4190 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4191}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004192#endif /* POLARSSL_SHA512_C */
4193#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004194
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004195static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004196{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004197 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004198
4199 /*
4200 * Free our handshake params
4201 */
4202 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004203 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004204 ssl->handshake = NULL;
4205
4206 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004207 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004208 */
4209 if( ssl->transform )
4210 {
4211 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004212 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004213 }
4214 ssl->transform = ssl->transform_negotiate;
4215 ssl->transform_negotiate = NULL;
4216
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004217 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4218}
4219
4220void ssl_handshake_wrapup( ssl_context *ssl )
4221{
4222 int resume = ssl->handshake->resume;
4223
4224 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4225
4226 if( ssl->renegotiation == SSL_RENEGOTIATION )
4227 {
4228 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4229 ssl->renego_records_seen = 0;
4230 }
4231
4232 /*
4233 * Free the previous session and switch in the current one
4234 */
Paul Bakker0a597072012-09-25 21:55:46 +00004235 if( ssl->session )
4236 {
4237 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004238 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004239 }
4240 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004241 ssl->session_negotiate = NULL;
4242
Paul Bakker0a597072012-09-25 21:55:46 +00004243 /*
4244 * Add cache entry
4245 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004246 if( ssl->f_set_cache != NULL &&
4247 ssl->session->length != 0 &&
4248 resume == 0 )
4249 {
Paul Bakker0a597072012-09-25 21:55:46 +00004250 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4251 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004252 }
Paul Bakker0a597072012-09-25 21:55:46 +00004253
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004254#if defined(POLARSSL_SSL_PROTO_DTLS)
4255 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4256 ssl->handshake->flight != NULL )
4257 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004258 /* Cancel handshake timer */
4259 ssl_set_timer( ssl, 0 );
4260
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004261 /* Keep last flight around in case we need to resend it:
4262 * we need the handshake and transform structures for that */
4263 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4264 }
4265 else
4266#endif
4267 ssl_handshake_wrapup_free_hs_transform( ssl );
4268
Paul Bakker48916f92012-09-16 19:57:18 +00004269 ssl->state++;
4270
4271 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4272}
4273
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274int ssl_write_finished( ssl_context *ssl )
4275{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004276 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004277
4278 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4279
Paul Bakker92be97b2013-01-02 17:30:03 +01004280 /*
4281 * Set the out_msg pointer to the correct location based on IV length
4282 */
4283 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4284 {
4285 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4286 ssl->transform_negotiate->fixed_ivlen;
4287 }
4288 else
4289 ssl->out_msg = ssl->out_iv;
4290
Paul Bakker48916f92012-09-16 19:57:18 +00004291 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004292
4293 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004294 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4295
Paul Bakker48916f92012-09-16 19:57:18 +00004296 ssl->verify_data_len = hash_len;
4297 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4298
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 ssl->out_msglen = 4 + hash_len;
4300 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4301 ssl->out_msg[0] = SSL_HS_FINISHED;
4302
4303 /*
4304 * In case of session resuming, invert the client and server
4305 * ChangeCipherSpec messages order.
4306 */
Paul Bakker0a597072012-09-25 21:55:46 +00004307 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 {
4309 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004310 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 else
4312 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4313 }
4314 else
4315 ssl->state++;
4316
Paul Bakker48916f92012-09-16 19:57:18 +00004317 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004318 * Switch to our negotiated transform and session parameters for outbound
4319 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004320 */
4321 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004322
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004323#if defined(POLARSSL_SSL_PROTO_DTLS)
4324 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4325 {
4326 unsigned char i;
4327
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004328 /* Remember current epoch settings for resending */
4329 ssl->handshake->alt_transform_out = ssl->transform_out;
4330 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4331
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004332 /* Set sequence_number to zero */
4333 memset( ssl->out_ctr + 2, 0, 6 );
4334
4335 /* Increment epoch */
4336 for( i = 2; i > 0; i-- )
4337 if( ++ssl->out_ctr[i - 1] != 0 )
4338 break;
4339
4340 /* The loop goes to its end iff the counter is wrapping */
4341 if( i == 0 )
4342 {
4343 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4344 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4345 }
4346 }
4347 else
4348#endif /* POLARSSL_SSL_PROTO_DTLS */
4349 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004350
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004351 ssl->transform_out = ssl->transform_negotiate;
4352 ssl->session_out = ssl->session_negotiate;
4353
Paul Bakker07eb38b2012-12-19 14:42:06 +01004354#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004355 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004356 {
4357 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4358 {
4359 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4360 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4361 }
4362 }
4363#endif
4364
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004365#if defined(POLARSSL_SSL_PROTO_DTLS)
4366 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4367 ssl_send_flight_completed( ssl );
4368#endif
4369
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4371 {
4372 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4373 return( ret );
4374 }
4375
4376 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4377
4378 return( 0 );
4379}
4380
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004381#if defined(POLARSSL_SSL_PROTO_SSL3)
4382#define SSL_MAX_HASH_LEN 36
4383#else
4384#define SSL_MAX_HASH_LEN 12
4385#endif
4386
Paul Bakker5121ce52009-01-03 21:22:43 +00004387int ssl_parse_finished( ssl_context *ssl )
4388{
Paul Bakker23986e52011-04-24 08:57:21 +00004389 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004390 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004391 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004392
4393 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4394
Paul Bakker48916f92012-09-16 19:57:18 +00004395 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004396
Paul Bakker5121ce52009-01-03 21:22:43 +00004397 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4398 {
4399 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4400 return( ret );
4401 }
4402
4403 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4404 {
4405 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004406 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004407 }
4408
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004409 /* There is currently no ciphersuite using another length with TLS 1.2 */
4410#if defined(POLARSSL_SSL_PROTO_SSL3)
4411 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4412 hash_len = 36;
4413 else
4414#endif
4415 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004416
4417 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004418 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004419 {
4420 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004421 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 }
4423
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004424 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4425 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004426 {
4427 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004428 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 }
4430
Paul Bakker48916f92012-09-16 19:57:18 +00004431 ssl->verify_data_len = hash_len;
4432 memcpy( ssl->peer_verify_data, buf, hash_len );
4433
Paul Bakker0a597072012-09-25 21:55:46 +00004434 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 {
4436 if( ssl->endpoint == SSL_IS_CLIENT )
4437 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4438
4439 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004440 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004441 }
4442 else
4443 ssl->state++;
4444
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004445#if defined(POLARSSL_SSL_PROTO_DTLS)
4446 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4447 ssl_recv_flight_completed( ssl );
4448#endif
4449
Paul Bakker5121ce52009-01-03 21:22:43 +00004450 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4451
4452 return( 0 );
4453}
4454
Paul Bakker968afaa2014-07-09 11:09:24 +02004455static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004456{
4457 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4458
4459#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4460 defined(POLARSSL_SSL_PROTO_TLS1_1)
4461 md5_init( &handshake->fin_md5 );
4462 sha1_init( &handshake->fin_sha1 );
4463 md5_starts( &handshake->fin_md5 );
4464 sha1_starts( &handshake->fin_sha1 );
4465#endif
4466#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4467#if defined(POLARSSL_SHA256_C)
4468 sha256_init( &handshake->fin_sha256 );
4469 sha256_starts( &handshake->fin_sha256, 0 );
4470#endif
4471#if defined(POLARSSL_SHA512_C)
4472 sha512_init( &handshake->fin_sha512 );
4473 sha512_starts( &handshake->fin_sha512, 1 );
4474#endif
4475#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4476
4477 handshake->update_checksum = ssl_update_checksum_start;
4478 handshake->sig_alg = SSL_HASH_SHA1;
4479
4480#if defined(POLARSSL_DHM_C)
4481 dhm_init( &handshake->dhm_ctx );
4482#endif
4483#if defined(POLARSSL_ECDH_C)
4484 ecdh_init( &handshake->ecdh_ctx );
4485#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004486}
4487
4488static void ssl_transform_init( ssl_transform *transform )
4489{
4490 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004491
4492 cipher_init( &transform->cipher_ctx_enc );
4493 cipher_init( &transform->cipher_ctx_dec );
4494
4495 md_init( &transform->md_ctx_enc );
4496 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004497}
4498
4499void ssl_session_init( ssl_session *session )
4500{
4501 memset( session, 0, sizeof(ssl_session) );
4502}
4503
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004504static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004505{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004506 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004507 if( ssl->transform_negotiate )
4508 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004509 if( ssl->session_negotiate )
4510 ssl_session_free( ssl->session_negotiate );
4511 if( ssl->handshake )
4512 ssl_handshake_free( ssl->handshake );
4513
4514 /*
4515 * Either the pointers are now NULL or cleared properly and can be freed.
4516 * Now allocate missing structures.
4517 */
4518 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004519 {
4520 ssl->transform_negotiate =
4521 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4522 }
Paul Bakker48916f92012-09-16 19:57:18 +00004523
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004524 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004525 {
4526 ssl->session_negotiate =
4527 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4528 }
Paul Bakker48916f92012-09-16 19:57:18 +00004529
Paul Bakker82788fb2014-10-20 13:59:19 +02004530 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004531 {
4532 ssl->handshake = (ssl_handshake_params *)
4533 polarssl_malloc( sizeof(ssl_handshake_params) );
4534 }
Paul Bakker48916f92012-09-16 19:57:18 +00004535
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004536 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004537 if( ssl->handshake == NULL ||
4538 ssl->transform_negotiate == NULL ||
4539 ssl->session_negotiate == NULL )
4540 {
4541 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004542
4543 polarssl_free( ssl->handshake );
4544 polarssl_free( ssl->transform_negotiate );
4545 polarssl_free( ssl->session_negotiate );
4546
4547 ssl->handshake = NULL;
4548 ssl->transform_negotiate = NULL;
4549 ssl->session_negotiate = NULL;
4550
Paul Bakker48916f92012-09-16 19:57:18 +00004551 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4552 }
4553
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004554 /* Initialize structures */
4555 ssl_session_init( ssl->session_negotiate );
4556 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004557 ssl_handshake_params_init( ssl->handshake );
4558
4559#if defined(POLARSSL_X509_CRT_PARSE_C)
4560 ssl->handshake->key_cert = ssl->key_cert;
4561#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004562
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004563 /*
4564 * We may not know yet if we're using DTLS,
4565 * so always initiliase DTLS-specific fields.
4566 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004567#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004568 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004569
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004570 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004571 if( ssl->endpoint == SSL_IS_CLIENT )
4572 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4573 else
4574 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004575#endif
4576
Paul Bakker48916f92012-09-16 19:57:18 +00004577 return( 0 );
4578}
4579
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004580#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4581/* Dummy cookie callbacks for defaults */
4582static int ssl_cookie_write_dummy( void *ctx,
4583 unsigned char **p, unsigned char *end,
4584 const unsigned char *cli_id, size_t cli_id_len )
4585{
4586 ((void) ctx);
4587 ((void) p);
4588 ((void) end);
4589 ((void) cli_id);
4590 ((void) cli_id_len);
4591
4592 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4593}
4594
4595static int ssl_cookie_check_dummy( void *ctx,
4596 const unsigned char *cookie, size_t cookie_len,
4597 const unsigned char *cli_id, size_t cli_id_len )
4598{
4599 ((void) ctx);
4600 ((void) cookie);
4601 ((void) cookie_len);
4602 ((void) cli_id);
4603 ((void) cli_id_len);
4604
4605 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4606}
4607#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4608
Paul Bakker5121ce52009-01-03 21:22:43 +00004609/*
4610 * Initialize an SSL context
4611 */
4612int ssl_init( ssl_context *ssl )
4613{
Paul Bakker48916f92012-09-16 19:57:18 +00004614 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 int len = SSL_BUFFER_LEN;
4616
4617 memset( ssl, 0, sizeof( ssl_context ) );
4618
Paul Bakker62f2dee2012-09-28 07:31:51 +00004619 /*
4620 * Sane defaults
4621 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004622 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4623 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4624 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4625 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004626
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004627 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004628
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004629 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4630
Paul Bakker62f2dee2012-09-28 07:31:51 +00004631#if defined(POLARSSL_DHM_C)
4632 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4633 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4634 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4635 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4636 {
4637 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4638 return( ret );
4639 }
4640#endif
4641
4642 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004643 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004644 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004645 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004646 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004647
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004648 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 {
4650 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004651 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004652 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004653 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004654 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004655 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004656 }
4657
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004658 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4659 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004660
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004661 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4662 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4663
Paul Bakker606b4ba2013-08-14 16:52:14 +02004664#if defined(POLARSSL_SSL_SESSION_TICKETS)
4665 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4666#endif
4667
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004668#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004669 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004670#endif
4671
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004672#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4673 ssl->f_cookie_write = ssl_cookie_write_dummy;
4674 ssl->f_cookie_check = ssl_cookie_check_dummy;
4675#endif
4676
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004677#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4678 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4679#endif
4680
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004681#if defined(POLARSSL_SSL_PROTO_DTLS)
4682 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4683 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4684#endif
4685
Paul Bakker48916f92012-09-16 19:57:18 +00004686 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4687 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004688
4689 return( 0 );
4690}
4691
4692/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004693 * Reset an initialized and used SSL context for re-use while retaining
4694 * all application-set variables, function pointers and data.
4695 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004696int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004697{
Paul Bakker48916f92012-09-16 19:57:18 +00004698 int ret;
4699
Paul Bakker7eb013f2011-10-06 12:37:39 +00004700 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004701 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4702 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4703
4704 ssl->verify_data_len = 0;
4705 memset( ssl->own_verify_data, 0, 36 );
4706 memset( ssl->peer_verify_data, 0, 36 );
4707
Paul Bakker7eb013f2011-10-06 12:37:39 +00004708 ssl->in_offt = NULL;
4709
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004710 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004711 ssl->in_msgtype = 0;
4712 ssl->in_msglen = 0;
4713 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004714#if defined(POLARSSL_SSL_PROTO_DTLS)
4715 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004716 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004717#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004718#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4719 ssl_dtls_replay_reset( ssl );
4720#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004721
4722 ssl->in_hslen = 0;
4723 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004724 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004725
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004726 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004727 ssl->out_msgtype = 0;
4728 ssl->out_msglen = 0;
4729 ssl->out_left = 0;
4730
Paul Bakker48916f92012-09-16 19:57:18 +00004731 ssl->transform_in = NULL;
4732 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004733
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004734 ssl->renego_records_seen = 0;
4735
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004736 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4737 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004738
4739#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004740 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004741 {
4742 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004743 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004744 {
4745 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4746 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4747 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004748 }
4749#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004750
Paul Bakker48916f92012-09-16 19:57:18 +00004751 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004752 {
Paul Bakker48916f92012-09-16 19:57:18 +00004753 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004754 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004755 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004756 }
Paul Bakker48916f92012-09-16 19:57:18 +00004757
Paul Bakkerc0463502013-02-14 11:19:38 +01004758 if( ssl->session )
4759 {
4760 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004761 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004762 ssl->session = NULL;
4763 }
4764
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004765#if defined(POLARSSL_SSL_ALPN)
4766 ssl->alpn_chosen = NULL;
4767#endif
4768
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004769#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004770 polarssl_free( ssl->cli_id );
4771 ssl->cli_id = NULL;
4772 ssl->cli_id_len = 0;
4773#endif
4774
Paul Bakker48916f92012-09-16 19:57:18 +00004775 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4776 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004777
4778 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004779}
4780
Paul Bakkera503a632013-08-14 13:48:06 +02004781#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004782static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4783{
4784 aes_free( &tkeys->enc );
4785 aes_free( &tkeys->dec );
4786
4787 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4788}
4789
Paul Bakker7eb013f2011-10-06 12:37:39 +00004790/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004791 * Allocate and initialize ticket keys
4792 */
4793static int ssl_ticket_keys_init( ssl_context *ssl )
4794{
4795 int ret;
4796 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004797 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004798
4799 if( ssl->ticket_keys != NULL )
4800 return( 0 );
4801
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004802 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4803 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004804 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4805
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004806 aes_init( &tkeys->enc );
4807 aes_init( &tkeys->dec );
4808
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004809 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004810 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004811 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004812 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004813 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004814 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004815
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004816 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4817 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4818 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4819 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004820 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004821 polarssl_free( tkeys );
4822 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004823 }
4824
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004825 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004826 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004827 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004828 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004829 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004830 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004831
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004832 ssl->ticket_keys = tkeys;
4833
4834 return( 0 );
4835}
Paul Bakkera503a632013-08-14 13:48:06 +02004836#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004837
4838/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 * SSL set accessors
4840 */
4841void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4842{
4843 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004844
Paul Bakker606b4ba2013-08-14 16:52:14 +02004845#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004846 if( endpoint == SSL_IS_CLIENT )
4847 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004848#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004849}
4850
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004851int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004852{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004853#if defined(POLARSSL_SSL_PROTO_DTLS)
4854 if( transport == SSL_TRANSPORT_DATAGRAM )
4855 {
4856 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004857
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004858 ssl->out_hdr = ssl->out_buf;
4859 ssl->out_ctr = ssl->out_buf + 3;
4860 ssl->out_len = ssl->out_buf + 11;
4861 ssl->out_iv = ssl->out_buf + 13;
4862 ssl->out_msg = ssl->out_buf + 13;
4863
4864 ssl->in_hdr = ssl->in_buf;
4865 ssl->in_ctr = ssl->in_buf + 3;
4866 ssl->in_len = ssl->in_buf + 11;
4867 ssl->in_iv = ssl->in_buf + 13;
4868 ssl->in_msg = ssl->in_buf + 13;
4869
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004870 /* DTLS starts with TLS1.1 */
4871 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4872 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004873
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004874 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4875 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4876
4877 return( 0 );
4878 }
4879#endif
4880
4881 if( transport == SSL_TRANSPORT_STREAM )
4882 {
4883 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004884
4885 ssl->out_ctr = ssl->out_buf;
4886 ssl->out_hdr = ssl->out_buf + 8;
4887 ssl->out_len = ssl->out_buf + 11;
4888 ssl->out_iv = ssl->out_buf + 13;
4889 ssl->out_msg = ssl->out_buf + 13;
4890
4891 ssl->in_ctr = ssl->in_buf;
4892 ssl->in_hdr = ssl->in_buf + 8;
4893 ssl->in_len = ssl->in_buf + 11;
4894 ssl->in_iv = ssl->in_buf + 13;
4895 ssl->in_msg = ssl->in_buf + 13;
4896
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004897 return( 0 );
4898 }
4899
4900 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004901}
4902
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004903#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4904void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4905{
4906 ssl->anti_replay = mode;
4907}
4908#endif
4909
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004910#if defined(POLARSSL_SSL_PROTO_DTLS)
4911void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4912{
4913 ssl->hs_timeout_min = min;
4914 ssl->hs_timeout_max = max;
4915}
4916#endif
4917
Paul Bakker5121ce52009-01-03 21:22:43 +00004918void ssl_set_authmode( ssl_context *ssl, int authmode )
4919{
4920 ssl->authmode = authmode;
4921}
4922
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004923#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004924void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004925 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004926 void *p_vrfy )
4927{
4928 ssl->f_vrfy = f_vrfy;
4929 ssl->p_vrfy = p_vrfy;
4930}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004931#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004932
Paul Bakker5121ce52009-01-03 21:22:43 +00004933void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004934 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004935 void *p_rng )
4936{
4937 ssl->f_rng = f_rng;
4938 ssl->p_rng = p_rng;
4939}
4940
4941void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004942 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004943 void *p_dbg )
4944{
4945 ssl->f_dbg = f_dbg;
4946 ssl->p_dbg = p_dbg;
4947}
4948
4949void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004950 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004951 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004952{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004953 if( p_recv != p_send )
4954 {
4955 ssl->f_recv = NULL;
4956 ssl->f_send = NULL;
4957 ssl->p_bio = NULL;
4958 return;
4959 }
4960
Paul Bakker5121ce52009-01-03 21:22:43 +00004961 ssl->f_recv = f_recv;
4962 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004963 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004964}
4965
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004966void ssl_set_bio_timeout( ssl_context *ssl,
4967 void *p_bio,
4968 int (*f_send)(void *, const unsigned char *, size_t),
4969 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004970 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4971 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004972{
4973 ssl->p_bio = p_bio;
4974 ssl->f_send = f_send;
4975 ssl->f_recv = f_recv;
4976 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004977 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004978}
4979
Paul Bakker0a597072012-09-25 21:55:46 +00004980void ssl_set_session_cache( ssl_context *ssl,
4981 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4982 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004983{
Paul Bakker0a597072012-09-25 21:55:46 +00004984 ssl->f_get_cache = f_get_cache;
4985 ssl->p_get_cache = p_get_cache;
4986 ssl->f_set_cache = f_set_cache;
4987 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004988}
4989
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004990int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004991{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004992 int ret;
4993
4994 if( ssl == NULL ||
4995 session == NULL ||
4996 ssl->session_negotiate == NULL ||
4997 ssl->endpoint != SSL_IS_CLIENT )
4998 {
4999 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5000 }
5001
5002 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5003 return( ret );
5004
Paul Bakker0a597072012-09-25 21:55:46 +00005005 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005006
5007 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005008}
5009
Paul Bakkerb68cad62012-08-23 08:34:18 +00005010void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005011{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005012 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5013 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5014 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5015 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5016}
5017
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005018void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5019 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005020 int major, int minor )
5021{
5022 if( major != SSL_MAJOR_VERSION_3 )
5023 return;
5024
5025 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5026 return;
5027
5028 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005029}
5030
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005031#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005032/* Add a new (empty) key_cert entry an return a pointer to it */
5033static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5034{
5035 ssl_key_cert *key_cert, *last;
5036
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005037 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5038 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005039 return( NULL );
5040
5041 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5042
5043 /* Append the new key_cert to the (possibly empty) current list */
5044 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005045 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005046 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005047 if( ssl->handshake != NULL )
5048 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005049 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005050 else
5051 {
5052 last = ssl->key_cert;
5053 while( last->next != NULL )
5054 last = last->next;
5055 last->next = key_cert;
5056 }
5057
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005058 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005059}
5060
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005061void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005062 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005063{
5064 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005065 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005066 ssl->peer_cn = peer_cn;
5067}
5068
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005069int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005070 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005071{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005072 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5073
5074 if( key_cert == NULL )
5075 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5076
5077 key_cert->cert = own_cert;
5078 key_cert->key = pk_key;
5079
5080 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005081}
5082
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005083#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005084int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005085 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005086{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005087 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005088 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005089
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005090 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005091 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5092
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005093 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5094 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005095 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005096
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005097 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005098
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005099 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005100 if( ret != 0 )
5101 return( ret );
5102
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005103 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005104 return( ret );
5105
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005106 key_cert->cert = own_cert;
5107 key_cert->key_own_alloc = 1;
5108
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005109 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005110}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005111#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005112
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005113int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005114 void *rsa_key,
5115 rsa_decrypt_func rsa_decrypt,
5116 rsa_sign_func rsa_sign,
5117 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005118{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005119 int ret;
5120 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005121
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005122 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005123 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5124
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005125 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5126 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005127 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005128
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005129 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005130
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005131 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5132 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5133 return( ret );
5134
5135 key_cert->cert = own_cert;
5136 key_cert->key_own_alloc = 1;
5137
5138 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005139}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005140#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005141
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005142#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005143int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5144 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005145{
Paul Bakker6db455e2013-09-18 17:29:31 +02005146 if( psk == NULL || psk_identity == NULL )
5147 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5148
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005149 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005150 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5151
Paul Bakker6db455e2013-09-18 17:29:31 +02005152 if( ssl->psk != NULL )
5153 {
5154 polarssl_free( ssl->psk );
5155 polarssl_free( ssl->psk_identity );
5156 }
5157
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005158 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005159 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005160
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005161 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005162 ssl->psk_identity = (unsigned char *)
5163 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005164
5165 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5166 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5167
5168 memcpy( ssl->psk, psk, ssl->psk_len );
5169 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005170
5171 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005172}
5173
5174void ssl_set_psk_cb( ssl_context *ssl,
5175 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5176 size_t),
5177 void *p_psk )
5178{
5179 ssl->f_psk = f_psk;
5180 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005181}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005182#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005183
Paul Bakker48916f92012-09-16 19:57:18 +00005184#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005185int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005186{
5187 int ret;
5188
Paul Bakker48916f92012-09-16 19:57:18 +00005189 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005190 {
5191 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5192 return( ret );
5193 }
5194
Paul Bakker48916f92012-09-16 19:57:18 +00005195 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005196 {
5197 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5198 return( ret );
5199 }
5200
5201 return( 0 );
5202}
5203
Paul Bakker1b57b062011-01-06 15:48:19 +00005204int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5205{
5206 int ret;
5207
Paul Bakker66d5d072014-06-17 16:39:18 +02005208 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005209 {
5210 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5211 return( ret );
5212 }
5213
Paul Bakker66d5d072014-06-17 16:39:18 +02005214 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005215 {
5216 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5217 return( ret );
5218 }
5219
5220 return( 0 );
5221}
Paul Bakker48916f92012-09-16 19:57:18 +00005222#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005223
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005224#if defined(POLARSSL_SSL_SET_CURVES)
5225/*
5226 * Set the allowed elliptic curves
5227 */
5228void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5229{
5230 ssl->curve_list = curve_list;
5231}
5232#endif
5233
Paul Bakker0be444a2013-08-27 21:55:01 +02005234#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005235int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005236{
5237 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005238 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005239
5240 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005241
5242 if( ssl->hostname_len + 1 == 0 )
5243 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5244
Paul Bakker6e339b52013-07-03 13:37:05 +02005245 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005246
Paul Bakkerb15b8512012-01-13 13:44:06 +00005247 if( ssl->hostname == NULL )
5248 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5249
Paul Bakker3c2122f2013-06-24 19:03:14 +02005250 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005251 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005252
Paul Bakker40ea7de2009-05-03 10:18:48 +00005253 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005254
5255 return( 0 );
5256}
5257
Paul Bakker5701cdc2012-09-27 21:49:42 +00005258void ssl_set_sni( ssl_context *ssl,
5259 int (*f_sni)(void *, ssl_context *,
5260 const unsigned char *, size_t),
5261 void *p_sni )
5262{
5263 ssl->f_sni = f_sni;
5264 ssl->p_sni = p_sni;
5265}
Paul Bakker0be444a2013-08-27 21:55:01 +02005266#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005267
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005268#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005269int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005270{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005271 size_t cur_len, tot_len;
5272 const char **p;
5273
5274 /*
5275 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5276 * truncated". Check lengths now rather than later.
5277 */
5278 tot_len = 0;
5279 for( p = protos; *p != NULL; p++ )
5280 {
5281 cur_len = strlen( *p );
5282 tot_len += cur_len;
5283
5284 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5285 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5286 }
5287
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005288 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005289
5290 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005291}
5292
5293const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5294{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005295 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005296}
5297#endif /* POLARSSL_SSL_ALPN */
5298
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005299static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005300{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005301 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5302 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5303 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5304 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005305 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005306 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005307 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005308
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005309 return( 0 );
5310}
5311
5312int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5313{
5314 if( ssl_check_version( ssl, major, minor ) != 0 )
5315 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5316
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005317 ssl->max_major_ver = major;
5318 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005319
5320 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005321}
5322
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005323int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005324{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005325 if( ssl_check_version( ssl, major, minor ) != 0 )
5326 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005327
5328 ssl->min_major_ver = major;
5329 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005330
5331 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005332}
5333
Paul Bakker05decb22013-08-15 13:33:48 +02005334#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005335int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5336{
Paul Bakker77e257e2013-12-16 15:29:52 +01005337 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005338 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005339 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005340 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005341 }
5342
5343 ssl->mfl_code = mfl_code;
5344
5345 return( 0 );
5346}
Paul Bakker05decb22013-08-15 13:33:48 +02005347#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005348
Paul Bakker1f2bc622013-08-15 13:45:55 +02005349#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005350int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005351{
5352 if( ssl->endpoint != SSL_IS_CLIENT )
5353 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5354
Paul Bakker8c1ede62013-07-19 14:14:37 +02005355 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005356
5357 return( 0 );
5358}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005359#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005360
Paul Bakker48916f92012-09-16 19:57:18 +00005361void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5362{
5363 ssl->disable_renegotiation = renegotiation;
5364}
5365
5366void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5367{
5368 ssl->allow_legacy_renegotiation = allow_legacy;
5369}
5370
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005371void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5372{
5373 ssl->renego_max_records = max_records;
5374}
5375
Paul Bakkera503a632013-08-14 13:48:06 +02005376#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005377int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5378{
5379 ssl->session_tickets = use_tickets;
5380
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005381 if( ssl->endpoint == SSL_IS_CLIENT )
5382 return( 0 );
5383
5384 if( ssl->f_rng == NULL )
5385 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5386
5387 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005388}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005389
5390void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5391{
5392 ssl->ticket_lifetime = lifetime;
5393}
Paul Bakkera503a632013-08-14 13:48:06 +02005394#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005395
Paul Bakker5121ce52009-01-03 21:22:43 +00005396/*
5397 * SSL get accessors
5398 */
Paul Bakker23986e52011-04-24 08:57:21 +00005399size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005400{
5401 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5402}
5403
Paul Bakkerff60ee62010-03-16 21:09:09 +00005404int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005405{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005406 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005407}
5408
Paul Bakkere3166ce2011-01-27 17:40:50 +00005409const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005410{
Paul Bakker926c8e42013-03-06 10:23:34 +01005411 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005412 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005413
Paul Bakkere3166ce2011-01-27 17:40:50 +00005414 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005415}
5416
Paul Bakker43ca69c2011-01-15 17:35:19 +00005417const char *ssl_get_version( const ssl_context *ssl )
5418{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005419#if defined(POLARSSL_SSL_PROTO_DTLS)
5420 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5421 {
5422 switch( ssl->minor_ver )
5423 {
5424 case SSL_MINOR_VERSION_2:
5425 return( "DTLSv1.0" );
5426
5427 case SSL_MINOR_VERSION_3:
5428 return( "DTLSv1.2" );
5429
5430 default:
5431 return( "unknown (DTLS)" );
5432 }
5433 }
5434#endif
5435
Paul Bakker43ca69c2011-01-15 17:35:19 +00005436 switch( ssl->minor_ver )
5437 {
5438 case SSL_MINOR_VERSION_0:
5439 return( "SSLv3.0" );
5440
5441 case SSL_MINOR_VERSION_1:
5442 return( "TLSv1.0" );
5443
5444 case SSL_MINOR_VERSION_2:
5445 return( "TLSv1.1" );
5446
Paul Bakker1ef83d62012-04-11 12:09:53 +00005447 case SSL_MINOR_VERSION_3:
5448 return( "TLSv1.2" );
5449
Paul Bakker43ca69c2011-01-15 17:35:19 +00005450 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005451 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005452 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005453}
5454
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005455#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005456const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005457{
5458 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005459 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005460
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005461 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005462}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005463#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005464
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005465int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5466{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005467 if( ssl == NULL ||
5468 dst == NULL ||
5469 ssl->session == NULL ||
5470 ssl->endpoint != SSL_IS_CLIENT )
5471 {
5472 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5473 }
5474
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005475 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005476}
5477
Paul Bakker5121ce52009-01-03 21:22:43 +00005478/*
Paul Bakker1961b702013-01-25 14:49:24 +01005479 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005480 */
Paul Bakker1961b702013-01-25 14:49:24 +01005481int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005482{
Paul Bakker40e46942009-01-03 21:51:57 +00005483 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005484
Paul Bakker40e46942009-01-03 21:51:57 +00005485#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005486 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005487 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005488#endif
5489
Paul Bakker40e46942009-01-03 21:51:57 +00005490#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005491 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005492 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005493#endif
5494
Paul Bakker1961b702013-01-25 14:49:24 +01005495 return( ret );
5496}
5497
5498/*
5499 * Perform the SSL handshake
5500 */
5501int ssl_handshake( ssl_context *ssl )
5502{
5503 int ret = 0;
5504
5505 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5506
5507 while( ssl->state != SSL_HANDSHAKE_OVER )
5508 {
5509 ret = ssl_handshake_step( ssl );
5510
5511 if( ret != 0 )
5512 break;
5513 }
5514
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5516
5517 return( ret );
5518}
5519
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005520#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005521/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005522 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005523 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005524static int ssl_write_hello_request( ssl_context *ssl )
5525{
5526 int ret;
5527
5528 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5529
5530 ssl->out_msglen = 4;
5531 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5532 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5533
5534 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5535 {
5536 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5537 return( ret );
5538 }
5539
5540 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5541
5542 return( 0 );
5543}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005544#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005545
5546/*
5547 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005548 * - any side: calling ssl_renegotiate(),
5549 * - client: receiving a HelloRequest during ssl_read(),
5550 * - server: receiving any handshake message on server during ssl_read() after
5551 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005552 * If the handshake doesn't complete due to waiting for I/O, it will continue
5553 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005554 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005555static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005556{
5557 int ret;
5558
5559 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5560
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005561 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5562 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005563
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005564 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5565 * the ServerHello will have message_seq = 1" */
5566#if defined(POLARSSL_SSL_PROTO_DTLS)
5567 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005568 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5569 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005570 if( ssl->endpoint == SSL_IS_SERVER )
5571 ssl->handshake->out_msg_seq = 1;
5572 else
5573 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005574 }
5575#endif
5576
Paul Bakker48916f92012-09-16 19:57:18 +00005577 ssl->state = SSL_HELLO_REQUEST;
5578 ssl->renegotiation = SSL_RENEGOTIATION;
5579
Paul Bakker48916f92012-09-16 19:57:18 +00005580 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5581 {
5582 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5583 return( ret );
5584 }
5585
5586 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5587
5588 return( 0 );
5589}
5590
5591/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005592 * Renegotiate current connection on client,
5593 * or request renegotiation on server
5594 */
5595int ssl_renegotiate( ssl_context *ssl )
5596{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005597 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005598
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005599#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005600 /* On server, just send the request */
5601 if( ssl->endpoint == SSL_IS_SERVER )
5602 {
5603 if( ssl->state != SSL_HANDSHAKE_OVER )
5604 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5605
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005606 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5607
5608 /* Did we already try/start sending HelloRequest? */
5609 if( ssl->out_left != 0 )
5610 return( ssl_flush_output( ssl ) );
5611
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005612 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005613 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005614#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005615
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005616#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005617 /*
5618 * On client, either start the renegotiation process or,
5619 * if already in progress, continue the handshake
5620 */
5621 if( ssl->renegotiation != SSL_RENEGOTIATION )
5622 {
5623 if( ssl->state != SSL_HANDSHAKE_OVER )
5624 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5625
5626 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5627 {
5628 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5629 return( ret );
5630 }
5631 }
5632 else
5633 {
5634 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5635 {
5636 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5637 return( ret );
5638 }
5639 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005640#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005641
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005642 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005643}
5644
5645/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005646 * Receive application data decrypted from the SSL layer
5647 */
Paul Bakker23986e52011-04-24 08:57:21 +00005648int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005649{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005650 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005651 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005652
5653 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5654
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005655#if defined(POLARSSL_SSL_PROTO_DTLS)
5656 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5657 ssl->handshake != NULL &&
5658 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5659 {
5660 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5661 return( ret );
5662
5663 if( ( ret = ssl_resend( ssl ) ) != 0 )
5664 return( ret );
5665 }
5666#endif
5667
Paul Bakker5121ce52009-01-03 21:22:43 +00005668 if( ssl->state != SSL_HANDSHAKE_OVER )
5669 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005670 ret = ssl_handshake( ssl );
5671 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5672 {
5673 record_read = 1;
5674 }
5675 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005676 {
5677 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5678 return( ret );
5679 }
5680 }
5681
5682 if( ssl->in_offt == NULL )
5683 {
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005684#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005685 /* Start timer if not already running */
5686 if( ssl->time_limit == 0 )
5687 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005688#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005689
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005690 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005691 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005692 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5693 {
5694 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5695 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005696
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005697 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5698 return( ret );
5699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 }
5701
5702 if( ssl->in_msglen == 0 &&
5703 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5704 {
5705 /*
5706 * OpenSSL sends empty messages to randomize the IV
5707 */
5708 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5709 {
Paul Bakker831a7552011-05-18 13:32:51 +00005710 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5711 return( 0 );
5712
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5714 return( ret );
5715 }
5716 }
5717
Paul Bakker48916f92012-09-16 19:57:18 +00005718 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5719 {
5720 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5721
5722 if( ssl->endpoint == SSL_IS_CLIENT &&
5723 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005724 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005725 {
5726 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005727
5728 /* With DTLS, drop the packet (probably from last handshake) */
5729#if defined(POLARSSL_SSL_PROTO_DTLS)
5730 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5731 return( POLARSSL_ERR_NET_WANT_READ );
5732#endif
5733 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5734 }
5735
5736 if( ssl->endpoint == SSL_IS_SERVER &&
5737 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5738 {
5739 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5740
5741 /* With DTLS, drop the packet (probably from last handshake) */
5742#if defined(POLARSSL_SSL_PROTO_DTLS)
5743 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5744 return( POLARSSL_ERR_NET_WANT_READ );
5745#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005746 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5747 }
5748
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005749 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5750 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005751 ssl->allow_legacy_renegotiation ==
5752 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005753 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005754 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005755
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005756#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005757 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005758 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005759 /*
5760 * SSLv3 does not have a "no_renegotiation" alert
5761 */
5762 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5763 return( ret );
5764 }
5765 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005766#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005767#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5768 defined(POLARSSL_SSL_PROTO_TLS1_2)
5769 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005770 {
5771 if( ( ret = ssl_send_alert_message( ssl,
5772 SSL_ALERT_LEVEL_WARNING,
5773 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5774 {
5775 return( ret );
5776 }
Paul Bakker48916f92012-09-16 19:57:18 +00005777 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005778 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005779#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5780 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005781 {
5782 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005783 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005784 }
Paul Bakker48916f92012-09-16 19:57:18 +00005785 }
5786 else
5787 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005788#if defined(POLARSSL_SSL_PROTO_DTLS)
5789 /* DTLS clients need to know renego is server-initiated */
5790 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5791 ssl->endpoint == SSL_IS_CLIENT )
5792 {
5793 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5794 }
5795#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005796 ret = ssl_start_renegotiation( ssl );
5797 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5798 {
5799 record_read = 1;
5800 }
5801 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005802 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005803 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005804 return( ret );
5805 }
Paul Bakker48916f92012-09-16 19:57:18 +00005806 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005807
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005808 /* If a non-handshake record was read during renego, fallthrough,
5809 * else tell the user they should call ssl_read() again */
5810 if( ! record_read )
5811 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005812 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005813 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5814 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005815 ssl->renego_records_seen++;
5816
5817 if( ssl->renego_max_records >= 0 &&
5818 ssl->renego_records_seen > ssl->renego_max_records )
5819 {
5820 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5821 "but not honored by client" ) );
5822 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5823 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005824 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005825
5826 /* Fatal and closure alerts handled by ssl_read_record() */
5827 if( ssl->in_msgtype == SSL_MSG_ALERT )
5828 {
5829 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5830 return( POLARSSL_ERR_NET_WANT_READ );
5831 }
5832
5833 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005834 {
5835 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005836 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 }
5838
5839 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005840
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005841#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005842 /* We're going to return something now, cancel timer */
5843 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005845 }
5846
5847 n = ( len < ssl->in_msglen )
5848 ? len : ssl->in_msglen;
5849
5850 memcpy( buf, ssl->in_offt, n );
5851 ssl->in_msglen -= n;
5852
5853 if( ssl->in_msglen == 0 )
5854 /* all bytes consumed */
5855 ssl->in_offt = NULL;
5856 else
5857 /* more data available */
5858 ssl->in_offt += n;
5859
5860 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5861
Paul Bakker23986e52011-04-24 08:57:21 +00005862 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005863}
5864
5865/*
5866 * Send application data to be encrypted by the SSL layer
5867 */
Paul Bakker23986e52011-04-24 08:57:21 +00005868int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005869{
Paul Bakker23986e52011-04-24 08:57:21 +00005870 int ret;
5871 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005872 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005873
5874 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5875
5876 if( ssl->state != SSL_HANDSHAKE_OVER )
5877 {
5878 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5879 {
5880 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5881 return( ret );
5882 }
5883 }
5884
Paul Bakker05decb22013-08-15 13:33:48 +02005885#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005886 /*
5887 * Assume mfl_code is correct since it was checked when set
5888 */
5889 max_len = mfl_code_to_length[ssl->mfl_code];
5890
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005891 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005892 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005893 */
5894 if( ssl->session_out != NULL &&
5895 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5896 {
5897 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5898 }
Paul Bakker05decb22013-08-15 13:33:48 +02005899#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005900
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005901 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005902
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 if( ssl->out_left != 0 )
5904 {
5905 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5906 {
5907 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5908 return( ret );
5909 }
5910 }
Paul Bakker887bd502011-06-08 13:10:54 +00005911 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005912 {
Paul Bakker887bd502011-06-08 13:10:54 +00005913 ssl->out_msglen = n;
5914 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5915 memcpy( ssl->out_msg, buf, n );
5916
5917 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5918 {
5919 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5920 return( ret );
5921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005922 }
5923
5924 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5925
Paul Bakker23986e52011-04-24 08:57:21 +00005926 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005927}
5928
5929/*
5930 * Notify the peer that the connection is being closed
5931 */
5932int ssl_close_notify( ssl_context *ssl )
5933{
5934 int ret;
5935
5936 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5937
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005938 if( ssl->out_left != 0 )
5939 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005940
5941 if( ssl->state == SSL_HANDSHAKE_OVER )
5942 {
Paul Bakker48916f92012-09-16 19:57:18 +00005943 if( ( ret = ssl_send_alert_message( ssl,
5944 SSL_ALERT_LEVEL_WARNING,
5945 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005946 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005947 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005948 return( ret );
5949 }
5950 }
5951
5952 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5953
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005954 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005955}
5956
Paul Bakker48916f92012-09-16 19:57:18 +00005957void ssl_transform_free( ssl_transform *transform )
5958{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005959 if( transform == NULL )
5960 return;
5961
Paul Bakker48916f92012-09-16 19:57:18 +00005962#if defined(POLARSSL_ZLIB_SUPPORT)
5963 deflateEnd( &transform->ctx_deflate );
5964 inflateEnd( &transform->ctx_inflate );
5965#endif
5966
Paul Bakker84bbeb52014-07-01 14:53:22 +02005967 cipher_free( &transform->cipher_ctx_enc );
5968 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005969
Paul Bakker84bbeb52014-07-01 14:53:22 +02005970 md_free( &transform->md_ctx_enc );
5971 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005972
Paul Bakker34617722014-06-13 17:20:13 +02005973 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005974}
5975
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005976#if defined(POLARSSL_X509_CRT_PARSE_C)
5977static void ssl_key_cert_free( ssl_key_cert *key_cert )
5978{
5979 ssl_key_cert *cur = key_cert, *next;
5980
5981 while( cur != NULL )
5982 {
5983 next = cur->next;
5984
5985 if( cur->key_own_alloc )
5986 {
5987 pk_free( cur->key );
5988 polarssl_free( cur->key );
5989 }
5990 polarssl_free( cur );
5991
5992 cur = next;
5993 }
5994}
5995#endif /* POLARSSL_X509_CRT_PARSE_C */
5996
Paul Bakker48916f92012-09-16 19:57:18 +00005997void ssl_handshake_free( ssl_handshake_params *handshake )
5998{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005999 if( handshake == NULL )
6000 return;
6001
Paul Bakker48916f92012-09-16 19:57:18 +00006002#if defined(POLARSSL_DHM_C)
6003 dhm_free( &handshake->dhm_ctx );
6004#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006005#if defined(POLARSSL_ECDH_C)
6006 ecdh_free( &handshake->ecdh_ctx );
6007#endif
6008
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006009#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006010 /* explicit void pointer cast for buggy MS compiler */
6011 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006012#endif
6013
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006014#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6015 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6016 /*
6017 * Free only the linked list wrapper, not the keys themselves
6018 * since the belong to the SNI callback
6019 */
6020 if( handshake->sni_key_cert != NULL )
6021 {
6022 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6023
6024 while( cur != NULL )
6025 {
6026 next = cur->next;
6027 polarssl_free( cur );
6028 cur = next;
6029 }
6030 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006031#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006032
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006033#if defined(POLARSSL_SSL_PROTO_DTLS)
6034 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006035 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006036 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006037#endif
6038
Paul Bakker34617722014-06-13 17:20:13 +02006039 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006040}
6041
6042void ssl_session_free( ssl_session *session )
6043{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006044 if( session == NULL )
6045 return;
6046
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006047#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006048 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006049 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006050 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006051 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006052 }
Paul Bakkered27a042013-04-18 22:46:23 +02006053#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006054
Paul Bakkera503a632013-08-14 13:48:06 +02006055#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006056 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006057#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006058
Paul Bakker34617722014-06-13 17:20:13 +02006059 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006060}
6061
Paul Bakker5121ce52009-01-03 21:22:43 +00006062/*
6063 * Free an SSL context
6064 */
6065void ssl_free( ssl_context *ssl )
6066{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006067 if( ssl == NULL )
6068 return;
6069
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6071
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006072 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006074 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6075 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 }
6077
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006078 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006079 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006080 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6081 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006082 }
6083
Paul Bakker16770332013-10-11 09:59:44 +02006084#if defined(POLARSSL_ZLIB_SUPPORT)
6085 if( ssl->compress_buf != NULL )
6086 {
Paul Bakker34617722014-06-13 17:20:13 +02006087 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006088 polarssl_free( ssl->compress_buf );
6089 }
6090#endif
6091
Paul Bakker40e46942009-01-03 21:51:57 +00006092#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006093 mpi_free( &ssl->dhm_P );
6094 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006095#endif
6096
Paul Bakker48916f92012-09-16 19:57:18 +00006097 if( ssl->transform )
6098 {
6099 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006100 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006101 }
6102
6103 if( ssl->handshake )
6104 {
6105 ssl_handshake_free( ssl->handshake );
6106 ssl_transform_free( ssl->transform_negotiate );
6107 ssl_session_free( ssl->session_negotiate );
6108
Paul Bakker6e339b52013-07-03 13:37:05 +02006109 polarssl_free( ssl->handshake );
6110 polarssl_free( ssl->transform_negotiate );
6111 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006112 }
6113
Paul Bakkerc0463502013-02-14 11:19:38 +01006114 if( ssl->session )
6115 {
6116 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006117 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006118 }
6119
Paul Bakkera503a632013-08-14 13:48:06 +02006120#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006121 if( ssl->ticket_keys )
6122 {
6123 ssl_ticket_keys_free( ssl->ticket_keys );
6124 polarssl_free( ssl->ticket_keys );
6125 }
Paul Bakkera503a632013-08-14 13:48:06 +02006126#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006127
Paul Bakker0be444a2013-08-27 21:55:01 +02006128#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006129 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
Paul Bakker34617722014-06-13 17:20:13 +02006131 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006132 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 ssl->hostname_len = 0;
6134 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006136
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006137#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006138 if( ssl->psk != NULL )
6139 {
Paul Bakker34617722014-06-13 17:20:13 +02006140 polarssl_zeroize( ssl->psk, ssl->psk_len );
6141 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006142 polarssl_free( ssl->psk );
6143 polarssl_free( ssl->psk_identity );
6144 ssl->psk_len = 0;
6145 ssl->psk_identity_len = 0;
6146 }
6147#endif
6148
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006149#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006150 ssl_key_cert_free( ssl->key_cert );
6151#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006152
Paul Bakker05ef8352012-05-08 09:17:57 +00006153#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6154 if( ssl_hw_record_finish != NULL )
6155 {
6156 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6157 ssl_hw_record_finish( ssl );
6158 }
6159#endif
6160
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006161#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006162 polarssl_free( ssl->cli_id );
6163#endif
6164
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006166
Paul Bakker86f04f42013-02-14 11:20:09 +01006167 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006168 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006169}
6170
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006171#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006172/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006173 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006174 */
6175unsigned char ssl_sig_from_pk( pk_context *pk )
6176{
6177#if defined(POLARSSL_RSA_C)
6178 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6179 return( SSL_SIG_RSA );
6180#endif
6181#if defined(POLARSSL_ECDSA_C)
6182 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6183 return( SSL_SIG_ECDSA );
6184#endif
6185 return( SSL_SIG_ANON );
6186}
6187
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006188pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6189{
6190 switch( sig )
6191 {
6192#if defined(POLARSSL_RSA_C)
6193 case SSL_SIG_RSA:
6194 return( POLARSSL_PK_RSA );
6195#endif
6196#if defined(POLARSSL_ECDSA_C)
6197 case SSL_SIG_ECDSA:
6198 return( POLARSSL_PK_ECDSA );
6199#endif
6200 default:
6201 return( POLARSSL_PK_NONE );
6202 }
6203}
Paul Bakker9af723c2014-05-01 13:03:14 +02006204#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006205
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006206/*
6207 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6208 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006209md_type_t ssl_md_alg_from_hash( unsigned char hash )
6210{
6211 switch( hash )
6212 {
6213#if defined(POLARSSL_MD5_C)
6214 case SSL_HASH_MD5:
6215 return( POLARSSL_MD_MD5 );
6216#endif
6217#if defined(POLARSSL_SHA1_C)
6218 case SSL_HASH_SHA1:
6219 return( POLARSSL_MD_SHA1 );
6220#endif
6221#if defined(POLARSSL_SHA256_C)
6222 case SSL_HASH_SHA224:
6223 return( POLARSSL_MD_SHA224 );
6224 case SSL_HASH_SHA256:
6225 return( POLARSSL_MD_SHA256 );
6226#endif
6227#if defined(POLARSSL_SHA512_C)
6228 case SSL_HASH_SHA384:
6229 return( POLARSSL_MD_SHA384 );
6230 case SSL_HASH_SHA512:
6231 return( POLARSSL_MD_SHA512 );
6232#endif
6233 default:
6234 return( POLARSSL_MD_NONE );
6235 }
6236}
6237
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006238#if defined(POLARSSL_SSL_SET_CURVES)
6239/*
6240 * Check is a curve proposed by the peer is in our list.
6241 * Return 1 if we're willing to use it, 0 otherwise.
6242 */
6243int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6244{
6245 const ecp_group_id *gid;
6246
6247 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6248 if( *gid == grp_id )
6249 return( 1 );
6250
6251 return( 0 );
6252}
Paul Bakker9af723c2014-05-01 13:03:14 +02006253#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006254
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006255#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006256int ssl_check_cert_usage( const x509_crt *cert,
6257 const ssl_ciphersuite_t *ciphersuite,
6258 int cert_endpoint )
6259{
6260#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6261 int usage = 0;
6262#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006263#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6264 const char *ext_oid;
6265 size_t ext_len;
6266#endif
6267
6268#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6269 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6270 ((void) cert);
6271 ((void) cert_endpoint);
6272#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006273
6274#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6275 if( cert_endpoint == SSL_IS_SERVER )
6276 {
6277 /* Server part of the key exchange */
6278 switch( ciphersuite->key_exchange )
6279 {
6280 case POLARSSL_KEY_EXCHANGE_RSA:
6281 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6282 usage = KU_KEY_ENCIPHERMENT;
6283 break;
6284
6285 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6286 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6287 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6288 usage = KU_DIGITAL_SIGNATURE;
6289 break;
6290
6291 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6292 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6293 usage = KU_KEY_AGREEMENT;
6294 break;
6295
6296 /* Don't use default: we want warnings when adding new values */
6297 case POLARSSL_KEY_EXCHANGE_NONE:
6298 case POLARSSL_KEY_EXCHANGE_PSK:
6299 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6300 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6301 usage = 0;
6302 }
6303 }
6304 else
6305 {
6306 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6307 usage = KU_DIGITAL_SIGNATURE;
6308 }
6309
6310 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6311 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006312#else
6313 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006314#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6315
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006316#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6317 if( cert_endpoint == SSL_IS_SERVER )
6318 {
6319 ext_oid = OID_SERVER_AUTH;
6320 ext_len = OID_SIZE( OID_SERVER_AUTH );
6321 }
6322 else
6323 {
6324 ext_oid = OID_CLIENT_AUTH;
6325 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6326 }
6327
6328 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6329 return( -1 );
6330#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6331
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006332 return( 0 );
6333}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006334#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006335
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006336/*
6337 * Convert version numbers to/from wire format
6338 * and, for DTLS, to/from TLS equivalent.
6339 *
6340 * For TLS this is the identity.
6341 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6342 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6343 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6344 */
6345void ssl_write_version( int major, int minor, int transport,
6346 unsigned char ver[2] )
6347{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006348#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006349 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006350 {
6351 if( minor == SSL_MINOR_VERSION_2 )
6352 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6353
6354 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6355 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6356 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006357 else
6358#else
6359 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006360#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006361 {
6362 ver[0] = (unsigned char) major;
6363 ver[1] = (unsigned char) minor;
6364 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006365}
6366
6367void ssl_read_version( int *major, int *minor, int transport,
6368 const unsigned char ver[2] )
6369{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006370#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006371 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006372 {
6373 *major = 255 - ver[0] + 2;
6374 *minor = 255 - ver[1] + 1;
6375
6376 if( *minor == SSL_MINOR_VERSION_1 )
6377 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6378 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006379 else
6380#else
6381 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006382#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006383 {
6384 *major = ver[0];
6385 *minor = ver[1];
6386 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006387}
6388
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006389#endif /* POLARSSL_SSL_TLS_C */