blob: 7cb14424dbe05c297d73ba695be6d2a8c68016a8 [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é-Gonnard23cad332014-10-13 17:06:41 +02002691 if( frag_len + 12 < ssl->in_msglen )
2692 {
2693 /*
2694 * We'got more handshake messages in the same record.
2695 * This case is not handled now because no know implementation does
2696 * that and it's hard to test, so we prefer to fail cleanly for now.
2697 */
2698 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2699 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2700 }
2701
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002702 if( ssl->in_left > ssl->next_record_offset )
2703 {
2704 /*
2705 * We've got more data in the buffer after the current record,
2706 * that we don't want to overwrite. Move it before writing the
2707 * reassembled message, and adjust in_left and next_record_offset.
2708 */
2709 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2710 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2711 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2712
2713 /* First compute and check new lengths */
2714 ssl->next_record_offset = new_remain - ssl->in_hdr;
2715 ssl->in_left = ssl->next_record_offset + remain_len;
2716
2717 if( ssl->in_left > SSL_BUFFER_LEN -
2718 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2719 {
2720 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2721 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2722 }
2723
2724 memmove( new_remain, cur_remain, remain_len );
2725 }
2726
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002727 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2728
2729 polarssl_free( ssl->handshake->hs_msg );
2730 ssl->handshake->hs_msg = NULL;
2731
2732 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2733 ssl->in_msg, ssl->in_hslen );
2734
2735 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002736}
2737#endif /* POLARSSL_SSL_PROTO_DTLS */
2738
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002739static int ssl_prepare_handshake_record( ssl_context *ssl )
2740{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002741 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2742 {
2743 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2744 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002745 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002746 }
2747
2748 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2749 ( ssl->in_msg[1] << 16 ) |
2750 ( ssl->in_msg[2] << 8 ) |
2751 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002752
2753 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2754 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002755 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002756
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002757#if defined(POLARSSL_SSL_PROTO_DTLS)
2758 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002759 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002760 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002761 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002762
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002763 /* ssl->handshake is NULL when receiving ClientHello for renego */
2764 if( ssl->handshake != NULL &&
2765 recv_msg_seq != ssl->handshake->in_msg_seq )
2766 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002767 /* Retransmit only on last message from previous flight, to avoid
2768 * too many retransmissions.
2769 * Besides, No sane server ever retransmits HelloVerifyRequest */
2770 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002771 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002772 {
2773 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2774 "message_seq = %d, start_of_flight = %d",
2775 recv_msg_seq,
2776 ssl->handshake->in_flight_start_seq ) );
2777
2778 if( ( ret = ssl_resend( ssl ) ) != 0 )
2779 {
2780 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2781 return( ret );
2782 }
2783 }
2784 else
2785 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002786 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002787 "message_seq = %d, expected = %d",
2788 recv_msg_seq,
2789 ssl->handshake->in_msg_seq ) );
2790 }
2791
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002792 return( POLARSSL_ERR_NET_WANT_READ );
2793 }
2794 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002795
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002796 /* Reassemble if current message is fragmented or reassembly is
2797 * already in progress */
2798 if( ssl->in_msglen < ssl->in_hslen ||
2799 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2800 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2801 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002802 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002803 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2804
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002805 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2806 {
2807 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2808 return( ret );
2809 }
2810 }
2811 }
2812 else
2813#endif /* POLARSSL_SSL_PROTO_DTLS */
2814 /* With TLS we don't handle fragmentation (for now) */
2815 if( ssl->in_msglen < ssl->in_hslen )
2816 {
2817 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002818 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002819 }
2820
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002821 if( ssl->state != SSL_HANDSHAKE_OVER )
2822 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2823
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002824 /* Handshake message is complete, increment counter */
2825#if defined(POLARSSL_SSL_PROTO_DTLS)
2826 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2827 ssl->handshake != NULL )
2828 {
2829 ssl->handshake->in_msg_seq++;
2830 }
2831#endif
2832
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002833 return( 0 );
2834}
2835
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002836/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002837 * DTLS anti-replay: RFC 6347 4.1.2.6
2838 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002839 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2840 * Bit n is set iff record number in_window_top - n has been seen.
2841 *
2842 * Usually, in_window_top is the last record number seen and the lsb of
2843 * in_window is set. The only exception is the initial state (record number 0
2844 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002845 */
2846#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2847static void ssl_dtls_replay_reset( ssl_context *ssl )
2848{
2849 ssl->in_window_top = 0;
2850 ssl->in_window = 0;
2851}
2852
2853static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2854{
2855 return( ( (uint64_t) buf[0] << 40 ) |
2856 ( (uint64_t) buf[1] << 32 ) |
2857 ( (uint64_t) buf[2] << 24 ) |
2858 ( (uint64_t) buf[3] << 16 ) |
2859 ( (uint64_t) buf[4] << 8 ) |
2860 ( (uint64_t) buf[5] ) );
2861}
2862
2863/*
2864 * Return 0 if sequence number is acceptable, -1 otherwise
2865 */
2866int ssl_dtls_replay_check( ssl_context *ssl )
2867{
2868 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2869 uint64_t bit;
2870
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002871 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2872 return( 0 );
2873
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002874 if( rec_seqnum > ssl->in_window_top )
2875 return( 0 );
2876
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002877 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002878
2879 if( bit >= 64 )
2880 return( -1 );
2881
2882 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2883 return( -1 );
2884
2885 return( 0 );
2886}
2887
2888/*
2889 * Update replay window on new validated record
2890 */
2891void ssl_dtls_replay_update( ssl_context *ssl )
2892{
2893 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2894
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002895 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2896 return;
2897
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002898 if( rec_seqnum > ssl->in_window_top )
2899 {
2900 /* Update window_top and the contents of the window */
2901 uint64_t shift = rec_seqnum - ssl->in_window_top;
2902
2903 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002904 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002905 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002906 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002907 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002908 ssl->in_window |= 1;
2909 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002910
2911 ssl->in_window_top = rec_seqnum;
2912 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002913 else
2914 {
2915 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002916 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002917
2918 if( bit < 64 ) /* Always true, but be extra sure */
2919 ssl->in_window |= (uint64_t) 1 << bit;
2920 }
2921}
2922#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2923
2924/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002925 * ContentType type;
2926 * ProtocolVersion version;
2927 * uint16 epoch; // DTLS only
2928 * uint48 sequence_number; // DTLS only
2929 * uint16 length;
2930 */
2931static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002932{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002933 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002934 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002936 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2937
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002939 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002940 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
2942 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2943 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002944 ssl->in_msgtype,
2945 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002947 /* Check record type */
2948 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2949 ssl->in_msgtype != SSL_MSG_ALERT &&
2950 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2951 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2952 {
2953 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002954
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002955 if( ( ret = ssl_send_alert_message( ssl,
2956 SSL_ALERT_LEVEL_FATAL,
2957 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2958 {
2959 return( ret );
2960 }
2961
2962 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2963 }
2964
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002965#if defined(POLARSSL_SSL_PROTO_DTLS)
2966 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2967 {
2968 /* Drop unexpected ChangeCipherSpec messages */
2969 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2970 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2971 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2972 {
2973 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2974 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2975 }
2976
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002977 /* Drop unexpected ApplicationData records,
2978 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002979 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002980 ssl->state != SSL_HANDSHAKE_OVER &&
2981 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
2982 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002983 {
2984 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2985 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2986 }
2987 }
2988#endif
2989
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002990 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002991 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 {
2993 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002994 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 }
2996
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002997 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 {
2999 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003000 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 }
3002
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003003 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003004#if defined(POLARSSL_SSL_PROTO_DTLS)
3005 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3006 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003007 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003008
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003009 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003010 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003011 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003012 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003013 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003014 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003015 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003016
3017#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3018 if( ssl_dtls_replay_check( ssl ) != 0 )
3019 {
3020 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3021 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3022 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003023#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003024 }
3025#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003026
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003027 /* Check length against the size of our buffer */
3028 if( ssl->in_msglen > SSL_BUFFER_LEN
3029 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003030 {
3031 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3032 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3033 }
3034
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003035 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003036 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003038 if( ssl->in_msglen < 1 ||
3039 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003040 {
3041 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003042 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 }
3044 }
3045 else
3046 {
Paul Bakker48916f92012-09-16 19:57:18 +00003047 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 {
3049 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003050 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 }
3052
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003053#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003055 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 {
3057 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003058 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003060#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003061#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3062 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003063 /*
3064 * TLS encrypted messages can have up to 256 bytes of padding
3065 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003067 ssl->in_msglen > ssl->transform_in->minlen +
3068 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 {
3070 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003073#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 }
3075
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003076 return( 0 );
3077}
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003079/*
3080 * If applicable, decrypt (and decompress) record content
3081 */
3082static int ssl_prepare_record_content( ssl_context *ssl )
3083{
3084 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003085
Paul Bakker5121ce52009-01-03 21:22:43 +00003086 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003087 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003088
Paul Bakker05ef8352012-05-08 09:17:57 +00003089#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003090 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003091 {
3092 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3093
3094 ret = ssl_hw_record_read( ssl );
3095 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3096 {
3097 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003098 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003099 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003100
3101 if( ret == 0 )
3102 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003103 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003104#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003105 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003106 {
3107 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3108 {
3109 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3110 return( ret );
3111 }
3112
3113 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3114 ssl->in_msg, ssl->in_msglen );
3115
3116 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3117 {
3118 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003119 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003120 }
3121 }
3122
Paul Bakker2770fbd2012-07-03 13:30:23 +00003123#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003124 if( ssl->transform_in != NULL &&
3125 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003126 {
3127 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3128 {
3129 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3130 return( ret );
3131 }
3132
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003133 // TODO: what's the purpose of these lines? is in_len used?
3134 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3135 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003136 }
3137#endif /* POLARSSL_ZLIB_SUPPORT */
3138
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003139#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003140 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3141 {
3142 ssl_dtls_replay_update( ssl );
3143 }
3144#endif
3145
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003146 return( 0 );
3147}
3148
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003149static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3150
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003151/*
3152 * Read a record.
3153 *
3154 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3155 * and continue reading until a valid record is found.
3156 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003157int ssl_read_record( ssl_context *ssl )
3158{
3159 int ret;
3160
3161 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3162
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003163 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003164 {
3165 /*
3166 * Get next Handshake message in the current record
3167 */
3168 ssl->in_msglen -= ssl->in_hslen;
3169
3170 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3171 ssl->in_msglen );
3172
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003173 SSL_DEBUG_BUF( 4, "remaining content in record",
3174 ssl->in_msg, ssl->in_msglen );
3175
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003176 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3177 return( ret );
3178
3179 return( 0 );
3180 }
3181
3182 ssl->in_hslen = 0;
3183
3184 /*
3185 * Read the record header and parse it
3186 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003187#if defined(POLARSSL_SSL_PROTO_DTLS)
3188read_record_header:
3189#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003190 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3191 {
3192 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3193 return( ret );
3194 }
3195
3196 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003197 {
3198#if defined(POLARSSL_SSL_PROTO_DTLS)
3199 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3200 {
3201 /* Ignore bad record and get next one; drop the whole datagram
3202 * since current header cannot be trusted to find the next record
3203 * in current datagram */
3204 ssl->next_record_offset = 0;
3205 ssl->in_left = 0;
3206
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003207 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003208 goto read_record_header;
3209 }
3210#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003211 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003212 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003213
3214 /*
3215 * Read and optionally decrypt the message contents
3216 */
3217 if( ( ret = ssl_fetch_input( ssl,
3218 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3219 {
3220 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3221 return( ret );
3222 }
3223
3224 /* Done reading this record, get ready for the next one */
3225#if defined(POLARSSL_SSL_PROTO_DTLS)
3226 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3227 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3228 else
3229#endif
3230 ssl->in_left = 0;
3231
3232 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003233 {
3234#if defined(POLARSSL_SSL_PROTO_DTLS)
3235 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3236 {
3237 /* Silently discard invalid records */
3238 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3239 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3240 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003241#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3242 if( ssl->badmac_limit != 0 &&
3243 ++ssl->badmac_seen >= ssl->badmac_limit )
3244 {
3245 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3246 return( POLARSSL_ERR_SSL_INVALID_MAC );
3247 }
3248#endif
3249
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003250 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003251 goto read_record_header;
3252 }
3253
3254 return( ret );
3255 }
3256 else
3257#endif
3258 {
3259 /* Error out (and send alert) on invalid records */
3260#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3261 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3262 {
3263 ssl_send_alert_message( ssl,
3264 SSL_ALERT_LEVEL_FATAL,
3265 SSL_ALERT_MSG_BAD_RECORD_MAC );
3266 }
3267#endif
3268 return( ret );
3269 }
3270 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003271
3272 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003273 * When we sent the last flight of the handshake, we MUST respond to a
3274 * retransmit of the peer's previous flight with a retransmit. (In
3275 * practice, only the Finished message will make it, other messages
3276 * including CCS use the old transform so they're dropped as invalid.)
3277 *
3278 * If the record we received is not a handshake message, however, it
3279 * means the peer received our last flight so we can clean up
3280 * handshake info.
3281 *
3282 * This check needs to be done before prepare_handshake() due to an edge
3283 * case: if the client immediately requests renegotiation, this
3284 * finishes the current handshake first, avoiding the new ClientHello
3285 * being mistaken for an ancient message in the current handshake.
3286 */
3287#if defined(POLARSSL_SSL_PROTO_DTLS)
3288 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3289 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003290 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003291 {
3292 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3293 ssl->in_msg[0] == SSL_HS_FINISHED )
3294 {
3295 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3296
3297 if( ( ret = ssl_resend( ssl ) ) != 0 )
3298 {
3299 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3300 return( ret );
3301 }
3302
3303 return( POLARSSL_ERR_NET_WANT_READ );
3304 }
3305 else
3306 {
3307 ssl_handshake_wrapup_free_hs_transform( ssl );
3308 }
3309 }
3310#endif
3311
3312 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003313 * Handle particular types of records
3314 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3316 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003317 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3318 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 }
3320
3321 if( ssl->in_msgtype == SSL_MSG_ALERT )
3322 {
3323 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3324 ssl->in_msg[0], ssl->in_msg[1] ) );
3325
3326 /*
3327 * Ignore non-fatal alerts, except close_notify
3328 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003329 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003331 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3332 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003333 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 }
3335
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003336 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3337 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 {
3339 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003340 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 }
3342 }
3343
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3345
3346 return( 0 );
3347}
3348
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003349int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3350{
3351 int ret;
3352
3353 if( ( ret = ssl_send_alert_message( ssl,
3354 SSL_ALERT_LEVEL_FATAL,
3355 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3356 {
3357 return( ret );
3358 }
3359
3360 return( 0 );
3361}
3362
Paul Bakker0a925182012-04-16 06:46:41 +00003363int ssl_send_alert_message( ssl_context *ssl,
3364 unsigned char level,
3365 unsigned char message )
3366{
3367 int ret;
3368
3369 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3370
3371 ssl->out_msgtype = SSL_MSG_ALERT;
3372 ssl->out_msglen = 2;
3373 ssl->out_msg[0] = level;
3374 ssl->out_msg[1] = message;
3375
3376 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3377 {
3378 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3379 return( ret );
3380 }
3381
3382 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3383
3384 return( 0 );
3385}
3386
Paul Bakker5121ce52009-01-03 21:22:43 +00003387/*
3388 * Handshake functions
3389 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003390#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3391 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3392 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3393 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3394 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3395 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3396 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003397int ssl_write_certificate( ssl_context *ssl )
3398{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003399 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003400
3401 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3402
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003403 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003404 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3405 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003406 {
3407 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3408 ssl->state++;
3409 return( 0 );
3410 }
3411
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003412 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3413 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003414}
3415
3416int ssl_parse_certificate( ssl_context *ssl )
3417{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003418 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3419
3420 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3421
3422 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3424 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003425 {
3426 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3427 ssl->state++;
3428 return( 0 );
3429 }
3430
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003431 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3432 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003433}
3434#else
3435int ssl_write_certificate( ssl_context *ssl )
3436{
3437 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3438 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003439 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003440 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3441
3442 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3443
3444 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003445 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3446 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003447 {
3448 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3449 ssl->state++;
3450 return( 0 );
3451 }
3452
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 if( ssl->endpoint == SSL_IS_CLIENT )
3454 {
3455 if( ssl->client_auth == 0 )
3456 {
3457 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3458 ssl->state++;
3459 return( 0 );
3460 }
3461
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003462#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 /*
3464 * If using SSLv3 and got no cert, send an Alert message
3465 * (otherwise an empty Certificate message will be sent).
3466 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003467 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3469 {
3470 ssl->out_msglen = 2;
3471 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003472 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3473 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
3475 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3476 goto write_msg;
3477 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003478#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003479 }
3480 else /* SSL_IS_SERVER */
3481 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003482 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003483 {
3484 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003485 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003486 }
3487 }
3488
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003489 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490
3491 /*
3492 * 0 . 0 handshake type
3493 * 1 . 3 handshake length
3494 * 4 . 6 length of all certs
3495 * 7 . 9 length of cert. 1
3496 * 10 . n-1 peer certificate
3497 * n . n+2 length of cert. 2
3498 * n+3 . ... upper level cert, etc.
3499 */
3500 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003501 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
Paul Bakker29087132010-03-21 21:03:34 +00003503 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003504 {
3505 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003506 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 {
3508 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3509 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003510 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003511 }
3512
3513 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3514 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3515 ssl->out_msg[i + 2] = (unsigned char)( n );
3516
3517 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3518 i += n; crt = crt->next;
3519 }
3520
3521 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3522 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3523 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3524
3525 ssl->out_msglen = i;
3526 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3527 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3528
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003529#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003530write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003531#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
3533 ssl->state++;
3534
3535 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3536 {
3537 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3538 return( ret );
3539 }
3540
3541 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3542
Paul Bakkered27a042013-04-18 22:46:23 +02003543 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003544}
3545
3546int ssl_parse_certificate( ssl_context *ssl )
3547{
Paul Bakkered27a042013-04-18 22:46:23 +02003548 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003549 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003550 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003551
3552 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3553
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003554 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003555 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3556 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003557 {
3558 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3559 ssl->state++;
3560 return( 0 );
3561 }
3562
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003564 ( ssl->authmode == SSL_VERIFY_NONE ||
3565 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003566 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003567 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003568 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3569 ssl->state++;
3570 return( 0 );
3571 }
3572
3573 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3574 {
3575 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3576 return( ret );
3577 }
3578
3579 ssl->state++;
3580
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003581#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 /*
3583 * Check if the client sent an empty certificate
3584 */
3585 if( ssl->endpoint == SSL_IS_SERVER &&
3586 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3587 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003588 if( ssl->in_msglen == 2 &&
3589 ssl->in_msgtype == SSL_MSG_ALERT &&
3590 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3591 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 {
3593 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3594
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003595 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3597 return( 0 );
3598 else
Paul Bakker40e46942009-01-03 21:51:57 +00003599 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 }
3601 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003602#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003604#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3605 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 if( ssl->endpoint == SSL_IS_SERVER &&
3607 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3608 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003609 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3611 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003612 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 {
3614 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3615
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003616 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003617 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003618 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 else
3620 return( 0 );
3621 }
3622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003623#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3624 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003625
3626 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3627 {
3628 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003629 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 }
3631
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003632 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3633 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 {
3635 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003636 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 }
3638
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003639 i = ssl_hs_hdr_len( ssl );
3640
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 /*
3642 * Same message structure as in ssl_write_certificate()
3643 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003644 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003646 if( ssl->in_msg[i] != 0 ||
3647 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 {
3649 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003650 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003651 }
3652
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003653 /* In case we tried to reuse a session but it failed */
3654 if( ssl->session_negotiate->peer_cert != NULL )
3655 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003656 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003657 polarssl_free( ssl->session_negotiate->peer_cert );
3658 }
3659
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003660 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3661 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 {
3663 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003664 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003665 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 }
3667
Paul Bakkerb6b09562013-09-18 14:17:41 +02003668 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003669
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003670 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
3672 while( i < ssl->in_hslen )
3673 {
3674 if( ssl->in_msg[i] != 0 )
3675 {
3676 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003677 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 }
3679
3680 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3681 | (unsigned int) ssl->in_msg[i + 2];
3682 i += 3;
3683
3684 if( n < 128 || i + n > ssl->in_hslen )
3685 {
3686 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003687 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 }
3689
Paul Bakkerddf26b42013-09-18 13:46:23 +02003690 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3691 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 if( ret != 0 )
3693 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003694 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 return( ret );
3696 }
3697
3698 i += n;
3699 }
3700
Paul Bakker48916f92012-09-16 19:57:18 +00003701 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003702
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003703 /*
3704 * On client, make sure the server cert doesn't change during renego to
3705 * avoid "triple handshake" attack: https://secure-resumption.com/
3706 */
3707 if( ssl->endpoint == SSL_IS_CLIENT &&
3708 ssl->renegotiation == SSL_RENEGOTIATION )
3709 {
3710 if( ssl->session->peer_cert == NULL )
3711 {
3712 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3713 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3714 }
3715
3716 if( ssl->session->peer_cert->raw.len !=
3717 ssl->session_negotiate->peer_cert->raw.len ||
3718 memcmp( ssl->session->peer_cert->raw.p,
3719 ssl->session_negotiate->peer_cert->raw.p,
3720 ssl->session->peer_cert->raw.len ) != 0 )
3721 {
3722 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3723 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3724 }
3725 }
3726
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 if( ssl->authmode != SSL_VERIFY_NONE )
3728 {
3729 if( ssl->ca_chain == NULL )
3730 {
3731 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003732 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 }
3734
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003735 /*
3736 * Main check: verify certificate
3737 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003738 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3739 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3740 &ssl->session_negotiate->verify_result,
3741 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
3743 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003744 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003746 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003747
3748 /*
3749 * Secondary checks: always done, but change 'ret' only if it was 0
3750 */
3751
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003752#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003753 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003754 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003755
3756 /* If certificate uses an EC key, make sure the curve is OK */
3757 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3758 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3759 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003760 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003761 if( ret == 0 )
3762 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003763 }
3764 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003765#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003766
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003767 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3768 ciphersuite_info,
3769 ! ssl->endpoint ) != 0 )
3770 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003771 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003772 if( ret == 0 )
3773 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3774 }
3775
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3777 ret = 0;
3778 }
3779
3780 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3781
3782 return( ret );
3783}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003784#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3785 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3786 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3787 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3788 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3789 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3790 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003791
3792int ssl_write_change_cipher_spec( ssl_context *ssl )
3793{
3794 int ret;
3795
3796 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3797
3798 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3799 ssl->out_msglen = 1;
3800 ssl->out_msg[0] = 1;
3801
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 ssl->state++;
3803
3804 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3805 {
3806 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3807 return( ret );
3808 }
3809
3810 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3811
3812 return( 0 );
3813}
3814
3815int ssl_parse_change_cipher_spec( ssl_context *ssl )
3816{
3817 int ret;
3818
3819 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3820
Paul Bakker5121ce52009-01-03 21:22:43 +00003821 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3822 {
3823 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3824 return( ret );
3825 }
3826
3827 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3828 {
3829 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003830 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003831 }
3832
3833 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3834 {
3835 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003836 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003837 }
3838
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003839 /*
3840 * Switch to our negotiated transform and session parameters for inbound
3841 * data.
3842 */
3843 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3844 ssl->transform_in = ssl->transform_negotiate;
3845 ssl->session_in = ssl->session_negotiate;
3846
3847#if defined(POLARSSL_SSL_PROTO_DTLS)
3848 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3849 {
3850#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3851 ssl_dtls_replay_reset( ssl );
3852#endif
3853
3854 /* Increment epoch */
3855 if( ++ssl->in_epoch == 0 )
3856 {
3857 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3858 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3859 }
3860 }
3861 else
3862#endif /* POLARSSL_SSL_PROTO_DTLS */
3863 memset( ssl->in_ctr, 0, 8 );
3864
3865 /*
3866 * Set the in_msg pointer to the correct location based on IV length
3867 */
3868 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3869 {
3870 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3871 ssl->transform_negotiate->fixed_ivlen;
3872 }
3873 else
3874 ssl->in_msg = ssl->in_iv;
3875
3876#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3877 if( ssl_hw_record_activate != NULL )
3878 {
3879 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3880 {
3881 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3882 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3883 }
3884 }
3885#endif
3886
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 ssl->state++;
3888
3889 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3890
3891 return( 0 );
3892}
3893
Paul Bakker41c83d32013-03-20 14:39:14 +01003894void ssl_optimize_checksum( ssl_context *ssl,
3895 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003896{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003897 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003898
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003899#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3900 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003901 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003902 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003903 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003904#endif
3905#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3906#if defined(POLARSSL_SHA512_C)
3907 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3908 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3909 else
3910#endif
3911#if defined(POLARSSL_SHA256_C)
3912 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003913 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003914 else
3915#endif
3916#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003917 {
3918 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003919 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003920 }
Paul Bakker380da532012-04-18 16:10:25 +00003921}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003922
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003923void ssl_reset_checksum( ssl_context *ssl )
3924{
3925#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3926 defined(POLARSSL_SSL_PROTO_TLS1_1)
3927 md5_starts( &ssl->handshake->fin_md5 );
3928 sha1_starts( &ssl->handshake->fin_sha1 );
3929#endif
3930#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3931#if defined(POLARSSL_SHA256_C)
3932 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3933#endif
3934#if defined(POLARSSL_SHA512_C)
3935 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3936#endif
3937#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3938}
3939
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003940static void ssl_update_checksum_start( ssl_context *ssl,
3941 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003942{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003943#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3944 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003945 md5_update( &ssl->handshake->fin_md5 , buf, len );
3946 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003947#endif
3948#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3949#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003950 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003951#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003952#if defined(POLARSSL_SHA512_C)
3953 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003954#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003955#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003956}
3957
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003958#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3959 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003960static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3961 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003962{
Paul Bakker48916f92012-09-16 19:57:18 +00003963 md5_update( &ssl->handshake->fin_md5 , buf, len );
3964 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003965}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003966#endif
Paul Bakker380da532012-04-18 16:10:25 +00003967
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003968#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3969#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003970static void ssl_update_checksum_sha256( ssl_context *ssl,
3971 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003972{
Paul Bakker9e36f042013-06-30 14:34:05 +02003973 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003974}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003975#endif
Paul Bakker380da532012-04-18 16:10:25 +00003976
Paul Bakker9e36f042013-06-30 14:34:05 +02003977#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003978static void ssl_update_checksum_sha384( ssl_context *ssl,
3979 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003980{
Paul Bakker9e36f042013-06-30 14:34:05 +02003981 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003982}
Paul Bakker769075d2012-11-24 11:26:46 +01003983#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003984#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003985
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003986#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003987static void ssl_calc_finished_ssl(
3988 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003989{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003990 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003991 md5_context md5;
3992 sha1_context sha1;
3993
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 unsigned char padbuf[48];
3995 unsigned char md5sum[16];
3996 unsigned char sha1sum[20];
3997
Paul Bakker48916f92012-09-16 19:57:18 +00003998 ssl_session *session = ssl->session_negotiate;
3999 if( !session )
4000 session = ssl->session;
4001
Paul Bakker1ef83d62012-04-11 12:09:53 +00004002 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4003
Paul Bakker48916f92012-09-16 19:57:18 +00004004 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4005 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
4007 /*
4008 * SSLv3:
4009 * hash =
4010 * MD5( master + pad2 +
4011 * MD5( handshake + sender + master + pad1 ) )
4012 * + SHA1( master + pad2 +
4013 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004014 */
4015
Paul Bakker90995b52013-06-24 19:20:35 +02004016#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004019#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Paul Bakker90995b52013-06-24 19:20:35 +02004021#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004022 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004023 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004025
Paul Bakker3c2122f2013-06-24 19:03:14 +02004026 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4027 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
Paul Bakker1ef83d62012-04-11 12:09:53 +00004029 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004030
Paul Bakker3c2122f2013-06-24 19:03:14 +02004031 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004032 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004033 md5_update( &md5, padbuf, 48 );
4034 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035
Paul Bakker3c2122f2013-06-24 19:03:14 +02004036 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004037 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004038 sha1_update( &sha1, padbuf, 40 );
4039 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Paul Bakker1ef83d62012-04-11 12:09:53 +00004041 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Paul Bakker1ef83d62012-04-11 12:09:53 +00004043 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004044 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004045 md5_update( &md5, padbuf, 48 );
4046 md5_update( &md5, md5sum, 16 );
4047 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004048
Paul Bakker1ef83d62012-04-11 12:09:53 +00004049 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004050 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004051 sha1_update( &sha1, padbuf , 40 );
4052 sha1_update( &sha1, sha1sum, 20 );
4053 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054
Paul Bakker1ef83d62012-04-11 12:09:53 +00004055 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Paul Bakker5b4af392014-06-26 12:09:34 +02004057 md5_free( &md5 );
4058 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Paul Bakker34617722014-06-13 17:20:13 +02004060 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4061 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4062 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004063
4064 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4065}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004066#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004067
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004068#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004069static void ssl_calc_finished_tls(
4070 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004071{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004072 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004073 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004074 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004075 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004076 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004077
Paul Bakker48916f92012-09-16 19:57:18 +00004078 ssl_session *session = ssl->session_negotiate;
4079 if( !session )
4080 session = ssl->session;
4081
Paul Bakker1ef83d62012-04-11 12:09:53 +00004082 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004083
Paul Bakker48916f92012-09-16 19:57:18 +00004084 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4085 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004086
Paul Bakker1ef83d62012-04-11 12:09:53 +00004087 /*
4088 * TLSv1:
4089 * hash = PRF( master, finished_label,
4090 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4091 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004092
Paul Bakker90995b52013-06-24 19:20:35 +02004093#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004094 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4095 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004096#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004097
Paul Bakker90995b52013-06-24 19:20:35 +02004098#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004099 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4100 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004101#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004102
4103 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004104 ? "client finished"
4105 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004106
4107 md5_finish( &md5, padbuf );
4108 sha1_finish( &sha1, padbuf + 16 );
4109
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004110 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004111 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004112
4113 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4114
Paul Bakker5b4af392014-06-26 12:09:34 +02004115 md5_free( &md5 );
4116 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004117
Paul Bakker34617722014-06-13 17:20:13 +02004118 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004119
4120 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4121}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004122#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004123
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004124#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4125#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004126static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004127 ssl_context *ssl, unsigned char *buf, int from )
4128{
4129 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004130 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004131 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004132 unsigned char padbuf[32];
4133
Paul Bakker48916f92012-09-16 19:57:18 +00004134 ssl_session *session = ssl->session_negotiate;
4135 if( !session )
4136 session = ssl->session;
4137
Paul Bakker380da532012-04-18 16:10:25 +00004138 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004139
Paul Bakker9e36f042013-06-30 14:34:05 +02004140 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004141
4142 /*
4143 * TLSv1.2:
4144 * hash = PRF( master, finished_label,
4145 * Hash( handshake ) )[0.11]
4146 */
4147
Paul Bakker9e36f042013-06-30 14:34:05 +02004148#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004149 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004150 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004151#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004152
4153 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004154 ? "client finished"
4155 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004156
Paul Bakker9e36f042013-06-30 14:34:05 +02004157 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004158
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004159 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004160 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004161
4162 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4163
Paul Bakker5b4af392014-06-26 12:09:34 +02004164 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004165
Paul Bakker34617722014-06-13 17:20:13 +02004166 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004167
4168 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4169}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004170#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004171
Paul Bakker9e36f042013-06-30 14:34:05 +02004172#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004173static void ssl_calc_finished_tls_sha384(
4174 ssl_context *ssl, unsigned char *buf, int from )
4175{
4176 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004177 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004178 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004179 unsigned char padbuf[48];
4180
Paul Bakker48916f92012-09-16 19:57:18 +00004181 ssl_session *session = ssl->session_negotiate;
4182 if( !session )
4183 session = ssl->session;
4184
Paul Bakker380da532012-04-18 16:10:25 +00004185 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004186
Paul Bakker9e36f042013-06-30 14:34:05 +02004187 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004188
4189 /*
4190 * TLSv1.2:
4191 * hash = PRF( master, finished_label,
4192 * Hash( handshake ) )[0.11]
4193 */
4194
Paul Bakker9e36f042013-06-30 14:34:05 +02004195#if !defined(POLARSSL_SHA512_ALT)
4196 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4197 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004198#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004199
4200 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004201 ? "client finished"
4202 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004203
Paul Bakker9e36f042013-06-30 14:34:05 +02004204 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004205
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004206 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004207 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004208
4209 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4210
Paul Bakker5b4af392014-06-26 12:09:34 +02004211 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004212
Paul Bakker34617722014-06-13 17:20:13 +02004213 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004214
4215 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4216}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004217#endif /* POLARSSL_SHA512_C */
4218#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004219
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004220static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004221{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004222 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004223
4224 /*
4225 * Free our handshake params
4226 */
4227 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004228 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004229 ssl->handshake = NULL;
4230
4231 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004232 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004233 */
4234 if( ssl->transform )
4235 {
4236 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004237 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004238 }
4239 ssl->transform = ssl->transform_negotiate;
4240 ssl->transform_negotiate = NULL;
4241
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004242 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4243}
4244
4245void ssl_handshake_wrapup( ssl_context *ssl )
4246{
4247 int resume = ssl->handshake->resume;
4248
4249 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4250
4251 if( ssl->renegotiation == SSL_RENEGOTIATION )
4252 {
4253 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4254 ssl->renego_records_seen = 0;
4255 }
4256
4257 /*
4258 * Free the previous session and switch in the current one
4259 */
Paul Bakker0a597072012-09-25 21:55:46 +00004260 if( ssl->session )
4261 {
4262 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004263 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004264 }
4265 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004266 ssl->session_negotiate = NULL;
4267
Paul Bakker0a597072012-09-25 21:55:46 +00004268 /*
4269 * Add cache entry
4270 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004271 if( ssl->f_set_cache != NULL &&
4272 ssl->session->length != 0 &&
4273 resume == 0 )
4274 {
Paul Bakker0a597072012-09-25 21:55:46 +00004275 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4276 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004277 }
Paul Bakker0a597072012-09-25 21:55:46 +00004278
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004279#if defined(POLARSSL_SSL_PROTO_DTLS)
4280 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4281 ssl->handshake->flight != NULL )
4282 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004283 /* Cancel handshake timer */
4284 ssl_set_timer( ssl, 0 );
4285
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004286 /* Keep last flight around in case we need to resend it:
4287 * we need the handshake and transform structures for that */
4288 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4289 }
4290 else
4291#endif
4292 ssl_handshake_wrapup_free_hs_transform( ssl );
4293
Paul Bakker48916f92012-09-16 19:57:18 +00004294 ssl->state++;
4295
4296 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4297}
4298
Paul Bakker1ef83d62012-04-11 12:09:53 +00004299int ssl_write_finished( ssl_context *ssl )
4300{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004301 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004302
4303 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4304
Paul Bakker92be97b2013-01-02 17:30:03 +01004305 /*
4306 * Set the out_msg pointer to the correct location based on IV length
4307 */
4308 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4309 {
4310 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4311 ssl->transform_negotiate->fixed_ivlen;
4312 }
4313 else
4314 ssl->out_msg = ssl->out_iv;
4315
Paul Bakker48916f92012-09-16 19:57:18 +00004316 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004317
4318 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4320
Paul Bakker48916f92012-09-16 19:57:18 +00004321 ssl->verify_data_len = hash_len;
4322 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4323
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 ssl->out_msglen = 4 + hash_len;
4325 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4326 ssl->out_msg[0] = SSL_HS_FINISHED;
4327
4328 /*
4329 * In case of session resuming, invert the client and server
4330 * ChangeCipherSpec messages order.
4331 */
Paul Bakker0a597072012-09-25 21:55:46 +00004332 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004333 {
4334 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004335 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 else
4337 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4338 }
4339 else
4340 ssl->state++;
4341
Paul Bakker48916f92012-09-16 19:57:18 +00004342 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004343 * Switch to our negotiated transform and session parameters for outbound
4344 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004345 */
4346 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004347
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004348#if defined(POLARSSL_SSL_PROTO_DTLS)
4349 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4350 {
4351 unsigned char i;
4352
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004353 /* Remember current epoch settings for resending */
4354 ssl->handshake->alt_transform_out = ssl->transform_out;
4355 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4356
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004357 /* Set sequence_number to zero */
4358 memset( ssl->out_ctr + 2, 0, 6 );
4359
4360 /* Increment epoch */
4361 for( i = 2; i > 0; i-- )
4362 if( ++ssl->out_ctr[i - 1] != 0 )
4363 break;
4364
4365 /* The loop goes to its end iff the counter is wrapping */
4366 if( i == 0 )
4367 {
4368 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4369 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4370 }
4371 }
4372 else
4373#endif /* POLARSSL_SSL_PROTO_DTLS */
4374 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004375
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004376 ssl->transform_out = ssl->transform_negotiate;
4377 ssl->session_out = ssl->session_negotiate;
4378
Paul Bakker07eb38b2012-12-19 14:42:06 +01004379#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004380 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004381 {
4382 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4383 {
4384 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4385 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4386 }
4387 }
4388#endif
4389
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004390#if defined(POLARSSL_SSL_PROTO_DTLS)
4391 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4392 ssl_send_flight_completed( ssl );
4393#endif
4394
Paul Bakker5121ce52009-01-03 21:22:43 +00004395 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4396 {
4397 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4398 return( ret );
4399 }
4400
4401 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4402
4403 return( 0 );
4404}
4405
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004406#if defined(POLARSSL_SSL_PROTO_SSL3)
4407#define SSL_MAX_HASH_LEN 36
4408#else
4409#define SSL_MAX_HASH_LEN 12
4410#endif
4411
Paul Bakker5121ce52009-01-03 21:22:43 +00004412int ssl_parse_finished( ssl_context *ssl )
4413{
Paul Bakker23986e52011-04-24 08:57:21 +00004414 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004415 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004416 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004417
4418 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4419
Paul Bakker48916f92012-09-16 19:57:18 +00004420 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004421
Paul Bakker5121ce52009-01-03 21:22:43 +00004422 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4423 {
4424 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4425 return( ret );
4426 }
4427
4428 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4429 {
4430 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004431 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 }
4433
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004434 /* There is currently no ciphersuite using another length with TLS 1.2 */
4435#if defined(POLARSSL_SSL_PROTO_SSL3)
4436 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4437 hash_len = 36;
4438 else
4439#endif
4440 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004441
4442 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004443 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004444 {
4445 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004446 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004447 }
4448
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004449 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4450 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
4452 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004453 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004454 }
4455
Paul Bakker48916f92012-09-16 19:57:18 +00004456 ssl->verify_data_len = hash_len;
4457 memcpy( ssl->peer_verify_data, buf, hash_len );
4458
Paul Bakker0a597072012-09-25 21:55:46 +00004459 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 {
4461 if( ssl->endpoint == SSL_IS_CLIENT )
4462 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4463
4464 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004465 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 }
4467 else
4468 ssl->state++;
4469
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004470#if defined(POLARSSL_SSL_PROTO_DTLS)
4471 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4472 ssl_recv_flight_completed( ssl );
4473#endif
4474
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4476
4477 return( 0 );
4478}
4479
Paul Bakker968afaa2014-07-09 11:09:24 +02004480static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004481{
4482 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4483
4484#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4485 defined(POLARSSL_SSL_PROTO_TLS1_1)
4486 md5_init( &handshake->fin_md5 );
4487 sha1_init( &handshake->fin_sha1 );
4488 md5_starts( &handshake->fin_md5 );
4489 sha1_starts( &handshake->fin_sha1 );
4490#endif
4491#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4492#if defined(POLARSSL_SHA256_C)
4493 sha256_init( &handshake->fin_sha256 );
4494 sha256_starts( &handshake->fin_sha256, 0 );
4495#endif
4496#if defined(POLARSSL_SHA512_C)
4497 sha512_init( &handshake->fin_sha512 );
4498 sha512_starts( &handshake->fin_sha512, 1 );
4499#endif
4500#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4501
4502 handshake->update_checksum = ssl_update_checksum_start;
4503 handshake->sig_alg = SSL_HASH_SHA1;
4504
4505#if defined(POLARSSL_DHM_C)
4506 dhm_init( &handshake->dhm_ctx );
4507#endif
4508#if defined(POLARSSL_ECDH_C)
4509 ecdh_init( &handshake->ecdh_ctx );
4510#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004511}
4512
4513static void ssl_transform_init( ssl_transform *transform )
4514{
4515 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004516
4517 cipher_init( &transform->cipher_ctx_enc );
4518 cipher_init( &transform->cipher_ctx_dec );
4519
4520 md_init( &transform->md_ctx_enc );
4521 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004522}
4523
4524void ssl_session_init( ssl_session *session )
4525{
4526 memset( session, 0, sizeof(ssl_session) );
4527}
4528
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004529static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004530{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004531 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004532 if( ssl->transform_negotiate )
4533 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004534 if( ssl->session_negotiate )
4535 ssl_session_free( ssl->session_negotiate );
4536 if( ssl->handshake )
4537 ssl_handshake_free( ssl->handshake );
4538
4539 /*
4540 * Either the pointers are now NULL or cleared properly and can be freed.
4541 * Now allocate missing structures.
4542 */
4543 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004544 {
4545 ssl->transform_negotiate =
4546 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4547 }
Paul Bakker48916f92012-09-16 19:57:18 +00004548
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004549 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004550 {
4551 ssl->session_negotiate =
4552 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4553 }
Paul Bakker48916f92012-09-16 19:57:18 +00004554
Paul Bakker82788fb2014-10-20 13:59:19 +02004555 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004556 {
4557 ssl->handshake = (ssl_handshake_params *)
4558 polarssl_malloc( sizeof(ssl_handshake_params) );
4559 }
Paul Bakker48916f92012-09-16 19:57:18 +00004560
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004561 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004562 if( ssl->handshake == NULL ||
4563 ssl->transform_negotiate == NULL ||
4564 ssl->session_negotiate == NULL )
4565 {
4566 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004567
4568 polarssl_free( ssl->handshake );
4569 polarssl_free( ssl->transform_negotiate );
4570 polarssl_free( ssl->session_negotiate );
4571
4572 ssl->handshake = NULL;
4573 ssl->transform_negotiate = NULL;
4574 ssl->session_negotiate = NULL;
4575
Paul Bakker48916f92012-09-16 19:57:18 +00004576 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4577 }
4578
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004579 /* Initialize structures */
4580 ssl_session_init( ssl->session_negotiate );
4581 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004582 ssl_handshake_params_init( ssl->handshake );
4583
4584#if defined(POLARSSL_X509_CRT_PARSE_C)
4585 ssl->handshake->key_cert = ssl->key_cert;
4586#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004587
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004588 /*
4589 * We may not know yet if we're using DTLS,
4590 * so always initiliase DTLS-specific fields.
4591 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004592#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004593 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004594
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004595 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004596 if( ssl->endpoint == SSL_IS_CLIENT )
4597 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4598 else
4599 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004600#endif
4601
Paul Bakker48916f92012-09-16 19:57:18 +00004602 return( 0 );
4603}
4604
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004605#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4606/* Dummy cookie callbacks for defaults */
4607static int ssl_cookie_write_dummy( void *ctx,
4608 unsigned char **p, unsigned char *end,
4609 const unsigned char *cli_id, size_t cli_id_len )
4610{
4611 ((void) ctx);
4612 ((void) p);
4613 ((void) end);
4614 ((void) cli_id);
4615 ((void) cli_id_len);
4616
4617 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4618}
4619
4620static int ssl_cookie_check_dummy( void *ctx,
4621 const unsigned char *cookie, size_t cookie_len,
4622 const unsigned char *cli_id, size_t cli_id_len )
4623{
4624 ((void) ctx);
4625 ((void) cookie);
4626 ((void) cookie_len);
4627 ((void) cli_id);
4628 ((void) cli_id_len);
4629
4630 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4631}
4632#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4633
Paul Bakker5121ce52009-01-03 21:22:43 +00004634/*
4635 * Initialize an SSL context
4636 */
4637int ssl_init( ssl_context *ssl )
4638{
Paul Bakker48916f92012-09-16 19:57:18 +00004639 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004640 int len = SSL_BUFFER_LEN;
4641
4642 memset( ssl, 0, sizeof( ssl_context ) );
4643
Paul Bakker62f2dee2012-09-28 07:31:51 +00004644 /*
4645 * Sane defaults
4646 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004647 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4648 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4649 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4650 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004651
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004652 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004653
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004654 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4655
Paul Bakker62f2dee2012-09-28 07:31:51 +00004656#if defined(POLARSSL_DHM_C)
4657 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4658 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4659 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4660 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4661 {
4662 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4663 return( ret );
4664 }
4665#endif
4666
4667 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004668 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004669 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004670 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004671 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004672
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004673 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004674 {
4675 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004676 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004677 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004678 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004679 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004680 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004681 }
4682
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004683 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4684 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004685
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004686 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4687 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4688
Paul Bakker606b4ba2013-08-14 16:52:14 +02004689#if defined(POLARSSL_SSL_SESSION_TICKETS)
4690 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4691#endif
4692
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004693#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004694 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004695#endif
4696
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004697#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4698 ssl->f_cookie_write = ssl_cookie_write_dummy;
4699 ssl->f_cookie_check = ssl_cookie_check_dummy;
4700#endif
4701
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004702#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4703 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4704#endif
4705
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004706#if defined(POLARSSL_SSL_PROTO_DTLS)
4707 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4708 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4709#endif
4710
Paul Bakker48916f92012-09-16 19:57:18 +00004711 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004713
4714 return( 0 );
4715}
4716
4717/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004718 * Reset an initialized and used SSL context for re-use while retaining
4719 * all application-set variables, function pointers and data.
4720 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004721int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004722{
Paul Bakker48916f92012-09-16 19:57:18 +00004723 int ret;
4724
Paul Bakker7eb013f2011-10-06 12:37:39 +00004725 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004726 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4727 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4728
4729 ssl->verify_data_len = 0;
4730 memset( ssl->own_verify_data, 0, 36 );
4731 memset( ssl->peer_verify_data, 0, 36 );
4732
Paul Bakker7eb013f2011-10-06 12:37:39 +00004733 ssl->in_offt = NULL;
4734
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004735 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004736 ssl->in_msgtype = 0;
4737 ssl->in_msglen = 0;
4738 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004739#if defined(POLARSSL_SSL_PROTO_DTLS)
4740 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004741 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004742#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004743#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4744 ssl_dtls_replay_reset( ssl );
4745#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004746
4747 ssl->in_hslen = 0;
4748 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004749 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004750
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004751 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004752 ssl->out_msgtype = 0;
4753 ssl->out_msglen = 0;
4754 ssl->out_left = 0;
4755
Paul Bakker48916f92012-09-16 19:57:18 +00004756 ssl->transform_in = NULL;
4757 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004758
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004759 ssl->renego_records_seen = 0;
4760
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004761 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4762 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004763
4764#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004765 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004766 {
4767 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004768 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004769 {
4770 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4771 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4772 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004773 }
4774#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004775
Paul Bakker48916f92012-09-16 19:57:18 +00004776 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004777 {
Paul Bakker48916f92012-09-16 19:57:18 +00004778 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004779 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004780 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004781 }
Paul Bakker48916f92012-09-16 19:57:18 +00004782
Paul Bakkerc0463502013-02-14 11:19:38 +01004783 if( ssl->session )
4784 {
4785 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004786 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004787 ssl->session = NULL;
4788 }
4789
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004790#if defined(POLARSSL_SSL_ALPN)
4791 ssl->alpn_chosen = NULL;
4792#endif
4793
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004794#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004795 polarssl_free( ssl->cli_id );
4796 ssl->cli_id = NULL;
4797 ssl->cli_id_len = 0;
4798#endif
4799
Paul Bakker48916f92012-09-16 19:57:18 +00004800 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4801 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004802
4803 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004804}
4805
Paul Bakkera503a632013-08-14 13:48:06 +02004806#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004807static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4808{
4809 aes_free( &tkeys->enc );
4810 aes_free( &tkeys->dec );
4811
4812 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4813}
4814
Paul Bakker7eb013f2011-10-06 12:37:39 +00004815/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004816 * Allocate and initialize ticket keys
4817 */
4818static int ssl_ticket_keys_init( ssl_context *ssl )
4819{
4820 int ret;
4821 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004822 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004823
4824 if( ssl->ticket_keys != NULL )
4825 return( 0 );
4826
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004827 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4828 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004829 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4830
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004831 aes_init( &tkeys->enc );
4832 aes_init( &tkeys->dec );
4833
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004834 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004835 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004836 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004837 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004838 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004839 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004840
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004841 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4842 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4843 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4844 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004845 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004846 polarssl_free( tkeys );
4847 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004848 }
4849
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004850 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004851 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004852 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004853 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004854 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004855 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004856
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004857 ssl->ticket_keys = tkeys;
4858
4859 return( 0 );
4860}
Paul Bakkera503a632013-08-14 13:48:06 +02004861#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004862
4863/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004864 * SSL set accessors
4865 */
4866void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4867{
4868 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004869
Paul Bakker606b4ba2013-08-14 16:52:14 +02004870#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004871 if( endpoint == SSL_IS_CLIENT )
4872 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004874}
4875
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004876int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004877{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004878#if defined(POLARSSL_SSL_PROTO_DTLS)
4879 if( transport == SSL_TRANSPORT_DATAGRAM )
4880 {
4881 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004882
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004883 ssl->out_hdr = ssl->out_buf;
4884 ssl->out_ctr = ssl->out_buf + 3;
4885 ssl->out_len = ssl->out_buf + 11;
4886 ssl->out_iv = ssl->out_buf + 13;
4887 ssl->out_msg = ssl->out_buf + 13;
4888
4889 ssl->in_hdr = ssl->in_buf;
4890 ssl->in_ctr = ssl->in_buf + 3;
4891 ssl->in_len = ssl->in_buf + 11;
4892 ssl->in_iv = ssl->in_buf + 13;
4893 ssl->in_msg = ssl->in_buf + 13;
4894
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004895 /* DTLS starts with TLS1.1 */
4896 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4897 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004898
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004899 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4900 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4901
4902 return( 0 );
4903 }
4904#endif
4905
4906 if( transport == SSL_TRANSPORT_STREAM )
4907 {
4908 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004909
4910 ssl->out_ctr = ssl->out_buf;
4911 ssl->out_hdr = ssl->out_buf + 8;
4912 ssl->out_len = ssl->out_buf + 11;
4913 ssl->out_iv = ssl->out_buf + 13;
4914 ssl->out_msg = ssl->out_buf + 13;
4915
4916 ssl->in_ctr = ssl->in_buf;
4917 ssl->in_hdr = ssl->in_buf + 8;
4918 ssl->in_len = ssl->in_buf + 11;
4919 ssl->in_iv = ssl->in_buf + 13;
4920 ssl->in_msg = ssl->in_buf + 13;
4921
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004922 return( 0 );
4923 }
4924
4925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004926}
4927
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004928#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4929void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4930{
4931 ssl->anti_replay = mode;
4932}
4933#endif
4934
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004935#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
4936void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
4937{
4938 ssl->badmac_limit = limit;
4939}
4940#endif
4941
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004942#if defined(POLARSSL_SSL_PROTO_DTLS)
4943void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4944{
4945 ssl->hs_timeout_min = min;
4946 ssl->hs_timeout_max = max;
4947}
4948#endif
4949
Paul Bakker5121ce52009-01-03 21:22:43 +00004950void ssl_set_authmode( ssl_context *ssl, int authmode )
4951{
4952 ssl->authmode = authmode;
4953}
4954
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004955#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004956void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004957 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004958 void *p_vrfy )
4959{
4960 ssl->f_vrfy = f_vrfy;
4961 ssl->p_vrfy = p_vrfy;
4962}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004963#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004964
Paul Bakker5121ce52009-01-03 21:22:43 +00004965void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004966 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004967 void *p_rng )
4968{
4969 ssl->f_rng = f_rng;
4970 ssl->p_rng = p_rng;
4971}
4972
4973void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004974 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004975 void *p_dbg )
4976{
4977 ssl->f_dbg = f_dbg;
4978 ssl->p_dbg = p_dbg;
4979}
4980
4981void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004982 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004983 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004984{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004985 if( p_recv != p_send )
4986 {
4987 ssl->f_recv = NULL;
4988 ssl->f_send = NULL;
4989 ssl->p_bio = NULL;
4990 return;
4991 }
4992
Paul Bakker5121ce52009-01-03 21:22:43 +00004993 ssl->f_recv = f_recv;
4994 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004995 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004996}
4997
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004998void ssl_set_bio_timeout( ssl_context *ssl,
4999 void *p_bio,
5000 int (*f_send)(void *, const unsigned char *, size_t),
5001 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005002 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5003 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005004{
5005 ssl->p_bio = p_bio;
5006 ssl->f_send = f_send;
5007 ssl->f_recv = f_recv;
5008 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005009 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005010}
5011
Paul Bakker0a597072012-09-25 21:55:46 +00005012void ssl_set_session_cache( ssl_context *ssl,
5013 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5014 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005015{
Paul Bakker0a597072012-09-25 21:55:46 +00005016 ssl->f_get_cache = f_get_cache;
5017 ssl->p_get_cache = p_get_cache;
5018 ssl->f_set_cache = f_set_cache;
5019 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005020}
5021
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005022int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005023{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005024 int ret;
5025
5026 if( ssl == NULL ||
5027 session == NULL ||
5028 ssl->session_negotiate == NULL ||
5029 ssl->endpoint != SSL_IS_CLIENT )
5030 {
5031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5032 }
5033
5034 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5035 return( ret );
5036
Paul Bakker0a597072012-09-25 21:55:46 +00005037 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005038
5039 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005040}
5041
Paul Bakkerb68cad62012-08-23 08:34:18 +00005042void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005043{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005044 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5045 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5046 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5047 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5048}
5049
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005050void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5051 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005052 int major, int minor )
5053{
5054 if( major != SSL_MAJOR_VERSION_3 )
5055 return;
5056
5057 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5058 return;
5059
5060 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005061}
5062
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005063#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005064/* Add a new (empty) key_cert entry an return a pointer to it */
5065static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5066{
5067 ssl_key_cert *key_cert, *last;
5068
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005069 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5070 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005071 return( NULL );
5072
5073 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5074
5075 /* Append the new key_cert to the (possibly empty) current list */
5076 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005077 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005078 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005079 if( ssl->handshake != NULL )
5080 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005081 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005082 else
5083 {
5084 last = ssl->key_cert;
5085 while( last->next != NULL )
5086 last = last->next;
5087 last->next = key_cert;
5088 }
5089
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005090 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005091}
5092
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005093void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005094 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005095{
5096 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005097 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 ssl->peer_cn = peer_cn;
5099}
5100
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005101int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005102 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005104 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5105
5106 if( key_cert == NULL )
5107 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5108
5109 key_cert->cert = own_cert;
5110 key_cert->key = pk_key;
5111
5112 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005113}
5114
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005115#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005116int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005117 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005118{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005119 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005120 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005121
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005122 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005123 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5124
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005125 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5126 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005127 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005128
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005129 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005130
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005131 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005132 if( ret != 0 )
5133 return( ret );
5134
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005135 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005136 return( ret );
5137
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005138 key_cert->cert = own_cert;
5139 key_cert->key_own_alloc = 1;
5140
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005141 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005142}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005143#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005144
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005145int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005146 void *rsa_key,
5147 rsa_decrypt_func rsa_decrypt,
5148 rsa_sign_func rsa_sign,
5149 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005150{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005151 int ret;
5152 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005153
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005154 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005155 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5156
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005157 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5158 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005159 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005160
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005161 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005162
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005163 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5164 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5165 return( ret );
5166
5167 key_cert->cert = own_cert;
5168 key_cert->key_own_alloc = 1;
5169
5170 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005171}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005172#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005173
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005174#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005175int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5176 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005177{
Paul Bakker6db455e2013-09-18 17:29:31 +02005178 if( psk == NULL || psk_identity == NULL )
5179 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5180
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005181 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005182 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5183
Paul Bakker6db455e2013-09-18 17:29:31 +02005184 if( ssl->psk != NULL )
5185 {
5186 polarssl_free( ssl->psk );
5187 polarssl_free( ssl->psk_identity );
5188 }
5189
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005190 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005191 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005192
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005193 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005194 ssl->psk_identity = (unsigned char *)
5195 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005196
5197 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5198 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5199
5200 memcpy( ssl->psk, psk, ssl->psk_len );
5201 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005202
5203 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005204}
5205
5206void ssl_set_psk_cb( ssl_context *ssl,
5207 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5208 size_t),
5209 void *p_psk )
5210{
5211 ssl->f_psk = f_psk;
5212 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005213}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005214#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005215
Paul Bakker48916f92012-09-16 19:57:18 +00005216#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005217int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005218{
5219 int ret;
5220
Paul Bakker48916f92012-09-16 19:57:18 +00005221 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005222 {
5223 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5224 return( ret );
5225 }
5226
Paul Bakker48916f92012-09-16 19:57:18 +00005227 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005228 {
5229 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5230 return( ret );
5231 }
5232
5233 return( 0 );
5234}
5235
Paul Bakker1b57b062011-01-06 15:48:19 +00005236int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5237{
5238 int ret;
5239
Paul Bakker66d5d072014-06-17 16:39:18 +02005240 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005241 {
5242 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5243 return( ret );
5244 }
5245
Paul Bakker66d5d072014-06-17 16:39:18 +02005246 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005247 {
5248 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5249 return( ret );
5250 }
5251
5252 return( 0 );
5253}
Paul Bakker48916f92012-09-16 19:57:18 +00005254#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005255
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005256#if defined(POLARSSL_SSL_SET_CURVES)
5257/*
5258 * Set the allowed elliptic curves
5259 */
5260void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5261{
5262 ssl->curve_list = curve_list;
5263}
5264#endif
5265
Paul Bakker0be444a2013-08-27 21:55:01 +02005266#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005267int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005268{
5269 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005271
5272 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005273
5274 if( ssl->hostname_len + 1 == 0 )
5275 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5276
Paul Bakker6e339b52013-07-03 13:37:05 +02005277 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005278
Paul Bakkerb15b8512012-01-13 13:44:06 +00005279 if( ssl->hostname == NULL )
5280 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5281
Paul Bakker3c2122f2013-06-24 19:03:14 +02005282 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005283 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005284
Paul Bakker40ea7de2009-05-03 10:18:48 +00005285 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005286
5287 return( 0 );
5288}
5289
Paul Bakker5701cdc2012-09-27 21:49:42 +00005290void ssl_set_sni( ssl_context *ssl,
5291 int (*f_sni)(void *, ssl_context *,
5292 const unsigned char *, size_t),
5293 void *p_sni )
5294{
5295 ssl->f_sni = f_sni;
5296 ssl->p_sni = p_sni;
5297}
Paul Bakker0be444a2013-08-27 21:55:01 +02005298#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005299
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005300#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005301int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005302{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005303 size_t cur_len, tot_len;
5304 const char **p;
5305
5306 /*
5307 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5308 * truncated". Check lengths now rather than later.
5309 */
5310 tot_len = 0;
5311 for( p = protos; *p != NULL; p++ )
5312 {
5313 cur_len = strlen( *p );
5314 tot_len += cur_len;
5315
5316 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5317 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5318 }
5319
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005320 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005321
5322 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005323}
5324
5325const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5326{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005327 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005328}
5329#endif /* POLARSSL_SSL_ALPN */
5330
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005331static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005332{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005333 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005334 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005335 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005336 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005337 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005338
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005339#if defined(POLARSSL_SSL_PROTO_DTLS)
5340 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5341 minor < SSL_MINOR_VERSION_2 )
5342 {
5343 return( -1 );
5344 }
5345#else
5346 ((void) ssl);
5347#endif
5348
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005349 return( 0 );
5350}
5351
5352int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5353{
5354 if( ssl_check_version( ssl, major, minor ) != 0 )
5355 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5356
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005357 ssl->max_major_ver = major;
5358 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005359
5360 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005361}
5362
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005363int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005364{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005365 if( ssl_check_version( ssl, major, minor ) != 0 )
5366 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005367
5368 ssl->min_major_ver = major;
5369 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005370
5371 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005372}
5373
Paul Bakker05decb22013-08-15 13:33:48 +02005374#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005375int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5376{
Paul Bakker77e257e2013-12-16 15:29:52 +01005377 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005378 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005379 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005380 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005381 }
5382
5383 ssl->mfl_code = mfl_code;
5384
5385 return( 0 );
5386}
Paul Bakker05decb22013-08-15 13:33:48 +02005387#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005388
Paul Bakker1f2bc622013-08-15 13:45:55 +02005389#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005390int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005391{
5392 if( ssl->endpoint != SSL_IS_CLIENT )
5393 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5394
Paul Bakker8c1ede62013-07-19 14:14:37 +02005395 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005396
5397 return( 0 );
5398}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005399#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005400
Paul Bakker48916f92012-09-16 19:57:18 +00005401void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5402{
5403 ssl->disable_renegotiation = renegotiation;
5404}
5405
5406void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5407{
5408 ssl->allow_legacy_renegotiation = allow_legacy;
5409}
5410
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005411void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5412{
5413 ssl->renego_max_records = max_records;
5414}
5415
Paul Bakkera503a632013-08-14 13:48:06 +02005416#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005417int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5418{
5419 ssl->session_tickets = use_tickets;
5420
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005421 if( ssl->endpoint == SSL_IS_CLIENT )
5422 return( 0 );
5423
5424 if( ssl->f_rng == NULL )
5425 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5426
5427 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005428}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005429
5430void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5431{
5432 ssl->ticket_lifetime = lifetime;
5433}
Paul Bakkera503a632013-08-14 13:48:06 +02005434#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005435
Paul Bakker5121ce52009-01-03 21:22:43 +00005436/*
5437 * SSL get accessors
5438 */
Paul Bakker23986e52011-04-24 08:57:21 +00005439size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005440{
5441 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5442}
5443
Paul Bakkerff60ee62010-03-16 21:09:09 +00005444int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005445{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005446 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005447}
5448
Paul Bakkere3166ce2011-01-27 17:40:50 +00005449const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005450{
Paul Bakker926c8e42013-03-06 10:23:34 +01005451 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005452 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005453
Paul Bakkere3166ce2011-01-27 17:40:50 +00005454 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005455}
5456
Paul Bakker43ca69c2011-01-15 17:35:19 +00005457const char *ssl_get_version( const ssl_context *ssl )
5458{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005459#if defined(POLARSSL_SSL_PROTO_DTLS)
5460 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5461 {
5462 switch( ssl->minor_ver )
5463 {
5464 case SSL_MINOR_VERSION_2:
5465 return( "DTLSv1.0" );
5466
5467 case SSL_MINOR_VERSION_3:
5468 return( "DTLSv1.2" );
5469
5470 default:
5471 return( "unknown (DTLS)" );
5472 }
5473 }
5474#endif
5475
Paul Bakker43ca69c2011-01-15 17:35:19 +00005476 switch( ssl->minor_ver )
5477 {
5478 case SSL_MINOR_VERSION_0:
5479 return( "SSLv3.0" );
5480
5481 case SSL_MINOR_VERSION_1:
5482 return( "TLSv1.0" );
5483
5484 case SSL_MINOR_VERSION_2:
5485 return( "TLSv1.1" );
5486
Paul Bakker1ef83d62012-04-11 12:09:53 +00005487 case SSL_MINOR_VERSION_3:
5488 return( "TLSv1.2" );
5489
Paul Bakker43ca69c2011-01-15 17:35:19 +00005490 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005491 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005492 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005493}
5494
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005495int ssl_get_record_expansion( const ssl_context *ssl )
5496{
5497 int transform_expansion;
5498 const ssl_transform *transform = ssl->transform_out;
5499
5500#if defined(POLARSSL_ZLIB_SUPPORT)
5501 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5502 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5503#endif
5504
5505 if( transform == NULL )
5506 return( ssl_hdr_len( ssl ) );
5507
5508 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5509 {
5510 case POLARSSL_MODE_GCM:
5511 case POLARSSL_MODE_CCM:
5512 case POLARSSL_MODE_STREAM:
5513 transform_expansion = transform->minlen;
5514 break;
5515
5516 case POLARSSL_MODE_CBC:
5517 transform_expansion = transform->maclen
5518 + cipher_get_block_size( &transform->cipher_ctx_enc );
5519 break;
5520
5521 default:
5522 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5523 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5524 }
5525
5526 return( ssl_hdr_len( ssl ) + transform_expansion );
5527}
5528
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005529#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005530const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005531{
5532 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005533 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005534
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005535 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005536}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005537#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005538
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005539int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5540{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005541 if( ssl == NULL ||
5542 dst == NULL ||
5543 ssl->session == NULL ||
5544 ssl->endpoint != SSL_IS_CLIENT )
5545 {
5546 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5547 }
5548
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005549 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005550}
5551
Paul Bakker5121ce52009-01-03 21:22:43 +00005552/*
Paul Bakker1961b702013-01-25 14:49:24 +01005553 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005554 */
Paul Bakker1961b702013-01-25 14:49:24 +01005555int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005556{
Paul Bakker40e46942009-01-03 21:51:57 +00005557 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005558
Paul Bakker40e46942009-01-03 21:51:57 +00005559#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005560 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005561 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562#endif
5563
Paul Bakker40e46942009-01-03 21:51:57 +00005564#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005565 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005566 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005567#endif
5568
Paul Bakker1961b702013-01-25 14:49:24 +01005569 return( ret );
5570}
5571
5572/*
5573 * Perform the SSL handshake
5574 */
5575int ssl_handshake( ssl_context *ssl )
5576{
5577 int ret = 0;
5578
5579 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5580
5581 while( ssl->state != SSL_HANDSHAKE_OVER )
5582 {
5583 ret = ssl_handshake_step( ssl );
5584
5585 if( ret != 0 )
5586 break;
5587 }
5588
Paul Bakker5121ce52009-01-03 21:22:43 +00005589 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5590
5591 return( ret );
5592}
5593
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005594#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005595/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005596 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005597 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005598static int ssl_write_hello_request( ssl_context *ssl )
5599{
5600 int ret;
5601
5602 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5603
5604 ssl->out_msglen = 4;
5605 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5606 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5607
5608 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5609 {
5610 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5611 return( ret );
5612 }
5613
5614 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5615
5616 return( 0 );
5617}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005618#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005619
5620/*
5621 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005622 * - any side: calling ssl_renegotiate(),
5623 * - client: receiving a HelloRequest during ssl_read(),
5624 * - server: receiving any handshake message on server during ssl_read() after
5625 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005626 * If the handshake doesn't complete due to waiting for I/O, it will continue
5627 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005628 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005629static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005630{
5631 int ret;
5632
5633 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5634
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005635 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5636 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005637
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005638 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5639 * the ServerHello will have message_seq = 1" */
5640#if defined(POLARSSL_SSL_PROTO_DTLS)
5641 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005642 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5643 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005644 if( ssl->endpoint == SSL_IS_SERVER )
5645 ssl->handshake->out_msg_seq = 1;
5646 else
5647 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005648 }
5649#endif
5650
Paul Bakker48916f92012-09-16 19:57:18 +00005651 ssl->state = SSL_HELLO_REQUEST;
5652 ssl->renegotiation = SSL_RENEGOTIATION;
5653
Paul Bakker48916f92012-09-16 19:57:18 +00005654 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5655 {
5656 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5657 return( ret );
5658 }
5659
5660 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5661
5662 return( 0 );
5663}
5664
5665/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005666 * Renegotiate current connection on client,
5667 * or request renegotiation on server
5668 */
5669int ssl_renegotiate( ssl_context *ssl )
5670{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005671 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005672
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005673#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005674 /* On server, just send the request */
5675 if( ssl->endpoint == SSL_IS_SERVER )
5676 {
5677 if( ssl->state != SSL_HANDSHAKE_OVER )
5678 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5679
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005680 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5681
5682 /* Did we already try/start sending HelloRequest? */
5683 if( ssl->out_left != 0 )
5684 return( ssl_flush_output( ssl ) );
5685
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005686 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005687 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005688#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005689
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005690#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005691 /*
5692 * On client, either start the renegotiation process or,
5693 * if already in progress, continue the handshake
5694 */
5695 if( ssl->renegotiation != SSL_RENEGOTIATION )
5696 {
5697 if( ssl->state != SSL_HANDSHAKE_OVER )
5698 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5699
5700 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5701 {
5702 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5703 return( ret );
5704 }
5705 }
5706 else
5707 {
5708 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5709 {
5710 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5711 return( ret );
5712 }
5713 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005714#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005715
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005716 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005717}
5718
5719/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005720 * Receive application data decrypted from the SSL layer
5721 */
Paul Bakker23986e52011-04-24 08:57:21 +00005722int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005723{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005724 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005725 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005726
5727 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5728
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005729#if defined(POLARSSL_SSL_PROTO_DTLS)
5730 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5731 ssl->handshake != NULL &&
5732 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5733 {
5734 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5735 return( ret );
5736
5737 if( ( ret = ssl_resend( ssl ) ) != 0 )
5738 return( ret );
5739 }
5740#endif
5741
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 if( ssl->state != SSL_HANDSHAKE_OVER )
5743 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005744 ret = ssl_handshake( ssl );
5745 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5746 {
5747 record_read = 1;
5748 }
5749 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005750 {
5751 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5752 return( ret );
5753 }
5754 }
5755
5756 if( ssl->in_offt == NULL )
5757 {
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005758#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005759 /* Start timer if not already running */
5760 if( ssl->time_limit == 0 )
5761 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005762#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005763
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005764 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005765 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005766 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5767 {
5768 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5769 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005770
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005771 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5772 return( ret );
5773 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005774 }
5775
5776 if( ssl->in_msglen == 0 &&
5777 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5778 {
5779 /*
5780 * OpenSSL sends empty messages to randomize the IV
5781 */
5782 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5783 {
Paul Bakker831a7552011-05-18 13:32:51 +00005784 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5785 return( 0 );
5786
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5788 return( ret );
5789 }
5790 }
5791
Paul Bakker48916f92012-09-16 19:57:18 +00005792 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5793 {
5794 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5795
5796 if( ssl->endpoint == SSL_IS_CLIENT &&
5797 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005798 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005799 {
5800 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005801
5802 /* With DTLS, drop the packet (probably from last handshake) */
5803#if defined(POLARSSL_SSL_PROTO_DTLS)
5804 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5805 return( POLARSSL_ERR_NET_WANT_READ );
5806#endif
5807 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5808 }
5809
5810 if( ssl->endpoint == SSL_IS_SERVER &&
5811 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5812 {
5813 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5814
5815 /* With DTLS, drop the packet (probably from last handshake) */
5816#if defined(POLARSSL_SSL_PROTO_DTLS)
5817 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5818 return( POLARSSL_ERR_NET_WANT_READ );
5819#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005820 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5821 }
5822
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005823 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5824 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005825 ssl->allow_legacy_renegotiation ==
5826 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005827 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005828 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005829
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005830#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005831 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005832 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005833 /*
5834 * SSLv3 does not have a "no_renegotiation" alert
5835 */
5836 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5837 return( ret );
5838 }
5839 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005840#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005841#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5842 defined(POLARSSL_SSL_PROTO_TLS1_2)
5843 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005844 {
5845 if( ( ret = ssl_send_alert_message( ssl,
5846 SSL_ALERT_LEVEL_WARNING,
5847 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5848 {
5849 return( ret );
5850 }
Paul Bakker48916f92012-09-16 19:57:18 +00005851 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005852 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005853#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5854 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005855 {
5856 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005857 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005858 }
Paul Bakker48916f92012-09-16 19:57:18 +00005859 }
5860 else
5861 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005862 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005863#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005864 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5865 ssl->endpoint == SSL_IS_CLIENT )
5866 {
5867 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5868 }
5869#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005870 ret = ssl_start_renegotiation( ssl );
5871 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5872 {
5873 record_read = 1;
5874 }
5875 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005876 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005877 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005878 return( ret );
5879 }
Paul Bakker48916f92012-09-16 19:57:18 +00005880 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005881
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005882 /* If a non-handshake record was read during renego, fallthrough,
5883 * else tell the user they should call ssl_read() again */
5884 if( ! record_read )
5885 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005886 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005887 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5888 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005889 ssl->renego_records_seen++;
5890
5891 if( ssl->renego_max_records >= 0 &&
5892 ssl->renego_records_seen > ssl->renego_max_records )
5893 {
5894 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5895 "but not honored by client" ) );
5896 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5897 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005898 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005899
5900 /* Fatal and closure alerts handled by ssl_read_record() */
5901 if( ssl->in_msgtype == SSL_MSG_ALERT )
5902 {
5903 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5904 return( POLARSSL_ERR_NET_WANT_READ );
5905 }
5906
5907 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005908 {
5909 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005910 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005911 }
5912
5913 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005914
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005915#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005916 /* We're going to return something now, cancel timer,
5917 * except if handshake (renegotiation) is in progress */
5918 if( ssl->state == SSL_HANDSHAKE_OVER )
5919 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005920#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005921 }
5922
5923 n = ( len < ssl->in_msglen )
5924 ? len : ssl->in_msglen;
5925
5926 memcpy( buf, ssl->in_offt, n );
5927 ssl->in_msglen -= n;
5928
5929 if( ssl->in_msglen == 0 )
5930 /* all bytes consumed */
5931 ssl->in_offt = NULL;
5932 else
5933 /* more data available */
5934 ssl->in_offt += n;
5935
5936 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5937
Paul Bakker23986e52011-04-24 08:57:21 +00005938 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005939}
5940
5941/*
5942 * Send application data to be encrypted by the SSL layer
5943 */
Paul Bakker23986e52011-04-24 08:57:21 +00005944int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005945{
Paul Bakker23986e52011-04-24 08:57:21 +00005946 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005947#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
5948 unsigned int max_len;
5949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005950
5951 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5952
5953 if( ssl->state != SSL_HANDSHAKE_OVER )
5954 {
5955 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5956 {
5957 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5958 return( ret );
5959 }
5960 }
5961
Paul Bakker05decb22013-08-15 13:33:48 +02005962#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005963 /*
5964 * Assume mfl_code is correct since it was checked when set
5965 */
5966 max_len = mfl_code_to_length[ssl->mfl_code];
5967
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005968 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005969 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005970 */
5971 if( ssl->session_out != NULL &&
5972 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5973 {
5974 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5975 }
5976
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005977 if( len > max_len )
5978 {
5979#if defined(POLARSSL_SSL_PROTO_DTLS)
5980 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5981 {
5982 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
5983 "maximum fragment length: %d > %d",
5984 len, max_len ) );
5985 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5986 }
5987 else
5988#endif
5989 len = max_len;
5990 }
5991#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00005992
Paul Bakker5121ce52009-01-03 21:22:43 +00005993 if( ssl->out_left != 0 )
5994 {
5995 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5996 {
5997 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5998 return( ret );
5999 }
6000 }
Paul Bakker887bd502011-06-08 13:10:54 +00006001 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006002 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006003 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006004 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006005 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006006
6007 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6008 {
6009 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6010 return( ret );
6011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006012 }
6013
6014 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6015
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006016 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006017}
6018
6019/*
6020 * Notify the peer that the connection is being closed
6021 */
6022int ssl_close_notify( ssl_context *ssl )
6023{
6024 int ret;
6025
6026 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6027
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006028 if( ssl->out_left != 0 )
6029 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006030
6031 if( ssl->state == SSL_HANDSHAKE_OVER )
6032 {
Paul Bakker48916f92012-09-16 19:57:18 +00006033 if( ( ret = ssl_send_alert_message( ssl,
6034 SSL_ALERT_LEVEL_WARNING,
6035 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006037 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006038 return( ret );
6039 }
6040 }
6041
6042 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6043
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006044 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006045}
6046
Paul Bakker48916f92012-09-16 19:57:18 +00006047void ssl_transform_free( ssl_transform *transform )
6048{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006049 if( transform == NULL )
6050 return;
6051
Paul Bakker48916f92012-09-16 19:57:18 +00006052#if defined(POLARSSL_ZLIB_SUPPORT)
6053 deflateEnd( &transform->ctx_deflate );
6054 inflateEnd( &transform->ctx_inflate );
6055#endif
6056
Paul Bakker84bbeb52014-07-01 14:53:22 +02006057 cipher_free( &transform->cipher_ctx_enc );
6058 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006059
Paul Bakker84bbeb52014-07-01 14:53:22 +02006060 md_free( &transform->md_ctx_enc );
6061 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006062
Paul Bakker34617722014-06-13 17:20:13 +02006063 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006064}
6065
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006066#if defined(POLARSSL_X509_CRT_PARSE_C)
6067static void ssl_key_cert_free( ssl_key_cert *key_cert )
6068{
6069 ssl_key_cert *cur = key_cert, *next;
6070
6071 while( cur != NULL )
6072 {
6073 next = cur->next;
6074
6075 if( cur->key_own_alloc )
6076 {
6077 pk_free( cur->key );
6078 polarssl_free( cur->key );
6079 }
6080 polarssl_free( cur );
6081
6082 cur = next;
6083 }
6084}
6085#endif /* POLARSSL_X509_CRT_PARSE_C */
6086
Paul Bakker48916f92012-09-16 19:57:18 +00006087void ssl_handshake_free( ssl_handshake_params *handshake )
6088{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006089 if( handshake == NULL )
6090 return;
6091
Paul Bakker48916f92012-09-16 19:57:18 +00006092#if defined(POLARSSL_DHM_C)
6093 dhm_free( &handshake->dhm_ctx );
6094#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006095#if defined(POLARSSL_ECDH_C)
6096 ecdh_free( &handshake->ecdh_ctx );
6097#endif
6098
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006099#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006100 /* explicit void pointer cast for buggy MS compiler */
6101 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006102#endif
6103
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006104#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6105 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6106 /*
6107 * Free only the linked list wrapper, not the keys themselves
6108 * since the belong to the SNI callback
6109 */
6110 if( handshake->sni_key_cert != NULL )
6111 {
6112 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6113
6114 while( cur != NULL )
6115 {
6116 next = cur->next;
6117 polarssl_free( cur );
6118 cur = next;
6119 }
6120 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006121#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006122
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006123#if defined(POLARSSL_SSL_PROTO_DTLS)
6124 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006125 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006126 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006127#endif
6128
Paul Bakker34617722014-06-13 17:20:13 +02006129 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006130}
6131
6132void ssl_session_free( ssl_session *session )
6133{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006134 if( session == NULL )
6135 return;
6136
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006137#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006138 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006139 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006140 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006141 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006142 }
Paul Bakkered27a042013-04-18 22:46:23 +02006143#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006144
Paul Bakkera503a632013-08-14 13:48:06 +02006145#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006146 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006147#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006148
Paul Bakker34617722014-06-13 17:20:13 +02006149 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006150}
6151
Paul Bakker5121ce52009-01-03 21:22:43 +00006152/*
6153 * Free an SSL context
6154 */
6155void ssl_free( ssl_context *ssl )
6156{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006157 if( ssl == NULL )
6158 return;
6159
Paul Bakker5121ce52009-01-03 21:22:43 +00006160 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6161
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006162 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006163 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006164 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6165 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 }
6167
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006168 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006169 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006170 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6171 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006172 }
6173
Paul Bakker16770332013-10-11 09:59:44 +02006174#if defined(POLARSSL_ZLIB_SUPPORT)
6175 if( ssl->compress_buf != NULL )
6176 {
Paul Bakker34617722014-06-13 17:20:13 +02006177 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006178 polarssl_free( ssl->compress_buf );
6179 }
6180#endif
6181
Paul Bakker40e46942009-01-03 21:51:57 +00006182#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006183 mpi_free( &ssl->dhm_P );
6184 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006185#endif
6186
Paul Bakker48916f92012-09-16 19:57:18 +00006187 if( ssl->transform )
6188 {
6189 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006190 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006191 }
6192
6193 if( ssl->handshake )
6194 {
6195 ssl_handshake_free( ssl->handshake );
6196 ssl_transform_free( ssl->transform_negotiate );
6197 ssl_session_free( ssl->session_negotiate );
6198
Paul Bakker6e339b52013-07-03 13:37:05 +02006199 polarssl_free( ssl->handshake );
6200 polarssl_free( ssl->transform_negotiate );
6201 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006202 }
6203
Paul Bakkerc0463502013-02-14 11:19:38 +01006204 if( ssl->session )
6205 {
6206 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006207 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006208 }
6209
Paul Bakkera503a632013-08-14 13:48:06 +02006210#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006211 if( ssl->ticket_keys )
6212 {
6213 ssl_ticket_keys_free( ssl->ticket_keys );
6214 polarssl_free( ssl->ticket_keys );
6215 }
Paul Bakkera503a632013-08-14 13:48:06 +02006216#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006217
Paul Bakker0be444a2013-08-27 21:55:01 +02006218#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006219 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006220 {
Paul Bakker34617722014-06-13 17:20:13 +02006221 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006222 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006223 ssl->hostname_len = 0;
6224 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006225#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006227#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006228 if( ssl->psk != NULL )
6229 {
Paul Bakker34617722014-06-13 17:20:13 +02006230 polarssl_zeroize( ssl->psk, ssl->psk_len );
6231 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006232 polarssl_free( ssl->psk );
6233 polarssl_free( ssl->psk_identity );
6234 ssl->psk_len = 0;
6235 ssl->psk_identity_len = 0;
6236 }
6237#endif
6238
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006239#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006240 ssl_key_cert_free( ssl->key_cert );
6241#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006242
Paul Bakker05ef8352012-05-08 09:17:57 +00006243#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6244 if( ssl_hw_record_finish != NULL )
6245 {
6246 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6247 ssl_hw_record_finish( ssl );
6248 }
6249#endif
6250
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006251#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006252 polarssl_free( ssl->cli_id );
6253#endif
6254
Paul Bakker5121ce52009-01-03 21:22:43 +00006255 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006256
Paul Bakker86f04f42013-02-14 11:20:09 +01006257 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006258 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006259}
6260
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006261#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006262/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006263 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006264 */
6265unsigned char ssl_sig_from_pk( pk_context *pk )
6266{
6267#if defined(POLARSSL_RSA_C)
6268 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6269 return( SSL_SIG_RSA );
6270#endif
6271#if defined(POLARSSL_ECDSA_C)
6272 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6273 return( SSL_SIG_ECDSA );
6274#endif
6275 return( SSL_SIG_ANON );
6276}
6277
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006278pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6279{
6280 switch( sig )
6281 {
6282#if defined(POLARSSL_RSA_C)
6283 case SSL_SIG_RSA:
6284 return( POLARSSL_PK_RSA );
6285#endif
6286#if defined(POLARSSL_ECDSA_C)
6287 case SSL_SIG_ECDSA:
6288 return( POLARSSL_PK_ECDSA );
6289#endif
6290 default:
6291 return( POLARSSL_PK_NONE );
6292 }
6293}
Paul Bakker9af723c2014-05-01 13:03:14 +02006294#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006295
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006296/*
6297 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6298 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006299md_type_t ssl_md_alg_from_hash( unsigned char hash )
6300{
6301 switch( hash )
6302 {
6303#if defined(POLARSSL_MD5_C)
6304 case SSL_HASH_MD5:
6305 return( POLARSSL_MD_MD5 );
6306#endif
6307#if defined(POLARSSL_SHA1_C)
6308 case SSL_HASH_SHA1:
6309 return( POLARSSL_MD_SHA1 );
6310#endif
6311#if defined(POLARSSL_SHA256_C)
6312 case SSL_HASH_SHA224:
6313 return( POLARSSL_MD_SHA224 );
6314 case SSL_HASH_SHA256:
6315 return( POLARSSL_MD_SHA256 );
6316#endif
6317#if defined(POLARSSL_SHA512_C)
6318 case SSL_HASH_SHA384:
6319 return( POLARSSL_MD_SHA384 );
6320 case SSL_HASH_SHA512:
6321 return( POLARSSL_MD_SHA512 );
6322#endif
6323 default:
6324 return( POLARSSL_MD_NONE );
6325 }
6326}
6327
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006328#if defined(POLARSSL_SSL_SET_CURVES)
6329/*
6330 * Check is a curve proposed by the peer is in our list.
6331 * Return 1 if we're willing to use it, 0 otherwise.
6332 */
6333int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6334{
6335 const ecp_group_id *gid;
6336
6337 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6338 if( *gid == grp_id )
6339 return( 1 );
6340
6341 return( 0 );
6342}
Paul Bakker9af723c2014-05-01 13:03:14 +02006343#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006344
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006345#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006346int ssl_check_cert_usage( const x509_crt *cert,
6347 const ssl_ciphersuite_t *ciphersuite,
6348 int cert_endpoint )
6349{
6350#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6351 int usage = 0;
6352#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006353#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6354 const char *ext_oid;
6355 size_t ext_len;
6356#endif
6357
6358#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6359 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6360 ((void) cert);
6361 ((void) cert_endpoint);
6362#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006363
6364#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6365 if( cert_endpoint == SSL_IS_SERVER )
6366 {
6367 /* Server part of the key exchange */
6368 switch( ciphersuite->key_exchange )
6369 {
6370 case POLARSSL_KEY_EXCHANGE_RSA:
6371 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6372 usage = KU_KEY_ENCIPHERMENT;
6373 break;
6374
6375 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6376 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6377 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6378 usage = KU_DIGITAL_SIGNATURE;
6379 break;
6380
6381 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6382 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6383 usage = KU_KEY_AGREEMENT;
6384 break;
6385
6386 /* Don't use default: we want warnings when adding new values */
6387 case POLARSSL_KEY_EXCHANGE_NONE:
6388 case POLARSSL_KEY_EXCHANGE_PSK:
6389 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6390 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6391 usage = 0;
6392 }
6393 }
6394 else
6395 {
6396 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6397 usage = KU_DIGITAL_SIGNATURE;
6398 }
6399
6400 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6401 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006402#else
6403 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006404#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6405
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006406#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6407 if( cert_endpoint == SSL_IS_SERVER )
6408 {
6409 ext_oid = OID_SERVER_AUTH;
6410 ext_len = OID_SIZE( OID_SERVER_AUTH );
6411 }
6412 else
6413 {
6414 ext_oid = OID_CLIENT_AUTH;
6415 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6416 }
6417
6418 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6419 return( -1 );
6420#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6421
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006422 return( 0 );
6423}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006424#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006425
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006426/*
6427 * Convert version numbers to/from wire format
6428 * and, for DTLS, to/from TLS equivalent.
6429 *
6430 * For TLS this is the identity.
6431 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6432 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6433 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6434 */
6435void ssl_write_version( int major, int minor, int transport,
6436 unsigned char ver[2] )
6437{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006438#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006439 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006440 {
6441 if( minor == SSL_MINOR_VERSION_2 )
6442 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6443
6444 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6445 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6446 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006447 else
6448#else
6449 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006450#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006451 {
6452 ver[0] = (unsigned char) major;
6453 ver[1] = (unsigned char) minor;
6454 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006455}
6456
6457void ssl_read_version( int *major, int *minor, int transport,
6458 const unsigned char ver[2] )
6459{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006460#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006461 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006462 {
6463 *major = 255 - ver[0] + 2;
6464 *minor = 255 - ver[1] + 1;
6465
6466 if( *minor == SSL_MINOR_VERSION_1 )
6467 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6468 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006469 else
6470#else
6471 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006472#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006473 {
6474 *major = ver[0];
6475 *minor = ver[1];
6476 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006477}
6478
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006479#endif /* POLARSSL_SSL_TLS_C */