blob: bd830e5eef0bc4c8580f2e54dc74ca578b1893f5 [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é-Gonnardea22ce52014-09-24 09:46:10 +02001708#if defined(POLARSSL_SSL_PROTO_DTLS)
1709 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001710 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001711 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001712 }
1713 else
1714#endif
1715 {
1716 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1717 if( ++ssl->in_ctr[i - 1] != 0 )
1718 break;
1719
1720 /* The loop goes to its end iff the counter is wrapping */
1721 if( i == ssl_ep_len( ssl ) )
1722 {
1723 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1724 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1725 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001726 }
1727
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1729
1730 return( 0 );
1731}
1732
Paul Bakker2770fbd2012-07-03 13:30:23 +00001733#if defined(POLARSSL_ZLIB_SUPPORT)
1734/*
1735 * Compression/decompression functions
1736 */
1737static int ssl_compress_buf( ssl_context *ssl )
1738{
1739 int ret;
1740 unsigned char *msg_post = ssl->out_msg;
1741 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001742 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743
1744 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1745
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001746 if( len_pre == 0 )
1747 return( 0 );
1748
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749 memcpy( msg_pre, ssl->out_msg, len_pre );
1750
1751 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1752 ssl->out_msglen ) );
1753
1754 SSL_DEBUG_BUF( 4, "before compression: output payload",
1755 ssl->out_msg, ssl->out_msglen );
1756
Paul Bakker48916f92012-09-16 19:57:18 +00001757 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1758 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1759 ssl->transform_out->ctx_deflate.next_out = msg_post;
1760 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761
Paul Bakker48916f92012-09-16 19:57:18 +00001762 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001763 if( ret != Z_OK )
1764 {
1765 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1766 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1767 }
1768
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001769 ssl->out_msglen = SSL_BUFFER_LEN -
1770 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771
Paul Bakker2770fbd2012-07-03 13:30:23 +00001772 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1773 ssl->out_msglen ) );
1774
1775 SSL_DEBUG_BUF( 4, "after compression: output payload",
1776 ssl->out_msg, ssl->out_msglen );
1777
1778 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1779
1780 return( 0 );
1781}
1782
1783static int ssl_decompress_buf( ssl_context *ssl )
1784{
1785 int ret;
1786 unsigned char *msg_post = ssl->in_msg;
1787 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001788 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789
1790 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1791
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001792 if( len_pre == 0 )
1793 return( 0 );
1794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 memcpy( msg_pre, ssl->in_msg, len_pre );
1796
1797 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1798 ssl->in_msglen ) );
1799
1800 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1801 ssl->in_msg, ssl->in_msglen );
1802
Paul Bakker48916f92012-09-16 19:57:18 +00001803 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1804 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1805 ssl->transform_in->ctx_inflate.next_out = msg_post;
1806 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807
Paul Bakker48916f92012-09-16 19:57:18 +00001808 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809 if( ret != Z_OK )
1810 {
1811 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1812 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1813 }
1814
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001815 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1816 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001817
Paul Bakker2770fbd2012-07-03 13:30:23 +00001818 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1819 ssl->in_msglen ) );
1820
1821 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1822 ssl->in_msg, ssl->in_msglen );
1823
1824 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1825
1826 return( 0 );
1827}
1828#endif /* POLARSSL_ZLIB_SUPPORT */
1829
Paul Bakker5121ce52009-01-03 21:22:43 +00001830/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001831 * Fill the input message buffer by appending data to it.
1832 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001833 *
1834 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1835 * available (from this read and/or a previous one). Otherwise, an error code
1836 * is returned (possibly EOF or WANT_READ).
1837 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001838 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1839 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1840 * since we always read a whole datagram at once.
1841 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001842 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001843 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 */
Paul Bakker23986e52011-04-24 08:57:21 +00001845int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001846{
Paul Bakker23986e52011-04-24 08:57:21 +00001847 int ret;
1848 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
1850 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1851
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001852 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1853 {
1854 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1855 "or ssl_set_bio_timeout()" ) );
1856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1857 }
1858
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001859 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001860 {
1861 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1862 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1863 }
1864
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001865#if defined(POLARSSL_SSL_PROTO_DTLS)
1866 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001868 /*
1869 * The point is, we need to always read a full datagram at once, so we
1870 * sometimes read more then requested, and handle the additional data.
1871 * It could be the rest of the current record (while fetching the
1872 * header) and/or some other records in the same datagram.
1873 */
1874
1875 /*
1876 * Move to the next record in the already read datagram if applicable
1877 */
1878 if( ssl->next_record_offset != 0 )
1879 {
1880 if( ssl->in_left < ssl->next_record_offset )
1881 {
1882 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1883 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1884 }
1885
1886 ssl->in_left -= ssl->next_record_offset;
1887
1888 if( ssl->in_left != 0 )
1889 {
1890 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1891 ssl->next_record_offset ) );
1892 memmove( ssl->in_hdr,
1893 ssl->in_hdr + ssl->next_record_offset,
1894 ssl->in_left );
1895 }
1896
1897 ssl->next_record_offset = 0;
1898 }
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1901 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001902
1903 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001904 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001905 */
1906 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001907 {
1908 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001909 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001910 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001911
1912 /*
1913 * A record can't be split accross datagrams. If we need to read but
1914 * are not at the beginning of a new record, the caller did something
1915 * wrong.
1916 */
1917 if( ssl->in_left != 0 )
1918 {
1919 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1920 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1921 }
1922
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001923 // TODO-DTLS: for now, use constant timeout = 1 sec/datagram
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001924 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001925 if( ssl->f_recv_timeout != NULL &&
1926 ssl->handshake != NULL ) /* No resend outside handshake */
1927 {
1928 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, 1 );
1929 }
1930 else
1931 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001932
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1934
Paul Bakker831a7552011-05-18 13:32:51 +00001935 if( ret == 0 )
1936 return( POLARSSL_ERR_SSL_CONN_EOF );
1937
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001938 if( ret == POLARSSL_ERR_NET_TIMEOUT )
1939 {
1940 SSL_DEBUG_MSG( 2, ( "recv timeout" ) );
1941
1942 if( ( ret = ssl_resend( ssl ) ) != 0 )
1943 {
1944 SSL_DEBUG_RET( 1, "ssl_resend", ret );
1945 return( ret );
1946 }
1947
1948 return( POLARSSL_ERR_NET_WANT_READ );
1949 }
1950
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 if( ret < 0 )
1952 return( ret );
1953
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001954 ssl->in_left = ret;
1955 }
1956 else
1957#endif
1958 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001959 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1960 ssl->in_left, nb_want ) );
1961
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001962 while( ssl->in_left < nb_want )
1963 {
1964 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001965 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001966
1967 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1968 ssl->in_left, nb_want ) );
1969 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1970
1971 if( ret == 0 )
1972 return( POLARSSL_ERR_SSL_CONN_EOF );
1973
1974 if( ret < 0 )
1975 return( ret );
1976
1977 ssl->in_left += ret;
1978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980
1981 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1982
1983 return( 0 );
1984}
1985
1986/*
1987 * Flush any data not yet written
1988 */
1989int ssl_flush_output( ssl_context *ssl )
1990{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001991 int ret;
1992 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001993
1994 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1995
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001996 if( ssl->f_send == NULL )
1997 {
1998 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1999 "or ssl_set_bio_timeout()" ) );
2000 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2001 }
2002
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002003 /* Avoid incrementing counter if data is flushed */
2004 if( ssl->out_left == 0 )
2005 {
2006 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2007 return( 0 );
2008 }
2009
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 while( ssl->out_left > 0 )
2011 {
2012 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002013 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002015 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2016 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002017 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002018
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2020
2021 if( ret <= 0 )
2022 return( ret );
2023
2024 ssl->out_left -= ret;
2025 }
2026
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002027 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002028 if( ++ssl->out_ctr[i - 1] != 0 )
2029 break;
2030
2031 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002032 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002033 {
2034 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2035 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2036 }
2037
Paul Bakker5121ce52009-01-03 21:22:43 +00002038 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2039
2040 return( 0 );
2041}
2042
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002043/*
2044 * Functions to handle the DTLS retransmission state machine
2045 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002046#if defined(POLARSSL_SSL_PROTO_DTLS)
2047/*
2048 * Append current handshake message to current outgoing flight
2049 */
2050static int ssl_flight_append( ssl_context *ssl )
2051{
2052 ssl_flight_item *msg;
2053
2054 /* Allocate space for current message */
2055 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2056 {
2057 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2058 sizeof( ssl_flight_item ) ) );
2059 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2060 }
2061
2062 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2063 {
2064 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2065 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2066 }
2067
2068 /* Copy current handshake message with headers */
2069 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2070 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002071 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002072 msg->next = NULL;
2073
2074 /* Append to the current flight */
2075 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002076 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002077 else
2078 {
2079 ssl_flight_item *cur = ssl->handshake->flight;
2080 while( cur->next != NULL )
2081 cur = cur->next;
2082 cur->next = msg;
2083 }
2084
2085 return( 0 );
2086}
2087
2088/*
2089 * Free the current flight of handshake messages
2090 */
2091static void ssl_flight_free( ssl_flight_item *flight )
2092{
2093 ssl_flight_item *cur = flight;
2094 ssl_flight_item *next;
2095
2096 while( cur != NULL )
2097 {
2098 next = cur->next;
2099
2100 polarssl_free( cur->p );
2101 polarssl_free( cur );
2102
2103 cur = next;
2104 }
2105}
2106
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002107#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2108static void ssl_dtls_replay_reset( ssl_context *ssl );
2109#endif
2110
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002111/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002112 * Swap transform_out and out_ctr with the alternative ones
2113 */
2114static void ssl_swap_epochs( ssl_context *ssl )
2115{
2116 ssl_transform *tmp_transform;
2117 unsigned char tmp_out_ctr[8];
2118
2119 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2120 {
2121 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2122 return;
2123 }
2124
2125 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2126
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002127 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002128 tmp_transform = ssl->transform_out;
2129 ssl->transform_out = ssl->handshake->alt_transform_out;
2130 ssl->handshake->alt_transform_out = tmp_transform;
2131
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002132 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002133 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2134 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2135 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002136
2137 /* Adjust to the newly activated transform */
2138 if( ssl->transform_out != NULL &&
2139 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2140 {
2141 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2142 ssl->transform_out->fixed_ivlen;
2143 }
2144 else
2145 ssl->out_msg = ssl->out_iv;
2146
2147#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2148 if( ssl_hw_record_activate != NULL )
2149 {
2150 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2151 {
2152 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2153 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2154 }
2155 }
2156#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002157}
2158
2159/*
2160 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002161 *
2162 * Need to remember the current message in case flush_output returns
2163 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002164 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002165 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002166int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002167{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002168 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002169
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002170 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2171 {
2172 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2173
2174 ssl->handshake->cur_msg = ssl->handshake->flight;
2175 ssl_swap_epochs( ssl );
2176
2177 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2178 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002179
2180 while( ssl->handshake->cur_msg != NULL )
2181 {
2182 int ret;
2183 ssl_flight_item *cur = ssl->handshake->cur_msg;
2184
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002185 /* Swap epochs before sending Finished: we can't do it after
2186 * sending ChangeCipherSpec, in case write returns WANT_READ.
2187 * Must be done before copying, may change out_msg pointer */
2188 if( cur->type == SSL_MSG_HANDSHAKE &&
2189 cur->p[0] == SSL_HS_FINISHED )
2190 {
2191 ssl_swap_epochs( ssl );
2192 }
2193
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002194 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002195 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002196 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002197
2198 ssl->handshake->cur_msg = cur->next;
2199
2200 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2201
2202 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2203 {
2204 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2205 return( ret );
2206 }
2207 }
2208
2209 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2210
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002211 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002212
2213 return( 0 );
2214}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002215
2216/*
2217 * To be called when the last message of an incoming flight is received.
2218 */
2219void ssl_recv_flight_completed( ssl_context *ssl )
2220{
2221 /* We won't need to resend that one any more */
2222 ssl_flight_free( ssl->handshake->flight );
2223 ssl->handshake->flight = NULL;
2224 ssl->handshake->cur_msg = NULL;
2225
2226 /* The next incoming flight will start with this msg_seq */
2227 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2228
2229 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2230 ssl->in_msg[0] == SSL_HS_FINISHED )
2231 {
2232 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2233 }
2234 else
2235 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2236}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002237#endif /* POLARSSL_SSL_PROTO_DTLS */
2238
Paul Bakker5121ce52009-01-03 21:22:43 +00002239/*
2240 * Record layer functions
2241 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002242
2243/*
2244 * Write current record.
2245 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2246 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002247int ssl_write_record( ssl_context *ssl )
2248{
Paul Bakker05ef8352012-05-08 09:17:57 +00002249 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002250 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002251
2252 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2253
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002254#if defined(POLARSSL_SSL_PROTO_DTLS)
2255 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2256 ssl->handshake != NULL &&
2257 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2258 {
2259 ; /* Skip special handshake treatment when resending */
2260 }
2261 else
2262#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2264 {
2265 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2266 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2267 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2268
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002269 /*
2270 * DTLS has additional fields in the Handshake layer,
2271 * between the length field and the actual payload:
2272 * uint16 message_seq;
2273 * uint24 fragment_offset;
2274 * uint24 fragment_length;
2275 */
2276#if defined(POLARSSL_SSL_PROTO_DTLS)
2277 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2278 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002279 /* Make room for the additional DTLS fields */
2280 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002281 ssl->out_msglen += 8;
2282 len += 8;
2283
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002284 /* Write message_seq and update it, except for HelloRequest */
2285 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2286 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002287 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2288 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2289 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002290 }
2291 else
2292 {
2293 ssl->out_msg[4] = 0;
2294 ssl->out_msg[5] = 0;
2295 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002296
2297 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2298 memset( ssl->out_msg + 6, 0x00, 3 );
2299 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002300 }
2301#endif /* POLARSSL_SSL_PROTO_DTLS */
2302
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002303 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2304 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002305 }
2306
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002307 /* Save handshake and CCS messages for resending */
2308#if defined(POLARSSL_SSL_PROTO_DTLS)
2309 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2310 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002311 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002312 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2313 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2314 {
2315 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2316 {
2317 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2318 return( ret );
2319 }
2320 }
2321#endif
2322
Paul Bakker2770fbd2012-07-03 13:30:23 +00002323#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002324 if( ssl->transform_out != NULL &&
2325 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002326 {
2327 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2328 {
2329 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2330 return( ret );
2331 }
2332
2333 len = ssl->out_msglen;
2334 }
2335#endif /*POLARSSL_ZLIB_SUPPORT */
2336
Paul Bakker05ef8352012-05-08 09:17:57 +00002337#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002338 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002340 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002341
Paul Bakker05ef8352012-05-08 09:17:57 +00002342 ret = ssl_hw_record_write( ssl );
2343 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2344 {
2345 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002346 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002347 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002348
2349 if( ret == 0 )
2350 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002351 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002352#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002353 if( !done )
2354 {
2355 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002356 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2357 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002358
2359 ssl->out_len[0] = (unsigned char)( len >> 8 );
2360 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002361
Paul Bakker48916f92012-09-16 19:57:18 +00002362 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002363 {
2364 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2365 {
2366 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2367 return( ret );
2368 }
2369
2370 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002371 ssl->out_len[0] = (unsigned char)( len >> 8 );
2372 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002373 }
2374
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002375 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002376
2377 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2378 "version = [%d:%d], msglen = %d",
2379 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002380 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002381
2382 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002383 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 }
2385
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2387 {
2388 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2389 return( ret );
2390 }
2391
2392 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2393
2394 return( 0 );
2395}
2396
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002397#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002398/*
2399 * Mark bits in bitmask (used for DTLS HS reassembly)
2400 */
2401static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2402{
2403 unsigned int start_bits, end_bits;
2404
2405 start_bits = 8 - ( offset % 8 );
2406 if( start_bits != 8 )
2407 {
2408 size_t first_byte_idx = offset / 8;
2409
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002410 /* Special case */
2411 if( len <= start_bits )
2412 {
2413 for( ; len != 0; len-- )
2414 mask[first_byte_idx] |= 1 << ( start_bits - len );
2415
2416 /* Avoid potential issues with offset or len becoming invalid */
2417 return;
2418 }
2419
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002420 offset += start_bits; /* Now offset % 8 == 0 */
2421 len -= start_bits;
2422
2423 for( ; start_bits != 0; start_bits-- )
2424 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2425 }
2426
2427 end_bits = len % 8;
2428 if( end_bits != 0 )
2429 {
2430 size_t last_byte_idx = ( offset + len ) / 8;
2431
2432 len -= end_bits; /* Now len % 8 == 0 */
2433
2434 for( ; end_bits != 0; end_bits-- )
2435 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2436 }
2437
2438 memset( mask + offset / 8, 0xFF, len / 8 );
2439}
2440
2441/*
2442 * Check that bitmask is full
2443 */
2444static int ssl_bitmask_check( unsigned char *mask, size_t len )
2445{
2446 size_t i;
2447
2448 for( i = 0; i < len / 8; i++ )
2449 if( mask[i] != 0xFF )
2450 return( -1 );
2451
2452 for( i = 0; i < len % 8; i++ )
2453 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2454 return( -1 );
2455
2456 return( 0 );
2457}
2458
2459/*
2460 * Reassemble fragmented DTLS handshake messages.
2461 *
2462 * Use a temporary buffer for reassembly, divided in two parts:
2463 * - the first holds the reassembled message (including handshake header),
2464 * - the second holds a bitmask indicating which parts of the message
2465 * (excluding headers) have been received so far.
2466 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002467static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2468{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002469 unsigned char *msg, *bitmask;
2470 size_t frag_len, frag_off;
2471 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2472
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002473 if( ssl->handshake == NULL )
2474 {
2475 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2476 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2477 }
2478
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002479 /*
2480 * For first fragment, check size and allocate buffer
2481 */
2482 if( ssl->handshake->hs_msg == NULL )
2483 {
2484 size_t alloc_len;
2485
2486 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2487 msg_len ) );
2488
2489 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2490 {
2491 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2492 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2493 }
2494
2495 /* The bitmask needs one bit per byte of message excluding header */
2496 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2497
2498 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2499 if( ssl->handshake->hs_msg == NULL )
2500 {
2501 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2502 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2503 }
2504
2505 memset( ssl->handshake->hs_msg, 0, alloc_len );
2506
2507 /* Prepare final header: copy msg_type, length and message_seq,
2508 * then add standardised fragment_offset and fragment_length */
2509 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2510 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2511 memcpy( ssl->handshake->hs_msg + 9,
2512 ssl->handshake->hs_msg + 1, 3 );
2513 }
2514 else
2515 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002516 /* Make sure msg_type and length are consistent */
2517 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002518 {
2519 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2520 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2521 }
2522 }
2523
2524 msg = ssl->handshake->hs_msg + 12;
2525 bitmask = msg + msg_len;
2526
2527 /*
2528 * Check and copy current fragment
2529 */
2530 frag_off = ( ssl->in_msg[6] << 16 ) |
2531 ( ssl->in_msg[7] << 8 ) |
2532 ssl->in_msg[8];
2533 frag_len = ( ssl->in_msg[9] << 16 ) |
2534 ( ssl->in_msg[10] << 8 ) |
2535 ssl->in_msg[11];
2536
2537 if( frag_off + frag_len > msg_len )
2538 {
2539 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2540 frag_off, frag_len, msg_len ) );
2541 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2542 }
2543
2544 if( frag_len + 12 > ssl->in_msglen )
2545 {
2546 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2547 frag_len, ssl->in_msglen ) );
2548 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2549 }
2550
2551 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2552 frag_off, frag_len ) );
2553
2554 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2555 ssl_bitmask_set( bitmask, frag_off, frag_len );
2556
2557 /*
2558 * Do we have the complete message by now?
2559 * If yes, finalize it, else ask to read the next record.
2560 */
2561 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2562 {
2563 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2564 return( POLARSSL_ERR_NET_WANT_READ );
2565 }
2566
2567 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2568
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002569 if( ssl->in_left > ssl->next_record_offset )
2570 {
2571 /*
2572 * We've got more data in the buffer after the current record,
2573 * that we don't want to overwrite. Move it before writing the
2574 * reassembled message, and adjust in_left and next_record_offset.
2575 */
2576 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2577 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2578 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2579
2580 /* First compute and check new lengths */
2581 ssl->next_record_offset = new_remain - ssl->in_hdr;
2582 ssl->in_left = ssl->next_record_offset + remain_len;
2583
2584 if( ssl->in_left > SSL_BUFFER_LEN -
2585 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2586 {
2587 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2588 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2589 }
2590
2591 memmove( new_remain, cur_remain, remain_len );
2592 }
2593
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002594 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2595
2596 polarssl_free( ssl->handshake->hs_msg );
2597 ssl->handshake->hs_msg = NULL;
2598
2599 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2600 ssl->in_msg, ssl->in_hslen );
2601
2602 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002603}
2604#endif /* POLARSSL_SSL_PROTO_DTLS */
2605
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002606static int ssl_prepare_handshake_record( ssl_context *ssl )
2607{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002608 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2611 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002612 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002613 }
2614
2615 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2616 ( ssl->in_msg[1] << 16 ) |
2617 ( ssl->in_msg[2] << 8 ) |
2618 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002619
2620 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2621 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002622 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002623
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002624#if defined(POLARSSL_SSL_PROTO_DTLS)
2625 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002626 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002627 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002628 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002629
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002630 /* ssl->handshake is NULL when receiving ClientHello for renego */
2631 if( ssl->handshake != NULL &&
2632 recv_msg_seq != ssl->handshake->in_msg_seq )
2633 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002634 /* No sane server ever retransmits HelloVerifyRequest */
2635 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2636 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002637 {
2638 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2639 "message_seq = %d, start_of_flight = %d",
2640 recv_msg_seq,
2641 ssl->handshake->in_flight_start_seq ) );
2642
2643 if( ( ret = ssl_resend( ssl ) ) != 0 )
2644 {
2645 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2646 return( ret );
2647 }
2648 }
2649 else
2650 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002651 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002652 "message_seq = %d, expected = %d",
2653 recv_msg_seq,
2654 ssl->handshake->in_msg_seq ) );
2655 }
2656
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002657 return( POLARSSL_ERR_NET_WANT_READ );
2658 }
2659 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002660
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002661 /* Reassemble if current message is fragmented or reassembly is
2662 * already in progress */
2663 if( ssl->in_msglen < ssl->in_hslen ||
2664 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2665 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2666 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002667 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002668 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2669
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002670 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2671 {
2672 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2673 return( ret );
2674 }
2675 }
2676 }
2677 else
2678#endif /* POLARSSL_SSL_PROTO_DTLS */
2679 /* With TLS we don't handle fragmentation (for now) */
2680 if( ssl->in_msglen < ssl->in_hslen )
2681 {
2682 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002683 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002684 }
2685
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002686 if( ssl->state != SSL_HANDSHAKE_OVER )
2687 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2688
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002689 /* Handshake message is complete, increment counter */
2690#if defined(POLARSSL_SSL_PROTO_DTLS)
2691 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2692 ssl->handshake != NULL )
2693 {
2694 ssl->handshake->in_msg_seq++;
2695 }
2696#endif
2697
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002698 return( 0 );
2699}
2700
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002701/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002702 * DTLS anti-replay: RFC 6347 4.1.2.6
2703 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002704 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2705 * Bit n is set iff record number in_window_top - n has been seen.
2706 *
2707 * Usually, in_window_top is the last record number seen and the lsb of
2708 * in_window is set. The only exception is the initial state (record number 0
2709 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002710 */
2711#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2712static void ssl_dtls_replay_reset( ssl_context *ssl )
2713{
2714 ssl->in_window_top = 0;
2715 ssl->in_window = 0;
2716}
2717
2718static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2719{
2720 return( ( (uint64_t) buf[0] << 40 ) |
2721 ( (uint64_t) buf[1] << 32 ) |
2722 ( (uint64_t) buf[2] << 24 ) |
2723 ( (uint64_t) buf[3] << 16 ) |
2724 ( (uint64_t) buf[4] << 8 ) |
2725 ( (uint64_t) buf[5] ) );
2726}
2727
2728/*
2729 * Return 0 if sequence number is acceptable, -1 otherwise
2730 */
2731int ssl_dtls_replay_check( ssl_context *ssl )
2732{
2733 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2734 uint64_t bit;
2735
2736 if( rec_seqnum > ssl->in_window_top )
2737 return( 0 );
2738
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002739 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002740
2741 if( bit >= 64 )
2742 return( -1 );
2743
2744 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2745 return( -1 );
2746
2747 return( 0 );
2748}
2749
2750/*
2751 * Update replay window on new validated record
2752 */
2753void ssl_dtls_replay_update( ssl_context *ssl )
2754{
2755 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2756
2757 if( rec_seqnum > ssl->in_window_top )
2758 {
2759 /* Update window_top and the contents of the window */
2760 uint64_t shift = rec_seqnum - ssl->in_window_top;
2761
2762 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002763 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002764 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002765 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002766 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002767 ssl->in_window |= 1;
2768 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002769
2770 ssl->in_window_top = rec_seqnum;
2771 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002772 else
2773 {
2774 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002775 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002776
2777 if( bit < 64 ) /* Always true, but be extra sure */
2778 ssl->in_window |= (uint64_t) 1 << bit;
2779 }
2780}
2781#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2782
2783/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002784 * ContentType type;
2785 * ProtocolVersion version;
2786 * uint16 epoch; // DTLS only
2787 * uint48 sequence_number; // DTLS only
2788 * uint16 length;
2789 */
2790static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002791{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002792 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002793 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002795 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2796
Paul Bakker5121ce52009-01-03 21:22:43 +00002797 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002798 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002799 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
2801 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2802 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002803 ssl->in_msgtype,
2804 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002806 /* Check record type */
2807 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2808 ssl->in_msgtype != SSL_MSG_ALERT &&
2809 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2810 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2811 {
2812 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002813
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002814 if( ( ret = ssl_send_alert_message( ssl,
2815 SSL_ALERT_LEVEL_FATAL,
2816 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2817 {
2818 return( ret );
2819 }
2820
2821 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2822 }
2823
2824 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002825 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 {
2827 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002828 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 }
2830
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002831 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002832 {
2833 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002834 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835 }
2836
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002837 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002838#if defined(POLARSSL_SSL_PROTO_DTLS)
2839 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2840 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002841 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002842
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002843 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002844 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002845 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002846 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002847 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002848 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002849 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002850
2851#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2852 if( ssl_dtls_replay_check( ssl ) != 0 )
2853 {
2854 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
2855 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2856 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002857#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002858 }
2859#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002860
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002861 /* Check length against the size of our buffer */
2862 if( ssl->in_msglen > SSL_BUFFER_LEN
2863 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002864 {
2865 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2866 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2867 }
2868
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002869 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002870 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002872 if( ssl->in_msglen < 1 ||
2873 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 {
2875 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002876 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 }
2878 }
2879 else
2880 {
Paul Bakker48916f92012-09-16 19:57:18 +00002881 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 {
2883 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002884 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 }
2886
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002887#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002889 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 {
2891 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002892 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002894#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002895#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2896 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 /*
2898 * TLS encrypted messages can have up to 256 bytes of padding
2899 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002901 ssl->in_msglen > ssl->transform_in->minlen +
2902 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 {
2904 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002905 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002907#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 }
2909
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002910 return( 0 );
2911}
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002913/*
2914 * If applicable, decrypt (and decompress) record content
2915 */
2916static int ssl_prepare_record_content( ssl_context *ssl )
2917{
2918 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002919
Paul Bakker5121ce52009-01-03 21:22:43 +00002920 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002921 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker05ef8352012-05-08 09:17:57 +00002923#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002924 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002925 {
2926 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2927
2928 ret = ssl_hw_record_read( ssl );
2929 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2930 {
2931 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002932 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002933 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002934
2935 if( ret == 0 )
2936 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002937 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002938#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002939 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002940 {
2941 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2942 {
2943 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2944 return( ret );
2945 }
2946
2947 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2948 ssl->in_msg, ssl->in_msglen );
2949
2950 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2951 {
2952 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002953 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 }
2955 }
2956
Paul Bakker2770fbd2012-07-03 13:30:23 +00002957#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002958 if( ssl->transform_in != NULL &&
2959 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002960 {
2961 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2962 {
2963 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2964 return( ret );
2965 }
2966
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002967 // TODO: what's the purpose of these lines? is in_len used?
2968 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2969 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002970 }
2971#endif /* POLARSSL_ZLIB_SUPPORT */
2972
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002973#if defined(POLARSSL_SSL_PROTO_DTLS) && \
2974 defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2975 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2976 {
2977 ssl_dtls_replay_update( ssl );
2978 }
2979#endif
2980
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002981 return( 0 );
2982}
2983
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002984static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
2985
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02002986/*
2987 * Read a record.
2988 *
2989 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
2990 * and continue reading until a valid record is found.
2991 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002992int ssl_read_record( ssl_context *ssl )
2993{
2994 int ret;
2995
2996 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2997
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02002998 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002999 {
3000 /*
3001 * Get next Handshake message in the current record
3002 */
3003 ssl->in_msglen -= ssl->in_hslen;
3004
3005 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3006 ssl->in_msglen );
3007
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003008 SSL_DEBUG_BUF( 4, "remaining content in record",
3009 ssl->in_msg, ssl->in_msglen );
3010
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003011 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3012 return( ret );
3013
3014 return( 0 );
3015 }
3016
3017 ssl->in_hslen = 0;
3018
3019 /*
3020 * Read the record header and parse it
3021 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003022#if defined(POLARSSL_SSL_PROTO_DTLS)
3023read_record_header:
3024#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003025 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3026 {
3027 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3028 return( ret );
3029 }
3030
3031 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003032 {
3033#if defined(POLARSSL_SSL_PROTO_DTLS)
3034 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3035 {
3036 /* Ignore bad record and get next one; drop the whole datagram
3037 * since current header cannot be trusted to find the next record
3038 * in current datagram */
3039 ssl->next_record_offset = 0;
3040 ssl->in_left = 0;
3041
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003042 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003043 goto read_record_header;
3044 }
3045#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003046 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003047 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003048
3049 /*
3050 * Read and optionally decrypt the message contents
3051 */
3052 if( ( ret = ssl_fetch_input( ssl,
3053 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3054 {
3055 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3056 return( ret );
3057 }
3058
3059 /* Done reading this record, get ready for the next one */
3060#if defined(POLARSSL_SSL_PROTO_DTLS)
3061 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3062 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3063 else
3064#endif
3065 ssl->in_left = 0;
3066
3067 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003068 {
3069#if defined(POLARSSL_SSL_PROTO_DTLS)
3070 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3071 {
3072 /* Silently discard invalid records */
3073 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3074 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3075 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003076 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003077 goto read_record_header;
3078 }
3079
3080 return( ret );
3081 }
3082 else
3083#endif
3084 {
3085 /* Error out (and send alert) on invalid records */
3086#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3087 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3088 {
3089 ssl_send_alert_message( ssl,
3090 SSL_ALERT_LEVEL_FATAL,
3091 SSL_ALERT_MSG_BAD_RECORD_MAC );
3092 }
3093#endif
3094 return( ret );
3095 }
3096 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003097
3098 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003099 * When we sent the last flight of the handshake, we MUST respond to a
3100 * retransmit of the peer's previous flight with a retransmit. (In
3101 * practice, only the Finished message will make it, other messages
3102 * including CCS use the old transform so they're dropped as invalid.)
3103 *
3104 * If the record we received is not a handshake message, however, it
3105 * means the peer received our last flight so we can clean up
3106 * handshake info.
3107 *
3108 * This check needs to be done before prepare_handshake() due to an edge
3109 * case: if the client immediately requests renegotiation, this
3110 * finishes the current handshake first, avoiding the new ClientHello
3111 * being mistaken for an ancient message in the current handshake.
3112 */
3113#if defined(POLARSSL_SSL_PROTO_DTLS)
3114 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3115 ssl->handshake != NULL &&
3116 ssl->handshake->retransmit_state == SSL_RETRANS_FINISHED )
3117 {
3118 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3119 ssl->in_msg[0] == SSL_HS_FINISHED )
3120 {
3121 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3122
3123 if( ( ret = ssl_resend( ssl ) ) != 0 )
3124 {
3125 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3126 return( ret );
3127 }
3128
3129 return( POLARSSL_ERR_NET_WANT_READ );
3130 }
3131 else
3132 {
3133 ssl_handshake_wrapup_free_hs_transform( ssl );
3134 }
3135 }
3136#endif
3137
3138 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003139 * Handle particular types of records
3140 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3142 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003143 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3144 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145 }
3146
3147 if( ssl->in_msgtype == SSL_MSG_ALERT )
3148 {
3149 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3150 ssl->in_msg[0], ssl->in_msg[1] ) );
3151
3152 /*
3153 * Ignore non-fatal alerts, except close_notify
3154 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003155 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003156 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003157 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3158 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 }
3161
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003162 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3163 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 {
3165 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003166 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 }
3168 }
3169
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003170#if defined(POLARSSL_SSL_PROTO_DTLS)
3171 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3172 {
3173 /* Drop unexpected ChangeCipherSpec messages */
3174 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3175 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3176 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3177 {
3178 SSL_DEBUG_MSG( 2, ( "dropping unexpected ChangeCipherSpec" ) );
3179 return( POLARSSL_ERR_NET_WANT_READ );
3180 }
3181 }
3182#endif
3183
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3185
3186 return( 0 );
3187}
3188
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003189int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3190{
3191 int ret;
3192
3193 if( ( ret = ssl_send_alert_message( ssl,
3194 SSL_ALERT_LEVEL_FATAL,
3195 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3196 {
3197 return( ret );
3198 }
3199
3200 return( 0 );
3201}
3202
Paul Bakker0a925182012-04-16 06:46:41 +00003203int ssl_send_alert_message( ssl_context *ssl,
3204 unsigned char level,
3205 unsigned char message )
3206{
3207 int ret;
3208
3209 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3210
3211 ssl->out_msgtype = SSL_MSG_ALERT;
3212 ssl->out_msglen = 2;
3213 ssl->out_msg[0] = level;
3214 ssl->out_msg[1] = message;
3215
3216 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3217 {
3218 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3219 return( ret );
3220 }
3221
3222 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3223
3224 return( 0 );
3225}
3226
Paul Bakker5121ce52009-01-03 21:22:43 +00003227/*
3228 * Handshake functions
3229 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003230#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3231 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3232 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3233 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3234 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3235 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3236 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003237int ssl_write_certificate( ssl_context *ssl )
3238{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003239 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003240
3241 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3242
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003243 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003244 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3245 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003246 {
3247 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3248 ssl->state++;
3249 return( 0 );
3250 }
3251
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003252 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3253 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003254}
3255
3256int ssl_parse_certificate( ssl_context *ssl )
3257{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3259
3260 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3261
3262 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003263 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3264 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003265 {
3266 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3267 ssl->state++;
3268 return( 0 );
3269 }
3270
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003271 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3272 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003273}
3274#else
3275int ssl_write_certificate( ssl_context *ssl )
3276{
3277 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3278 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003279 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003280 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3281
3282 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3283
3284 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003285 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3286 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003287 {
3288 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3289 ssl->state++;
3290 return( 0 );
3291 }
3292
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 if( ssl->endpoint == SSL_IS_CLIENT )
3294 {
3295 if( ssl->client_auth == 0 )
3296 {
3297 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3298 ssl->state++;
3299 return( 0 );
3300 }
3301
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003302#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 /*
3304 * If using SSLv3 and got no cert, send an Alert message
3305 * (otherwise an empty Certificate message will be sent).
3306 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003307 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3309 {
3310 ssl->out_msglen = 2;
3311 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003312 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3313 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003314
3315 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3316 goto write_msg;
3317 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003318#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 }
3320 else /* SSL_IS_SERVER */
3321 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003322 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 {
3324 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003325 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 }
3327 }
3328
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003329 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003330
3331 /*
3332 * 0 . 0 handshake type
3333 * 1 . 3 handshake length
3334 * 4 . 6 length of all certs
3335 * 7 . 9 length of cert. 1
3336 * 10 . n-1 peer certificate
3337 * n . n+2 length of cert. 2
3338 * n+3 . ... upper level cert, etc.
3339 */
3340 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003341 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003342
Paul Bakker29087132010-03-21 21:03:34 +00003343 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003344 {
3345 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003346 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 {
3348 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3349 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003350 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 }
3352
3353 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3354 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3355 ssl->out_msg[i + 2] = (unsigned char)( n );
3356
3357 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3358 i += n; crt = crt->next;
3359 }
3360
3361 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3362 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3363 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3364
3365 ssl->out_msglen = i;
3366 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3367 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3368
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003369#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003370write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003371#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003372
3373 ssl->state++;
3374
3375 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3376 {
3377 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3378 return( ret );
3379 }
3380
3381 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3382
Paul Bakkered27a042013-04-18 22:46:23 +02003383 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003384}
3385
3386int ssl_parse_certificate( ssl_context *ssl )
3387{
Paul Bakkered27a042013-04-18 22:46:23 +02003388 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003389 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003390 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003391
3392 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3393
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003394 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003395 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3396 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003397 {
3398 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3399 ssl->state++;
3400 return( 0 );
3401 }
3402
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003404 ( ssl->authmode == SSL_VERIFY_NONE ||
3405 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003406 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003407 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3409 ssl->state++;
3410 return( 0 );
3411 }
3412
3413 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3414 {
3415 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3416 return( ret );
3417 }
3418
3419 ssl->state++;
3420
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003421#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 /*
3423 * Check if the client sent an empty certificate
3424 */
3425 if( ssl->endpoint == SSL_IS_SERVER &&
3426 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3427 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003428 if( ssl->in_msglen == 2 &&
3429 ssl->in_msgtype == SSL_MSG_ALERT &&
3430 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3431 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 {
3433 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3434
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003435 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3437 return( 0 );
3438 else
Paul Bakker40e46942009-01-03 21:51:57 +00003439 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 }
3441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003442#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003443
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003444#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3445 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 if( ssl->endpoint == SSL_IS_SERVER &&
3447 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3448 {
3449 if( ssl->in_hslen == 7 &&
3450 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3451 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
3452 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
3453 {
3454 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3455
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003456 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003458 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 else
3460 return( 0 );
3461 }
3462 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003463#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3464 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
3466 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3467 {
3468 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003469 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 }
3471
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003472 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3473 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 {
3475 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003476 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 }
3478
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003479 i = ssl_hs_hdr_len( ssl );
3480
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 /*
3482 * Same message structure as in ssl_write_certificate()
3483 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003484 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003486 if( ssl->in_msg[i] != 0 ||
3487 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 {
3489 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003490 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491 }
3492
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003493 /* In case we tried to reuse a session but it failed */
3494 if( ssl->session_negotiate->peer_cert != NULL )
3495 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003496 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003497 polarssl_free( ssl->session_negotiate->peer_cert );
3498 }
3499
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003500 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3501 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 {
3503 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003504 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003505 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 }
3507
Paul Bakkerb6b09562013-09-18 14:17:41 +02003508 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003510 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
3512 while( i < ssl->in_hslen )
3513 {
3514 if( ssl->in_msg[i] != 0 )
3515 {
3516 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003517 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 }
3519
3520 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3521 | (unsigned int) ssl->in_msg[i + 2];
3522 i += 3;
3523
3524 if( n < 128 || i + n > ssl->in_hslen )
3525 {
3526 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003527 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 }
3529
Paul Bakkerddf26b42013-09-18 13:46:23 +02003530 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3531 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 if( ret != 0 )
3533 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003534 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 return( ret );
3536 }
3537
3538 i += n;
3539 }
3540
Paul Bakker48916f92012-09-16 19:57:18 +00003541 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003543 /*
3544 * On client, make sure the server cert doesn't change during renego to
3545 * avoid "triple handshake" attack: https://secure-resumption.com/
3546 */
3547 if( ssl->endpoint == SSL_IS_CLIENT &&
3548 ssl->renegotiation == SSL_RENEGOTIATION )
3549 {
3550 if( ssl->session->peer_cert == NULL )
3551 {
3552 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3553 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3554 }
3555
3556 if( ssl->session->peer_cert->raw.len !=
3557 ssl->session_negotiate->peer_cert->raw.len ||
3558 memcmp( ssl->session->peer_cert->raw.p,
3559 ssl->session_negotiate->peer_cert->raw.p,
3560 ssl->session->peer_cert->raw.len ) != 0 )
3561 {
3562 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3563 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3564 }
3565 }
3566
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 if( ssl->authmode != SSL_VERIFY_NONE )
3568 {
3569 if( ssl->ca_chain == NULL )
3570 {
3571 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003572 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 }
3574
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003575 /*
3576 * Main check: verify certificate
3577 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003578 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3579 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3580 &ssl->session_negotiate->verify_result,
3581 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
3583 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003584 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003586 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003587
3588 /*
3589 * Secondary checks: always done, but change 'ret' only if it was 0
3590 */
3591
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003592#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003593 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003594 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003595
3596 /* If certificate uses an EC key, make sure the curve is OK */
3597 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3598 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3599 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003600 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003601 if( ret == 0 )
3602 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003603 }
3604 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003605#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003607 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3608 ciphersuite_info,
3609 ! ssl->endpoint ) != 0 )
3610 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003611 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003612 if( ret == 0 )
3613 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3614 }
3615
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3617 ret = 0;
3618 }
3619
3620 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3621
3622 return( ret );
3623}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003624#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3625 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3626 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3627 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3628 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3629 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3630 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003631
3632int ssl_write_change_cipher_spec( ssl_context *ssl )
3633{
3634 int ret;
3635
3636 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3637
3638 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3639 ssl->out_msglen = 1;
3640 ssl->out_msg[0] = 1;
3641
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 ssl->state++;
3643
3644 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3645 {
3646 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3647 return( ret );
3648 }
3649
3650 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3651
3652 return( 0 );
3653}
3654
3655int ssl_parse_change_cipher_spec( ssl_context *ssl )
3656{
3657 int ret;
3658
3659 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3660
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3662 {
3663 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3664 return( ret );
3665 }
3666
3667 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3668 {
3669 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003670 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 }
3672
3673 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3674 {
3675 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003676 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 }
3678
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003679 /*
3680 * Switch to our negotiated transform and session parameters for inbound
3681 * data.
3682 */
3683 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3684 ssl->transform_in = ssl->transform_negotiate;
3685 ssl->session_in = ssl->session_negotiate;
3686
3687#if defined(POLARSSL_SSL_PROTO_DTLS)
3688 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3689 {
3690#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3691 ssl_dtls_replay_reset( ssl );
3692#endif
3693
3694 /* Increment epoch */
3695 if( ++ssl->in_epoch == 0 )
3696 {
3697 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3698 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3699 }
3700 }
3701 else
3702#endif /* POLARSSL_SSL_PROTO_DTLS */
3703 memset( ssl->in_ctr, 0, 8 );
3704
3705 /*
3706 * Set the in_msg pointer to the correct location based on IV length
3707 */
3708 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3709 {
3710 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3711 ssl->transform_negotiate->fixed_ivlen;
3712 }
3713 else
3714 ssl->in_msg = ssl->in_iv;
3715
3716#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3717 if( ssl_hw_record_activate != NULL )
3718 {
3719 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3720 {
3721 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3722 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3723 }
3724 }
3725#endif
3726
Paul Bakker5121ce52009-01-03 21:22:43 +00003727 ssl->state++;
3728
3729 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3730
3731 return( 0 );
3732}
3733
Paul Bakker41c83d32013-03-20 14:39:14 +01003734void ssl_optimize_checksum( ssl_context *ssl,
3735 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003736{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003737 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003738
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003739#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3740 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003741 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003742 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003743 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003744#endif
3745#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3746#if defined(POLARSSL_SHA512_C)
3747 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3748 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3749 else
3750#endif
3751#if defined(POLARSSL_SHA256_C)
3752 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003753 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003754 else
3755#endif
3756#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003757 {
3758 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003759 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003760 }
Paul Bakker380da532012-04-18 16:10:25 +00003761}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003762
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003763void ssl_reset_checksum( ssl_context *ssl )
3764{
3765#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3766 defined(POLARSSL_SSL_PROTO_TLS1_1)
3767 md5_starts( &ssl->handshake->fin_md5 );
3768 sha1_starts( &ssl->handshake->fin_sha1 );
3769#endif
3770#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3771#if defined(POLARSSL_SHA256_C)
3772 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3773#endif
3774#if defined(POLARSSL_SHA512_C)
3775 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3776#endif
3777#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3778}
3779
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003780static void ssl_update_checksum_start( ssl_context *ssl,
3781 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003782{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003783#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3784 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003785 md5_update( &ssl->handshake->fin_md5 , buf, len );
3786 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003787#endif
3788#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3789#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003790 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003791#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003792#if defined(POLARSSL_SHA512_C)
3793 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003794#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003795#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003796}
3797
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003798#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3799 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003800static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3801 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003802{
Paul Bakker48916f92012-09-16 19:57:18 +00003803 md5_update( &ssl->handshake->fin_md5 , buf, len );
3804 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003805}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003806#endif
Paul Bakker380da532012-04-18 16:10:25 +00003807
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003808#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3809#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003810static void ssl_update_checksum_sha256( ssl_context *ssl,
3811 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003812{
Paul Bakker9e36f042013-06-30 14:34:05 +02003813 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003814}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003815#endif
Paul Bakker380da532012-04-18 16:10:25 +00003816
Paul Bakker9e36f042013-06-30 14:34:05 +02003817#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003818static void ssl_update_checksum_sha384( ssl_context *ssl,
3819 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003820{
Paul Bakker9e36f042013-06-30 14:34:05 +02003821 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003822}
Paul Bakker769075d2012-11-24 11:26:46 +01003823#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003824#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003825
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003826#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003827static void ssl_calc_finished_ssl(
3828 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003829{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003830 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003831 md5_context md5;
3832 sha1_context sha1;
3833
Paul Bakker5121ce52009-01-03 21:22:43 +00003834 unsigned char padbuf[48];
3835 unsigned char md5sum[16];
3836 unsigned char sha1sum[20];
3837
Paul Bakker48916f92012-09-16 19:57:18 +00003838 ssl_session *session = ssl->session_negotiate;
3839 if( !session )
3840 session = ssl->session;
3841
Paul Bakker1ef83d62012-04-11 12:09:53 +00003842 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3843
Paul Bakker48916f92012-09-16 19:57:18 +00003844 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3845 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003846
3847 /*
3848 * SSLv3:
3849 * hash =
3850 * MD5( master + pad2 +
3851 * MD5( handshake + sender + master + pad1 ) )
3852 * + SHA1( master + pad2 +
3853 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 */
3855
Paul Bakker90995b52013-06-24 19:20:35 +02003856#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003857 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003858 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003860
Paul Bakker90995b52013-06-24 19:20:35 +02003861#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003862 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003863 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003864#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003865
Paul Bakker3c2122f2013-06-24 19:03:14 +02003866 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3867 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003868
Paul Bakker1ef83d62012-04-11 12:09:53 +00003869 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870
Paul Bakker3c2122f2013-06-24 19:03:14 +02003871 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003872 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003873 md5_update( &md5, padbuf, 48 );
3874 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003875
Paul Bakker3c2122f2013-06-24 19:03:14 +02003876 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003877 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003878 sha1_update( &sha1, padbuf, 40 );
3879 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003880
Paul Bakker1ef83d62012-04-11 12:09:53 +00003881 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882
Paul Bakker1ef83d62012-04-11 12:09:53 +00003883 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003884 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003885 md5_update( &md5, padbuf, 48 );
3886 md5_update( &md5, md5sum, 16 );
3887 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003888
Paul Bakker1ef83d62012-04-11 12:09:53 +00003889 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003890 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003891 sha1_update( &sha1, padbuf , 40 );
3892 sha1_update( &sha1, sha1sum, 20 );
3893 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003894
Paul Bakker1ef83d62012-04-11 12:09:53 +00003895 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003896
Paul Bakker5b4af392014-06-26 12:09:34 +02003897 md5_free( &md5 );
3898 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003899
Paul Bakker34617722014-06-13 17:20:13 +02003900 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3901 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3902 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003903
3904 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3905}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003906#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003908#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003909static void ssl_calc_finished_tls(
3910 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003912 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003913 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003914 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003915 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003916 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003917
Paul Bakker48916f92012-09-16 19:57:18 +00003918 ssl_session *session = ssl->session_negotiate;
3919 if( !session )
3920 session = ssl->session;
3921
Paul Bakker1ef83d62012-04-11 12:09:53 +00003922 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003923
Paul Bakker48916f92012-09-16 19:57:18 +00003924 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3925 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003926
Paul Bakker1ef83d62012-04-11 12:09:53 +00003927 /*
3928 * TLSv1:
3929 * hash = PRF( master, finished_label,
3930 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3931 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003932
Paul Bakker90995b52013-06-24 19:20:35 +02003933#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003934 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3935 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003936#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003937
Paul Bakker90995b52013-06-24 19:20:35 +02003938#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003939 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3940 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003941#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003942
3943 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003944 ? "client finished"
3945 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003946
3947 md5_finish( &md5, padbuf );
3948 sha1_finish( &sha1, padbuf + 16 );
3949
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003950 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003951 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003952
3953 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3954
Paul Bakker5b4af392014-06-26 12:09:34 +02003955 md5_free( &md5 );
3956 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003957
Paul Bakker34617722014-06-13 17:20:13 +02003958 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003959
3960 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3961}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003962#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003963
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003964#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3965#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003966static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003967 ssl_context *ssl, unsigned char *buf, int from )
3968{
3969 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003970 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003971 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003972 unsigned char padbuf[32];
3973
Paul Bakker48916f92012-09-16 19:57:18 +00003974 ssl_session *session = ssl->session_negotiate;
3975 if( !session )
3976 session = ssl->session;
3977
Paul Bakker380da532012-04-18 16:10:25 +00003978 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003979
Paul Bakker9e36f042013-06-30 14:34:05 +02003980 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003981
3982 /*
3983 * TLSv1.2:
3984 * hash = PRF( master, finished_label,
3985 * Hash( handshake ) )[0.11]
3986 */
3987
Paul Bakker9e36f042013-06-30 14:34:05 +02003988#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003989 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003990 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003991#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003992
3993 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003994 ? "client finished"
3995 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003996
Paul Bakker9e36f042013-06-30 14:34:05 +02003997 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003999 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004000 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004001
4002 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4003
Paul Bakker5b4af392014-06-26 12:09:34 +02004004 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004005
Paul Bakker34617722014-06-13 17:20:13 +02004006 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004007
4008 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4009}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004010#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004011
Paul Bakker9e36f042013-06-30 14:34:05 +02004012#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004013static void ssl_calc_finished_tls_sha384(
4014 ssl_context *ssl, unsigned char *buf, int from )
4015{
4016 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004017 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004018 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004019 unsigned char padbuf[48];
4020
Paul Bakker48916f92012-09-16 19:57:18 +00004021 ssl_session *session = ssl->session_negotiate;
4022 if( !session )
4023 session = ssl->session;
4024
Paul Bakker380da532012-04-18 16:10:25 +00004025 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004026
Paul Bakker9e36f042013-06-30 14:34:05 +02004027 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004028
4029 /*
4030 * TLSv1.2:
4031 * hash = PRF( master, finished_label,
4032 * Hash( handshake ) )[0.11]
4033 */
4034
Paul Bakker9e36f042013-06-30 14:34:05 +02004035#if !defined(POLARSSL_SHA512_ALT)
4036 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4037 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004038#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004039
4040 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004041 ? "client finished"
4042 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004043
Paul Bakker9e36f042013-06-30 14:34:05 +02004044 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004045
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004046 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004047 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004048
4049 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4050
Paul Bakker5b4af392014-06-26 12:09:34 +02004051 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004052
Paul Bakker34617722014-06-13 17:20:13 +02004053 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004054
4055 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4056}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004057#endif /* POLARSSL_SHA512_C */
4058#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004059
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004060static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004061{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004062 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004063
4064 /*
4065 * Free our handshake params
4066 */
4067 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004068 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004069 ssl->handshake = NULL;
4070
4071 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004072 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004073 */
4074 if( ssl->transform )
4075 {
4076 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004077 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004078 }
4079 ssl->transform = ssl->transform_negotiate;
4080 ssl->transform_negotiate = NULL;
4081
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004082 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4083}
4084
4085void ssl_handshake_wrapup( ssl_context *ssl )
4086{
4087 int resume = ssl->handshake->resume;
4088
4089 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4090
4091 if( ssl->renegotiation == SSL_RENEGOTIATION )
4092 {
4093 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4094 ssl->renego_records_seen = 0;
4095 }
4096
4097 /*
4098 * Free the previous session and switch in the current one
4099 */
Paul Bakker0a597072012-09-25 21:55:46 +00004100 if( ssl->session )
4101 {
4102 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004103 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004104 }
4105 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004106 ssl->session_negotiate = NULL;
4107
Paul Bakker0a597072012-09-25 21:55:46 +00004108 /*
4109 * Add cache entry
4110 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004111 if( ssl->f_set_cache != NULL &&
4112 ssl->session->length != 0 &&
4113 resume == 0 )
4114 {
Paul Bakker0a597072012-09-25 21:55:46 +00004115 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4116 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004117 }
Paul Bakker0a597072012-09-25 21:55:46 +00004118
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004119#if defined(POLARSSL_SSL_PROTO_DTLS)
4120 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4121 ssl->handshake->flight != NULL )
4122 {
4123 /* Keep last flight around in case we need to resend it:
4124 * we need the handshake and transform structures for that */
4125 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4126 }
4127 else
4128#endif
4129 ssl_handshake_wrapup_free_hs_transform( ssl );
4130
Paul Bakker48916f92012-09-16 19:57:18 +00004131 ssl->state++;
4132
4133 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4134}
4135
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136int ssl_write_finished( ssl_context *ssl )
4137{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004138 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004139
4140 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4141
Paul Bakker92be97b2013-01-02 17:30:03 +01004142 /*
4143 * Set the out_msg pointer to the correct location based on IV length
4144 */
4145 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4146 {
4147 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4148 ssl->transform_negotiate->fixed_ivlen;
4149 }
4150 else
4151 ssl->out_msg = ssl->out_iv;
4152
Paul Bakker48916f92012-09-16 19:57:18 +00004153 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004154
4155 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004156 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4157
Paul Bakker48916f92012-09-16 19:57:18 +00004158 ssl->verify_data_len = hash_len;
4159 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4160
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 ssl->out_msglen = 4 + hash_len;
4162 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4163 ssl->out_msg[0] = SSL_HS_FINISHED;
4164
4165 /*
4166 * In case of session resuming, invert the client and server
4167 * ChangeCipherSpec messages order.
4168 */
Paul Bakker0a597072012-09-25 21:55:46 +00004169 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004170 {
4171 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004172 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004173 else
4174 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4175 }
4176 else
4177 ssl->state++;
4178
Paul Bakker48916f92012-09-16 19:57:18 +00004179 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004180 * Switch to our negotiated transform and session parameters for outbound
4181 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004182 */
4183 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004184
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004185#if defined(POLARSSL_SSL_PROTO_DTLS)
4186 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4187 {
4188 unsigned char i;
4189
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004190 /* Remember current epoch settings for resending */
4191 ssl->handshake->alt_transform_out = ssl->transform_out;
4192 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4193
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004194 /* Set sequence_number to zero */
4195 memset( ssl->out_ctr + 2, 0, 6 );
4196
4197 /* Increment epoch */
4198 for( i = 2; i > 0; i-- )
4199 if( ++ssl->out_ctr[i - 1] != 0 )
4200 break;
4201
4202 /* The loop goes to its end iff the counter is wrapping */
4203 if( i == 0 )
4204 {
4205 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4206 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4207 }
4208 }
4209 else
4210#endif /* POLARSSL_SSL_PROTO_DTLS */
4211 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004212
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004213 ssl->transform_out = ssl->transform_negotiate;
4214 ssl->session_out = ssl->session_negotiate;
4215
Paul Bakker07eb38b2012-12-19 14:42:06 +01004216#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004217 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004218 {
4219 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4220 {
4221 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4222 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4223 }
4224 }
4225#endif
4226
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4228 {
4229 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4230 return( ret );
4231 }
4232
4233 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4234
4235 return( 0 );
4236}
4237
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004238#if defined(POLARSSL_SSL_PROTO_SSL3)
4239#define SSL_MAX_HASH_LEN 36
4240#else
4241#define SSL_MAX_HASH_LEN 12
4242#endif
4243
Paul Bakker5121ce52009-01-03 21:22:43 +00004244int ssl_parse_finished( ssl_context *ssl )
4245{
Paul Bakker23986e52011-04-24 08:57:21 +00004246 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004247 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004248 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004249
4250 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4251
Paul Bakker48916f92012-09-16 19:57:18 +00004252 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004253
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4255 {
4256 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4257 return( ret );
4258 }
4259
4260 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4261 {
4262 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004263 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004264 }
4265
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004266 /* There is currently no ciphersuite using another length with TLS 1.2 */
4267#if defined(POLARSSL_SSL_PROTO_SSL3)
4268 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4269 hash_len = 36;
4270 else
4271#endif
4272 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
4274 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004275 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 {
4277 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004278 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 }
4280
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004281 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4282 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004283 {
4284 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004285 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004286 }
4287
Paul Bakker48916f92012-09-16 19:57:18 +00004288 ssl->verify_data_len = hash_len;
4289 memcpy( ssl->peer_verify_data, buf, hash_len );
4290
Paul Bakker0a597072012-09-25 21:55:46 +00004291 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004292 {
4293 if( ssl->endpoint == SSL_IS_CLIENT )
4294 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4295
4296 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004297 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004298 }
4299 else
4300 ssl->state++;
4301
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004302#if defined(POLARSSL_SSL_PROTO_DTLS)
4303 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4304 ssl_recv_flight_completed( ssl );
4305#endif
4306
Paul Bakker5121ce52009-01-03 21:22:43 +00004307 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4308
4309 return( 0 );
4310}
4311
Paul Bakker968afaa2014-07-09 11:09:24 +02004312static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004313{
4314 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4315
4316#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4317 defined(POLARSSL_SSL_PROTO_TLS1_1)
4318 md5_init( &handshake->fin_md5 );
4319 sha1_init( &handshake->fin_sha1 );
4320 md5_starts( &handshake->fin_md5 );
4321 sha1_starts( &handshake->fin_sha1 );
4322#endif
4323#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4324#if defined(POLARSSL_SHA256_C)
4325 sha256_init( &handshake->fin_sha256 );
4326 sha256_starts( &handshake->fin_sha256, 0 );
4327#endif
4328#if defined(POLARSSL_SHA512_C)
4329 sha512_init( &handshake->fin_sha512 );
4330 sha512_starts( &handshake->fin_sha512, 1 );
4331#endif
4332#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4333
4334 handshake->update_checksum = ssl_update_checksum_start;
4335 handshake->sig_alg = SSL_HASH_SHA1;
4336
4337#if defined(POLARSSL_DHM_C)
4338 dhm_init( &handshake->dhm_ctx );
4339#endif
4340#if defined(POLARSSL_ECDH_C)
4341 ecdh_init( &handshake->ecdh_ctx );
4342#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004343}
4344
4345static void ssl_transform_init( ssl_transform *transform )
4346{
4347 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004348
4349 cipher_init( &transform->cipher_ctx_enc );
4350 cipher_init( &transform->cipher_ctx_dec );
4351
4352 md_init( &transform->md_ctx_enc );
4353 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004354}
4355
4356void ssl_session_init( ssl_session *session )
4357{
4358 memset( session, 0, sizeof(ssl_session) );
4359}
4360
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004361static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004362{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004363 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004364 if( ssl->transform_negotiate )
4365 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004366 if( ssl->session_negotiate )
4367 ssl_session_free( ssl->session_negotiate );
4368 if( ssl->handshake )
4369 ssl_handshake_free( ssl->handshake );
4370
4371 /*
4372 * Either the pointers are now NULL or cleared properly and can be freed.
4373 * Now allocate missing structures.
4374 */
4375 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004376 {
4377 ssl->transform_negotiate =
4378 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4379 }
Paul Bakker48916f92012-09-16 19:57:18 +00004380
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004381 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004382 {
4383 ssl->session_negotiate =
4384 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4385 }
Paul Bakker48916f92012-09-16 19:57:18 +00004386
Paul Bakker82788fb2014-10-20 13:59:19 +02004387 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004388 {
4389 ssl->handshake = (ssl_handshake_params *)
4390 polarssl_malloc( sizeof(ssl_handshake_params) );
4391 }
Paul Bakker48916f92012-09-16 19:57:18 +00004392
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004393 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004394 if( ssl->handshake == NULL ||
4395 ssl->transform_negotiate == NULL ||
4396 ssl->session_negotiate == NULL )
4397 {
4398 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004399
4400 polarssl_free( ssl->handshake );
4401 polarssl_free( ssl->transform_negotiate );
4402 polarssl_free( ssl->session_negotiate );
4403
4404 ssl->handshake = NULL;
4405 ssl->transform_negotiate = NULL;
4406 ssl->session_negotiate = NULL;
4407
Paul Bakker48916f92012-09-16 19:57:18 +00004408 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4409 }
4410
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004411 /* Initialize structures */
4412 ssl_session_init( ssl->session_negotiate );
4413 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004414 ssl_handshake_params_init( ssl->handshake );
4415
4416#if defined(POLARSSL_X509_CRT_PARSE_C)
4417 ssl->handshake->key_cert = ssl->key_cert;
4418#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004419
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004420#if defined(POLARSSL_SSL_PROTO_DTLS)
4421 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4422 {
4423 ssl->handshake->alt_transform_out = ssl->transform_out;
4424
4425 if( ssl->endpoint == SSL_IS_CLIENT )
4426 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4427 else
4428 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
4429 }
4430#endif
4431
Paul Bakker48916f92012-09-16 19:57:18 +00004432 return( 0 );
4433}
4434
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004435#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4436/* Dummy cookie callbacks for defaults */
4437static int ssl_cookie_write_dummy( void *ctx,
4438 unsigned char **p, unsigned char *end,
4439 const unsigned char *cli_id, size_t cli_id_len )
4440{
4441 ((void) ctx);
4442 ((void) p);
4443 ((void) end);
4444 ((void) cli_id);
4445 ((void) cli_id_len);
4446
4447 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4448}
4449
4450static int ssl_cookie_check_dummy( void *ctx,
4451 const unsigned char *cookie, size_t cookie_len,
4452 const unsigned char *cli_id, size_t cli_id_len )
4453{
4454 ((void) ctx);
4455 ((void) cookie);
4456 ((void) cookie_len);
4457 ((void) cli_id);
4458 ((void) cli_id_len);
4459
4460 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4461}
4462#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4463
Paul Bakker5121ce52009-01-03 21:22:43 +00004464/*
4465 * Initialize an SSL context
4466 */
4467int ssl_init( ssl_context *ssl )
4468{
Paul Bakker48916f92012-09-16 19:57:18 +00004469 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 int len = SSL_BUFFER_LEN;
4471
4472 memset( ssl, 0, sizeof( ssl_context ) );
4473
Paul Bakker62f2dee2012-09-28 07:31:51 +00004474 /*
4475 * Sane defaults
4476 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004477 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4478 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4479 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4480 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004481
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004482 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004483
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004484 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4485
Paul Bakker62f2dee2012-09-28 07:31:51 +00004486#if defined(POLARSSL_DHM_C)
4487 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4488 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4489 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4490 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4491 {
4492 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4493 return( ret );
4494 }
4495#endif
4496
4497 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004498 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004499 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004500 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004501 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004502
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004503 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 {
4505 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004506 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004507 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004508 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004509 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004510 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004511 }
4512
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004513 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4514 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004515
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004516 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4517 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4518
Paul Bakker606b4ba2013-08-14 16:52:14 +02004519#if defined(POLARSSL_SSL_SESSION_TICKETS)
4520 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4521#endif
4522
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004523#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004524 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004525#endif
4526
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004527#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4528 ssl->f_cookie_write = ssl_cookie_write_dummy;
4529 ssl->f_cookie_check = ssl_cookie_check_dummy;
4530#endif
4531
Paul Bakker48916f92012-09-16 19:57:18 +00004532 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4533 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004534
4535 return( 0 );
4536}
4537
4538/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004539 * Reset an initialized and used SSL context for re-use while retaining
4540 * all application-set variables, function pointers and data.
4541 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004542int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004543{
Paul Bakker48916f92012-09-16 19:57:18 +00004544 int ret;
4545
Paul Bakker7eb013f2011-10-06 12:37:39 +00004546 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004547 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4548 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4549
4550 ssl->verify_data_len = 0;
4551 memset( ssl->own_verify_data, 0, 36 );
4552 memset( ssl->peer_verify_data, 0, 36 );
4553
Paul Bakker7eb013f2011-10-06 12:37:39 +00004554 ssl->in_offt = NULL;
4555
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004556 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004557 ssl->in_msgtype = 0;
4558 ssl->in_msglen = 0;
4559 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004560#if defined(POLARSSL_SSL_PROTO_DTLS)
4561 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004562 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004563#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004564#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4565 ssl_dtls_replay_reset( ssl );
4566#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004567
4568 ssl->in_hslen = 0;
4569 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004570 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004571
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004572 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004573 ssl->out_msgtype = 0;
4574 ssl->out_msglen = 0;
4575 ssl->out_left = 0;
4576
Paul Bakker48916f92012-09-16 19:57:18 +00004577 ssl->transform_in = NULL;
4578 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004579
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004580 ssl->renego_records_seen = 0;
4581
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004582 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4583 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004584
4585#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004586 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004587 {
4588 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004589 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004590 {
4591 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4592 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4593 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004594 }
4595#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004596
Paul Bakker48916f92012-09-16 19:57:18 +00004597 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004598 {
Paul Bakker48916f92012-09-16 19:57:18 +00004599 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004600 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004601 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004602 }
Paul Bakker48916f92012-09-16 19:57:18 +00004603
Paul Bakkerc0463502013-02-14 11:19:38 +01004604 if( ssl->session )
4605 {
4606 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004607 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004608 ssl->session = NULL;
4609 }
4610
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004611#if defined(POLARSSL_SSL_ALPN)
4612 ssl->alpn_chosen = NULL;
4613#endif
4614
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004615#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004616 polarssl_free( ssl->cli_id );
4617 ssl->cli_id = NULL;
4618 ssl->cli_id_len = 0;
4619#endif
4620
Paul Bakker48916f92012-09-16 19:57:18 +00004621 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4622 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004623
4624 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004625}
4626
Paul Bakkera503a632013-08-14 13:48:06 +02004627#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004628static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4629{
4630 aes_free( &tkeys->enc );
4631 aes_free( &tkeys->dec );
4632
4633 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4634}
4635
Paul Bakker7eb013f2011-10-06 12:37:39 +00004636/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004637 * Allocate and initialize ticket keys
4638 */
4639static int ssl_ticket_keys_init( ssl_context *ssl )
4640{
4641 int ret;
4642 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004643 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004644
4645 if( ssl->ticket_keys != NULL )
4646 return( 0 );
4647
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004648 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4649 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004650 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4651
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004652 aes_init( &tkeys->enc );
4653 aes_init( &tkeys->dec );
4654
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004655 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004656 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004657 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004658 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004659 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004660 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004661
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004662 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4663 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4664 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4665 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004666 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004667 polarssl_free( tkeys );
4668 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004669 }
4670
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004671 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004672 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004673 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004674 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004675 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004676 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004677
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004678 ssl->ticket_keys = tkeys;
4679
4680 return( 0 );
4681}
Paul Bakkera503a632013-08-14 13:48:06 +02004682#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004683
4684/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 * SSL set accessors
4686 */
4687void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4688{
4689 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004690
Paul Bakker606b4ba2013-08-14 16:52:14 +02004691#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004692 if( endpoint == SSL_IS_CLIENT )
4693 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004694#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004695}
4696
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004697int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004698{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004699#if defined(POLARSSL_SSL_PROTO_DTLS)
4700 if( transport == SSL_TRANSPORT_DATAGRAM )
4701 {
4702 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004703
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004704 ssl->out_hdr = ssl->out_buf;
4705 ssl->out_ctr = ssl->out_buf + 3;
4706 ssl->out_len = ssl->out_buf + 11;
4707 ssl->out_iv = ssl->out_buf + 13;
4708 ssl->out_msg = ssl->out_buf + 13;
4709
4710 ssl->in_hdr = ssl->in_buf;
4711 ssl->in_ctr = ssl->in_buf + 3;
4712 ssl->in_len = ssl->in_buf + 11;
4713 ssl->in_iv = ssl->in_buf + 13;
4714 ssl->in_msg = ssl->in_buf + 13;
4715
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004716 /* DTLS starts with TLS1.1 */
4717 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4718 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004719
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004720 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4721 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4722
4723 return( 0 );
4724 }
4725#endif
4726
4727 if( transport == SSL_TRANSPORT_STREAM )
4728 {
4729 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004730
4731 ssl->out_ctr = ssl->out_buf;
4732 ssl->out_hdr = ssl->out_buf + 8;
4733 ssl->out_len = ssl->out_buf + 11;
4734 ssl->out_iv = ssl->out_buf + 13;
4735 ssl->out_msg = ssl->out_buf + 13;
4736
4737 ssl->in_ctr = ssl->in_buf;
4738 ssl->in_hdr = ssl->in_buf + 8;
4739 ssl->in_len = ssl->in_buf + 11;
4740 ssl->in_iv = ssl->in_buf + 13;
4741 ssl->in_msg = ssl->in_buf + 13;
4742
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004743 return( 0 );
4744 }
4745
4746 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004747}
4748
Paul Bakker5121ce52009-01-03 21:22:43 +00004749void ssl_set_authmode( ssl_context *ssl, int authmode )
4750{
4751 ssl->authmode = authmode;
4752}
4753
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004754#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004755void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004756 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004757 void *p_vrfy )
4758{
4759 ssl->f_vrfy = f_vrfy;
4760 ssl->p_vrfy = p_vrfy;
4761}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004762#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004763
Paul Bakker5121ce52009-01-03 21:22:43 +00004764void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004765 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004766 void *p_rng )
4767{
4768 ssl->f_rng = f_rng;
4769 ssl->p_rng = p_rng;
4770}
4771
4772void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004773 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004774 void *p_dbg )
4775{
4776 ssl->f_dbg = f_dbg;
4777 ssl->p_dbg = p_dbg;
4778}
4779
4780void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004781 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004782 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004783{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004784 if( p_recv != p_send )
4785 {
4786 ssl->f_recv = NULL;
4787 ssl->f_send = NULL;
4788 ssl->p_bio = NULL;
4789 return;
4790 }
4791
Paul Bakker5121ce52009-01-03 21:22:43 +00004792 ssl->f_recv = f_recv;
4793 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004794 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004795}
4796
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004797void ssl_set_bio_timeout( ssl_context *ssl,
4798 void *p_bio,
4799 int (*f_send)(void *, const unsigned char *, size_t),
4800 int (*f_recv)(void *, unsigned char *, size_t),
4801 int (*f_recv_timeout)(void *, unsigned char *, size_t, unsigned char),
4802 unsigned char timeout )
4803{
4804 ssl->p_bio = p_bio;
4805 ssl->f_send = f_send;
4806 ssl->f_recv = f_recv;
4807 ssl->f_recv_timeout = f_recv_timeout;
4808 ssl->timeout = timeout;
4809}
4810
Paul Bakker0a597072012-09-25 21:55:46 +00004811void ssl_set_session_cache( ssl_context *ssl,
4812 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4813 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004814{
Paul Bakker0a597072012-09-25 21:55:46 +00004815 ssl->f_get_cache = f_get_cache;
4816 ssl->p_get_cache = p_get_cache;
4817 ssl->f_set_cache = f_set_cache;
4818 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004819}
4820
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004821int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004822{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004823 int ret;
4824
4825 if( ssl == NULL ||
4826 session == NULL ||
4827 ssl->session_negotiate == NULL ||
4828 ssl->endpoint != SSL_IS_CLIENT )
4829 {
4830 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4831 }
4832
4833 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4834 return( ret );
4835
Paul Bakker0a597072012-09-25 21:55:46 +00004836 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004837
4838 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004839}
4840
Paul Bakkerb68cad62012-08-23 08:34:18 +00004841void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004842{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004843 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4844 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4845 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4846 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4847}
4848
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004849void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4850 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004851 int major, int minor )
4852{
4853 if( major != SSL_MAJOR_VERSION_3 )
4854 return;
4855
4856 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4857 return;
4858
4859 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004860}
4861
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004862#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004863/* Add a new (empty) key_cert entry an return a pointer to it */
4864static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4865{
4866 ssl_key_cert *key_cert, *last;
4867
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004868 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
4869 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004870 return( NULL );
4871
4872 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4873
4874 /* Append the new key_cert to the (possibly empty) current list */
4875 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004876 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004877 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004878 if( ssl->handshake != NULL )
4879 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004880 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004881 else
4882 {
4883 last = ssl->key_cert;
4884 while( last->next != NULL )
4885 last = last->next;
4886 last->next = key_cert;
4887 }
4888
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004889 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004890}
4891
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004892void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004893 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004894{
4895 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004896 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 ssl->peer_cn = peer_cn;
4898}
4899
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004900int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004901 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004903 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4904
4905 if( key_cert == NULL )
4906 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4907
4908 key_cert->cert = own_cert;
4909 key_cert->key = pk_key;
4910
4911 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912}
4913
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004914#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004915int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004916 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004917{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004918 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004919 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004920
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004921 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004922 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4923
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004924 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4925 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004926 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004927
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004928 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004929
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004930 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004931 if( ret != 0 )
4932 return( ret );
4933
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004934 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004935 return( ret );
4936
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004937 key_cert->cert = own_cert;
4938 key_cert->key_own_alloc = 1;
4939
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004940 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004941}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004942#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004943
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004944int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004945 void *rsa_key,
4946 rsa_decrypt_func rsa_decrypt,
4947 rsa_sign_func rsa_sign,
4948 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004949{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004950 int ret;
4951 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004952
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004953 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004954 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4955
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004956 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4957 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004958 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004959
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004960 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004961
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004962 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4963 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4964 return( ret );
4965
4966 key_cert->cert = own_cert;
4967 key_cert->key_own_alloc = 1;
4968
4969 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004970}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004971#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004972
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004973#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004974int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4975 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004976{
Paul Bakker6db455e2013-09-18 17:29:31 +02004977 if( psk == NULL || psk_identity == NULL )
4978 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4979
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004980 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4982
Paul Bakker6db455e2013-09-18 17:29:31 +02004983 if( ssl->psk != NULL )
4984 {
4985 polarssl_free( ssl->psk );
4986 polarssl_free( ssl->psk_identity );
4987 }
4988
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004989 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004990 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004991
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004992 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004993 ssl->psk_identity = (unsigned char *)
4994 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004995
4996 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4997 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4998
4999 memcpy( ssl->psk, psk, ssl->psk_len );
5000 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005001
5002 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005003}
5004
5005void ssl_set_psk_cb( ssl_context *ssl,
5006 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5007 size_t),
5008 void *p_psk )
5009{
5010 ssl->f_psk = f_psk;
5011 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005012}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005013#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005014
Paul Bakker48916f92012-09-16 19:57:18 +00005015#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005016int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005017{
5018 int ret;
5019
Paul Bakker48916f92012-09-16 19:57:18 +00005020 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005021 {
5022 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5023 return( ret );
5024 }
5025
Paul Bakker48916f92012-09-16 19:57:18 +00005026 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005027 {
5028 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5029 return( ret );
5030 }
5031
5032 return( 0 );
5033}
5034
Paul Bakker1b57b062011-01-06 15:48:19 +00005035int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5036{
5037 int ret;
5038
Paul Bakker66d5d072014-06-17 16:39:18 +02005039 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005040 {
5041 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5042 return( ret );
5043 }
5044
Paul Bakker66d5d072014-06-17 16:39:18 +02005045 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005046 {
5047 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5048 return( ret );
5049 }
5050
5051 return( 0 );
5052}
Paul Bakker48916f92012-09-16 19:57:18 +00005053#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005054
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005055#if defined(POLARSSL_SSL_SET_CURVES)
5056/*
5057 * Set the allowed elliptic curves
5058 */
5059void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5060{
5061 ssl->curve_list = curve_list;
5062}
5063#endif
5064
Paul Bakker0be444a2013-08-27 21:55:01 +02005065#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005066int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005067{
5068 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005069 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005070
5071 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005072
5073 if( ssl->hostname_len + 1 == 0 )
5074 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5075
Paul Bakker6e339b52013-07-03 13:37:05 +02005076 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005077
Paul Bakkerb15b8512012-01-13 13:44:06 +00005078 if( ssl->hostname == NULL )
5079 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5080
Paul Bakker3c2122f2013-06-24 19:03:14 +02005081 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005082 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005083
Paul Bakker40ea7de2009-05-03 10:18:48 +00005084 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005085
5086 return( 0 );
5087}
5088
Paul Bakker5701cdc2012-09-27 21:49:42 +00005089void ssl_set_sni( ssl_context *ssl,
5090 int (*f_sni)(void *, ssl_context *,
5091 const unsigned char *, size_t),
5092 void *p_sni )
5093{
5094 ssl->f_sni = f_sni;
5095 ssl->p_sni = p_sni;
5096}
Paul Bakker0be444a2013-08-27 21:55:01 +02005097#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005098
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005099#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005100int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005101{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005102 size_t cur_len, tot_len;
5103 const char **p;
5104
5105 /*
5106 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5107 * truncated". Check lengths now rather than later.
5108 */
5109 tot_len = 0;
5110 for( p = protos; *p != NULL; p++ )
5111 {
5112 cur_len = strlen( *p );
5113 tot_len += cur_len;
5114
5115 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5116 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5117 }
5118
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005119 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005120
5121 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005122}
5123
5124const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5125{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005126 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005127}
5128#endif /* POLARSSL_SSL_ALPN */
5129
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005130static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005131{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005132 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5133 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5134 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5135 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005136 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005137 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005138 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005139
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005140 return( 0 );
5141}
5142
5143int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5144{
5145 if( ssl_check_version( ssl, major, minor ) != 0 )
5146 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5147
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005148 ssl->max_major_ver = major;
5149 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005150
5151 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005152}
5153
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005154int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005155{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005156 if( ssl_check_version( ssl, major, minor ) != 0 )
5157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005158
5159 ssl->min_major_ver = major;
5160 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005161
5162 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005163}
5164
Paul Bakker05decb22013-08-15 13:33:48 +02005165#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005166int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5167{
Paul Bakker77e257e2013-12-16 15:29:52 +01005168 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005169 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005170 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005171 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005172 }
5173
5174 ssl->mfl_code = mfl_code;
5175
5176 return( 0 );
5177}
Paul Bakker05decb22013-08-15 13:33:48 +02005178#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005179
Paul Bakker1f2bc622013-08-15 13:45:55 +02005180#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005181int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005182{
5183 if( ssl->endpoint != SSL_IS_CLIENT )
5184 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5185
Paul Bakker8c1ede62013-07-19 14:14:37 +02005186 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005187
5188 return( 0 );
5189}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005190#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005191
Paul Bakker48916f92012-09-16 19:57:18 +00005192void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5193{
5194 ssl->disable_renegotiation = renegotiation;
5195}
5196
5197void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5198{
5199 ssl->allow_legacy_renegotiation = allow_legacy;
5200}
5201
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005202void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5203{
5204 ssl->renego_max_records = max_records;
5205}
5206
Paul Bakkera503a632013-08-14 13:48:06 +02005207#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005208int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5209{
5210 ssl->session_tickets = use_tickets;
5211
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005212 if( ssl->endpoint == SSL_IS_CLIENT )
5213 return( 0 );
5214
5215 if( ssl->f_rng == NULL )
5216 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5217
5218 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005219}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005220
5221void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5222{
5223 ssl->ticket_lifetime = lifetime;
5224}
Paul Bakkera503a632013-08-14 13:48:06 +02005225#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005226
Paul Bakker5121ce52009-01-03 21:22:43 +00005227/*
5228 * SSL get accessors
5229 */
Paul Bakker23986e52011-04-24 08:57:21 +00005230size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005231{
5232 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5233}
5234
Paul Bakkerff60ee62010-03-16 21:09:09 +00005235int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005236{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005237 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005238}
5239
Paul Bakkere3166ce2011-01-27 17:40:50 +00005240const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005241{
Paul Bakker926c8e42013-03-06 10:23:34 +01005242 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005243 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005244
Paul Bakkere3166ce2011-01-27 17:40:50 +00005245 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005246}
5247
Paul Bakker43ca69c2011-01-15 17:35:19 +00005248const char *ssl_get_version( const ssl_context *ssl )
5249{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005250#if defined(POLARSSL_SSL_PROTO_DTLS)
5251 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5252 {
5253 switch( ssl->minor_ver )
5254 {
5255 case SSL_MINOR_VERSION_2:
5256 return( "DTLSv1.0" );
5257
5258 case SSL_MINOR_VERSION_3:
5259 return( "DTLSv1.2" );
5260
5261 default:
5262 return( "unknown (DTLS)" );
5263 }
5264 }
5265#endif
5266
Paul Bakker43ca69c2011-01-15 17:35:19 +00005267 switch( ssl->minor_ver )
5268 {
5269 case SSL_MINOR_VERSION_0:
5270 return( "SSLv3.0" );
5271
5272 case SSL_MINOR_VERSION_1:
5273 return( "TLSv1.0" );
5274
5275 case SSL_MINOR_VERSION_2:
5276 return( "TLSv1.1" );
5277
Paul Bakker1ef83d62012-04-11 12:09:53 +00005278 case SSL_MINOR_VERSION_3:
5279 return( "TLSv1.2" );
5280
Paul Bakker43ca69c2011-01-15 17:35:19 +00005281 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005282 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005283 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005284}
5285
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005286#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005287const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005288{
5289 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005290 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005291
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005292 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005293}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005294#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005295
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005296int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5297{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005298 if( ssl == NULL ||
5299 dst == NULL ||
5300 ssl->session == NULL ||
5301 ssl->endpoint != SSL_IS_CLIENT )
5302 {
5303 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5304 }
5305
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005306 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005307}
5308
Paul Bakker5121ce52009-01-03 21:22:43 +00005309/*
Paul Bakker1961b702013-01-25 14:49:24 +01005310 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005311 */
Paul Bakker1961b702013-01-25 14:49:24 +01005312int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005313{
Paul Bakker40e46942009-01-03 21:51:57 +00005314 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005315
Paul Bakker40e46942009-01-03 21:51:57 +00005316#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005317 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005318 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005319#endif
5320
Paul Bakker40e46942009-01-03 21:51:57 +00005321#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005322 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005323 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005324#endif
5325
Paul Bakker1961b702013-01-25 14:49:24 +01005326 return( ret );
5327}
5328
5329/*
5330 * Perform the SSL handshake
5331 */
5332int ssl_handshake( ssl_context *ssl )
5333{
5334 int ret = 0;
5335
5336 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5337
5338 while( ssl->state != SSL_HANDSHAKE_OVER )
5339 {
5340 ret = ssl_handshake_step( ssl );
5341
5342 if( ret != 0 )
5343 break;
5344 }
5345
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5347
5348 return( ret );
5349}
5350
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005351#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005352/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005353 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005354 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005355static int ssl_write_hello_request( ssl_context *ssl )
5356{
5357 int ret;
5358
5359 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5360
5361 ssl->out_msglen = 4;
5362 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5363 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5364
5365 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5366 {
5367 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5368 return( ret );
5369 }
5370
5371 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5372
5373 return( 0 );
5374}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005375#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005376
5377/*
5378 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005379 * - any side: calling ssl_renegotiate(),
5380 * - client: receiving a HelloRequest during ssl_read(),
5381 * - server: receiving any handshake message on server during ssl_read() after
5382 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005383 * If the handshake doesn't complete due to waiting for I/O, it will continue
5384 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005385 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005386static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005387{
5388 int ret;
5389
5390 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5391
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005392 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5393 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005394
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005395 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5396 * the ServerHello will have message_seq = 1" */
5397#if defined(POLARSSL_SSL_PROTO_DTLS)
5398 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005399 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5400 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005401 if( ssl->endpoint == SSL_IS_SERVER )
5402 ssl->handshake->out_msg_seq = 1;
5403 else
5404 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005405 }
5406#endif
5407
Paul Bakker48916f92012-09-16 19:57:18 +00005408 ssl->state = SSL_HELLO_REQUEST;
5409 ssl->renegotiation = SSL_RENEGOTIATION;
5410
Paul Bakker48916f92012-09-16 19:57:18 +00005411 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5412 {
5413 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5414 return( ret );
5415 }
5416
5417 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5418
5419 return( 0 );
5420}
5421
5422/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005423 * Renegotiate current connection on client,
5424 * or request renegotiation on server
5425 */
5426int ssl_renegotiate( ssl_context *ssl )
5427{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005428 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005429
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005430#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005431 /* On server, just send the request */
5432 if( ssl->endpoint == SSL_IS_SERVER )
5433 {
5434 if( ssl->state != SSL_HANDSHAKE_OVER )
5435 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5436
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005437 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5438
5439 /* Did we already try/start sending HelloRequest? */
5440 if( ssl->out_left != 0 )
5441 return( ssl_flush_output( ssl ) );
5442
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005443 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005444 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005445#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005446
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005447#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005448 /*
5449 * On client, either start the renegotiation process or,
5450 * if already in progress, continue the handshake
5451 */
5452 if( ssl->renegotiation != SSL_RENEGOTIATION )
5453 {
5454 if( ssl->state != SSL_HANDSHAKE_OVER )
5455 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5456
5457 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5458 {
5459 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5460 return( ret );
5461 }
5462 }
5463 else
5464 {
5465 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5466 {
5467 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5468 return( ret );
5469 }
5470 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005471#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005472
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005473 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005474}
5475
5476/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 * Receive application data decrypted from the SSL layer
5478 */
Paul Bakker23986e52011-04-24 08:57:21 +00005479int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005481 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005482 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005483
5484 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5485
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005486#if defined(POLARSSL_SSL_PROTO_DTLS)
5487 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5488 ssl->handshake != NULL &&
5489 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5490 {
5491 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5492 return( ret );
5493
5494 if( ( ret = ssl_resend( ssl ) ) != 0 )
5495 return( ret );
5496 }
5497#endif
5498
Paul Bakker5121ce52009-01-03 21:22:43 +00005499 if( ssl->state != SSL_HANDSHAKE_OVER )
5500 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005501 ret = ssl_handshake( ssl );
5502 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5503 {
5504 record_read = 1;
5505 }
5506 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005507 {
5508 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5509 return( ret );
5510 }
5511 }
5512
5513 if( ssl->in_offt == NULL )
5514 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005515 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005516 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005517 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5518 {
5519 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5520 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005521
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005522 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5523 return( ret );
5524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005525 }
5526
5527 if( ssl->in_msglen == 0 &&
5528 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5529 {
5530 /*
5531 * OpenSSL sends empty messages to randomize the IV
5532 */
5533 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5534 {
Paul Bakker831a7552011-05-18 13:32:51 +00005535 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5536 return( 0 );
5537
Paul Bakker5121ce52009-01-03 21:22:43 +00005538 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5539 return( ret );
5540 }
5541 }
5542
Paul Bakker48916f92012-09-16 19:57:18 +00005543 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5544 {
5545 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5546
5547 if( ssl->endpoint == SSL_IS_CLIENT &&
5548 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005549 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005550 {
5551 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005552
5553 /* With DTLS, drop the packet (probably from last handshake) */
5554#if defined(POLARSSL_SSL_PROTO_DTLS)
5555 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5556 return( POLARSSL_ERR_NET_WANT_READ );
5557#endif
5558 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5559 }
5560
5561 if( ssl->endpoint == SSL_IS_SERVER &&
5562 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5563 {
5564 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5565
5566 /* With DTLS, drop the packet (probably from last handshake) */
5567#if defined(POLARSSL_SSL_PROTO_DTLS)
5568 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5569 return( POLARSSL_ERR_NET_WANT_READ );
5570#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005571 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5572 }
5573
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005574 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5575 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005576 ssl->allow_legacy_renegotiation ==
5577 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005578 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005579 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005580
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005581#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005582 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005583 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005584 /*
5585 * SSLv3 does not have a "no_renegotiation" alert
5586 */
5587 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5588 return( ret );
5589 }
5590 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005591#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005592#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5593 defined(POLARSSL_SSL_PROTO_TLS1_2)
5594 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005595 {
5596 if( ( ret = ssl_send_alert_message( ssl,
5597 SSL_ALERT_LEVEL_WARNING,
5598 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5599 {
5600 return( ret );
5601 }
Paul Bakker48916f92012-09-16 19:57:18 +00005602 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005603 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005604#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5605 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005606 {
5607 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005608 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005609 }
Paul Bakker48916f92012-09-16 19:57:18 +00005610 }
5611 else
5612 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005613#if defined(POLARSSL_SSL_PROTO_DTLS)
5614 /* DTLS clients need to know renego is server-initiated */
5615 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5616 ssl->endpoint == SSL_IS_CLIENT )
5617 {
5618 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5619 }
5620#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005621 ret = ssl_start_renegotiation( ssl );
5622 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5623 {
5624 record_read = 1;
5625 }
5626 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005627 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005628 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005629 return( ret );
5630 }
Paul Bakker48916f92012-09-16 19:57:18 +00005631 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005632
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005633 /* If a non-handshake record was read during renego, fallthrough,
5634 * else tell the user they should call ssl_read() again */
5635 if( ! record_read )
5636 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005637 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005638 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5639 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005640 ssl->renego_records_seen++;
5641
5642 if( ssl->renego_max_records >= 0 &&
5643 ssl->renego_records_seen > ssl->renego_max_records )
5644 {
5645 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5646 "but not honored by client" ) );
5647 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5648 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005649 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005650
5651 /* Fatal and closure alerts handled by ssl_read_record() */
5652 if( ssl->in_msgtype == SSL_MSG_ALERT )
5653 {
5654 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5655 return( POLARSSL_ERR_NET_WANT_READ );
5656 }
5657
5658 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005659 {
5660 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005661 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005662 }
5663
5664 ssl->in_offt = ssl->in_msg;
5665 }
5666
5667 n = ( len < ssl->in_msglen )
5668 ? len : ssl->in_msglen;
5669
5670 memcpy( buf, ssl->in_offt, n );
5671 ssl->in_msglen -= n;
5672
5673 if( ssl->in_msglen == 0 )
5674 /* all bytes consumed */
5675 ssl->in_offt = NULL;
5676 else
5677 /* more data available */
5678 ssl->in_offt += n;
5679
5680 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5681
Paul Bakker23986e52011-04-24 08:57:21 +00005682 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005683}
5684
5685/*
5686 * Send application data to be encrypted by the SSL layer
5687 */
Paul Bakker23986e52011-04-24 08:57:21 +00005688int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005689{
Paul Bakker23986e52011-04-24 08:57:21 +00005690 int ret;
5691 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005692 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005693
5694 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5695
5696 if( ssl->state != SSL_HANDSHAKE_OVER )
5697 {
5698 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5699 {
5700 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5701 return( ret );
5702 }
5703 }
5704
Paul Bakker05decb22013-08-15 13:33:48 +02005705#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005706 /*
5707 * Assume mfl_code is correct since it was checked when set
5708 */
5709 max_len = mfl_code_to_length[ssl->mfl_code];
5710
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005711 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005712 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005713 */
5714 if( ssl->session_out != NULL &&
5715 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5716 {
5717 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5718 }
Paul Bakker05decb22013-08-15 13:33:48 +02005719#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005720
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005721 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005722
Paul Bakker5121ce52009-01-03 21:22:43 +00005723 if( ssl->out_left != 0 )
5724 {
5725 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5726 {
5727 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5728 return( ret );
5729 }
5730 }
Paul Bakker887bd502011-06-08 13:10:54 +00005731 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005732 {
Paul Bakker887bd502011-06-08 13:10:54 +00005733 ssl->out_msglen = n;
5734 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5735 memcpy( ssl->out_msg, buf, n );
5736
5737 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5738 {
5739 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5740 return( ret );
5741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 }
5743
5744 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5745
Paul Bakker23986e52011-04-24 08:57:21 +00005746 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005747}
5748
5749/*
5750 * Notify the peer that the connection is being closed
5751 */
5752int ssl_close_notify( ssl_context *ssl )
5753{
5754 int ret;
5755
5756 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5757
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005758 if( ssl->out_left != 0 )
5759 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005760
5761 if( ssl->state == SSL_HANDSHAKE_OVER )
5762 {
Paul Bakker48916f92012-09-16 19:57:18 +00005763 if( ( ret = ssl_send_alert_message( ssl,
5764 SSL_ALERT_LEVEL_WARNING,
5765 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005766 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005767 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005768 return( ret );
5769 }
5770 }
5771
5772 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5773
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005774 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005775}
5776
Paul Bakker48916f92012-09-16 19:57:18 +00005777void ssl_transform_free( ssl_transform *transform )
5778{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005779 if( transform == NULL )
5780 return;
5781
Paul Bakker48916f92012-09-16 19:57:18 +00005782#if defined(POLARSSL_ZLIB_SUPPORT)
5783 deflateEnd( &transform->ctx_deflate );
5784 inflateEnd( &transform->ctx_inflate );
5785#endif
5786
Paul Bakker84bbeb52014-07-01 14:53:22 +02005787 cipher_free( &transform->cipher_ctx_enc );
5788 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005789
Paul Bakker84bbeb52014-07-01 14:53:22 +02005790 md_free( &transform->md_ctx_enc );
5791 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005792
Paul Bakker34617722014-06-13 17:20:13 +02005793 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005794}
5795
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005796#if defined(POLARSSL_X509_CRT_PARSE_C)
5797static void ssl_key_cert_free( ssl_key_cert *key_cert )
5798{
5799 ssl_key_cert *cur = key_cert, *next;
5800
5801 while( cur != NULL )
5802 {
5803 next = cur->next;
5804
5805 if( cur->key_own_alloc )
5806 {
5807 pk_free( cur->key );
5808 polarssl_free( cur->key );
5809 }
5810 polarssl_free( cur );
5811
5812 cur = next;
5813 }
5814}
5815#endif /* POLARSSL_X509_CRT_PARSE_C */
5816
Paul Bakker48916f92012-09-16 19:57:18 +00005817void ssl_handshake_free( ssl_handshake_params *handshake )
5818{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005819 if( handshake == NULL )
5820 return;
5821
Paul Bakker48916f92012-09-16 19:57:18 +00005822#if defined(POLARSSL_DHM_C)
5823 dhm_free( &handshake->dhm_ctx );
5824#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005825#if defined(POLARSSL_ECDH_C)
5826 ecdh_free( &handshake->ecdh_ctx );
5827#endif
5828
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005829#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005830 /* explicit void pointer cast for buggy MS compiler */
5831 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005832#endif
5833
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005834#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5835 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5836 /*
5837 * Free only the linked list wrapper, not the keys themselves
5838 * since the belong to the SNI callback
5839 */
5840 if( handshake->sni_key_cert != NULL )
5841 {
5842 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5843
5844 while( cur != NULL )
5845 {
5846 next = cur->next;
5847 polarssl_free( cur );
5848 cur = next;
5849 }
5850 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005851#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005852
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005853#if defined(POLARSSL_SSL_PROTO_DTLS)
5854 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02005855 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02005856 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005857#endif
5858
Paul Bakker34617722014-06-13 17:20:13 +02005859 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005860}
5861
5862void ssl_session_free( ssl_session *session )
5863{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005864 if( session == NULL )
5865 return;
5866
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005867#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005868 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005869 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005870 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005871 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005872 }
Paul Bakkered27a042013-04-18 22:46:23 +02005873#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005874
Paul Bakkera503a632013-08-14 13:48:06 +02005875#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005876 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005877#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005878
Paul Bakker34617722014-06-13 17:20:13 +02005879 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005880}
5881
Paul Bakker5121ce52009-01-03 21:22:43 +00005882/*
5883 * Free an SSL context
5884 */
5885void ssl_free( ssl_context *ssl )
5886{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005887 if( ssl == NULL )
5888 return;
5889
Paul Bakker5121ce52009-01-03 21:22:43 +00005890 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5891
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005892 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005893 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005894 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
5895 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005896 }
5897
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005898 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005899 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005900 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
5901 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 }
5903
Paul Bakker16770332013-10-11 09:59:44 +02005904#if defined(POLARSSL_ZLIB_SUPPORT)
5905 if( ssl->compress_buf != NULL )
5906 {
Paul Bakker34617722014-06-13 17:20:13 +02005907 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005908 polarssl_free( ssl->compress_buf );
5909 }
5910#endif
5911
Paul Bakker40e46942009-01-03 21:51:57 +00005912#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005913 mpi_free( &ssl->dhm_P );
5914 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005915#endif
5916
Paul Bakker48916f92012-09-16 19:57:18 +00005917 if( ssl->transform )
5918 {
5919 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005920 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005921 }
5922
5923 if( ssl->handshake )
5924 {
5925 ssl_handshake_free( ssl->handshake );
5926 ssl_transform_free( ssl->transform_negotiate );
5927 ssl_session_free( ssl->session_negotiate );
5928
Paul Bakker6e339b52013-07-03 13:37:05 +02005929 polarssl_free( ssl->handshake );
5930 polarssl_free( ssl->transform_negotiate );
5931 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005932 }
5933
Paul Bakkerc0463502013-02-14 11:19:38 +01005934 if( ssl->session )
5935 {
5936 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005937 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005938 }
5939
Paul Bakkera503a632013-08-14 13:48:06 +02005940#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005941 if( ssl->ticket_keys )
5942 {
5943 ssl_ticket_keys_free( ssl->ticket_keys );
5944 polarssl_free( ssl->ticket_keys );
5945 }
Paul Bakkera503a632013-08-14 13:48:06 +02005946#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005947
Paul Bakker0be444a2013-08-27 21:55:01 +02005948#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005949 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005950 {
Paul Bakker34617722014-06-13 17:20:13 +02005951 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005952 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005953 ssl->hostname_len = 0;
5954 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005955#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005956
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005957#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005958 if( ssl->psk != NULL )
5959 {
Paul Bakker34617722014-06-13 17:20:13 +02005960 polarssl_zeroize( ssl->psk, ssl->psk_len );
5961 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005962 polarssl_free( ssl->psk );
5963 polarssl_free( ssl->psk_identity );
5964 ssl->psk_len = 0;
5965 ssl->psk_identity_len = 0;
5966 }
5967#endif
5968
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005969#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005970 ssl_key_cert_free( ssl->key_cert );
5971#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005972
Paul Bakker05ef8352012-05-08 09:17:57 +00005973#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5974 if( ssl_hw_record_finish != NULL )
5975 {
5976 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5977 ssl_hw_record_finish( ssl );
5978 }
5979#endif
5980
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005981#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005982 polarssl_free( ssl->cli_id );
5983#endif
5984
Paul Bakker5121ce52009-01-03 21:22:43 +00005985 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005986
Paul Bakker86f04f42013-02-14 11:20:09 +01005987 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005988 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005989}
5990
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005991#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005992/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005993 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005994 */
5995unsigned char ssl_sig_from_pk( pk_context *pk )
5996{
5997#if defined(POLARSSL_RSA_C)
5998 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5999 return( SSL_SIG_RSA );
6000#endif
6001#if defined(POLARSSL_ECDSA_C)
6002 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6003 return( SSL_SIG_ECDSA );
6004#endif
6005 return( SSL_SIG_ANON );
6006}
6007
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006008pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6009{
6010 switch( sig )
6011 {
6012#if defined(POLARSSL_RSA_C)
6013 case SSL_SIG_RSA:
6014 return( POLARSSL_PK_RSA );
6015#endif
6016#if defined(POLARSSL_ECDSA_C)
6017 case SSL_SIG_ECDSA:
6018 return( POLARSSL_PK_ECDSA );
6019#endif
6020 default:
6021 return( POLARSSL_PK_NONE );
6022 }
6023}
Paul Bakker9af723c2014-05-01 13:03:14 +02006024#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006025
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006026/*
6027 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6028 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006029md_type_t ssl_md_alg_from_hash( unsigned char hash )
6030{
6031 switch( hash )
6032 {
6033#if defined(POLARSSL_MD5_C)
6034 case SSL_HASH_MD5:
6035 return( POLARSSL_MD_MD5 );
6036#endif
6037#if defined(POLARSSL_SHA1_C)
6038 case SSL_HASH_SHA1:
6039 return( POLARSSL_MD_SHA1 );
6040#endif
6041#if defined(POLARSSL_SHA256_C)
6042 case SSL_HASH_SHA224:
6043 return( POLARSSL_MD_SHA224 );
6044 case SSL_HASH_SHA256:
6045 return( POLARSSL_MD_SHA256 );
6046#endif
6047#if defined(POLARSSL_SHA512_C)
6048 case SSL_HASH_SHA384:
6049 return( POLARSSL_MD_SHA384 );
6050 case SSL_HASH_SHA512:
6051 return( POLARSSL_MD_SHA512 );
6052#endif
6053 default:
6054 return( POLARSSL_MD_NONE );
6055 }
6056}
6057
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006058#if defined(POLARSSL_SSL_SET_CURVES)
6059/*
6060 * Check is a curve proposed by the peer is in our list.
6061 * Return 1 if we're willing to use it, 0 otherwise.
6062 */
6063int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6064{
6065 const ecp_group_id *gid;
6066
6067 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6068 if( *gid == grp_id )
6069 return( 1 );
6070
6071 return( 0 );
6072}
Paul Bakker9af723c2014-05-01 13:03:14 +02006073#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006074
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006075#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006076int ssl_check_cert_usage( const x509_crt *cert,
6077 const ssl_ciphersuite_t *ciphersuite,
6078 int cert_endpoint )
6079{
6080#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6081 int usage = 0;
6082#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006083#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6084 const char *ext_oid;
6085 size_t ext_len;
6086#endif
6087
6088#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6089 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6090 ((void) cert);
6091 ((void) cert_endpoint);
6092#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006093
6094#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6095 if( cert_endpoint == SSL_IS_SERVER )
6096 {
6097 /* Server part of the key exchange */
6098 switch( ciphersuite->key_exchange )
6099 {
6100 case POLARSSL_KEY_EXCHANGE_RSA:
6101 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6102 usage = KU_KEY_ENCIPHERMENT;
6103 break;
6104
6105 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6106 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6107 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6108 usage = KU_DIGITAL_SIGNATURE;
6109 break;
6110
6111 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6112 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6113 usage = KU_KEY_AGREEMENT;
6114 break;
6115
6116 /* Don't use default: we want warnings when adding new values */
6117 case POLARSSL_KEY_EXCHANGE_NONE:
6118 case POLARSSL_KEY_EXCHANGE_PSK:
6119 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6120 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6121 usage = 0;
6122 }
6123 }
6124 else
6125 {
6126 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6127 usage = KU_DIGITAL_SIGNATURE;
6128 }
6129
6130 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6131 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006132#else
6133 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006134#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6135
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006136#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6137 if( cert_endpoint == SSL_IS_SERVER )
6138 {
6139 ext_oid = OID_SERVER_AUTH;
6140 ext_len = OID_SIZE( OID_SERVER_AUTH );
6141 }
6142 else
6143 {
6144 ext_oid = OID_CLIENT_AUTH;
6145 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6146 }
6147
6148 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6149 return( -1 );
6150#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6151
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006152 return( 0 );
6153}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006154#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006155
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006156/*
6157 * Convert version numbers to/from wire format
6158 * and, for DTLS, to/from TLS equivalent.
6159 *
6160 * For TLS this is the identity.
6161 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6162 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6163 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6164 */
6165void ssl_write_version( int major, int minor, int transport,
6166 unsigned char ver[2] )
6167{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006168#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006169 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006170 {
6171 if( minor == SSL_MINOR_VERSION_2 )
6172 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6173
6174 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6175 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6176 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006177 else
6178#else
6179 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006180#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006181 {
6182 ver[0] = (unsigned char) major;
6183 ver[1] = (unsigned char) minor;
6184 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006185}
6186
6187void ssl_read_version( int *major, int *minor, int transport,
6188 const unsigned char ver[2] )
6189{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006190#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006191 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006192 {
6193 *major = 255 - ver[0] + 2;
6194 *minor = 255 - ver[1] + 1;
6195
6196 if( *minor == SSL_MINOR_VERSION_1 )
6197 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6198 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006199 else
6200#else
6201 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006202#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006203 {
6204 *major = ver[0];
6205 *minor = ver[1];
6206 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006207}
6208
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006209#endif /* POLARSSL_SSL_TLS_C */