blob: e72aa3ea29449509b192bd84bd61ea288a050d76 [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é-Gonnardfc572dd2014-10-09 17:56:57 +02002756 /* Retransmit only on last message from previous flight, to avoid
2757 * too many retransmissions.
2758 * Besides, No sane server ever retransmits HelloVerifyRequest */
2759 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002760 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002761 {
2762 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2763 "message_seq = %d, start_of_flight = %d",
2764 recv_msg_seq,
2765 ssl->handshake->in_flight_start_seq ) );
2766
2767 if( ( ret = ssl_resend( ssl ) ) != 0 )
2768 {
2769 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2770 return( ret );
2771 }
2772 }
2773 else
2774 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002775 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002776 "message_seq = %d, expected = %d",
2777 recv_msg_seq,
2778 ssl->handshake->in_msg_seq ) );
2779 }
2780
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002781 return( POLARSSL_ERR_NET_WANT_READ );
2782 }
2783 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002784
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002785 /* Reassemble if current message is fragmented or reassembly is
2786 * already in progress */
2787 if( ssl->in_msglen < ssl->in_hslen ||
2788 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2789 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2790 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002791 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002792 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2793
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002794 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2795 {
2796 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2797 return( ret );
2798 }
2799 }
2800 }
2801 else
2802#endif /* POLARSSL_SSL_PROTO_DTLS */
2803 /* With TLS we don't handle fragmentation (for now) */
2804 if( ssl->in_msglen < ssl->in_hslen )
2805 {
2806 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002807 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002808 }
2809
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002810 if( ssl->state != SSL_HANDSHAKE_OVER )
2811 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2812
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002813 /* Handshake message is complete, increment counter */
2814#if defined(POLARSSL_SSL_PROTO_DTLS)
2815 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2816 ssl->handshake != NULL )
2817 {
2818 ssl->handshake->in_msg_seq++;
2819 }
2820#endif
2821
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002822 return( 0 );
2823}
2824
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002825/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002826 * DTLS anti-replay: RFC 6347 4.1.2.6
2827 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002828 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2829 * Bit n is set iff record number in_window_top - n has been seen.
2830 *
2831 * Usually, in_window_top is the last record number seen and the lsb of
2832 * in_window is set. The only exception is the initial state (record number 0
2833 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002834 */
2835#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2836static void ssl_dtls_replay_reset( ssl_context *ssl )
2837{
2838 ssl->in_window_top = 0;
2839 ssl->in_window = 0;
2840}
2841
2842static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2843{
2844 return( ( (uint64_t) buf[0] << 40 ) |
2845 ( (uint64_t) buf[1] << 32 ) |
2846 ( (uint64_t) buf[2] << 24 ) |
2847 ( (uint64_t) buf[3] << 16 ) |
2848 ( (uint64_t) buf[4] << 8 ) |
2849 ( (uint64_t) buf[5] ) );
2850}
2851
2852/*
2853 * Return 0 if sequence number is acceptable, -1 otherwise
2854 */
2855int ssl_dtls_replay_check( ssl_context *ssl )
2856{
2857 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2858 uint64_t bit;
2859
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002860 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2861 return( 0 );
2862
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002863 if( rec_seqnum > ssl->in_window_top )
2864 return( 0 );
2865
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002866 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002867
2868 if( bit >= 64 )
2869 return( -1 );
2870
2871 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2872 return( -1 );
2873
2874 return( 0 );
2875}
2876
2877/*
2878 * Update replay window on new validated record
2879 */
2880void ssl_dtls_replay_update( ssl_context *ssl )
2881{
2882 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2883
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002884 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2885 return;
2886
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002887 if( rec_seqnum > ssl->in_window_top )
2888 {
2889 /* Update window_top and the contents of the window */
2890 uint64_t shift = rec_seqnum - ssl->in_window_top;
2891
2892 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002893 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002894 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002895 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002896 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002897 ssl->in_window |= 1;
2898 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002899
2900 ssl->in_window_top = rec_seqnum;
2901 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002902 else
2903 {
2904 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002905 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002906
2907 if( bit < 64 ) /* Always true, but be extra sure */
2908 ssl->in_window |= (uint64_t) 1 << bit;
2909 }
2910}
2911#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2912
2913/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002914 * ContentType type;
2915 * ProtocolVersion version;
2916 * uint16 epoch; // DTLS only
2917 * uint48 sequence_number; // DTLS only
2918 * uint16 length;
2919 */
2920static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002922 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002923 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002925 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2926
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002928 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002929 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
2931 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2932 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002933 ssl->in_msgtype,
2934 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002936 /* Check record type */
2937 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2938 ssl->in_msgtype != SSL_MSG_ALERT &&
2939 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2940 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2941 {
2942 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002943
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002944 if( ( ret = ssl_send_alert_message( ssl,
2945 SSL_ALERT_LEVEL_FATAL,
2946 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2947 {
2948 return( ret );
2949 }
2950
2951 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2952 }
2953
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002954#if defined(POLARSSL_SSL_PROTO_DTLS)
2955 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2956 {
2957 /* Drop unexpected ChangeCipherSpec messages */
2958 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2959 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2960 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2961 {
2962 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2963 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2964 }
2965
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002966 /* Drop unexpected ApplicationData records,
2967 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002968 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002969 ssl->state != SSL_HANDSHAKE_OVER &&
2970 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
2971 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002972 {
2973 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2974 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2975 }
2976 }
2977#endif
2978
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002979 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002980 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 {
2982 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002983 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 }
2985
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002986 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 {
2988 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002989 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
2991
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002992 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002993#if defined(POLARSSL_SSL_PROTO_DTLS)
2994 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2995 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002996 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002997
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002998 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002999 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003000 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003001 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003002 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003003 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003004 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003005
3006#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3007 if( ssl_dtls_replay_check( ssl ) != 0 )
3008 {
3009 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3010 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3011 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003012#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003013 }
3014#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003015
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003016 /* Check length against the size of our buffer */
3017 if( ssl->in_msglen > SSL_BUFFER_LEN
3018 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003019 {
3020 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3021 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3022 }
3023
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003024 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003025 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003027 if( ssl->in_msglen < 1 ||
3028 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003029 {
3030 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003031 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 }
3033 }
3034 else
3035 {
Paul Bakker48916f92012-09-16 19:57:18 +00003036 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 {
3038 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003039 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 }
3041
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003042#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003044 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 {
3046 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003047 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003049#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3051 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 /*
3053 * TLS encrypted messages can have up to 256 bytes of padding
3054 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003056 ssl->in_msglen > ssl->transform_in->minlen +
3057 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 {
3059 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003060 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003062#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 }
3064
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003065 return( 0 );
3066}
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003068/*
3069 * If applicable, decrypt (and decompress) record content
3070 */
3071static int ssl_prepare_record_content( ssl_context *ssl )
3072{
3073 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003074
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003076 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003077
Paul Bakker05ef8352012-05-08 09:17:57 +00003078#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003079 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003080 {
3081 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3082
3083 ret = ssl_hw_record_read( ssl );
3084 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3085 {
3086 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003087 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003088 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003089
3090 if( ret == 0 )
3091 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003092 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003093#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003094 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
3096 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3097 {
3098 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3099 return( ret );
3100 }
3101
3102 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3103 ssl->in_msg, ssl->in_msglen );
3104
3105 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3106 {
3107 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003108 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 }
3110 }
3111
Paul Bakker2770fbd2012-07-03 13:30:23 +00003112#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003113 if( ssl->transform_in != NULL &&
3114 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003115 {
3116 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3117 {
3118 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3119 return( ret );
3120 }
3121
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003122 // TODO: what's the purpose of these lines? is in_len used?
3123 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3124 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003125 }
3126#endif /* POLARSSL_ZLIB_SUPPORT */
3127
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003128#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003129 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3130 {
3131 ssl_dtls_replay_update( ssl );
3132 }
3133#endif
3134
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003135 return( 0 );
3136}
3137
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003138static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3139
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003140/*
3141 * Read a record.
3142 *
3143 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3144 * and continue reading until a valid record is found.
3145 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003146int ssl_read_record( ssl_context *ssl )
3147{
3148 int ret;
3149
3150 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3151
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003152 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003153 {
3154 /*
3155 * Get next Handshake message in the current record
3156 */
3157 ssl->in_msglen -= ssl->in_hslen;
3158
3159 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3160 ssl->in_msglen );
3161
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003162 SSL_DEBUG_BUF( 4, "remaining content in record",
3163 ssl->in_msg, ssl->in_msglen );
3164
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003165 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3166 return( ret );
3167
3168 return( 0 );
3169 }
3170
3171 ssl->in_hslen = 0;
3172
3173 /*
3174 * Read the record header and parse it
3175 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003176#if defined(POLARSSL_SSL_PROTO_DTLS)
3177read_record_header:
3178#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003179 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3180 {
3181 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3182 return( ret );
3183 }
3184
3185 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003186 {
3187#if defined(POLARSSL_SSL_PROTO_DTLS)
3188 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3189 {
3190 /* Ignore bad record and get next one; drop the whole datagram
3191 * since current header cannot be trusted to find the next record
3192 * in current datagram */
3193 ssl->next_record_offset = 0;
3194 ssl->in_left = 0;
3195
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003196 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003197 goto read_record_header;
3198 }
3199#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003200 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003201 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003202
3203 /*
3204 * Read and optionally decrypt the message contents
3205 */
3206 if( ( ret = ssl_fetch_input( ssl,
3207 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3208 {
3209 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3210 return( ret );
3211 }
3212
3213 /* Done reading this record, get ready for the next one */
3214#if defined(POLARSSL_SSL_PROTO_DTLS)
3215 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3216 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3217 else
3218#endif
3219 ssl->in_left = 0;
3220
3221 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003222 {
3223#if defined(POLARSSL_SSL_PROTO_DTLS)
3224 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3225 {
3226 /* Silently discard invalid records */
3227 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3228 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3229 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003230 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003231 goto read_record_header;
3232 }
3233
3234 return( ret );
3235 }
3236 else
3237#endif
3238 {
3239 /* Error out (and send alert) on invalid records */
3240#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3241 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3242 {
3243 ssl_send_alert_message( ssl,
3244 SSL_ALERT_LEVEL_FATAL,
3245 SSL_ALERT_MSG_BAD_RECORD_MAC );
3246 }
3247#endif
3248 return( ret );
3249 }
3250 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003251
3252 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003253 * When we sent the last flight of the handshake, we MUST respond to a
3254 * retransmit of the peer's previous flight with a retransmit. (In
3255 * practice, only the Finished message will make it, other messages
3256 * including CCS use the old transform so they're dropped as invalid.)
3257 *
3258 * If the record we received is not a handshake message, however, it
3259 * means the peer received our last flight so we can clean up
3260 * handshake info.
3261 *
3262 * This check needs to be done before prepare_handshake() due to an edge
3263 * case: if the client immediately requests renegotiation, this
3264 * finishes the current handshake first, avoiding the new ClientHello
3265 * being mistaken for an ancient message in the current handshake.
3266 */
3267#if defined(POLARSSL_SSL_PROTO_DTLS)
3268 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3269 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003270 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003271 {
3272 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3273 ssl->in_msg[0] == SSL_HS_FINISHED )
3274 {
3275 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3276
3277 if( ( ret = ssl_resend( ssl ) ) != 0 )
3278 {
3279 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3280 return( ret );
3281 }
3282
3283 return( POLARSSL_ERR_NET_WANT_READ );
3284 }
3285 else
3286 {
3287 ssl_handshake_wrapup_free_hs_transform( ssl );
3288 }
3289 }
3290#endif
3291
3292 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003293 * Handle particular types of records
3294 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3296 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003297 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3298 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 }
3300
3301 if( ssl->in_msgtype == SSL_MSG_ALERT )
3302 {
3303 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3304 ssl->in_msg[0], ssl->in_msg[1] ) );
3305
3306 /*
3307 * Ignore non-fatal alerts, except close_notify
3308 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003309 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003311 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3312 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003313 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 }
3315
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003316 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3317 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 {
3319 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003320 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 }
3322 }
3323
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3325
3326 return( 0 );
3327}
3328
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003329int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3330{
3331 int ret;
3332
3333 if( ( ret = ssl_send_alert_message( ssl,
3334 SSL_ALERT_LEVEL_FATAL,
3335 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3336 {
3337 return( ret );
3338 }
3339
3340 return( 0 );
3341}
3342
Paul Bakker0a925182012-04-16 06:46:41 +00003343int ssl_send_alert_message( ssl_context *ssl,
3344 unsigned char level,
3345 unsigned char message )
3346{
3347 int ret;
3348
3349 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3350
3351 ssl->out_msgtype = SSL_MSG_ALERT;
3352 ssl->out_msglen = 2;
3353 ssl->out_msg[0] = level;
3354 ssl->out_msg[1] = message;
3355
3356 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3357 {
3358 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3359 return( ret );
3360 }
3361
3362 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3363
3364 return( 0 );
3365}
3366
Paul Bakker5121ce52009-01-03 21:22:43 +00003367/*
3368 * Handshake functions
3369 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003370#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3371 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3372 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3373 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3374 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3375 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3376 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003377int ssl_write_certificate( ssl_context *ssl )
3378{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003379 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003380
3381 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3382
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003383 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003384 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3385 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003386 {
3387 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3388 ssl->state++;
3389 return( 0 );
3390 }
3391
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003392 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3393 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003394}
3395
3396int ssl_parse_certificate( ssl_context *ssl )
3397{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3399
3400 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3401
3402 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003403 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3404 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003405 {
3406 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3407 ssl->state++;
3408 return( 0 );
3409 }
3410
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003411 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3412 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003413}
3414#else
3415int ssl_write_certificate( ssl_context *ssl )
3416{
3417 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3418 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003419 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003420 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3421
3422 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3423
3424 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003425 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3426 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003427 {
3428 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3429 ssl->state++;
3430 return( 0 );
3431 }
3432
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 if( ssl->endpoint == SSL_IS_CLIENT )
3434 {
3435 if( ssl->client_auth == 0 )
3436 {
3437 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3438 ssl->state++;
3439 return( 0 );
3440 }
3441
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003442#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 /*
3444 * If using SSLv3 and got no cert, send an Alert message
3445 * (otherwise an empty Certificate message will be sent).
3446 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003447 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3449 {
3450 ssl->out_msglen = 2;
3451 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003452 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3453 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003454
3455 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3456 goto write_msg;
3457 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003458#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460 else /* SSL_IS_SERVER */
3461 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003462 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 {
3464 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003465 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 }
3467 }
3468
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003469 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003470
3471 /*
3472 * 0 . 0 handshake type
3473 * 1 . 3 handshake length
3474 * 4 . 6 length of all certs
3475 * 7 . 9 length of cert. 1
3476 * 10 . n-1 peer certificate
3477 * n . n+2 length of cert. 2
3478 * n+3 . ... upper level cert, etc.
3479 */
3480 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003481 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003482
Paul Bakker29087132010-03-21 21:03:34 +00003483 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 {
3485 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003486 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003487 {
3488 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3489 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003490 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 }
3492
3493 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3494 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3495 ssl->out_msg[i + 2] = (unsigned char)( n );
3496
3497 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3498 i += n; crt = crt->next;
3499 }
3500
3501 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3502 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3503 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3504
3505 ssl->out_msglen = i;
3506 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3507 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3508
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003509#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003510write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003512
3513 ssl->state++;
3514
3515 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3516 {
3517 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3518 return( ret );
3519 }
3520
3521 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3522
Paul Bakkered27a042013-04-18 22:46:23 +02003523 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003524}
3525
3526int ssl_parse_certificate( ssl_context *ssl )
3527{
Paul Bakkered27a042013-04-18 22:46:23 +02003528 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003529 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003530 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003531
3532 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3533
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003534 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003535 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3536 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003537 {
3538 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3539 ssl->state++;
3540 return( 0 );
3541 }
3542
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003544 ( ssl->authmode == SSL_VERIFY_NONE ||
3545 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003547 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3549 ssl->state++;
3550 return( 0 );
3551 }
3552
3553 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3554 {
3555 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3556 return( ret );
3557 }
3558
3559 ssl->state++;
3560
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003561#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 /*
3563 * Check if the client sent an empty certificate
3564 */
3565 if( ssl->endpoint == SSL_IS_SERVER &&
3566 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3567 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003568 if( ssl->in_msglen == 2 &&
3569 ssl->in_msgtype == SSL_MSG_ALERT &&
3570 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3571 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003572 {
3573 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3574
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003575 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3577 return( 0 );
3578 else
Paul Bakker40e46942009-01-03 21:51:57 +00003579 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 }
3581 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003582#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003583
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003584#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3585 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 if( ssl->endpoint == SSL_IS_SERVER &&
3587 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3588 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003589 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003590 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3591 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003592 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 {
3594 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3595
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003596 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003598 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 else
3600 return( 0 );
3601 }
3602 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003603#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3604 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003605
3606 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3607 {
3608 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003609 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 }
3611
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003612 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3613 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 {
3615 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003616 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 }
3618
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003619 i = ssl_hs_hdr_len( ssl );
3620
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 /*
3622 * Same message structure as in ssl_write_certificate()
3623 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003624 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003626 if( ssl->in_msg[i] != 0 ||
3627 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 {
3629 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003630 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 }
3632
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003633 /* In case we tried to reuse a session but it failed */
3634 if( ssl->session_negotiate->peer_cert != NULL )
3635 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003636 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003637 polarssl_free( ssl->session_negotiate->peer_cert );
3638 }
3639
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003640 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3641 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 {
3643 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003644 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003645 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 }
3647
Paul Bakkerb6b09562013-09-18 14:17:41 +02003648 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003650 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
3652 while( i < ssl->in_hslen )
3653 {
3654 if( ssl->in_msg[i] != 0 )
3655 {
3656 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003657 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 }
3659
3660 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3661 | (unsigned int) ssl->in_msg[i + 2];
3662 i += 3;
3663
3664 if( n < 128 || i + n > ssl->in_hslen )
3665 {
3666 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003667 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 }
3669
Paul Bakkerddf26b42013-09-18 13:46:23 +02003670 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3671 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003672 if( ret != 0 )
3673 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003674 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675 return( ret );
3676 }
3677
3678 i += n;
3679 }
3680
Paul Bakker48916f92012-09-16 19:57:18 +00003681 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003682
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003683 /*
3684 * On client, make sure the server cert doesn't change during renego to
3685 * avoid "triple handshake" attack: https://secure-resumption.com/
3686 */
3687 if( ssl->endpoint == SSL_IS_CLIENT &&
3688 ssl->renegotiation == SSL_RENEGOTIATION )
3689 {
3690 if( ssl->session->peer_cert == NULL )
3691 {
3692 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3693 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3694 }
3695
3696 if( ssl->session->peer_cert->raw.len !=
3697 ssl->session_negotiate->peer_cert->raw.len ||
3698 memcmp( ssl->session->peer_cert->raw.p,
3699 ssl->session_negotiate->peer_cert->raw.p,
3700 ssl->session->peer_cert->raw.len ) != 0 )
3701 {
3702 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3703 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3704 }
3705 }
3706
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 if( ssl->authmode != SSL_VERIFY_NONE )
3708 {
3709 if( ssl->ca_chain == NULL )
3710 {
3711 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003712 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713 }
3714
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003715 /*
3716 * Main check: verify certificate
3717 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003718 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3719 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3720 &ssl->session_negotiate->verify_result,
3721 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
3723 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003724 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003725 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003726 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003727
3728 /*
3729 * Secondary checks: always done, but change 'ret' only if it was 0
3730 */
3731
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003732#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003733 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003734 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003735
3736 /* If certificate uses an EC key, make sure the curve is OK */
3737 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3738 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3739 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003740 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003741 if( ret == 0 )
3742 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003743 }
3744 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003745#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003746
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003747 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3748 ciphersuite_info,
3749 ! ssl->endpoint ) != 0 )
3750 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003751 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003752 if( ret == 0 )
3753 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3754 }
3755
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3757 ret = 0;
3758 }
3759
3760 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3761
3762 return( ret );
3763}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003764#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3765 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3766 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3767 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3768 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3769 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3770 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003771
3772int ssl_write_change_cipher_spec( ssl_context *ssl )
3773{
3774 int ret;
3775
3776 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3777
3778 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3779 ssl->out_msglen = 1;
3780 ssl->out_msg[0] = 1;
3781
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 ssl->state++;
3783
3784 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3785 {
3786 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3787 return( ret );
3788 }
3789
3790 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3791
3792 return( 0 );
3793}
3794
3795int ssl_parse_change_cipher_spec( ssl_context *ssl )
3796{
3797 int ret;
3798
3799 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3800
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3802 {
3803 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3804 return( ret );
3805 }
3806
3807 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3808 {
3809 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003810 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003811 }
3812
3813 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3814 {
3815 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003816 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003817 }
3818
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003819 /*
3820 * Switch to our negotiated transform and session parameters for inbound
3821 * data.
3822 */
3823 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3824 ssl->transform_in = ssl->transform_negotiate;
3825 ssl->session_in = ssl->session_negotiate;
3826
3827#if defined(POLARSSL_SSL_PROTO_DTLS)
3828 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3829 {
3830#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3831 ssl_dtls_replay_reset( ssl );
3832#endif
3833
3834 /* Increment epoch */
3835 if( ++ssl->in_epoch == 0 )
3836 {
3837 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3838 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3839 }
3840 }
3841 else
3842#endif /* POLARSSL_SSL_PROTO_DTLS */
3843 memset( ssl->in_ctr, 0, 8 );
3844
3845 /*
3846 * Set the in_msg pointer to the correct location based on IV length
3847 */
3848 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3849 {
3850 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3851 ssl->transform_negotiate->fixed_ivlen;
3852 }
3853 else
3854 ssl->in_msg = ssl->in_iv;
3855
3856#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3857 if( ssl_hw_record_activate != NULL )
3858 {
3859 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3860 {
3861 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3862 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3863 }
3864 }
3865#endif
3866
Paul Bakker5121ce52009-01-03 21:22:43 +00003867 ssl->state++;
3868
3869 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3870
3871 return( 0 );
3872}
3873
Paul Bakker41c83d32013-03-20 14:39:14 +01003874void ssl_optimize_checksum( ssl_context *ssl,
3875 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003876{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003877 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003878
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003879#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3880 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003881 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003882 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003883 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003884#endif
3885#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3886#if defined(POLARSSL_SHA512_C)
3887 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3888 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3889 else
3890#endif
3891#if defined(POLARSSL_SHA256_C)
3892 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003893 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003894 else
3895#endif
3896#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003897 {
3898 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003899 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003900 }
Paul Bakker380da532012-04-18 16:10:25 +00003901}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003902
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003903void ssl_reset_checksum( ssl_context *ssl )
3904{
3905#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3906 defined(POLARSSL_SSL_PROTO_TLS1_1)
3907 md5_starts( &ssl->handshake->fin_md5 );
3908 sha1_starts( &ssl->handshake->fin_sha1 );
3909#endif
3910#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3911#if defined(POLARSSL_SHA256_C)
3912 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3913#endif
3914#if defined(POLARSSL_SHA512_C)
3915 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3916#endif
3917#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3918}
3919
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003920static void ssl_update_checksum_start( ssl_context *ssl,
3921 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003922{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003923#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3924 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003925 md5_update( &ssl->handshake->fin_md5 , buf, len );
3926 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003927#endif
3928#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3929#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003930 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003931#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003932#if defined(POLARSSL_SHA512_C)
3933 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003934#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003935#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003936}
3937
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003938#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3939 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003940static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3941 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003942{
Paul Bakker48916f92012-09-16 19:57:18 +00003943 md5_update( &ssl->handshake->fin_md5 , buf, len );
3944 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003945}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003946#endif
Paul Bakker380da532012-04-18 16:10:25 +00003947
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003948#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3949#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003950static void ssl_update_checksum_sha256( ssl_context *ssl,
3951 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003952{
Paul Bakker9e36f042013-06-30 14:34:05 +02003953 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003954}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003955#endif
Paul Bakker380da532012-04-18 16:10:25 +00003956
Paul Bakker9e36f042013-06-30 14:34:05 +02003957#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003958static void ssl_update_checksum_sha384( ssl_context *ssl,
3959 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003960{
Paul Bakker9e36f042013-06-30 14:34:05 +02003961 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003962}
Paul Bakker769075d2012-11-24 11:26:46 +01003963#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003964#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003965
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003966#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003967static void ssl_calc_finished_ssl(
3968 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003969{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003970 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003971 md5_context md5;
3972 sha1_context sha1;
3973
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 unsigned char padbuf[48];
3975 unsigned char md5sum[16];
3976 unsigned char sha1sum[20];
3977
Paul Bakker48916f92012-09-16 19:57:18 +00003978 ssl_session *session = ssl->session_negotiate;
3979 if( !session )
3980 session = ssl->session;
3981
Paul Bakker1ef83d62012-04-11 12:09:53 +00003982 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3983
Paul Bakker48916f92012-09-16 19:57:18 +00003984 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3985 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
3987 /*
3988 * SSLv3:
3989 * hash =
3990 * MD5( master + pad2 +
3991 * MD5( handshake + sender + master + pad1 ) )
3992 * + SHA1( master + pad2 +
3993 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 */
3995
Paul Bakker90995b52013-06-24 19:20:35 +02003996#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003997 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003999#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004000
Paul Bakker90995b52013-06-24 19:20:35 +02004001#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004003 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004004#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
Paul Bakker3c2122f2013-06-24 19:03:14 +02004006 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4007 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
Paul Bakker1ef83d62012-04-11 12:09:53 +00004009 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
Paul Bakker3c2122f2013-06-24 19:03:14 +02004011 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004012 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013 md5_update( &md5, padbuf, 48 );
4014 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakker3c2122f2013-06-24 19:03:14 +02004016 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004017 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018 sha1_update( &sha1, padbuf, 40 );
4019 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Paul Bakker1ef83d62012-04-11 12:09:53 +00004021 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004022
Paul Bakker1ef83d62012-04-11 12:09:53 +00004023 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004024 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004025 md5_update( &md5, padbuf, 48 );
4026 md5_update( &md5, md5sum, 16 );
4027 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
Paul Bakker1ef83d62012-04-11 12:09:53 +00004029 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004030 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004031 sha1_update( &sha1, padbuf , 40 );
4032 sha1_update( &sha1, sha1sum, 20 );
4033 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004034
Paul Bakker1ef83d62012-04-11 12:09:53 +00004035 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
Paul Bakker5b4af392014-06-26 12:09:34 +02004037 md5_free( &md5 );
4038 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004039
Paul Bakker34617722014-06-13 17:20:13 +02004040 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4041 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4042 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043
4044 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4045}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004046#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004048#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004049static void ssl_calc_finished_tls(
4050 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004051{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004052 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004053 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004054 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004056 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
Paul Bakker48916f92012-09-16 19:57:18 +00004058 ssl_session *session = ssl->session_negotiate;
4059 if( !session )
4060 session = ssl->session;
4061
Paul Bakker1ef83d62012-04-11 12:09:53 +00004062 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004063
Paul Bakker48916f92012-09-16 19:57:18 +00004064 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4065 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Paul Bakker1ef83d62012-04-11 12:09:53 +00004067 /*
4068 * TLSv1:
4069 * hash = PRF( master, finished_label,
4070 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4071 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004072
Paul Bakker90995b52013-06-24 19:20:35 +02004073#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004074 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4075 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004076#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004077
Paul Bakker90995b52013-06-24 19:20:35 +02004078#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004079 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4080 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004081#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004082
4083 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004084 ? "client finished"
4085 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004086
4087 md5_finish( &md5, padbuf );
4088 sha1_finish( &sha1, padbuf + 16 );
4089
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004090 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004091 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004092
4093 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4094
Paul Bakker5b4af392014-06-26 12:09:34 +02004095 md5_free( &md5 );
4096 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004097
Paul Bakker34617722014-06-13 17:20:13 +02004098 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004099
4100 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4101}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004102#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004103
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004104#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4105#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004106static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004107 ssl_context *ssl, unsigned char *buf, int from )
4108{
4109 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004110 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004111 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004112 unsigned char padbuf[32];
4113
Paul Bakker48916f92012-09-16 19:57:18 +00004114 ssl_session *session = ssl->session_negotiate;
4115 if( !session )
4116 session = ssl->session;
4117
Paul Bakker380da532012-04-18 16:10:25 +00004118 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004119
Paul Bakker9e36f042013-06-30 14:34:05 +02004120 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004121
4122 /*
4123 * TLSv1.2:
4124 * hash = PRF( master, finished_label,
4125 * Hash( handshake ) )[0.11]
4126 */
4127
Paul Bakker9e36f042013-06-30 14:34:05 +02004128#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004129 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004130 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004131#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004132
4133 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004134 ? "client finished"
4135 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136
Paul Bakker9e36f042013-06-30 14:34:05 +02004137 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004138
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004139 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004140 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004141
4142 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4143
Paul Bakker5b4af392014-06-26 12:09:34 +02004144 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004145
Paul Bakker34617722014-06-13 17:20:13 +02004146 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004147
4148 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4149}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004150#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004151
Paul Bakker9e36f042013-06-30 14:34:05 +02004152#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004153static void ssl_calc_finished_tls_sha384(
4154 ssl_context *ssl, unsigned char *buf, int from )
4155{
4156 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004157 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004158 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004159 unsigned char padbuf[48];
4160
Paul Bakker48916f92012-09-16 19:57:18 +00004161 ssl_session *session = ssl->session_negotiate;
4162 if( !session )
4163 session = ssl->session;
4164
Paul Bakker380da532012-04-18 16:10:25 +00004165 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004166
Paul Bakker9e36f042013-06-30 14:34:05 +02004167 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004168
4169 /*
4170 * TLSv1.2:
4171 * hash = PRF( master, finished_label,
4172 * Hash( handshake ) )[0.11]
4173 */
4174
Paul Bakker9e36f042013-06-30 14:34:05 +02004175#if !defined(POLARSSL_SHA512_ALT)
4176 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4177 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004178#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004179
4180 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004181 ? "client finished"
4182 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004183
Paul Bakker9e36f042013-06-30 14:34:05 +02004184 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004185
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004186 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004187 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004188
4189 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4190
Paul Bakker5b4af392014-06-26 12:09:34 +02004191 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004192
Paul Bakker34617722014-06-13 17:20:13 +02004193 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004194
4195 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4196}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004197#endif /* POLARSSL_SHA512_C */
4198#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004199
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004200static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004201{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004202 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004203
4204 /*
4205 * Free our handshake params
4206 */
4207 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004208 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004209 ssl->handshake = NULL;
4210
4211 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004212 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004213 */
4214 if( ssl->transform )
4215 {
4216 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004217 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004218 }
4219 ssl->transform = ssl->transform_negotiate;
4220 ssl->transform_negotiate = NULL;
4221
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004222 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4223}
4224
4225void ssl_handshake_wrapup( ssl_context *ssl )
4226{
4227 int resume = ssl->handshake->resume;
4228
4229 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4230
4231 if( ssl->renegotiation == SSL_RENEGOTIATION )
4232 {
4233 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4234 ssl->renego_records_seen = 0;
4235 }
4236
4237 /*
4238 * Free the previous session and switch in the current one
4239 */
Paul Bakker0a597072012-09-25 21:55:46 +00004240 if( ssl->session )
4241 {
4242 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004243 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004244 }
4245 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004246 ssl->session_negotiate = NULL;
4247
Paul Bakker0a597072012-09-25 21:55:46 +00004248 /*
4249 * Add cache entry
4250 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004251 if( ssl->f_set_cache != NULL &&
4252 ssl->session->length != 0 &&
4253 resume == 0 )
4254 {
Paul Bakker0a597072012-09-25 21:55:46 +00004255 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4256 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004257 }
Paul Bakker0a597072012-09-25 21:55:46 +00004258
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004259#if defined(POLARSSL_SSL_PROTO_DTLS)
4260 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4261 ssl->handshake->flight != NULL )
4262 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004263 /* Cancel handshake timer */
4264 ssl_set_timer( ssl, 0 );
4265
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004266 /* Keep last flight around in case we need to resend it:
4267 * we need the handshake and transform structures for that */
4268 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4269 }
4270 else
4271#endif
4272 ssl_handshake_wrapup_free_hs_transform( ssl );
4273
Paul Bakker48916f92012-09-16 19:57:18 +00004274 ssl->state++;
4275
4276 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4277}
4278
Paul Bakker1ef83d62012-04-11 12:09:53 +00004279int ssl_write_finished( ssl_context *ssl )
4280{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004281 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282
4283 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4284
Paul Bakker92be97b2013-01-02 17:30:03 +01004285 /*
4286 * Set the out_msg pointer to the correct location based on IV length
4287 */
4288 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4289 {
4290 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4291 ssl->transform_negotiate->fixed_ivlen;
4292 }
4293 else
4294 ssl->out_msg = ssl->out_iv;
4295
Paul Bakker48916f92012-09-16 19:57:18 +00004296 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004297
4298 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4300
Paul Bakker48916f92012-09-16 19:57:18 +00004301 ssl->verify_data_len = hash_len;
4302 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4303
Paul Bakker5121ce52009-01-03 21:22:43 +00004304 ssl->out_msglen = 4 + hash_len;
4305 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4306 ssl->out_msg[0] = SSL_HS_FINISHED;
4307
4308 /*
4309 * In case of session resuming, invert the client and server
4310 * ChangeCipherSpec messages order.
4311 */
Paul Bakker0a597072012-09-25 21:55:46 +00004312 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004313 {
4314 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004315 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004316 else
4317 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4318 }
4319 else
4320 ssl->state++;
4321
Paul Bakker48916f92012-09-16 19:57:18 +00004322 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004323 * Switch to our negotiated transform and session parameters for outbound
4324 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004325 */
4326 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004327
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004328#if defined(POLARSSL_SSL_PROTO_DTLS)
4329 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4330 {
4331 unsigned char i;
4332
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004333 /* Remember current epoch settings for resending */
4334 ssl->handshake->alt_transform_out = ssl->transform_out;
4335 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4336
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004337 /* Set sequence_number to zero */
4338 memset( ssl->out_ctr + 2, 0, 6 );
4339
4340 /* Increment epoch */
4341 for( i = 2; i > 0; i-- )
4342 if( ++ssl->out_ctr[i - 1] != 0 )
4343 break;
4344
4345 /* The loop goes to its end iff the counter is wrapping */
4346 if( i == 0 )
4347 {
4348 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4349 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4350 }
4351 }
4352 else
4353#endif /* POLARSSL_SSL_PROTO_DTLS */
4354 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004355
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004356 ssl->transform_out = ssl->transform_negotiate;
4357 ssl->session_out = ssl->session_negotiate;
4358
Paul Bakker07eb38b2012-12-19 14:42:06 +01004359#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004360 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004361 {
4362 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4363 {
4364 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4365 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4366 }
4367 }
4368#endif
4369
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004370#if defined(POLARSSL_SSL_PROTO_DTLS)
4371 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4372 ssl_send_flight_completed( ssl );
4373#endif
4374
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4376 {
4377 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4378 return( ret );
4379 }
4380
4381 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4382
4383 return( 0 );
4384}
4385
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004386#if defined(POLARSSL_SSL_PROTO_SSL3)
4387#define SSL_MAX_HASH_LEN 36
4388#else
4389#define SSL_MAX_HASH_LEN 12
4390#endif
4391
Paul Bakker5121ce52009-01-03 21:22:43 +00004392int ssl_parse_finished( ssl_context *ssl )
4393{
Paul Bakker23986e52011-04-24 08:57:21 +00004394 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004395 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004396 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004397
4398 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4399
Paul Bakker48916f92012-09-16 19:57:18 +00004400 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004401
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4403 {
4404 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4405 return( ret );
4406 }
4407
4408 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4409 {
4410 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004411 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004412 }
4413
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004414 /* There is currently no ciphersuite using another length with TLS 1.2 */
4415#if defined(POLARSSL_SSL_PROTO_SSL3)
4416 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4417 hash_len = 36;
4418 else
4419#endif
4420 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004421
4422 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004423 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 {
4425 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004426 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 }
4428
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004429 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4430 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 {
4432 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004433 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004434 }
4435
Paul Bakker48916f92012-09-16 19:57:18 +00004436 ssl->verify_data_len = hash_len;
4437 memcpy( ssl->peer_verify_data, buf, hash_len );
4438
Paul Bakker0a597072012-09-25 21:55:46 +00004439 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 {
4441 if( ssl->endpoint == SSL_IS_CLIENT )
4442 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4443
4444 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004445 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 }
4447 else
4448 ssl->state++;
4449
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004450#if defined(POLARSSL_SSL_PROTO_DTLS)
4451 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4452 ssl_recv_flight_completed( ssl );
4453#endif
4454
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4456
4457 return( 0 );
4458}
4459
Paul Bakker968afaa2014-07-09 11:09:24 +02004460static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004461{
4462 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4463
4464#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4465 defined(POLARSSL_SSL_PROTO_TLS1_1)
4466 md5_init( &handshake->fin_md5 );
4467 sha1_init( &handshake->fin_sha1 );
4468 md5_starts( &handshake->fin_md5 );
4469 sha1_starts( &handshake->fin_sha1 );
4470#endif
4471#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4472#if defined(POLARSSL_SHA256_C)
4473 sha256_init( &handshake->fin_sha256 );
4474 sha256_starts( &handshake->fin_sha256, 0 );
4475#endif
4476#if defined(POLARSSL_SHA512_C)
4477 sha512_init( &handshake->fin_sha512 );
4478 sha512_starts( &handshake->fin_sha512, 1 );
4479#endif
4480#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4481
4482 handshake->update_checksum = ssl_update_checksum_start;
4483 handshake->sig_alg = SSL_HASH_SHA1;
4484
4485#if defined(POLARSSL_DHM_C)
4486 dhm_init( &handshake->dhm_ctx );
4487#endif
4488#if defined(POLARSSL_ECDH_C)
4489 ecdh_init( &handshake->ecdh_ctx );
4490#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004491}
4492
4493static void ssl_transform_init( ssl_transform *transform )
4494{
4495 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004496
4497 cipher_init( &transform->cipher_ctx_enc );
4498 cipher_init( &transform->cipher_ctx_dec );
4499
4500 md_init( &transform->md_ctx_enc );
4501 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004502}
4503
4504void ssl_session_init( ssl_session *session )
4505{
4506 memset( session, 0, sizeof(ssl_session) );
4507}
4508
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004509static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004510{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004511 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004512 if( ssl->transform_negotiate )
4513 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004514 if( ssl->session_negotiate )
4515 ssl_session_free( ssl->session_negotiate );
4516 if( ssl->handshake )
4517 ssl_handshake_free( ssl->handshake );
4518
4519 /*
4520 * Either the pointers are now NULL or cleared properly and can be freed.
4521 * Now allocate missing structures.
4522 */
4523 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004524 {
4525 ssl->transform_negotiate =
4526 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4527 }
Paul Bakker48916f92012-09-16 19:57:18 +00004528
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004529 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004530 {
4531 ssl->session_negotiate =
4532 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4533 }
Paul Bakker48916f92012-09-16 19:57:18 +00004534
Paul Bakker82788fb2014-10-20 13:59:19 +02004535 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004536 {
4537 ssl->handshake = (ssl_handshake_params *)
4538 polarssl_malloc( sizeof(ssl_handshake_params) );
4539 }
Paul Bakker48916f92012-09-16 19:57:18 +00004540
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004541 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004542 if( ssl->handshake == NULL ||
4543 ssl->transform_negotiate == NULL ||
4544 ssl->session_negotiate == NULL )
4545 {
4546 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004547
4548 polarssl_free( ssl->handshake );
4549 polarssl_free( ssl->transform_negotiate );
4550 polarssl_free( ssl->session_negotiate );
4551
4552 ssl->handshake = NULL;
4553 ssl->transform_negotiate = NULL;
4554 ssl->session_negotiate = NULL;
4555
Paul Bakker48916f92012-09-16 19:57:18 +00004556 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4557 }
4558
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004559 /* Initialize structures */
4560 ssl_session_init( ssl->session_negotiate );
4561 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004562 ssl_handshake_params_init( ssl->handshake );
4563
4564#if defined(POLARSSL_X509_CRT_PARSE_C)
4565 ssl->handshake->key_cert = ssl->key_cert;
4566#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004567
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004568 /*
4569 * We may not know yet if we're using DTLS,
4570 * so always initiliase DTLS-specific fields.
4571 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004572#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004573 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004574
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004575 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004576 if( ssl->endpoint == SSL_IS_CLIENT )
4577 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4578 else
4579 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004580#endif
4581
Paul Bakker48916f92012-09-16 19:57:18 +00004582 return( 0 );
4583}
4584
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004585#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4586/* Dummy cookie callbacks for defaults */
4587static int ssl_cookie_write_dummy( void *ctx,
4588 unsigned char **p, unsigned char *end,
4589 const unsigned char *cli_id, size_t cli_id_len )
4590{
4591 ((void) ctx);
4592 ((void) p);
4593 ((void) end);
4594 ((void) cli_id);
4595 ((void) cli_id_len);
4596
4597 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4598}
4599
4600static int ssl_cookie_check_dummy( void *ctx,
4601 const unsigned char *cookie, size_t cookie_len,
4602 const unsigned char *cli_id, size_t cli_id_len )
4603{
4604 ((void) ctx);
4605 ((void) cookie);
4606 ((void) cookie_len);
4607 ((void) cli_id);
4608 ((void) cli_id_len);
4609
4610 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4611}
4612#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4613
Paul Bakker5121ce52009-01-03 21:22:43 +00004614/*
4615 * Initialize an SSL context
4616 */
4617int ssl_init( ssl_context *ssl )
4618{
Paul Bakker48916f92012-09-16 19:57:18 +00004619 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004620 int len = SSL_BUFFER_LEN;
4621
4622 memset( ssl, 0, sizeof( ssl_context ) );
4623
Paul Bakker62f2dee2012-09-28 07:31:51 +00004624 /*
4625 * Sane defaults
4626 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004627 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4628 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4629 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4630 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004631
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004632 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004633
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004634 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4635
Paul Bakker62f2dee2012-09-28 07:31:51 +00004636#if defined(POLARSSL_DHM_C)
4637 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4638 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4639 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4640 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4641 {
4642 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4643 return( ret );
4644 }
4645#endif
4646
4647 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004648 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004649 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004650 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004651 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004652
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004653 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 {
4655 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004656 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004657 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004658 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004659 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004660 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004661 }
4662
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004663 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4664 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004665
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004666 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4667 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4668
Paul Bakker606b4ba2013-08-14 16:52:14 +02004669#if defined(POLARSSL_SSL_SESSION_TICKETS)
4670 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4671#endif
4672
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004673#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004674 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004675#endif
4676
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004677#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4678 ssl->f_cookie_write = ssl_cookie_write_dummy;
4679 ssl->f_cookie_check = ssl_cookie_check_dummy;
4680#endif
4681
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004682#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4683 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4684#endif
4685
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004686#if defined(POLARSSL_SSL_PROTO_DTLS)
4687 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4688 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4689#endif
4690
Paul Bakker48916f92012-09-16 19:57:18 +00004691 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4692 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004693
4694 return( 0 );
4695}
4696
4697/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004698 * Reset an initialized and used SSL context for re-use while retaining
4699 * all application-set variables, function pointers and data.
4700 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004701int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004702{
Paul Bakker48916f92012-09-16 19:57:18 +00004703 int ret;
4704
Paul Bakker7eb013f2011-10-06 12:37:39 +00004705 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004706 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4707 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4708
4709 ssl->verify_data_len = 0;
4710 memset( ssl->own_verify_data, 0, 36 );
4711 memset( ssl->peer_verify_data, 0, 36 );
4712
Paul Bakker7eb013f2011-10-06 12:37:39 +00004713 ssl->in_offt = NULL;
4714
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004715 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004716 ssl->in_msgtype = 0;
4717 ssl->in_msglen = 0;
4718 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004719#if defined(POLARSSL_SSL_PROTO_DTLS)
4720 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004721 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004722#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004723#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4724 ssl_dtls_replay_reset( ssl );
4725#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004726
4727 ssl->in_hslen = 0;
4728 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004729 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004730
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004731 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004732 ssl->out_msgtype = 0;
4733 ssl->out_msglen = 0;
4734 ssl->out_left = 0;
4735
Paul Bakker48916f92012-09-16 19:57:18 +00004736 ssl->transform_in = NULL;
4737 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004738
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004739 ssl->renego_records_seen = 0;
4740
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004741 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4742 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004743
4744#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004745 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004746 {
4747 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004748 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004749 {
4750 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4751 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4752 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004753 }
4754#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004755
Paul Bakker48916f92012-09-16 19:57:18 +00004756 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004757 {
Paul Bakker48916f92012-09-16 19:57:18 +00004758 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004759 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004760 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004761 }
Paul Bakker48916f92012-09-16 19:57:18 +00004762
Paul Bakkerc0463502013-02-14 11:19:38 +01004763 if( ssl->session )
4764 {
4765 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004766 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004767 ssl->session = NULL;
4768 }
4769
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004770#if defined(POLARSSL_SSL_ALPN)
4771 ssl->alpn_chosen = NULL;
4772#endif
4773
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004774#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004775 polarssl_free( ssl->cli_id );
4776 ssl->cli_id = NULL;
4777 ssl->cli_id_len = 0;
4778#endif
4779
Paul Bakker48916f92012-09-16 19:57:18 +00004780 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4781 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004782
4783 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004784}
4785
Paul Bakkera503a632013-08-14 13:48:06 +02004786#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004787static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4788{
4789 aes_free( &tkeys->enc );
4790 aes_free( &tkeys->dec );
4791
4792 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4793}
4794
Paul Bakker7eb013f2011-10-06 12:37:39 +00004795/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004796 * Allocate and initialize ticket keys
4797 */
4798static int ssl_ticket_keys_init( ssl_context *ssl )
4799{
4800 int ret;
4801 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004802 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004803
4804 if( ssl->ticket_keys != NULL )
4805 return( 0 );
4806
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004807 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4808 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004809 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4810
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004811 aes_init( &tkeys->enc );
4812 aes_init( &tkeys->dec );
4813
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004814 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004815 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004816 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004817 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004818 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004819 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004820
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004821 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4822 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4823 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4824 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004825 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004826 polarssl_free( tkeys );
4827 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004828 }
4829
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004830 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004831 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004832 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004833 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004834 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004835 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004836
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004837 ssl->ticket_keys = tkeys;
4838
4839 return( 0 );
4840}
Paul Bakkera503a632013-08-14 13:48:06 +02004841#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004842
4843/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004844 * SSL set accessors
4845 */
4846void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4847{
4848 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004849
Paul Bakker606b4ba2013-08-14 16:52:14 +02004850#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004851 if( endpoint == SSL_IS_CLIENT )
4852 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004853#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004854}
4855
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004856int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004857{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004858#if defined(POLARSSL_SSL_PROTO_DTLS)
4859 if( transport == SSL_TRANSPORT_DATAGRAM )
4860 {
4861 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004862
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004863 ssl->out_hdr = ssl->out_buf;
4864 ssl->out_ctr = ssl->out_buf + 3;
4865 ssl->out_len = ssl->out_buf + 11;
4866 ssl->out_iv = ssl->out_buf + 13;
4867 ssl->out_msg = ssl->out_buf + 13;
4868
4869 ssl->in_hdr = ssl->in_buf;
4870 ssl->in_ctr = ssl->in_buf + 3;
4871 ssl->in_len = ssl->in_buf + 11;
4872 ssl->in_iv = ssl->in_buf + 13;
4873 ssl->in_msg = ssl->in_buf + 13;
4874
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004875 /* DTLS starts with TLS1.1 */
4876 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4877 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004878
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004879 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4880 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4881
4882 return( 0 );
4883 }
4884#endif
4885
4886 if( transport == SSL_TRANSPORT_STREAM )
4887 {
4888 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004889
4890 ssl->out_ctr = ssl->out_buf;
4891 ssl->out_hdr = ssl->out_buf + 8;
4892 ssl->out_len = ssl->out_buf + 11;
4893 ssl->out_iv = ssl->out_buf + 13;
4894 ssl->out_msg = ssl->out_buf + 13;
4895
4896 ssl->in_ctr = ssl->in_buf;
4897 ssl->in_hdr = ssl->in_buf + 8;
4898 ssl->in_len = ssl->in_buf + 11;
4899 ssl->in_iv = ssl->in_buf + 13;
4900 ssl->in_msg = ssl->in_buf + 13;
4901
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004902 return( 0 );
4903 }
4904
4905 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004906}
4907
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004908#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4909void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4910{
4911 ssl->anti_replay = mode;
4912}
4913#endif
4914
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004915#if defined(POLARSSL_SSL_PROTO_DTLS)
4916void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4917{
4918 ssl->hs_timeout_min = min;
4919 ssl->hs_timeout_max = max;
4920}
4921#endif
4922
Paul Bakker5121ce52009-01-03 21:22:43 +00004923void ssl_set_authmode( ssl_context *ssl, int authmode )
4924{
4925 ssl->authmode = authmode;
4926}
4927
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004928#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004929void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004930 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004931 void *p_vrfy )
4932{
4933 ssl->f_vrfy = f_vrfy;
4934 ssl->p_vrfy = p_vrfy;
4935}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004936#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004937
Paul Bakker5121ce52009-01-03 21:22:43 +00004938void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004939 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004940 void *p_rng )
4941{
4942 ssl->f_rng = f_rng;
4943 ssl->p_rng = p_rng;
4944}
4945
4946void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004947 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004948 void *p_dbg )
4949{
4950 ssl->f_dbg = f_dbg;
4951 ssl->p_dbg = p_dbg;
4952}
4953
4954void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004955 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004956 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004957{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004958 if( p_recv != p_send )
4959 {
4960 ssl->f_recv = NULL;
4961 ssl->f_send = NULL;
4962 ssl->p_bio = NULL;
4963 return;
4964 }
4965
Paul Bakker5121ce52009-01-03 21:22:43 +00004966 ssl->f_recv = f_recv;
4967 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004968 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004969}
4970
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004971void ssl_set_bio_timeout( ssl_context *ssl,
4972 void *p_bio,
4973 int (*f_send)(void *, const unsigned char *, size_t),
4974 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004975 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4976 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004977{
4978 ssl->p_bio = p_bio;
4979 ssl->f_send = f_send;
4980 ssl->f_recv = f_recv;
4981 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004982 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004983}
4984
Paul Bakker0a597072012-09-25 21:55:46 +00004985void ssl_set_session_cache( ssl_context *ssl,
4986 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4987 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004988{
Paul Bakker0a597072012-09-25 21:55:46 +00004989 ssl->f_get_cache = f_get_cache;
4990 ssl->p_get_cache = p_get_cache;
4991 ssl->f_set_cache = f_set_cache;
4992 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004993}
4994
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004995int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004996{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004997 int ret;
4998
4999 if( ssl == NULL ||
5000 session == NULL ||
5001 ssl->session_negotiate == NULL ||
5002 ssl->endpoint != SSL_IS_CLIENT )
5003 {
5004 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5005 }
5006
5007 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5008 return( ret );
5009
Paul Bakker0a597072012-09-25 21:55:46 +00005010 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005011
5012 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005013}
5014
Paul Bakkerb68cad62012-08-23 08:34:18 +00005015void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005016{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005017 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5018 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5019 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5020 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5021}
5022
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005023void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5024 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005025 int major, int minor )
5026{
5027 if( major != SSL_MAJOR_VERSION_3 )
5028 return;
5029
5030 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5031 return;
5032
5033 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005034}
5035
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005036#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005037/* Add a new (empty) key_cert entry an return a pointer to it */
5038static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5039{
5040 ssl_key_cert *key_cert, *last;
5041
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005042 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5043 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005044 return( NULL );
5045
5046 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5047
5048 /* Append the new key_cert to the (possibly empty) current list */
5049 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005050 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005051 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005052 if( ssl->handshake != NULL )
5053 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005054 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005055 else
5056 {
5057 last = ssl->key_cert;
5058 while( last->next != NULL )
5059 last = last->next;
5060 last->next = key_cert;
5061 }
5062
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005063 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005064}
5065
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005066void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005067 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005068{
5069 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005070 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 ssl->peer_cn = peer_cn;
5072}
5073
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005074int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005075 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005076{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005077 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5078
5079 if( key_cert == NULL )
5080 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5081
5082 key_cert->cert = own_cert;
5083 key_cert->key = pk_key;
5084
5085 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005086}
5087
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005088#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005089int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005090 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005091{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005092 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005093 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005094
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005095 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005096 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5097
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005098 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5099 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005101
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005102 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005103
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005104 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005105 if( ret != 0 )
5106 return( ret );
5107
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005108 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005109 return( ret );
5110
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005111 key_cert->cert = own_cert;
5112 key_cert->key_own_alloc = 1;
5113
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005114 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005115}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005116#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005117
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005118int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005119 void *rsa_key,
5120 rsa_decrypt_func rsa_decrypt,
5121 rsa_sign_func rsa_sign,
5122 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005123{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005124 int ret;
5125 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005126
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005127 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005128 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5129
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005130 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5131 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005132 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005133
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005134 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005135
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005136 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5137 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5138 return( ret );
5139
5140 key_cert->cert = own_cert;
5141 key_cert->key_own_alloc = 1;
5142
5143 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005144}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005145#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005146
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005147#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005148int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5149 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005150{
Paul Bakker6db455e2013-09-18 17:29:31 +02005151 if( psk == NULL || psk_identity == NULL )
5152 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5153
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005154 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005155 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5156
Paul Bakker6db455e2013-09-18 17:29:31 +02005157 if( ssl->psk != NULL )
5158 {
5159 polarssl_free( ssl->psk );
5160 polarssl_free( ssl->psk_identity );
5161 }
5162
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005163 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005164 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005165
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005166 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005167 ssl->psk_identity = (unsigned char *)
5168 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005169
5170 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5171 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5172
5173 memcpy( ssl->psk, psk, ssl->psk_len );
5174 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005175
5176 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005177}
5178
5179void ssl_set_psk_cb( ssl_context *ssl,
5180 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5181 size_t),
5182 void *p_psk )
5183{
5184 ssl->f_psk = f_psk;
5185 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005186}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005187#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005188
Paul Bakker48916f92012-09-16 19:57:18 +00005189#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005190int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005191{
5192 int ret;
5193
Paul Bakker48916f92012-09-16 19:57:18 +00005194 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005195 {
5196 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5197 return( ret );
5198 }
5199
Paul Bakker48916f92012-09-16 19:57:18 +00005200 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005201 {
5202 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5203 return( ret );
5204 }
5205
5206 return( 0 );
5207}
5208
Paul Bakker1b57b062011-01-06 15:48:19 +00005209int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5210{
5211 int ret;
5212
Paul Bakker66d5d072014-06-17 16:39:18 +02005213 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005214 {
5215 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5216 return( ret );
5217 }
5218
Paul Bakker66d5d072014-06-17 16:39:18 +02005219 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005220 {
5221 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5222 return( ret );
5223 }
5224
5225 return( 0 );
5226}
Paul Bakker48916f92012-09-16 19:57:18 +00005227#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005228
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005229#if defined(POLARSSL_SSL_SET_CURVES)
5230/*
5231 * Set the allowed elliptic curves
5232 */
5233void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5234{
5235 ssl->curve_list = curve_list;
5236}
5237#endif
5238
Paul Bakker0be444a2013-08-27 21:55:01 +02005239#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005240int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005241{
5242 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005243 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005244
5245 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005246
5247 if( ssl->hostname_len + 1 == 0 )
5248 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5249
Paul Bakker6e339b52013-07-03 13:37:05 +02005250 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Paul Bakkerb15b8512012-01-13 13:44:06 +00005252 if( ssl->hostname == NULL )
5253 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5254
Paul Bakker3c2122f2013-06-24 19:03:14 +02005255 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005256 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005257
Paul Bakker40ea7de2009-05-03 10:18:48 +00005258 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005259
5260 return( 0 );
5261}
5262
Paul Bakker5701cdc2012-09-27 21:49:42 +00005263void ssl_set_sni( ssl_context *ssl,
5264 int (*f_sni)(void *, ssl_context *,
5265 const unsigned char *, size_t),
5266 void *p_sni )
5267{
5268 ssl->f_sni = f_sni;
5269 ssl->p_sni = p_sni;
5270}
Paul Bakker0be444a2013-08-27 21:55:01 +02005271#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005272
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005273#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005274int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005275{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005276 size_t cur_len, tot_len;
5277 const char **p;
5278
5279 /*
5280 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5281 * truncated". Check lengths now rather than later.
5282 */
5283 tot_len = 0;
5284 for( p = protos; *p != NULL; p++ )
5285 {
5286 cur_len = strlen( *p );
5287 tot_len += cur_len;
5288
5289 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5290 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5291 }
5292
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005293 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005294
5295 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005296}
5297
5298const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5299{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005300 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005301}
5302#endif /* POLARSSL_SSL_ALPN */
5303
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005304static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005305{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005306 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005307 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005308 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005309 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005310 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005311
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005312#if defined(POLARSSL_SSL_PROTO_DTLS)
5313 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5314 minor < SSL_MINOR_VERSION_2 )
5315 {
5316 return( -1 );
5317 }
5318#else
5319 ((void) ssl);
5320#endif
5321
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005322 return( 0 );
5323}
5324
5325int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5326{
5327 if( ssl_check_version( ssl, major, minor ) != 0 )
5328 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5329
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005330 ssl->max_major_ver = major;
5331 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005332
5333 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005334}
5335
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005336int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005337{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005338 if( ssl_check_version( ssl, major, minor ) != 0 )
5339 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005340
5341 ssl->min_major_ver = major;
5342 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005343
5344 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005345}
5346
Paul Bakker05decb22013-08-15 13:33:48 +02005347#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005348int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5349{
Paul Bakker77e257e2013-12-16 15:29:52 +01005350 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005351 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005352 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005353 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005354 }
5355
5356 ssl->mfl_code = mfl_code;
5357
5358 return( 0 );
5359}
Paul Bakker05decb22013-08-15 13:33:48 +02005360#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005361
Paul Bakker1f2bc622013-08-15 13:45:55 +02005362#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005363int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005364{
5365 if( ssl->endpoint != SSL_IS_CLIENT )
5366 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5367
Paul Bakker8c1ede62013-07-19 14:14:37 +02005368 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005369
5370 return( 0 );
5371}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005372#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005373
Paul Bakker48916f92012-09-16 19:57:18 +00005374void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5375{
5376 ssl->disable_renegotiation = renegotiation;
5377}
5378
5379void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5380{
5381 ssl->allow_legacy_renegotiation = allow_legacy;
5382}
5383
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005384void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5385{
5386 ssl->renego_max_records = max_records;
5387}
5388
Paul Bakkera503a632013-08-14 13:48:06 +02005389#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005390int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5391{
5392 ssl->session_tickets = use_tickets;
5393
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005394 if( ssl->endpoint == SSL_IS_CLIENT )
5395 return( 0 );
5396
5397 if( ssl->f_rng == NULL )
5398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5399
5400 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005401}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005402
5403void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5404{
5405 ssl->ticket_lifetime = lifetime;
5406}
Paul Bakkera503a632013-08-14 13:48:06 +02005407#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005408
Paul Bakker5121ce52009-01-03 21:22:43 +00005409/*
5410 * SSL get accessors
5411 */
Paul Bakker23986e52011-04-24 08:57:21 +00005412size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005413{
5414 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5415}
5416
Paul Bakkerff60ee62010-03-16 21:09:09 +00005417int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005418{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005419 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005420}
5421
Paul Bakkere3166ce2011-01-27 17:40:50 +00005422const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005423{
Paul Bakker926c8e42013-03-06 10:23:34 +01005424 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005425 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005426
Paul Bakkere3166ce2011-01-27 17:40:50 +00005427 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005428}
5429
Paul Bakker43ca69c2011-01-15 17:35:19 +00005430const char *ssl_get_version( const ssl_context *ssl )
5431{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005432#if defined(POLARSSL_SSL_PROTO_DTLS)
5433 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5434 {
5435 switch( ssl->minor_ver )
5436 {
5437 case SSL_MINOR_VERSION_2:
5438 return( "DTLSv1.0" );
5439
5440 case SSL_MINOR_VERSION_3:
5441 return( "DTLSv1.2" );
5442
5443 default:
5444 return( "unknown (DTLS)" );
5445 }
5446 }
5447#endif
5448
Paul Bakker43ca69c2011-01-15 17:35:19 +00005449 switch( ssl->minor_ver )
5450 {
5451 case SSL_MINOR_VERSION_0:
5452 return( "SSLv3.0" );
5453
5454 case SSL_MINOR_VERSION_1:
5455 return( "TLSv1.0" );
5456
5457 case SSL_MINOR_VERSION_2:
5458 return( "TLSv1.1" );
5459
Paul Bakker1ef83d62012-04-11 12:09:53 +00005460 case SSL_MINOR_VERSION_3:
5461 return( "TLSv1.2" );
5462
Paul Bakker43ca69c2011-01-15 17:35:19 +00005463 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005464 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005465 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005466}
5467
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005468#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005469const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005470{
5471 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005472 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005473
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005474 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005475}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005476#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005477
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005478int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5479{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005480 if( ssl == NULL ||
5481 dst == NULL ||
5482 ssl->session == NULL ||
5483 ssl->endpoint != SSL_IS_CLIENT )
5484 {
5485 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5486 }
5487
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005488 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005489}
5490
Paul Bakker5121ce52009-01-03 21:22:43 +00005491/*
Paul Bakker1961b702013-01-25 14:49:24 +01005492 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005493 */
Paul Bakker1961b702013-01-25 14:49:24 +01005494int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005495{
Paul Bakker40e46942009-01-03 21:51:57 +00005496 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005497
Paul Bakker40e46942009-01-03 21:51:57 +00005498#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005500 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005501#endif
5502
Paul Bakker40e46942009-01-03 21:51:57 +00005503#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005505 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005506#endif
5507
Paul Bakker1961b702013-01-25 14:49:24 +01005508 return( ret );
5509}
5510
5511/*
5512 * Perform the SSL handshake
5513 */
5514int ssl_handshake( ssl_context *ssl )
5515{
5516 int ret = 0;
5517
5518 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5519
5520 while( ssl->state != SSL_HANDSHAKE_OVER )
5521 {
5522 ret = ssl_handshake_step( ssl );
5523
5524 if( ret != 0 )
5525 break;
5526 }
5527
Paul Bakker5121ce52009-01-03 21:22:43 +00005528 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5529
5530 return( ret );
5531}
5532
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005533#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005534/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005535 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005536 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005537static int ssl_write_hello_request( ssl_context *ssl )
5538{
5539 int ret;
5540
5541 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5542
5543 ssl->out_msglen = 4;
5544 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5545 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5546
5547 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5548 {
5549 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5550 return( ret );
5551 }
5552
5553 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5554
5555 return( 0 );
5556}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005557#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005558
5559/*
5560 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005561 * - any side: calling ssl_renegotiate(),
5562 * - client: receiving a HelloRequest during ssl_read(),
5563 * - server: receiving any handshake message on server during ssl_read() after
5564 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005565 * If the handshake doesn't complete due to waiting for I/O, it will continue
5566 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005567 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005568static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005569{
5570 int ret;
5571
5572 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5573
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005574 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5575 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005576
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005577 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5578 * the ServerHello will have message_seq = 1" */
5579#if defined(POLARSSL_SSL_PROTO_DTLS)
5580 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005581 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5582 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005583 if( ssl->endpoint == SSL_IS_SERVER )
5584 ssl->handshake->out_msg_seq = 1;
5585 else
5586 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005587 }
5588#endif
5589
Paul Bakker48916f92012-09-16 19:57:18 +00005590 ssl->state = SSL_HELLO_REQUEST;
5591 ssl->renegotiation = SSL_RENEGOTIATION;
5592
Paul Bakker48916f92012-09-16 19:57:18 +00005593 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5594 {
5595 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5596 return( ret );
5597 }
5598
5599 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5600
5601 return( 0 );
5602}
5603
5604/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005605 * Renegotiate current connection on client,
5606 * or request renegotiation on server
5607 */
5608int ssl_renegotiate( ssl_context *ssl )
5609{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005610 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005611
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005612#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005613 /* On server, just send the request */
5614 if( ssl->endpoint == SSL_IS_SERVER )
5615 {
5616 if( ssl->state != SSL_HANDSHAKE_OVER )
5617 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5618
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005619 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5620
5621 /* Did we already try/start sending HelloRequest? */
5622 if( ssl->out_left != 0 )
5623 return( ssl_flush_output( ssl ) );
5624
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005625 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005626 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005627#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005628
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005629#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005630 /*
5631 * On client, either start the renegotiation process or,
5632 * if already in progress, continue the handshake
5633 */
5634 if( ssl->renegotiation != SSL_RENEGOTIATION )
5635 {
5636 if( ssl->state != SSL_HANDSHAKE_OVER )
5637 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5638
5639 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5640 {
5641 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5642 return( ret );
5643 }
5644 }
5645 else
5646 {
5647 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5648 {
5649 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5650 return( ret );
5651 }
5652 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005653#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005654
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005655 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005656}
5657
5658/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 * Receive application data decrypted from the SSL layer
5660 */
Paul Bakker23986e52011-04-24 08:57:21 +00005661int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005662{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005663 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005664 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005665
5666 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5667
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005668#if defined(POLARSSL_SSL_PROTO_DTLS)
5669 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5670 ssl->handshake != NULL &&
5671 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5672 {
5673 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5674 return( ret );
5675
5676 if( ( ret = ssl_resend( ssl ) ) != 0 )
5677 return( ret );
5678 }
5679#endif
5680
Paul Bakker5121ce52009-01-03 21:22:43 +00005681 if( ssl->state != SSL_HANDSHAKE_OVER )
5682 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005683 ret = ssl_handshake( ssl );
5684 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5685 {
5686 record_read = 1;
5687 }
5688 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
5690 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5691 return( ret );
5692 }
5693 }
5694
5695 if( ssl->in_offt == NULL )
5696 {
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005697#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005698 /* Start timer if not already running */
5699 if( ssl->time_limit == 0 )
5700 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005701#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005702
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005703 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005704 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005705 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5706 {
5707 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5708 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005709
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005710 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5711 return( ret );
5712 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005713 }
5714
5715 if( ssl->in_msglen == 0 &&
5716 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5717 {
5718 /*
5719 * OpenSSL sends empty messages to randomize the IV
5720 */
5721 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5722 {
Paul Bakker831a7552011-05-18 13:32:51 +00005723 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5724 return( 0 );
5725
Paul Bakker5121ce52009-01-03 21:22:43 +00005726 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5727 return( ret );
5728 }
5729 }
5730
Paul Bakker48916f92012-09-16 19:57:18 +00005731 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5732 {
5733 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5734
5735 if( ssl->endpoint == SSL_IS_CLIENT &&
5736 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005737 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005738 {
5739 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005740
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
5746 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5747 }
5748
5749 if( ssl->endpoint == SSL_IS_SERVER &&
5750 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5751 {
5752 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5753
5754 /* With DTLS, drop the packet (probably from last handshake) */
5755#if defined(POLARSSL_SSL_PROTO_DTLS)
5756 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5757 return( POLARSSL_ERR_NET_WANT_READ );
5758#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005759 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5760 }
5761
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005762 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5763 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005764 ssl->allow_legacy_renegotiation ==
5765 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005766 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005767 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005768
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005769#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005770 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005771 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005772 /*
5773 * SSLv3 does not have a "no_renegotiation" alert
5774 */
5775 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5776 return( ret );
5777 }
5778 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005779#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005780#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5781 defined(POLARSSL_SSL_PROTO_TLS1_2)
5782 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005783 {
5784 if( ( ret = ssl_send_alert_message( ssl,
5785 SSL_ALERT_LEVEL_WARNING,
5786 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5787 {
5788 return( ret );
5789 }
Paul Bakker48916f92012-09-16 19:57:18 +00005790 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005791 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005792#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5793 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005794 {
5795 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005796 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005797 }
Paul Bakker48916f92012-09-16 19:57:18 +00005798 }
5799 else
5800 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005801 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005802#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005803 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5804 ssl->endpoint == SSL_IS_CLIENT )
5805 {
5806 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5807 }
5808#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005809 ret = ssl_start_renegotiation( ssl );
5810 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5811 {
5812 record_read = 1;
5813 }
5814 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005815 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005816 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005817 return( ret );
5818 }
Paul Bakker48916f92012-09-16 19:57:18 +00005819 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005820
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005821 /* If a non-handshake record was read during renego, fallthrough,
5822 * else tell the user they should call ssl_read() again */
5823 if( ! record_read )
5824 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005825 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005826 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5827 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005828 ssl->renego_records_seen++;
5829
5830 if( ssl->renego_max_records >= 0 &&
5831 ssl->renego_records_seen > ssl->renego_max_records )
5832 {
5833 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5834 "but not honored by client" ) );
5835 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5836 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005837 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005838
5839 /* Fatal and closure alerts handled by ssl_read_record() */
5840 if( ssl->in_msgtype == SSL_MSG_ALERT )
5841 {
5842 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5843 return( POLARSSL_ERR_NET_WANT_READ );
5844 }
5845
5846 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 {
5848 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005849 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005850 }
5851
5852 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005853
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005854#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005855 /* We're going to return something now, cancel timer,
5856 * except if handshake (renegotiation) is in progress */
5857 if( ssl->state == SSL_HANDSHAKE_OVER )
5858 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 }
5861
5862 n = ( len < ssl->in_msglen )
5863 ? len : ssl->in_msglen;
5864
5865 memcpy( buf, ssl->in_offt, n );
5866 ssl->in_msglen -= n;
5867
5868 if( ssl->in_msglen == 0 )
5869 /* all bytes consumed */
5870 ssl->in_offt = NULL;
5871 else
5872 /* more data available */
5873 ssl->in_offt += n;
5874
5875 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5876
Paul Bakker23986e52011-04-24 08:57:21 +00005877 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005878}
5879
5880/*
5881 * Send application data to be encrypted by the SSL layer
5882 */
Paul Bakker23986e52011-04-24 08:57:21 +00005883int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005884{
Paul Bakker23986e52011-04-24 08:57:21 +00005885 int ret;
5886 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005887 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005888
5889 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5890
5891 if( ssl->state != SSL_HANDSHAKE_OVER )
5892 {
5893 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5894 {
5895 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5896 return( ret );
5897 }
5898 }
5899
Paul Bakker05decb22013-08-15 13:33:48 +02005900#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005901 /*
5902 * Assume mfl_code is correct since it was checked when set
5903 */
5904 max_len = mfl_code_to_length[ssl->mfl_code];
5905
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005906 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005907 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005908 */
5909 if( ssl->session_out != NULL &&
5910 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5911 {
5912 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5913 }
Paul Bakker05decb22013-08-15 13:33:48 +02005914#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005915
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005916 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005917
Paul Bakker5121ce52009-01-03 21:22:43 +00005918 if( ssl->out_left != 0 )
5919 {
5920 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5921 {
5922 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5923 return( ret );
5924 }
5925 }
Paul Bakker887bd502011-06-08 13:10:54 +00005926 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005927 {
Paul Bakker887bd502011-06-08 13:10:54 +00005928 ssl->out_msglen = n;
5929 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5930 memcpy( ssl->out_msg, buf, n );
5931
5932 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5933 {
5934 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5935 return( ret );
5936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005937 }
5938
5939 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5940
Paul Bakker23986e52011-04-24 08:57:21 +00005941 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942}
5943
5944/*
5945 * Notify the peer that the connection is being closed
5946 */
5947int ssl_close_notify( ssl_context *ssl )
5948{
5949 int ret;
5950
5951 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5952
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005953 if( ssl->out_left != 0 )
5954 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005955
5956 if( ssl->state == SSL_HANDSHAKE_OVER )
5957 {
Paul Bakker48916f92012-09-16 19:57:18 +00005958 if( ( ret = ssl_send_alert_message( ssl,
5959 SSL_ALERT_LEVEL_WARNING,
5960 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005961 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005962 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005963 return( ret );
5964 }
5965 }
5966
5967 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5968
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005969 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005970}
5971
Paul Bakker48916f92012-09-16 19:57:18 +00005972void ssl_transform_free( ssl_transform *transform )
5973{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005974 if( transform == NULL )
5975 return;
5976
Paul Bakker48916f92012-09-16 19:57:18 +00005977#if defined(POLARSSL_ZLIB_SUPPORT)
5978 deflateEnd( &transform->ctx_deflate );
5979 inflateEnd( &transform->ctx_inflate );
5980#endif
5981
Paul Bakker84bbeb52014-07-01 14:53:22 +02005982 cipher_free( &transform->cipher_ctx_enc );
5983 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005984
Paul Bakker84bbeb52014-07-01 14:53:22 +02005985 md_free( &transform->md_ctx_enc );
5986 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005987
Paul Bakker34617722014-06-13 17:20:13 +02005988 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005989}
5990
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005991#if defined(POLARSSL_X509_CRT_PARSE_C)
5992static void ssl_key_cert_free( ssl_key_cert *key_cert )
5993{
5994 ssl_key_cert *cur = key_cert, *next;
5995
5996 while( cur != NULL )
5997 {
5998 next = cur->next;
5999
6000 if( cur->key_own_alloc )
6001 {
6002 pk_free( cur->key );
6003 polarssl_free( cur->key );
6004 }
6005 polarssl_free( cur );
6006
6007 cur = next;
6008 }
6009}
6010#endif /* POLARSSL_X509_CRT_PARSE_C */
6011
Paul Bakker48916f92012-09-16 19:57:18 +00006012void ssl_handshake_free( ssl_handshake_params *handshake )
6013{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006014 if( handshake == NULL )
6015 return;
6016
Paul Bakker48916f92012-09-16 19:57:18 +00006017#if defined(POLARSSL_DHM_C)
6018 dhm_free( &handshake->dhm_ctx );
6019#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006020#if defined(POLARSSL_ECDH_C)
6021 ecdh_free( &handshake->ecdh_ctx );
6022#endif
6023
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006024#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006025 /* explicit void pointer cast for buggy MS compiler */
6026 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006027#endif
6028
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006029#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6030 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6031 /*
6032 * Free only the linked list wrapper, not the keys themselves
6033 * since the belong to the SNI callback
6034 */
6035 if( handshake->sni_key_cert != NULL )
6036 {
6037 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6038
6039 while( cur != NULL )
6040 {
6041 next = cur->next;
6042 polarssl_free( cur );
6043 cur = next;
6044 }
6045 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006046#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006047
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006048#if defined(POLARSSL_SSL_PROTO_DTLS)
6049 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006050 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006051 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006052#endif
6053
Paul Bakker34617722014-06-13 17:20:13 +02006054 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006055}
6056
6057void ssl_session_free( ssl_session *session )
6058{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006059 if( session == NULL )
6060 return;
6061
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006062#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006063 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006064 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006065 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006066 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006067 }
Paul Bakkered27a042013-04-18 22:46:23 +02006068#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006069
Paul Bakkera503a632013-08-14 13:48:06 +02006070#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006071 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006072#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006073
Paul Bakker34617722014-06-13 17:20:13 +02006074 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006075}
6076
Paul Bakker5121ce52009-01-03 21:22:43 +00006077/*
6078 * Free an SSL context
6079 */
6080void ssl_free( ssl_context *ssl )
6081{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006082 if( ssl == NULL )
6083 return;
6084
Paul Bakker5121ce52009-01-03 21:22:43 +00006085 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6086
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006087 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006088 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006089 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6090 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006091 }
6092
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006093 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006095 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6096 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006097 }
6098
Paul Bakker16770332013-10-11 09:59:44 +02006099#if defined(POLARSSL_ZLIB_SUPPORT)
6100 if( ssl->compress_buf != NULL )
6101 {
Paul Bakker34617722014-06-13 17:20:13 +02006102 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006103 polarssl_free( ssl->compress_buf );
6104 }
6105#endif
6106
Paul Bakker40e46942009-01-03 21:51:57 +00006107#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006108 mpi_free( &ssl->dhm_P );
6109 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006110#endif
6111
Paul Bakker48916f92012-09-16 19:57:18 +00006112 if( ssl->transform )
6113 {
6114 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006115 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006116 }
6117
6118 if( ssl->handshake )
6119 {
6120 ssl_handshake_free( ssl->handshake );
6121 ssl_transform_free( ssl->transform_negotiate );
6122 ssl_session_free( ssl->session_negotiate );
6123
Paul Bakker6e339b52013-07-03 13:37:05 +02006124 polarssl_free( ssl->handshake );
6125 polarssl_free( ssl->transform_negotiate );
6126 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006127 }
6128
Paul Bakkerc0463502013-02-14 11:19:38 +01006129 if( ssl->session )
6130 {
6131 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006132 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006133 }
6134
Paul Bakkera503a632013-08-14 13:48:06 +02006135#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006136 if( ssl->ticket_keys )
6137 {
6138 ssl_ticket_keys_free( ssl->ticket_keys );
6139 polarssl_free( ssl->ticket_keys );
6140 }
Paul Bakkera503a632013-08-14 13:48:06 +02006141#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006142
Paul Bakker0be444a2013-08-27 21:55:01 +02006143#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006144 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 {
Paul Bakker34617722014-06-13 17:20:13 +02006146 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006147 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006148 ssl->hostname_len = 0;
6149 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006151
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006152#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006153 if( ssl->psk != NULL )
6154 {
Paul Bakker34617722014-06-13 17:20:13 +02006155 polarssl_zeroize( ssl->psk, ssl->psk_len );
6156 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006157 polarssl_free( ssl->psk );
6158 polarssl_free( ssl->psk_identity );
6159 ssl->psk_len = 0;
6160 ssl->psk_identity_len = 0;
6161 }
6162#endif
6163
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006164#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006165 ssl_key_cert_free( ssl->key_cert );
6166#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006167
Paul Bakker05ef8352012-05-08 09:17:57 +00006168#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6169 if( ssl_hw_record_finish != NULL )
6170 {
6171 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6172 ssl_hw_record_finish( ssl );
6173 }
6174#endif
6175
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006176#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006177 polarssl_free( ssl->cli_id );
6178#endif
6179
Paul Bakker5121ce52009-01-03 21:22:43 +00006180 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006181
Paul Bakker86f04f42013-02-14 11:20:09 +01006182 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006183 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006184}
6185
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006186#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006187/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006188 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006189 */
6190unsigned char ssl_sig_from_pk( pk_context *pk )
6191{
6192#if defined(POLARSSL_RSA_C)
6193 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6194 return( SSL_SIG_RSA );
6195#endif
6196#if defined(POLARSSL_ECDSA_C)
6197 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6198 return( SSL_SIG_ECDSA );
6199#endif
6200 return( SSL_SIG_ANON );
6201}
6202
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006203pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6204{
6205 switch( sig )
6206 {
6207#if defined(POLARSSL_RSA_C)
6208 case SSL_SIG_RSA:
6209 return( POLARSSL_PK_RSA );
6210#endif
6211#if defined(POLARSSL_ECDSA_C)
6212 case SSL_SIG_ECDSA:
6213 return( POLARSSL_PK_ECDSA );
6214#endif
6215 default:
6216 return( POLARSSL_PK_NONE );
6217 }
6218}
Paul Bakker9af723c2014-05-01 13:03:14 +02006219#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006220
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006221/*
6222 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6223 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006224md_type_t ssl_md_alg_from_hash( unsigned char hash )
6225{
6226 switch( hash )
6227 {
6228#if defined(POLARSSL_MD5_C)
6229 case SSL_HASH_MD5:
6230 return( POLARSSL_MD_MD5 );
6231#endif
6232#if defined(POLARSSL_SHA1_C)
6233 case SSL_HASH_SHA1:
6234 return( POLARSSL_MD_SHA1 );
6235#endif
6236#if defined(POLARSSL_SHA256_C)
6237 case SSL_HASH_SHA224:
6238 return( POLARSSL_MD_SHA224 );
6239 case SSL_HASH_SHA256:
6240 return( POLARSSL_MD_SHA256 );
6241#endif
6242#if defined(POLARSSL_SHA512_C)
6243 case SSL_HASH_SHA384:
6244 return( POLARSSL_MD_SHA384 );
6245 case SSL_HASH_SHA512:
6246 return( POLARSSL_MD_SHA512 );
6247#endif
6248 default:
6249 return( POLARSSL_MD_NONE );
6250 }
6251}
6252
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006253#if defined(POLARSSL_SSL_SET_CURVES)
6254/*
6255 * Check is a curve proposed by the peer is in our list.
6256 * Return 1 if we're willing to use it, 0 otherwise.
6257 */
6258int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6259{
6260 const ecp_group_id *gid;
6261
6262 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6263 if( *gid == grp_id )
6264 return( 1 );
6265
6266 return( 0 );
6267}
Paul Bakker9af723c2014-05-01 13:03:14 +02006268#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006269
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006270#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006271int ssl_check_cert_usage( const x509_crt *cert,
6272 const ssl_ciphersuite_t *ciphersuite,
6273 int cert_endpoint )
6274{
6275#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6276 int usage = 0;
6277#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006278#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6279 const char *ext_oid;
6280 size_t ext_len;
6281#endif
6282
6283#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6284 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6285 ((void) cert);
6286 ((void) cert_endpoint);
6287#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006288
6289#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6290 if( cert_endpoint == SSL_IS_SERVER )
6291 {
6292 /* Server part of the key exchange */
6293 switch( ciphersuite->key_exchange )
6294 {
6295 case POLARSSL_KEY_EXCHANGE_RSA:
6296 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6297 usage = KU_KEY_ENCIPHERMENT;
6298 break;
6299
6300 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6301 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6302 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6303 usage = KU_DIGITAL_SIGNATURE;
6304 break;
6305
6306 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6307 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6308 usage = KU_KEY_AGREEMENT;
6309 break;
6310
6311 /* Don't use default: we want warnings when adding new values */
6312 case POLARSSL_KEY_EXCHANGE_NONE:
6313 case POLARSSL_KEY_EXCHANGE_PSK:
6314 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6315 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6316 usage = 0;
6317 }
6318 }
6319 else
6320 {
6321 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6322 usage = KU_DIGITAL_SIGNATURE;
6323 }
6324
6325 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6326 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006327#else
6328 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006329#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6330
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006331#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6332 if( cert_endpoint == SSL_IS_SERVER )
6333 {
6334 ext_oid = OID_SERVER_AUTH;
6335 ext_len = OID_SIZE( OID_SERVER_AUTH );
6336 }
6337 else
6338 {
6339 ext_oid = OID_CLIENT_AUTH;
6340 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6341 }
6342
6343 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6344 return( -1 );
6345#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6346
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006347 return( 0 );
6348}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006349#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006350
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006351/*
6352 * Convert version numbers to/from wire format
6353 * and, for DTLS, to/from TLS equivalent.
6354 *
6355 * For TLS this is the identity.
6356 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6357 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6358 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6359 */
6360void ssl_write_version( int major, int minor, int transport,
6361 unsigned char ver[2] )
6362{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006363#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006364 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006365 {
6366 if( minor == SSL_MINOR_VERSION_2 )
6367 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6368
6369 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6370 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6371 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006372 else
6373#else
6374 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006375#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006376 {
6377 ver[0] = (unsigned char) major;
6378 ver[1] = (unsigned char) minor;
6379 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006380}
6381
6382void ssl_read_version( int *major, int *minor, int transport,
6383 const unsigned char ver[2] )
6384{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006385#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006386 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006387 {
6388 *major = 255 - ver[0] + 2;
6389 *minor = 255 - ver[1] + 1;
6390
6391 if( *minor == SSL_MINOR_VERSION_1 )
6392 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6393 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006394 else
6395#else
6396 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006397#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006398 {
6399 *major = ver[0];
6400 *minor = ver[1];
6401 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006402}
6403
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006404#endif /* POLARSSL_SSL_TLS_C */