blob: 3206a734e20763933c3a622c311fedb54bc2bb32 [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é-Gonnard246c13a2014-09-24 13:56:09 +02003241 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003242 goto read_record_header;
3243 }
3244
3245 return( ret );
3246 }
3247 else
3248#endif
3249 {
3250 /* Error out (and send alert) on invalid records */
3251#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3252 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3253 {
3254 ssl_send_alert_message( ssl,
3255 SSL_ALERT_LEVEL_FATAL,
3256 SSL_ALERT_MSG_BAD_RECORD_MAC );
3257 }
3258#endif
3259 return( ret );
3260 }
3261 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003262
3263 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003264 * When we sent the last flight of the handshake, we MUST respond to a
3265 * retransmit of the peer's previous flight with a retransmit. (In
3266 * practice, only the Finished message will make it, other messages
3267 * including CCS use the old transform so they're dropped as invalid.)
3268 *
3269 * If the record we received is not a handshake message, however, it
3270 * means the peer received our last flight so we can clean up
3271 * handshake info.
3272 *
3273 * This check needs to be done before prepare_handshake() due to an edge
3274 * case: if the client immediately requests renegotiation, this
3275 * finishes the current handshake first, avoiding the new ClientHello
3276 * being mistaken for an ancient message in the current handshake.
3277 */
3278#if defined(POLARSSL_SSL_PROTO_DTLS)
3279 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3280 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003281 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003282 {
3283 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3284 ssl->in_msg[0] == SSL_HS_FINISHED )
3285 {
3286 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3287
3288 if( ( ret = ssl_resend( ssl ) ) != 0 )
3289 {
3290 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3291 return( ret );
3292 }
3293
3294 return( POLARSSL_ERR_NET_WANT_READ );
3295 }
3296 else
3297 {
3298 ssl_handshake_wrapup_free_hs_transform( ssl );
3299 }
3300 }
3301#endif
3302
3303 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003304 * Handle particular types of records
3305 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3307 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003308 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3309 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 }
3311
3312 if( ssl->in_msgtype == SSL_MSG_ALERT )
3313 {
3314 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3315 ssl->in_msg[0], ssl->in_msg[1] ) );
3316
3317 /*
3318 * Ignore non-fatal alerts, except close_notify
3319 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003320 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003321 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003322 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3323 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003324 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 }
3326
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003327 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3328 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 {
3330 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003331 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 }
3333 }
3334
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3336
3337 return( 0 );
3338}
3339
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003340int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3341{
3342 int ret;
3343
3344 if( ( ret = ssl_send_alert_message( ssl,
3345 SSL_ALERT_LEVEL_FATAL,
3346 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3347 {
3348 return( ret );
3349 }
3350
3351 return( 0 );
3352}
3353
Paul Bakker0a925182012-04-16 06:46:41 +00003354int ssl_send_alert_message( ssl_context *ssl,
3355 unsigned char level,
3356 unsigned char message )
3357{
3358 int ret;
3359
3360 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3361
3362 ssl->out_msgtype = SSL_MSG_ALERT;
3363 ssl->out_msglen = 2;
3364 ssl->out_msg[0] = level;
3365 ssl->out_msg[1] = message;
3366
3367 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3368 {
3369 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3370 return( ret );
3371 }
3372
3373 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3374
3375 return( 0 );
3376}
3377
Paul Bakker5121ce52009-01-03 21:22:43 +00003378/*
3379 * Handshake functions
3380 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003381#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3382 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3383 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3384 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3385 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3386 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3387 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003388int ssl_write_certificate( ssl_context *ssl )
3389{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003390 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
3392 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3393
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003394 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003395 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3396 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003397 {
3398 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3399 ssl->state++;
3400 return( 0 );
3401 }
3402
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003403 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3404 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003405}
3406
3407int ssl_parse_certificate( ssl_context *ssl )
3408{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3410
3411 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3412
3413 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003414 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3415 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003416 {
3417 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3418 ssl->state++;
3419 return( 0 );
3420 }
3421
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003422 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3423 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003424}
3425#else
3426int ssl_write_certificate( ssl_context *ssl )
3427{
3428 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3429 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003430 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003431 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3432
3433 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3434
3435 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003436 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3437 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003438 {
3439 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3440 ssl->state++;
3441 return( 0 );
3442 }
3443
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 if( ssl->endpoint == SSL_IS_CLIENT )
3445 {
3446 if( ssl->client_auth == 0 )
3447 {
3448 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3449 ssl->state++;
3450 return( 0 );
3451 }
3452
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003453#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 /*
3455 * If using SSLv3 and got no cert, send an Alert message
3456 * (otherwise an empty Certificate message will be sent).
3457 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003458 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3460 {
3461 ssl->out_msglen = 2;
3462 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003463 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3464 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
3466 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3467 goto write_msg;
3468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003469#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 }
3471 else /* SSL_IS_SERVER */
3472 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003473 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 {
3475 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003476 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 }
3478 }
3479
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003480 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481
3482 /*
3483 * 0 . 0 handshake type
3484 * 1 . 3 handshake length
3485 * 4 . 6 length of all certs
3486 * 7 . 9 length of cert. 1
3487 * 10 . n-1 peer certificate
3488 * n . n+2 length of cert. 2
3489 * n+3 . ... upper level cert, etc.
3490 */
3491 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003492 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003493
Paul Bakker29087132010-03-21 21:03:34 +00003494 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003495 {
3496 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003497 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 {
3499 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3500 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003501 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 }
3503
3504 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3505 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3506 ssl->out_msg[i + 2] = (unsigned char)( n );
3507
3508 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3509 i += n; crt = crt->next;
3510 }
3511
3512 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3513 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3514 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3515
3516 ssl->out_msglen = i;
3517 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3518 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3519
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003520#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003521write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
3524 ssl->state++;
3525
3526 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3527 {
3528 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3529 return( ret );
3530 }
3531
3532 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3533
Paul Bakkered27a042013-04-18 22:46:23 +02003534 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003535}
3536
3537int ssl_parse_certificate( ssl_context *ssl )
3538{
Paul Bakkered27a042013-04-18 22:46:23 +02003539 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003540 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003541 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
3543 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3544
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003545 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003546 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3547 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003548 {
3549 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3550 ssl->state++;
3551 return( 0 );
3552 }
3553
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003555 ( ssl->authmode == SSL_VERIFY_NONE ||
3556 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003558 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3560 ssl->state++;
3561 return( 0 );
3562 }
3563
3564 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3565 {
3566 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3567 return( ret );
3568 }
3569
3570 ssl->state++;
3571
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003572#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 /*
3574 * Check if the client sent an empty certificate
3575 */
3576 if( ssl->endpoint == SSL_IS_SERVER &&
3577 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3578 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003579 if( ssl->in_msglen == 2 &&
3580 ssl->in_msgtype == SSL_MSG_ALERT &&
3581 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3582 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 {
3584 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3585
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003586 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3588 return( 0 );
3589 else
Paul Bakker40e46942009-01-03 21:51:57 +00003590 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 }
3592 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003593#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003594
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003595#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3596 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 if( ssl->endpoint == SSL_IS_SERVER &&
3598 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3599 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003600 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3602 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003603 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 {
3605 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3606
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003607 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003609 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 else
3611 return( 0 );
3612 }
3613 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003614#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3615 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003616
3617 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3618 {
3619 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003620 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 }
3622
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003623 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3624 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 {
3626 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003627 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003628 }
3629
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003630 i = ssl_hs_hdr_len( ssl );
3631
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 /*
3633 * Same message structure as in ssl_write_certificate()
3634 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003635 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003637 if( ssl->in_msg[i] != 0 ||
3638 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 {
3640 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003641 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 }
3643
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003644 /* In case we tried to reuse a session but it failed */
3645 if( ssl->session_negotiate->peer_cert != NULL )
3646 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003647 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003648 polarssl_free( ssl->session_negotiate->peer_cert );
3649 }
3650
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003651 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3652 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 {
3654 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003655 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003656 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 }
3658
Paul Bakkerb6b09562013-09-18 14:17:41 +02003659 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003660
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003661 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003662
3663 while( i < ssl->in_hslen )
3664 {
3665 if( ssl->in_msg[i] != 0 )
3666 {
3667 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003668 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003669 }
3670
3671 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3672 | (unsigned int) ssl->in_msg[i + 2];
3673 i += 3;
3674
3675 if( n < 128 || i + n > ssl->in_hslen )
3676 {
3677 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003678 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 }
3680
Paul Bakkerddf26b42013-09-18 13:46:23 +02003681 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3682 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 if( ret != 0 )
3684 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003685 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 return( ret );
3687 }
3688
3689 i += n;
3690 }
3691
Paul Bakker48916f92012-09-16 19:57:18 +00003692 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003693
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003694 /*
3695 * On client, make sure the server cert doesn't change during renego to
3696 * avoid "triple handshake" attack: https://secure-resumption.com/
3697 */
3698 if( ssl->endpoint == SSL_IS_CLIENT &&
3699 ssl->renegotiation == SSL_RENEGOTIATION )
3700 {
3701 if( ssl->session->peer_cert == NULL )
3702 {
3703 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3704 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3705 }
3706
3707 if( ssl->session->peer_cert->raw.len !=
3708 ssl->session_negotiate->peer_cert->raw.len ||
3709 memcmp( ssl->session->peer_cert->raw.p,
3710 ssl->session_negotiate->peer_cert->raw.p,
3711 ssl->session->peer_cert->raw.len ) != 0 )
3712 {
3713 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3714 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3715 }
3716 }
3717
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 if( ssl->authmode != SSL_VERIFY_NONE )
3719 {
3720 if( ssl->ca_chain == NULL )
3721 {
3722 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003723 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724 }
3725
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003726 /*
3727 * Main check: verify certificate
3728 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003729 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3730 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3731 &ssl->session_negotiate->verify_result,
3732 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003733
3734 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003735 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003736 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003737 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003738
3739 /*
3740 * Secondary checks: always done, but change 'ret' only if it was 0
3741 */
3742
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003743#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003744 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003745 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003746
3747 /* If certificate uses an EC key, make sure the curve is OK */
3748 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3749 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3750 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003751 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003752 if( ret == 0 )
3753 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003754 }
3755 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003756#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003757
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003758 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3759 ciphersuite_info,
3760 ! ssl->endpoint ) != 0 )
3761 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003762 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003763 if( ret == 0 )
3764 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3765 }
3766
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3768 ret = 0;
3769 }
3770
3771 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3772
3773 return( ret );
3774}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003775#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3776 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3777 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3778 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3779 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3780 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3781 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
3783int ssl_write_change_cipher_spec( ssl_context *ssl )
3784{
3785 int ret;
3786
3787 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3788
3789 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3790 ssl->out_msglen = 1;
3791 ssl->out_msg[0] = 1;
3792
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 ssl->state++;
3794
3795 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3796 {
3797 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3798 return( ret );
3799 }
3800
3801 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3802
3803 return( 0 );
3804}
3805
3806int ssl_parse_change_cipher_spec( ssl_context *ssl )
3807{
3808 int ret;
3809
3810 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3811
Paul Bakker5121ce52009-01-03 21:22:43 +00003812 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3813 {
3814 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3815 return( ret );
3816 }
3817
3818 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3819 {
3820 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003821 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003822 }
3823
3824 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3825 {
3826 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003827 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003828 }
3829
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003830 /*
3831 * Switch to our negotiated transform and session parameters for inbound
3832 * data.
3833 */
3834 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3835 ssl->transform_in = ssl->transform_negotiate;
3836 ssl->session_in = ssl->session_negotiate;
3837
3838#if defined(POLARSSL_SSL_PROTO_DTLS)
3839 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3840 {
3841#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3842 ssl_dtls_replay_reset( ssl );
3843#endif
3844
3845 /* Increment epoch */
3846 if( ++ssl->in_epoch == 0 )
3847 {
3848 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3849 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3850 }
3851 }
3852 else
3853#endif /* POLARSSL_SSL_PROTO_DTLS */
3854 memset( ssl->in_ctr, 0, 8 );
3855
3856 /*
3857 * Set the in_msg pointer to the correct location based on IV length
3858 */
3859 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3860 {
3861 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3862 ssl->transform_negotiate->fixed_ivlen;
3863 }
3864 else
3865 ssl->in_msg = ssl->in_iv;
3866
3867#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3868 if( ssl_hw_record_activate != NULL )
3869 {
3870 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3871 {
3872 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3873 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3874 }
3875 }
3876#endif
3877
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 ssl->state++;
3879
3880 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3881
3882 return( 0 );
3883}
3884
Paul Bakker41c83d32013-03-20 14:39:14 +01003885void ssl_optimize_checksum( ssl_context *ssl,
3886 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003887{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003888 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003889
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003890#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3891 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003892 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003893 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003894 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003895#endif
3896#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3897#if defined(POLARSSL_SHA512_C)
3898 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3899 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3900 else
3901#endif
3902#if defined(POLARSSL_SHA256_C)
3903 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003904 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003905 else
3906#endif
3907#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003908 {
3909 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003910 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003911 }
Paul Bakker380da532012-04-18 16:10:25 +00003912}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003913
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003914void ssl_reset_checksum( ssl_context *ssl )
3915{
3916#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3917 defined(POLARSSL_SSL_PROTO_TLS1_1)
3918 md5_starts( &ssl->handshake->fin_md5 );
3919 sha1_starts( &ssl->handshake->fin_sha1 );
3920#endif
3921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3922#if defined(POLARSSL_SHA256_C)
3923 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3924#endif
3925#if defined(POLARSSL_SHA512_C)
3926 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3927#endif
3928#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3929}
3930
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003931static void ssl_update_checksum_start( ssl_context *ssl,
3932 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003933{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003934#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3935 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003936 md5_update( &ssl->handshake->fin_md5 , buf, len );
3937 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003938#endif
3939#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3940#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003941 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003942#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003943#if defined(POLARSSL_SHA512_C)
3944 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003945#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003946#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003947}
3948
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003949#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3950 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003951static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3952 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003953{
Paul Bakker48916f92012-09-16 19:57:18 +00003954 md5_update( &ssl->handshake->fin_md5 , buf, len );
3955 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003956}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003957#endif
Paul Bakker380da532012-04-18 16:10:25 +00003958
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003959#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3960#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003961static void ssl_update_checksum_sha256( ssl_context *ssl,
3962 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003963{
Paul Bakker9e36f042013-06-30 14:34:05 +02003964 sha256_update( &ssl->handshake->fin_sha256, 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 Bakker9e36f042013-06-30 14:34:05 +02003968#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003969static void ssl_update_checksum_sha384( ssl_context *ssl,
3970 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003971{
Paul Bakker9e36f042013-06-30 14:34:05 +02003972 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003973}
Paul Bakker769075d2012-11-24 11:26:46 +01003974#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003975#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003976
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003977#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003978static void ssl_calc_finished_ssl(
3979 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003980{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003981 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003982 md5_context md5;
3983 sha1_context sha1;
3984
Paul Bakker5121ce52009-01-03 21:22:43 +00003985 unsigned char padbuf[48];
3986 unsigned char md5sum[16];
3987 unsigned char sha1sum[20];
3988
Paul Bakker48916f92012-09-16 19:57:18 +00003989 ssl_session *session = ssl->session_negotiate;
3990 if( !session )
3991 session = ssl->session;
3992
Paul Bakker1ef83d62012-04-11 12:09:53 +00003993 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3994
Paul Bakker48916f92012-09-16 19:57:18 +00003995 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3996 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003997
3998 /*
3999 * SSLv3:
4000 * hash =
4001 * MD5( master + pad2 +
4002 * MD5( handshake + sender + master + pad1 ) )
4003 * + SHA1( master + pad2 +
4004 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 */
4006
Paul Bakker90995b52013-06-24 19:20:35 +02004007#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004008 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004009 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004010#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
Paul Bakker90995b52013-06-24 19:20:35 +02004012#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004013 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004014 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004015#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004016
Paul Bakker3c2122f2013-06-24 19:03:14 +02004017 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4018 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
Paul Bakker1ef83d62012-04-11 12:09:53 +00004020 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
Paul Bakker3c2122f2013-06-24 19:03:14 +02004022 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004023 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024 md5_update( &md5, padbuf, 48 );
4025 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004026
Paul Bakker3c2122f2013-06-24 19:03:14 +02004027 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004028 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004029 sha1_update( &sha1, padbuf, 40 );
4030 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004031
Paul Bakker1ef83d62012-04-11 12:09:53 +00004032 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004033
Paul Bakker1ef83d62012-04-11 12:09:53 +00004034 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004035 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004036 md5_update( &md5, padbuf, 48 );
4037 md5_update( &md5, md5sum, 16 );
4038 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004039
Paul Bakker1ef83d62012-04-11 12:09:53 +00004040 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004041 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004042 sha1_update( &sha1, padbuf , 40 );
4043 sha1_update( &sha1, sha1sum, 20 );
4044 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004045
Paul Bakker1ef83d62012-04-11 12:09:53 +00004046 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004047
Paul Bakker5b4af392014-06-26 12:09:34 +02004048 md5_free( &md5 );
4049 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004050
Paul Bakker34617722014-06-13 17:20:13 +02004051 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4052 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4053 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054
4055 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4056}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004057#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004059#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004060static void ssl_calc_finished_tls(
4061 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004062{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004063 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004064 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004065 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004066 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004067 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004068
Paul Bakker48916f92012-09-16 19:57:18 +00004069 ssl_session *session = ssl->session_negotiate;
4070 if( !session )
4071 session = ssl->session;
4072
Paul Bakker1ef83d62012-04-11 12:09:53 +00004073 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004074
Paul Bakker48916f92012-09-16 19:57:18 +00004075 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4076 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004077
Paul Bakker1ef83d62012-04-11 12:09:53 +00004078 /*
4079 * TLSv1:
4080 * hash = PRF( master, finished_label,
4081 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4082 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004083
Paul Bakker90995b52013-06-24 19:20:35 +02004084#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004085 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4086 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004087#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004088
Paul Bakker90995b52013-06-24 19:20:35 +02004089#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004090 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4091 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004092#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004093
4094 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004095 ? "client finished"
4096 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004097
4098 md5_finish( &md5, padbuf );
4099 sha1_finish( &sha1, padbuf + 16 );
4100
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004101 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004102 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004103
4104 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4105
Paul Bakker5b4af392014-06-26 12:09:34 +02004106 md5_free( &md5 );
4107 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004108
Paul Bakker34617722014-06-13 17:20:13 +02004109 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004110
4111 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4112}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004113#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004115#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4116#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004117static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004118 ssl_context *ssl, unsigned char *buf, int from )
4119{
4120 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004121 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004122 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004123 unsigned char padbuf[32];
4124
Paul Bakker48916f92012-09-16 19:57:18 +00004125 ssl_session *session = ssl->session_negotiate;
4126 if( !session )
4127 session = ssl->session;
4128
Paul Bakker380da532012-04-18 16:10:25 +00004129 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004130
Paul Bakker9e36f042013-06-30 14:34:05 +02004131 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004132
4133 /*
4134 * TLSv1.2:
4135 * hash = PRF( master, finished_label,
4136 * Hash( handshake ) )[0.11]
4137 */
4138
Paul Bakker9e36f042013-06-30 14:34:05 +02004139#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004140 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004141 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004142#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004143
4144 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004145 ? "client finished"
4146 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004147
Paul Bakker9e36f042013-06-30 14:34:05 +02004148 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004149
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004150 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004151 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004152
4153 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4154
Paul Bakker5b4af392014-06-26 12:09:34 +02004155 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004156
Paul Bakker34617722014-06-13 17:20:13 +02004157 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004158
4159 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4160}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004161#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004162
Paul Bakker9e36f042013-06-30 14:34:05 +02004163#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004164static void ssl_calc_finished_tls_sha384(
4165 ssl_context *ssl, unsigned char *buf, int from )
4166{
4167 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004168 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004169 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004170 unsigned char padbuf[48];
4171
Paul Bakker48916f92012-09-16 19:57:18 +00004172 ssl_session *session = ssl->session_negotiate;
4173 if( !session )
4174 session = ssl->session;
4175
Paul Bakker380da532012-04-18 16:10:25 +00004176 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004177
Paul Bakker9e36f042013-06-30 14:34:05 +02004178 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004179
4180 /*
4181 * TLSv1.2:
4182 * hash = PRF( master, finished_label,
4183 * Hash( handshake ) )[0.11]
4184 */
4185
Paul Bakker9e36f042013-06-30 14:34:05 +02004186#if !defined(POLARSSL_SHA512_ALT)
4187 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4188 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004189#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004190
4191 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004192 ? "client finished"
4193 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004194
Paul Bakker9e36f042013-06-30 14:34:05 +02004195 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004196
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004197 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004198 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004199
4200 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4201
Paul Bakker5b4af392014-06-26 12:09:34 +02004202 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004203
Paul Bakker34617722014-06-13 17:20:13 +02004204 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004205
4206 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4207}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004208#endif /* POLARSSL_SHA512_C */
4209#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004210
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004211static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004212{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004213 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004214
4215 /*
4216 * Free our handshake params
4217 */
4218 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004219 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004220 ssl->handshake = NULL;
4221
4222 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004223 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004224 */
4225 if( ssl->transform )
4226 {
4227 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004228 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004229 }
4230 ssl->transform = ssl->transform_negotiate;
4231 ssl->transform_negotiate = NULL;
4232
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004233 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4234}
4235
4236void ssl_handshake_wrapup( ssl_context *ssl )
4237{
4238 int resume = ssl->handshake->resume;
4239
4240 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4241
4242 if( ssl->renegotiation == SSL_RENEGOTIATION )
4243 {
4244 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4245 ssl->renego_records_seen = 0;
4246 }
4247
4248 /*
4249 * Free the previous session and switch in the current one
4250 */
Paul Bakker0a597072012-09-25 21:55:46 +00004251 if( ssl->session )
4252 {
4253 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004254 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004255 }
4256 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004257 ssl->session_negotiate = NULL;
4258
Paul Bakker0a597072012-09-25 21:55:46 +00004259 /*
4260 * Add cache entry
4261 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004262 if( ssl->f_set_cache != NULL &&
4263 ssl->session->length != 0 &&
4264 resume == 0 )
4265 {
Paul Bakker0a597072012-09-25 21:55:46 +00004266 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4267 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004268 }
Paul Bakker0a597072012-09-25 21:55:46 +00004269
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004270#if defined(POLARSSL_SSL_PROTO_DTLS)
4271 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4272 ssl->handshake->flight != NULL )
4273 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004274 /* Cancel handshake timer */
4275 ssl_set_timer( ssl, 0 );
4276
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004277 /* Keep last flight around in case we need to resend it:
4278 * we need the handshake and transform structures for that */
4279 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4280 }
4281 else
4282#endif
4283 ssl_handshake_wrapup_free_hs_transform( ssl );
4284
Paul Bakker48916f92012-09-16 19:57:18 +00004285 ssl->state++;
4286
4287 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4288}
4289
Paul Bakker1ef83d62012-04-11 12:09:53 +00004290int ssl_write_finished( ssl_context *ssl )
4291{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004292 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004293
4294 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4295
Paul Bakker92be97b2013-01-02 17:30:03 +01004296 /*
4297 * Set the out_msg pointer to the correct location based on IV length
4298 */
4299 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4300 {
4301 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4302 ssl->transform_negotiate->fixed_ivlen;
4303 }
4304 else
4305 ssl->out_msg = ssl->out_iv;
4306
Paul Bakker48916f92012-09-16 19:57:18 +00004307 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004308
4309 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004310 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4311
Paul Bakker48916f92012-09-16 19:57:18 +00004312 ssl->verify_data_len = hash_len;
4313 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4314
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 ssl->out_msglen = 4 + hash_len;
4316 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4317 ssl->out_msg[0] = SSL_HS_FINISHED;
4318
4319 /*
4320 * In case of session resuming, invert the client and server
4321 * ChangeCipherSpec messages order.
4322 */
Paul Bakker0a597072012-09-25 21:55:46 +00004323 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 {
4325 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004326 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004327 else
4328 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4329 }
4330 else
4331 ssl->state++;
4332
Paul Bakker48916f92012-09-16 19:57:18 +00004333 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004334 * Switch to our negotiated transform and session parameters for outbound
4335 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004336 */
4337 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004338
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004339#if defined(POLARSSL_SSL_PROTO_DTLS)
4340 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4341 {
4342 unsigned char i;
4343
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004344 /* Remember current epoch settings for resending */
4345 ssl->handshake->alt_transform_out = ssl->transform_out;
4346 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4347
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004348 /* Set sequence_number to zero */
4349 memset( ssl->out_ctr + 2, 0, 6 );
4350
4351 /* Increment epoch */
4352 for( i = 2; i > 0; i-- )
4353 if( ++ssl->out_ctr[i - 1] != 0 )
4354 break;
4355
4356 /* The loop goes to its end iff the counter is wrapping */
4357 if( i == 0 )
4358 {
4359 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4360 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4361 }
4362 }
4363 else
4364#endif /* POLARSSL_SSL_PROTO_DTLS */
4365 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004366
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004367 ssl->transform_out = ssl->transform_negotiate;
4368 ssl->session_out = ssl->session_negotiate;
4369
Paul Bakker07eb38b2012-12-19 14:42:06 +01004370#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004371 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004372 {
4373 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4374 {
4375 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4376 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4377 }
4378 }
4379#endif
4380
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004381#if defined(POLARSSL_SSL_PROTO_DTLS)
4382 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4383 ssl_send_flight_completed( ssl );
4384#endif
4385
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4387 {
4388 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4389 return( ret );
4390 }
4391
4392 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4393
4394 return( 0 );
4395}
4396
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004397#if defined(POLARSSL_SSL_PROTO_SSL3)
4398#define SSL_MAX_HASH_LEN 36
4399#else
4400#define SSL_MAX_HASH_LEN 12
4401#endif
4402
Paul Bakker5121ce52009-01-03 21:22:43 +00004403int ssl_parse_finished( ssl_context *ssl )
4404{
Paul Bakker23986e52011-04-24 08:57:21 +00004405 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004406 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004407 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004408
4409 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4410
Paul Bakker48916f92012-09-16 19:57:18 +00004411 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004412
Paul Bakker5121ce52009-01-03 21:22:43 +00004413 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4414 {
4415 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4416 return( ret );
4417 }
4418
4419 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4420 {
4421 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004422 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
4424
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004425 /* There is currently no ciphersuite using another length with TLS 1.2 */
4426#if defined(POLARSSL_SSL_PROTO_SSL3)
4427 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4428 hash_len = 36;
4429 else
4430#endif
4431 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004432
4433 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004434 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 {
4436 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004437 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004438 }
4439
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004440 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4441 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 {
4443 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004444 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004445 }
4446
Paul Bakker48916f92012-09-16 19:57:18 +00004447 ssl->verify_data_len = hash_len;
4448 memcpy( ssl->peer_verify_data, buf, hash_len );
4449
Paul Bakker0a597072012-09-25 21:55:46 +00004450 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004451 {
4452 if( ssl->endpoint == SSL_IS_CLIENT )
4453 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4454
4455 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004456 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004457 }
4458 else
4459 ssl->state++;
4460
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004461#if defined(POLARSSL_SSL_PROTO_DTLS)
4462 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4463 ssl_recv_flight_completed( ssl );
4464#endif
4465
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4467
4468 return( 0 );
4469}
4470
Paul Bakker968afaa2014-07-09 11:09:24 +02004471static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004472{
4473 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4474
4475#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4476 defined(POLARSSL_SSL_PROTO_TLS1_1)
4477 md5_init( &handshake->fin_md5 );
4478 sha1_init( &handshake->fin_sha1 );
4479 md5_starts( &handshake->fin_md5 );
4480 sha1_starts( &handshake->fin_sha1 );
4481#endif
4482#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4483#if defined(POLARSSL_SHA256_C)
4484 sha256_init( &handshake->fin_sha256 );
4485 sha256_starts( &handshake->fin_sha256, 0 );
4486#endif
4487#if defined(POLARSSL_SHA512_C)
4488 sha512_init( &handshake->fin_sha512 );
4489 sha512_starts( &handshake->fin_sha512, 1 );
4490#endif
4491#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4492
4493 handshake->update_checksum = ssl_update_checksum_start;
4494 handshake->sig_alg = SSL_HASH_SHA1;
4495
4496#if defined(POLARSSL_DHM_C)
4497 dhm_init( &handshake->dhm_ctx );
4498#endif
4499#if defined(POLARSSL_ECDH_C)
4500 ecdh_init( &handshake->ecdh_ctx );
4501#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004502}
4503
4504static void ssl_transform_init( ssl_transform *transform )
4505{
4506 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004507
4508 cipher_init( &transform->cipher_ctx_enc );
4509 cipher_init( &transform->cipher_ctx_dec );
4510
4511 md_init( &transform->md_ctx_enc );
4512 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004513}
4514
4515void ssl_session_init( ssl_session *session )
4516{
4517 memset( session, 0, sizeof(ssl_session) );
4518}
4519
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004520static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004521{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004522 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004523 if( ssl->transform_negotiate )
4524 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004525 if( ssl->session_negotiate )
4526 ssl_session_free( ssl->session_negotiate );
4527 if( ssl->handshake )
4528 ssl_handshake_free( ssl->handshake );
4529
4530 /*
4531 * Either the pointers are now NULL or cleared properly and can be freed.
4532 * Now allocate missing structures.
4533 */
4534 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004535 {
4536 ssl->transform_negotiate =
4537 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4538 }
Paul Bakker48916f92012-09-16 19:57:18 +00004539
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004540 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004541 {
4542 ssl->session_negotiate =
4543 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4544 }
Paul Bakker48916f92012-09-16 19:57:18 +00004545
Paul Bakker82788fb2014-10-20 13:59:19 +02004546 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004547 {
4548 ssl->handshake = (ssl_handshake_params *)
4549 polarssl_malloc( sizeof(ssl_handshake_params) );
4550 }
Paul Bakker48916f92012-09-16 19:57:18 +00004551
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004552 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004553 if( ssl->handshake == NULL ||
4554 ssl->transform_negotiate == NULL ||
4555 ssl->session_negotiate == NULL )
4556 {
4557 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004558
4559 polarssl_free( ssl->handshake );
4560 polarssl_free( ssl->transform_negotiate );
4561 polarssl_free( ssl->session_negotiate );
4562
4563 ssl->handshake = NULL;
4564 ssl->transform_negotiate = NULL;
4565 ssl->session_negotiate = NULL;
4566
Paul Bakker48916f92012-09-16 19:57:18 +00004567 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4568 }
4569
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004570 /* Initialize structures */
4571 ssl_session_init( ssl->session_negotiate );
4572 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004573 ssl_handshake_params_init( ssl->handshake );
4574
4575#if defined(POLARSSL_X509_CRT_PARSE_C)
4576 ssl->handshake->key_cert = ssl->key_cert;
4577#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004578
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004579 /*
4580 * We may not know yet if we're using DTLS,
4581 * so always initiliase DTLS-specific fields.
4582 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004583#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004584 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004585
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004586 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004587 if( ssl->endpoint == SSL_IS_CLIENT )
4588 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4589 else
4590 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004591#endif
4592
Paul Bakker48916f92012-09-16 19:57:18 +00004593 return( 0 );
4594}
4595
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004596#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4597/* Dummy cookie callbacks for defaults */
4598static int ssl_cookie_write_dummy( void *ctx,
4599 unsigned char **p, unsigned char *end,
4600 const unsigned char *cli_id, size_t cli_id_len )
4601{
4602 ((void) ctx);
4603 ((void) p);
4604 ((void) end);
4605 ((void) cli_id);
4606 ((void) cli_id_len);
4607
4608 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4609}
4610
4611static int ssl_cookie_check_dummy( void *ctx,
4612 const unsigned char *cookie, size_t cookie_len,
4613 const unsigned char *cli_id, size_t cli_id_len )
4614{
4615 ((void) ctx);
4616 ((void) cookie);
4617 ((void) cookie_len);
4618 ((void) cli_id);
4619 ((void) cli_id_len);
4620
4621 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4622}
4623#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4624
Paul Bakker5121ce52009-01-03 21:22:43 +00004625/*
4626 * Initialize an SSL context
4627 */
4628int ssl_init( ssl_context *ssl )
4629{
Paul Bakker48916f92012-09-16 19:57:18 +00004630 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 int len = SSL_BUFFER_LEN;
4632
4633 memset( ssl, 0, sizeof( ssl_context ) );
4634
Paul Bakker62f2dee2012-09-28 07:31:51 +00004635 /*
4636 * Sane defaults
4637 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004638 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4639 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4640 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4641 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004642
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004643 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004644
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004645 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4646
Paul Bakker62f2dee2012-09-28 07:31:51 +00004647#if defined(POLARSSL_DHM_C)
4648 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4649 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4650 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4651 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4652 {
4653 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4654 return( ret );
4655 }
4656#endif
4657
4658 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004659 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004660 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004661 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004662 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004663
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004664 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004665 {
4666 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004667 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004668 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004669 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004670 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004671 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004672 }
4673
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004674 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4675 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004677 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4678 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4679
Paul Bakker606b4ba2013-08-14 16:52:14 +02004680#if defined(POLARSSL_SSL_SESSION_TICKETS)
4681 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4682#endif
4683
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004684#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004685 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004686#endif
4687
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004688#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4689 ssl->f_cookie_write = ssl_cookie_write_dummy;
4690 ssl->f_cookie_check = ssl_cookie_check_dummy;
4691#endif
4692
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004693#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4694 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4695#endif
4696
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004697#if defined(POLARSSL_SSL_PROTO_DTLS)
4698 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4699 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4700#endif
4701
Paul Bakker48916f92012-09-16 19:57:18 +00004702 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4703 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004704
4705 return( 0 );
4706}
4707
4708/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004709 * Reset an initialized and used SSL context for re-use while retaining
4710 * all application-set variables, function pointers and data.
4711 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004712int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004713{
Paul Bakker48916f92012-09-16 19:57:18 +00004714 int ret;
4715
Paul Bakker7eb013f2011-10-06 12:37:39 +00004716 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004717 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4718 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4719
4720 ssl->verify_data_len = 0;
4721 memset( ssl->own_verify_data, 0, 36 );
4722 memset( ssl->peer_verify_data, 0, 36 );
4723
Paul Bakker7eb013f2011-10-06 12:37:39 +00004724 ssl->in_offt = NULL;
4725
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004726 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004727 ssl->in_msgtype = 0;
4728 ssl->in_msglen = 0;
4729 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004730#if defined(POLARSSL_SSL_PROTO_DTLS)
4731 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004732 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004733#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004734#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4735 ssl_dtls_replay_reset( ssl );
4736#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004737
4738 ssl->in_hslen = 0;
4739 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004740 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004741
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004742 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004743 ssl->out_msgtype = 0;
4744 ssl->out_msglen = 0;
4745 ssl->out_left = 0;
4746
Paul Bakker48916f92012-09-16 19:57:18 +00004747 ssl->transform_in = NULL;
4748 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004749
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004750 ssl->renego_records_seen = 0;
4751
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004752 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4753 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004754
4755#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004756 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004757 {
4758 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004759 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004760 {
4761 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4762 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4763 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004764 }
4765#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004766
Paul Bakker48916f92012-09-16 19:57:18 +00004767 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004768 {
Paul Bakker48916f92012-09-16 19:57:18 +00004769 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004770 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004771 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004772 }
Paul Bakker48916f92012-09-16 19:57:18 +00004773
Paul Bakkerc0463502013-02-14 11:19:38 +01004774 if( ssl->session )
4775 {
4776 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004777 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004778 ssl->session = NULL;
4779 }
4780
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004781#if defined(POLARSSL_SSL_ALPN)
4782 ssl->alpn_chosen = NULL;
4783#endif
4784
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004785#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004786 polarssl_free( ssl->cli_id );
4787 ssl->cli_id = NULL;
4788 ssl->cli_id_len = 0;
4789#endif
4790
Paul Bakker48916f92012-09-16 19:57:18 +00004791 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4792 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004793
4794 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004795}
4796
Paul Bakkera503a632013-08-14 13:48:06 +02004797#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004798static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4799{
4800 aes_free( &tkeys->enc );
4801 aes_free( &tkeys->dec );
4802
4803 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4804}
4805
Paul Bakker7eb013f2011-10-06 12:37:39 +00004806/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004807 * Allocate and initialize ticket keys
4808 */
4809static int ssl_ticket_keys_init( ssl_context *ssl )
4810{
4811 int ret;
4812 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004813 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004814
4815 if( ssl->ticket_keys != NULL )
4816 return( 0 );
4817
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004818 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4819 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004820 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4821
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004822 aes_init( &tkeys->enc );
4823 aes_init( &tkeys->dec );
4824
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004825 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004826 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004827 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004828 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004829 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004830 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004831
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004832 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4833 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4834 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4835 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004836 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004837 polarssl_free( tkeys );
4838 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004839 }
4840
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004841 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004842 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004843 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004844 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004845 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004846 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004847
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004848 ssl->ticket_keys = tkeys;
4849
4850 return( 0 );
4851}
Paul Bakkera503a632013-08-14 13:48:06 +02004852#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004853
4854/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 * SSL set accessors
4856 */
4857void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4858{
4859 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004860
Paul Bakker606b4ba2013-08-14 16:52:14 +02004861#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004862 if( endpoint == SSL_IS_CLIENT )
4863 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004864#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004865}
4866
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004867int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004868{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004869#if defined(POLARSSL_SSL_PROTO_DTLS)
4870 if( transport == SSL_TRANSPORT_DATAGRAM )
4871 {
4872 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004873
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004874 ssl->out_hdr = ssl->out_buf;
4875 ssl->out_ctr = ssl->out_buf + 3;
4876 ssl->out_len = ssl->out_buf + 11;
4877 ssl->out_iv = ssl->out_buf + 13;
4878 ssl->out_msg = ssl->out_buf + 13;
4879
4880 ssl->in_hdr = ssl->in_buf;
4881 ssl->in_ctr = ssl->in_buf + 3;
4882 ssl->in_len = ssl->in_buf + 11;
4883 ssl->in_iv = ssl->in_buf + 13;
4884 ssl->in_msg = ssl->in_buf + 13;
4885
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004886 /* DTLS starts with TLS1.1 */
4887 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4888 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004889
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004890 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4891 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4892
4893 return( 0 );
4894 }
4895#endif
4896
4897 if( transport == SSL_TRANSPORT_STREAM )
4898 {
4899 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004900
4901 ssl->out_ctr = ssl->out_buf;
4902 ssl->out_hdr = ssl->out_buf + 8;
4903 ssl->out_len = ssl->out_buf + 11;
4904 ssl->out_iv = ssl->out_buf + 13;
4905 ssl->out_msg = ssl->out_buf + 13;
4906
4907 ssl->in_ctr = ssl->in_buf;
4908 ssl->in_hdr = ssl->in_buf + 8;
4909 ssl->in_len = ssl->in_buf + 11;
4910 ssl->in_iv = ssl->in_buf + 13;
4911 ssl->in_msg = ssl->in_buf + 13;
4912
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004913 return( 0 );
4914 }
4915
4916 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004917}
4918
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004919#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4920void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4921{
4922 ssl->anti_replay = mode;
4923}
4924#endif
4925
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004926#if defined(POLARSSL_SSL_PROTO_DTLS)
4927void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4928{
4929 ssl->hs_timeout_min = min;
4930 ssl->hs_timeout_max = max;
4931}
4932#endif
4933
Paul Bakker5121ce52009-01-03 21:22:43 +00004934void ssl_set_authmode( ssl_context *ssl, int authmode )
4935{
4936 ssl->authmode = authmode;
4937}
4938
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004939#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004940void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004941 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004942 void *p_vrfy )
4943{
4944 ssl->f_vrfy = f_vrfy;
4945 ssl->p_vrfy = p_vrfy;
4946}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004947#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004948
Paul Bakker5121ce52009-01-03 21:22:43 +00004949void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004950 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 void *p_rng )
4952{
4953 ssl->f_rng = f_rng;
4954 ssl->p_rng = p_rng;
4955}
4956
4957void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004958 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004959 void *p_dbg )
4960{
4961 ssl->f_dbg = f_dbg;
4962 ssl->p_dbg = p_dbg;
4963}
4964
4965void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004966 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004967 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004968{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004969 if( p_recv != p_send )
4970 {
4971 ssl->f_recv = NULL;
4972 ssl->f_send = NULL;
4973 ssl->p_bio = NULL;
4974 return;
4975 }
4976
Paul Bakker5121ce52009-01-03 21:22:43 +00004977 ssl->f_recv = f_recv;
4978 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004979 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004980}
4981
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004982void ssl_set_bio_timeout( ssl_context *ssl,
4983 void *p_bio,
4984 int (*f_send)(void *, const unsigned char *, size_t),
4985 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004986 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4987 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004988{
4989 ssl->p_bio = p_bio;
4990 ssl->f_send = f_send;
4991 ssl->f_recv = f_recv;
4992 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004993 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004994}
4995
Paul Bakker0a597072012-09-25 21:55:46 +00004996void ssl_set_session_cache( ssl_context *ssl,
4997 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4998 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004999{
Paul Bakker0a597072012-09-25 21:55:46 +00005000 ssl->f_get_cache = f_get_cache;
5001 ssl->p_get_cache = p_get_cache;
5002 ssl->f_set_cache = f_set_cache;
5003 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005004}
5005
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005006int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005007{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005008 int ret;
5009
5010 if( ssl == NULL ||
5011 session == NULL ||
5012 ssl->session_negotiate == NULL ||
5013 ssl->endpoint != SSL_IS_CLIENT )
5014 {
5015 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5016 }
5017
5018 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5019 return( ret );
5020
Paul Bakker0a597072012-09-25 21:55:46 +00005021 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005022
5023 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024}
5025
Paul Bakkerb68cad62012-08-23 08:34:18 +00005026void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005027{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005028 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5029 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5030 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5031 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5032}
5033
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005034void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5035 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005036 int major, int minor )
5037{
5038 if( major != SSL_MAJOR_VERSION_3 )
5039 return;
5040
5041 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5042 return;
5043
5044 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005045}
5046
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005047#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005048/* Add a new (empty) key_cert entry an return a pointer to it */
5049static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5050{
5051 ssl_key_cert *key_cert, *last;
5052
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005053 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5054 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005055 return( NULL );
5056
5057 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5058
5059 /* Append the new key_cert to the (possibly empty) current list */
5060 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005061 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005062 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005063 if( ssl->handshake != NULL )
5064 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005065 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005066 else
5067 {
5068 last = ssl->key_cert;
5069 while( last->next != NULL )
5070 last = last->next;
5071 last->next = key_cert;
5072 }
5073
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005074 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005075}
5076
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005077void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005078 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005079{
5080 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005081 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005082 ssl->peer_cn = peer_cn;
5083}
5084
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005085int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005086 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005087{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005088 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5089
5090 if( key_cert == NULL )
5091 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5092
5093 key_cert->cert = own_cert;
5094 key_cert->key = pk_key;
5095
5096 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005097}
5098
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005099#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005100int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005101 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005102{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005103 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005104 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005105
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005106 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005107 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5108
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005109 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5110 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005111 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005112
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005113 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005114
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005115 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005116 if( ret != 0 )
5117 return( ret );
5118
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005119 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005120 return( ret );
5121
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005122 key_cert->cert = own_cert;
5123 key_cert->key_own_alloc = 1;
5124
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005125 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005126}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005127#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005128
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005129int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005130 void *rsa_key,
5131 rsa_decrypt_func rsa_decrypt,
5132 rsa_sign_func rsa_sign,
5133 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005134{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005135 int ret;
5136 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005137
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005138 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005139 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5140
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005141 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5142 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005143 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005144
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005145 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005146
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005147 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5148 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5149 return( ret );
5150
5151 key_cert->cert = own_cert;
5152 key_cert->key_own_alloc = 1;
5153
5154 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005155}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005156#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005157
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005158#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005159int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5160 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005161{
Paul Bakker6db455e2013-09-18 17:29:31 +02005162 if( psk == NULL || psk_identity == NULL )
5163 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5164
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005165 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5167
Paul Bakker6db455e2013-09-18 17:29:31 +02005168 if( ssl->psk != NULL )
5169 {
5170 polarssl_free( ssl->psk );
5171 polarssl_free( ssl->psk_identity );
5172 }
5173
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005174 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005175 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005176
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005177 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005178 ssl->psk_identity = (unsigned char *)
5179 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005180
5181 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5182 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5183
5184 memcpy( ssl->psk, psk, ssl->psk_len );
5185 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005186
5187 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005188}
5189
5190void ssl_set_psk_cb( ssl_context *ssl,
5191 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5192 size_t),
5193 void *p_psk )
5194{
5195 ssl->f_psk = f_psk;
5196 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005197}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005198#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005199
Paul Bakker48916f92012-09-16 19:57:18 +00005200#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005201int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005202{
5203 int ret;
5204
Paul Bakker48916f92012-09-16 19:57:18 +00005205 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005206 {
5207 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5208 return( ret );
5209 }
5210
Paul Bakker48916f92012-09-16 19:57:18 +00005211 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005212 {
5213 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5214 return( ret );
5215 }
5216
5217 return( 0 );
5218}
5219
Paul Bakker1b57b062011-01-06 15:48:19 +00005220int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5221{
5222 int ret;
5223
Paul Bakker66d5d072014-06-17 16:39:18 +02005224 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005225 {
5226 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5227 return( ret );
5228 }
5229
Paul Bakker66d5d072014-06-17 16:39:18 +02005230 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005231 {
5232 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5233 return( ret );
5234 }
5235
5236 return( 0 );
5237}
Paul Bakker48916f92012-09-16 19:57:18 +00005238#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005239
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005240#if defined(POLARSSL_SSL_SET_CURVES)
5241/*
5242 * Set the allowed elliptic curves
5243 */
5244void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5245{
5246 ssl->curve_list = curve_list;
5247}
5248#endif
5249
Paul Bakker0be444a2013-08-27 21:55:01 +02005250#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005251int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005252{
5253 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005254 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005255
5256 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005257
5258 if( ssl->hostname_len + 1 == 0 )
5259 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5260
Paul Bakker6e339b52013-07-03 13:37:05 +02005261 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005262
Paul Bakkerb15b8512012-01-13 13:44:06 +00005263 if( ssl->hostname == NULL )
5264 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5265
Paul Bakker3c2122f2013-06-24 19:03:14 +02005266 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005268
Paul Bakker40ea7de2009-05-03 10:18:48 +00005269 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005270
5271 return( 0 );
5272}
5273
Paul Bakker5701cdc2012-09-27 21:49:42 +00005274void ssl_set_sni( ssl_context *ssl,
5275 int (*f_sni)(void *, ssl_context *,
5276 const unsigned char *, size_t),
5277 void *p_sni )
5278{
5279 ssl->f_sni = f_sni;
5280 ssl->p_sni = p_sni;
5281}
Paul Bakker0be444a2013-08-27 21:55:01 +02005282#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005283
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005284#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005285int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005286{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005287 size_t cur_len, tot_len;
5288 const char **p;
5289
5290 /*
5291 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5292 * truncated". Check lengths now rather than later.
5293 */
5294 tot_len = 0;
5295 for( p = protos; *p != NULL; p++ )
5296 {
5297 cur_len = strlen( *p );
5298 tot_len += cur_len;
5299
5300 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5301 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5302 }
5303
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005304 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005305
5306 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005307}
5308
5309const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5310{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005311 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005312}
5313#endif /* POLARSSL_SSL_ALPN */
5314
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005315static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005316{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005317 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005318 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005319 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005320 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005321 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005322
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005323#if defined(POLARSSL_SSL_PROTO_DTLS)
5324 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5325 minor < SSL_MINOR_VERSION_2 )
5326 {
5327 return( -1 );
5328 }
5329#else
5330 ((void) ssl);
5331#endif
5332
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005333 return( 0 );
5334}
5335
5336int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5337{
5338 if( ssl_check_version( ssl, major, minor ) != 0 )
5339 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5340
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005341 ssl->max_major_ver = major;
5342 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005343
5344 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005345}
5346
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005347int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005348{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005349 if( ssl_check_version( ssl, major, minor ) != 0 )
5350 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005351
5352 ssl->min_major_ver = major;
5353 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005354
5355 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005356}
5357
Paul Bakker05decb22013-08-15 13:33:48 +02005358#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005359int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5360{
Paul Bakker77e257e2013-12-16 15:29:52 +01005361 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005362 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005363 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005364 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005365 }
5366
5367 ssl->mfl_code = mfl_code;
5368
5369 return( 0 );
5370}
Paul Bakker05decb22013-08-15 13:33:48 +02005371#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005372
Paul Bakker1f2bc622013-08-15 13:45:55 +02005373#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005374int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005375{
5376 if( ssl->endpoint != SSL_IS_CLIENT )
5377 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5378
Paul Bakker8c1ede62013-07-19 14:14:37 +02005379 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005380
5381 return( 0 );
5382}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005383#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005384
Paul Bakker48916f92012-09-16 19:57:18 +00005385void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5386{
5387 ssl->disable_renegotiation = renegotiation;
5388}
5389
5390void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5391{
5392 ssl->allow_legacy_renegotiation = allow_legacy;
5393}
5394
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005395void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5396{
5397 ssl->renego_max_records = max_records;
5398}
5399
Paul Bakkera503a632013-08-14 13:48:06 +02005400#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005401int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5402{
5403 ssl->session_tickets = use_tickets;
5404
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005405 if( ssl->endpoint == SSL_IS_CLIENT )
5406 return( 0 );
5407
5408 if( ssl->f_rng == NULL )
5409 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5410
5411 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005412}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005413
5414void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5415{
5416 ssl->ticket_lifetime = lifetime;
5417}
Paul Bakkera503a632013-08-14 13:48:06 +02005418#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005419
Paul Bakker5121ce52009-01-03 21:22:43 +00005420/*
5421 * SSL get accessors
5422 */
Paul Bakker23986e52011-04-24 08:57:21 +00005423size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005424{
5425 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5426}
5427
Paul Bakkerff60ee62010-03-16 21:09:09 +00005428int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005429{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005430 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005431}
5432
Paul Bakkere3166ce2011-01-27 17:40:50 +00005433const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005434{
Paul Bakker926c8e42013-03-06 10:23:34 +01005435 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005436 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005437
Paul Bakkere3166ce2011-01-27 17:40:50 +00005438 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005439}
5440
Paul Bakker43ca69c2011-01-15 17:35:19 +00005441const char *ssl_get_version( const ssl_context *ssl )
5442{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005443#if defined(POLARSSL_SSL_PROTO_DTLS)
5444 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5445 {
5446 switch( ssl->minor_ver )
5447 {
5448 case SSL_MINOR_VERSION_2:
5449 return( "DTLSv1.0" );
5450
5451 case SSL_MINOR_VERSION_3:
5452 return( "DTLSv1.2" );
5453
5454 default:
5455 return( "unknown (DTLS)" );
5456 }
5457 }
5458#endif
5459
Paul Bakker43ca69c2011-01-15 17:35:19 +00005460 switch( ssl->minor_ver )
5461 {
5462 case SSL_MINOR_VERSION_0:
5463 return( "SSLv3.0" );
5464
5465 case SSL_MINOR_VERSION_1:
5466 return( "TLSv1.0" );
5467
5468 case SSL_MINOR_VERSION_2:
5469 return( "TLSv1.1" );
5470
Paul Bakker1ef83d62012-04-11 12:09:53 +00005471 case SSL_MINOR_VERSION_3:
5472 return( "TLSv1.2" );
5473
Paul Bakker43ca69c2011-01-15 17:35:19 +00005474 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005475 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005476 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005477}
5478
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005479#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005480const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005481{
5482 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005483 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005484
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005485 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005486}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005487#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005488
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005489int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5490{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005491 if( ssl == NULL ||
5492 dst == NULL ||
5493 ssl->session == NULL ||
5494 ssl->endpoint != SSL_IS_CLIENT )
5495 {
5496 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5497 }
5498
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005499 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005500}
5501
Paul Bakker5121ce52009-01-03 21:22:43 +00005502/*
Paul Bakker1961b702013-01-25 14:49:24 +01005503 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005504 */
Paul Bakker1961b702013-01-25 14:49:24 +01005505int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506{
Paul Bakker40e46942009-01-03 21:51:57 +00005507 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005508
Paul Bakker40e46942009-01-03 21:51:57 +00005509#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005511 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005512#endif
5513
Paul Bakker40e46942009-01-03 21:51:57 +00005514#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005515 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005516 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005517#endif
5518
Paul Bakker1961b702013-01-25 14:49:24 +01005519 return( ret );
5520}
5521
5522/*
5523 * Perform the SSL handshake
5524 */
5525int ssl_handshake( ssl_context *ssl )
5526{
5527 int ret = 0;
5528
5529 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5530
5531 while( ssl->state != SSL_HANDSHAKE_OVER )
5532 {
5533 ret = ssl_handshake_step( ssl );
5534
5535 if( ret != 0 )
5536 break;
5537 }
5538
Paul Bakker5121ce52009-01-03 21:22:43 +00005539 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5540
5541 return( ret );
5542}
5543
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005544#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005545/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005546 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005547 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005548static int ssl_write_hello_request( ssl_context *ssl )
5549{
5550 int ret;
5551
5552 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5553
5554 ssl->out_msglen = 4;
5555 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5556 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5557
5558 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5559 {
5560 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5561 return( ret );
5562 }
5563
5564 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5565
5566 return( 0 );
5567}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005568#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005569
5570/*
5571 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005572 * - any side: calling ssl_renegotiate(),
5573 * - client: receiving a HelloRequest during ssl_read(),
5574 * - server: receiving any handshake message on server during ssl_read() after
5575 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005576 * If the handshake doesn't complete due to waiting for I/O, it will continue
5577 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005578 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005579static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005580{
5581 int ret;
5582
5583 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5584
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005585 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5586 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005587
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005588 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5589 * the ServerHello will have message_seq = 1" */
5590#if defined(POLARSSL_SSL_PROTO_DTLS)
5591 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005592 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5593 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005594 if( ssl->endpoint == SSL_IS_SERVER )
5595 ssl->handshake->out_msg_seq = 1;
5596 else
5597 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005598 }
5599#endif
5600
Paul Bakker48916f92012-09-16 19:57:18 +00005601 ssl->state = SSL_HELLO_REQUEST;
5602 ssl->renegotiation = SSL_RENEGOTIATION;
5603
Paul Bakker48916f92012-09-16 19:57:18 +00005604 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5605 {
5606 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5607 return( ret );
5608 }
5609
5610 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5611
5612 return( 0 );
5613}
5614
5615/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005616 * Renegotiate current connection on client,
5617 * or request renegotiation on server
5618 */
5619int ssl_renegotiate( ssl_context *ssl )
5620{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005621 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005622
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005623#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005624 /* On server, just send the request */
5625 if( ssl->endpoint == SSL_IS_SERVER )
5626 {
5627 if( ssl->state != SSL_HANDSHAKE_OVER )
5628 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5629
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005630 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5631
5632 /* Did we already try/start sending HelloRequest? */
5633 if( ssl->out_left != 0 )
5634 return( ssl_flush_output( ssl ) );
5635
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005636 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005637 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005638#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005639
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005640#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005641 /*
5642 * On client, either start the renegotiation process or,
5643 * if already in progress, continue the handshake
5644 */
5645 if( ssl->renegotiation != SSL_RENEGOTIATION )
5646 {
5647 if( ssl->state != SSL_HANDSHAKE_OVER )
5648 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5649
5650 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5651 {
5652 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5653 return( ret );
5654 }
5655 }
5656 else
5657 {
5658 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5659 {
5660 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5661 return( ret );
5662 }
5663 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005664#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005665
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005666 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005667}
5668
5669/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005670 * Receive application data decrypted from the SSL layer
5671 */
Paul Bakker23986e52011-04-24 08:57:21 +00005672int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005673{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005674 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005675 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005676
5677 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5678
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005679#if defined(POLARSSL_SSL_PROTO_DTLS)
5680 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5681 ssl->handshake != NULL &&
5682 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5683 {
5684 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5685 return( ret );
5686
5687 if( ( ret = ssl_resend( ssl ) ) != 0 )
5688 return( ret );
5689 }
5690#endif
5691
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 if( ssl->state != SSL_HANDSHAKE_OVER )
5693 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005694 ret = ssl_handshake( ssl );
5695 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5696 {
5697 record_read = 1;
5698 }
5699 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005700 {
5701 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5702 return( ret );
5703 }
5704 }
5705
5706 if( ssl->in_offt == NULL )
5707 {
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005708#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005709 /* Start timer if not already running */
5710 if( ssl->time_limit == 0 )
5711 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005712#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005713
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005714 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005715 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005716 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5717 {
5718 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5719 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005720
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005721 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5722 return( ret );
5723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005724 }
5725
5726 if( ssl->in_msglen == 0 &&
5727 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5728 {
5729 /*
5730 * OpenSSL sends empty messages to randomize the IV
5731 */
5732 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5733 {
Paul Bakker831a7552011-05-18 13:32:51 +00005734 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5735 return( 0 );
5736
Paul Bakker5121ce52009-01-03 21:22:43 +00005737 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5738 return( ret );
5739 }
5740 }
5741
Paul Bakker48916f92012-09-16 19:57:18 +00005742 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5743 {
5744 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5745
5746 if( ssl->endpoint == SSL_IS_CLIENT &&
5747 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005748 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005749 {
5750 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005751
5752 /* With DTLS, drop the packet (probably from last handshake) */
5753#if defined(POLARSSL_SSL_PROTO_DTLS)
5754 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5755 return( POLARSSL_ERR_NET_WANT_READ );
5756#endif
5757 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5758 }
5759
5760 if( ssl->endpoint == SSL_IS_SERVER &&
5761 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5762 {
5763 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5764
5765 /* With DTLS, drop the packet (probably from last handshake) */
5766#if defined(POLARSSL_SSL_PROTO_DTLS)
5767 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5768 return( POLARSSL_ERR_NET_WANT_READ );
5769#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005770 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5771 }
5772
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005773 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5774 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005775 ssl->allow_legacy_renegotiation ==
5776 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005777 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005778 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005779
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005780#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005781 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005782 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005783 /*
5784 * SSLv3 does not have a "no_renegotiation" alert
5785 */
5786 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5787 return( ret );
5788 }
5789 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005790#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005791#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5792 defined(POLARSSL_SSL_PROTO_TLS1_2)
5793 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005794 {
5795 if( ( ret = ssl_send_alert_message( ssl,
5796 SSL_ALERT_LEVEL_WARNING,
5797 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5798 {
5799 return( ret );
5800 }
Paul Bakker48916f92012-09-16 19:57:18 +00005801 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005802 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005803#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5804 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005805 {
5806 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005807 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005808 }
Paul Bakker48916f92012-09-16 19:57:18 +00005809 }
5810 else
5811 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005812 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005813#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005814 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5815 ssl->endpoint == SSL_IS_CLIENT )
5816 {
5817 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5818 }
5819#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005820 ret = ssl_start_renegotiation( ssl );
5821 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5822 {
5823 record_read = 1;
5824 }
5825 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005826 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005827 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005828 return( ret );
5829 }
Paul Bakker48916f92012-09-16 19:57:18 +00005830 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005831
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005832 /* If a non-handshake record was read during renego, fallthrough,
5833 * else tell the user they should call ssl_read() again */
5834 if( ! record_read )
5835 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005836 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005837 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5838 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005839 ssl->renego_records_seen++;
5840
5841 if( ssl->renego_max_records >= 0 &&
5842 ssl->renego_records_seen > ssl->renego_max_records )
5843 {
5844 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5845 "but not honored by client" ) );
5846 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5847 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005848 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005849
5850 /* Fatal and closure alerts handled by ssl_read_record() */
5851 if( ssl->in_msgtype == SSL_MSG_ALERT )
5852 {
5853 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5854 return( POLARSSL_ERR_NET_WANT_READ );
5855 }
5856
5857 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005858 {
5859 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005860 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005861 }
5862
5863 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005864
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005865#if defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005866 /* We're going to return something now, cancel timer,
5867 * except if handshake (renegotiation) is in progress */
5868 if( ssl->state == SSL_HANDSHAKE_OVER )
5869 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005870#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005871 }
5872
5873 n = ( len < ssl->in_msglen )
5874 ? len : ssl->in_msglen;
5875
5876 memcpy( buf, ssl->in_offt, n );
5877 ssl->in_msglen -= n;
5878
5879 if( ssl->in_msglen == 0 )
5880 /* all bytes consumed */
5881 ssl->in_offt = NULL;
5882 else
5883 /* more data available */
5884 ssl->in_offt += n;
5885
5886 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5887
Paul Bakker23986e52011-04-24 08:57:21 +00005888 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005889}
5890
5891/*
5892 * Send application data to be encrypted by the SSL layer
5893 */
Paul Bakker23986e52011-04-24 08:57:21 +00005894int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005895{
Paul Bakker23986e52011-04-24 08:57:21 +00005896 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005897#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
5898 unsigned int max_len;
5899#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005900
5901 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5902
5903 if( ssl->state != SSL_HANDSHAKE_OVER )
5904 {
5905 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5906 {
5907 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5908 return( ret );
5909 }
5910 }
5911
Paul Bakker05decb22013-08-15 13:33:48 +02005912#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005913 /*
5914 * Assume mfl_code is correct since it was checked when set
5915 */
5916 max_len = mfl_code_to_length[ssl->mfl_code];
5917
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005918 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005919 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005920 */
5921 if( ssl->session_out != NULL &&
5922 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5923 {
5924 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5925 }
5926
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005927 if( len > max_len )
5928 {
5929#if defined(POLARSSL_SSL_PROTO_DTLS)
5930 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5931 {
5932 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
5933 "maximum fragment length: %d > %d",
5934 len, max_len ) );
5935 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5936 }
5937 else
5938#endif
5939 len = max_len;
5940 }
5941#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00005942
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 if( ssl->out_left != 0 )
5944 {
5945 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5946 {
5947 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5948 return( ret );
5949 }
5950 }
Paul Bakker887bd502011-06-08 13:10:54 +00005951 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005952 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005953 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00005954 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005955 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00005956
5957 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5958 {
5959 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5960 return( ret );
5961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005962 }
5963
5964 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5965
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005966 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005967}
5968
5969/*
5970 * Notify the peer that the connection is being closed
5971 */
5972int ssl_close_notify( ssl_context *ssl )
5973{
5974 int ret;
5975
5976 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5977
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005978 if( ssl->out_left != 0 )
5979 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005980
5981 if( ssl->state == SSL_HANDSHAKE_OVER )
5982 {
Paul Bakker48916f92012-09-16 19:57:18 +00005983 if( ( ret = ssl_send_alert_message( ssl,
5984 SSL_ALERT_LEVEL_WARNING,
5985 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005986 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005987 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005988 return( ret );
5989 }
5990 }
5991
5992 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5993
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005994 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005995}
5996
Paul Bakker48916f92012-09-16 19:57:18 +00005997void ssl_transform_free( ssl_transform *transform )
5998{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005999 if( transform == NULL )
6000 return;
6001
Paul Bakker48916f92012-09-16 19:57:18 +00006002#if defined(POLARSSL_ZLIB_SUPPORT)
6003 deflateEnd( &transform->ctx_deflate );
6004 inflateEnd( &transform->ctx_inflate );
6005#endif
6006
Paul Bakker84bbeb52014-07-01 14:53:22 +02006007 cipher_free( &transform->cipher_ctx_enc );
6008 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006009
Paul Bakker84bbeb52014-07-01 14:53:22 +02006010 md_free( &transform->md_ctx_enc );
6011 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006012
Paul Bakker34617722014-06-13 17:20:13 +02006013 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006014}
6015
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006016#if defined(POLARSSL_X509_CRT_PARSE_C)
6017static void ssl_key_cert_free( ssl_key_cert *key_cert )
6018{
6019 ssl_key_cert *cur = key_cert, *next;
6020
6021 while( cur != NULL )
6022 {
6023 next = cur->next;
6024
6025 if( cur->key_own_alloc )
6026 {
6027 pk_free( cur->key );
6028 polarssl_free( cur->key );
6029 }
6030 polarssl_free( cur );
6031
6032 cur = next;
6033 }
6034}
6035#endif /* POLARSSL_X509_CRT_PARSE_C */
6036
Paul Bakker48916f92012-09-16 19:57:18 +00006037void ssl_handshake_free( ssl_handshake_params *handshake )
6038{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006039 if( handshake == NULL )
6040 return;
6041
Paul Bakker48916f92012-09-16 19:57:18 +00006042#if defined(POLARSSL_DHM_C)
6043 dhm_free( &handshake->dhm_ctx );
6044#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006045#if defined(POLARSSL_ECDH_C)
6046 ecdh_free( &handshake->ecdh_ctx );
6047#endif
6048
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006049#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006050 /* explicit void pointer cast for buggy MS compiler */
6051 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006052#endif
6053
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006054#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6055 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6056 /*
6057 * Free only the linked list wrapper, not the keys themselves
6058 * since the belong to the SNI callback
6059 */
6060 if( handshake->sni_key_cert != NULL )
6061 {
6062 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6063
6064 while( cur != NULL )
6065 {
6066 next = cur->next;
6067 polarssl_free( cur );
6068 cur = next;
6069 }
6070 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006071#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006072
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006073#if defined(POLARSSL_SSL_PROTO_DTLS)
6074 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006075 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006076 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006077#endif
6078
Paul Bakker34617722014-06-13 17:20:13 +02006079 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006080}
6081
6082void ssl_session_free( ssl_session *session )
6083{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006084 if( session == NULL )
6085 return;
6086
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006087#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006088 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006089 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006090 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006091 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006092 }
Paul Bakkered27a042013-04-18 22:46:23 +02006093#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006094
Paul Bakkera503a632013-08-14 13:48:06 +02006095#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006096 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006097#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006098
Paul Bakker34617722014-06-13 17:20:13 +02006099 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006100}
6101
Paul Bakker5121ce52009-01-03 21:22:43 +00006102/*
6103 * Free an SSL context
6104 */
6105void ssl_free( ssl_context *ssl )
6106{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006107 if( ssl == NULL )
6108 return;
6109
Paul Bakker5121ce52009-01-03 21:22:43 +00006110 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6111
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006112 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006114 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6115 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 }
6117
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006118 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006120 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6121 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 }
6123
Paul Bakker16770332013-10-11 09:59:44 +02006124#if defined(POLARSSL_ZLIB_SUPPORT)
6125 if( ssl->compress_buf != NULL )
6126 {
Paul Bakker34617722014-06-13 17:20:13 +02006127 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006128 polarssl_free( ssl->compress_buf );
6129 }
6130#endif
6131
Paul Bakker40e46942009-01-03 21:51:57 +00006132#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006133 mpi_free( &ssl->dhm_P );
6134 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135#endif
6136
Paul Bakker48916f92012-09-16 19:57:18 +00006137 if( ssl->transform )
6138 {
6139 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006140 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006141 }
6142
6143 if( ssl->handshake )
6144 {
6145 ssl_handshake_free( ssl->handshake );
6146 ssl_transform_free( ssl->transform_negotiate );
6147 ssl_session_free( ssl->session_negotiate );
6148
Paul Bakker6e339b52013-07-03 13:37:05 +02006149 polarssl_free( ssl->handshake );
6150 polarssl_free( ssl->transform_negotiate );
6151 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006152 }
6153
Paul Bakkerc0463502013-02-14 11:19:38 +01006154 if( ssl->session )
6155 {
6156 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006157 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006158 }
6159
Paul Bakkera503a632013-08-14 13:48:06 +02006160#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006161 if( ssl->ticket_keys )
6162 {
6163 ssl_ticket_keys_free( ssl->ticket_keys );
6164 polarssl_free( ssl->ticket_keys );
6165 }
Paul Bakkera503a632013-08-14 13:48:06 +02006166#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006167
Paul Bakker0be444a2013-08-27 21:55:01 +02006168#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006169 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006170 {
Paul Bakker34617722014-06-13 17:20:13 +02006171 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006172 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006173 ssl->hostname_len = 0;
6174 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006175#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006176
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006177#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006178 if( ssl->psk != NULL )
6179 {
Paul Bakker34617722014-06-13 17:20:13 +02006180 polarssl_zeroize( ssl->psk, ssl->psk_len );
6181 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006182 polarssl_free( ssl->psk );
6183 polarssl_free( ssl->psk_identity );
6184 ssl->psk_len = 0;
6185 ssl->psk_identity_len = 0;
6186 }
6187#endif
6188
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006189#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006190 ssl_key_cert_free( ssl->key_cert );
6191#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006192
Paul Bakker05ef8352012-05-08 09:17:57 +00006193#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6194 if( ssl_hw_record_finish != NULL )
6195 {
6196 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6197 ssl_hw_record_finish( ssl );
6198 }
6199#endif
6200
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006201#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006202 polarssl_free( ssl->cli_id );
6203#endif
6204
Paul Bakker5121ce52009-01-03 21:22:43 +00006205 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006206
Paul Bakker86f04f42013-02-14 11:20:09 +01006207 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006208 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006209}
6210
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006211#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006212/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006213 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006214 */
6215unsigned char ssl_sig_from_pk( pk_context *pk )
6216{
6217#if defined(POLARSSL_RSA_C)
6218 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6219 return( SSL_SIG_RSA );
6220#endif
6221#if defined(POLARSSL_ECDSA_C)
6222 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6223 return( SSL_SIG_ECDSA );
6224#endif
6225 return( SSL_SIG_ANON );
6226}
6227
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006228pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6229{
6230 switch( sig )
6231 {
6232#if defined(POLARSSL_RSA_C)
6233 case SSL_SIG_RSA:
6234 return( POLARSSL_PK_RSA );
6235#endif
6236#if defined(POLARSSL_ECDSA_C)
6237 case SSL_SIG_ECDSA:
6238 return( POLARSSL_PK_ECDSA );
6239#endif
6240 default:
6241 return( POLARSSL_PK_NONE );
6242 }
6243}
Paul Bakker9af723c2014-05-01 13:03:14 +02006244#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006245
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006246/*
6247 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6248 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006249md_type_t ssl_md_alg_from_hash( unsigned char hash )
6250{
6251 switch( hash )
6252 {
6253#if defined(POLARSSL_MD5_C)
6254 case SSL_HASH_MD5:
6255 return( POLARSSL_MD_MD5 );
6256#endif
6257#if defined(POLARSSL_SHA1_C)
6258 case SSL_HASH_SHA1:
6259 return( POLARSSL_MD_SHA1 );
6260#endif
6261#if defined(POLARSSL_SHA256_C)
6262 case SSL_HASH_SHA224:
6263 return( POLARSSL_MD_SHA224 );
6264 case SSL_HASH_SHA256:
6265 return( POLARSSL_MD_SHA256 );
6266#endif
6267#if defined(POLARSSL_SHA512_C)
6268 case SSL_HASH_SHA384:
6269 return( POLARSSL_MD_SHA384 );
6270 case SSL_HASH_SHA512:
6271 return( POLARSSL_MD_SHA512 );
6272#endif
6273 default:
6274 return( POLARSSL_MD_NONE );
6275 }
6276}
6277
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006278#if defined(POLARSSL_SSL_SET_CURVES)
6279/*
6280 * Check is a curve proposed by the peer is in our list.
6281 * Return 1 if we're willing to use it, 0 otherwise.
6282 */
6283int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6284{
6285 const ecp_group_id *gid;
6286
6287 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6288 if( *gid == grp_id )
6289 return( 1 );
6290
6291 return( 0 );
6292}
Paul Bakker9af723c2014-05-01 13:03:14 +02006293#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006294
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006295#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006296int ssl_check_cert_usage( const x509_crt *cert,
6297 const ssl_ciphersuite_t *ciphersuite,
6298 int cert_endpoint )
6299{
6300#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6301 int usage = 0;
6302#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006303#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6304 const char *ext_oid;
6305 size_t ext_len;
6306#endif
6307
6308#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6309 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6310 ((void) cert);
6311 ((void) cert_endpoint);
6312#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006313
6314#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6315 if( cert_endpoint == SSL_IS_SERVER )
6316 {
6317 /* Server part of the key exchange */
6318 switch( ciphersuite->key_exchange )
6319 {
6320 case POLARSSL_KEY_EXCHANGE_RSA:
6321 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6322 usage = KU_KEY_ENCIPHERMENT;
6323 break;
6324
6325 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6326 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6327 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6328 usage = KU_DIGITAL_SIGNATURE;
6329 break;
6330
6331 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6332 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6333 usage = KU_KEY_AGREEMENT;
6334 break;
6335
6336 /* Don't use default: we want warnings when adding new values */
6337 case POLARSSL_KEY_EXCHANGE_NONE:
6338 case POLARSSL_KEY_EXCHANGE_PSK:
6339 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6340 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6341 usage = 0;
6342 }
6343 }
6344 else
6345 {
6346 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6347 usage = KU_DIGITAL_SIGNATURE;
6348 }
6349
6350 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6351 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006352#else
6353 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006354#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6355
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006356#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6357 if( cert_endpoint == SSL_IS_SERVER )
6358 {
6359 ext_oid = OID_SERVER_AUTH;
6360 ext_len = OID_SIZE( OID_SERVER_AUTH );
6361 }
6362 else
6363 {
6364 ext_oid = OID_CLIENT_AUTH;
6365 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6366 }
6367
6368 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6369 return( -1 );
6370#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6371
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006372 return( 0 );
6373}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006374#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006375
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006376/*
6377 * Convert version numbers to/from wire format
6378 * and, for DTLS, to/from TLS equivalent.
6379 *
6380 * For TLS this is the identity.
6381 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6382 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6383 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6384 */
6385void ssl_write_version( int major, int minor, int transport,
6386 unsigned char ver[2] )
6387{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006388#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006389 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006390 {
6391 if( minor == SSL_MINOR_VERSION_2 )
6392 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6393
6394 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6395 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6396 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006397 else
6398#else
6399 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006400#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006401 {
6402 ver[0] = (unsigned char) major;
6403 ver[1] = (unsigned char) minor;
6404 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006405}
6406
6407void ssl_read_version( int *major, int *minor, int transport,
6408 const unsigned char ver[2] )
6409{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006410#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006411 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006412 {
6413 *major = 255 - ver[0] + 2;
6414 *minor = 255 - ver[1] + 1;
6415
6416 if( *minor == SSL_MINOR_VERSION_1 )
6417 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6418 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006419 else
6420#else
6421 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006422#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006423 {
6424 *major = ver[0];
6425 *minor = ver[1];
6426 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006427}
6428
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006429#endif /* POLARSSL_SSL_TLS_C */