blob: 9160baa72c3126148ecf1cfdb33652dadf9cc7d9 [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
1916 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001917 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001918
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1920
Paul Bakker831a7552011-05-18 13:32:51 +00001921 if( ret == 0 )
1922 return( POLARSSL_ERR_SSL_CONN_EOF );
1923
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 if( ret < 0 )
1925 return( ret );
1926
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001927 ssl->in_left = ret;
1928 }
1929 else
1930#endif
1931 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001932 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1933 ssl->in_left, nb_want ) );
1934
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001935 while( ssl->in_left < nb_want )
1936 {
1937 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001938 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001939
1940 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1941 ssl->in_left, nb_want ) );
1942 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1943
1944 if( ret == 0 )
1945 return( POLARSSL_ERR_SSL_CONN_EOF );
1946
1947 if( ret < 0 )
1948 return( ret );
1949
1950 ssl->in_left += ret;
1951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 }
1953
1954 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1955
1956 return( 0 );
1957}
1958
1959/*
1960 * Flush any data not yet written
1961 */
1962int ssl_flush_output( ssl_context *ssl )
1963{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001964 int ret;
1965 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001966
1967 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1968
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001969 if( ssl->f_send == NULL )
1970 {
1971 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1972 "or ssl_set_bio_timeout()" ) );
1973 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1974 }
1975
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001976 /* Avoid incrementing counter if data is flushed */
1977 if( ssl->out_left == 0 )
1978 {
1979 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1980 return( 0 );
1981 }
1982
Paul Bakker5121ce52009-01-03 21:22:43 +00001983 while( ssl->out_left > 0 )
1984 {
1985 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001986 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001988 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
1989 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001990 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001991
Paul Bakker5121ce52009-01-03 21:22:43 +00001992 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1993
1994 if( ret <= 0 )
1995 return( ret );
1996
1997 ssl->out_left -= ret;
1998 }
1999
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002000 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002001 if( ++ssl->out_ctr[i - 1] != 0 )
2002 break;
2003
2004 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002005 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002006 {
2007 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2008 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2009 }
2010
Paul Bakker5121ce52009-01-03 21:22:43 +00002011 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2012
2013 return( 0 );
2014}
2015
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002016/*
2017 * Functions to handle the DTLS retransmission state machine
2018 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002019#if defined(POLARSSL_SSL_PROTO_DTLS)
2020/*
2021 * Append current handshake message to current outgoing flight
2022 */
2023static int ssl_flight_append( ssl_context *ssl )
2024{
2025 ssl_flight_item *msg;
2026
2027 /* Allocate space for current message */
2028 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2029 {
2030 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2031 sizeof( ssl_flight_item ) ) );
2032 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2033 }
2034
2035 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2036 {
2037 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2038 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2039 }
2040
2041 /* Copy current handshake message with headers */
2042 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2043 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002044 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002045 msg->next = NULL;
2046
2047 /* Append to the current flight */
2048 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002049 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002050 else
2051 {
2052 ssl_flight_item *cur = ssl->handshake->flight;
2053 while( cur->next != NULL )
2054 cur = cur->next;
2055 cur->next = msg;
2056 }
2057
2058 return( 0 );
2059}
2060
2061/*
2062 * Free the current flight of handshake messages
2063 */
2064static void ssl_flight_free( ssl_flight_item *flight )
2065{
2066 ssl_flight_item *cur = flight;
2067 ssl_flight_item *next;
2068
2069 while( cur != NULL )
2070 {
2071 next = cur->next;
2072
2073 polarssl_free( cur->p );
2074 polarssl_free( cur );
2075
2076 cur = next;
2077 }
2078}
2079
2080/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002081 * Swap transform_out and out_ctr with the alternative ones
2082 */
2083static void ssl_swap_epochs( ssl_context *ssl )
2084{
2085 ssl_transform *tmp_transform;
2086 unsigned char tmp_out_ctr[8];
2087
2088 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2089 {
2090 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2091 return;
2092 }
2093
2094 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2095
2096 tmp_transform = ssl->transform_out;
2097 ssl->transform_out = ssl->handshake->alt_transform_out;
2098 ssl->handshake->alt_transform_out = tmp_transform;
2099
2100 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2101 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2102 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
2103}
2104
2105/*
2106 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002107 *
2108 * Need to remember the current message in case flush_output returns
2109 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002110 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002111 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002112int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002113{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002114 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002115
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002116 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2117 {
2118 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2119
2120 ssl->handshake->cur_msg = ssl->handshake->flight;
2121 ssl_swap_epochs( ssl );
2122
2123 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2124 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002125
2126 while( ssl->handshake->cur_msg != NULL )
2127 {
2128 int ret;
2129 ssl_flight_item *cur = ssl->handshake->cur_msg;
2130
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002131 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002132 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002133 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002134
2135 ssl->handshake->cur_msg = cur->next;
2136
2137 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2138
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002139 /* Swap epochs before sending Finished: we can't do it right after
2140 * sending ChangeCipherSpec, in case write returns WANT_READ */
2141 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE &&
2142 ssl->out_msg[0] == SSL_HS_FINISHED )
2143 {
2144 ssl_swap_epochs( ssl );
2145 }
2146
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002147 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2148 {
2149 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2150 return( ret );
2151 }
2152 }
2153
2154 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2155
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002156 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002157
2158 return( 0 );
2159}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002160
2161/*
2162 * To be called when the last message of an incoming flight is received.
2163 */
2164void ssl_recv_flight_completed( ssl_context *ssl )
2165{
2166 /* We won't need to resend that one any more */
2167 ssl_flight_free( ssl->handshake->flight );
2168 ssl->handshake->flight = NULL;
2169 ssl->handshake->cur_msg = NULL;
2170
2171 /* The next incoming flight will start with this msg_seq */
2172 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2173
2174 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2175 ssl->in_msg[0] == SSL_HS_FINISHED )
2176 {
2177 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2178 }
2179 else
2180 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2181}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002182#endif /* POLARSSL_SSL_PROTO_DTLS */
2183
Paul Bakker5121ce52009-01-03 21:22:43 +00002184/*
2185 * Record layer functions
2186 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002187
2188/*
2189 * Write current record.
2190 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2191 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002192int ssl_write_record( ssl_context *ssl )
2193{
Paul Bakker05ef8352012-05-08 09:17:57 +00002194 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002195 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
2197 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2198
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002199#if defined(POLARSSL_SSL_PROTO_DTLS)
2200 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2201 ssl->handshake != NULL &&
2202 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2203 {
2204 ; /* Skip special handshake treatment when resending */
2205 }
2206 else
2207#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2209 {
2210 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2211 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2212 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2213
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002214 /*
2215 * DTLS has additional fields in the Handshake layer,
2216 * between the length field and the actual payload:
2217 * uint16 message_seq;
2218 * uint24 fragment_offset;
2219 * uint24 fragment_length;
2220 */
2221#if defined(POLARSSL_SSL_PROTO_DTLS)
2222 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2223 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002224 /* Make room for the additional DTLS fields */
2225 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002226 ssl->out_msglen += 8;
2227 len += 8;
2228
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002229 /* Write message_seq and update it, except for HelloRequest */
2230 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2231 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002232 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2233 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2234 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002235 }
2236 else
2237 {
2238 ssl->out_msg[4] = 0;
2239 ssl->out_msg[5] = 0;
2240 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002241
2242 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2243 memset( ssl->out_msg + 6, 0x00, 3 );
2244 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002245 }
2246#endif /* POLARSSL_SSL_PROTO_DTLS */
2247
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002248 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2249 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 }
2251
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002252 /* Save handshake and CCS messages for resending */
2253#if defined(POLARSSL_SSL_PROTO_DTLS)
2254 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2255 ssl->handshake != NULL &&
2256 ssl->handshake->retransmit_state == SSL_RETRANS_PREPARING &&
2257 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2258 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2259 {
2260 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2261 {
2262 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2263 return( ret );
2264 }
2265 }
2266#endif
2267
Paul Bakker2770fbd2012-07-03 13:30:23 +00002268#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002269 if( ssl->transform_out != NULL &&
2270 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002271 {
2272 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2273 {
2274 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2275 return( ret );
2276 }
2277
2278 len = ssl->out_msglen;
2279 }
2280#endif /*POLARSSL_ZLIB_SUPPORT */
2281
Paul Bakker05ef8352012-05-08 09:17:57 +00002282#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002283 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002285 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
Paul Bakker05ef8352012-05-08 09:17:57 +00002287 ret = ssl_hw_record_write( ssl );
2288 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2289 {
2290 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002291 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002292 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002293
2294 if( ret == 0 )
2295 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002296 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002297#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002298 if( !done )
2299 {
2300 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002301 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2302 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002303
2304 ssl->out_len[0] = (unsigned char)( len >> 8 );
2305 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002306
Paul Bakker48916f92012-09-16 19:57:18 +00002307 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002308 {
2309 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2310 {
2311 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2312 return( ret );
2313 }
2314
2315 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002316 ssl->out_len[0] = (unsigned char)( len >> 8 );
2317 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002318 }
2319
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002320 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002321
2322 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2323 "version = [%d:%d], msglen = %d",
2324 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002325 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002326
2327 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002328 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002329 }
2330
Paul Bakker5121ce52009-01-03 21:22:43 +00002331 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2332 {
2333 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2334 return( ret );
2335 }
2336
2337 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2338
2339 return( 0 );
2340}
2341
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002342#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002343/*
2344 * Mark bits in bitmask (used for DTLS HS reassembly)
2345 */
2346static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2347{
2348 unsigned int start_bits, end_bits;
2349
2350 start_bits = 8 - ( offset % 8 );
2351 if( start_bits != 8 )
2352 {
2353 size_t first_byte_idx = offset / 8;
2354
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002355 /* Special case */
2356 if( len <= start_bits )
2357 {
2358 for( ; len != 0; len-- )
2359 mask[first_byte_idx] |= 1 << ( start_bits - len );
2360
2361 /* Avoid potential issues with offset or len becoming invalid */
2362 return;
2363 }
2364
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002365 offset += start_bits; /* Now offset % 8 == 0 */
2366 len -= start_bits;
2367
2368 for( ; start_bits != 0; start_bits-- )
2369 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2370 }
2371
2372 end_bits = len % 8;
2373 if( end_bits != 0 )
2374 {
2375 size_t last_byte_idx = ( offset + len ) / 8;
2376
2377 len -= end_bits; /* Now len % 8 == 0 */
2378
2379 for( ; end_bits != 0; end_bits-- )
2380 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2381 }
2382
2383 memset( mask + offset / 8, 0xFF, len / 8 );
2384}
2385
2386/*
2387 * Check that bitmask is full
2388 */
2389static int ssl_bitmask_check( unsigned char *mask, size_t len )
2390{
2391 size_t i;
2392
2393 for( i = 0; i < len / 8; i++ )
2394 if( mask[i] != 0xFF )
2395 return( -1 );
2396
2397 for( i = 0; i < len % 8; i++ )
2398 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2399 return( -1 );
2400
2401 return( 0 );
2402}
2403
2404/*
2405 * Reassemble fragmented DTLS handshake messages.
2406 *
2407 * Use a temporary buffer for reassembly, divided in two parts:
2408 * - the first holds the reassembled message (including handshake header),
2409 * - the second holds a bitmask indicating which parts of the message
2410 * (excluding headers) have been received so far.
2411 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002412static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2413{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002414 unsigned char *msg, *bitmask;
2415 size_t frag_len, frag_off;
2416 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2417
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002418 if( ssl->handshake == NULL )
2419 {
2420 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2421 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2422 }
2423
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002424 /*
2425 * For first fragment, check size and allocate buffer
2426 */
2427 if( ssl->handshake->hs_msg == NULL )
2428 {
2429 size_t alloc_len;
2430
2431 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2432 msg_len ) );
2433
2434 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2435 {
2436 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2437 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2438 }
2439
2440 /* The bitmask needs one bit per byte of message excluding header */
2441 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2442
2443 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2444 if( ssl->handshake->hs_msg == NULL )
2445 {
2446 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2447 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2448 }
2449
2450 memset( ssl->handshake->hs_msg, 0, alloc_len );
2451
2452 /* Prepare final header: copy msg_type, length and message_seq,
2453 * then add standardised fragment_offset and fragment_length */
2454 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2455 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2456 memcpy( ssl->handshake->hs_msg + 9,
2457 ssl->handshake->hs_msg + 1, 3 );
2458 }
2459 else
2460 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002461 /* Make sure msg_type and length are consistent */
2462 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002463 {
2464 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2465 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2466 }
2467 }
2468
2469 msg = ssl->handshake->hs_msg + 12;
2470 bitmask = msg + msg_len;
2471
2472 /*
2473 * Check and copy current fragment
2474 */
2475 frag_off = ( ssl->in_msg[6] << 16 ) |
2476 ( ssl->in_msg[7] << 8 ) |
2477 ssl->in_msg[8];
2478 frag_len = ( ssl->in_msg[9] << 16 ) |
2479 ( ssl->in_msg[10] << 8 ) |
2480 ssl->in_msg[11];
2481
2482 if( frag_off + frag_len > msg_len )
2483 {
2484 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2485 frag_off, frag_len, msg_len ) );
2486 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2487 }
2488
2489 if( frag_len + 12 > ssl->in_msglen )
2490 {
2491 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2492 frag_len, ssl->in_msglen ) );
2493 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2494 }
2495
2496 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2497 frag_off, frag_len ) );
2498
2499 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2500 ssl_bitmask_set( bitmask, frag_off, frag_len );
2501
2502 /*
2503 * Do we have the complete message by now?
2504 * If yes, finalize it, else ask to read the next record.
2505 */
2506 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2507 {
2508 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2509 return( POLARSSL_ERR_NET_WANT_READ );
2510 }
2511
2512 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2513
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002514 if( ssl->in_left > ssl->next_record_offset )
2515 {
2516 /*
2517 * We've got more data in the buffer after the current record,
2518 * that we don't want to overwrite. Move it before writing the
2519 * reassembled message, and adjust in_left and next_record_offset.
2520 */
2521 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2522 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2523 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2524
2525 /* First compute and check new lengths */
2526 ssl->next_record_offset = new_remain - ssl->in_hdr;
2527 ssl->in_left = ssl->next_record_offset + remain_len;
2528
2529 if( ssl->in_left > SSL_BUFFER_LEN -
2530 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2531 {
2532 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2533 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2534 }
2535
2536 memmove( new_remain, cur_remain, remain_len );
2537 }
2538
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002539 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2540
2541 polarssl_free( ssl->handshake->hs_msg );
2542 ssl->handshake->hs_msg = NULL;
2543
2544 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2545 ssl->in_msg, ssl->in_hslen );
2546
2547 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002548}
2549#endif /* POLARSSL_SSL_PROTO_DTLS */
2550
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002551static int ssl_prepare_handshake_record( ssl_context *ssl )
2552{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002553 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2554 {
2555 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2556 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002557 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002558 }
2559
2560 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2561 ( ssl->in_msg[1] << 16 ) |
2562 ( ssl->in_msg[2] << 8 ) |
2563 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002564
2565 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2566 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002567 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002568
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002569#if defined(POLARSSL_SSL_PROTO_DTLS)
2570 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002571 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002572 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002573 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002574
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002575 /* ssl->handshake is NULL when receiving ClientHello for renego */
2576 if( ssl->handshake != NULL &&
2577 recv_msg_seq != ssl->handshake->in_msg_seq )
2578 {
2579 SSL_DEBUG_MSG( 2, ( "dropping out-of-order message: "
2580 "message_seq = %d, expected = %d",
2581 recv_msg_seq, ssl->handshake->in_msg_seq ) );
2582 return( POLARSSL_ERR_NET_WANT_READ );
2583 }
2584 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002585
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002586 /* Reassemble if current message is fragmented or reassembly is
2587 * already in progress */
2588 if( ssl->in_msglen < ssl->in_hslen ||
2589 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2590 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2591 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002592 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002593 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2594
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002595 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2596 {
2597 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2598 return( ret );
2599 }
2600 }
2601 }
2602 else
2603#endif /* POLARSSL_SSL_PROTO_DTLS */
2604 /* With TLS we don't handle fragmentation (for now) */
2605 if( ssl->in_msglen < ssl->in_hslen )
2606 {
2607 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002608 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002609 }
2610
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002611 if( ssl->state != SSL_HANDSHAKE_OVER )
2612 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2613
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002614 /* Handshake message is complete, increment counter */
2615#if defined(POLARSSL_SSL_PROTO_DTLS)
2616 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2617 ssl->handshake != NULL )
2618 {
2619 ssl->handshake->in_msg_seq++;
2620 }
2621#endif
2622
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002623 return( 0 );
2624}
2625
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002626/*
2627 * ContentType type;
2628 * ProtocolVersion version;
2629 * uint16 epoch; // DTLS only
2630 * uint48 sequence_number; // DTLS only
2631 * uint16 length;
2632 */
2633static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002634{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002635 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002636 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002638 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2639
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002641 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002642 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2645 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002646 ssl->in_msgtype,
2647 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002649 /* Check record type */
2650 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2651 ssl->in_msgtype != SSL_MSG_ALERT &&
2652 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2653 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2654 {
2655 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002656
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002657 if( ( ret = ssl_send_alert_message( ssl,
2658 SSL_ALERT_LEVEL_FATAL,
2659 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2660 {
2661 return( ret );
2662 }
2663
2664 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2665 }
2666
2667 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002668 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 {
2670 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002671 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 }
2673
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002674 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002675 {
2676 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002677 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
2679
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002680 /* Check epoch with DTLS */
2681#if defined(POLARSSL_SSL_PROTO_DTLS)
2682 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2683 {
2684 unsigned int exp_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
2685 unsigned int rec_epoch = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2686
2687 if( exp_epoch != rec_epoch )
2688 {
2689 SSL_DEBUG_MSG( 1, ( "discarding record from another epoch: "
2690 "expected %d, received %d",
2691 exp_epoch, rec_epoch ) );
2692 return( POLARSSL_ERR_NET_WANT_READ );
2693 }
2694 }
2695#endif
2696
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002697 /* Check length against the size of our buffer */
2698 if( ssl->in_msglen > SSL_BUFFER_LEN
2699 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002700 {
2701 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2702 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2703 }
2704
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002705 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002706 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002708 if( ssl->in_msglen < 1 ||
2709 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 {
2711 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002712 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 }
2714 }
2715 else
2716 {
Paul Bakker48916f92012-09-16 19:57:18 +00002717 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 {
2719 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002720 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 }
2722
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002723#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002725 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 {
2727 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002728 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002730#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002731#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2732 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 /*
2734 * TLS encrypted messages can have up to 256 bytes of padding
2735 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002736 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002737 ssl->in_msglen > ssl->transform_in->minlen +
2738 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 {
2740 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002741 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002743#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002746 return( 0 );
2747}
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002749/*
2750 * If applicable, decrypt (and decompress) record content
2751 */
2752static int ssl_prepare_record_content( ssl_context *ssl )
2753{
2754 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002755
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002757 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Paul Bakker05ef8352012-05-08 09:17:57 +00002759#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002760 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002761 {
2762 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2763
2764 ret = ssl_hw_record_read( ssl );
2765 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2766 {
2767 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002768 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002769 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002770
2771 if( ret == 0 )
2772 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002773 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002774#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002775 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 {
2777 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2778 {
2779 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2780 return( ret );
2781 }
2782
2783 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2784 ssl->in_msg, ssl->in_msglen );
2785
2786 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2787 {
2788 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002789 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002790 }
2791 }
2792
Paul Bakker2770fbd2012-07-03 13:30:23 +00002793#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002794 if( ssl->transform_in != NULL &&
2795 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002796 {
2797 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2798 {
2799 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2800 return( ret );
2801 }
2802
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002803 // TODO: what's the purpose of these lines? is in_len used?
2804 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2805 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002806 }
2807#endif /* POLARSSL_ZLIB_SUPPORT */
2808
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002809 return( 0 );
2810}
2811
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002812/*
2813 * Read a record.
2814 *
2815 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
2816 * and continue reading until a valid record is found.
2817 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002818int ssl_read_record( ssl_context *ssl )
2819{
2820 int ret;
2821
2822 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2823
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02002824 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002825 {
2826 /*
2827 * Get next Handshake message in the current record
2828 */
2829 ssl->in_msglen -= ssl->in_hslen;
2830
2831 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
2832 ssl->in_msglen );
2833
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002834 SSL_DEBUG_BUF( 4, "remaining content in record",
2835 ssl->in_msg, ssl->in_msglen );
2836
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002837 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2838 return( ret );
2839
2840 return( 0 );
2841 }
2842
2843 ssl->in_hslen = 0;
2844
2845 /*
2846 * Read the record header and parse it
2847 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002848#if defined(POLARSSL_SSL_PROTO_DTLS)
2849read_record_header:
2850#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002851 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
2852 {
2853 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2854 return( ret );
2855 }
2856
2857 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002858 {
2859#if defined(POLARSSL_SSL_PROTO_DTLS)
2860 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2861 {
2862 /* Ignore bad record and get next one; drop the whole datagram
2863 * since current header cannot be trusted to find the next record
2864 * in current datagram */
2865 ssl->next_record_offset = 0;
2866 ssl->in_left = 0;
2867
2868 SSL_DEBUG_MSG( 2, ( "discarding invalid record" ) );
2869 goto read_record_header;
2870 }
2871#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002872 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002873 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002874
2875 /*
2876 * Read and optionally decrypt the message contents
2877 */
2878 if( ( ret = ssl_fetch_input( ssl,
2879 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
2880 {
2881 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2882 return( ret );
2883 }
2884
2885 /* Done reading this record, get ready for the next one */
2886#if defined(POLARSSL_SSL_PROTO_DTLS)
2887 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2888 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
2889 else
2890#endif
2891 ssl->in_left = 0;
2892
2893 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002894 {
2895#if defined(POLARSSL_SSL_PROTO_DTLS)
2896 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2897 {
2898 /* Silently discard invalid records */
2899 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
2900 ret == POLARSSL_ERR_SSL_INVALID_MAC )
2901 {
2902 SSL_DEBUG_MSG( 2, ( "discarding invalid record" ) );
2903 goto read_record_header;
2904 }
2905
2906 return( ret );
2907 }
2908 else
2909#endif
2910 {
2911 /* Error out (and send alert) on invalid records */
2912#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2913 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2914 {
2915 ssl_send_alert_message( ssl,
2916 SSL_ALERT_LEVEL_FATAL,
2917 SSL_ALERT_MSG_BAD_RECORD_MAC );
2918 }
2919#endif
2920 return( ret );
2921 }
2922 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002923
2924 /*
2925 * Handle particular types of records
2926 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2928 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002929 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2930 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 }
2932
2933 if( ssl->in_msgtype == SSL_MSG_ALERT )
2934 {
2935 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2936 ssl->in_msg[0], ssl->in_msg[1] ) );
2937
2938 /*
2939 * Ignore non-fatal alerts, except close_notify
2940 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002941 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002943 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2944 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002945 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 }
2947
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002948 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2949 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 {
2951 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002952 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
2954 }
2955
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2957
2958 return( 0 );
2959}
2960
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002961int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2962{
2963 int ret;
2964
2965 if( ( ret = ssl_send_alert_message( ssl,
2966 SSL_ALERT_LEVEL_FATAL,
2967 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2968 {
2969 return( ret );
2970 }
2971
2972 return( 0 );
2973}
2974
Paul Bakker0a925182012-04-16 06:46:41 +00002975int ssl_send_alert_message( ssl_context *ssl,
2976 unsigned char level,
2977 unsigned char message )
2978{
2979 int ret;
2980
2981 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2982
2983 ssl->out_msgtype = SSL_MSG_ALERT;
2984 ssl->out_msglen = 2;
2985 ssl->out_msg[0] = level;
2986 ssl->out_msg[1] = message;
2987
2988 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2989 {
2990 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2991 return( ret );
2992 }
2993
2994 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2995
2996 return( 0 );
2997}
2998
Paul Bakker5121ce52009-01-03 21:22:43 +00002999/*
3000 * Handshake functions
3001 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003002#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3003 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3004 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3005 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3006 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3007 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3008 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003009int ssl_write_certificate( ssl_context *ssl )
3010{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003011 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
3013 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3014
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003015 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003016 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3017 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003018 {
3019 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3020 ssl->state++;
3021 return( 0 );
3022 }
3023
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003024 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3025 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003026}
3027
3028int ssl_parse_certificate( ssl_context *ssl )
3029{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003030 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3031
3032 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3033
3034 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003035 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3036 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003037 {
3038 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3039 ssl->state++;
3040 return( 0 );
3041 }
3042
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003043 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3044 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045}
3046#else
3047int ssl_write_certificate( ssl_context *ssl )
3048{
3049 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3050 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003051 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003052 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3053
3054 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3055
3056 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003057 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3058 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003059 {
3060 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3061 ssl->state++;
3062 return( 0 );
3063 }
3064
Paul Bakker5121ce52009-01-03 21:22:43 +00003065 if( ssl->endpoint == SSL_IS_CLIENT )
3066 {
3067 if( ssl->client_auth == 0 )
3068 {
3069 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3070 ssl->state++;
3071 return( 0 );
3072 }
3073
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003074#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003075 /*
3076 * If using SSLv3 and got no cert, send an Alert message
3077 * (otherwise an empty Certificate message will be sent).
3078 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003079 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3081 {
3082 ssl->out_msglen = 2;
3083 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003084 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3085 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
3087 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3088 goto write_msg;
3089 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003090#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 }
3092 else /* SSL_IS_SERVER */
3093 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003094 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
3096 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003097 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003098 }
3099 }
3100
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003101 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102
3103 /*
3104 * 0 . 0 handshake type
3105 * 1 . 3 handshake length
3106 * 4 . 6 length of all certs
3107 * 7 . 9 length of cert. 1
3108 * 10 . n-1 peer certificate
3109 * n . n+2 length of cert. 2
3110 * n+3 . ... upper level cert, etc.
3111 */
3112 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003113 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker29087132010-03-21 21:03:34 +00003115 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 {
3117 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003118 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003119 {
3120 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3121 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003122 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003123 }
3124
3125 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3126 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3127 ssl->out_msg[i + 2] = (unsigned char)( n );
3128
3129 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3130 i += n; crt = crt->next;
3131 }
3132
3133 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3134 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3135 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3136
3137 ssl->out_msglen = i;
3138 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3139 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3140
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003141#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003142write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003143#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003144
3145 ssl->state++;
3146
3147 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3148 {
3149 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3150 return( ret );
3151 }
3152
3153 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3154
Paul Bakkered27a042013-04-18 22:46:23 +02003155 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003156}
3157
3158int ssl_parse_certificate( ssl_context *ssl )
3159{
Paul Bakkered27a042013-04-18 22:46:23 +02003160 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003161 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003162 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003163
3164 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3165
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003166 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003167 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3168 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003169 {
3170 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3171 ssl->state++;
3172 return( 0 );
3173 }
3174
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003176 ( ssl->authmode == SSL_VERIFY_NONE ||
3177 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003179 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3181 ssl->state++;
3182 return( 0 );
3183 }
3184
3185 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3186 {
3187 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3188 return( ret );
3189 }
3190
3191 ssl->state++;
3192
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003193#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003194 /*
3195 * Check if the client sent an empty certificate
3196 */
3197 if( ssl->endpoint == SSL_IS_SERVER &&
3198 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3199 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003200 if( ssl->in_msglen == 2 &&
3201 ssl->in_msgtype == SSL_MSG_ALERT &&
3202 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3203 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 {
3205 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3206
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003207 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3209 return( 0 );
3210 else
Paul Bakker40e46942009-01-03 21:51:57 +00003211 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 }
3213 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003214#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003215
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003216#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3217 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 if( ssl->endpoint == SSL_IS_SERVER &&
3219 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3220 {
3221 if( ssl->in_hslen == 7 &&
3222 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3223 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
3224 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
3225 {
3226 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3227
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003228 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003230 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 else
3232 return( 0 );
3233 }
3234 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003235#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3236 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003237
3238 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3239 {
3240 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003241 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 }
3243
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003244 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3245 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003246 {
3247 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003248 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 }
3250
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003251 i = ssl_hs_hdr_len( ssl );
3252
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 /*
3254 * Same message structure as in ssl_write_certificate()
3255 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003256 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003257
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003258 if( ssl->in_msg[i] != 0 ||
3259 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 {
3261 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003262 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 }
3264
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003265 /* In case we tried to reuse a session but it failed */
3266 if( ssl->session_negotiate->peer_cert != NULL )
3267 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003268 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003269 polarssl_free( ssl->session_negotiate->peer_cert );
3270 }
3271
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003272 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3273 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003274 {
3275 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003276 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003277 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 }
3279
Paul Bakkerb6b09562013-09-18 14:17:41 +02003280 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003281
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003282 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283
3284 while( i < ssl->in_hslen )
3285 {
3286 if( ssl->in_msg[i] != 0 )
3287 {
3288 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003289 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 }
3291
3292 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3293 | (unsigned int) ssl->in_msg[i + 2];
3294 i += 3;
3295
3296 if( n < 128 || i + n > ssl->in_hslen )
3297 {
3298 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003299 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 }
3301
Paul Bakkerddf26b42013-09-18 13:46:23 +02003302 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3303 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 if( ret != 0 )
3305 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003306 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 return( ret );
3308 }
3309
3310 i += n;
3311 }
3312
Paul Bakker48916f92012-09-16 19:57:18 +00003313 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003315 /*
3316 * On client, make sure the server cert doesn't change during renego to
3317 * avoid "triple handshake" attack: https://secure-resumption.com/
3318 */
3319 if( ssl->endpoint == SSL_IS_CLIENT &&
3320 ssl->renegotiation == SSL_RENEGOTIATION )
3321 {
3322 if( ssl->session->peer_cert == NULL )
3323 {
3324 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3325 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3326 }
3327
3328 if( ssl->session->peer_cert->raw.len !=
3329 ssl->session_negotiate->peer_cert->raw.len ||
3330 memcmp( ssl->session->peer_cert->raw.p,
3331 ssl->session_negotiate->peer_cert->raw.p,
3332 ssl->session->peer_cert->raw.len ) != 0 )
3333 {
3334 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3335 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3336 }
3337 }
3338
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 if( ssl->authmode != SSL_VERIFY_NONE )
3340 {
3341 if( ssl->ca_chain == NULL )
3342 {
3343 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003344 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003345 }
3346
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003347 /*
3348 * Main check: verify certificate
3349 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003350 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3351 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3352 &ssl->session_negotiate->verify_result,
3353 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003354
3355 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003356 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003358 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003359
3360 /*
3361 * Secondary checks: always done, but change 'ret' only if it was 0
3362 */
3363
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003364#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003365 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003366 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003367
3368 /* If certificate uses an EC key, make sure the curve is OK */
3369 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3370 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3371 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003372 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003373 if( ret == 0 )
3374 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003375 }
3376 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003377#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003378
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003379 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3380 ciphersuite_info,
3381 ! ssl->endpoint ) != 0 )
3382 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003383 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003384 if( ret == 0 )
3385 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3386 }
3387
Paul Bakker5121ce52009-01-03 21:22:43 +00003388 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3389 ret = 0;
3390 }
3391
3392 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3393
3394 return( ret );
3395}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003396#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3397 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3398 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3399 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3400 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3401 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3402 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003403
3404int ssl_write_change_cipher_spec( ssl_context *ssl )
3405{
3406 int ret;
3407
3408 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3409
3410 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3411 ssl->out_msglen = 1;
3412 ssl->out_msg[0] = 1;
3413
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 ssl->state++;
3415
3416 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3417 {
3418 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3419 return( ret );
3420 }
3421
3422 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3423
3424 return( 0 );
3425}
3426
3427int ssl_parse_change_cipher_spec( ssl_context *ssl )
3428{
3429 int ret;
3430
3431 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3432
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3434 {
3435 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3436 return( ret );
3437 }
3438
3439 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3440 {
3441 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003442 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 }
3444
3445 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3446 {
3447 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003448 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 }
3450
3451 ssl->state++;
3452
3453 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3454
3455 return( 0 );
3456}
3457
Paul Bakker41c83d32013-03-20 14:39:14 +01003458void ssl_optimize_checksum( ssl_context *ssl,
3459 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003460{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003461 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003462
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003463#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3464 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003465 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003466 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003467 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003468#endif
3469#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3470#if defined(POLARSSL_SHA512_C)
3471 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3472 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3473 else
3474#endif
3475#if defined(POLARSSL_SHA256_C)
3476 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003477 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003478 else
3479#endif
3480#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003481 {
3482 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003483 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003484 }
Paul Bakker380da532012-04-18 16:10:25 +00003485}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003486
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003487void ssl_reset_checksum( ssl_context *ssl )
3488{
3489#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3490 defined(POLARSSL_SSL_PROTO_TLS1_1)
3491 md5_starts( &ssl->handshake->fin_md5 );
3492 sha1_starts( &ssl->handshake->fin_sha1 );
3493#endif
3494#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3495#if defined(POLARSSL_SHA256_C)
3496 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3497#endif
3498#if defined(POLARSSL_SHA512_C)
3499 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3500#endif
3501#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3502}
3503
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003504static void ssl_update_checksum_start( ssl_context *ssl,
3505 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003506{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003507#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3508 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003509 md5_update( &ssl->handshake->fin_md5 , buf, len );
3510 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003511#endif
3512#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3513#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003514 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003515#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003516#if defined(POLARSSL_SHA512_C)
3517 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003518#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003519#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003520}
3521
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003522#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3523 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003524static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3525 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003526{
Paul Bakker48916f92012-09-16 19:57:18 +00003527 md5_update( &ssl->handshake->fin_md5 , buf, len );
3528 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003529}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003530#endif
Paul Bakker380da532012-04-18 16:10:25 +00003531
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003532#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3533#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003534static void ssl_update_checksum_sha256( ssl_context *ssl,
3535 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003536{
Paul Bakker9e36f042013-06-30 14:34:05 +02003537 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003538}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003539#endif
Paul Bakker380da532012-04-18 16:10:25 +00003540
Paul Bakker9e36f042013-06-30 14:34:05 +02003541#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003542static void ssl_update_checksum_sha384( ssl_context *ssl,
3543 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003544{
Paul Bakker9e36f042013-06-30 14:34:05 +02003545 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003546}
Paul Bakker769075d2012-11-24 11:26:46 +01003547#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003548#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003549
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003550#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003551static void ssl_calc_finished_ssl(
3552 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003553{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003554 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003555 md5_context md5;
3556 sha1_context sha1;
3557
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 unsigned char padbuf[48];
3559 unsigned char md5sum[16];
3560 unsigned char sha1sum[20];
3561
Paul Bakker48916f92012-09-16 19:57:18 +00003562 ssl_session *session = ssl->session_negotiate;
3563 if( !session )
3564 session = ssl->session;
3565
Paul Bakker1ef83d62012-04-11 12:09:53 +00003566 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3567
Paul Bakker48916f92012-09-16 19:57:18 +00003568 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3569 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003570
3571 /*
3572 * SSLv3:
3573 * hash =
3574 * MD5( master + pad2 +
3575 * MD5( handshake + sender + master + pad1 ) )
3576 * + SHA1( master + pad2 +
3577 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 */
3579
Paul Bakker90995b52013-06-24 19:20:35 +02003580#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003581 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003582 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003583#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003584
Paul Bakker90995b52013-06-24 19:20:35 +02003585#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003587 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003588#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003589
Paul Bakker3c2122f2013-06-24 19:03:14 +02003590 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3591 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003592
Paul Bakker1ef83d62012-04-11 12:09:53 +00003593 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594
Paul Bakker3c2122f2013-06-24 19:03:14 +02003595 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003596 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003597 md5_update( &md5, padbuf, 48 );
3598 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003599
Paul Bakker3c2122f2013-06-24 19:03:14 +02003600 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003601 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003602 sha1_update( &sha1, padbuf, 40 );
3603 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003604
Paul Bakker1ef83d62012-04-11 12:09:53 +00003605 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Paul Bakker1ef83d62012-04-11 12:09:53 +00003607 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003608 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003609 md5_update( &md5, padbuf, 48 );
3610 md5_update( &md5, md5sum, 16 );
3611 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003612
Paul Bakker1ef83d62012-04-11 12:09:53 +00003613 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003614 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003615 sha1_update( &sha1, padbuf , 40 );
3616 sha1_update( &sha1, sha1sum, 20 );
3617 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
Paul Bakker1ef83d62012-04-11 12:09:53 +00003619 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
Paul Bakker5b4af392014-06-26 12:09:34 +02003621 md5_free( &md5 );
3622 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003623
Paul Bakker34617722014-06-13 17:20:13 +02003624 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3625 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3626 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003627
3628 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3629}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003630#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003632#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003633static void ssl_calc_finished_tls(
3634 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003635{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003636 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003637 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003638 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003640 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003641
Paul Bakker48916f92012-09-16 19:57:18 +00003642 ssl_session *session = ssl->session_negotiate;
3643 if( !session )
3644 session = ssl->session;
3645
Paul Bakker1ef83d62012-04-11 12:09:53 +00003646 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003647
Paul Bakker48916f92012-09-16 19:57:18 +00003648 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3649 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003650
Paul Bakker1ef83d62012-04-11 12:09:53 +00003651 /*
3652 * TLSv1:
3653 * hash = PRF( master, finished_label,
3654 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3655 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
Paul Bakker90995b52013-06-24 19:20:35 +02003657#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003658 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3659 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003660#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003661
Paul Bakker90995b52013-06-24 19:20:35 +02003662#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003663 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3664 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003665#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003666
3667 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003668 ? "client finished"
3669 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003670
3671 md5_finish( &md5, padbuf );
3672 sha1_finish( &sha1, padbuf + 16 );
3673
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003674 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003675 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003676
3677 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3678
Paul Bakker5b4af392014-06-26 12:09:34 +02003679 md5_free( &md5 );
3680 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003681
Paul Bakker34617722014-06-13 17:20:13 +02003682 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003683
3684 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3685}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003686#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003687
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003688#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3689#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003690static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003691 ssl_context *ssl, unsigned char *buf, int from )
3692{
3693 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003694 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003695 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003696 unsigned char padbuf[32];
3697
Paul Bakker48916f92012-09-16 19:57:18 +00003698 ssl_session *session = ssl->session_negotiate;
3699 if( !session )
3700 session = ssl->session;
3701
Paul Bakker380da532012-04-18 16:10:25 +00003702 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003703
Paul Bakker9e36f042013-06-30 14:34:05 +02003704 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003705
3706 /*
3707 * TLSv1.2:
3708 * hash = PRF( master, finished_label,
3709 * Hash( handshake ) )[0.11]
3710 */
3711
Paul Bakker9e36f042013-06-30 14:34:05 +02003712#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003713 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003714 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003715#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003716
3717 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003718 ? "client finished"
3719 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003720
Paul Bakker9e36f042013-06-30 14:34:05 +02003721 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003722
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003723 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003724 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003725
3726 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3727
Paul Bakker5b4af392014-06-26 12:09:34 +02003728 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003729
Paul Bakker34617722014-06-13 17:20:13 +02003730 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003731
3732 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3733}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003734#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003735
Paul Bakker9e36f042013-06-30 14:34:05 +02003736#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003737static void ssl_calc_finished_tls_sha384(
3738 ssl_context *ssl, unsigned char *buf, int from )
3739{
3740 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003741 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003742 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003743 unsigned char padbuf[48];
3744
Paul Bakker48916f92012-09-16 19:57:18 +00003745 ssl_session *session = ssl->session_negotiate;
3746 if( !session )
3747 session = ssl->session;
3748
Paul Bakker380da532012-04-18 16:10:25 +00003749 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003750
Paul Bakker9e36f042013-06-30 14:34:05 +02003751 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003752
3753 /*
3754 * TLSv1.2:
3755 * hash = PRF( master, finished_label,
3756 * Hash( handshake ) )[0.11]
3757 */
3758
Paul Bakker9e36f042013-06-30 14:34:05 +02003759#if !defined(POLARSSL_SHA512_ALT)
3760 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3761 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003762#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003763
3764 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003765 ? "client finished"
3766 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003767
Paul Bakker9e36f042013-06-30 14:34:05 +02003768 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003769
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003770 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003771 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003772
3773 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3774
Paul Bakker5b4af392014-06-26 12:09:34 +02003775 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003776
Paul Bakker34617722014-06-13 17:20:13 +02003777 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003778
3779 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3780}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003781#endif /* POLARSSL_SHA512_C */
3782#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003783
Paul Bakker48916f92012-09-16 19:57:18 +00003784void ssl_handshake_wrapup( ssl_context *ssl )
3785{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003786 int resume = ssl->handshake->resume;
3787
Paul Bakker48916f92012-09-16 19:57:18 +00003788 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3789
3790 /*
3791 * Free our handshake params
3792 */
3793 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003794 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003795 ssl->handshake = NULL;
3796
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003797 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003798 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003799 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003800 ssl->renego_records_seen = 0;
3801 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003802
Paul Bakker48916f92012-09-16 19:57:18 +00003803 /*
3804 * Switch in our now active transform context
3805 */
3806 if( ssl->transform )
3807 {
3808 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003809 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003810 }
3811 ssl->transform = ssl->transform_negotiate;
3812 ssl->transform_negotiate = NULL;
3813
Paul Bakker0a597072012-09-25 21:55:46 +00003814 if( ssl->session )
3815 {
3816 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003817 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003818 }
3819 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003820 ssl->session_negotiate = NULL;
3821
Paul Bakker0a597072012-09-25 21:55:46 +00003822 /*
3823 * Add cache entry
3824 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003825 if( ssl->f_set_cache != NULL &&
3826 ssl->session->length != 0 &&
3827 resume == 0 )
3828 {
Paul Bakker0a597072012-09-25 21:55:46 +00003829 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3830 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003831 }
Paul Bakker0a597072012-09-25 21:55:46 +00003832
Paul Bakker48916f92012-09-16 19:57:18 +00003833 ssl->state++;
3834
3835 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3836}
3837
Paul Bakker1ef83d62012-04-11 12:09:53 +00003838int ssl_write_finished( ssl_context *ssl )
3839{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003840 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003841
3842 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3843
Paul Bakker92be97b2013-01-02 17:30:03 +01003844 /*
3845 * Set the out_msg pointer to the correct location based on IV length
3846 */
3847 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3848 {
3849 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3850 ssl->transform_negotiate->fixed_ivlen;
3851 }
3852 else
3853 ssl->out_msg = ssl->out_iv;
3854
Paul Bakker48916f92012-09-16 19:57:18 +00003855 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003856
3857 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3859
Paul Bakker48916f92012-09-16 19:57:18 +00003860 ssl->verify_data_len = hash_len;
3861 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3862
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 ssl->out_msglen = 4 + hash_len;
3864 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3865 ssl->out_msg[0] = SSL_HS_FINISHED;
3866
3867 /*
3868 * In case of session resuming, invert the client and server
3869 * ChangeCipherSpec messages order.
3870 */
Paul Bakker0a597072012-09-25 21:55:46 +00003871 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 {
3873 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003874 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 else
3876 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3877 }
3878 else
3879 ssl->state++;
3880
Paul Bakker48916f92012-09-16 19:57:18 +00003881 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003882 * Switch to our negotiated transform and session parameters for outbound
3883 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003884 */
3885 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003886
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003887#if defined(POLARSSL_SSL_PROTO_DTLS)
3888 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3889 {
3890 unsigned char i;
3891
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003892 /* Remember current epoch settings for resending */
3893 ssl->handshake->alt_transform_out = ssl->transform_out;
3894 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
3895
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003896 /* Set sequence_number to zero */
3897 memset( ssl->out_ctr + 2, 0, 6 );
3898
3899 /* Increment epoch */
3900 for( i = 2; i > 0; i-- )
3901 if( ++ssl->out_ctr[i - 1] != 0 )
3902 break;
3903
3904 /* The loop goes to its end iff the counter is wrapping */
3905 if( i == 0 )
3906 {
3907 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3908 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3909 }
3910 }
3911 else
3912#endif /* POLARSSL_SSL_PROTO_DTLS */
3913 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003915 ssl->transform_out = ssl->transform_negotiate;
3916 ssl->session_out = ssl->session_negotiate;
3917
Paul Bakker07eb38b2012-12-19 14:42:06 +01003918#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003919 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003920 {
3921 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3922 {
3923 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3924 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3925 }
3926 }
3927#endif
3928
Paul Bakker5121ce52009-01-03 21:22:43 +00003929 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3930 {
3931 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3932 return( ret );
3933 }
3934
3935 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3936
3937 return( 0 );
3938}
3939
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003940#if defined(POLARSSL_SSL_PROTO_SSL3)
3941#define SSL_MAX_HASH_LEN 36
3942#else
3943#define SSL_MAX_HASH_LEN 12
3944#endif
3945
Paul Bakker5121ce52009-01-03 21:22:43 +00003946int ssl_parse_finished( ssl_context *ssl )
3947{
Paul Bakker23986e52011-04-24 08:57:21 +00003948 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003949 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00003950 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
3952 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3953
Paul Bakker48916f92012-09-16 19:57:18 +00003954 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Paul Bakker48916f92012-09-16 19:57:18 +00003956 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003957 * Switch to our negotiated transform and session parameters for inbound
3958 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003959 */
3960 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3961 ssl->transform_in = ssl->transform_negotiate;
3962 ssl->session_in = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003963
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02003964#if defined(POLARSSL_SSL_PROTO_DTLS)
3965 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3966 {
3967 unsigned char i;
3968
3969 /* Set sequence_number to zero */
3970 memset( ssl->in_ctr + 2, 0, 6 );
3971
3972 /* Increment epoch */
3973 for( i = 2; i > 0; i-- )
3974 if( ++ssl->in_ctr[i - 1] != 0 )
3975 break;
3976
3977 /* The loop goes to its end iff the counter is wrapping */
3978 if( i == 0 )
3979 {
3980 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3981 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3982 }
3983 }
3984 else
3985#endif /* POLARSSL_SSL_PROTO_DTLS */
3986 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987
Paul Bakker92be97b2013-01-02 17:30:03 +01003988 /*
3989 * Set the in_msg pointer to the correct location based on IV length
3990 */
3991 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3992 {
3993 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3994 ssl->transform_negotiate->fixed_ivlen;
3995 }
3996 else
3997 ssl->in_msg = ssl->in_iv;
3998
Paul Bakker07eb38b2012-12-19 14:42:06 +01003999#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004000 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004001 {
4002 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4003 {
4004 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4005 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4006 }
4007 }
4008#endif
4009
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4011 {
4012 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4013 return( ret );
4014 }
4015
4016 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4017 {
4018 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004019 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020 }
4021
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004022 /* There is currently no ciphersuite using another length with TLS 1.2 */
4023#if defined(POLARSSL_SSL_PROTO_SSL3)
4024 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4025 hash_len = 36;
4026 else
4027#endif
4028 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
4030 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004031 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 {
4033 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004034 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035 }
4036
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004037 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4038 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004039 {
4040 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004041 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004042 }
4043
Paul Bakker48916f92012-09-16 19:57:18 +00004044 ssl->verify_data_len = hash_len;
4045 memcpy( ssl->peer_verify_data, buf, hash_len );
4046
Paul Bakker0a597072012-09-25 21:55:46 +00004047 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 {
4049 if( ssl->endpoint == SSL_IS_CLIENT )
4050 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4051
4052 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004053 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 }
4055 else
4056 ssl->state++;
4057
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004058#if defined(POLARSSL_SSL_PROTO_DTLS)
4059 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4060 ssl_recv_flight_completed( ssl );
4061#endif
4062
Paul Bakker5121ce52009-01-03 21:22:43 +00004063 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4064
4065 return( 0 );
4066}
4067
Paul Bakker968afaa2014-07-09 11:09:24 +02004068static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004069{
4070 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4071
4072#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4073 defined(POLARSSL_SSL_PROTO_TLS1_1)
4074 md5_init( &handshake->fin_md5 );
4075 sha1_init( &handshake->fin_sha1 );
4076 md5_starts( &handshake->fin_md5 );
4077 sha1_starts( &handshake->fin_sha1 );
4078#endif
4079#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4080#if defined(POLARSSL_SHA256_C)
4081 sha256_init( &handshake->fin_sha256 );
4082 sha256_starts( &handshake->fin_sha256, 0 );
4083#endif
4084#if defined(POLARSSL_SHA512_C)
4085 sha512_init( &handshake->fin_sha512 );
4086 sha512_starts( &handshake->fin_sha512, 1 );
4087#endif
4088#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4089
4090 handshake->update_checksum = ssl_update_checksum_start;
4091 handshake->sig_alg = SSL_HASH_SHA1;
4092
4093#if defined(POLARSSL_DHM_C)
4094 dhm_init( &handshake->dhm_ctx );
4095#endif
4096#if defined(POLARSSL_ECDH_C)
4097 ecdh_init( &handshake->ecdh_ctx );
4098#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004099}
4100
4101static void ssl_transform_init( ssl_transform *transform )
4102{
4103 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004104
4105 cipher_init( &transform->cipher_ctx_enc );
4106 cipher_init( &transform->cipher_ctx_dec );
4107
4108 md_init( &transform->md_ctx_enc );
4109 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004110}
4111
4112void ssl_session_init( ssl_session *session )
4113{
4114 memset( session, 0, sizeof(ssl_session) );
4115}
4116
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004117static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004118{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004119 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004120 if( ssl->transform_negotiate )
4121 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004122 if( ssl->session_negotiate )
4123 ssl_session_free( ssl->session_negotiate );
4124 if( ssl->handshake )
4125 ssl_handshake_free( ssl->handshake );
4126
4127 /*
4128 * Either the pointers are now NULL or cleared properly and can be freed.
4129 * Now allocate missing structures.
4130 */
4131 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004132 {
4133 ssl->transform_negotiate =
4134 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4135 }
Paul Bakker48916f92012-09-16 19:57:18 +00004136
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004137 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004138 {
4139 ssl->session_negotiate =
4140 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4141 }
Paul Bakker48916f92012-09-16 19:57:18 +00004142
Paul Bakker82788fb2014-10-20 13:59:19 +02004143 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004144 {
4145 ssl->handshake = (ssl_handshake_params *)
4146 polarssl_malloc( sizeof(ssl_handshake_params) );
4147 }
Paul Bakker48916f92012-09-16 19:57:18 +00004148
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004149 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004150 if( ssl->handshake == NULL ||
4151 ssl->transform_negotiate == NULL ||
4152 ssl->session_negotiate == NULL )
4153 {
4154 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004155
4156 polarssl_free( ssl->handshake );
4157 polarssl_free( ssl->transform_negotiate );
4158 polarssl_free( ssl->session_negotiate );
4159
4160 ssl->handshake = NULL;
4161 ssl->transform_negotiate = NULL;
4162 ssl->session_negotiate = NULL;
4163
Paul Bakker48916f92012-09-16 19:57:18 +00004164 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4165 }
4166
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004167 /* Initialize structures */
4168 ssl_session_init( ssl->session_negotiate );
4169 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004170 ssl_handshake_params_init( ssl->handshake );
4171
4172#if defined(POLARSSL_X509_CRT_PARSE_C)
4173 ssl->handshake->key_cert = ssl->key_cert;
4174#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004175
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004176#if defined(POLARSSL_SSL_PROTO_DTLS)
4177 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4178 {
4179 ssl->handshake->alt_transform_out = ssl->transform_out;
4180
4181 if( ssl->endpoint == SSL_IS_CLIENT )
4182 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4183 else
4184 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
4185 }
4186#endif
4187
Paul Bakker48916f92012-09-16 19:57:18 +00004188 return( 0 );
4189}
4190
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004191#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4192/* Dummy cookie callbacks for defaults */
4193static int ssl_cookie_write_dummy( void *ctx,
4194 unsigned char **p, unsigned char *end,
4195 const unsigned char *cli_id, size_t cli_id_len )
4196{
4197 ((void) ctx);
4198 ((void) p);
4199 ((void) end);
4200 ((void) cli_id);
4201 ((void) cli_id_len);
4202
4203 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4204}
4205
4206static int ssl_cookie_check_dummy( void *ctx,
4207 const unsigned char *cookie, size_t cookie_len,
4208 const unsigned char *cli_id, size_t cli_id_len )
4209{
4210 ((void) ctx);
4211 ((void) cookie);
4212 ((void) cookie_len);
4213 ((void) cli_id);
4214 ((void) cli_id_len);
4215
4216 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4217}
4218#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4219
Paul Bakker5121ce52009-01-03 21:22:43 +00004220/*
4221 * Initialize an SSL context
4222 */
4223int ssl_init( ssl_context *ssl )
4224{
Paul Bakker48916f92012-09-16 19:57:18 +00004225 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004226 int len = SSL_BUFFER_LEN;
4227
4228 memset( ssl, 0, sizeof( ssl_context ) );
4229
Paul Bakker62f2dee2012-09-28 07:31:51 +00004230 /*
4231 * Sane defaults
4232 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004233 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4234 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4235 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4236 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004237
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004238 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004239
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004240 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4241
Paul Bakker62f2dee2012-09-28 07:31:51 +00004242#if defined(POLARSSL_DHM_C)
4243 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4244 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4245 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4246 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4247 {
4248 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4249 return( ret );
4250 }
4251#endif
4252
4253 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004254 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004255 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004256 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004257 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004258
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004259 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 {
4261 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004262 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004263 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004264 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004265 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004266 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 }
4268
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004269 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4270 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004272 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4273 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4274
Paul Bakker606b4ba2013-08-14 16:52:14 +02004275#if defined(POLARSSL_SSL_SESSION_TICKETS)
4276 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4277#endif
4278
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004279#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004280 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004281#endif
4282
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004283#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4284 ssl->f_cookie_write = ssl_cookie_write_dummy;
4285 ssl->f_cookie_check = ssl_cookie_check_dummy;
4286#endif
4287
Paul Bakker48916f92012-09-16 19:57:18 +00004288 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4289 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004290
4291 return( 0 );
4292}
4293
4294/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004295 * Reset an initialized and used SSL context for re-use while retaining
4296 * all application-set variables, function pointers and data.
4297 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004298int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004299{
Paul Bakker48916f92012-09-16 19:57:18 +00004300 int ret;
4301
Paul Bakker7eb013f2011-10-06 12:37:39 +00004302 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004303 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4304 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4305
4306 ssl->verify_data_len = 0;
4307 memset( ssl->own_verify_data, 0, 36 );
4308 memset( ssl->peer_verify_data, 0, 36 );
4309
Paul Bakker7eb013f2011-10-06 12:37:39 +00004310 ssl->in_offt = NULL;
4311
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004312 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004313 ssl->in_msgtype = 0;
4314 ssl->in_msglen = 0;
4315 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004316#if defined(POLARSSL_SSL_PROTO_DTLS)
4317 ssl->next_record_offset = 0;
4318#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004319
4320 ssl->in_hslen = 0;
4321 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004322 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004323
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004324 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004325 ssl->out_msgtype = 0;
4326 ssl->out_msglen = 0;
4327 ssl->out_left = 0;
4328
Paul Bakker48916f92012-09-16 19:57:18 +00004329 ssl->transform_in = NULL;
4330 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004331
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004332 ssl->renego_records_seen = 0;
4333
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004334 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4335 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004336
4337#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004338 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004339 {
4340 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004341 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004342 {
4343 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4344 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4345 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004346 }
4347#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004348
Paul Bakker48916f92012-09-16 19:57:18 +00004349 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004350 {
Paul Bakker48916f92012-09-16 19:57:18 +00004351 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004352 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004353 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004354 }
Paul Bakker48916f92012-09-16 19:57:18 +00004355
Paul Bakkerc0463502013-02-14 11:19:38 +01004356 if( ssl->session )
4357 {
4358 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004359 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004360 ssl->session = NULL;
4361 }
4362
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004363#if defined(POLARSSL_SSL_ALPN)
4364 ssl->alpn_chosen = NULL;
4365#endif
4366
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004367#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004368 polarssl_free( ssl->cli_id );
4369 ssl->cli_id = NULL;
4370 ssl->cli_id_len = 0;
4371#endif
4372
Paul Bakker48916f92012-09-16 19:57:18 +00004373 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4374 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004375
4376 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004377}
4378
Paul Bakkera503a632013-08-14 13:48:06 +02004379#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004380static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4381{
4382 aes_free( &tkeys->enc );
4383 aes_free( &tkeys->dec );
4384
4385 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4386}
4387
Paul Bakker7eb013f2011-10-06 12:37:39 +00004388/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004389 * Allocate and initialize ticket keys
4390 */
4391static int ssl_ticket_keys_init( ssl_context *ssl )
4392{
4393 int ret;
4394 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004395 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004396
4397 if( ssl->ticket_keys != NULL )
4398 return( 0 );
4399
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004400 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4401 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004402 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4403
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004404 aes_init( &tkeys->enc );
4405 aes_init( &tkeys->dec );
4406
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004407 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004408 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004409 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004410 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004411 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004412 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004413
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004414 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4415 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4416 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4417 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004418 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004419 polarssl_free( tkeys );
4420 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004421 }
4422
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004423 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004424 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004425 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004426 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004427 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004428 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004429
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004430 ssl->ticket_keys = tkeys;
4431
4432 return( 0 );
4433}
Paul Bakkera503a632013-08-14 13:48:06 +02004434#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004435
4436/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004437 * SSL set accessors
4438 */
4439void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4440{
4441 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004442
Paul Bakker606b4ba2013-08-14 16:52:14 +02004443#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004444 if( endpoint == SSL_IS_CLIENT )
4445 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004447}
4448
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004449int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004450{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004451#if defined(POLARSSL_SSL_PROTO_DTLS)
4452 if( transport == SSL_TRANSPORT_DATAGRAM )
4453 {
4454 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004455
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004456 ssl->out_hdr = ssl->out_buf;
4457 ssl->out_ctr = ssl->out_buf + 3;
4458 ssl->out_len = ssl->out_buf + 11;
4459 ssl->out_iv = ssl->out_buf + 13;
4460 ssl->out_msg = ssl->out_buf + 13;
4461
4462 ssl->in_hdr = ssl->in_buf;
4463 ssl->in_ctr = ssl->in_buf + 3;
4464 ssl->in_len = ssl->in_buf + 11;
4465 ssl->in_iv = ssl->in_buf + 13;
4466 ssl->in_msg = ssl->in_buf + 13;
4467
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004468 /* DTLS starts with TLS1.1 */
4469 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4470 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004471
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004472 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4473 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4474
4475 return( 0 );
4476 }
4477#endif
4478
4479 if( transport == SSL_TRANSPORT_STREAM )
4480 {
4481 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004482
4483 ssl->out_ctr = ssl->out_buf;
4484 ssl->out_hdr = ssl->out_buf + 8;
4485 ssl->out_len = ssl->out_buf + 11;
4486 ssl->out_iv = ssl->out_buf + 13;
4487 ssl->out_msg = ssl->out_buf + 13;
4488
4489 ssl->in_ctr = ssl->in_buf;
4490 ssl->in_hdr = ssl->in_buf + 8;
4491 ssl->in_len = ssl->in_buf + 11;
4492 ssl->in_iv = ssl->in_buf + 13;
4493 ssl->in_msg = ssl->in_buf + 13;
4494
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004495 return( 0 );
4496 }
4497
4498 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004499}
4500
Paul Bakker5121ce52009-01-03 21:22:43 +00004501void ssl_set_authmode( ssl_context *ssl, int authmode )
4502{
4503 ssl->authmode = authmode;
4504}
4505
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004506#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004507void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004508 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004509 void *p_vrfy )
4510{
4511 ssl->f_vrfy = f_vrfy;
4512 ssl->p_vrfy = p_vrfy;
4513}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004514#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004515
Paul Bakker5121ce52009-01-03 21:22:43 +00004516void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004517 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004518 void *p_rng )
4519{
4520 ssl->f_rng = f_rng;
4521 ssl->p_rng = p_rng;
4522}
4523
4524void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004525 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004526 void *p_dbg )
4527{
4528 ssl->f_dbg = f_dbg;
4529 ssl->p_dbg = p_dbg;
4530}
4531
4532void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004533 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004534 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004535{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004536 if( p_recv != p_send )
4537 {
4538 ssl->f_recv = NULL;
4539 ssl->f_send = NULL;
4540 ssl->p_bio = NULL;
4541 return;
4542 }
4543
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 ssl->f_recv = f_recv;
4545 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004546 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004547}
4548
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004549void ssl_set_bio_timeout( ssl_context *ssl,
4550 void *p_bio,
4551 int (*f_send)(void *, const unsigned char *, size_t),
4552 int (*f_recv)(void *, unsigned char *, size_t),
4553 int (*f_recv_timeout)(void *, unsigned char *, size_t, unsigned char),
4554 unsigned char timeout )
4555{
4556 ssl->p_bio = p_bio;
4557 ssl->f_send = f_send;
4558 ssl->f_recv = f_recv;
4559 ssl->f_recv_timeout = f_recv_timeout;
4560 ssl->timeout = timeout;
4561}
4562
Paul Bakker0a597072012-09-25 21:55:46 +00004563void ssl_set_session_cache( ssl_context *ssl,
4564 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4565 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004566{
Paul Bakker0a597072012-09-25 21:55:46 +00004567 ssl->f_get_cache = f_get_cache;
4568 ssl->p_get_cache = p_get_cache;
4569 ssl->f_set_cache = f_set_cache;
4570 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004571}
4572
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004573int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004574{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004575 int ret;
4576
4577 if( ssl == NULL ||
4578 session == NULL ||
4579 ssl->session_negotiate == NULL ||
4580 ssl->endpoint != SSL_IS_CLIENT )
4581 {
4582 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4583 }
4584
4585 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4586 return( ret );
4587
Paul Bakker0a597072012-09-25 21:55:46 +00004588 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004589
4590 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004591}
4592
Paul Bakkerb68cad62012-08-23 08:34:18 +00004593void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004594{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004595 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4596 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4597 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4598 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4599}
4600
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004601void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4602 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004603 int major, int minor )
4604{
4605 if( major != SSL_MAJOR_VERSION_3 )
4606 return;
4607
4608 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4609 return;
4610
4611 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004612}
4613
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004614#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004615/* Add a new (empty) key_cert entry an return a pointer to it */
4616static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4617{
4618 ssl_key_cert *key_cert, *last;
4619
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004620 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
4621 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004622 return( NULL );
4623
4624 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4625
4626 /* Append the new key_cert to the (possibly empty) current list */
4627 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004628 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004629 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004630 if( ssl->handshake != NULL )
4631 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004632 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004633 else
4634 {
4635 last = ssl->key_cert;
4636 while( last->next != NULL )
4637 last = last->next;
4638 last->next = key_cert;
4639 }
4640
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004641 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004642}
4643
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004644void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004645 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004646{
4647 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004648 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 ssl->peer_cn = peer_cn;
4650}
4651
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004652int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004653 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004654{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004655 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4656
4657 if( key_cert == NULL )
4658 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4659
4660 key_cert->cert = own_cert;
4661 key_cert->key = pk_key;
4662
4663 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004664}
4665
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004666#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004667int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004668 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004669{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004670 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004671 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004672
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004673 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004674 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4675
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004676 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4677 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004678 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004679
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004680 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004681
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004682 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004683 if( ret != 0 )
4684 return( ret );
4685
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004686 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004687 return( ret );
4688
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004689 key_cert->cert = own_cert;
4690 key_cert->key_own_alloc = 1;
4691
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004692 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004693}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004694#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004695
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004696int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004697 void *rsa_key,
4698 rsa_decrypt_func rsa_decrypt,
4699 rsa_sign_func rsa_sign,
4700 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004701{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004702 int ret;
4703 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004704
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004705 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004706 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4707
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004708 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4709 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004710 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004711
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004712 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004713
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004714 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4715 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4716 return( ret );
4717
4718 key_cert->cert = own_cert;
4719 key_cert->key_own_alloc = 1;
4720
4721 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004722}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004723#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004724
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004725#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004726int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4727 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004728{
Paul Bakker6db455e2013-09-18 17:29:31 +02004729 if( psk == NULL || psk_identity == NULL )
4730 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4731
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004732 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004733 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4734
Paul Bakker6db455e2013-09-18 17:29:31 +02004735 if( ssl->psk != NULL )
4736 {
4737 polarssl_free( ssl->psk );
4738 polarssl_free( ssl->psk_identity );
4739 }
4740
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004741 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004742 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004743
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004744 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004745 ssl->psk_identity = (unsigned char *)
4746 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004747
4748 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4749 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4750
4751 memcpy( ssl->psk, psk, ssl->psk_len );
4752 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004753
4754 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004755}
4756
4757void ssl_set_psk_cb( ssl_context *ssl,
4758 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4759 size_t),
4760 void *p_psk )
4761{
4762 ssl->f_psk = f_psk;
4763 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004764}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004765#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004766
Paul Bakker48916f92012-09-16 19:57:18 +00004767#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004768int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004769{
4770 int ret;
4771
Paul Bakker48916f92012-09-16 19:57:18 +00004772 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004773 {
4774 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4775 return( ret );
4776 }
4777
Paul Bakker48916f92012-09-16 19:57:18 +00004778 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004779 {
4780 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4781 return( ret );
4782 }
4783
4784 return( 0 );
4785}
4786
Paul Bakker1b57b062011-01-06 15:48:19 +00004787int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4788{
4789 int ret;
4790
Paul Bakker66d5d072014-06-17 16:39:18 +02004791 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004792 {
4793 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4794 return( ret );
4795 }
4796
Paul Bakker66d5d072014-06-17 16:39:18 +02004797 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004798 {
4799 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4800 return( ret );
4801 }
4802
4803 return( 0 );
4804}
Paul Bakker48916f92012-09-16 19:57:18 +00004805#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004806
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004807#if defined(POLARSSL_SSL_SET_CURVES)
4808/*
4809 * Set the allowed elliptic curves
4810 */
4811void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4812{
4813 ssl->curve_list = curve_list;
4814}
4815#endif
4816
Paul Bakker0be444a2013-08-27 21:55:01 +02004817#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004818int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004819{
4820 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004821 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004822
4823 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004824
4825 if( ssl->hostname_len + 1 == 0 )
4826 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4827
Paul Bakker6e339b52013-07-03 13:37:05 +02004828 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004829
Paul Bakkerb15b8512012-01-13 13:44:06 +00004830 if( ssl->hostname == NULL )
4831 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4832
Paul Bakker3c2122f2013-06-24 19:03:14 +02004833 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004835
Paul Bakker40ea7de2009-05-03 10:18:48 +00004836 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004837
4838 return( 0 );
4839}
4840
Paul Bakker5701cdc2012-09-27 21:49:42 +00004841void ssl_set_sni( ssl_context *ssl,
4842 int (*f_sni)(void *, ssl_context *,
4843 const unsigned char *, size_t),
4844 void *p_sni )
4845{
4846 ssl->f_sni = f_sni;
4847 ssl->p_sni = p_sni;
4848}
Paul Bakker0be444a2013-08-27 21:55:01 +02004849#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004850
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004851#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004852int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004853{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004854 size_t cur_len, tot_len;
4855 const char **p;
4856
4857 /*
4858 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4859 * truncated". Check lengths now rather than later.
4860 */
4861 tot_len = 0;
4862 for( p = protos; *p != NULL; p++ )
4863 {
4864 cur_len = strlen( *p );
4865 tot_len += cur_len;
4866
4867 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4868 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4869 }
4870
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004871 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004872
4873 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004874}
4875
4876const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4877{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004878 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004879}
4880#endif /* POLARSSL_SSL_ALPN */
4881
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004882static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00004883{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004884 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
4885 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
4886 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4887 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004888 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004889 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004890 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004891
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004892 return( 0 );
4893}
4894
4895int ssl_set_max_version( ssl_context *ssl, int major, int minor )
4896{
4897 if( ssl_check_version( ssl, major, minor ) != 0 )
4898 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4899
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004900 ssl->max_major_ver = major;
4901 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004902
4903 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00004904}
4905
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004906int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00004907{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004908 if( ssl_check_version( ssl, major, minor ) != 0 )
4909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004910
4911 ssl->min_major_ver = major;
4912 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004913
4914 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00004915}
4916
Paul Bakker05decb22013-08-15 13:33:48 +02004917#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004918int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4919{
Paul Bakker77e257e2013-12-16 15:29:52 +01004920 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004921 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004922 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004923 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004924 }
4925
4926 ssl->mfl_code = mfl_code;
4927
4928 return( 0 );
4929}
Paul Bakker05decb22013-08-15 13:33:48 +02004930#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004931
Paul Bakker1f2bc622013-08-15 13:45:55 +02004932#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004933int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004934{
4935 if( ssl->endpoint != SSL_IS_CLIENT )
4936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4937
Paul Bakker8c1ede62013-07-19 14:14:37 +02004938 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004939
4940 return( 0 );
4941}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004942#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004943
Paul Bakker48916f92012-09-16 19:57:18 +00004944void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4945{
4946 ssl->disable_renegotiation = renegotiation;
4947}
4948
4949void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4950{
4951 ssl->allow_legacy_renegotiation = allow_legacy;
4952}
4953
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004954void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4955{
4956 ssl->renego_max_records = max_records;
4957}
4958
Paul Bakkera503a632013-08-14 13:48:06 +02004959#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004960int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4961{
4962 ssl->session_tickets = use_tickets;
4963
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004964 if( ssl->endpoint == SSL_IS_CLIENT )
4965 return( 0 );
4966
4967 if( ssl->f_rng == NULL )
4968 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4969
4970 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004971}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004972
4973void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4974{
4975 ssl->ticket_lifetime = lifetime;
4976}
Paul Bakkera503a632013-08-14 13:48:06 +02004977#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004978
Paul Bakker5121ce52009-01-03 21:22:43 +00004979/*
4980 * SSL get accessors
4981 */
Paul Bakker23986e52011-04-24 08:57:21 +00004982size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004983{
4984 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4985}
4986
Paul Bakkerff60ee62010-03-16 21:09:09 +00004987int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004988{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004989 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990}
4991
Paul Bakkere3166ce2011-01-27 17:40:50 +00004992const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004993{
Paul Bakker926c8e42013-03-06 10:23:34 +01004994 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004995 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004996
Paul Bakkere3166ce2011-01-27 17:40:50 +00004997 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004998}
4999
Paul Bakker43ca69c2011-01-15 17:35:19 +00005000const char *ssl_get_version( const ssl_context *ssl )
5001{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005002#if defined(POLARSSL_SSL_PROTO_DTLS)
5003 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5004 {
5005 switch( ssl->minor_ver )
5006 {
5007 case SSL_MINOR_VERSION_2:
5008 return( "DTLSv1.0" );
5009
5010 case SSL_MINOR_VERSION_3:
5011 return( "DTLSv1.2" );
5012
5013 default:
5014 return( "unknown (DTLS)" );
5015 }
5016 }
5017#endif
5018
Paul Bakker43ca69c2011-01-15 17:35:19 +00005019 switch( ssl->minor_ver )
5020 {
5021 case SSL_MINOR_VERSION_0:
5022 return( "SSLv3.0" );
5023
5024 case SSL_MINOR_VERSION_1:
5025 return( "TLSv1.0" );
5026
5027 case SSL_MINOR_VERSION_2:
5028 return( "TLSv1.1" );
5029
Paul Bakker1ef83d62012-04-11 12:09:53 +00005030 case SSL_MINOR_VERSION_3:
5031 return( "TLSv1.2" );
5032
Paul Bakker43ca69c2011-01-15 17:35:19 +00005033 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005034 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005035 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005036}
5037
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005038#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005039const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005040{
5041 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005042 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005043
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005044 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005045}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005046#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005047
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005048int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5049{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005050 if( ssl == NULL ||
5051 dst == NULL ||
5052 ssl->session == NULL ||
5053 ssl->endpoint != SSL_IS_CLIENT )
5054 {
5055 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5056 }
5057
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005058 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005059}
5060
Paul Bakker5121ce52009-01-03 21:22:43 +00005061/*
Paul Bakker1961b702013-01-25 14:49:24 +01005062 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005063 */
Paul Bakker1961b702013-01-25 14:49:24 +01005064int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005065{
Paul Bakker40e46942009-01-03 21:51:57 +00005066 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005067
Paul Bakker40e46942009-01-03 21:51:57 +00005068#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005069 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005070 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005071#endif
5072
Paul Bakker40e46942009-01-03 21:51:57 +00005073#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005074 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005075 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005076#endif
5077
Paul Bakker1961b702013-01-25 14:49:24 +01005078 return( ret );
5079}
5080
5081/*
5082 * Perform the SSL handshake
5083 */
5084int ssl_handshake( ssl_context *ssl )
5085{
5086 int ret = 0;
5087
5088 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5089
5090 while( ssl->state != SSL_HANDSHAKE_OVER )
5091 {
5092 ret = ssl_handshake_step( ssl );
5093
5094 if( ret != 0 )
5095 break;
5096 }
5097
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5099
5100 return( ret );
5101}
5102
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005103#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005104/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005105 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005106 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005107static int ssl_write_hello_request( ssl_context *ssl )
5108{
5109 int ret;
5110
5111 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5112
5113 ssl->out_msglen = 4;
5114 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5115 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5116
5117 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5118 {
5119 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5120 return( ret );
5121 }
5122
5123 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5124
5125 return( 0 );
5126}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005127#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005128
5129/*
5130 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005131 * - any side: calling ssl_renegotiate(),
5132 * - client: receiving a HelloRequest during ssl_read(),
5133 * - server: receiving any handshake message on server during ssl_read() after
5134 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005135 * If the handshake doesn't complete due to waiting for I/O, it will continue
5136 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005137 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005138static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005139{
5140 int ret;
5141
5142 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5143
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005144 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5145 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005146
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005147 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5148 * the ServerHello will have message_seq = 1" */
5149#if defined(POLARSSL_SSL_PROTO_DTLS)
5150 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005151 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5152 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005153 if( ssl->endpoint == SSL_IS_SERVER )
5154 ssl->handshake->out_msg_seq = 1;
5155 else
5156 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005157 }
5158#endif
5159
Paul Bakker48916f92012-09-16 19:57:18 +00005160 ssl->state = SSL_HELLO_REQUEST;
5161 ssl->renegotiation = SSL_RENEGOTIATION;
5162
Paul Bakker48916f92012-09-16 19:57:18 +00005163 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5164 {
5165 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5166 return( ret );
5167 }
5168
5169 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5170
5171 return( 0 );
5172}
5173
5174/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005175 * Renegotiate current connection on client,
5176 * or request renegotiation on server
5177 */
5178int ssl_renegotiate( ssl_context *ssl )
5179{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005180 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005181
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005182#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005183 /* On server, just send the request */
5184 if( ssl->endpoint == SSL_IS_SERVER )
5185 {
5186 if( ssl->state != SSL_HANDSHAKE_OVER )
5187 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5188
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005189 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5190
5191 /* Did we already try/start sending HelloRequest? */
5192 if( ssl->out_left != 0 )
5193 return( ssl_flush_output( ssl ) );
5194
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005195 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005196 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005197#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005198
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005199#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005200 /*
5201 * On client, either start the renegotiation process or,
5202 * if already in progress, continue the handshake
5203 */
5204 if( ssl->renegotiation != SSL_RENEGOTIATION )
5205 {
5206 if( ssl->state != SSL_HANDSHAKE_OVER )
5207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5208
5209 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5210 {
5211 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5212 return( ret );
5213 }
5214 }
5215 else
5216 {
5217 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5218 {
5219 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5220 return( ret );
5221 }
5222 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005223#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005224
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005225 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005226}
5227
5228/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005229 * Receive application data decrypted from the SSL layer
5230 */
Paul Bakker23986e52011-04-24 08:57:21 +00005231int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005232{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005233 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005234 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005235
5236 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5237
5238 if( ssl->state != SSL_HANDSHAKE_OVER )
5239 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005240 ret = ssl_handshake( ssl );
5241 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5242 {
5243 record_read = 1;
5244 }
5245 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005246 {
5247 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5248 return( ret );
5249 }
5250 }
5251
5252 if( ssl->in_offt == NULL )
5253 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005254 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005255 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005256 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5257 {
5258 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5259 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005260
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005261 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5262 return( ret );
5263 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005264 }
5265
5266 if( ssl->in_msglen == 0 &&
5267 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5268 {
5269 /*
5270 * OpenSSL sends empty messages to randomize the IV
5271 */
5272 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5273 {
Paul Bakker831a7552011-05-18 13:32:51 +00005274 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5275 return( 0 );
5276
Paul Bakker5121ce52009-01-03 21:22:43 +00005277 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5278 return( ret );
5279 }
5280 }
5281
Paul Bakker48916f92012-09-16 19:57:18 +00005282 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5283 {
5284 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5285
5286 if( ssl->endpoint == SSL_IS_CLIENT &&
5287 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005288 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005289 {
5290 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005291
5292 /* With DTLS, drop the packet (probably from last handshake) */
5293#if defined(POLARSSL_SSL_PROTO_DTLS)
5294 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5295 return( POLARSSL_ERR_NET_WANT_READ );
5296#endif
5297 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5298 }
5299
5300 if( ssl->endpoint == SSL_IS_SERVER &&
5301 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5302 {
5303 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5304
5305 /* With DTLS, drop the packet (probably from last handshake) */
5306#if defined(POLARSSL_SSL_PROTO_DTLS)
5307 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5308 return( POLARSSL_ERR_NET_WANT_READ );
5309#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005310 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5311 }
5312
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005313 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5314 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005315 ssl->allow_legacy_renegotiation ==
5316 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005317 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005318 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005319
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005320#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005321 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005322 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005323 /*
5324 * SSLv3 does not have a "no_renegotiation" alert
5325 */
5326 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5327 return( ret );
5328 }
5329 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005330#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005331#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5332 defined(POLARSSL_SSL_PROTO_TLS1_2)
5333 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005334 {
5335 if( ( ret = ssl_send_alert_message( ssl,
5336 SSL_ALERT_LEVEL_WARNING,
5337 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5338 {
5339 return( ret );
5340 }
Paul Bakker48916f92012-09-16 19:57:18 +00005341 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005342 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005343#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5344 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005345 {
5346 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005347 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005348 }
Paul Bakker48916f92012-09-16 19:57:18 +00005349 }
5350 else
5351 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005352#if defined(POLARSSL_SSL_PROTO_DTLS)
5353 /* DTLS clients need to know renego is server-initiated */
5354 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5355 ssl->endpoint == SSL_IS_CLIENT )
5356 {
5357 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5358 }
5359#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005360 ret = ssl_start_renegotiation( ssl );
5361 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5362 {
5363 record_read = 1;
5364 }
5365 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005366 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005367 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005368 return( ret );
5369 }
Paul Bakker48916f92012-09-16 19:57:18 +00005370 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005371
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005372 /* If a non-handshake record was read during renego, fallthrough,
5373 * else tell the user they should call ssl_read() again */
5374 if( ! record_read )
5375 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005376 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005377 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5378 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005379 ssl->renego_records_seen++;
5380
5381 if( ssl->renego_max_records >= 0 &&
5382 ssl->renego_records_seen > ssl->renego_max_records )
5383 {
5384 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5385 "but not honored by client" ) );
5386 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5387 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005388 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005389
5390 /* Fatal and closure alerts handled by ssl_read_record() */
5391 if( ssl->in_msgtype == SSL_MSG_ALERT )
5392 {
5393 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5394 return( POLARSSL_ERR_NET_WANT_READ );
5395 }
5396
5397 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 {
5399 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005400 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005401 }
5402
5403 ssl->in_offt = ssl->in_msg;
5404 }
5405
5406 n = ( len < ssl->in_msglen )
5407 ? len : ssl->in_msglen;
5408
5409 memcpy( buf, ssl->in_offt, n );
5410 ssl->in_msglen -= n;
5411
5412 if( ssl->in_msglen == 0 )
5413 /* all bytes consumed */
5414 ssl->in_offt = NULL;
5415 else
5416 /* more data available */
5417 ssl->in_offt += n;
5418
5419 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5420
Paul Bakker23986e52011-04-24 08:57:21 +00005421 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005422}
5423
5424/*
5425 * Send application data to be encrypted by the SSL layer
5426 */
Paul Bakker23986e52011-04-24 08:57:21 +00005427int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005428{
Paul Bakker23986e52011-04-24 08:57:21 +00005429 int ret;
5430 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005431 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005432
5433 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5434
5435 if( ssl->state != SSL_HANDSHAKE_OVER )
5436 {
5437 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5438 {
5439 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5440 return( ret );
5441 }
5442 }
5443
Paul Bakker05decb22013-08-15 13:33:48 +02005444#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005445 /*
5446 * Assume mfl_code is correct since it was checked when set
5447 */
5448 max_len = mfl_code_to_length[ssl->mfl_code];
5449
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005450 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005451 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005452 */
5453 if( ssl->session_out != NULL &&
5454 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5455 {
5456 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5457 }
Paul Bakker05decb22013-08-15 13:33:48 +02005458#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005459
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005460 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005461
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 if( ssl->out_left != 0 )
5463 {
5464 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5465 {
5466 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5467 return( ret );
5468 }
5469 }
Paul Bakker887bd502011-06-08 13:10:54 +00005470 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005471 {
Paul Bakker887bd502011-06-08 13:10:54 +00005472 ssl->out_msglen = n;
5473 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5474 memcpy( ssl->out_msg, buf, n );
5475
5476 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5477 {
5478 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5479 return( ret );
5480 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 }
5482
5483 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5484
Paul Bakker23986e52011-04-24 08:57:21 +00005485 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005486}
5487
5488/*
5489 * Notify the peer that the connection is being closed
5490 */
5491int ssl_close_notify( ssl_context *ssl )
5492{
5493 int ret;
5494
5495 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5496
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005497 if( ssl->out_left != 0 )
5498 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005499
5500 if( ssl->state == SSL_HANDSHAKE_OVER )
5501 {
Paul Bakker48916f92012-09-16 19:57:18 +00005502 if( ( ret = ssl_send_alert_message( ssl,
5503 SSL_ALERT_LEVEL_WARNING,
5504 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005506 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 return( ret );
5508 }
5509 }
5510
5511 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5512
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005513 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005514}
5515
Paul Bakker48916f92012-09-16 19:57:18 +00005516void ssl_transform_free( ssl_transform *transform )
5517{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005518 if( transform == NULL )
5519 return;
5520
Paul Bakker48916f92012-09-16 19:57:18 +00005521#if defined(POLARSSL_ZLIB_SUPPORT)
5522 deflateEnd( &transform->ctx_deflate );
5523 inflateEnd( &transform->ctx_inflate );
5524#endif
5525
Paul Bakker84bbeb52014-07-01 14:53:22 +02005526 cipher_free( &transform->cipher_ctx_enc );
5527 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005528
Paul Bakker84bbeb52014-07-01 14:53:22 +02005529 md_free( &transform->md_ctx_enc );
5530 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005531
Paul Bakker34617722014-06-13 17:20:13 +02005532 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005533}
5534
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005535#if defined(POLARSSL_X509_CRT_PARSE_C)
5536static void ssl_key_cert_free( ssl_key_cert *key_cert )
5537{
5538 ssl_key_cert *cur = key_cert, *next;
5539
5540 while( cur != NULL )
5541 {
5542 next = cur->next;
5543
5544 if( cur->key_own_alloc )
5545 {
5546 pk_free( cur->key );
5547 polarssl_free( cur->key );
5548 }
5549 polarssl_free( cur );
5550
5551 cur = next;
5552 }
5553}
5554#endif /* POLARSSL_X509_CRT_PARSE_C */
5555
Paul Bakker48916f92012-09-16 19:57:18 +00005556void ssl_handshake_free( ssl_handshake_params *handshake )
5557{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005558 if( handshake == NULL )
5559 return;
5560
Paul Bakker48916f92012-09-16 19:57:18 +00005561#if defined(POLARSSL_DHM_C)
5562 dhm_free( &handshake->dhm_ctx );
5563#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005564#if defined(POLARSSL_ECDH_C)
5565 ecdh_free( &handshake->ecdh_ctx );
5566#endif
5567
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005568#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005569 /* explicit void pointer cast for buggy MS compiler */
5570 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005571#endif
5572
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005573#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5574 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5575 /*
5576 * Free only the linked list wrapper, not the keys themselves
5577 * since the belong to the SNI callback
5578 */
5579 if( handshake->sni_key_cert != NULL )
5580 {
5581 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5582
5583 while( cur != NULL )
5584 {
5585 next = cur->next;
5586 polarssl_free( cur );
5587 cur = next;
5588 }
5589 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005590#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005591
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005592#if defined(POLARSSL_SSL_PROTO_DTLS)
5593 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02005594 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02005595 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005596#endif
5597
Paul Bakker34617722014-06-13 17:20:13 +02005598 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005599}
5600
5601void ssl_session_free( ssl_session *session )
5602{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005603 if( session == NULL )
5604 return;
5605
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005606#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005607 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005608 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005609 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005610 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005611 }
Paul Bakkered27a042013-04-18 22:46:23 +02005612#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005613
Paul Bakkera503a632013-08-14 13:48:06 +02005614#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005615 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005616#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005617
Paul Bakker34617722014-06-13 17:20:13 +02005618 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005619}
5620
Paul Bakker5121ce52009-01-03 21:22:43 +00005621/*
5622 * Free an SSL context
5623 */
5624void ssl_free( ssl_context *ssl )
5625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005626 if( ssl == NULL )
5627 return;
5628
Paul Bakker5121ce52009-01-03 21:22:43 +00005629 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5630
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005631 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005632 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005633 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
5634 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005635 }
5636
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005637 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005638 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005639 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
5640 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005641 }
5642
Paul Bakker16770332013-10-11 09:59:44 +02005643#if defined(POLARSSL_ZLIB_SUPPORT)
5644 if( ssl->compress_buf != NULL )
5645 {
Paul Bakker34617722014-06-13 17:20:13 +02005646 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005647 polarssl_free( ssl->compress_buf );
5648 }
5649#endif
5650
Paul Bakker40e46942009-01-03 21:51:57 +00005651#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005652 mpi_free( &ssl->dhm_P );
5653 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005654#endif
5655
Paul Bakker48916f92012-09-16 19:57:18 +00005656 if( ssl->transform )
5657 {
5658 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005659 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005660 }
5661
5662 if( ssl->handshake )
5663 {
5664 ssl_handshake_free( ssl->handshake );
5665 ssl_transform_free( ssl->transform_negotiate );
5666 ssl_session_free( ssl->session_negotiate );
5667
Paul Bakker6e339b52013-07-03 13:37:05 +02005668 polarssl_free( ssl->handshake );
5669 polarssl_free( ssl->transform_negotiate );
5670 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005671 }
5672
Paul Bakkerc0463502013-02-14 11:19:38 +01005673 if( ssl->session )
5674 {
5675 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005676 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005677 }
5678
Paul Bakkera503a632013-08-14 13:48:06 +02005679#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005680 if( ssl->ticket_keys )
5681 {
5682 ssl_ticket_keys_free( ssl->ticket_keys );
5683 polarssl_free( ssl->ticket_keys );
5684 }
Paul Bakkera503a632013-08-14 13:48:06 +02005685#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005686
Paul Bakker0be444a2013-08-27 21:55:01 +02005687#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005688 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689 {
Paul Bakker34617722014-06-13 17:20:13 +02005690 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005691 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005692 ssl->hostname_len = 0;
5693 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005694#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005695
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005696#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005697 if( ssl->psk != NULL )
5698 {
Paul Bakker34617722014-06-13 17:20:13 +02005699 polarssl_zeroize( ssl->psk, ssl->psk_len );
5700 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005701 polarssl_free( ssl->psk );
5702 polarssl_free( ssl->psk_identity );
5703 ssl->psk_len = 0;
5704 ssl->psk_identity_len = 0;
5705 }
5706#endif
5707
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005708#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005709 ssl_key_cert_free( ssl->key_cert );
5710#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005711
Paul Bakker05ef8352012-05-08 09:17:57 +00005712#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5713 if( ssl_hw_record_finish != NULL )
5714 {
5715 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5716 ssl_hw_record_finish( ssl );
5717 }
5718#endif
5719
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005720#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005721 polarssl_free( ssl->cli_id );
5722#endif
5723
Paul Bakker5121ce52009-01-03 21:22:43 +00005724 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005725
Paul Bakker86f04f42013-02-14 11:20:09 +01005726 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005727 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005728}
5729
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005730#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005731/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005732 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005733 */
5734unsigned char ssl_sig_from_pk( pk_context *pk )
5735{
5736#if defined(POLARSSL_RSA_C)
5737 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5738 return( SSL_SIG_RSA );
5739#endif
5740#if defined(POLARSSL_ECDSA_C)
5741 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5742 return( SSL_SIG_ECDSA );
5743#endif
5744 return( SSL_SIG_ANON );
5745}
5746
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005747pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5748{
5749 switch( sig )
5750 {
5751#if defined(POLARSSL_RSA_C)
5752 case SSL_SIG_RSA:
5753 return( POLARSSL_PK_RSA );
5754#endif
5755#if defined(POLARSSL_ECDSA_C)
5756 case SSL_SIG_ECDSA:
5757 return( POLARSSL_PK_ECDSA );
5758#endif
5759 default:
5760 return( POLARSSL_PK_NONE );
5761 }
5762}
Paul Bakker9af723c2014-05-01 13:03:14 +02005763#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005764
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005765/*
5766 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5767 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005768md_type_t ssl_md_alg_from_hash( unsigned char hash )
5769{
5770 switch( hash )
5771 {
5772#if defined(POLARSSL_MD5_C)
5773 case SSL_HASH_MD5:
5774 return( POLARSSL_MD_MD5 );
5775#endif
5776#if defined(POLARSSL_SHA1_C)
5777 case SSL_HASH_SHA1:
5778 return( POLARSSL_MD_SHA1 );
5779#endif
5780#if defined(POLARSSL_SHA256_C)
5781 case SSL_HASH_SHA224:
5782 return( POLARSSL_MD_SHA224 );
5783 case SSL_HASH_SHA256:
5784 return( POLARSSL_MD_SHA256 );
5785#endif
5786#if defined(POLARSSL_SHA512_C)
5787 case SSL_HASH_SHA384:
5788 return( POLARSSL_MD_SHA384 );
5789 case SSL_HASH_SHA512:
5790 return( POLARSSL_MD_SHA512 );
5791#endif
5792 default:
5793 return( POLARSSL_MD_NONE );
5794 }
5795}
5796
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005797#if defined(POLARSSL_SSL_SET_CURVES)
5798/*
5799 * Check is a curve proposed by the peer is in our list.
5800 * Return 1 if we're willing to use it, 0 otherwise.
5801 */
5802int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5803{
5804 const ecp_group_id *gid;
5805
5806 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5807 if( *gid == grp_id )
5808 return( 1 );
5809
5810 return( 0 );
5811}
Paul Bakker9af723c2014-05-01 13:03:14 +02005812#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005813
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005814#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005815int ssl_check_cert_usage( const x509_crt *cert,
5816 const ssl_ciphersuite_t *ciphersuite,
5817 int cert_endpoint )
5818{
5819#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5820 int usage = 0;
5821#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005822#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5823 const char *ext_oid;
5824 size_t ext_len;
5825#endif
5826
5827#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5828 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5829 ((void) cert);
5830 ((void) cert_endpoint);
5831#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005832
5833#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5834 if( cert_endpoint == SSL_IS_SERVER )
5835 {
5836 /* Server part of the key exchange */
5837 switch( ciphersuite->key_exchange )
5838 {
5839 case POLARSSL_KEY_EXCHANGE_RSA:
5840 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5841 usage = KU_KEY_ENCIPHERMENT;
5842 break;
5843
5844 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5845 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5846 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5847 usage = KU_DIGITAL_SIGNATURE;
5848 break;
5849
5850 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5851 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5852 usage = KU_KEY_AGREEMENT;
5853 break;
5854
5855 /* Don't use default: we want warnings when adding new values */
5856 case POLARSSL_KEY_EXCHANGE_NONE:
5857 case POLARSSL_KEY_EXCHANGE_PSK:
5858 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5859 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5860 usage = 0;
5861 }
5862 }
5863 else
5864 {
5865 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5866 usage = KU_DIGITAL_SIGNATURE;
5867 }
5868
5869 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5870 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005871#else
5872 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005873#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5874
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005875#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5876 if( cert_endpoint == SSL_IS_SERVER )
5877 {
5878 ext_oid = OID_SERVER_AUTH;
5879 ext_len = OID_SIZE( OID_SERVER_AUTH );
5880 }
5881 else
5882 {
5883 ext_oid = OID_CLIENT_AUTH;
5884 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5885 }
5886
5887 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5888 return( -1 );
5889#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5890
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005891 return( 0 );
5892}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005893#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005894
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005895/*
5896 * Convert version numbers to/from wire format
5897 * and, for DTLS, to/from TLS equivalent.
5898 *
5899 * For TLS this is the identity.
5900 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
5901 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5902 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5903 */
5904void ssl_write_version( int major, int minor, int transport,
5905 unsigned char ver[2] )
5906{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005907#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005908 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005909 {
5910 if( minor == SSL_MINOR_VERSION_2 )
5911 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5912
5913 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5914 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5915 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005916 else
5917#else
5918 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005919#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005920 {
5921 ver[0] = (unsigned char) major;
5922 ver[1] = (unsigned char) minor;
5923 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005924}
5925
5926void ssl_read_version( int *major, int *minor, int transport,
5927 const unsigned char ver[2] )
5928{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005929#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005930 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005931 {
5932 *major = 255 - ver[0] + 2;
5933 *minor = 255 - ver[1] + 1;
5934
5935 if( *minor == SSL_MINOR_VERSION_1 )
5936 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5937 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005938 else
5939#else
5940 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005941#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005942 {
5943 *major = ver[0];
5944 *minor = ver[1];
5945 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005946}
5947
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005948#endif /* POLARSSL_SSL_TLS_C */