blob: fff5a1f8d2d1351d4f81113a2f5f823b4cc583ea [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
Paul Bakker05decb22013-08-15 13:33:48 +020081#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082/*
83 * Convert max_fragment_length codes to length.
84 * RFC 6066 says:
85 * enum{
86 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
87 * } MaxFragmentLength;
88 * and we add 0 -> extension unused
89 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020090static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020091{
92 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
93 512, /* SSL_MAX_FRAG_LEN_512 */
94 1024, /* SSL_MAX_FRAG_LEN_1024 */
95 2048, /* SSL_MAX_FRAG_LEN_2048 */
96 4096, /* SSL_MAX_FRAG_LEN_4096 */
97};
Paul Bakker05decb22013-08-15 13:33:48 +020098#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020099
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
101{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200102 ssl_session_free( dst );
103 memcpy( dst, src, sizeof( ssl_session ) );
104
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 if( src->peer_cert != NULL )
107 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200108 int ret;
109
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200110 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
111 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
113
Paul Bakkerb6b09562013-09-18 14:17:41 +0200114 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200116 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
117 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200118 {
119 polarssl_free( dst->peer_cert );
120 dst->peer_cert = NULL;
121 return( ret );
122 }
123 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200125
Paul Bakkera503a632013-08-14 13:48:06 +0200126#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200127 if( src->ticket != NULL )
128 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
130 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200131 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
132
133 memcpy( dst->ticket, src->ticket, src->ticket_len );
134 }
Paul Bakkera503a632013-08-14 13:48:06 +0200135#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200136
137 return( 0 );
138}
139
Paul Bakker05ef8352012-05-08 09:17:57 +0000140#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200141int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200142 const unsigned char *key_enc, const unsigned char *key_dec,
143 size_t keylen,
144 const unsigned char *iv_enc, const unsigned char *iv_dec,
145 size_t ivlen,
146 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200147 size_t maclen ) = NULL;
148int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
149int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
150int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
151int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
152int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200153#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000154
Paul Bakker5121ce52009-01-03 21:22:43 +0000155/*
156 * Key material generation
157 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200158#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200159static int ssl3_prf( const unsigned char *secret, size_t slen,
160 const char *label,
161 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 unsigned char *dstbuf, size_t dlen )
163{
164 size_t i;
165 md5_context md5;
166 sha1_context sha1;
167 unsigned char padding[16];
168 unsigned char sha1sum[20];
169 ((void)label);
170
Paul Bakker5b4af392014-06-26 12:09:34 +0200171 md5_init( &md5 );
172 sha1_init( &sha1 );
173
Paul Bakker5f70b252012-09-13 14:23:06 +0000174 /*
175 * SSLv3:
176 * block =
177 * MD5( secret + SHA1( 'A' + secret + random ) ) +
178 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
179 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
180 * ...
181 */
182 for( i = 0; i < dlen / 16; i++ )
183 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200184 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
186 sha1_starts( &sha1 );
187 sha1_update( &sha1, padding, 1 + i );
188 sha1_update( &sha1, secret, slen );
189 sha1_update( &sha1, random, rlen );
190 sha1_finish( &sha1, sha1sum );
191
192 md5_starts( &md5 );
193 md5_update( &md5, secret, slen );
194 md5_update( &md5, sha1sum, 20 );
195 md5_finish( &md5, dstbuf + i * 16 );
196 }
197
Paul Bakker5b4af392014-06-26 12:09:34 +0200198 md5_free( &md5 );
199 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000200
Paul Bakker34617722014-06-13 17:20:13 +0200201 polarssl_zeroize( padding, sizeof( padding ) );
202 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000203
204 return( 0 );
205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200206#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000207
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200208#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200209static int tls1_prf( const unsigned char *secret, size_t slen,
210 const char *label,
211 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000212 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213{
Paul Bakker23986e52011-04-24 08:57:21 +0000214 size_t nb, hs;
215 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200216 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 unsigned char tmp[128];
218 unsigned char h_i[20];
219
220 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223 hs = ( slen + 1 ) / 2;
224 S1 = secret;
225 S2 = secret + slen - hs;
226
227 nb = strlen( label );
228 memcpy( tmp + 20, label, nb );
229 memcpy( tmp + 20 + nb, random, rlen );
230 nb += rlen;
231
232 /*
233 * First compute P_md5(secret,label+random)[0..dlen]
234 */
235 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
236
237 for( i = 0; i < dlen; i += 16 )
238 {
239 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
240 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
241
242 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
243
244 for( j = 0; j < k; j++ )
245 dstbuf[i + j] = h_i[j];
246 }
247
248 /*
249 * XOR out with P_sha1(secret,label+random)[0..dlen]
250 */
251 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
252
253 for( i = 0; i < dlen; i += 20 )
254 {
255 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
256 sha1_hmac( S2, hs, tmp, 20, tmp );
257
258 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
259
260 for( j = 0; j < k; j++ )
261 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
262 }
263
Paul Bakker34617722014-06-13 17:20:13 +0200264 polarssl_zeroize( tmp, sizeof( tmp ) );
265 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 return( 0 );
268}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200269#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200271#if defined(POLARSSL_SSL_PROTO_TLS1_2)
272#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200273static int tls_prf_sha256( const unsigned char *secret, size_t slen,
274 const char *label,
275 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000276 unsigned char *dstbuf, size_t dlen )
277{
278 size_t nb;
279 size_t i, j, k;
280 unsigned char tmp[128];
281 unsigned char h_i[32];
282
283 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
284 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
285
286 nb = strlen( label );
287 memcpy( tmp + 32, label, nb );
288 memcpy( tmp + 32 + nb, random, rlen );
289 nb += rlen;
290
291 /*
292 * Compute P_<hash>(secret, label + random)[0..dlen]
293 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200294 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 for( i = 0; i < dlen; i += 32 )
297 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200298 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
299 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000300
301 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
302
303 for( j = 0; j < k; j++ )
304 dstbuf[i + j] = h_i[j];
305 }
306
Paul Bakker34617722014-06-13 17:20:13 +0200307 polarssl_zeroize( tmp, sizeof( tmp ) );
308 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000309
310 return( 0 );
311}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200312#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000313
Paul Bakker9e36f042013-06-30 14:34:05 +0200314#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200315static int tls_prf_sha384( const unsigned char *secret, size_t slen,
316 const char *label,
317 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000318 unsigned char *dstbuf, size_t dlen )
319{
320 size_t nb;
321 size_t i, j, k;
322 unsigned char tmp[128];
323 unsigned char h_i[48];
324
325 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
326 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
327
328 nb = strlen( label );
329 memcpy( tmp + 48, label, nb );
330 memcpy( tmp + 48 + nb, random, rlen );
331 nb += rlen;
332
333 /*
334 * Compute P_<hash>(secret, label + random)[0..dlen]
335 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200336 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 for( i = 0; i < dlen; i += 48 )
339 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200340 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
341 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
343 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
344
345 for( j = 0; j < k; j++ )
346 dstbuf[i + j] = h_i[j];
347 }
348
Paul Bakker34617722014-06-13 17:20:13 +0200349 polarssl_zeroize( tmp, sizeof( tmp ) );
350 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351
352 return( 0 );
353}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200354#endif /* POLARSSL_SHA512_C */
355#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000356
Paul Bakker66d5d072014-06-17 16:39:18 +0200357static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358
359#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
360 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#endif
Paul Bakker380da532012-04-18 16:10:25 +0000363
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200365static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
366static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
368
369#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
371static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200372#endif
373
374#if defined(POLARSSL_SSL_PROTO_TLS1_2)
375#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200376static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
377static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
378static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200379#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100380
Paul Bakker9e36f042013-06-30 14:34:05 +0200381#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200382static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
383static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
384static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100385#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200386#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388int ssl_derive_keys( ssl_context *ssl )
389{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200390 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 unsigned char keyblk[256];
393 unsigned char *key1;
394 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100395 unsigned char *mac_enc;
396 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100398 const cipher_info_t *cipher_info;
399 const md_info_t *md_info;
400
Paul Bakker48916f92012-09-16 19:57:18 +0000401 ssl_session *session = ssl->session_negotiate;
402 ssl_transform *transform = ssl->transform_negotiate;
403 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
406
Paul Bakker68884e32013-01-07 18:20:04 +0100407 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
408 if( cipher_info == NULL )
409 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200410 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100411 transform->ciphersuite_info->cipher ) );
412 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
413 }
414
415 md_info = md_info_from_type( transform->ciphersuite_info->mac );
416 if( md_info == NULL )
417 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200418 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100419 transform->ciphersuite_info->mac ) );
420 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
421 }
422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000424 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000425 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000427 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = ssl3_prf;
430 handshake->calc_verify = ssl_calc_verify_ssl;
431 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
436 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls1_prf;
439 handshake->calc_verify = ssl_calc_verify_tls;
440 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
444#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200445#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200446 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
447 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000448 {
Paul Bakker48916f92012-09-16 19:57:18 +0000449 handshake->tls_prf = tls_prf_sha384;
450 handshake->calc_verify = ssl_calc_verify_tls_sha384;
451 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000452 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000453 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200454#endif
455#if defined(POLARSSL_SHA256_C)
456 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000457 {
Paul Bakker48916f92012-09-16 19:57:18 +0000458 handshake->tls_prf = tls_prf_sha256;
459 handshake->calc_verify = ssl_calc_verify_tls_sha256;
460 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000461 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 else
463#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200464#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200465 {
466 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200467 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200468 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000469
470 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * SSLv3:
472 * master =
473 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
474 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
475 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200476 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200477 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 * master = PRF( premaster, "master secret", randbytes )[0..47]
479 */
Paul Bakker0a597072012-09-25 21:55:46 +0000480 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 {
Paul Bakker48916f92012-09-16 19:57:18 +0000482 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
483 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( handshake->premaster, handshake->pmslen,
486 "master secret",
487 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Paul Bakker34617722014-06-13 17:20:13 +0200489 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 }
491 else
492 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
493
494 /*
495 * Swap the client and server random values.
496 */
Paul Bakker48916f92012-09-16 19:57:18 +0000497 memcpy( tmp, handshake->randbytes, 64 );
498 memcpy( handshake->randbytes, tmp + 32, 32 );
499 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200500 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 /*
503 * SSLv3:
504 * key block =
505 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
506 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
507 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
508 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
509 * ...
510 *
511 * TLSv1:
512 * key block = PRF( master, "key expansion", randbytes )
513 */
Paul Bakker48916f92012-09-16 19:57:18 +0000514 handshake->tls_prf( session->master, 48, "key expansion",
515 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker48916f92012-09-16 19:57:18 +0000517 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
518 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
519 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
520 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
522
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * Determine the appropriate key, IV and MAC length.
527 */
Paul Bakker68884e32013-01-07 18:20:04 +0100528
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200529 transform->keylen = cipher_info->key_length / 8;
530
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200531 if( cipher_info->mode == POLARSSL_MODE_GCM ||
532 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 transform->maclen = 0;
535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->ivlen = 12;
537 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200538
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200539 /* Minimum length is expicit IV + tag */
540 transform->minlen = transform->ivlen - transform->fixed_ivlen
541 + ( transform->ciphersuite_info->flags &
542 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100543 }
544 else
545 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200546 int ret;
547
548 /* Initialize HMAC contexts */
549 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
550 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100551 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
553 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100554 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200556 /* Get MAC length */
557 transform->maclen = md_get_size( md_info );
558
559#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
560 /*
561 * If HMAC is to be truncated, we shall keep the leftmost bytes,
562 * (rfc 6066 page 13 or rfc 2104 section 4),
563 * so we only need to adjust the length here.
564 */
565 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
566 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
567#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
568
569 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100570 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200572 /* Minimum length */
573 if( cipher_info->mode == POLARSSL_MODE_STREAM )
574 transform->minlen = transform->maclen;
575 else
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200577 /*
578 * GenericBlockCipher:
579 * first multiple of blocklen greater than maclen
580 * + IV except for SSL3 and TLS 1.0
581 */
582 transform->minlen = transform->maclen
583 + cipher_info->block_size
584 - transform->maclen % cipher_info->block_size;
585
586#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
587 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
588 ssl->minor_ver == SSL_MINOR_VERSION_1 )
589 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100590 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200591#endif
592#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
593 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
594 ssl->minor_ver == SSL_MINOR_VERSION_3 )
595 {
596 transform->minlen += transform->ivlen;
597 }
598 else
599#endif
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
603 }
Paul Bakker68884e32013-01-07 18:20:04 +0100604 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
606
607 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000608 transform->keylen, transform->minlen, transform->ivlen,
609 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
611 /*
612 * Finally setup the cipher contexts, IVs and MAC secrets.
613 */
614 if( ssl->endpoint == SSL_IS_CLIENT )
615 {
Paul Bakker48916f92012-09-16 19:57:18 +0000616 key1 = keyblk + transform->maclen * 2;
617 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Paul Bakker68884e32013-01-07 18:20:04 +0100619 mac_enc = keyblk;
620 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000622 /*
623 * This is not used in TLS v1.1.
624 */
Paul Bakker48916f92012-09-16 19:57:18 +0000625 iv_copy_len = ( transform->fixed_ivlen ) ?
626 transform->fixed_ivlen : transform->ivlen;
627 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
628 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000629 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631 else
632 {
Paul Bakker48916f92012-09-16 19:57:18 +0000633 key1 = keyblk + transform->maclen * 2 + transform->keylen;
634 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker68884e32013-01-07 18:20:04 +0100636 mac_enc = keyblk + transform->maclen;
637 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000639 /*
640 * This is not used in TLS v1.1.
641 */
Paul Bakker48916f92012-09-16 19:57:18 +0000642 iv_copy_len = ( transform->fixed_ivlen ) ?
643 transform->fixed_ivlen : transform->ivlen;
644 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
645 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 }
648
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200649#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100650 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
651 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100652 if( transform->maclen > sizeof transform->mac_enc )
653 {
654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100656 }
657
Paul Bakker68884e32013-01-07 18:20:04 +0100658 memcpy( transform->mac_enc, mac_enc, transform->maclen );
659 memcpy( transform->mac_dec, mac_dec, transform->maclen );
660 }
661 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200662#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200663#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
664 defined(POLARSSL_SSL_PROTO_TLS1_2)
665 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100666 {
667 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
668 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
669 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670 else
671#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200672 {
673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200674 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200675 }
Paul Bakker68884e32013-01-07 18:20:04 +0100676
Paul Bakker05ef8352012-05-08 09:17:57 +0000677#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200678 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000679 {
680 int ret = 0;
681
682 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
683
Paul Bakker07eb38b2012-12-19 14:42:06 +0100684 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
685 transform->iv_enc, transform->iv_dec,
686 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100687 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100688 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000689 {
690 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200691 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000692 }
693 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200694#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000695
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200696 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
697 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
700 return( ret );
701 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
704 cipher_info ) ) != 0 )
705 {
706 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
707 return( ret );
708 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200709
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200710 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
711 cipher_info->key_length,
712 POLARSSL_ENCRYPT ) ) != 0 )
713 {
714 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
715 return( ret );
716 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200717
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200718 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
719 cipher_info->key_length,
720 POLARSSL_DECRYPT ) ) != 0 )
721 {
722 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
723 return( ret );
724 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200725
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200726#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200727 if( cipher_info->mode == POLARSSL_MODE_CBC )
728 {
729 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
730 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200731 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200732 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
733 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200734 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735
736 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
737 POLARSSL_PADDING_NONE ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
740 return( ret );
741 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker34617722014-06-13 17:20:13 +0200745 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Paul Bakker2770fbd2012-07-03 13:30:23 +0000747#if defined(POLARSSL_ZLIB_SUPPORT)
748 // Initialize compression
749 //
Paul Bakker48916f92012-09-16 19:57:18 +0000750 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000751 {
Paul Bakker16770332013-10-11 09:59:44 +0200752 if( ssl->compress_buf == NULL )
753 {
754 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
755 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
756 if( ssl->compress_buf == NULL )
757 {
758 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
759 SSL_BUFFER_LEN ) );
760 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
761 }
762 }
763
Paul Bakker2770fbd2012-07-03 13:30:23 +0000764 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
767 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000768
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200769 if( deflateInit( &transform->ctx_deflate,
770 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000771 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772 {
773 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
774 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
775 }
776 }
777#endif /* POLARSSL_ZLIB_SUPPORT */
778
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
780
781 return( 0 );
782}
783
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000785void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786{
787 md5_context md5;
788 sha1_context sha1;
789 unsigned char pad_1[48];
790 unsigned char pad_2[48];
791
Paul Bakker380da532012-04-18 16:10:25 +0000792 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
795 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Paul Bakker380da532012-04-18 16:10:25 +0000797 memset( pad_1, 0x36, 48 );
798 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
Paul Bakker48916f92012-09-16 19:57:18 +0000800 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 md5_update( &md5, pad_1, 48 );
802 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000803
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000805 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000806 md5_update( &md5, pad_2, 48 );
807 md5_update( &md5, hash, 16 );
808 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000811 sha1_update( &sha1, pad_1, 40 );
812 sha1_finish( &sha1, hash + 16 );
813
814 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000815 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000816 sha1_update( &sha1, pad_2, 40 );
817 sha1_update( &sha1, hash + 16, 20 );
818 sha1_finish( &sha1, hash + 16 );
819
820 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
821 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
822
Paul Bakker5b4af392014-06-26 12:09:34 +0200823 md5_free( &md5 );
824 sha1_free( &sha1 );
825
Paul Bakker380da532012-04-18 16:10:25 +0000826 return;
827}
Paul Bakker9af723c2014-05-01 13:03:14 +0200828#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000829
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200830#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000831void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
832{
833 md5_context md5;
834 sha1_context sha1;
835
836 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000840
Paul Bakker48916f92012-09-16 19:57:18 +0000841 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000842 sha1_finish( &sha1, hash + 16 );
843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
Paul Bakker5b4af392014-06-26 12:09:34 +0200847 md5_free( &md5 );
848 sha1_free( &sha1 );
849
Paul Bakker380da532012-04-18 16:10:25 +0000850 return;
851}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000853
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200854#if defined(POLARSSL_SSL_PROTO_TLS1_2)
855#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
857{
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000859
860 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
861
Paul Bakker9e36f042013-06-30 14:34:05 +0200862 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
863 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000864
865 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
866 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
867
Paul Bakker5b4af392014-06-26 12:09:34 +0200868 sha256_free( &sha256 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakker9e36f042013-06-30 14:34:05 +0200874#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
876{
Paul Bakker9e36f042013-06-30 14:34:05 +0200877 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000878
879 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
880
Paul Bakker9e36f042013-06-30 14:34:05 +0200881 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
882 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Paul Bakkerca4ab492012-04-18 14:23:57 +0000884 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000885 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
886
Paul Bakker5b4af392014-06-26 12:09:34 +0200887 sha512_free( &sha512 );
888
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 return;
890}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200891#endif /* POLARSSL_SHA512_C */
892#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200894#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
896{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200897 unsigned char *p = ssl->handshake->premaster;
898 unsigned char *end = p + sizeof( ssl->handshake->premaster );
899
900 /*
901 * PMS = struct {
902 * opaque other_secret<0..2^16-1>;
903 * opaque psk<0..2^16-1>;
904 * };
905 * with "other_secret" depending on the particular key exchange
906 */
907#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
909 {
910 if( end - p < 2 + (int) ssl->psk_len )
911 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
912
913 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
914 *(p++) = (unsigned char)( ssl->psk_len );
915 p += ssl->psk_len;
916 }
917 else
918#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200919#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
920 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
921 {
922 /*
923 * other_secret already set by the ClientKeyExchange message,
924 * and is 48 bytes long
925 */
926 *p++ = 0;
927 *p++ = 48;
928 p += 48;
929 }
930 else
931#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
934 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200935 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200936 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200940 p + 2, &len,
941 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942 {
943 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
944 return( ret );
945 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200946 *(p++) = (unsigned char)( len >> 8 );
947 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200948 p += len;
949
950 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
951 }
952 else
953#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
954#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
955 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
956 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200957 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200958 size_t zlen;
959
960 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200961 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 ssl->f_rng, ssl->p_rng ) ) != 0 )
963 {
964 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
965 return( ret );
966 }
967
968 *(p++) = (unsigned char)( zlen >> 8 );
969 *(p++) = (unsigned char)( zlen );
970 p += zlen;
971
972 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
973 }
974 else
975#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
976 {
977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979 }
980
981 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100982 if( end - p < 2 + (int) ssl->psk_len )
983 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
984
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200985 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
986 *(p++) = (unsigned char)( ssl->psk_len );
987 memcpy( p, ssl->psk, ssl->psk_len );
988 p += ssl->psk_len;
989
990 ssl->handshake->pmslen = p - ssl->handshake->premaster;
991
992 return( 0 );
993}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200994#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200995
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200996#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000997/*
998 * SSLv3.0 MAC functions
999 */
Paul Bakker68884e32013-01-07 18:20:04 +01001000static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1001 unsigned char *buf, size_t len,
1002 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003{
1004 unsigned char header[11];
1005 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001006 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001007 int md_size = md_get_size( md_ctx->md_info );
1008 int md_type = md_get_type( md_ctx->md_info );
1009
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001010 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001011 if( md_type == POLARSSL_MD_MD5 )
1012 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001013 else
Paul Bakker68884e32013-01-07 18:20:04 +01001014 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
1016 memcpy( header, ctr, 8 );
1017 header[ 8] = (unsigned char) type;
1018 header[ 9] = (unsigned char)( len >> 8 );
1019 header[10] = (unsigned char)( len );
1020
Paul Bakker68884e32013-01-07 18:20:04 +01001021 memset( padding, 0x36, padlen );
1022 md_starts( md_ctx );
1023 md_update( md_ctx, secret, md_size );
1024 md_update( md_ctx, padding, padlen );
1025 md_update( md_ctx, header, 11 );
1026 md_update( md_ctx, buf, len );
1027 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker68884e32013-01-07 18:20:04 +01001029 memset( padding, 0x5C, padlen );
1030 md_starts( md_ctx );
1031 md_update( md_ctx, secret, md_size );
1032 md_update( md_ctx, padding, padlen );
1033 md_update( md_ctx, buf + len, md_size );
1034 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001035}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001036#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038/*
1039 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001040 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041static int ssl_encrypt_buf( ssl_context *ssl )
1042{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001043 const cipher_mode_t mode = cipher_get_cipher_mode(
1044 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1047
1048 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001049 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001051#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1052 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1053 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001054 if( mode != POLARSSL_MODE_GCM &&
1055 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001057#if defined(POLARSSL_SSL_PROTO_SSL3)
1058 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1059 {
1060 ssl_mac( &ssl->transform_out->md_ctx_enc,
1061 ssl->transform_out->mac_enc,
1062 ssl->out_msg, ssl->out_msglen,
1063 ssl->out_ctr, ssl->out_msgtype );
1064 }
1065 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001066#endif
1067#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001068 defined(POLARSSL_SSL_PROTO_TLS1_2)
1069 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1070 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001071 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1072 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1073 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001074 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1075 ssl->out_msg, ssl->out_msglen );
1076 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1077 ssl->out_msg + ssl->out_msglen );
1078 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1079 }
1080 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001081#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 {
1083 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001084 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001085 }
1086
1087 SSL_DEBUG_BUF( 4, "computed mac",
1088 ssl->out_msg + ssl->out_msglen,
1089 ssl->transform_out->maclen );
1090
1091 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001092 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001093#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001095 /*
1096 * Encrypt
1097 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001099 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001101 int ret;
1102 size_t olen = 0;
1103
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1105 "including %d bytes of padding",
1106 ssl->out_msglen, 0 ) );
1107
1108 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1109 ssl->out_msg, ssl->out_msglen );
1110
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001111 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001112 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001113 ssl->transform_out->ivlen,
1114 ssl->out_msg, ssl->out_msglen,
1115 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001116 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001117 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001118 return( ret );
1119 }
1120
1121 if( ssl->out_msglen != olen )
1122 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001123 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001124 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 }
Paul Bakker68884e32013-01-07 18:20:04 +01001127 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001129#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1130 if( mode == POLARSSL_MODE_GCM ||
1131 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001133 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001134 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135 unsigned char *enc_msg;
1136 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001137 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1138 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140 memcpy( add_data, ssl->out_ctr, 8 );
1141 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001142 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1143 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001144 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1145 add_data[12] = ssl->out_msglen & 0xFF;
1146
1147 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1148 add_data, 13 );
1149
Paul Bakker68884e32013-01-07 18:20:04 +01001150 /*
1151 * Generate IV
1152 */
1153 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001154 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1155 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001156 if( ret != 0 )
1157 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Paul Bakker68884e32013-01-07 18:20:04 +01001159 memcpy( ssl->out_iv,
1160 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1161 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001162
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001163 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001165
Paul Bakker68884e32013-01-07 18:20:04 +01001166 /*
1167 * Fix pointer positions and message length with added IV
1168 */
1169 enc_msg = ssl->out_msg;
1170 enc_msglen = ssl->out_msglen;
1171 ssl->out_msglen += ssl->transform_out->ivlen -
1172 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001173
Paul Bakker68884e32013-01-07 18:20:04 +01001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Paul Bakker68884e32013-01-07 18:20:04 +01001178 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1179 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001180
Paul Bakker68884e32013-01-07 18:20:04 +01001181 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001182 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001184 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1185 ssl->transform_out->iv_enc,
1186 ssl->transform_out->ivlen,
1187 add_data, 13,
1188 enc_msg, enc_msglen,
1189 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001190 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001191 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 return( ret );
1194 }
1195
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001197 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001198 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001199 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001200 }
1201
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001203
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001207#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001208#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1209 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001210 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001212 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001214 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215
Paul Bakker48916f92012-09-16 19:57:18 +00001216 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1217 ssl->transform_out->ivlen;
1218 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 padlen = 0;
1220
1221 for( i = 0; i <= padlen; i++ )
1222 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1223
1224 ssl->out_msglen += padlen + 1;
1225
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 enc_msglen = ssl->out_msglen;
1227 enc_msg = ssl->out_msg;
1228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001229#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001231 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1232 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001234 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 {
1236 /*
1237 * Generate IV
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1240 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001241 if( ret != 0 )
1242 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246
1247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001250 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001251 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001252 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001254#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001258 ssl->out_msglen, ssl->transform_out->ivlen,
1259 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001262 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001264 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001265 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001266 ssl->transform_out->ivlen,
1267 enc_msg, enc_msglen,
1268 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001270 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 return( ret );
1272 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( enc_msglen != olen )
1275 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001276 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001277 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279
1280#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1282 {
1283 /*
1284 * Save IV in SSL3 and TLS1
1285 */
1286 memcpy( ssl->transform_out->iv_enc,
1287 ssl->transform_out->cipher_ctx_enc.iv,
1288 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001293#endif /* POLARSSL_CIPHER_MODE_CBC &&
1294 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001295 {
1296 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1301
1302 return( 0 );
1303}
1304
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001305#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001306
Paul Bakker5121ce52009-01-03 21:22:43 +00001307static int ssl_decrypt_buf( ssl_context *ssl )
1308{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001309 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001310 const cipher_mode_t mode = cipher_get_cipher_mode(
1311 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001312#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1313 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1314 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1315 size_t padlen = 0, correct = 1;
1316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1319
Paul Bakker48916f92012-09-16 19:57:18 +00001320 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 {
1322 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001324 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001327#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001328 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001329 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001330 int ret;
1331 size_t olen = 0;
1332
Paul Bakker68884e32013-01-07 18:20:04 +01001333 padlen = 0;
1334
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001336 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001337 ssl->transform_in->ivlen,
1338 ssl->in_msg, ssl->in_msglen,
1339 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001342 return( ret );
1343 }
1344
1345 if( ssl->in_msglen != olen )
1346 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001347 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001348 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
Paul Bakker68884e32013-01-07 18:20:04 +01001351 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001352#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001353#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1354 if( mode == POLARSSL_MODE_GCM ||
1355 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001357 int ret;
1358 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 unsigned char *dec_msg;
1360 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001362 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1363 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001364 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1365 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001366
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001367 if( ssl->in_msglen < explicit_iv_len + taglen )
1368 {
1369 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1370 "+ taglen (%d)", ssl->in_msglen,
1371 explicit_iv_len, taglen ) );
1372 return( POLARSSL_ERR_SSL_INVALID_MAC );
1373 }
1374 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1375
Paul Bakker68884e32013-01-07 18:20:04 +01001376 dec_msg = ssl->in_msg;
1377 dec_msg_result = ssl->in_msg;
1378 ssl->in_msglen = dec_msglen;
1379
1380 memcpy( add_data, ssl->in_ctr, 8 );
1381 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001382 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1383 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001384 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1385 add_data[12] = ssl->in_msglen & 0xFF;
1386
1387 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1388 add_data, 13 );
1389
1390 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1391 ssl->in_iv,
1392 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1393
1394 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1395 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001396 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001397
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001398 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001399 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001400 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001401 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1402 ssl->transform_in->iv_dec,
1403 ssl->transform_in->ivlen,
1404 add_data, 13,
1405 dec_msg, dec_msglen,
1406 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001407 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001409 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1410
1411 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1412 return( POLARSSL_ERR_SSL_INVALID_MAC );
1413
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001414 return( ret );
1415 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001416
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001417 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001418 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001419 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001420 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001421 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001425#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1426 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001427 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Paul Bakker45829992013-01-03 14:52:21 +01001429 /*
1430 * Decrypt and check the padding
1431 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 unsigned char *dec_msg;
1434 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001435 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001436 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 /*
Paul Bakker45829992013-01-03 14:52:21 +01001440 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 */
Paul Bakker48916f92012-09-16 19:57:18 +00001442 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
1444 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001445 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001446 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001450 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1451 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#endif
Paul Bakker45829992013-01-03 14:52:21 +01001453
1454 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1455 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1456 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001457 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1458 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1459 ssl->transform_in->ivlen,
1460 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001461 return( POLARSSL_ERR_SSL_INVALID_MAC );
1462 }
1463
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 dec_msglen = ssl->in_msglen;
1465 dec_msg = ssl->in_msg;
1466 dec_msg_result = ssl->in_msg;
1467
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001470 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001471 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001472 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473 {
Paul Bakker48916f92012-09-16 19:57:18 +00001474 dec_msglen -= ssl->transform_in->ivlen;
1475 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476
Paul Bakker48916f92012-09-16 19:57:18 +00001477 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001478 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001480#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001481
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001482 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001483 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001484 ssl->transform_in->ivlen,
1485 dec_msg, dec_msglen,
1486 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001487 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001488 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001489 return( ret );
1490 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001491
Paul Bakkercca5b812013-08-31 17:40:26 +02001492 if( dec_msglen != olen )
1493 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001494 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001495 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001496 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001497
1498#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001499 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1500 {
1501 /*
1502 * Save IV in SSL3 and TLS1
1503 */
1504 memcpy( ssl->transform_in->iv_dec,
1505 ssl->transform_in->cipher_ctx_dec.iv,
1506 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001511
1512 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1513 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001514#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001515 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1516 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001517#endif
Paul Bakker45829992013-01-03 14:52:21 +01001518 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001519 correct = 0;
1520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1524 {
Paul Bakker48916f92012-09-16 19:57:18 +00001525 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001527#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1529 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001530 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001531#endif
Paul Bakker45829992013-01-03 14:52:21 +01001532 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 }
1534 }
1535 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001536#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001537#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1538 defined(POLARSSL_SSL_PROTO_TLS1_2)
1539 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 {
1541 /*
Paul Bakker45829992013-01-03 14:52:21 +01001542 * TLSv1+: always check the padding up to the first failure
1543 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001545 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001546 size_t padding_idx = ssl->in_msglen - padlen - 1;
1547
Paul Bakker956c9e02013-12-19 14:42:28 +01001548 /*
1549 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001550 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001551 *
Paul Bakker61885c72014-04-25 12:59:03 +02001552 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1553 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001554 *
1555 * In both cases we reset padding_idx to a safe value (0) to
1556 * prevent out-of-buffer reads.
1557 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001558 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001559 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1560 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001561
1562 padding_idx *= correct;
1563
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001564 for( i = 1; i <= 256; i++ )
1565 {
1566 real_count &= ( i <= padlen );
1567 pad_count += real_count *
1568 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1569 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570
1571 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001572
Paul Bakkerd66f0702013-01-31 16:57:45 +01001573#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001574 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001575 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001577 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579 else
1580#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1581 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001582 {
1583 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001584 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001587 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001588#endif /* POLARSSL_CIPHER_MODE_CBC &&
1589 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001590 {
1591 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001592 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1596 ssl->in_msg, ssl->in_msglen );
1597
1598 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001599 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001601#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1602 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1603 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001604 if( mode != POLARSSL_MODE_GCM &&
1605 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001607 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001611 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1612 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001616#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001617 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1618 {
1619 ssl_mac( &ssl->transform_in->md_ctx_dec,
1620 ssl->transform_in->mac_dec,
1621 ssl->in_msg, ssl->in_msglen,
1622 ssl->in_ctr, ssl->in_msgtype );
1623 }
1624 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#endif /* POLARSSL_SSL_PROTO_SSL3 */
1626#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 defined(POLARSSL_SSL_PROTO_TLS1_2)
1628 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1629 {
1630 /*
1631 * Process MAC and always update for padlen afterwards to make
1632 * total time independent of padlen
1633 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001634 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 *
1636 * Known timing attacks:
1637 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1638 *
1639 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1640 * correctly. (We round down instead of up, so -56 is the correct
1641 * value for our calculations instead of -55)
1642 */
1643 size_t j, extra_run = 0;
1644 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1645 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001646
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001648
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001649 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1650 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1651 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1653 ssl->in_msglen );
1654 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1655 ssl->in_msg + ssl->in_msglen );
1656 for( j = 0; j < extra_run; j++ )
1657 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001658
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1660 }
1661 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001662#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 POLARSSL_SSL_PROTO_TLS1_2 */
1664 {
1665 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001666 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1670 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1671 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001673 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001674 ssl->transform_in->maclen ) != 0 )
1675 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001676#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001678#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 correct = 0;
1680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 /*
1683 * Finally check the correct flag
1684 */
1685 if( correct == 0 )
1686 return( POLARSSL_ERR_SSL_INVALID_MAC );
1687 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001688#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 if( ssl->in_msglen == 0 )
1691 {
1692 ssl->nb_zero++;
1693
1694 /*
1695 * Three or more empty messages may be a DoS attack
1696 * (excessive CPU consumption).
1697 */
1698 if( ssl->nb_zero > 3 )
1699 {
1700 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1701 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001702 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 }
1704 }
1705 else
1706 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001707
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001708 /* Input counter not used with DTLS right now,
1709 * but it doesn't hurt to have this part ready */
1710 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1711 if( ++ssl->in_ctr[i - 1] != 0 )
1712 break;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001713
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001714 /* The loop goes to its end iff the counter is wrapping */
1715 if( i == ssl_ep_len( ssl ) )
1716 {
1717 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1718 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001719 }
1720
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1722
1723 return( 0 );
1724}
1725
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726#if defined(POLARSSL_ZLIB_SUPPORT)
1727/*
1728 * Compression/decompression functions
1729 */
1730static int ssl_compress_buf( ssl_context *ssl )
1731{
1732 int ret;
1733 unsigned char *msg_post = ssl->out_msg;
1734 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001735 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001736
1737 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1738
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001739 if( len_pre == 0 )
1740 return( 0 );
1741
Paul Bakker2770fbd2012-07-03 13:30:23 +00001742 memcpy( msg_pre, ssl->out_msg, len_pre );
1743
1744 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1745 ssl->out_msglen ) );
1746
1747 SSL_DEBUG_BUF( 4, "before compression: output payload",
1748 ssl->out_msg, ssl->out_msglen );
1749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1751 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1752 ssl->transform_out->ctx_deflate.next_out = msg_post;
1753 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754
Paul Bakker48916f92012-09-16 19:57:18 +00001755 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756 if( ret != Z_OK )
1757 {
1758 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1760 }
1761
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001762 ssl->out_msglen = SSL_BUFFER_LEN -
1763 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764
Paul Bakker2770fbd2012-07-03 13:30:23 +00001765 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1766 ssl->out_msglen ) );
1767
1768 SSL_DEBUG_BUF( 4, "after compression: output payload",
1769 ssl->out_msg, ssl->out_msglen );
1770
1771 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1772
1773 return( 0 );
1774}
1775
1776static int ssl_decompress_buf( ssl_context *ssl )
1777{
1778 int ret;
1779 unsigned char *msg_post = ssl->in_msg;
1780 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001781 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782
1783 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1784
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001785 if( len_pre == 0 )
1786 return( 0 );
1787
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788 memcpy( msg_pre, ssl->in_msg, len_pre );
1789
1790 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1791 ssl->in_msglen ) );
1792
1793 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1794 ssl->in_msg, ssl->in_msglen );
1795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1797 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1798 ssl->transform_in->ctx_inflate.next_out = msg_post;
1799 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800
Paul Bakker48916f92012-09-16 19:57:18 +00001801 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802 if( ret != Z_OK )
1803 {
1804 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1805 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1806 }
1807
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001808 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1809 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1812 ssl->in_msglen ) );
1813
1814 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1815 ssl->in_msg, ssl->in_msglen );
1816
1817 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1818
1819 return( 0 );
1820}
1821#endif /* POLARSSL_ZLIB_SUPPORT */
1822
Paul Bakker5121ce52009-01-03 21:22:43 +00001823/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001824 * Fill the input message buffer by appending data to it.
1825 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001826 *
1827 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1828 * available (from this read and/or a previous one). Otherwise, an error code
1829 * is returned (possibly EOF or WANT_READ).
1830 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001831 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1832 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1833 * since we always read a whole datagram at once.
1834 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001835 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001836 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 */
Paul Bakker23986e52011-04-24 08:57:21 +00001838int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001839{
Paul Bakker23986e52011-04-24 08:57:21 +00001840 int ret;
1841 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
1843 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1844
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001845 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1846 {
1847 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1848 "or ssl_set_bio_timeout()" ) );
1849 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1850 }
1851
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001852 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001853 {
1854 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1855 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1856 }
1857
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001858#if defined(POLARSSL_SSL_PROTO_DTLS)
1859 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001861 /*
1862 * The point is, we need to always read a full datagram at once, so we
1863 * sometimes read more then requested, and handle the additional data.
1864 * It could be the rest of the current record (while fetching the
1865 * header) and/or some other records in the same datagram.
1866 */
1867
1868 /*
1869 * Move to the next record in the already read datagram if applicable
1870 */
1871 if( ssl->next_record_offset != 0 )
1872 {
1873 if( ssl->in_left < ssl->next_record_offset )
1874 {
1875 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1876 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1877 }
1878
1879 ssl->in_left -= ssl->next_record_offset;
1880
1881 if( ssl->in_left != 0 )
1882 {
1883 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1884 ssl->next_record_offset ) );
1885 memmove( ssl->in_hdr,
1886 ssl->in_hdr + ssl->next_record_offset,
1887 ssl->in_left );
1888 }
1889
1890 ssl->next_record_offset = 0;
1891 }
1892
Paul Bakker5121ce52009-01-03 21:22:43 +00001893 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1894 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001895
1896 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001897 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001898 */
1899 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001900 {
1901 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001902 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001903 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001904
1905 /*
1906 * A record can't be split accross datagrams. If we need to read but
1907 * are not at the beginning of a new record, the caller did something
1908 * wrong.
1909 */
1910 if( ssl->in_left != 0 )
1911 {
1912 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1913 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1914 }
1915
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001916 // TODO-DTLS: for now, use constant timeout = 1 sec/datagram
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001917 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001918 if( ssl->f_recv_timeout != NULL &&
1919 ssl->handshake != NULL ) /* No resend outside handshake */
1920 {
1921 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, 1 );
1922 }
1923 else
1924 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001925
Paul Bakker5121ce52009-01-03 21:22:43 +00001926 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1927
Paul Bakker831a7552011-05-18 13:32:51 +00001928 if( ret == 0 )
1929 return( POLARSSL_ERR_SSL_CONN_EOF );
1930
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001931 if( ret == POLARSSL_ERR_NET_TIMEOUT )
1932 {
1933 SSL_DEBUG_MSG( 2, ( "recv timeout" ) );
1934
1935 if( ( ret = ssl_resend( ssl ) ) != 0 )
1936 {
1937 SSL_DEBUG_RET( 1, "ssl_resend", ret );
1938 return( ret );
1939 }
1940
1941 return( POLARSSL_ERR_NET_WANT_READ );
1942 }
1943
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 if( ret < 0 )
1945 return( ret );
1946
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001947 ssl->in_left = ret;
1948 }
1949 else
1950#endif
1951 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001952 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1953 ssl->in_left, nb_want ) );
1954
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001955 while( ssl->in_left < nb_want )
1956 {
1957 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001958 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001959
1960 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1961 ssl->in_left, nb_want ) );
1962 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1963
1964 if( ret == 0 )
1965 return( POLARSSL_ERR_SSL_CONN_EOF );
1966
1967 if( ret < 0 )
1968 return( ret );
1969
1970 ssl->in_left += ret;
1971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 }
1973
1974 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1975
1976 return( 0 );
1977}
1978
1979/*
1980 * Flush any data not yet written
1981 */
1982int ssl_flush_output( ssl_context *ssl )
1983{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001984 int ret;
1985 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
1987 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1988
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001989 if( ssl->f_send == NULL )
1990 {
1991 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1992 "or ssl_set_bio_timeout()" ) );
1993 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1994 }
1995
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001996 /* Avoid incrementing counter if data is flushed */
1997 if( ssl->out_left == 0 )
1998 {
1999 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2000 return( 0 );
2001 }
2002
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 while( ssl->out_left > 0 )
2004 {
2005 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002006 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002007
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002008 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2009 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002010 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002011
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2013
2014 if( ret <= 0 )
2015 return( ret );
2016
2017 ssl->out_left -= ret;
2018 }
2019
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002020 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002021 if( ++ssl->out_ctr[i - 1] != 0 )
2022 break;
2023
2024 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002025 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002026 {
2027 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2028 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2029 }
2030
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2032
2033 return( 0 );
2034}
2035
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002036/*
2037 * Functions to handle the DTLS retransmission state machine
2038 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002039#if defined(POLARSSL_SSL_PROTO_DTLS)
2040/*
2041 * Append current handshake message to current outgoing flight
2042 */
2043static int ssl_flight_append( ssl_context *ssl )
2044{
2045 ssl_flight_item *msg;
2046
2047 /* Allocate space for current message */
2048 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2049 {
2050 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2051 sizeof( ssl_flight_item ) ) );
2052 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2053 }
2054
2055 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2056 {
2057 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2058 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2059 }
2060
2061 /* Copy current handshake message with headers */
2062 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2063 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002064 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002065 msg->next = NULL;
2066
2067 /* Append to the current flight */
2068 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002069 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002070 else
2071 {
2072 ssl_flight_item *cur = ssl->handshake->flight;
2073 while( cur->next != NULL )
2074 cur = cur->next;
2075 cur->next = msg;
2076 }
2077
2078 return( 0 );
2079}
2080
2081/*
2082 * Free the current flight of handshake messages
2083 */
2084static void ssl_flight_free( ssl_flight_item *flight )
2085{
2086 ssl_flight_item *cur = flight;
2087 ssl_flight_item *next;
2088
2089 while( cur != NULL )
2090 {
2091 next = cur->next;
2092
2093 polarssl_free( cur->p );
2094 polarssl_free( cur );
2095
2096 cur = next;
2097 }
2098}
2099
2100/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002101 * Swap transform_out and out_ctr with the alternative ones
2102 */
2103static void ssl_swap_epochs( ssl_context *ssl )
2104{
2105 ssl_transform *tmp_transform;
2106 unsigned char tmp_out_ctr[8];
2107
2108 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2109 {
2110 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2111 return;
2112 }
2113
2114 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2115
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002116 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002117 tmp_transform = ssl->transform_out;
2118 ssl->transform_out = ssl->handshake->alt_transform_out;
2119 ssl->handshake->alt_transform_out = tmp_transform;
2120
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002121 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002122 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2123 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2124 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002125
2126 /* Adjust to the newly activated transform */
2127 if( ssl->transform_out != NULL &&
2128 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2129 {
2130 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2131 ssl->transform_out->fixed_ivlen;
2132 }
2133 else
2134 ssl->out_msg = ssl->out_iv;
2135
2136#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2137 if( ssl_hw_record_activate != NULL )
2138 {
2139 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2140 {
2141 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2142 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2143 }
2144 }
2145#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002146}
2147
2148/*
2149 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002150 *
2151 * Need to remember the current message in case flush_output returns
2152 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002153 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002154 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002155int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002156{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002157 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002158
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002159 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2160 {
2161 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2162
2163 ssl->handshake->cur_msg = ssl->handshake->flight;
2164 ssl_swap_epochs( ssl );
2165
2166 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2167 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002168
2169 while( ssl->handshake->cur_msg != NULL )
2170 {
2171 int ret;
2172 ssl_flight_item *cur = ssl->handshake->cur_msg;
2173
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002174 /* Swap epochs before sending Finished: we can't do it after
2175 * sending ChangeCipherSpec, in case write returns WANT_READ.
2176 * Must be done before copying, may change out_msg pointer */
2177 if( cur->type == SSL_MSG_HANDSHAKE &&
2178 cur->p[0] == SSL_HS_FINISHED )
2179 {
2180 ssl_swap_epochs( ssl );
2181 }
2182
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002183 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002184 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002185 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002186
2187 ssl->handshake->cur_msg = cur->next;
2188
2189 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2190
2191 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2192 {
2193 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2194 return( ret );
2195 }
2196 }
2197
2198 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2199
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002200 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002201
2202 return( 0 );
2203}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002204
2205/*
2206 * To be called when the last message of an incoming flight is received.
2207 */
2208void ssl_recv_flight_completed( ssl_context *ssl )
2209{
2210 /* We won't need to resend that one any more */
2211 ssl_flight_free( ssl->handshake->flight );
2212 ssl->handshake->flight = NULL;
2213 ssl->handshake->cur_msg = NULL;
2214
2215 /* The next incoming flight will start with this msg_seq */
2216 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2217
2218 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2219 ssl->in_msg[0] == SSL_HS_FINISHED )
2220 {
2221 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2222 }
2223 else
2224 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2225}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002226#endif /* POLARSSL_SSL_PROTO_DTLS */
2227
Paul Bakker5121ce52009-01-03 21:22:43 +00002228/*
2229 * Record layer functions
2230 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002231
2232/*
2233 * Write current record.
2234 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2235 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002236int ssl_write_record( ssl_context *ssl )
2237{
Paul Bakker05ef8352012-05-08 09:17:57 +00002238 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002239 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2242
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002243#if defined(POLARSSL_SSL_PROTO_DTLS)
2244 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2245 ssl->handshake != NULL &&
2246 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2247 {
2248 ; /* Skip special handshake treatment when resending */
2249 }
2250 else
2251#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2253 {
2254 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2255 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2256 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2257
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002258 /*
2259 * DTLS has additional fields in the Handshake layer,
2260 * between the length field and the actual payload:
2261 * uint16 message_seq;
2262 * uint24 fragment_offset;
2263 * uint24 fragment_length;
2264 */
2265#if defined(POLARSSL_SSL_PROTO_DTLS)
2266 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2267 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002268 /* Make room for the additional DTLS fields */
2269 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002270 ssl->out_msglen += 8;
2271 len += 8;
2272
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002273 /* Write message_seq and update it, except for HelloRequest */
2274 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2275 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002276 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2277 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2278 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002279 }
2280 else
2281 {
2282 ssl->out_msg[4] = 0;
2283 ssl->out_msg[5] = 0;
2284 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002285
2286 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2287 memset( ssl->out_msg + 6, 0x00, 3 );
2288 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002289 }
2290#endif /* POLARSSL_SSL_PROTO_DTLS */
2291
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002292 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2293 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
2295
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002296 /* Save handshake and CCS messages for resending */
2297#if defined(POLARSSL_SSL_PROTO_DTLS)
2298 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2299 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002300 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002301 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2302 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2303 {
2304 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2305 {
2306 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2307 return( ret );
2308 }
2309 }
2310#endif
2311
Paul Bakker2770fbd2012-07-03 13:30:23 +00002312#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002313 if( ssl->transform_out != NULL &&
2314 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002315 {
2316 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2317 {
2318 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2319 return( ret );
2320 }
2321
2322 len = ssl->out_msglen;
2323 }
2324#endif /*POLARSSL_ZLIB_SUPPORT */
2325
Paul Bakker05ef8352012-05-08 09:17:57 +00002326#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002327 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002329 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002330
Paul Bakker05ef8352012-05-08 09:17:57 +00002331 ret = ssl_hw_record_write( ssl );
2332 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2333 {
2334 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002335 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002336 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002337
2338 if( ret == 0 )
2339 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002340 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002341#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002342 if( !done )
2343 {
2344 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002345 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2346 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002347
2348 ssl->out_len[0] = (unsigned char)( len >> 8 );
2349 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002350
Paul Bakker48916f92012-09-16 19:57:18 +00002351 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002352 {
2353 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2354 {
2355 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2356 return( ret );
2357 }
2358
2359 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002360 ssl->out_len[0] = (unsigned char)( len >> 8 );
2361 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002362 }
2363
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002364 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002365
2366 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2367 "version = [%d:%d], msglen = %d",
2368 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002369 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002370
2371 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002372 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 }
2374
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2376 {
2377 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2378 return( ret );
2379 }
2380
2381 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2382
2383 return( 0 );
2384}
2385
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002386#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002387/*
2388 * Mark bits in bitmask (used for DTLS HS reassembly)
2389 */
2390static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2391{
2392 unsigned int start_bits, end_bits;
2393
2394 start_bits = 8 - ( offset % 8 );
2395 if( start_bits != 8 )
2396 {
2397 size_t first_byte_idx = offset / 8;
2398
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002399 /* Special case */
2400 if( len <= start_bits )
2401 {
2402 for( ; len != 0; len-- )
2403 mask[first_byte_idx] |= 1 << ( start_bits - len );
2404
2405 /* Avoid potential issues with offset or len becoming invalid */
2406 return;
2407 }
2408
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002409 offset += start_bits; /* Now offset % 8 == 0 */
2410 len -= start_bits;
2411
2412 for( ; start_bits != 0; start_bits-- )
2413 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2414 }
2415
2416 end_bits = len % 8;
2417 if( end_bits != 0 )
2418 {
2419 size_t last_byte_idx = ( offset + len ) / 8;
2420
2421 len -= end_bits; /* Now len % 8 == 0 */
2422
2423 for( ; end_bits != 0; end_bits-- )
2424 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2425 }
2426
2427 memset( mask + offset / 8, 0xFF, len / 8 );
2428}
2429
2430/*
2431 * Check that bitmask is full
2432 */
2433static int ssl_bitmask_check( unsigned char *mask, size_t len )
2434{
2435 size_t i;
2436
2437 for( i = 0; i < len / 8; i++ )
2438 if( mask[i] != 0xFF )
2439 return( -1 );
2440
2441 for( i = 0; i < len % 8; i++ )
2442 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2443 return( -1 );
2444
2445 return( 0 );
2446}
2447
2448/*
2449 * Reassemble fragmented DTLS handshake messages.
2450 *
2451 * Use a temporary buffer for reassembly, divided in two parts:
2452 * - the first holds the reassembled message (including handshake header),
2453 * - the second holds a bitmask indicating which parts of the message
2454 * (excluding headers) have been received so far.
2455 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002456static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2457{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002458 unsigned char *msg, *bitmask;
2459 size_t frag_len, frag_off;
2460 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2461
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002462 if( ssl->handshake == NULL )
2463 {
2464 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2465 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2466 }
2467
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002468 /*
2469 * For first fragment, check size and allocate buffer
2470 */
2471 if( ssl->handshake->hs_msg == NULL )
2472 {
2473 size_t alloc_len;
2474
2475 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2476 msg_len ) );
2477
2478 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2479 {
2480 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2481 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2482 }
2483
2484 /* The bitmask needs one bit per byte of message excluding header */
2485 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2486
2487 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2488 if( ssl->handshake->hs_msg == NULL )
2489 {
2490 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2491 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2492 }
2493
2494 memset( ssl->handshake->hs_msg, 0, alloc_len );
2495
2496 /* Prepare final header: copy msg_type, length and message_seq,
2497 * then add standardised fragment_offset and fragment_length */
2498 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2499 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2500 memcpy( ssl->handshake->hs_msg + 9,
2501 ssl->handshake->hs_msg + 1, 3 );
2502 }
2503 else
2504 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002505 /* Make sure msg_type and length are consistent */
2506 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002507 {
2508 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2509 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2510 }
2511 }
2512
2513 msg = ssl->handshake->hs_msg + 12;
2514 bitmask = msg + msg_len;
2515
2516 /*
2517 * Check and copy current fragment
2518 */
2519 frag_off = ( ssl->in_msg[6] << 16 ) |
2520 ( ssl->in_msg[7] << 8 ) |
2521 ssl->in_msg[8];
2522 frag_len = ( ssl->in_msg[9] << 16 ) |
2523 ( ssl->in_msg[10] << 8 ) |
2524 ssl->in_msg[11];
2525
2526 if( frag_off + frag_len > msg_len )
2527 {
2528 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2529 frag_off, frag_len, msg_len ) );
2530 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2531 }
2532
2533 if( frag_len + 12 > ssl->in_msglen )
2534 {
2535 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2536 frag_len, ssl->in_msglen ) );
2537 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2538 }
2539
2540 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2541 frag_off, frag_len ) );
2542
2543 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2544 ssl_bitmask_set( bitmask, frag_off, frag_len );
2545
2546 /*
2547 * Do we have the complete message by now?
2548 * If yes, finalize it, else ask to read the next record.
2549 */
2550 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2551 {
2552 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2553 return( POLARSSL_ERR_NET_WANT_READ );
2554 }
2555
2556 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2557
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002558 if( ssl->in_left > ssl->next_record_offset )
2559 {
2560 /*
2561 * We've got more data in the buffer after the current record,
2562 * that we don't want to overwrite. Move it before writing the
2563 * reassembled message, and adjust in_left and next_record_offset.
2564 */
2565 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2566 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2567 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2568
2569 /* First compute and check new lengths */
2570 ssl->next_record_offset = new_remain - ssl->in_hdr;
2571 ssl->in_left = ssl->next_record_offset + remain_len;
2572
2573 if( ssl->in_left > SSL_BUFFER_LEN -
2574 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2577 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2578 }
2579
2580 memmove( new_remain, cur_remain, remain_len );
2581 }
2582
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002583 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2584
2585 polarssl_free( ssl->handshake->hs_msg );
2586 ssl->handshake->hs_msg = NULL;
2587
2588 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2589 ssl->in_msg, ssl->in_hslen );
2590
2591 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002592}
2593#endif /* POLARSSL_SSL_PROTO_DTLS */
2594
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002595static int ssl_prepare_handshake_record( ssl_context *ssl )
2596{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002597 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2598 {
2599 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2600 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002601 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002602 }
2603
2604 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2605 ( ssl->in_msg[1] << 16 ) |
2606 ( ssl->in_msg[2] << 8 ) |
2607 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002608
2609 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2610 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002611 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002612
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002613#if defined(POLARSSL_SSL_PROTO_DTLS)
2614 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002615 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002616 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002617 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002618
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002619 /* ssl->handshake is NULL when receiving ClientHello for renego */
2620 if( ssl->handshake != NULL &&
2621 recv_msg_seq != ssl->handshake->in_msg_seq )
2622 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002623 /* No sane server ever retransmits HelloVerifyRequest */
2624 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2625 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002626 {
2627 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2628 "message_seq = %d, start_of_flight = %d",
2629 recv_msg_seq,
2630 ssl->handshake->in_flight_start_seq ) );
2631
2632 if( ( ret = ssl_resend( ssl ) ) != 0 )
2633 {
2634 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2635 return( ret );
2636 }
2637 }
2638 else
2639 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002640 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002641 "message_seq = %d, expected = %d",
2642 recv_msg_seq,
2643 ssl->handshake->in_msg_seq ) );
2644 }
2645
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002646 return( POLARSSL_ERR_NET_WANT_READ );
2647 }
2648 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002649
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002650 /* Reassemble if current message is fragmented or reassembly is
2651 * already in progress */
2652 if( ssl->in_msglen < ssl->in_hslen ||
2653 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2654 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2655 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002656 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002657 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2658
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002659 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2660 {
2661 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2662 return( ret );
2663 }
2664 }
2665 }
2666 else
2667#endif /* POLARSSL_SSL_PROTO_DTLS */
2668 /* With TLS we don't handle fragmentation (for now) */
2669 if( ssl->in_msglen < ssl->in_hslen )
2670 {
2671 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002672 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002673 }
2674
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002675 if( ssl->state != SSL_HANDSHAKE_OVER )
2676 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2677
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002678 /* Handshake message is complete, increment counter */
2679#if defined(POLARSSL_SSL_PROTO_DTLS)
2680 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2681 ssl->handshake != NULL )
2682 {
2683 ssl->handshake->in_msg_seq++;
2684 }
2685#endif
2686
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002687 return( 0 );
2688}
2689
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002690/*
2691 * ContentType type;
2692 * ProtocolVersion version;
2693 * uint16 epoch; // DTLS only
2694 * uint48 sequence_number; // DTLS only
2695 * uint16 length;
2696 */
2697static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002698{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002699 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002700 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002701
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002702 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2703
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002705 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002706 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
2708 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2709 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002710 ssl->in_msgtype,
2711 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002713 /* Check record type */
2714 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2715 ssl->in_msgtype != SSL_MSG_ALERT &&
2716 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2717 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2718 {
2719 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002720
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002721 if( ( ret = ssl_send_alert_message( ssl,
2722 SSL_ALERT_LEVEL_FATAL,
2723 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2724 {
2725 return( ret );
2726 }
2727
2728 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2729 }
2730
2731 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002732 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 {
2734 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002735 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
2737
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002738 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 {
2740 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002741 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
2743
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002744 /* Check epoch with DTLS */
2745#if defined(POLARSSL_SSL_PROTO_DTLS)
2746 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2747 {
2748 unsigned int exp_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
2749 unsigned int rec_epoch = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2750
2751 if( exp_epoch != rec_epoch )
2752 {
2753 SSL_DEBUG_MSG( 1, ( "discarding record from another epoch: "
2754 "expected %d, received %d",
2755 exp_epoch, rec_epoch ) );
2756 return( POLARSSL_ERR_NET_WANT_READ );
2757 }
2758 }
2759#endif
2760
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002761 /* Check length against the size of our buffer */
2762 if( ssl->in_msglen > SSL_BUFFER_LEN
2763 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002764 {
2765 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2766 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2767 }
2768
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002769 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002770 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002772 if( ssl->in_msglen < 1 ||
2773 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002774 {
2775 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002776 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 }
2778 }
2779 else
2780 {
Paul Bakker48916f92012-09-16 19:57:18 +00002781 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002782 {
2783 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002784 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 }
2786
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002787#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002789 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 {
2791 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002792 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002795#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2796 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 /*
2798 * TLS encrypted messages can have up to 256 bytes of padding
2799 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002800 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002801 ssl->in_msglen > ssl->transform_in->minlen +
2802 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002803 {
2804 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002805 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002808 }
2809
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002810 return( 0 );
2811}
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002813/*
2814 * If applicable, decrypt (and decompress) record content
2815 */
2816static int ssl_prepare_record_content( ssl_context *ssl )
2817{
2818 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002821 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002822
Paul Bakker05ef8352012-05-08 09:17:57 +00002823#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002824 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002825 {
2826 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2827
2828 ret = ssl_hw_record_read( ssl );
2829 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2830 {
2831 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002832 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002833 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002834
2835 if( ret == 0 )
2836 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002837 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002838#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002839 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 {
2841 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2842 {
2843 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2844 return( ret );
2845 }
2846
2847 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2848 ssl->in_msg, ssl->in_msglen );
2849
2850 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2851 {
2852 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002853 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 }
2855 }
2856
Paul Bakker2770fbd2012-07-03 13:30:23 +00002857#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002858 if( ssl->transform_in != NULL &&
2859 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002860 {
2861 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2862 {
2863 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2864 return( ret );
2865 }
2866
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002867 // TODO: what's the purpose of these lines? is in_len used?
2868 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2869 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002870 }
2871#endif /* POLARSSL_ZLIB_SUPPORT */
2872
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002873 return( 0 );
2874}
2875
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002876static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
2877
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002878/*
2879 * Read a record.
2880 *
2881 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
2882 * and continue reading until a valid record is found.
2883 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002884int ssl_read_record( ssl_context *ssl )
2885{
2886 int ret;
2887
2888 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2889
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02002890 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002891 {
2892 /*
2893 * Get next Handshake message in the current record
2894 */
2895 ssl->in_msglen -= ssl->in_hslen;
2896
2897 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
2898 ssl->in_msglen );
2899
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002900 SSL_DEBUG_BUF( 4, "remaining content in record",
2901 ssl->in_msg, ssl->in_msglen );
2902
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002903 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2904 return( ret );
2905
2906 return( 0 );
2907 }
2908
2909 ssl->in_hslen = 0;
2910
2911 /*
2912 * Read the record header and parse it
2913 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002914#if defined(POLARSSL_SSL_PROTO_DTLS)
2915read_record_header:
2916#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002917 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
2918 {
2919 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2920 return( ret );
2921 }
2922
2923 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002924 {
2925#if defined(POLARSSL_SSL_PROTO_DTLS)
2926 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2927 {
2928 /* Ignore bad record and get next one; drop the whole datagram
2929 * since current header cannot be trusted to find the next record
2930 * in current datagram */
2931 ssl->next_record_offset = 0;
2932 ssl->in_left = 0;
2933
2934 SSL_DEBUG_MSG( 2, ( "discarding invalid record" ) );
2935 goto read_record_header;
2936 }
2937#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002938 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002939 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002940
2941 /*
2942 * Read and optionally decrypt the message contents
2943 */
2944 if( ( ret = ssl_fetch_input( ssl,
2945 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
2946 {
2947 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2948 return( ret );
2949 }
2950
2951 /* Done reading this record, get ready for the next one */
2952#if defined(POLARSSL_SSL_PROTO_DTLS)
2953 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2954 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
2955 else
2956#endif
2957 ssl->in_left = 0;
2958
2959 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002960 {
2961#if defined(POLARSSL_SSL_PROTO_DTLS)
2962 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2963 {
2964 /* Silently discard invalid records */
2965 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
2966 ret == POLARSSL_ERR_SSL_INVALID_MAC )
2967 {
2968 SSL_DEBUG_MSG( 2, ( "discarding invalid record" ) );
2969 goto read_record_header;
2970 }
2971
2972 return( ret );
2973 }
2974 else
2975#endif
2976 {
2977 /* Error out (and send alert) on invalid records */
2978#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2979 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2980 {
2981 ssl_send_alert_message( ssl,
2982 SSL_ALERT_LEVEL_FATAL,
2983 SSL_ALERT_MSG_BAD_RECORD_MAC );
2984 }
2985#endif
2986 return( ret );
2987 }
2988 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002989
2990 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002991 * When we sent the last flight of the handshake, we MUST respond to a
2992 * retransmit of the peer's previous flight with a retransmit. (In
2993 * practice, only the Finished message will make it, other messages
2994 * including CCS use the old transform so they're dropped as invalid.)
2995 *
2996 * If the record we received is not a handshake message, however, it
2997 * means the peer received our last flight so we can clean up
2998 * handshake info.
2999 *
3000 * This check needs to be done before prepare_handshake() due to an edge
3001 * case: if the client immediately requests renegotiation, this
3002 * finishes the current handshake first, avoiding the new ClientHello
3003 * being mistaken for an ancient message in the current handshake.
3004 */
3005#if defined(POLARSSL_SSL_PROTO_DTLS)
3006 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3007 ssl->handshake != NULL &&
3008 ssl->handshake->retransmit_state == SSL_RETRANS_FINISHED )
3009 {
3010 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3011 ssl->in_msg[0] == SSL_HS_FINISHED )
3012 {
3013 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3014
3015 if( ( ret = ssl_resend( ssl ) ) != 0 )
3016 {
3017 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3018 return( ret );
3019 }
3020
3021 return( POLARSSL_ERR_NET_WANT_READ );
3022 }
3023 else
3024 {
3025 ssl_handshake_wrapup_free_hs_transform( ssl );
3026 }
3027 }
3028#endif
3029
3030 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003031 * Handle particular types of records
3032 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3034 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003035 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3036 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 }
3038
3039 if( ssl->in_msgtype == SSL_MSG_ALERT )
3040 {
3041 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3042 ssl->in_msg[0], ssl->in_msg[1] ) );
3043
3044 /*
3045 * Ignore non-fatal alerts, except close_notify
3046 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003047 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003049 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3050 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003051 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 }
3053
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003054 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3055 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 {
3057 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003058 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059 }
3060 }
3061
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003062#if defined(POLARSSL_SSL_PROTO_DTLS)
3063 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3064 {
3065 /* Drop unexpected ChangeCipherSpec messages */
3066 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3067 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3068 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3069 {
3070 SSL_DEBUG_MSG( 2, ( "dropping unexpected ChangeCipherSpec" ) );
3071 return( POLARSSL_ERR_NET_WANT_READ );
3072 }
3073 }
3074#endif
3075
Paul Bakker5121ce52009-01-03 21:22:43 +00003076 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3077
3078 return( 0 );
3079}
3080
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003081int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3082{
3083 int ret;
3084
3085 if( ( ret = ssl_send_alert_message( ssl,
3086 SSL_ALERT_LEVEL_FATAL,
3087 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3088 {
3089 return( ret );
3090 }
3091
3092 return( 0 );
3093}
3094
Paul Bakker0a925182012-04-16 06:46:41 +00003095int ssl_send_alert_message( ssl_context *ssl,
3096 unsigned char level,
3097 unsigned char message )
3098{
3099 int ret;
3100
3101 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3102
3103 ssl->out_msgtype = SSL_MSG_ALERT;
3104 ssl->out_msglen = 2;
3105 ssl->out_msg[0] = level;
3106 ssl->out_msg[1] = message;
3107
3108 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3109 {
3110 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3111 return( ret );
3112 }
3113
3114 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3115
3116 return( 0 );
3117}
3118
Paul Bakker5121ce52009-01-03 21:22:43 +00003119/*
3120 * Handshake functions
3121 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003122#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3123 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3124 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3125 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3126 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3127 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3128 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003129int ssl_write_certificate( ssl_context *ssl )
3130{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003131 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003132
3133 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3134
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003136 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3137 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003138 {
3139 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3140 ssl->state++;
3141 return( 0 );
3142 }
3143
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003144 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3145 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003146}
3147
3148int ssl_parse_certificate( ssl_context *ssl )
3149{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3151
3152 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3153
3154 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003155 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3156 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003157 {
3158 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3159 ssl->state++;
3160 return( 0 );
3161 }
3162
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003163 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3164 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003165}
3166#else
3167int ssl_write_certificate( ssl_context *ssl )
3168{
3169 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3170 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003171 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003172 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3173
3174 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3175
3176 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003177 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3178 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003179 {
3180 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3181 ssl->state++;
3182 return( 0 );
3183 }
3184
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 if( ssl->endpoint == SSL_IS_CLIENT )
3186 {
3187 if( ssl->client_auth == 0 )
3188 {
3189 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3190 ssl->state++;
3191 return( 0 );
3192 }
3193
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003194#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 /*
3196 * If using SSLv3 and got no cert, send an Alert message
3197 * (otherwise an empty Certificate message will be sent).
3198 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003199 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003200 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3201 {
3202 ssl->out_msglen = 2;
3203 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003204 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3205 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003206
3207 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3208 goto write_msg;
3209 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003210#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 }
3212 else /* SSL_IS_SERVER */
3213 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003214 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003215 {
3216 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003217 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 }
3219 }
3220
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003221 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003222
3223 /*
3224 * 0 . 0 handshake type
3225 * 1 . 3 handshake length
3226 * 4 . 6 length of all certs
3227 * 7 . 9 length of cert. 1
3228 * 10 . n-1 peer certificate
3229 * n . n+2 length of cert. 2
3230 * n+3 . ... upper level cert, etc.
3231 */
3232 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003233 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Paul Bakker29087132010-03-21 21:03:34 +00003235 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003236 {
3237 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003238 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003239 {
3240 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3241 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003242 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003243 }
3244
3245 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3246 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3247 ssl->out_msg[i + 2] = (unsigned char)( n );
3248
3249 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3250 i += n; crt = crt->next;
3251 }
3252
3253 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3254 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3255 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3256
3257 ssl->out_msglen = i;
3258 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3259 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3260
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003261#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003262write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003263#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003264
3265 ssl->state++;
3266
3267 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3268 {
3269 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3270 return( ret );
3271 }
3272
3273 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3274
Paul Bakkered27a042013-04-18 22:46:23 +02003275 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276}
3277
3278int ssl_parse_certificate( ssl_context *ssl )
3279{
Paul Bakkered27a042013-04-18 22:46:23 +02003280 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003281 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003282 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3285
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003286 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003287 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3288 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003289 {
3290 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3291 ssl->state++;
3292 return( 0 );
3293 }
3294
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003296 ( ssl->authmode == SSL_VERIFY_NONE ||
3297 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003299 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3301 ssl->state++;
3302 return( 0 );
3303 }
3304
3305 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3306 {
3307 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3308 return( ret );
3309 }
3310
3311 ssl->state++;
3312
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003313#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 /*
3315 * Check if the client sent an empty certificate
3316 */
3317 if( ssl->endpoint == SSL_IS_SERVER &&
3318 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3319 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003320 if( ssl->in_msglen == 2 &&
3321 ssl->in_msgtype == SSL_MSG_ALERT &&
3322 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3323 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 {
3325 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3326
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003327 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3329 return( 0 );
3330 else
Paul Bakker40e46942009-01-03 21:51:57 +00003331 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 }
3333 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003334#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003336#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3337 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003338 if( ssl->endpoint == SSL_IS_SERVER &&
3339 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3340 {
3341 if( ssl->in_hslen == 7 &&
3342 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3343 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
3344 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
3345 {
3346 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3347
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003348 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003349 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003350 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 else
3352 return( 0 );
3353 }
3354 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003355#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3356 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
3358 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3359 {
3360 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003361 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 }
3363
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003364 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3365 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003366 {
3367 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003368 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369 }
3370
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003371 i = ssl_hs_hdr_len( ssl );
3372
Paul Bakker5121ce52009-01-03 21:22:43 +00003373 /*
3374 * Same message structure as in ssl_write_certificate()
3375 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003376 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003377
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003378 if( ssl->in_msg[i] != 0 ||
3379 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 {
3381 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003382 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 }
3384
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003385 /* In case we tried to reuse a session but it failed */
3386 if( ssl->session_negotiate->peer_cert != NULL )
3387 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003388 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003389 polarssl_free( ssl->session_negotiate->peer_cert );
3390 }
3391
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003392 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3393 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 {
3395 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003396 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003397 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
3399
Paul Bakkerb6b09562013-09-18 14:17:41 +02003400 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003402 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003403
3404 while( i < ssl->in_hslen )
3405 {
3406 if( ssl->in_msg[i] != 0 )
3407 {
3408 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003409 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 }
3411
3412 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3413 | (unsigned int) ssl->in_msg[i + 2];
3414 i += 3;
3415
3416 if( n < 128 || i + n > ssl->in_hslen )
3417 {
3418 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003419 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 }
3421
Paul Bakkerddf26b42013-09-18 13:46:23 +02003422 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3423 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003424 if( ret != 0 )
3425 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003426 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 return( ret );
3428 }
3429
3430 i += n;
3431 }
3432
Paul Bakker48916f92012-09-16 19:57:18 +00003433 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003434
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003435 /*
3436 * On client, make sure the server cert doesn't change during renego to
3437 * avoid "triple handshake" attack: https://secure-resumption.com/
3438 */
3439 if( ssl->endpoint == SSL_IS_CLIENT &&
3440 ssl->renegotiation == SSL_RENEGOTIATION )
3441 {
3442 if( ssl->session->peer_cert == NULL )
3443 {
3444 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3445 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3446 }
3447
3448 if( ssl->session->peer_cert->raw.len !=
3449 ssl->session_negotiate->peer_cert->raw.len ||
3450 memcmp( ssl->session->peer_cert->raw.p,
3451 ssl->session_negotiate->peer_cert->raw.p,
3452 ssl->session->peer_cert->raw.len ) != 0 )
3453 {
3454 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3455 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3456 }
3457 }
3458
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 if( ssl->authmode != SSL_VERIFY_NONE )
3460 {
3461 if( ssl->ca_chain == NULL )
3462 {
3463 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003464 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 }
3466
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003467 /*
3468 * Main check: verify certificate
3469 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003470 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3471 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3472 &ssl->session_negotiate->verify_result,
3473 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
3475 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003476 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003478 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003479
3480 /*
3481 * Secondary checks: always done, but change 'ret' only if it was 0
3482 */
3483
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003484#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003485 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003486 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003487
3488 /* If certificate uses an EC key, make sure the curve is OK */
3489 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3490 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3491 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003492 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003493 if( ret == 0 )
3494 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003495 }
3496 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003497#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003498
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003499 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3500 ciphersuite_info,
3501 ! ssl->endpoint ) != 0 )
3502 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003503 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003504 if( ret == 0 )
3505 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3506 }
3507
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3509 ret = 0;
3510 }
3511
3512 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3513
3514 return( ret );
3515}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003516#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3517 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3518 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3519 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3520 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3521 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3522 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
3524int ssl_write_change_cipher_spec( ssl_context *ssl )
3525{
3526 int ret;
3527
3528 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3529
3530 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3531 ssl->out_msglen = 1;
3532 ssl->out_msg[0] = 1;
3533
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 ssl->state++;
3535
3536 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3537 {
3538 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3539 return( ret );
3540 }
3541
3542 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3543
3544 return( 0 );
3545}
3546
3547int ssl_parse_change_cipher_spec( ssl_context *ssl )
3548{
3549 int ret;
3550
3551 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3552
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3554 {
3555 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3556 return( ret );
3557 }
3558
3559 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3560 {
3561 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003562 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 }
3564
3565 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3566 {
3567 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003568 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 }
3570
3571 ssl->state++;
3572
3573 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3574
3575 return( 0 );
3576}
3577
Paul Bakker41c83d32013-03-20 14:39:14 +01003578void ssl_optimize_checksum( ssl_context *ssl,
3579 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003580{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003581 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003582
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003583#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3584 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003585 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003586 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003587 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003588#endif
3589#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3590#if defined(POLARSSL_SHA512_C)
3591 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3592 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3593 else
3594#endif
3595#if defined(POLARSSL_SHA256_C)
3596 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003597 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003598 else
3599#endif
3600#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003601 {
3602 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003603 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003604 }
Paul Bakker380da532012-04-18 16:10:25 +00003605}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003606
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003607void ssl_reset_checksum( ssl_context *ssl )
3608{
3609#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3610 defined(POLARSSL_SSL_PROTO_TLS1_1)
3611 md5_starts( &ssl->handshake->fin_md5 );
3612 sha1_starts( &ssl->handshake->fin_sha1 );
3613#endif
3614#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3615#if defined(POLARSSL_SHA256_C)
3616 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3617#endif
3618#if defined(POLARSSL_SHA512_C)
3619 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3620#endif
3621#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3622}
3623
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003624static void ssl_update_checksum_start( ssl_context *ssl,
3625 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003626{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003627#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3628 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003629 md5_update( &ssl->handshake->fin_md5 , buf, len );
3630 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003631#endif
3632#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3633#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003634 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003635#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003636#if defined(POLARSSL_SHA512_C)
3637 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003638#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003639#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003640}
3641
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003642#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3643 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003644static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3645 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003646{
Paul Bakker48916f92012-09-16 19:57:18 +00003647 md5_update( &ssl->handshake->fin_md5 , buf, len );
3648 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003649}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003650#endif
Paul Bakker380da532012-04-18 16:10:25 +00003651
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003652#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3653#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003654static void ssl_update_checksum_sha256( ssl_context *ssl,
3655 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003656{
Paul Bakker9e36f042013-06-30 14:34:05 +02003657 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003658}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003659#endif
Paul Bakker380da532012-04-18 16:10:25 +00003660
Paul Bakker9e36f042013-06-30 14:34:05 +02003661#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003662static void ssl_update_checksum_sha384( ssl_context *ssl,
3663 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003664{
Paul Bakker9e36f042013-06-30 14:34:05 +02003665 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003666}
Paul Bakker769075d2012-11-24 11:26:46 +01003667#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003668#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003669
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003670#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003671static void ssl_calc_finished_ssl(
3672 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003673{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003674 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003675 md5_context md5;
3676 sha1_context sha1;
3677
Paul Bakker5121ce52009-01-03 21:22:43 +00003678 unsigned char padbuf[48];
3679 unsigned char md5sum[16];
3680 unsigned char sha1sum[20];
3681
Paul Bakker48916f92012-09-16 19:57:18 +00003682 ssl_session *session = ssl->session_negotiate;
3683 if( !session )
3684 session = ssl->session;
3685
Paul Bakker1ef83d62012-04-11 12:09:53 +00003686 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3687
Paul Bakker48916f92012-09-16 19:57:18 +00003688 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3689 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690
3691 /*
3692 * SSLv3:
3693 * hash =
3694 * MD5( master + pad2 +
3695 * MD5( handshake + sender + master + pad1 ) )
3696 * + SHA1( master + pad2 +
3697 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 */
3699
Paul Bakker90995b52013-06-24 19:20:35 +02003700#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003701 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003702 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003703#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003704
Paul Bakker90995b52013-06-24 19:20:35 +02003705#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003707 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Paul Bakker3c2122f2013-06-24 19:03:14 +02003710 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3711 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003712
Paul Bakker1ef83d62012-04-11 12:09:53 +00003713 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003714
Paul Bakker3c2122f2013-06-24 19:03:14 +02003715 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003716 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003717 md5_update( &md5, padbuf, 48 );
3718 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
Paul Bakker3c2122f2013-06-24 19:03:14 +02003720 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003721 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003722 sha1_update( &sha1, padbuf, 40 );
3723 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
Paul Bakker1ef83d62012-04-11 12:09:53 +00003725 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003726
Paul Bakker1ef83d62012-04-11 12:09:53 +00003727 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003728 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003729 md5_update( &md5, padbuf, 48 );
3730 md5_update( &md5, md5sum, 16 );
3731 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Paul Bakker1ef83d62012-04-11 12:09:53 +00003733 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003734 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003735 sha1_update( &sha1, padbuf , 40 );
3736 sha1_update( &sha1, sha1sum, 20 );
3737 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738
Paul Bakker1ef83d62012-04-11 12:09:53 +00003739 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003740
Paul Bakker5b4af392014-06-26 12:09:34 +02003741 md5_free( &md5 );
3742 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003743
Paul Bakker34617722014-06-13 17:20:13 +02003744 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3745 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3746 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003747
3748 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3749}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003750#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003751
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003752#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003753static void ssl_calc_finished_tls(
3754 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003755{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003756 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003757 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003758 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003760 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003761
Paul Bakker48916f92012-09-16 19:57:18 +00003762 ssl_session *session = ssl->session_negotiate;
3763 if( !session )
3764 session = ssl->session;
3765
Paul Bakker1ef83d62012-04-11 12:09:53 +00003766 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767
Paul Bakker48916f92012-09-16 19:57:18 +00003768 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3769 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003770
Paul Bakker1ef83d62012-04-11 12:09:53 +00003771 /*
3772 * TLSv1:
3773 * hash = PRF( master, finished_label,
3774 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3775 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003776
Paul Bakker90995b52013-06-24 19:20:35 +02003777#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003778 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3779 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003780#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003781
Paul Bakker90995b52013-06-24 19:20:35 +02003782#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003783 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3784 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003785#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003786
3787 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003788 ? "client finished"
3789 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003790
3791 md5_finish( &md5, padbuf );
3792 sha1_finish( &sha1, padbuf + 16 );
3793
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003794 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003795 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003796
3797 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3798
Paul Bakker5b4af392014-06-26 12:09:34 +02003799 md5_free( &md5 );
3800 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003801
Paul Bakker34617722014-06-13 17:20:13 +02003802 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003803
3804 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3805}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003806#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003807
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003808#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3809#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003810static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003811 ssl_context *ssl, unsigned char *buf, int from )
3812{
3813 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003814 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003815 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003816 unsigned char padbuf[32];
3817
Paul Bakker48916f92012-09-16 19:57:18 +00003818 ssl_session *session = ssl->session_negotiate;
3819 if( !session )
3820 session = ssl->session;
3821
Paul Bakker380da532012-04-18 16:10:25 +00003822 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003823
Paul Bakker9e36f042013-06-30 14:34:05 +02003824 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003825
3826 /*
3827 * TLSv1.2:
3828 * hash = PRF( master, finished_label,
3829 * Hash( handshake ) )[0.11]
3830 */
3831
Paul Bakker9e36f042013-06-30 14:34:05 +02003832#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003833 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003834 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003835#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003836
3837 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003838 ? "client finished"
3839 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003840
Paul Bakker9e36f042013-06-30 14:34:05 +02003841 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003842
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003843 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003844 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003845
3846 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3847
Paul Bakker5b4af392014-06-26 12:09:34 +02003848 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003849
Paul Bakker34617722014-06-13 17:20:13 +02003850 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003851
3852 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3853}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003854#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003855
Paul Bakker9e36f042013-06-30 14:34:05 +02003856#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003857static void ssl_calc_finished_tls_sha384(
3858 ssl_context *ssl, unsigned char *buf, int from )
3859{
3860 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003861 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003862 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003863 unsigned char padbuf[48];
3864
Paul Bakker48916f92012-09-16 19:57:18 +00003865 ssl_session *session = ssl->session_negotiate;
3866 if( !session )
3867 session = ssl->session;
3868
Paul Bakker380da532012-04-18 16:10:25 +00003869 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003870
Paul Bakker9e36f042013-06-30 14:34:05 +02003871 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003872
3873 /*
3874 * TLSv1.2:
3875 * hash = PRF( master, finished_label,
3876 * Hash( handshake ) )[0.11]
3877 */
3878
Paul Bakker9e36f042013-06-30 14:34:05 +02003879#if !defined(POLARSSL_SHA512_ALT)
3880 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3881 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003882#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003883
3884 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003885 ? "client finished"
3886 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003887
Paul Bakker9e36f042013-06-30 14:34:05 +02003888 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003889
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003890 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003891 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003892
3893 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3894
Paul Bakker5b4af392014-06-26 12:09:34 +02003895 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003896
Paul Bakker34617722014-06-13 17:20:13 +02003897 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003898
3899 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3900}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003901#endif /* POLARSSL_SHA512_C */
3902#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003903
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003904static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003905{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003906 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00003907
3908 /*
3909 * Free our handshake params
3910 */
3911 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003912 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003913 ssl->handshake = NULL;
3914
3915 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003916 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00003917 */
3918 if( ssl->transform )
3919 {
3920 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003921 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003922 }
3923 ssl->transform = ssl->transform_negotiate;
3924 ssl->transform_negotiate = NULL;
3925
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003926 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
3927}
3928
3929void ssl_handshake_wrapup( ssl_context *ssl )
3930{
3931 int resume = ssl->handshake->resume;
3932
3933 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3934
3935 if( ssl->renegotiation == SSL_RENEGOTIATION )
3936 {
3937 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
3938 ssl->renego_records_seen = 0;
3939 }
3940
3941 /*
3942 * Free the previous session and switch in the current one
3943 */
Paul Bakker0a597072012-09-25 21:55:46 +00003944 if( ssl->session )
3945 {
3946 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003947 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003948 }
3949 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003950 ssl->session_negotiate = NULL;
3951
Paul Bakker0a597072012-09-25 21:55:46 +00003952 /*
3953 * Add cache entry
3954 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003955 if( ssl->f_set_cache != NULL &&
3956 ssl->session->length != 0 &&
3957 resume == 0 )
3958 {
Paul Bakker0a597072012-09-25 21:55:46 +00003959 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3960 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003961 }
Paul Bakker0a597072012-09-25 21:55:46 +00003962
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003963#if defined(POLARSSL_SSL_PROTO_DTLS)
3964 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3965 ssl->handshake->flight != NULL )
3966 {
3967 /* Keep last flight around in case we need to resend it:
3968 * we need the handshake and transform structures for that */
3969 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
3970 }
3971 else
3972#endif
3973 ssl_handshake_wrapup_free_hs_transform( ssl );
3974
Paul Bakker48916f92012-09-16 19:57:18 +00003975 ssl->state++;
3976
3977 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3978}
3979
Paul Bakker1ef83d62012-04-11 12:09:53 +00003980int ssl_write_finished( ssl_context *ssl )
3981{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003982 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003983
3984 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3985
Paul Bakker92be97b2013-01-02 17:30:03 +01003986 /*
3987 * Set the out_msg pointer to the correct location based on IV length
3988 */
3989 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3990 {
3991 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3992 ssl->transform_negotiate->fixed_ivlen;
3993 }
3994 else
3995 ssl->out_msg = ssl->out_iv;
3996
Paul Bakker48916f92012-09-16 19:57:18 +00003997 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998
3999 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004000 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4001
Paul Bakker48916f92012-09-16 19:57:18 +00004002 ssl->verify_data_len = hash_len;
4003 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4004
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 ssl->out_msglen = 4 + hash_len;
4006 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4007 ssl->out_msg[0] = SSL_HS_FINISHED;
4008
4009 /*
4010 * In case of session resuming, invert the client and server
4011 * ChangeCipherSpec messages order.
4012 */
Paul Bakker0a597072012-09-25 21:55:46 +00004013 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004014 {
4015 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004016 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 else
4018 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4019 }
4020 else
4021 ssl->state++;
4022
Paul Bakker48916f92012-09-16 19:57:18 +00004023 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004024 * Switch to our negotiated transform and session parameters for outbound
4025 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004026 */
4027 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004028
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004029#if defined(POLARSSL_SSL_PROTO_DTLS)
4030 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4031 {
4032 unsigned char i;
4033
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004034 /* Remember current epoch settings for resending */
4035 ssl->handshake->alt_transform_out = ssl->transform_out;
4036 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4037
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004038 /* Set sequence_number to zero */
4039 memset( ssl->out_ctr + 2, 0, 6 );
4040
4041 /* Increment epoch */
4042 for( i = 2; i > 0; i-- )
4043 if( ++ssl->out_ctr[i - 1] != 0 )
4044 break;
4045
4046 /* The loop goes to its end iff the counter is wrapping */
4047 if( i == 0 )
4048 {
4049 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4050 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4051 }
4052 }
4053 else
4054#endif /* POLARSSL_SSL_PROTO_DTLS */
4055 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004057 ssl->transform_out = ssl->transform_negotiate;
4058 ssl->session_out = ssl->session_negotiate;
4059
Paul Bakker07eb38b2012-12-19 14:42:06 +01004060#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004061 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004062 {
4063 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4064 {
4065 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4066 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4067 }
4068 }
4069#endif
4070
Paul Bakker5121ce52009-01-03 21:22:43 +00004071 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4072 {
4073 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4074 return( ret );
4075 }
4076
4077 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4078
4079 return( 0 );
4080}
4081
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004082#if defined(POLARSSL_SSL_PROTO_SSL3)
4083#define SSL_MAX_HASH_LEN 36
4084#else
4085#define SSL_MAX_HASH_LEN 12
4086#endif
4087
Paul Bakker5121ce52009-01-03 21:22:43 +00004088int ssl_parse_finished( ssl_context *ssl )
4089{
Paul Bakker23986e52011-04-24 08:57:21 +00004090 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004091 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004092 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004093
4094 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4095
Paul Bakker48916f92012-09-16 19:57:18 +00004096 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004097
Paul Bakker48916f92012-09-16 19:57:18 +00004098 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004099 * Switch to our negotiated transform and session parameters for inbound
4100 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004101 */
4102 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4103 ssl->transform_in = ssl->transform_negotiate;
4104 ssl->session_in = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004105
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004106#if defined(POLARSSL_SSL_PROTO_DTLS)
4107 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4108 {
4109 unsigned char i;
4110
4111 /* Set sequence_number to zero */
4112 memset( ssl->in_ctr + 2, 0, 6 );
4113
4114 /* Increment epoch */
4115 for( i = 2; i > 0; i-- )
4116 if( ++ssl->in_ctr[i - 1] != 0 )
4117 break;
4118
4119 /* The loop goes to its end iff the counter is wrapping */
4120 if( i == 0 )
4121 {
4122 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4123 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4124 }
4125 }
4126 else
4127#endif /* POLARSSL_SSL_PROTO_DTLS */
4128 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004129
Paul Bakker92be97b2013-01-02 17:30:03 +01004130 /*
4131 * Set the in_msg pointer to the correct location based on IV length
4132 */
4133 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4134 {
4135 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4136 ssl->transform_negotiate->fixed_ivlen;
4137 }
4138 else
4139 ssl->in_msg = ssl->in_iv;
4140
Paul Bakker07eb38b2012-12-19 14:42:06 +01004141#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004142 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004143 {
4144 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4145 {
4146 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4147 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4148 }
4149 }
4150#endif
4151
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4153 {
4154 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4155 return( ret );
4156 }
4157
4158 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4159 {
4160 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004161 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004162 }
4163
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004164 /* There is currently no ciphersuite using another length with TLS 1.2 */
4165#if defined(POLARSSL_SSL_PROTO_SSL3)
4166 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4167 hash_len = 36;
4168 else
4169#endif
4170 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004171
4172 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004173 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004174 {
4175 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004176 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004177 }
4178
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004179 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4180 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004181 {
4182 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004183 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004184 }
4185
Paul Bakker48916f92012-09-16 19:57:18 +00004186 ssl->verify_data_len = hash_len;
4187 memcpy( ssl->peer_verify_data, buf, hash_len );
4188
Paul Bakker0a597072012-09-25 21:55:46 +00004189 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 {
4191 if( ssl->endpoint == SSL_IS_CLIENT )
4192 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4193
4194 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004195 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004196 }
4197 else
4198 ssl->state++;
4199
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004200#if defined(POLARSSL_SSL_PROTO_DTLS)
4201 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4202 ssl_recv_flight_completed( ssl );
4203#endif
4204
Paul Bakker5121ce52009-01-03 21:22:43 +00004205 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4206
4207 return( 0 );
4208}
4209
Paul Bakker968afaa2014-07-09 11:09:24 +02004210static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004211{
4212 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4213
4214#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4215 defined(POLARSSL_SSL_PROTO_TLS1_1)
4216 md5_init( &handshake->fin_md5 );
4217 sha1_init( &handshake->fin_sha1 );
4218 md5_starts( &handshake->fin_md5 );
4219 sha1_starts( &handshake->fin_sha1 );
4220#endif
4221#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4222#if defined(POLARSSL_SHA256_C)
4223 sha256_init( &handshake->fin_sha256 );
4224 sha256_starts( &handshake->fin_sha256, 0 );
4225#endif
4226#if defined(POLARSSL_SHA512_C)
4227 sha512_init( &handshake->fin_sha512 );
4228 sha512_starts( &handshake->fin_sha512, 1 );
4229#endif
4230#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4231
4232 handshake->update_checksum = ssl_update_checksum_start;
4233 handshake->sig_alg = SSL_HASH_SHA1;
4234
4235#if defined(POLARSSL_DHM_C)
4236 dhm_init( &handshake->dhm_ctx );
4237#endif
4238#if defined(POLARSSL_ECDH_C)
4239 ecdh_init( &handshake->ecdh_ctx );
4240#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004241}
4242
4243static void ssl_transform_init( ssl_transform *transform )
4244{
4245 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004246
4247 cipher_init( &transform->cipher_ctx_enc );
4248 cipher_init( &transform->cipher_ctx_dec );
4249
4250 md_init( &transform->md_ctx_enc );
4251 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004252}
4253
4254void ssl_session_init( ssl_session *session )
4255{
4256 memset( session, 0, sizeof(ssl_session) );
4257}
4258
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004259static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004260{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004261 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004262 if( ssl->transform_negotiate )
4263 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004264 if( ssl->session_negotiate )
4265 ssl_session_free( ssl->session_negotiate );
4266 if( ssl->handshake )
4267 ssl_handshake_free( ssl->handshake );
4268
4269 /*
4270 * Either the pointers are now NULL or cleared properly and can be freed.
4271 * Now allocate missing structures.
4272 */
4273 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004274 {
4275 ssl->transform_negotiate =
4276 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4277 }
Paul Bakker48916f92012-09-16 19:57:18 +00004278
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004279 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004280 {
4281 ssl->session_negotiate =
4282 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4283 }
Paul Bakker48916f92012-09-16 19:57:18 +00004284
Paul Bakker82788fb2014-10-20 13:59:19 +02004285 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004286 {
4287 ssl->handshake = (ssl_handshake_params *)
4288 polarssl_malloc( sizeof(ssl_handshake_params) );
4289 }
Paul Bakker48916f92012-09-16 19:57:18 +00004290
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004291 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004292 if( ssl->handshake == NULL ||
4293 ssl->transform_negotiate == NULL ||
4294 ssl->session_negotiate == NULL )
4295 {
4296 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004297
4298 polarssl_free( ssl->handshake );
4299 polarssl_free( ssl->transform_negotiate );
4300 polarssl_free( ssl->session_negotiate );
4301
4302 ssl->handshake = NULL;
4303 ssl->transform_negotiate = NULL;
4304 ssl->session_negotiate = NULL;
4305
Paul Bakker48916f92012-09-16 19:57:18 +00004306 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4307 }
4308
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004309 /* Initialize structures */
4310 ssl_session_init( ssl->session_negotiate );
4311 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004312 ssl_handshake_params_init( ssl->handshake );
4313
4314#if defined(POLARSSL_X509_CRT_PARSE_C)
4315 ssl->handshake->key_cert = ssl->key_cert;
4316#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004317
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004318#if defined(POLARSSL_SSL_PROTO_DTLS)
4319 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4320 {
4321 ssl->handshake->alt_transform_out = ssl->transform_out;
4322
4323 if( ssl->endpoint == SSL_IS_CLIENT )
4324 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4325 else
4326 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
4327 }
4328#endif
4329
Paul Bakker48916f92012-09-16 19:57:18 +00004330 return( 0 );
4331}
4332
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004333#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4334/* Dummy cookie callbacks for defaults */
4335static int ssl_cookie_write_dummy( void *ctx,
4336 unsigned char **p, unsigned char *end,
4337 const unsigned char *cli_id, size_t cli_id_len )
4338{
4339 ((void) ctx);
4340 ((void) p);
4341 ((void) end);
4342 ((void) cli_id);
4343 ((void) cli_id_len);
4344
4345 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4346}
4347
4348static int ssl_cookie_check_dummy( void *ctx,
4349 const unsigned char *cookie, size_t cookie_len,
4350 const unsigned char *cli_id, size_t cli_id_len )
4351{
4352 ((void) ctx);
4353 ((void) cookie);
4354 ((void) cookie_len);
4355 ((void) cli_id);
4356 ((void) cli_id_len);
4357
4358 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4359}
4360#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4361
Paul Bakker5121ce52009-01-03 21:22:43 +00004362/*
4363 * Initialize an SSL context
4364 */
4365int ssl_init( ssl_context *ssl )
4366{
Paul Bakker48916f92012-09-16 19:57:18 +00004367 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 int len = SSL_BUFFER_LEN;
4369
4370 memset( ssl, 0, sizeof( ssl_context ) );
4371
Paul Bakker62f2dee2012-09-28 07:31:51 +00004372 /*
4373 * Sane defaults
4374 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004375 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4376 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4377 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4378 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004379
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004380 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004381
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004382 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4383
Paul Bakker62f2dee2012-09-28 07:31:51 +00004384#if defined(POLARSSL_DHM_C)
4385 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4386 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4387 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4388 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4389 {
4390 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4391 return( ret );
4392 }
4393#endif
4394
4395 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004396 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004397 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004398 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004399 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004400
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004401 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 {
4403 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004404 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004405 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004406 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004407 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004408 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004409 }
4410
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004411 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4412 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004413
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004414 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4415 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4416
Paul Bakker606b4ba2013-08-14 16:52:14 +02004417#if defined(POLARSSL_SSL_SESSION_TICKETS)
4418 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4419#endif
4420
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004421#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004422 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004423#endif
4424
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004425#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4426 ssl->f_cookie_write = ssl_cookie_write_dummy;
4427 ssl->f_cookie_check = ssl_cookie_check_dummy;
4428#endif
4429
Paul Bakker48916f92012-09-16 19:57:18 +00004430 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4431 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004432
4433 return( 0 );
4434}
4435
4436/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004437 * Reset an initialized and used SSL context for re-use while retaining
4438 * all application-set variables, function pointers and data.
4439 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004440int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004441{
Paul Bakker48916f92012-09-16 19:57:18 +00004442 int ret;
4443
Paul Bakker7eb013f2011-10-06 12:37:39 +00004444 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004445 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4446 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4447
4448 ssl->verify_data_len = 0;
4449 memset( ssl->own_verify_data, 0, 36 );
4450 memset( ssl->peer_verify_data, 0, 36 );
4451
Paul Bakker7eb013f2011-10-06 12:37:39 +00004452 ssl->in_offt = NULL;
4453
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004454 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004455 ssl->in_msgtype = 0;
4456 ssl->in_msglen = 0;
4457 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004458#if defined(POLARSSL_SSL_PROTO_DTLS)
4459 ssl->next_record_offset = 0;
4460#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004461
4462 ssl->in_hslen = 0;
4463 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004464 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004465
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004466 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004467 ssl->out_msgtype = 0;
4468 ssl->out_msglen = 0;
4469 ssl->out_left = 0;
4470
Paul Bakker48916f92012-09-16 19:57:18 +00004471 ssl->transform_in = NULL;
4472 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004473
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004474 ssl->renego_records_seen = 0;
4475
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004476 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4477 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004478
4479#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004480 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004481 {
4482 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004483 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004484 {
4485 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4486 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4487 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004488 }
4489#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004490
Paul Bakker48916f92012-09-16 19:57:18 +00004491 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004492 {
Paul Bakker48916f92012-09-16 19:57:18 +00004493 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004494 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004495 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004496 }
Paul Bakker48916f92012-09-16 19:57:18 +00004497
Paul Bakkerc0463502013-02-14 11:19:38 +01004498 if( ssl->session )
4499 {
4500 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004501 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004502 ssl->session = NULL;
4503 }
4504
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004505#if defined(POLARSSL_SSL_ALPN)
4506 ssl->alpn_chosen = NULL;
4507#endif
4508
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004509#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004510 polarssl_free( ssl->cli_id );
4511 ssl->cli_id = NULL;
4512 ssl->cli_id_len = 0;
4513#endif
4514
Paul Bakker48916f92012-09-16 19:57:18 +00004515 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4516 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004517
4518 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004519}
4520
Paul Bakkera503a632013-08-14 13:48:06 +02004521#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004522static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4523{
4524 aes_free( &tkeys->enc );
4525 aes_free( &tkeys->dec );
4526
4527 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4528}
4529
Paul Bakker7eb013f2011-10-06 12:37:39 +00004530/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004531 * Allocate and initialize ticket keys
4532 */
4533static int ssl_ticket_keys_init( ssl_context *ssl )
4534{
4535 int ret;
4536 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004537 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004538
4539 if( ssl->ticket_keys != NULL )
4540 return( 0 );
4541
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004542 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4543 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004544 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4545
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004546 aes_init( &tkeys->enc );
4547 aes_init( &tkeys->dec );
4548
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004549 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004550 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004551 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004552 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004553 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004554 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004555
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004556 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4557 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4558 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4559 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004560 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004561 polarssl_free( tkeys );
4562 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004563 }
4564
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004565 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004566 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004567 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004568 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004569 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004570 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004571
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004572 ssl->ticket_keys = tkeys;
4573
4574 return( 0 );
4575}
Paul Bakkera503a632013-08-14 13:48:06 +02004576#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004577
4578/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004579 * SSL set accessors
4580 */
4581void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4582{
4583 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004584
Paul Bakker606b4ba2013-08-14 16:52:14 +02004585#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004586 if( endpoint == SSL_IS_CLIENT )
4587 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004589}
4590
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004591int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004592{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004593#if defined(POLARSSL_SSL_PROTO_DTLS)
4594 if( transport == SSL_TRANSPORT_DATAGRAM )
4595 {
4596 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004597
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004598 ssl->out_hdr = ssl->out_buf;
4599 ssl->out_ctr = ssl->out_buf + 3;
4600 ssl->out_len = ssl->out_buf + 11;
4601 ssl->out_iv = ssl->out_buf + 13;
4602 ssl->out_msg = ssl->out_buf + 13;
4603
4604 ssl->in_hdr = ssl->in_buf;
4605 ssl->in_ctr = ssl->in_buf + 3;
4606 ssl->in_len = ssl->in_buf + 11;
4607 ssl->in_iv = ssl->in_buf + 13;
4608 ssl->in_msg = ssl->in_buf + 13;
4609
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004610 /* DTLS starts with TLS1.1 */
4611 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4612 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004613
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004614 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4615 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4616
4617 return( 0 );
4618 }
4619#endif
4620
4621 if( transport == SSL_TRANSPORT_STREAM )
4622 {
4623 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004624
4625 ssl->out_ctr = ssl->out_buf;
4626 ssl->out_hdr = ssl->out_buf + 8;
4627 ssl->out_len = ssl->out_buf + 11;
4628 ssl->out_iv = ssl->out_buf + 13;
4629 ssl->out_msg = ssl->out_buf + 13;
4630
4631 ssl->in_ctr = ssl->in_buf;
4632 ssl->in_hdr = ssl->in_buf + 8;
4633 ssl->in_len = ssl->in_buf + 11;
4634 ssl->in_iv = ssl->in_buf + 13;
4635 ssl->in_msg = ssl->in_buf + 13;
4636
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004637 return( 0 );
4638 }
4639
4640 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004641}
4642
Paul Bakker5121ce52009-01-03 21:22:43 +00004643void ssl_set_authmode( ssl_context *ssl, int authmode )
4644{
4645 ssl->authmode = authmode;
4646}
4647
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004648#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004649void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004650 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004651 void *p_vrfy )
4652{
4653 ssl->f_vrfy = f_vrfy;
4654 ssl->p_vrfy = p_vrfy;
4655}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004656#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004657
Paul Bakker5121ce52009-01-03 21:22:43 +00004658void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004659 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004660 void *p_rng )
4661{
4662 ssl->f_rng = f_rng;
4663 ssl->p_rng = p_rng;
4664}
4665
4666void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004667 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 void *p_dbg )
4669{
4670 ssl->f_dbg = f_dbg;
4671 ssl->p_dbg = p_dbg;
4672}
4673
4674void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004675 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004676 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004677{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004678 if( p_recv != p_send )
4679 {
4680 ssl->f_recv = NULL;
4681 ssl->f_send = NULL;
4682 ssl->p_bio = NULL;
4683 return;
4684 }
4685
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 ssl->f_recv = f_recv;
4687 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004688 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004689}
4690
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004691void ssl_set_bio_timeout( ssl_context *ssl,
4692 void *p_bio,
4693 int (*f_send)(void *, const unsigned char *, size_t),
4694 int (*f_recv)(void *, unsigned char *, size_t),
4695 int (*f_recv_timeout)(void *, unsigned char *, size_t, unsigned char),
4696 unsigned char timeout )
4697{
4698 ssl->p_bio = p_bio;
4699 ssl->f_send = f_send;
4700 ssl->f_recv = f_recv;
4701 ssl->f_recv_timeout = f_recv_timeout;
4702 ssl->timeout = timeout;
4703}
4704
Paul Bakker0a597072012-09-25 21:55:46 +00004705void ssl_set_session_cache( ssl_context *ssl,
4706 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4707 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004708{
Paul Bakker0a597072012-09-25 21:55:46 +00004709 ssl->f_get_cache = f_get_cache;
4710 ssl->p_get_cache = p_get_cache;
4711 ssl->f_set_cache = f_set_cache;
4712 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004713}
4714
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004715int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004716{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004717 int ret;
4718
4719 if( ssl == NULL ||
4720 session == NULL ||
4721 ssl->session_negotiate == NULL ||
4722 ssl->endpoint != SSL_IS_CLIENT )
4723 {
4724 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4725 }
4726
4727 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4728 return( ret );
4729
Paul Bakker0a597072012-09-25 21:55:46 +00004730 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004731
4732 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004733}
4734
Paul Bakkerb68cad62012-08-23 08:34:18 +00004735void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004736{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004737 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4738 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4739 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4740 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4741}
4742
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004743void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4744 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004745 int major, int minor )
4746{
4747 if( major != SSL_MAJOR_VERSION_3 )
4748 return;
4749
4750 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4751 return;
4752
4753 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004754}
4755
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004756#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004757/* Add a new (empty) key_cert entry an return a pointer to it */
4758static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4759{
4760 ssl_key_cert *key_cert, *last;
4761
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004762 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
4763 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004764 return( NULL );
4765
4766 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4767
4768 /* Append the new key_cert to the (possibly empty) current list */
4769 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004770 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004771 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004772 if( ssl->handshake != NULL )
4773 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004774 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004775 else
4776 {
4777 last = ssl->key_cert;
4778 while( last->next != NULL )
4779 last = last->next;
4780 last->next = key_cert;
4781 }
4782
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004783 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004784}
4785
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004786void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004787 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004788{
4789 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004790 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004791 ssl->peer_cn = peer_cn;
4792}
4793
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004794int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004795 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004796{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004797 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4798
4799 if( key_cert == NULL )
4800 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4801
4802 key_cert->cert = own_cert;
4803 key_cert->key = pk_key;
4804
4805 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004806}
4807
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004808#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004809int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004810 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004811{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004812 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004813 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004814
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004815 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004816 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4817
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004818 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4819 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004820 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004821
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004822 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004823
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004824 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004825 if( ret != 0 )
4826 return( ret );
4827
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004828 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004829 return( ret );
4830
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004831 key_cert->cert = own_cert;
4832 key_cert->key_own_alloc = 1;
4833
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004834 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004836#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004837
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004838int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004839 void *rsa_key,
4840 rsa_decrypt_func rsa_decrypt,
4841 rsa_sign_func rsa_sign,
4842 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004843{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004844 int ret;
4845 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004846
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004847 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004848 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4849
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004850 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4851 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004852 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004853
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004854 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004855
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004856 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4857 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4858 return( ret );
4859
4860 key_cert->cert = own_cert;
4861 key_cert->key_own_alloc = 1;
4862
4863 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004864}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004865#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004866
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004867#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004868int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4869 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004870{
Paul Bakker6db455e2013-09-18 17:29:31 +02004871 if( psk == NULL || psk_identity == NULL )
4872 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4873
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004874 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004875 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4876
Paul Bakker6db455e2013-09-18 17:29:31 +02004877 if( ssl->psk != NULL )
4878 {
4879 polarssl_free( ssl->psk );
4880 polarssl_free( ssl->psk_identity );
4881 }
4882
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004883 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004884 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004885
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004886 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004887 ssl->psk_identity = (unsigned char *)
4888 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004889
4890 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4891 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4892
4893 memcpy( ssl->psk, psk, ssl->psk_len );
4894 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004895
4896 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004897}
4898
4899void ssl_set_psk_cb( ssl_context *ssl,
4900 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4901 size_t),
4902 void *p_psk )
4903{
4904 ssl->f_psk = f_psk;
4905 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004906}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004907#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004908
Paul Bakker48916f92012-09-16 19:57:18 +00004909#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004910int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004911{
4912 int ret;
4913
Paul Bakker48916f92012-09-16 19:57:18 +00004914 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004915 {
4916 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4917 return( ret );
4918 }
4919
Paul Bakker48916f92012-09-16 19:57:18 +00004920 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004921 {
4922 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4923 return( ret );
4924 }
4925
4926 return( 0 );
4927}
4928
Paul Bakker1b57b062011-01-06 15:48:19 +00004929int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4930{
4931 int ret;
4932
Paul Bakker66d5d072014-06-17 16:39:18 +02004933 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004934 {
4935 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4936 return( ret );
4937 }
4938
Paul Bakker66d5d072014-06-17 16:39:18 +02004939 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004940 {
4941 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4942 return( ret );
4943 }
4944
4945 return( 0 );
4946}
Paul Bakker48916f92012-09-16 19:57:18 +00004947#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004948
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004949#if defined(POLARSSL_SSL_SET_CURVES)
4950/*
4951 * Set the allowed elliptic curves
4952 */
4953void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4954{
4955 ssl->curve_list = curve_list;
4956}
4957#endif
4958
Paul Bakker0be444a2013-08-27 21:55:01 +02004959#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004960int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004961{
4962 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004963 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004964
4965 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004966
4967 if( ssl->hostname_len + 1 == 0 )
4968 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4969
Paul Bakker6e339b52013-07-03 13:37:05 +02004970 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004971
Paul Bakkerb15b8512012-01-13 13:44:06 +00004972 if( ssl->hostname == NULL )
4973 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4974
Paul Bakker3c2122f2013-06-24 19:03:14 +02004975 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004976 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004977
Paul Bakker40ea7de2009-05-03 10:18:48 +00004978 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004979
4980 return( 0 );
4981}
4982
Paul Bakker5701cdc2012-09-27 21:49:42 +00004983void ssl_set_sni( ssl_context *ssl,
4984 int (*f_sni)(void *, ssl_context *,
4985 const unsigned char *, size_t),
4986 void *p_sni )
4987{
4988 ssl->f_sni = f_sni;
4989 ssl->p_sni = p_sni;
4990}
Paul Bakker0be444a2013-08-27 21:55:01 +02004991#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004992
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004993#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004994int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004995{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004996 size_t cur_len, tot_len;
4997 const char **p;
4998
4999 /*
5000 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5001 * truncated". Check lengths now rather than later.
5002 */
5003 tot_len = 0;
5004 for( p = protos; *p != NULL; p++ )
5005 {
5006 cur_len = strlen( *p );
5007 tot_len += cur_len;
5008
5009 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5010 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5011 }
5012
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005013 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005014
5015 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005016}
5017
5018const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5019{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005020 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005021}
5022#endif /* POLARSSL_SSL_ALPN */
5023
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005024static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005025{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005026 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5027 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5028 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5029 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005030 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005031 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005032 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005033
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005034 return( 0 );
5035}
5036
5037int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5038{
5039 if( ssl_check_version( ssl, major, minor ) != 0 )
5040 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5041
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005042 ssl->max_major_ver = major;
5043 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005044
5045 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005046}
5047
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005048int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005049{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005050 if( ssl_check_version( ssl, major, minor ) != 0 )
5051 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005052
5053 ssl->min_major_ver = major;
5054 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005055
5056 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005057}
5058
Paul Bakker05decb22013-08-15 13:33:48 +02005059#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005060int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5061{
Paul Bakker77e257e2013-12-16 15:29:52 +01005062 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005063 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005064 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005065 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005066 }
5067
5068 ssl->mfl_code = mfl_code;
5069
5070 return( 0 );
5071}
Paul Bakker05decb22013-08-15 13:33:48 +02005072#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005073
Paul Bakker1f2bc622013-08-15 13:45:55 +02005074#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005075int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005076{
5077 if( ssl->endpoint != SSL_IS_CLIENT )
5078 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5079
Paul Bakker8c1ede62013-07-19 14:14:37 +02005080 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005081
5082 return( 0 );
5083}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005084#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005085
Paul Bakker48916f92012-09-16 19:57:18 +00005086void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5087{
5088 ssl->disable_renegotiation = renegotiation;
5089}
5090
5091void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5092{
5093 ssl->allow_legacy_renegotiation = allow_legacy;
5094}
5095
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005096void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5097{
5098 ssl->renego_max_records = max_records;
5099}
5100
Paul Bakkera503a632013-08-14 13:48:06 +02005101#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005102int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5103{
5104 ssl->session_tickets = use_tickets;
5105
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005106 if( ssl->endpoint == SSL_IS_CLIENT )
5107 return( 0 );
5108
5109 if( ssl->f_rng == NULL )
5110 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5111
5112 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005113}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005114
5115void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5116{
5117 ssl->ticket_lifetime = lifetime;
5118}
Paul Bakkera503a632013-08-14 13:48:06 +02005119#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005120
Paul Bakker5121ce52009-01-03 21:22:43 +00005121/*
5122 * SSL get accessors
5123 */
Paul Bakker23986e52011-04-24 08:57:21 +00005124size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005125{
5126 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5127}
5128
Paul Bakkerff60ee62010-03-16 21:09:09 +00005129int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005130{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005131 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005132}
5133
Paul Bakkere3166ce2011-01-27 17:40:50 +00005134const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005135{
Paul Bakker926c8e42013-03-06 10:23:34 +01005136 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005137 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005138
Paul Bakkere3166ce2011-01-27 17:40:50 +00005139 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005140}
5141
Paul Bakker43ca69c2011-01-15 17:35:19 +00005142const char *ssl_get_version( const ssl_context *ssl )
5143{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005144#if defined(POLARSSL_SSL_PROTO_DTLS)
5145 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5146 {
5147 switch( ssl->minor_ver )
5148 {
5149 case SSL_MINOR_VERSION_2:
5150 return( "DTLSv1.0" );
5151
5152 case SSL_MINOR_VERSION_3:
5153 return( "DTLSv1.2" );
5154
5155 default:
5156 return( "unknown (DTLS)" );
5157 }
5158 }
5159#endif
5160
Paul Bakker43ca69c2011-01-15 17:35:19 +00005161 switch( ssl->minor_ver )
5162 {
5163 case SSL_MINOR_VERSION_0:
5164 return( "SSLv3.0" );
5165
5166 case SSL_MINOR_VERSION_1:
5167 return( "TLSv1.0" );
5168
5169 case SSL_MINOR_VERSION_2:
5170 return( "TLSv1.1" );
5171
Paul Bakker1ef83d62012-04-11 12:09:53 +00005172 case SSL_MINOR_VERSION_3:
5173 return( "TLSv1.2" );
5174
Paul Bakker43ca69c2011-01-15 17:35:19 +00005175 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005176 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005177 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005178}
5179
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005180#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005181const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005182{
5183 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005184 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005185
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005186 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005187}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005188#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005189
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005190int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5191{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005192 if( ssl == NULL ||
5193 dst == NULL ||
5194 ssl->session == NULL ||
5195 ssl->endpoint != SSL_IS_CLIENT )
5196 {
5197 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5198 }
5199
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005200 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005201}
5202
Paul Bakker5121ce52009-01-03 21:22:43 +00005203/*
Paul Bakker1961b702013-01-25 14:49:24 +01005204 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005205 */
Paul Bakker1961b702013-01-25 14:49:24 +01005206int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005207{
Paul Bakker40e46942009-01-03 21:51:57 +00005208 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005209
Paul Bakker40e46942009-01-03 21:51:57 +00005210#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005211 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005212 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005213#endif
5214
Paul Bakker40e46942009-01-03 21:51:57 +00005215#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005217 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005218#endif
5219
Paul Bakker1961b702013-01-25 14:49:24 +01005220 return( ret );
5221}
5222
5223/*
5224 * Perform the SSL handshake
5225 */
5226int ssl_handshake( ssl_context *ssl )
5227{
5228 int ret = 0;
5229
5230 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5231
5232 while( ssl->state != SSL_HANDSHAKE_OVER )
5233 {
5234 ret = ssl_handshake_step( ssl );
5235
5236 if( ret != 0 )
5237 break;
5238 }
5239
Paul Bakker5121ce52009-01-03 21:22:43 +00005240 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5241
5242 return( ret );
5243}
5244
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005245#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005246/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005247 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005248 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005249static int ssl_write_hello_request( ssl_context *ssl )
5250{
5251 int ret;
5252
5253 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5254
5255 ssl->out_msglen = 4;
5256 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5257 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5258
5259 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5260 {
5261 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5262 return( ret );
5263 }
5264
5265 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5266
5267 return( 0 );
5268}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005269#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005270
5271/*
5272 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005273 * - any side: calling ssl_renegotiate(),
5274 * - client: receiving a HelloRequest during ssl_read(),
5275 * - server: receiving any handshake message on server during ssl_read() after
5276 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005277 * If the handshake doesn't complete due to waiting for I/O, it will continue
5278 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005279 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005280static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005281{
5282 int ret;
5283
5284 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5285
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005286 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5287 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005288
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005289 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5290 * the ServerHello will have message_seq = 1" */
5291#if defined(POLARSSL_SSL_PROTO_DTLS)
5292 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005293 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5294 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005295 if( ssl->endpoint == SSL_IS_SERVER )
5296 ssl->handshake->out_msg_seq = 1;
5297 else
5298 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005299 }
5300#endif
5301
Paul Bakker48916f92012-09-16 19:57:18 +00005302 ssl->state = SSL_HELLO_REQUEST;
5303 ssl->renegotiation = SSL_RENEGOTIATION;
5304
Paul Bakker48916f92012-09-16 19:57:18 +00005305 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5306 {
5307 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5308 return( ret );
5309 }
5310
5311 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5312
5313 return( 0 );
5314}
5315
5316/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005317 * Renegotiate current connection on client,
5318 * or request renegotiation on server
5319 */
5320int ssl_renegotiate( ssl_context *ssl )
5321{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005322 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005323
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005324#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005325 /* On server, just send the request */
5326 if( ssl->endpoint == SSL_IS_SERVER )
5327 {
5328 if( ssl->state != SSL_HANDSHAKE_OVER )
5329 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5330
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005331 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5332
5333 /* Did we already try/start sending HelloRequest? */
5334 if( ssl->out_left != 0 )
5335 return( ssl_flush_output( ssl ) );
5336
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005337 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005338 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005339#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005340
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005341#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005342 /*
5343 * On client, either start the renegotiation process or,
5344 * if already in progress, continue the handshake
5345 */
5346 if( ssl->renegotiation != SSL_RENEGOTIATION )
5347 {
5348 if( ssl->state != SSL_HANDSHAKE_OVER )
5349 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5350
5351 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5352 {
5353 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5354 return( ret );
5355 }
5356 }
5357 else
5358 {
5359 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5360 {
5361 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5362 return( ret );
5363 }
5364 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005365#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005366
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005367 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005368}
5369
5370/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005371 * Receive application data decrypted from the SSL layer
5372 */
Paul Bakker23986e52011-04-24 08:57:21 +00005373int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005374{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005375 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005376 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005377
5378 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5379
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005380#if defined(POLARSSL_SSL_PROTO_DTLS)
5381 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5382 ssl->handshake != NULL &&
5383 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5384 {
5385 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5386 return( ret );
5387
5388 if( ( ret = ssl_resend( ssl ) ) != 0 )
5389 return( ret );
5390 }
5391#endif
5392
Paul Bakker5121ce52009-01-03 21:22:43 +00005393 if( ssl->state != SSL_HANDSHAKE_OVER )
5394 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005395 ret = ssl_handshake( ssl );
5396 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5397 {
5398 record_read = 1;
5399 }
5400 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005401 {
5402 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5403 return( ret );
5404 }
5405 }
5406
5407 if( ssl->in_offt == NULL )
5408 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005409 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005410 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005411 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5412 {
5413 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5414 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005415
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005416 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5417 return( ret );
5418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005419 }
5420
5421 if( ssl->in_msglen == 0 &&
5422 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5423 {
5424 /*
5425 * OpenSSL sends empty messages to randomize the IV
5426 */
5427 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5428 {
Paul Bakker831a7552011-05-18 13:32:51 +00005429 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5430 return( 0 );
5431
Paul Bakker5121ce52009-01-03 21:22:43 +00005432 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5433 return( ret );
5434 }
5435 }
5436
Paul Bakker48916f92012-09-16 19:57:18 +00005437 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5438 {
5439 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5440
5441 if( ssl->endpoint == SSL_IS_CLIENT &&
5442 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005443 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005444 {
5445 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005446
5447 /* With DTLS, drop the packet (probably from last handshake) */
5448#if defined(POLARSSL_SSL_PROTO_DTLS)
5449 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5450 return( POLARSSL_ERR_NET_WANT_READ );
5451#endif
5452 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5453 }
5454
5455 if( ssl->endpoint == SSL_IS_SERVER &&
5456 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5457 {
5458 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5459
5460 /* With DTLS, drop the packet (probably from last handshake) */
5461#if defined(POLARSSL_SSL_PROTO_DTLS)
5462 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5463 return( POLARSSL_ERR_NET_WANT_READ );
5464#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005465 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5466 }
5467
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005468 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5469 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005470 ssl->allow_legacy_renegotiation ==
5471 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005472 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005473 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005474
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005475#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005476 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005477 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005478 /*
5479 * SSLv3 does not have a "no_renegotiation" alert
5480 */
5481 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5482 return( ret );
5483 }
5484 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005485#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005486#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5487 defined(POLARSSL_SSL_PROTO_TLS1_2)
5488 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005489 {
5490 if( ( ret = ssl_send_alert_message( ssl,
5491 SSL_ALERT_LEVEL_WARNING,
5492 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5493 {
5494 return( ret );
5495 }
Paul Bakker48916f92012-09-16 19:57:18 +00005496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005497 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005498#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5499 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005500 {
5501 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005502 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005503 }
Paul Bakker48916f92012-09-16 19:57:18 +00005504 }
5505 else
5506 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005507#if defined(POLARSSL_SSL_PROTO_DTLS)
5508 /* DTLS clients need to know renego is server-initiated */
5509 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5510 ssl->endpoint == SSL_IS_CLIENT )
5511 {
5512 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5513 }
5514#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005515 ret = ssl_start_renegotiation( ssl );
5516 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5517 {
5518 record_read = 1;
5519 }
5520 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005521 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005522 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005523 return( ret );
5524 }
Paul Bakker48916f92012-09-16 19:57:18 +00005525 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005526
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005527 /* If a non-handshake record was read during renego, fallthrough,
5528 * else tell the user they should call ssl_read() again */
5529 if( ! record_read )
5530 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005531 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005532 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5533 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005534 ssl->renego_records_seen++;
5535
5536 if( ssl->renego_max_records >= 0 &&
5537 ssl->renego_records_seen > ssl->renego_max_records )
5538 {
5539 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5540 "but not honored by client" ) );
5541 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5542 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005543 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005544
5545 /* Fatal and closure alerts handled by ssl_read_record() */
5546 if( ssl->in_msgtype == SSL_MSG_ALERT )
5547 {
5548 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5549 return( POLARSSL_ERR_NET_WANT_READ );
5550 }
5551
5552 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005553 {
5554 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005555 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556 }
5557
5558 ssl->in_offt = ssl->in_msg;
5559 }
5560
5561 n = ( len < ssl->in_msglen )
5562 ? len : ssl->in_msglen;
5563
5564 memcpy( buf, ssl->in_offt, n );
5565 ssl->in_msglen -= n;
5566
5567 if( ssl->in_msglen == 0 )
5568 /* all bytes consumed */
5569 ssl->in_offt = NULL;
5570 else
5571 /* more data available */
5572 ssl->in_offt += n;
5573
5574 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5575
Paul Bakker23986e52011-04-24 08:57:21 +00005576 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005577}
5578
5579/*
5580 * Send application data to be encrypted by the SSL layer
5581 */
Paul Bakker23986e52011-04-24 08:57:21 +00005582int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005583{
Paul Bakker23986e52011-04-24 08:57:21 +00005584 int ret;
5585 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005586 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005587
5588 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5589
5590 if( ssl->state != SSL_HANDSHAKE_OVER )
5591 {
5592 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5593 {
5594 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5595 return( ret );
5596 }
5597 }
5598
Paul Bakker05decb22013-08-15 13:33:48 +02005599#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005600 /*
5601 * Assume mfl_code is correct since it was checked when set
5602 */
5603 max_len = mfl_code_to_length[ssl->mfl_code];
5604
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005605 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005606 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005607 */
5608 if( ssl->session_out != NULL &&
5609 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5610 {
5611 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5612 }
Paul Bakker05decb22013-08-15 13:33:48 +02005613#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005614
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005615 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005616
Paul Bakker5121ce52009-01-03 21:22:43 +00005617 if( ssl->out_left != 0 )
5618 {
5619 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5620 {
5621 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5622 return( ret );
5623 }
5624 }
Paul Bakker887bd502011-06-08 13:10:54 +00005625 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005626 {
Paul Bakker887bd502011-06-08 13:10:54 +00005627 ssl->out_msglen = n;
5628 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5629 memcpy( ssl->out_msg, buf, n );
5630
5631 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5632 {
5633 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5634 return( ret );
5635 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 }
5637
5638 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5639
Paul Bakker23986e52011-04-24 08:57:21 +00005640 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641}
5642
5643/*
5644 * Notify the peer that the connection is being closed
5645 */
5646int ssl_close_notify( ssl_context *ssl )
5647{
5648 int ret;
5649
5650 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5651
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005652 if( ssl->out_left != 0 )
5653 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654
5655 if( ssl->state == SSL_HANDSHAKE_OVER )
5656 {
Paul Bakker48916f92012-09-16 19:57:18 +00005657 if( ( ret = ssl_send_alert_message( ssl,
5658 SSL_ALERT_LEVEL_WARNING,
5659 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005660 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005661 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005662 return( ret );
5663 }
5664 }
5665
5666 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5667
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005668 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005669}
5670
Paul Bakker48916f92012-09-16 19:57:18 +00005671void ssl_transform_free( ssl_transform *transform )
5672{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005673 if( transform == NULL )
5674 return;
5675
Paul Bakker48916f92012-09-16 19:57:18 +00005676#if defined(POLARSSL_ZLIB_SUPPORT)
5677 deflateEnd( &transform->ctx_deflate );
5678 inflateEnd( &transform->ctx_inflate );
5679#endif
5680
Paul Bakker84bbeb52014-07-01 14:53:22 +02005681 cipher_free( &transform->cipher_ctx_enc );
5682 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005683
Paul Bakker84bbeb52014-07-01 14:53:22 +02005684 md_free( &transform->md_ctx_enc );
5685 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005686
Paul Bakker34617722014-06-13 17:20:13 +02005687 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005688}
5689
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005690#if defined(POLARSSL_X509_CRT_PARSE_C)
5691static void ssl_key_cert_free( ssl_key_cert *key_cert )
5692{
5693 ssl_key_cert *cur = key_cert, *next;
5694
5695 while( cur != NULL )
5696 {
5697 next = cur->next;
5698
5699 if( cur->key_own_alloc )
5700 {
5701 pk_free( cur->key );
5702 polarssl_free( cur->key );
5703 }
5704 polarssl_free( cur );
5705
5706 cur = next;
5707 }
5708}
5709#endif /* POLARSSL_X509_CRT_PARSE_C */
5710
Paul Bakker48916f92012-09-16 19:57:18 +00005711void ssl_handshake_free( ssl_handshake_params *handshake )
5712{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005713 if( handshake == NULL )
5714 return;
5715
Paul Bakker48916f92012-09-16 19:57:18 +00005716#if defined(POLARSSL_DHM_C)
5717 dhm_free( &handshake->dhm_ctx );
5718#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005719#if defined(POLARSSL_ECDH_C)
5720 ecdh_free( &handshake->ecdh_ctx );
5721#endif
5722
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005723#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005724 /* explicit void pointer cast for buggy MS compiler */
5725 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005726#endif
5727
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005728#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5729 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5730 /*
5731 * Free only the linked list wrapper, not the keys themselves
5732 * since the belong to the SNI callback
5733 */
5734 if( handshake->sni_key_cert != NULL )
5735 {
5736 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5737
5738 while( cur != NULL )
5739 {
5740 next = cur->next;
5741 polarssl_free( cur );
5742 cur = next;
5743 }
5744 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005745#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005746
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005747#if defined(POLARSSL_SSL_PROTO_DTLS)
5748 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02005749 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02005750 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005751#endif
5752
Paul Bakker34617722014-06-13 17:20:13 +02005753 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005754}
5755
5756void ssl_session_free( ssl_session *session )
5757{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005758 if( session == NULL )
5759 return;
5760
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005761#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005762 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005763 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005764 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005765 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005766 }
Paul Bakkered27a042013-04-18 22:46:23 +02005767#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005768
Paul Bakkera503a632013-08-14 13:48:06 +02005769#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005770 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005771#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005772
Paul Bakker34617722014-06-13 17:20:13 +02005773 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005774}
5775
Paul Bakker5121ce52009-01-03 21:22:43 +00005776/*
5777 * Free an SSL context
5778 */
5779void ssl_free( ssl_context *ssl )
5780{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005781 if( ssl == NULL )
5782 return;
5783
Paul Bakker5121ce52009-01-03 21:22:43 +00005784 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5785
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005786 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005787 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005788 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
5789 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005790 }
5791
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005792 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005793 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005794 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
5795 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 }
5797
Paul Bakker16770332013-10-11 09:59:44 +02005798#if defined(POLARSSL_ZLIB_SUPPORT)
5799 if( ssl->compress_buf != NULL )
5800 {
Paul Bakker34617722014-06-13 17:20:13 +02005801 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005802 polarssl_free( ssl->compress_buf );
5803 }
5804#endif
5805
Paul Bakker40e46942009-01-03 21:51:57 +00005806#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005807 mpi_free( &ssl->dhm_P );
5808 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005809#endif
5810
Paul Bakker48916f92012-09-16 19:57:18 +00005811 if( ssl->transform )
5812 {
5813 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005814 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005815 }
5816
5817 if( ssl->handshake )
5818 {
5819 ssl_handshake_free( ssl->handshake );
5820 ssl_transform_free( ssl->transform_negotiate );
5821 ssl_session_free( ssl->session_negotiate );
5822
Paul Bakker6e339b52013-07-03 13:37:05 +02005823 polarssl_free( ssl->handshake );
5824 polarssl_free( ssl->transform_negotiate );
5825 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005826 }
5827
Paul Bakkerc0463502013-02-14 11:19:38 +01005828 if( ssl->session )
5829 {
5830 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005831 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005832 }
5833
Paul Bakkera503a632013-08-14 13:48:06 +02005834#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005835 if( ssl->ticket_keys )
5836 {
5837 ssl_ticket_keys_free( ssl->ticket_keys );
5838 polarssl_free( ssl->ticket_keys );
5839 }
Paul Bakkera503a632013-08-14 13:48:06 +02005840#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005841
Paul Bakker0be444a2013-08-27 21:55:01 +02005842#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005843 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005844 {
Paul Bakker34617722014-06-13 17:20:13 +02005845 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005846 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005847 ssl->hostname_len = 0;
5848 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005849#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005850
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005851#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005852 if( ssl->psk != NULL )
5853 {
Paul Bakker34617722014-06-13 17:20:13 +02005854 polarssl_zeroize( ssl->psk, ssl->psk_len );
5855 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005856 polarssl_free( ssl->psk );
5857 polarssl_free( ssl->psk_identity );
5858 ssl->psk_len = 0;
5859 ssl->psk_identity_len = 0;
5860 }
5861#endif
5862
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005863#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005864 ssl_key_cert_free( ssl->key_cert );
5865#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005866
Paul Bakker05ef8352012-05-08 09:17:57 +00005867#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5868 if( ssl_hw_record_finish != NULL )
5869 {
5870 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5871 ssl_hw_record_finish( ssl );
5872 }
5873#endif
5874
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005875#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005876 polarssl_free( ssl->cli_id );
5877#endif
5878
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005880
Paul Bakker86f04f42013-02-14 11:20:09 +01005881 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005882 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005883}
5884
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005885#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005886/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005887 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005888 */
5889unsigned char ssl_sig_from_pk( pk_context *pk )
5890{
5891#if defined(POLARSSL_RSA_C)
5892 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5893 return( SSL_SIG_RSA );
5894#endif
5895#if defined(POLARSSL_ECDSA_C)
5896 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5897 return( SSL_SIG_ECDSA );
5898#endif
5899 return( SSL_SIG_ANON );
5900}
5901
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005902pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5903{
5904 switch( sig )
5905 {
5906#if defined(POLARSSL_RSA_C)
5907 case SSL_SIG_RSA:
5908 return( POLARSSL_PK_RSA );
5909#endif
5910#if defined(POLARSSL_ECDSA_C)
5911 case SSL_SIG_ECDSA:
5912 return( POLARSSL_PK_ECDSA );
5913#endif
5914 default:
5915 return( POLARSSL_PK_NONE );
5916 }
5917}
Paul Bakker9af723c2014-05-01 13:03:14 +02005918#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005919
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005920/*
5921 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5922 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005923md_type_t ssl_md_alg_from_hash( unsigned char hash )
5924{
5925 switch( hash )
5926 {
5927#if defined(POLARSSL_MD5_C)
5928 case SSL_HASH_MD5:
5929 return( POLARSSL_MD_MD5 );
5930#endif
5931#if defined(POLARSSL_SHA1_C)
5932 case SSL_HASH_SHA1:
5933 return( POLARSSL_MD_SHA1 );
5934#endif
5935#if defined(POLARSSL_SHA256_C)
5936 case SSL_HASH_SHA224:
5937 return( POLARSSL_MD_SHA224 );
5938 case SSL_HASH_SHA256:
5939 return( POLARSSL_MD_SHA256 );
5940#endif
5941#if defined(POLARSSL_SHA512_C)
5942 case SSL_HASH_SHA384:
5943 return( POLARSSL_MD_SHA384 );
5944 case SSL_HASH_SHA512:
5945 return( POLARSSL_MD_SHA512 );
5946#endif
5947 default:
5948 return( POLARSSL_MD_NONE );
5949 }
5950}
5951
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005952#if defined(POLARSSL_SSL_SET_CURVES)
5953/*
5954 * Check is a curve proposed by the peer is in our list.
5955 * Return 1 if we're willing to use it, 0 otherwise.
5956 */
5957int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5958{
5959 const ecp_group_id *gid;
5960
5961 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5962 if( *gid == grp_id )
5963 return( 1 );
5964
5965 return( 0 );
5966}
Paul Bakker9af723c2014-05-01 13:03:14 +02005967#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005968
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005969#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005970int ssl_check_cert_usage( const x509_crt *cert,
5971 const ssl_ciphersuite_t *ciphersuite,
5972 int cert_endpoint )
5973{
5974#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5975 int usage = 0;
5976#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005977#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5978 const char *ext_oid;
5979 size_t ext_len;
5980#endif
5981
5982#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5983 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5984 ((void) cert);
5985 ((void) cert_endpoint);
5986#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005987
5988#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5989 if( cert_endpoint == SSL_IS_SERVER )
5990 {
5991 /* Server part of the key exchange */
5992 switch( ciphersuite->key_exchange )
5993 {
5994 case POLARSSL_KEY_EXCHANGE_RSA:
5995 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5996 usage = KU_KEY_ENCIPHERMENT;
5997 break;
5998
5999 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6000 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6001 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6002 usage = KU_DIGITAL_SIGNATURE;
6003 break;
6004
6005 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6006 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6007 usage = KU_KEY_AGREEMENT;
6008 break;
6009
6010 /* Don't use default: we want warnings when adding new values */
6011 case POLARSSL_KEY_EXCHANGE_NONE:
6012 case POLARSSL_KEY_EXCHANGE_PSK:
6013 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6014 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6015 usage = 0;
6016 }
6017 }
6018 else
6019 {
6020 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6021 usage = KU_DIGITAL_SIGNATURE;
6022 }
6023
6024 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6025 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006026#else
6027 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006028#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6029
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006030#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6031 if( cert_endpoint == SSL_IS_SERVER )
6032 {
6033 ext_oid = OID_SERVER_AUTH;
6034 ext_len = OID_SIZE( OID_SERVER_AUTH );
6035 }
6036 else
6037 {
6038 ext_oid = OID_CLIENT_AUTH;
6039 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6040 }
6041
6042 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6043 return( -1 );
6044#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6045
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006046 return( 0 );
6047}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006048#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006049
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006050/*
6051 * Convert version numbers to/from wire format
6052 * and, for DTLS, to/from TLS equivalent.
6053 *
6054 * For TLS this is the identity.
6055 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6056 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6057 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6058 */
6059void ssl_write_version( int major, int minor, int transport,
6060 unsigned char ver[2] )
6061{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006062#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006063 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006064 {
6065 if( minor == SSL_MINOR_VERSION_2 )
6066 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6067
6068 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6069 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6070 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006071 else
6072#else
6073 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006074#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006075 {
6076 ver[0] = (unsigned char) major;
6077 ver[1] = (unsigned char) minor;
6078 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006079}
6080
6081void ssl_read_version( int *major, int *minor, int transport,
6082 const unsigned char ver[2] )
6083{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006084#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006085 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006086 {
6087 *major = 255 - ver[0] + 2;
6088 *minor = 255 - ver[1] + 1;
6089
6090 if( *minor == SSL_MINOR_VERSION_1 )
6091 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6092 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006093 else
6094#else
6095 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006096#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006097 {
6098 *major = ver[0];
6099 *minor = ver[1];
6100 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006101}
6102
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006103#endif /* POLARSSL_SSL_TLS_C */