blob: ab6b67e460dcdbcc354a5080cb7da6e7dbb5f765 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020082#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083/*
84 * Start a timer.
85 * Passing millisecs = 0 cancels a running timer.
86 * The timer is already running iff time_limit != 0.
87 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020088static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
90 ssl->time_limit = millisecs;
91 get_timer( &ssl->time_info, 1 );
92}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020097static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
99 if( ssl->time_limit != 0 &&
100 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
101 {
102 return( -1 );
103 }
104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200108/*
109 * Double the retransmit timeout value, within the allowed range,
110 * returning -1 if the maximum value has already been reached.
111 */
112static int ssl_double_retransmit_timeout( ssl_context *ssl )
113{
114 uint32_t new_timeout;
115
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200116 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200117 return( -1 );
118
119 new_timeout = 2 * ssl->handshake->retransmit_timeout;
120
121 /* Avoid arithmetic overflow and range overflow */
122 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200125 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200126 }
127
128 ssl->handshake->retransmit_timeout = new_timeout;
129 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
130 ssl->handshake->retransmit_timeout ) );
131
132 return( 0 );
133}
134
135static void ssl_reset_retransmit_timeout( ssl_context *ssl )
136{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200137 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200138 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
139 ssl->handshake->retransmit_timeout ) );
140}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200141#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200142
Paul Bakker05decb22013-08-15 13:33:48 +0200143#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200144/*
145 * Convert max_fragment_length codes to length.
146 * RFC 6066 says:
147 * enum{
148 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
149 * } MaxFragmentLength;
150 * and we add 0 -> extension unused
151 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200152static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200153{
154 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
155 512, /* SSL_MAX_FRAG_LEN_512 */
156 1024, /* SSL_MAX_FRAG_LEN_1024 */
157 2048, /* SSL_MAX_FRAG_LEN_2048 */
158 4096, /* SSL_MAX_FRAG_LEN_4096 */
159};
Paul Bakker05decb22013-08-15 13:33:48 +0200160#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200161
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
163{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200164 ssl_session_free( dst );
165 memcpy( dst, src, sizeof( ssl_session ) );
166
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200168 if( src->peer_cert != NULL )
169 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200170 int ret;
171
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
173 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200174 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
175
Paul Bakkerb6b09562013-09-18 14:17:41 +0200176 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200177
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200178 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
179 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 {
181 polarssl_free( dst->peer_cert );
182 dst->peer_cert = NULL;
183 return( ret );
184 }
185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187
Paul Bakkera503a632013-08-14 13:48:06 +0200188#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200189 if( src->ticket != NULL )
190 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200191 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
192 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200193 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
194
195 memcpy( dst->ticket, src->ticket, src->ticket_len );
196 }
Paul Bakkera503a632013-08-14 13:48:06 +0200197#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200198
199 return( 0 );
200}
201
Paul Bakker05ef8352012-05-08 09:17:57 +0000202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200203int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200204 const unsigned char *key_enc, const unsigned char *key_dec,
205 size_t keylen,
206 const unsigned char *iv_enc, const unsigned char *iv_dec,
207 size_t ivlen,
208 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200209 size_t maclen ) = NULL;
210int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
211int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
213int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
214int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200215#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000216
Paul Bakker5121ce52009-01-03 21:22:43 +0000217/*
218 * Key material generation
219 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200220#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200221static int ssl3_prf( const unsigned char *secret, size_t slen,
222 const char *label,
223 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000224 unsigned char *dstbuf, size_t dlen )
225{
226 size_t i;
227 md5_context md5;
228 sha1_context sha1;
229 unsigned char padding[16];
230 unsigned char sha1sum[20];
231 ((void)label);
232
Paul Bakker5b4af392014-06-26 12:09:34 +0200233 md5_init( &md5 );
234 sha1_init( &sha1 );
235
Paul Bakker5f70b252012-09-13 14:23:06 +0000236 /*
237 * SSLv3:
238 * block =
239 * MD5( secret + SHA1( 'A' + secret + random ) ) +
240 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
241 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
242 * ...
243 */
244 for( i = 0; i < dlen / 16; i++ )
245 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000247
248 sha1_starts( &sha1 );
249 sha1_update( &sha1, padding, 1 + i );
250 sha1_update( &sha1, secret, slen );
251 sha1_update( &sha1, random, rlen );
252 sha1_finish( &sha1, sha1sum );
253
254 md5_starts( &md5 );
255 md5_update( &md5, secret, slen );
256 md5_update( &md5, sha1sum, 20 );
257 md5_finish( &md5, dstbuf + i * 16 );
258 }
259
Paul Bakker5b4af392014-06-26 12:09:34 +0200260 md5_free( &md5 );
261 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000262
Paul Bakker34617722014-06-13 17:20:13 +0200263 polarssl_zeroize( padding, sizeof( padding ) );
264 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000265
266 return( 0 );
267}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000269
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200270#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200271static int tls1_prf( const unsigned char *secret, size_t slen,
272 const char *label,
273 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000274 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000275{
Paul Bakker23986e52011-04-24 08:57:21 +0000276 size_t nb, hs;
277 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200278 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char tmp[128];
280 unsigned char h_i[20];
281
282 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 hs = ( slen + 1 ) / 2;
286 S1 = secret;
287 S2 = secret + slen - hs;
288
289 nb = strlen( label );
290 memcpy( tmp + 20, label, nb );
291 memcpy( tmp + 20 + nb, random, rlen );
292 nb += rlen;
293
294 /*
295 * First compute P_md5(secret,label+random)[0..dlen]
296 */
297 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
298
299 for( i = 0; i < dlen; i += 16 )
300 {
301 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
302 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
303
304 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
305
306 for( j = 0; j < k; j++ )
307 dstbuf[i + j] = h_i[j];
308 }
309
310 /*
311 * XOR out with P_sha1(secret,label+random)[0..dlen]
312 */
313 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
314
315 for( i = 0; i < dlen; i += 20 )
316 {
317 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
318 sha1_hmac( S2, hs, tmp, 20, tmp );
319
320 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
321
322 for( j = 0; j < k; j++ )
323 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
324 }
325
Paul Bakker34617722014-06-13 17:20:13 +0200326 polarssl_zeroize( tmp, sizeof( tmp ) );
327 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 return( 0 );
330}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200331#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#if defined(POLARSSL_SSL_PROTO_TLS1_2)
334#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200335static int tls_prf_sha256( const unsigned char *secret, size_t slen,
336 const char *label,
337 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000338 unsigned char *dstbuf, size_t dlen )
339{
340 size_t nb;
341 size_t i, j, k;
342 unsigned char tmp[128];
343 unsigned char h_i[32];
344
345 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
346 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
347
348 nb = strlen( label );
349 memcpy( tmp + 32, label, nb );
350 memcpy( tmp + 32 + nb, random, rlen );
351 nb += rlen;
352
353 /*
354 * Compute P_<hash>(secret, label + random)[0..dlen]
355 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200356 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000357
358 for( i = 0; i < dlen; i += 32 )
359 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200360 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
361 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000362
363 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
364
365 for( j = 0; j < k; j++ )
366 dstbuf[i + j] = h_i[j];
367 }
368
Paul Bakker34617722014-06-13 17:20:13 +0200369 polarssl_zeroize( tmp, sizeof( tmp ) );
370 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000371
372 return( 0 );
373}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200374#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker9e36f042013-06-30 14:34:05 +0200376#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200377static int tls_prf_sha384( const unsigned char *secret, size_t slen,
378 const char *label,
379 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000380 unsigned char *dstbuf, size_t dlen )
381{
382 size_t nb;
383 size_t i, j, k;
384 unsigned char tmp[128];
385 unsigned char h_i[48];
386
387 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389
390 nb = strlen( label );
391 memcpy( tmp + 48, label, nb );
392 memcpy( tmp + 48 + nb, random, rlen );
393 nb += rlen;
394
395 /*
396 * Compute P_<hash>(secret, label + random)[0..dlen]
397 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200398 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399
400 for( i = 0; i < dlen; i += 48 )
401 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200402 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
403 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404
405 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
406
407 for( j = 0; j < k; j++ )
408 dstbuf[i + j] = h_i[j];
409 }
410
Paul Bakker34617722014-06-13 17:20:13 +0200411 polarssl_zeroize( tmp, sizeof( tmp ) );
412 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413
414 return( 0 );
415}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200416#endif /* POLARSSL_SHA512_C */
417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418
Paul Bakker66d5d072014-06-17 16:39:18 +0200419static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200420
421#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
422 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200423static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#endif
Paul Bakker380da532012-04-18 16:10:25 +0000425
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200427static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
428static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#endif
430
431#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200432static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
433static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435
436#if defined(POLARSSL_SSL_PROTO_TLS1_2)
437#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200438static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
439static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
440static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200441#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100442
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200444static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
445static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
446static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100447#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200448#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
Paul Bakker5121ce52009-01-03 21:22:43 +0000450int ssl_derive_keys( ssl_context *ssl )
451{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200452 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 unsigned char keyblk[256];
455 unsigned char *key1;
456 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100457 unsigned char *mac_enc;
458 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200459 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100460 const cipher_info_t *cipher_info;
461 const md_info_t *md_info;
462
Paul Bakker48916f92012-09-16 19:57:18 +0000463 ssl_session *session = ssl->session_negotiate;
464 ssl_transform *transform = ssl->transform_negotiate;
465 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
468
Paul Bakker68884e32013-01-07 18:20:04 +0100469 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
470 if( cipher_info == NULL )
471 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200472 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100473 transform->ciphersuite_info->cipher ) );
474 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
475 }
476
477 md_info = md_info_from_type( transform->ciphersuite_info->mac );
478 if( md_info == NULL )
479 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200480 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100481 transform->ciphersuite_info->mac ) );
482 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
483 }
484
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000486 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200488#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000489 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000490 {
Paul Bakker48916f92012-09-16 19:57:18 +0000491 handshake->tls_prf = ssl3_prf;
492 handshake->calc_verify = ssl_calc_verify_ssl;
493 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200495 else
496#endif
497#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
498 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000499 {
Paul Bakker48916f92012-09-16 19:57:18 +0000500 handshake->tls_prf = tls1_prf;
501 handshake->calc_verify = ssl_calc_verify_tls;
502 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000503 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200504 else
505#endif
506#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200507#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200508 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
509 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000510 {
Paul Bakker48916f92012-09-16 19:57:18 +0000511 handshake->tls_prf = tls_prf_sha384;
512 handshake->calc_verify = ssl_calc_verify_tls_sha384;
513 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000514 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000515 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516#endif
517#if defined(POLARSSL_SHA256_C)
518 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000519 {
Paul Bakker48916f92012-09-16 19:57:18 +0000520 handshake->tls_prf = tls_prf_sha256;
521 handshake->calc_verify = ssl_calc_verify_tls_sha256;
522 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000523 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200524 else
525#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200526#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200527 {
528 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200529 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200530 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000531
532 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * SSLv3:
534 * master =
535 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
536 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
537 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200538 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * master = PRF( premaster, "master secret", randbytes )[0..47]
541 */
Paul Bakker0a597072012-09-25 21:55:46 +0000542 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 {
Paul Bakker48916f92012-09-16 19:57:18 +0000544 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
545 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200547#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
548 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200549 {
550 unsigned char session_hash[48];
551 size_t hash_len;
552
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200553 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200554
555 ssl->handshake->calc_verify( ssl, session_hash );
556
557#if defined(POLARSSL_SSL_PROTO_TLS1_2)
558 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
559 {
560#if defined(POLARSSL_SHA512_C)
561 if( ssl->transform_negotiate->ciphersuite_info->mac ==
562 POLARSSL_MD_SHA384 )
563 {
564 hash_len = 48;
565 }
566 else
567#endif
568 hash_len = 32;
569 }
570 else
571#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
572 hash_len = 36;
573
574 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
575
576 handshake->tls_prf( handshake->premaster, handshake->pmslen,
577 "extended master secret",
578 session_hash, hash_len, session->master, 48 );
579
580 }
581 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200582#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000583 handshake->tls_prf( handshake->premaster, handshake->pmslen,
584 "master secret",
585 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200587
Paul Bakker34617722014-06-13 17:20:13 +0200588 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 }
590 else
591 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
592
593 /*
594 * Swap the client and server random values.
595 */
Paul Bakker48916f92012-09-16 19:57:18 +0000596 memcpy( tmp, handshake->randbytes, 64 );
597 memcpy( handshake->randbytes, tmp + 32, 32 );
598 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200599 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 /*
602 * SSLv3:
603 * key block =
604 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
605 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
606 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
607 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
608 * ...
609 *
610 * TLSv1:
611 * key block = PRF( master, "key expansion", randbytes )
612 */
Paul Bakker48916f92012-09-16 19:57:18 +0000613 handshake->tls_prf( session->master, 48, "key expansion",
614 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Paul Bakker48916f92012-09-16 19:57:18 +0000616 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
617 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
618 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
619 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
621
Paul Bakker34617722014-06-13 17:20:13 +0200622 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624 /*
625 * Determine the appropriate key, IV and MAC length.
626 */
Paul Bakker68884e32013-01-07 18:20:04 +0100627
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200628 transform->keylen = cipher_info->key_length / 8;
629
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200630 if( cipher_info->mode == POLARSSL_MODE_GCM ||
631 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200633 transform->maclen = 0;
634
Paul Bakker68884e32013-01-07 18:20:04 +0100635 transform->ivlen = 12;
636 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200637
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200638 /* Minimum length is expicit IV + tag */
639 transform->minlen = transform->ivlen - transform->fixed_ivlen
640 + ( transform->ciphersuite_info->flags &
641 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100642 }
643 else
644 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200645 int ret;
646
647 /* Initialize HMAC contexts */
648 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
649 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100650 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200651 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
652 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100653 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200655 /* Get MAC length */
656 transform->maclen = md_get_size( md_info );
657
658#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
659 /*
660 * If HMAC is to be truncated, we shall keep the leftmost bytes,
661 * (rfc 6066 page 13 or rfc 2104 section 4),
662 * so we only need to adjust the length here.
663 */
664 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
665 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
666#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
667
668 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100669 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200671 /* Minimum length */
672 if( cipher_info->mode == POLARSSL_MODE_STREAM )
673 transform->minlen = transform->maclen;
674 else
Paul Bakker68884e32013-01-07 18:20:04 +0100675 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200676 /*
677 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100678 * 1. if EtM is in use: one block plus MAC
679 * otherwise: * first multiple of blocklen greater than maclen
680 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200681 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100682#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
683 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
684 {
685 transform->minlen = transform->maclen
686 + cipher_info->block_size;
687 }
688 else
689#endif
690 {
691 transform->minlen = transform->maclen
692 + cipher_info->block_size
693 - transform->maclen % cipher_info->block_size;
694 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200695
696#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
697 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
698 ssl->minor_ver == SSL_MINOR_VERSION_1 )
699 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100700 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200701#endif
702#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
703 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
704 ssl->minor_ver == SSL_MINOR_VERSION_3 )
705 {
706 transform->minlen += transform->ivlen;
707 }
708 else
709#endif
710 {
711 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
712 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
713 }
Paul Bakker68884e32013-01-07 18:20:04 +0100714 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 }
716
717 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000718 transform->keylen, transform->minlen, transform->ivlen,
719 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721 /*
722 * Finally setup the cipher contexts, IVs and MAC secrets.
723 */
724 if( ssl->endpoint == SSL_IS_CLIENT )
725 {
Paul Bakker48916f92012-09-16 19:57:18 +0000726 key1 = keyblk + transform->maclen * 2;
727 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
Paul Bakker68884e32013-01-07 18:20:04 +0100729 mac_enc = keyblk;
730 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000732 /*
733 * This is not used in TLS v1.1.
734 */
Paul Bakker48916f92012-09-16 19:57:18 +0000735 iv_copy_len = ( transform->fixed_ivlen ) ?
736 transform->fixed_ivlen : transform->ivlen;
737 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
738 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000739 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 }
741 else
742 {
Paul Bakker48916f92012-09-16 19:57:18 +0000743 key1 = keyblk + transform->maclen * 2 + transform->keylen;
744 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000745
Paul Bakker68884e32013-01-07 18:20:04 +0100746 mac_enc = keyblk + transform->maclen;
747 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000748
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000749 /*
750 * This is not used in TLS v1.1.
751 */
Paul Bakker48916f92012-09-16 19:57:18 +0000752 iv_copy_len = ( transform->fixed_ivlen ) ?
753 transform->fixed_ivlen : transform->ivlen;
754 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
755 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000756 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 }
758
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200759#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100760 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
761 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100762 if( transform->maclen > sizeof transform->mac_enc )
763 {
764 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200765 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100766 }
767
Paul Bakker68884e32013-01-07 18:20:04 +0100768 memcpy( transform->mac_enc, mac_enc, transform->maclen );
769 memcpy( transform->mac_dec, mac_dec, transform->maclen );
770 }
771 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200772#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
774 defined(POLARSSL_SSL_PROTO_TLS1_2)
775 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100776 {
777 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
778 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
779 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200780 else
781#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200782 {
783 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200784 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200785 }
Paul Bakker68884e32013-01-07 18:20:04 +0100786
Paul Bakker05ef8352012-05-08 09:17:57 +0000787#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200788 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000789 {
790 int ret = 0;
791
792 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
793
Paul Bakker07eb38b2012-12-19 14:42:06 +0100794 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
795 transform->iv_enc, transform->iv_dec,
796 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100797 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100798 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000799 {
800 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200801 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000802 }
803 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200804#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000805
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200806 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
807 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000808 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200809 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
810 return( ret );
811 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200812
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200813 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
814 cipher_info ) ) != 0 )
815 {
816 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
817 return( ret );
818 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200819
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200820 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
821 cipher_info->key_length,
822 POLARSSL_ENCRYPT ) ) != 0 )
823 {
824 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
825 return( ret );
826 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200827
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200828 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
829 cipher_info->key_length,
830 POLARSSL_DECRYPT ) ) != 0 )
831 {
832 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
833 return( ret );
834 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200835
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200836#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200837 if( cipher_info->mode == POLARSSL_MODE_CBC )
838 {
839 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
840 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200841 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200842 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
843 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200844 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200845
846 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
847 POLARSSL_PADDING_NONE ) ) != 0 )
848 {
849 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
850 return( ret );
851 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200853#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Paul Bakker34617722014-06-13 17:20:13 +0200855 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000856
Paul Bakker2770fbd2012-07-03 13:30:23 +0000857#if defined(POLARSSL_ZLIB_SUPPORT)
858 // Initialize compression
859 //
Paul Bakker48916f92012-09-16 19:57:18 +0000860 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000861 {
Paul Bakker16770332013-10-11 09:59:44 +0200862 if( ssl->compress_buf == NULL )
863 {
864 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
865 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
866 if( ssl->compress_buf == NULL )
867 {
868 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
869 SSL_BUFFER_LEN ) );
870 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
871 }
872 }
873
Paul Bakker2770fbd2012-07-03 13:30:23 +0000874 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
875
Paul Bakker48916f92012-09-16 19:57:18 +0000876 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
877 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000878
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200879 if( deflateInit( &transform->ctx_deflate,
880 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000881 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000882 {
883 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
884 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
885 }
886 }
887#endif /* POLARSSL_ZLIB_SUPPORT */
888
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
890
891 return( 0 );
892}
893
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200894#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000895void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000896{
897 md5_context md5;
898 sha1_context sha1;
899 unsigned char pad_1[48];
900 unsigned char pad_2[48];
901
Paul Bakker380da532012-04-18 16:10:25 +0000902 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000903
Paul Bakker48916f92012-09-16 19:57:18 +0000904 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
905 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000906
Paul Bakker380da532012-04-18 16:10:25 +0000907 memset( pad_1, 0x36, 48 );
908 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909
Paul Bakker48916f92012-09-16 19:57:18 +0000910 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000911 md5_update( &md5, pad_1, 48 );
912 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker380da532012-04-18 16:10:25 +0000914 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000915 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000916 md5_update( &md5, pad_2, 48 );
917 md5_update( &md5, hash, 16 );
918 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakker48916f92012-09-16 19:57:18 +0000920 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000921 sha1_update( &sha1, pad_1, 40 );
922 sha1_finish( &sha1, hash + 16 );
923
924 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000925 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000926 sha1_update( &sha1, pad_2, 40 );
927 sha1_update( &sha1, hash + 16, 20 );
928 sha1_finish( &sha1, hash + 16 );
929
930 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
931 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
932
Paul Bakker5b4af392014-06-26 12:09:34 +0200933 md5_free( &md5 );
934 sha1_free( &sha1 );
935
Paul Bakker380da532012-04-18 16:10:25 +0000936 return;
937}
Paul Bakker9af723c2014-05-01 13:03:14 +0200938#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000939
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200940#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000941void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
942{
943 md5_context md5;
944 sha1_context sha1;
945
946 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
947
Paul Bakker48916f92012-09-16 19:57:18 +0000948 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
949 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000950
Paul Bakker48916f92012-09-16 19:57:18 +0000951 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000952 sha1_finish( &sha1, hash + 16 );
953
954 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
955 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
956
Paul Bakker5b4af392014-06-26 12:09:34 +0200957 md5_free( &md5 );
958 sha1_free( &sha1 );
959
Paul Bakker380da532012-04-18 16:10:25 +0000960 return;
961}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200962#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000963
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200964#if defined(POLARSSL_SSL_PROTO_TLS1_2)
965#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000966void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
967{
Paul Bakker9e36f042013-06-30 14:34:05 +0200968 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000969
970 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
971
Paul Bakker9e36f042013-06-30 14:34:05 +0200972 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
973 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000974
975 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
976 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
977
Paul Bakker5b4af392014-06-26 12:09:34 +0200978 sha256_free( &sha256 );
979
Paul Bakker380da532012-04-18 16:10:25 +0000980 return;
981}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200982#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000983
Paul Bakker9e36f042013-06-30 14:34:05 +0200984#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000985void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
986{
Paul Bakker9e36f042013-06-30 14:34:05 +0200987 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000988
989 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
990
Paul Bakker9e36f042013-06-30 14:34:05 +0200991 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
992 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000993
Paul Bakkerca4ab492012-04-18 14:23:57 +0000994 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
996
Paul Bakker5b4af392014-06-26 12:09:34 +0200997 sha512_free( &sha512 );
998
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 return;
1000}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001001#endif /* POLARSSL_SHA512_C */
1002#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001004#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001005int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1006{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001007 unsigned char *p = ssl->handshake->premaster;
1008 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1009
1010 /*
1011 * PMS = struct {
1012 * opaque other_secret<0..2^16-1>;
1013 * opaque psk<0..2^16-1>;
1014 * };
1015 * with "other_secret" depending on the particular key exchange
1016 */
1017#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1018 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1019 {
1020 if( end - p < 2 + (int) ssl->psk_len )
1021 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1022
1023 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1024 *(p++) = (unsigned char)( ssl->psk_len );
1025 p += ssl->psk_len;
1026 }
1027 else
1028#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001029#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1030 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1031 {
1032 /*
1033 * other_secret already set by the ClientKeyExchange message,
1034 * and is 48 bytes long
1035 */
1036 *p++ = 0;
1037 *p++ = 48;
1038 p += 48;
1039 }
1040 else
1041#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001042#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1043 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1044 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001045 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001046 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001047
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001048 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001049 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001050 p + 2, &len,
1051 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001052 {
1053 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1054 return( ret );
1055 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001056 *(p++) = (unsigned char)( len >> 8 );
1057 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001058 p += len;
1059
1060 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1061 }
1062 else
1063#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1064#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1065 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1066 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001067 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001068 size_t zlen;
1069
1070 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001071 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001072 ssl->f_rng, ssl->p_rng ) ) != 0 )
1073 {
1074 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1075 return( ret );
1076 }
1077
1078 *(p++) = (unsigned char)( zlen >> 8 );
1079 *(p++) = (unsigned char)( zlen );
1080 p += zlen;
1081
1082 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1083 }
1084 else
1085#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1086 {
1087 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001088 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001089 }
1090
1091 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001092 if( end - p < 2 + (int) ssl->psk_len )
1093 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1094
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001095 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1096 *(p++) = (unsigned char)( ssl->psk_len );
1097 memcpy( p, ssl->psk, ssl->psk_len );
1098 p += ssl->psk_len;
1099
1100 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1101
1102 return( 0 );
1103}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001104#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001105
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001106#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001107/*
1108 * SSLv3.0 MAC functions
1109 */
Paul Bakker68884e32013-01-07 18:20:04 +01001110static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1111 unsigned char *buf, size_t len,
1112 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001113{
1114 unsigned char header[11];
1115 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001116 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001117 int md_size = md_get_size( md_ctx->md_info );
1118 int md_type = md_get_type( md_ctx->md_info );
1119
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001120 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001121 if( md_type == POLARSSL_MD_MD5 )
1122 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001123 else
Paul Bakker68884e32013-01-07 18:20:04 +01001124 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126 memcpy( header, ctr, 8 );
1127 header[ 8] = (unsigned char) type;
1128 header[ 9] = (unsigned char)( len >> 8 );
1129 header[10] = (unsigned char)( len );
1130
Paul Bakker68884e32013-01-07 18:20:04 +01001131 memset( padding, 0x36, padlen );
1132 md_starts( md_ctx );
1133 md_update( md_ctx, secret, md_size );
1134 md_update( md_ctx, padding, padlen );
1135 md_update( md_ctx, header, 11 );
1136 md_update( md_ctx, buf, len );
1137 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001138
Paul Bakker68884e32013-01-07 18:20:04 +01001139 memset( padding, 0x5C, padlen );
1140 md_starts( md_ctx );
1141 md_update( md_ctx, secret, md_size );
1142 md_update( md_ctx, padding, padlen );
1143 md_update( md_ctx, buf + len, md_size );
1144 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001145}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001146#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001147
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001148#define MAC_NONE 0
1149#define MAC_PLAINTEXT 1
1150#define MAC_CIPHERTEXT 2
1151
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001152#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1153 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1154 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1155#define POLARSSL_SOME_MODES_USE_MAC
1156#endif
1157
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001158/*
1159 * Is MAC applied on ciphertext, cleartext or not at all?
1160 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001161#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001162static char ssl_get_mac_order( ssl_context *ssl,
1163 const ssl_session *session,
1164 cipher_mode_t mode )
1165{
1166#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1167 if( mode == POLARSSL_MODE_STREAM )
1168 return( MAC_PLAINTEXT );
1169#endif
1170
1171#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1172 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1173 if( mode == POLARSSL_MODE_CBC )
1174 {
1175#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1176 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1177 {
1178 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1179 return( MAC_CIPHERTEXT );
1180 }
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001181#else
1182 ((void) ssl);
1183 ((void) session);
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001184#endif
1185
1186 return( MAC_PLAINTEXT );
1187 }
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001188#else
Manuel Pégourié-Gonnard9d7821d2014-11-06 01:19:52 +01001189 ((void) ssl);
1190 ((void) session);
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001191#endif
Manuel Pégourié-Gonnard9d7821d2014-11-06 01:19:52 +01001192
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001193 return( MAC_NONE );
1194}
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001195#endif /* POLARSSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197/*
1198 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001199 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001200static int ssl_encrypt_buf( ssl_context *ssl )
1201{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001202 const cipher_mode_t mode = cipher_get_cipher_mode(
1203 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
1205 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1206
1207 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001208 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001210#if defined(POLARSSL_SOME_MODES_USE_MAC)
1211 if( ssl_get_mac_order( ssl, ssl->session_out, mode ) == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001213#if defined(POLARSSL_SSL_PROTO_SSL3)
1214 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1215 {
1216 ssl_mac( &ssl->transform_out->md_ctx_enc,
1217 ssl->transform_out->mac_enc,
1218 ssl->out_msg, ssl->out_msglen,
1219 ssl->out_ctr, ssl->out_msgtype );
1220 }
1221 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001222#endif
1223#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001224 defined(POLARSSL_SSL_PROTO_TLS1_2)
1225 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1226 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001227 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1228 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1229 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001230 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1231 ssl->out_msg, ssl->out_msglen );
1232 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1233 ssl->out_msg + ssl->out_msglen );
1234 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1235 }
1236 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001237#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001238 {
1239 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001240 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001241 }
1242
1243 SSL_DEBUG_BUF( 4, "computed mac",
1244 ssl->out_msg + ssl->out_msglen,
1245 ssl->transform_out->maclen );
1246
1247 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001248 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001249#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001251 /*
1252 * Encrypt
1253 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001254#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001255 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001257 int ret;
1258 size_t olen = 0;
1259
Paul Bakker5121ce52009-01-03 21:22:43 +00001260 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1261 "including %d bytes of padding",
1262 ssl->out_msglen, 0 ) );
1263
1264 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1265 ssl->out_msg, ssl->out_msglen );
1266
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001267 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001268 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001269 ssl->transform_out->ivlen,
1270 ssl->out_msg, ssl->out_msglen,
1271 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001272 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001273 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001274 return( ret );
1275 }
1276
1277 if( ssl->out_msglen != olen )
1278 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001279 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001280 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001281 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 }
Paul Bakker68884e32013-01-07 18:20:04 +01001283 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001284#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001285#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1286 if( mode == POLARSSL_MODE_GCM ||
1287 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001288 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001289 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001290 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001291 unsigned char *enc_msg;
1292 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001293 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1294 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001295
Paul Bakkerca4ab492012-04-18 14:23:57 +00001296 memcpy( add_data, ssl->out_ctr, 8 );
1297 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001298 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1299 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001300 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1301 add_data[12] = ssl->out_msglen & 0xFF;
1302
1303 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1304 add_data, 13 );
1305
Paul Bakker68884e32013-01-07 18:20:04 +01001306 /*
1307 * Generate IV
1308 */
1309 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001310 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1311 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001312 if( ret != 0 )
1313 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001314
Paul Bakker68884e32013-01-07 18:20:04 +01001315 memcpy( ssl->out_iv,
1316 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1317 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001318
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001319 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001320 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001321
Paul Bakker68884e32013-01-07 18:20:04 +01001322 /*
1323 * Fix pointer positions and message length with added IV
1324 */
1325 enc_msg = ssl->out_msg;
1326 enc_msglen = ssl->out_msglen;
1327 ssl->out_msglen += ssl->transform_out->ivlen -
1328 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001329
Paul Bakker68884e32013-01-07 18:20:04 +01001330 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1331 "including %d bytes of padding",
1332 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001333
Paul Bakker68884e32013-01-07 18:20:04 +01001334 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1335 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001336
Paul Bakker68884e32013-01-07 18:20:04 +01001337 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001338 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001339 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001340 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1341 ssl->transform_out->iv_enc,
1342 ssl->transform_out->ivlen,
1343 add_data, 13,
1344 enc_msg, enc_msglen,
1345 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001346 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001347 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001348 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001349 return( ret );
1350 }
1351
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001352 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001353 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001354 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001355 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001356 }
1357
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001358 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001360 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001363#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001364#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1365 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001366 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001368 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001369 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001370 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001371
Paul Bakker48916f92012-09-16 19:57:18 +00001372 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1373 ssl->transform_out->ivlen;
1374 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 padlen = 0;
1376
1377 for( i = 0; i <= padlen; i++ )
1378 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1379
1380 ssl->out_msglen += padlen + 1;
1381
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001382 enc_msglen = ssl->out_msglen;
1383 enc_msg = ssl->out_msg;
1384
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001385#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001386 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001387 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1388 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001389 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001390 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391 {
1392 /*
1393 * Generate IV
1394 */
Paul Bakker48916f92012-09-16 19:57:18 +00001395 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1396 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001397 if( ret != 0 )
1398 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001399
Paul Bakker92be97b2013-01-02 17:30:03 +01001400 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001401 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001402
1403 /*
1404 * Fix pointer positions and message length with added IV
1405 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001406 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001407 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001408 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001409 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001410#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001411
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001413 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001414 ssl->out_msglen, ssl->transform_out->ivlen,
1415 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001416
1417 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001418 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001420 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001421 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001422 ssl->transform_out->ivlen,
1423 enc_msg, enc_msglen,
1424 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001425 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001426 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001427 return( ret );
1428 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001429
Paul Bakkercca5b812013-08-31 17:40:26 +02001430 if( enc_msglen != olen )
1431 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001432 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001433 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001434 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001435
1436#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001437 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1438 {
1439 /*
1440 * Save IV in SSL3 and TLS1
1441 */
1442 memcpy( ssl->transform_out->iv_enc,
1443 ssl->transform_out->cipher_ctx_enc.iv,
1444 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001446#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001447
1448#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001449 if( ssl_get_mac_order( ssl, ssl->session_out, mode ) == MAC_CIPHERTEXT )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001450 {
1451 /*
1452 * MAC(MAC_write_key, seq_num +
1453 * TLSCipherText.type +
1454 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001455 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001456 * IV + // except for TLS 1.0
1457 * ENC(content + padding + padding_length));
1458 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001459 unsigned char pseudo_hdr[13];
1460
1461 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1462 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001463 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1464 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1465
1466 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001467
1468 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1469 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1470 ssl->out_iv, ssl->out_msglen );
1471 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1472 ssl->out_iv + ssl->out_msglen );
1473 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1474
1475 ssl->out_msglen += ssl->transform_out->maclen;
1476 }
1477#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001479 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001480#endif /* POLARSSL_CIPHER_MODE_CBC &&
1481 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001482 {
1483 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001484 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001485 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001486
1487 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1488
1489 return( 0 );
1490}
1491
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001492#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001493
Paul Bakker5121ce52009-01-03 21:22:43 +00001494static int ssl_decrypt_buf( ssl_context *ssl )
1495{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001496 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001497 const cipher_mode_t mode = cipher_get_cipher_mode(
1498 &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001499#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001500 size_t padlen = 0, correct = 1;
1501#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
1503 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1504
Paul Bakker48916f92012-09-16 19:57:18 +00001505 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 {
1507 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001508 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001509 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 }
1511
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001512#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001513 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001514 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001515 int ret;
1516 size_t olen = 0;
1517
Paul Bakker68884e32013-01-07 18:20:04 +01001518 padlen = 0;
1519
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001520 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001521 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001522 ssl->transform_in->ivlen,
1523 ssl->in_msg, ssl->in_msglen,
1524 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001525 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001526 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001527 return( ret );
1528 }
1529
1530 if( ssl->in_msglen != olen )
1531 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001532 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001533 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001535 }
Paul Bakker68884e32013-01-07 18:20:04 +01001536 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001537#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001538#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1539 if( mode == POLARSSL_MODE_GCM ||
1540 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001541 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001542 int ret;
1543 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001544 unsigned char *dec_msg;
1545 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001546 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001547 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1548 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001549 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1550 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001552 if( ssl->in_msglen < explicit_iv_len + taglen )
1553 {
1554 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1555 "+ taglen (%d)", ssl->in_msglen,
1556 explicit_iv_len, taglen ) );
1557 return( POLARSSL_ERR_SSL_INVALID_MAC );
1558 }
1559 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1560
Paul Bakker68884e32013-01-07 18:20:04 +01001561 dec_msg = ssl->in_msg;
1562 dec_msg_result = ssl->in_msg;
1563 ssl->in_msglen = dec_msglen;
1564
1565 memcpy( add_data, ssl->in_ctr, 8 );
1566 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001567 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1568 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001569 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1570 add_data[12] = ssl->in_msglen & 0xFF;
1571
1572 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1573 add_data, 13 );
1574
1575 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1576 ssl->in_iv,
1577 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1578
1579 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1580 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001581 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001582
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001583 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001584 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001585 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001586 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1587 ssl->transform_in->iv_dec,
1588 ssl->transform_in->ivlen,
1589 add_data, 13,
1590 dec_msg, dec_msglen,
1591 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001592 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001593 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001594 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1595
1596 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1597 return( POLARSSL_ERR_SSL_INVALID_MAC );
1598
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001599 return( ret );
1600 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001601
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001602 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001603 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001604 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001605 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001606 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001609#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001610#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1611 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001612 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 {
Paul Bakker45829992013-01-03 14:52:21 +01001614 /*
1615 * Decrypt and check the padding
1616 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001617 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618 unsigned char *dec_msg;
1619 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001620 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001621 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001622 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001623
Paul Bakker5121ce52009-01-03 21:22:43 +00001624 /*
Paul Bakker45829992013-01-03 14:52:21 +01001625 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001627#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001628 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1629 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001630#endif
Paul Bakker45829992013-01-03 14:52:21 +01001631
1632 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1633 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1634 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001635 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1636 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1637 ssl->transform_in->ivlen,
1638 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001639 return( POLARSSL_ERR_SSL_INVALID_MAC );
1640 }
1641
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 dec_msglen = ssl->in_msglen;
1643 dec_msg = ssl->in_msg;
1644 dec_msg_result = ssl->in_msg;
1645
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001646 /*
1647 * Authenticate before decrypt if enabled
1648 */
1649#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001650 if( ssl_get_mac_order( ssl, ssl->session_in, mode ) == MAC_CIPHERTEXT )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001651 {
1652 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001653 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001654
1655 dec_msglen -= ssl->transform_in->maclen;
1656 ssl->in_msglen -= ssl->transform_in->maclen;
1657
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001658 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1659 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1660 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1661 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1662
1663 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1664
1665 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001666 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1667 ssl->in_iv, ssl->in_msglen );
1668 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1669 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1670
1671 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1672 ssl->transform_in->maclen );
1673 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1674 ssl->transform_in->maclen );
1675
1676 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1677 ssl->transform_in->maclen ) != 0 )
1678 {
1679 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1680
1681 return( POLARSSL_ERR_SSL_INVALID_MAC );
1682 }
1683 }
1684#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1685
1686 /*
1687 * Check length sanity
1688 */
1689 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1690 {
1691 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1692 ssl->in_msglen, ssl->transform_in->ivlen ) );
1693 return( POLARSSL_ERR_SSL_INVALID_MAC );
1694 }
1695
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001696#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001697 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001698 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001699 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001700 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001701 {
Paul Bakker48916f92012-09-16 19:57:18 +00001702 dec_msglen -= ssl->transform_in->ivlen;
1703 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001704
Paul Bakker48916f92012-09-16 19:57:18 +00001705 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001706 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001707 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001708#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001709
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001710 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001711 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001712 ssl->transform_in->ivlen,
1713 dec_msg, dec_msglen,
1714 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001715 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001716 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001717 return( ret );
1718 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001719
Paul Bakkercca5b812013-08-31 17:40:26 +02001720 if( dec_msglen != olen )
1721 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001722 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001723 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001724 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001725
1726#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001727 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1728 {
1729 /*
1730 * Save IV in SSL3 and TLS1
1731 */
1732 memcpy( ssl->transform_in->iv_dec,
1733 ssl->transform_in->cipher_ctx_dec.iv,
1734 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001735 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001736#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
1738 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001739
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001740 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001741 ssl_get_mac_order( ssl, ssl->session_in, mode ) == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001742 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001743#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001744 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1745 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001746#endif
Paul Bakker45829992013-01-03 14:52:21 +01001747 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001748 correct = 0;
1749 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001751#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1753 {
Paul Bakker48916f92012-09-16 19:57:18 +00001754 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001755 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001756#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001757 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1758 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001759 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001760#endif
Paul Bakker45829992013-01-03 14:52:21 +01001761 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 }
1763 }
1764 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001765#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001766#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1767 defined(POLARSSL_SSL_PROTO_TLS1_2)
1768 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001769 {
1770 /*
Paul Bakker45829992013-01-03 14:52:21 +01001771 * TLSv1+: always check the padding up to the first failure
1772 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001774 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001775 size_t padding_idx = ssl->in_msglen - padlen - 1;
1776
Paul Bakker956c9e02013-12-19 14:42:28 +01001777 /*
1778 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001779 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001780 *
Paul Bakker61885c72014-04-25 12:59:03 +02001781 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1782 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001783 *
1784 * In both cases we reset padding_idx to a safe value (0) to
1785 * prevent out-of-buffer reads.
1786 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001787 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001788 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1789 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001790
1791 padding_idx *= correct;
1792
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001793 for( i = 1; i <= 256; i++ )
1794 {
1795 real_count &= ( i <= padlen );
1796 pad_count += real_count *
1797 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1798 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001799
1800 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001801
Paul Bakkerd66f0702013-01-31 16:57:45 +01001802#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001803 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001804 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001805#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001806 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001808 else
1809#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1810 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001811 {
1812 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001813 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001814 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001815
1816 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001817 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001818 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001819#endif /* POLARSSL_CIPHER_MODE_CBC &&
1820 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001821 {
1822 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001823 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
1826 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1827 ssl->in_msg, ssl->in_msglen );
1828
1829 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001830 * Authenticate if not done yet.
1831 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1834 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1835 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001836 if( ssl_get_mac_order( ssl, ssl->session_in, mode ) == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001838 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1839
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001840 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001842 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1843 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001845 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001847#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1849 {
1850 ssl_mac( &ssl->transform_in->md_ctx_dec,
1851 ssl->transform_in->mac_dec,
1852 ssl->in_msg, ssl->in_msglen,
1853 ssl->in_ctr, ssl->in_msgtype );
1854 }
1855 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001856#endif /* POLARSSL_SSL_PROTO_SSL3 */
1857#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001858 defined(POLARSSL_SSL_PROTO_TLS1_2)
1859 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1860 {
1861 /*
1862 * Process MAC and always update for padlen afterwards to make
1863 * total time independent of padlen
1864 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001865 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001866 *
1867 * Known timing attacks:
1868 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1869 *
1870 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1871 * correctly. (We round down instead of up, so -56 is the correct
1872 * value for our calculations instead of -55)
1873 */
1874 size_t j, extra_run = 0;
1875 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1876 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001877
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001878 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001879
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001880 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1881 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1882 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001883 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1884 ssl->in_msglen );
1885 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1886 ssl->in_msg + ssl->in_msglen );
1887 for( j = 0; j < extra_run; j++ )
1888 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001889
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001890 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1891 }
1892 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001893#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001894 POLARSSL_SSL_PROTO_TLS1_2 */
1895 {
1896 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001897 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001898 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001900 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1901 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1902 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001904 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001905 ssl->transform_in->maclen ) != 0 )
1906 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001907#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001908 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001909#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001910 correct = 0;
1911 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001913 /*
1914 * Finally check the correct flag
1915 */
1916 if( correct == 0 )
1917 return( POLARSSL_ERR_SSL_INVALID_MAC );
1918 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001919#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
1921 if( ssl->in_msglen == 0 )
1922 {
1923 ssl->nb_zero++;
1924
1925 /*
1926 * Three or more empty messages may be a DoS attack
1927 * (excessive CPU consumption).
1928 */
1929 if( ssl->nb_zero > 3 )
1930 {
1931 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1932 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001933 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 }
1935 }
1936 else
1937 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001938
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001939#if defined(POLARSSL_SSL_PROTO_DTLS)
1940 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001941 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001942 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001943 }
1944 else
1945#endif
1946 {
1947 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1948 if( ++ssl->in_ctr[i - 1] != 0 )
1949 break;
1950
1951 /* The loop goes to its end iff the counter is wrapping */
1952 if( i == ssl_ep_len( ssl ) )
1953 {
1954 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1955 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1956 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001957 }
1958
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1960
1961 return( 0 );
1962}
1963
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001964#undef MAC_NONE
1965#undef MAC_PLAINTEXT
1966#undef MAC_CIPHERTEXT
1967
Paul Bakker2770fbd2012-07-03 13:30:23 +00001968#if defined(POLARSSL_ZLIB_SUPPORT)
1969/*
1970 * Compression/decompression functions
1971 */
1972static int ssl_compress_buf( ssl_context *ssl )
1973{
1974 int ret;
1975 unsigned char *msg_post = ssl->out_msg;
1976 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001977 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978
1979 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1980
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001981 if( len_pre == 0 )
1982 return( 0 );
1983
Paul Bakker2770fbd2012-07-03 13:30:23 +00001984 memcpy( msg_pre, ssl->out_msg, len_pre );
1985
1986 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1987 ssl->out_msglen ) );
1988
1989 SSL_DEBUG_BUF( 4, "before compression: output payload",
1990 ssl->out_msg, ssl->out_msglen );
1991
Paul Bakker48916f92012-09-16 19:57:18 +00001992 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1993 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1994 ssl->transform_out->ctx_deflate.next_out = msg_post;
1995 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001996
Paul Bakker48916f92012-09-16 19:57:18 +00001997 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001998 if( ret != Z_OK )
1999 {
2000 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2001 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2002 }
2003
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002004 ssl->out_msglen = SSL_BUFFER_LEN -
2005 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002006
Paul Bakker2770fbd2012-07-03 13:30:23 +00002007 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2008 ssl->out_msglen ) );
2009
2010 SSL_DEBUG_BUF( 4, "after compression: output payload",
2011 ssl->out_msg, ssl->out_msglen );
2012
2013 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2014
2015 return( 0 );
2016}
2017
2018static int ssl_decompress_buf( ssl_context *ssl )
2019{
2020 int ret;
2021 unsigned char *msg_post = ssl->in_msg;
2022 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002023 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002024
2025 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2026
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002027 if( len_pre == 0 )
2028 return( 0 );
2029
Paul Bakker2770fbd2012-07-03 13:30:23 +00002030 memcpy( msg_pre, ssl->in_msg, len_pre );
2031
2032 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2033 ssl->in_msglen ) );
2034
2035 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2036 ssl->in_msg, ssl->in_msglen );
2037
Paul Bakker48916f92012-09-16 19:57:18 +00002038 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2039 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2040 ssl->transform_in->ctx_inflate.next_out = msg_post;
2041 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002042
Paul Bakker48916f92012-09-16 19:57:18 +00002043 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002044 if( ret != Z_OK )
2045 {
2046 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2047 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2048 }
2049
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002050 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2051 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002052
Paul Bakker2770fbd2012-07-03 13:30:23 +00002053 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2054 ssl->in_msglen ) );
2055
2056 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2057 ssl->in_msg, ssl->in_msglen );
2058
2059 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2060
2061 return( 0 );
2062}
2063#endif /* POLARSSL_ZLIB_SUPPORT */
2064
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002065#if defined(POLARSSL_SSL_SRV_C)
2066static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002067
2068#if defined(POLARSSL_SSL_PROTO_DTLS)
2069static int ssl_resend_hello_request( ssl_context *ssl )
2070{
2071 /* If renegotiation is not enforced, retransmit until we would reach max
2072 * timeout if we were using the usual handshake doubling scheme */
2073 if( ssl->renego_max_records < 0 )
2074 {
2075 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2076 unsigned char doublings = 1;
2077
2078 while( ratio != 0 )
2079 {
2080 ++doublings;
2081 ratio >>= 1;
2082 }
2083
2084 if( ++ssl->renego_records_seen > doublings )
2085 {
2086 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2087 return( 0 );
2088 }
2089 }
2090
2091 return( ssl_write_hello_request( ssl ) );
2092}
2093#endif
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002094#endif
2095
Paul Bakker5121ce52009-01-03 21:22:43 +00002096/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002097 * Fill the input message buffer by appending data to it.
2098 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002099 *
2100 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2101 * available (from this read and/or a previous one). Otherwise, an error code
2102 * is returned (possibly EOF or WANT_READ).
2103 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002104 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2105 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2106 * since we always read a whole datagram at once.
2107 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002108 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002109 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 */
Paul Bakker23986e52011-04-24 08:57:21 +00002111int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112{
Paul Bakker23986e52011-04-24 08:57:21 +00002113 int ret;
2114 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
2116 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2117
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002118 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2119 {
2120 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2121 "or ssl_set_bio_timeout()" ) );
2122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2123 }
2124
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002125 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002126 {
2127 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2128 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2129 }
2130
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002131#if defined(POLARSSL_SSL_PROTO_DTLS)
2132 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002134 uint32_t timeout;
2135
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002136 /*
2137 * The point is, we need to always read a full datagram at once, so we
2138 * sometimes read more then requested, and handle the additional data.
2139 * It could be the rest of the current record (while fetching the
2140 * header) and/or some other records in the same datagram.
2141 */
2142
2143 /*
2144 * Move to the next record in the already read datagram if applicable
2145 */
2146 if( ssl->next_record_offset != 0 )
2147 {
2148 if( ssl->in_left < ssl->next_record_offset )
2149 {
2150 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2151 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2152 }
2153
2154 ssl->in_left -= ssl->next_record_offset;
2155
2156 if( ssl->in_left != 0 )
2157 {
2158 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2159 ssl->next_record_offset ) );
2160 memmove( ssl->in_hdr,
2161 ssl->in_hdr + ssl->next_record_offset,
2162 ssl->in_left );
2163 }
2164
2165 ssl->next_record_offset = 0;
2166 }
2167
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2169 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002170
2171 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002172 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002173 */
2174 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002175 {
2176 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002177 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002178 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002179
2180 /*
2181 * A record can't be split accross datagrams. If we need to read but
2182 * are not at the beginning of a new record, the caller did something
2183 * wrong.
2184 */
2185 if( ssl->in_left != 0 )
2186 {
2187 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2188 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2189 }
2190
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002191 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002192
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002193 /*
2194 * Don't even try to read if time's out already.
2195 * This avoids by-passing the timer when repeatedly receiving messages
2196 * that will end up being dropped.
2197 */
2198 if( ssl_check_timer( ssl ) != 0 )
2199 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002200 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002201 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002202 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2203
2204 if( ssl->state != SSL_HANDSHAKE_OVER )
2205 timeout = ssl->handshake->retransmit_timeout;
2206 else
2207 timeout = ssl->read_timeout;
2208
2209 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2210
2211 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2212 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2213 timeout );
2214 else
2215 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2216
2217 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2218
2219 if( ret == 0 )
2220 return( POLARSSL_ERR_SSL_CONN_EOF );
2221 }
2222
2223 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2224 {
2225 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2226 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002227
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002228 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002229 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002230 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2231 {
2232 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2233 return( POLARSSL_ERR_NET_TIMEOUT );
2234 }
2235
2236 if( ( ret = ssl_resend( ssl ) ) != 0 )
2237 {
2238 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2239 return( ret );
2240 }
2241
2242 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002243 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002244#if defined(POLARSSL_SSL_SRV_C)
2245 else if( ssl->endpoint == SSL_IS_SERVER &&
2246 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2247 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002248 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002249 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002250 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002251 return( ret );
2252 }
2253
2254 return( POLARSSL_ERR_NET_WANT_READ );
2255 }
2256#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002257 }
2258
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 if( ret < 0 )
2260 return( ret );
2261
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002262 ssl->in_left = ret;
2263 }
2264 else
2265#endif
2266 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002267 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2268 ssl->in_left, nb_want ) );
2269
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002270 while( ssl->in_left < nb_want )
2271 {
2272 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002273 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002274
2275 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2276 ssl->in_left, nb_want ) );
2277 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2278
2279 if( ret == 0 )
2280 return( POLARSSL_ERR_SSL_CONN_EOF );
2281
2282 if( ret < 0 )
2283 return( ret );
2284
2285 ssl->in_left += ret;
2286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 }
2288
2289 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2290
2291 return( 0 );
2292}
2293
2294/*
2295 * Flush any data not yet written
2296 */
2297int ssl_flush_output( ssl_context *ssl )
2298{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002299 int ret;
2300 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
2302 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2303
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002304 if( ssl->f_send == NULL )
2305 {
2306 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2307 "or ssl_set_bio_timeout()" ) );
2308 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2309 }
2310
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002311 /* Avoid incrementing counter if data is flushed */
2312 if( ssl->out_left == 0 )
2313 {
2314 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2315 return( 0 );
2316 }
2317
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 while( ssl->out_left > 0 )
2319 {
2320 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002321 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002322
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002323 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2324 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002325 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002326
Paul Bakker5121ce52009-01-03 21:22:43 +00002327 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2328
2329 if( ret <= 0 )
2330 return( ret );
2331
2332 ssl->out_left -= ret;
2333 }
2334
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002335 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002336 if( ++ssl->out_ctr[i - 1] != 0 )
2337 break;
2338
2339 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002340 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002341 {
2342 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2343 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2344 }
2345
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2347
2348 return( 0 );
2349}
2350
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002351/*
2352 * Functions to handle the DTLS retransmission state machine
2353 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002354#if defined(POLARSSL_SSL_PROTO_DTLS)
2355/*
2356 * Append current handshake message to current outgoing flight
2357 */
2358static int ssl_flight_append( ssl_context *ssl )
2359{
2360 ssl_flight_item *msg;
2361
2362 /* Allocate space for current message */
2363 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2364 {
2365 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2366 sizeof( ssl_flight_item ) ) );
2367 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2368 }
2369
2370 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2371 {
2372 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002373 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002374 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2375 }
2376
2377 /* Copy current handshake message with headers */
2378 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2379 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002380 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002381 msg->next = NULL;
2382
2383 /* Append to the current flight */
2384 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002385 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002386 else
2387 {
2388 ssl_flight_item *cur = ssl->handshake->flight;
2389 while( cur->next != NULL )
2390 cur = cur->next;
2391 cur->next = msg;
2392 }
2393
2394 return( 0 );
2395}
2396
2397/*
2398 * Free the current flight of handshake messages
2399 */
2400static void ssl_flight_free( ssl_flight_item *flight )
2401{
2402 ssl_flight_item *cur = flight;
2403 ssl_flight_item *next;
2404
2405 while( cur != NULL )
2406 {
2407 next = cur->next;
2408
2409 polarssl_free( cur->p );
2410 polarssl_free( cur );
2411
2412 cur = next;
2413 }
2414}
2415
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002416#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2417static void ssl_dtls_replay_reset( ssl_context *ssl );
2418#endif
2419
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002420/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002421 * Swap transform_out and out_ctr with the alternative ones
2422 */
2423static void ssl_swap_epochs( ssl_context *ssl )
2424{
2425 ssl_transform *tmp_transform;
2426 unsigned char tmp_out_ctr[8];
2427
2428 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2429 {
2430 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2431 return;
2432 }
2433
2434 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2435
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002436 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002437 tmp_transform = ssl->transform_out;
2438 ssl->transform_out = ssl->handshake->alt_transform_out;
2439 ssl->handshake->alt_transform_out = tmp_transform;
2440
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002441 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002442 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2443 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2444 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002445
2446 /* Adjust to the newly activated transform */
2447 if( ssl->transform_out != NULL &&
2448 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2449 {
2450 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2451 ssl->transform_out->fixed_ivlen;
2452 }
2453 else
2454 ssl->out_msg = ssl->out_iv;
2455
2456#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2457 if( ssl_hw_record_activate != NULL )
2458 {
2459 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2460 {
2461 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2462 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2463 }
2464 }
2465#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002466}
2467
2468/*
2469 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002470 *
2471 * Need to remember the current message in case flush_output returns
2472 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002473 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002474 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002475int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002476{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002477 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002478
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002479 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2480 {
2481 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2482
2483 ssl->handshake->cur_msg = ssl->handshake->flight;
2484 ssl_swap_epochs( ssl );
2485
2486 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2487 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002488
2489 while( ssl->handshake->cur_msg != NULL )
2490 {
2491 int ret;
2492 ssl_flight_item *cur = ssl->handshake->cur_msg;
2493
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002494 /* Swap epochs before sending Finished: we can't do it after
2495 * sending ChangeCipherSpec, in case write returns WANT_READ.
2496 * Must be done before copying, may change out_msg pointer */
2497 if( cur->type == SSL_MSG_HANDSHAKE &&
2498 cur->p[0] == SSL_HS_FINISHED )
2499 {
2500 ssl_swap_epochs( ssl );
2501 }
2502
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002503 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002504 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002505 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002506
2507 ssl->handshake->cur_msg = cur->next;
2508
2509 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2510
2511 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2512 {
2513 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2514 return( ret );
2515 }
2516 }
2517
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002518 if( ssl->state == SSL_HANDSHAKE_OVER )
2519 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2520 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002521 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002522 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002523 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2524 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002525
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002526 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002527
2528 return( 0 );
2529}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002530
2531/*
2532 * To be called when the last message of an incoming flight is received.
2533 */
2534void ssl_recv_flight_completed( ssl_context *ssl )
2535{
2536 /* We won't need to resend that one any more */
2537 ssl_flight_free( ssl->handshake->flight );
2538 ssl->handshake->flight = NULL;
2539 ssl->handshake->cur_msg = NULL;
2540
2541 /* The next incoming flight will start with this msg_seq */
2542 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2543
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002544 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002545 ssl_set_timer( ssl, 0 );
2546
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002547 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2548 ssl->in_msg[0] == SSL_HS_FINISHED )
2549 {
2550 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2551 }
2552 else
2553 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2554}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002555
2556/*
2557 * To be called when the last message of an outgoing flight is send.
2558 */
2559void ssl_send_flight_completed( ssl_context *ssl )
2560{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002561 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002562 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002563
2564 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2565 ssl->in_msg[0] == SSL_HS_FINISHED )
2566 {
2567 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2568 }
2569 else
2570 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2571}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002572#endif /* POLARSSL_SSL_PROTO_DTLS */
2573
Paul Bakker5121ce52009-01-03 21:22:43 +00002574/*
2575 * Record layer functions
2576 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002577
2578/*
2579 * Write current record.
2580 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2581 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002582int ssl_write_record( ssl_context *ssl )
2583{
Paul Bakker05ef8352012-05-08 09:17:57 +00002584 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002585 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2588
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002589#if defined(POLARSSL_SSL_PROTO_DTLS)
2590 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2591 ssl->handshake != NULL &&
2592 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2593 {
2594 ; /* Skip special handshake treatment when resending */
2595 }
2596 else
2597#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2599 {
2600 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2601 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2602 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2603
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002604 /*
2605 * DTLS has additional fields in the Handshake layer,
2606 * between the length field and the actual payload:
2607 * uint16 message_seq;
2608 * uint24 fragment_offset;
2609 * uint24 fragment_length;
2610 */
2611#if defined(POLARSSL_SSL_PROTO_DTLS)
2612 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2613 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002614 /* Make room for the additional DTLS fields */
2615 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002616 ssl->out_msglen += 8;
2617 len += 8;
2618
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002619 /* Write message_seq and update it, except for HelloRequest */
2620 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2621 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002622 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2623 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2624 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002625 }
2626 else
2627 {
2628 ssl->out_msg[4] = 0;
2629 ssl->out_msg[5] = 0;
2630 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002631
2632 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2633 memset( ssl->out_msg + 6, 0x00, 3 );
2634 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002635 }
2636#endif /* POLARSSL_SSL_PROTO_DTLS */
2637
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002638 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2639 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 }
2641
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002642 /* Save handshake and CCS messages for resending */
2643#if defined(POLARSSL_SSL_PROTO_DTLS)
2644 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2645 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002646 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002647 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2648 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2649 {
2650 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2651 {
2652 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2653 return( ret );
2654 }
2655 }
2656#endif
2657
Paul Bakker2770fbd2012-07-03 13:30:23 +00002658#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002659 if( ssl->transform_out != NULL &&
2660 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002661 {
2662 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2663 {
2664 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2665 return( ret );
2666 }
2667
2668 len = ssl->out_msglen;
2669 }
2670#endif /*POLARSSL_ZLIB_SUPPORT */
2671
Paul Bakker05ef8352012-05-08 09:17:57 +00002672#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002673 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002675 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002676
Paul Bakker05ef8352012-05-08 09:17:57 +00002677 ret = ssl_hw_record_write( ssl );
2678 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2679 {
2680 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002681 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002682 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002683
2684 if( ret == 0 )
2685 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002686 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002687#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002688 if( !done )
2689 {
2690 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002691 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2692 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002693
2694 ssl->out_len[0] = (unsigned char)( len >> 8 );
2695 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002696
Paul Bakker48916f92012-09-16 19:57:18 +00002697 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002698 {
2699 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2700 {
2701 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2702 return( ret );
2703 }
2704
2705 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002706 ssl->out_len[0] = (unsigned char)( len >> 8 );
2707 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002708 }
2709
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002710 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002711
2712 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2713 "version = [%d:%d], msglen = %d",
2714 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002715 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002716
2717 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002718 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 }
2720
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2722 {
2723 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2724 return( ret );
2725 }
2726
2727 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2728
2729 return( 0 );
2730}
2731
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002732#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002733/*
2734 * Mark bits in bitmask (used for DTLS HS reassembly)
2735 */
2736static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2737{
2738 unsigned int start_bits, end_bits;
2739
2740 start_bits = 8 - ( offset % 8 );
2741 if( start_bits != 8 )
2742 {
2743 size_t first_byte_idx = offset / 8;
2744
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002745 /* Special case */
2746 if( len <= start_bits )
2747 {
2748 for( ; len != 0; len-- )
2749 mask[first_byte_idx] |= 1 << ( start_bits - len );
2750
2751 /* Avoid potential issues with offset or len becoming invalid */
2752 return;
2753 }
2754
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002755 offset += start_bits; /* Now offset % 8 == 0 */
2756 len -= start_bits;
2757
2758 for( ; start_bits != 0; start_bits-- )
2759 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2760 }
2761
2762 end_bits = len % 8;
2763 if( end_bits != 0 )
2764 {
2765 size_t last_byte_idx = ( offset + len ) / 8;
2766
2767 len -= end_bits; /* Now len % 8 == 0 */
2768
2769 for( ; end_bits != 0; end_bits-- )
2770 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2771 }
2772
2773 memset( mask + offset / 8, 0xFF, len / 8 );
2774}
2775
2776/*
2777 * Check that bitmask is full
2778 */
2779static int ssl_bitmask_check( unsigned char *mask, size_t len )
2780{
2781 size_t i;
2782
2783 for( i = 0; i < len / 8; i++ )
2784 if( mask[i] != 0xFF )
2785 return( -1 );
2786
2787 for( i = 0; i < len % 8; i++ )
2788 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2789 return( -1 );
2790
2791 return( 0 );
2792}
2793
2794/*
2795 * Reassemble fragmented DTLS handshake messages.
2796 *
2797 * Use a temporary buffer for reassembly, divided in two parts:
2798 * - the first holds the reassembled message (including handshake header),
2799 * - the second holds a bitmask indicating which parts of the message
2800 * (excluding headers) have been received so far.
2801 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002802static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2803{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002804 unsigned char *msg, *bitmask;
2805 size_t frag_len, frag_off;
2806 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2807
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002808 if( ssl->handshake == NULL )
2809 {
2810 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2811 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2812 }
2813
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002814 /*
2815 * For first fragment, check size and allocate buffer
2816 */
2817 if( ssl->handshake->hs_msg == NULL )
2818 {
2819 size_t alloc_len;
2820
2821 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2822 msg_len ) );
2823
2824 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2825 {
2826 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2827 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2828 }
2829
2830 /* The bitmask needs one bit per byte of message excluding header */
2831 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2832
2833 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2834 if( ssl->handshake->hs_msg == NULL )
2835 {
2836 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2837 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2838 }
2839
2840 memset( ssl->handshake->hs_msg, 0, alloc_len );
2841
2842 /* Prepare final header: copy msg_type, length and message_seq,
2843 * then add standardised fragment_offset and fragment_length */
2844 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2845 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2846 memcpy( ssl->handshake->hs_msg + 9,
2847 ssl->handshake->hs_msg + 1, 3 );
2848 }
2849 else
2850 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002851 /* Make sure msg_type and length are consistent */
2852 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002853 {
2854 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2855 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2856 }
2857 }
2858
2859 msg = ssl->handshake->hs_msg + 12;
2860 bitmask = msg + msg_len;
2861
2862 /*
2863 * Check and copy current fragment
2864 */
2865 frag_off = ( ssl->in_msg[6] << 16 ) |
2866 ( ssl->in_msg[7] << 8 ) |
2867 ssl->in_msg[8];
2868 frag_len = ( ssl->in_msg[9] << 16 ) |
2869 ( ssl->in_msg[10] << 8 ) |
2870 ssl->in_msg[11];
2871
2872 if( frag_off + frag_len > msg_len )
2873 {
2874 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2875 frag_off, frag_len, msg_len ) );
2876 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2877 }
2878
2879 if( frag_len + 12 > ssl->in_msglen )
2880 {
2881 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2882 frag_len, ssl->in_msglen ) );
2883 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2884 }
2885
2886 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2887 frag_off, frag_len ) );
2888
2889 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2890 ssl_bitmask_set( bitmask, frag_off, frag_len );
2891
2892 /*
2893 * Do we have the complete message by now?
2894 * If yes, finalize it, else ask to read the next record.
2895 */
2896 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2897 {
2898 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2899 return( POLARSSL_ERR_NET_WANT_READ );
2900 }
2901
2902 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2903
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002904 if( frag_len + 12 < ssl->in_msglen )
2905 {
2906 /*
2907 * We'got more handshake messages in the same record.
2908 * This case is not handled now because no know implementation does
2909 * that and it's hard to test, so we prefer to fail cleanly for now.
2910 */
2911 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2912 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2913 }
2914
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002915 if( ssl->in_left > ssl->next_record_offset )
2916 {
2917 /*
2918 * We've got more data in the buffer after the current record,
2919 * that we don't want to overwrite. Move it before writing the
2920 * reassembled message, and adjust in_left and next_record_offset.
2921 */
2922 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2923 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2924 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2925
2926 /* First compute and check new lengths */
2927 ssl->next_record_offset = new_remain - ssl->in_hdr;
2928 ssl->in_left = ssl->next_record_offset + remain_len;
2929
2930 if( ssl->in_left > SSL_BUFFER_LEN -
2931 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2932 {
2933 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2934 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2935 }
2936
2937 memmove( new_remain, cur_remain, remain_len );
2938 }
2939
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002940 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2941
2942 polarssl_free( ssl->handshake->hs_msg );
2943 ssl->handshake->hs_msg = NULL;
2944
2945 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2946 ssl->in_msg, ssl->in_hslen );
2947
2948 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002949}
2950#endif /* POLARSSL_SSL_PROTO_DTLS */
2951
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002952static int ssl_prepare_handshake_record( ssl_context *ssl )
2953{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002954 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2955 {
2956 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2957 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002958 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002959 }
2960
2961 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2962 ( ssl->in_msg[1] << 16 ) |
2963 ( ssl->in_msg[2] << 8 ) |
2964 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002965
2966 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2967 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002968 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002969
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002970#if defined(POLARSSL_SSL_PROTO_DTLS)
2971 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002972 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002973 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002974 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002975
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002976 /* ssl->handshake is NULL when receiving ClientHello for renego */
2977 if( ssl->handshake != NULL &&
2978 recv_msg_seq != ssl->handshake->in_msg_seq )
2979 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002980 /* Retransmit only on last message from previous flight, to avoid
2981 * too many retransmissions.
2982 * Besides, No sane server ever retransmits HelloVerifyRequest */
2983 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002984 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002985 {
2986 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2987 "message_seq = %d, start_of_flight = %d",
2988 recv_msg_seq,
2989 ssl->handshake->in_flight_start_seq ) );
2990
2991 if( ( ret = ssl_resend( ssl ) ) != 0 )
2992 {
2993 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2994 return( ret );
2995 }
2996 }
2997 else
2998 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002999 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003000 "message_seq = %d, expected = %d",
3001 recv_msg_seq,
3002 ssl->handshake->in_msg_seq ) );
3003 }
3004
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003005 return( POLARSSL_ERR_NET_WANT_READ );
3006 }
3007 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003008
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003009 /* Reassemble if current message is fragmented or reassembly is
3010 * already in progress */
3011 if( ssl->in_msglen < ssl->in_hslen ||
3012 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3013 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3014 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003015 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003016 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3017
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003018 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3019 {
3020 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3021 return( ret );
3022 }
3023 }
3024 }
3025 else
3026#endif /* POLARSSL_SSL_PROTO_DTLS */
3027 /* With TLS we don't handle fragmentation (for now) */
3028 if( ssl->in_msglen < ssl->in_hslen )
3029 {
3030 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003031 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003032 }
3033
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003034 if( ssl->state != SSL_HANDSHAKE_OVER )
3035 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3036
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003037 /* Handshake message is complete, increment counter */
3038#if defined(POLARSSL_SSL_PROTO_DTLS)
3039 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3040 ssl->handshake != NULL )
3041 {
3042 ssl->handshake->in_msg_seq++;
3043 }
3044#endif
3045
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003046 return( 0 );
3047}
3048
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003049/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003050 * DTLS anti-replay: RFC 6347 4.1.2.6
3051 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003052 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3053 * Bit n is set iff record number in_window_top - n has been seen.
3054 *
3055 * Usually, in_window_top is the last record number seen and the lsb of
3056 * in_window is set. The only exception is the initial state (record number 0
3057 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003058 */
3059#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3060static void ssl_dtls_replay_reset( ssl_context *ssl )
3061{
3062 ssl->in_window_top = 0;
3063 ssl->in_window = 0;
3064}
3065
3066static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3067{
3068 return( ( (uint64_t) buf[0] << 40 ) |
3069 ( (uint64_t) buf[1] << 32 ) |
3070 ( (uint64_t) buf[2] << 24 ) |
3071 ( (uint64_t) buf[3] << 16 ) |
3072 ( (uint64_t) buf[4] << 8 ) |
3073 ( (uint64_t) buf[5] ) );
3074}
3075
3076/*
3077 * Return 0 if sequence number is acceptable, -1 otherwise
3078 */
3079int ssl_dtls_replay_check( ssl_context *ssl )
3080{
3081 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3082 uint64_t bit;
3083
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003084 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3085 return( 0 );
3086
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003087 if( rec_seqnum > ssl->in_window_top )
3088 return( 0 );
3089
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003090 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003091
3092 if( bit >= 64 )
3093 return( -1 );
3094
3095 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3096 return( -1 );
3097
3098 return( 0 );
3099}
3100
3101/*
3102 * Update replay window on new validated record
3103 */
3104void ssl_dtls_replay_update( ssl_context *ssl )
3105{
3106 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3107
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003108 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3109 return;
3110
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003111 if( rec_seqnum > ssl->in_window_top )
3112 {
3113 /* Update window_top and the contents of the window */
3114 uint64_t shift = rec_seqnum - ssl->in_window_top;
3115
3116 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003117 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003118 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003119 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003120 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003121 ssl->in_window |= 1;
3122 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003123
3124 ssl->in_window_top = rec_seqnum;
3125 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003126 else
3127 {
3128 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003129 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003130
3131 if( bit < 64 ) /* Always true, but be extra sure */
3132 ssl->in_window |= (uint64_t) 1 << bit;
3133 }
3134}
3135#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3136
3137/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003138 * ContentType type;
3139 * ProtocolVersion version;
3140 * uint16 epoch; // DTLS only
3141 * uint48 sequence_number; // DTLS only
3142 * uint16 length;
3143 */
3144static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003145{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003146 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003147 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003148
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003149 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3150
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003152 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003153 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
3155 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3156 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003157 ssl->in_msgtype,
3158 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003160 /* Check record type */
3161 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3162 ssl->in_msgtype != SSL_MSG_ALERT &&
3163 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3164 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3165 {
3166 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003167
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003168 if( ( ret = ssl_send_alert_message( ssl,
3169 SSL_ALERT_LEVEL_FATAL,
3170 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3171 {
3172 return( ret );
3173 }
3174
3175 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3176 }
3177
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003178#if defined(POLARSSL_SSL_PROTO_DTLS)
3179 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3180 {
3181 /* Drop unexpected ChangeCipherSpec messages */
3182 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3183 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3184 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3185 {
3186 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3187 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3188 }
3189
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003190 /* Drop unexpected ApplicationData records,
3191 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003192 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003193 ssl->state != SSL_HANDSHAKE_OVER &&
3194 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
3195 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003196 {
3197 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3198 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3199 }
3200 }
3201#endif
3202
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003203 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003204 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003205 {
3206 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003207 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 }
3209
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003210 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003211 {
3212 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 }
3215
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003216 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003217#if defined(POLARSSL_SSL_PROTO_DTLS)
3218 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3219 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003220 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003221
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003222 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003223 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003224 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003225 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003226 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003227 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003228 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003229
3230#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3231 if( ssl_dtls_replay_check( ssl ) != 0 )
3232 {
3233 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3234 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3235 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003236#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003237 }
3238#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003239
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003240 /* Check length against the size of our buffer */
3241 if( ssl->in_msglen > SSL_BUFFER_LEN
3242 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003243 {
3244 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3245 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3246 }
3247
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003248 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003249 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003251 if( ssl->in_msglen < 1 ||
3252 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003253 {
3254 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003255 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 }
3257 }
3258 else
3259 {
Paul Bakker48916f92012-09-16 19:57:18 +00003260 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 {
3262 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003263 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 }
3265
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003266#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003268 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 {
3270 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003271 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003272 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003273#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003274#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3275 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 /*
3277 * TLS encrypted messages can have up to 256 bytes of padding
3278 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003279 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003280 ssl->in_msglen > ssl->transform_in->minlen +
3281 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 {
3283 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003284 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 }
3288
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003289 return( 0 );
3290}
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003292/*
3293 * If applicable, decrypt (and decompress) record content
3294 */
3295static int ssl_prepare_record_content( ssl_context *ssl )
3296{
3297 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003298
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003300 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Paul Bakker05ef8352012-05-08 09:17:57 +00003302#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003303 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003304 {
3305 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3306
3307 ret = ssl_hw_record_read( ssl );
3308 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3309 {
3310 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003311 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003312 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003313
3314 if( ret == 0 )
3315 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003316 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003317#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003318 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 {
3320 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3321 {
3322 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3323 return( ret );
3324 }
3325
3326 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3327 ssl->in_msg, ssl->in_msglen );
3328
3329 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3330 {
3331 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003332 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
3334 }
3335
Paul Bakker2770fbd2012-07-03 13:30:23 +00003336#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003337 if( ssl->transform_in != NULL &&
3338 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003339 {
3340 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3341 {
3342 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3343 return( ret );
3344 }
3345
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003346 // TODO: what's the purpose of these lines? is in_len used?
3347 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3348 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003349 }
3350#endif /* POLARSSL_ZLIB_SUPPORT */
3351
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003352#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003353 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3354 {
3355 ssl_dtls_replay_update( ssl );
3356 }
3357#endif
3358
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003359 return( 0 );
3360}
3361
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003362static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3363
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003364/*
3365 * Read a record.
3366 *
3367 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3368 * and continue reading until a valid record is found.
3369 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003370int ssl_read_record( ssl_context *ssl )
3371{
3372 int ret;
3373
3374 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3375
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003376 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003377 {
3378 /*
3379 * Get next Handshake message in the current record
3380 */
3381 ssl->in_msglen -= ssl->in_hslen;
3382
3383 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3384 ssl->in_msglen );
3385
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003386 SSL_DEBUG_BUF( 4, "remaining content in record",
3387 ssl->in_msg, ssl->in_msglen );
3388
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003389 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3390 return( ret );
3391
3392 return( 0 );
3393 }
3394
3395 ssl->in_hslen = 0;
3396
3397 /*
3398 * Read the record header and parse it
3399 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003400#if defined(POLARSSL_SSL_PROTO_DTLS)
3401read_record_header:
3402#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003403 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3404 {
3405 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3406 return( ret );
3407 }
3408
3409 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003410 {
3411#if defined(POLARSSL_SSL_PROTO_DTLS)
3412 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3413 {
3414 /* Ignore bad record and get next one; drop the whole datagram
3415 * since current header cannot be trusted to find the next record
3416 * in current datagram */
3417 ssl->next_record_offset = 0;
3418 ssl->in_left = 0;
3419
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003420 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003421 goto read_record_header;
3422 }
3423#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003424 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003425 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003426
3427 /*
3428 * Read and optionally decrypt the message contents
3429 */
3430 if( ( ret = ssl_fetch_input( ssl,
3431 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3432 {
3433 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3434 return( ret );
3435 }
3436
3437 /* Done reading this record, get ready for the next one */
3438#if defined(POLARSSL_SSL_PROTO_DTLS)
3439 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3440 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3441 else
3442#endif
3443 ssl->in_left = 0;
3444
3445 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003446 {
3447#if defined(POLARSSL_SSL_PROTO_DTLS)
3448 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3449 {
3450 /* Silently discard invalid records */
3451 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3452 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3453 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003454#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3455 if( ssl->badmac_limit != 0 &&
3456 ++ssl->badmac_seen >= ssl->badmac_limit )
3457 {
3458 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3459 return( POLARSSL_ERR_SSL_INVALID_MAC );
3460 }
3461#endif
3462
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003463 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003464 goto read_record_header;
3465 }
3466
3467 return( ret );
3468 }
3469 else
3470#endif
3471 {
3472 /* Error out (and send alert) on invalid records */
3473#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3474 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3475 {
3476 ssl_send_alert_message( ssl,
3477 SSL_ALERT_LEVEL_FATAL,
3478 SSL_ALERT_MSG_BAD_RECORD_MAC );
3479 }
3480#endif
3481 return( ret );
3482 }
3483 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003484
3485 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003486 * When we sent the last flight of the handshake, we MUST respond to a
3487 * retransmit of the peer's previous flight with a retransmit. (In
3488 * practice, only the Finished message will make it, other messages
3489 * including CCS use the old transform so they're dropped as invalid.)
3490 *
3491 * If the record we received is not a handshake message, however, it
3492 * means the peer received our last flight so we can clean up
3493 * handshake info.
3494 *
3495 * This check needs to be done before prepare_handshake() due to an edge
3496 * case: if the client immediately requests renegotiation, this
3497 * finishes the current handshake first, avoiding the new ClientHello
3498 * being mistaken for an ancient message in the current handshake.
3499 */
3500#if defined(POLARSSL_SSL_PROTO_DTLS)
3501 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3502 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003503 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003504 {
3505 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3506 ssl->in_msg[0] == SSL_HS_FINISHED )
3507 {
3508 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3509
3510 if( ( ret = ssl_resend( ssl ) ) != 0 )
3511 {
3512 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3513 return( ret );
3514 }
3515
3516 return( POLARSSL_ERR_NET_WANT_READ );
3517 }
3518 else
3519 {
3520 ssl_handshake_wrapup_free_hs_transform( ssl );
3521 }
3522 }
3523#endif
3524
3525 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003526 * Handle particular types of records
3527 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3529 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003530 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3531 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 }
3533
3534 if( ssl->in_msgtype == SSL_MSG_ALERT )
3535 {
3536 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3537 ssl->in_msg[0], ssl->in_msg[1] ) );
3538
3539 /*
3540 * Ignore non-fatal alerts, except close_notify
3541 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003542 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003543 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003544 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3545 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003546 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 }
3548
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003549 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3550 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 {
3552 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003553 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 }
3555 }
3556
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3558
3559 return( 0 );
3560}
3561
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003562int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3563{
3564 int ret;
3565
3566 if( ( ret = ssl_send_alert_message( ssl,
3567 SSL_ALERT_LEVEL_FATAL,
3568 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3569 {
3570 return( ret );
3571 }
3572
3573 return( 0 );
3574}
3575
Paul Bakker0a925182012-04-16 06:46:41 +00003576int ssl_send_alert_message( ssl_context *ssl,
3577 unsigned char level,
3578 unsigned char message )
3579{
3580 int ret;
3581
3582 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3583
3584 ssl->out_msgtype = SSL_MSG_ALERT;
3585 ssl->out_msglen = 2;
3586 ssl->out_msg[0] = level;
3587 ssl->out_msg[1] = message;
3588
3589 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3590 {
3591 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3592 return( ret );
3593 }
3594
3595 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3596
3597 return( 0 );
3598}
3599
Paul Bakker5121ce52009-01-03 21:22:43 +00003600/*
3601 * Handshake functions
3602 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003603#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3604 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3605 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3606 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3607 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3608 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3609 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003610int ssl_write_certificate( ssl_context *ssl )
3611{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003612 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003613
3614 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3615
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003616 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003617 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3618 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003619 {
3620 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3621 ssl->state++;
3622 return( 0 );
3623 }
3624
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003625 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3626 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003627}
3628
3629int ssl_parse_certificate( ssl_context *ssl )
3630{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003631 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3632
3633 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3634
3635 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003638 {
3639 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3640 ssl->state++;
3641 return( 0 );
3642 }
3643
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003644 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3645 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003646}
3647#else
3648int ssl_write_certificate( ssl_context *ssl )
3649{
3650 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3651 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003652 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003653 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3654
3655 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3656
3657 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003658 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3659 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003660 {
3661 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3662 ssl->state++;
3663 return( 0 );
3664 }
3665
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 if( ssl->endpoint == SSL_IS_CLIENT )
3667 {
3668 if( ssl->client_auth == 0 )
3669 {
3670 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3671 ssl->state++;
3672 return( 0 );
3673 }
3674
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003675#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 /*
3677 * If using SSLv3 and got no cert, send an Alert message
3678 * (otherwise an empty Certificate message will be sent).
3679 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003680 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3682 {
3683 ssl->out_msglen = 2;
3684 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003685 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3686 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
3688 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3689 goto write_msg;
3690 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003691#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 }
3693 else /* SSL_IS_SERVER */
3694 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003695 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 {
3697 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003698 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 }
3700 }
3701
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003702 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003703
3704 /*
3705 * 0 . 0 handshake type
3706 * 1 . 3 handshake length
3707 * 4 . 6 length of all certs
3708 * 7 . 9 length of cert. 1
3709 * 10 . n-1 peer certificate
3710 * n . n+2 length of cert. 2
3711 * n+3 . ... upper level cert, etc.
3712 */
3713 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003714 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
Paul Bakker29087132010-03-21 21:03:34 +00003716 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003717 {
3718 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003719 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003720 {
3721 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3722 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003723 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724 }
3725
3726 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3727 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3728 ssl->out_msg[i + 2] = (unsigned char)( n );
3729
3730 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3731 i += n; crt = crt->next;
3732 }
3733
3734 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3735 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3736 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3737
3738 ssl->out_msglen = i;
3739 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3740 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3741
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003742#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003743write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003744#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003745
3746 ssl->state++;
3747
3748 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3749 {
3750 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3751 return( ret );
3752 }
3753
3754 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3755
Paul Bakkered27a042013-04-18 22:46:23 +02003756 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003757}
3758
3759int ssl_parse_certificate( ssl_context *ssl )
3760{
Paul Bakkered27a042013-04-18 22:46:23 +02003761 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003762 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003763 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
3765 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3766
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003767 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003768 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3769 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003770 {
3771 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3772 ssl->state++;
3773 return( 0 );
3774 }
3775
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003777 ( ssl->authmode == SSL_VERIFY_NONE ||
3778 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003779 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003780 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003781 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3782 ssl->state++;
3783 return( 0 );
3784 }
3785
3786 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3787 {
3788 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3789 return( ret );
3790 }
3791
3792 ssl->state++;
3793
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003794#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 /*
3796 * Check if the client sent an empty certificate
3797 */
3798 if( ssl->endpoint == SSL_IS_SERVER &&
3799 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3800 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003801 if( ssl->in_msglen == 2 &&
3802 ssl->in_msgtype == SSL_MSG_ALERT &&
3803 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3804 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003805 {
3806 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3807
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003808 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3810 return( 0 );
3811 else
Paul Bakker40e46942009-01-03 21:51:57 +00003812 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003813 }
3814 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003815#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003816
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003817#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3818 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 if( ssl->endpoint == SSL_IS_SERVER &&
3820 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3821 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003822 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3824 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003825 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 {
3827 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3828
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003829 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003830 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003831 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 else
3833 return( 0 );
3834 }
3835 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003836#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3837 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003838
3839 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3840 {
3841 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003842 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 }
3844
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003845 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3846 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 {
3848 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003849 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 }
3851
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003852 i = ssl_hs_hdr_len( ssl );
3853
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 /*
3855 * Same message structure as in ssl_write_certificate()
3856 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003857 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003858
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003859 if( ssl->in_msg[i] != 0 ||
3860 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003861 {
3862 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003863 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003864 }
3865
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003866 /* In case we tried to reuse a session but it failed */
3867 if( ssl->session_negotiate->peer_cert != NULL )
3868 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003869 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003870 polarssl_free( ssl->session_negotiate->peer_cert );
3871 }
3872
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003873 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3874 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 {
3876 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003877 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003878 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 }
3880
Paul Bakkerb6b09562013-09-18 14:17:41 +02003881 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003882
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003883 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003884
3885 while( i < ssl->in_hslen )
3886 {
3887 if( ssl->in_msg[i] != 0 )
3888 {
3889 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003890 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003891 }
3892
3893 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3894 | (unsigned int) ssl->in_msg[i + 2];
3895 i += 3;
3896
3897 if( n < 128 || i + n > ssl->in_hslen )
3898 {
3899 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003900 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 }
3902
Paul Bakkerddf26b42013-09-18 13:46:23 +02003903 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3904 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 if( ret != 0 )
3906 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003907 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 return( ret );
3909 }
3910
3911 i += n;
3912 }
3913
Paul Bakker48916f92012-09-16 19:57:18 +00003914 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003915
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003916 /*
3917 * On client, make sure the server cert doesn't change during renego to
3918 * avoid "triple handshake" attack: https://secure-resumption.com/
3919 */
3920 if( ssl->endpoint == SSL_IS_CLIENT &&
3921 ssl->renegotiation == SSL_RENEGOTIATION )
3922 {
3923 if( ssl->session->peer_cert == NULL )
3924 {
3925 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3926 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3927 }
3928
3929 if( ssl->session->peer_cert->raw.len !=
3930 ssl->session_negotiate->peer_cert->raw.len ||
3931 memcmp( ssl->session->peer_cert->raw.p,
3932 ssl->session_negotiate->peer_cert->raw.p,
3933 ssl->session->peer_cert->raw.len ) != 0 )
3934 {
3935 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3936 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3937 }
3938 }
3939
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 if( ssl->authmode != SSL_VERIFY_NONE )
3941 {
3942 if( ssl->ca_chain == NULL )
3943 {
3944 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003945 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946 }
3947
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003948 /*
3949 * Main check: verify certificate
3950 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003951 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3952 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3953 &ssl->session_negotiate->verify_result,
3954 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
3956 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003957 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003958 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003959 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003960
3961 /*
3962 * Secondary checks: always done, but change 'ret' only if it was 0
3963 */
3964
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003965#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003966 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003967 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003968
3969 /* If certificate uses an EC key, make sure the curve is OK */
3970 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3971 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3972 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003973 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003974 if( ret == 0 )
3975 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003976 }
3977 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003978#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003980 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3981 ciphersuite_info,
3982 ! ssl->endpoint ) != 0 )
3983 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003984 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003985 if( ret == 0 )
3986 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3987 }
3988
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3990 ret = 0;
3991 }
3992
3993 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3994
3995 return( ret );
3996}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003997#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3998 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3999 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4000 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4001 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4002 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4003 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004004
4005int ssl_write_change_cipher_spec( ssl_context *ssl )
4006{
4007 int ret;
4008
4009 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4010
4011 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4012 ssl->out_msglen = 1;
4013 ssl->out_msg[0] = 1;
4014
Paul Bakker5121ce52009-01-03 21:22:43 +00004015 ssl->state++;
4016
4017 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4018 {
4019 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4020 return( ret );
4021 }
4022
4023 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4024
4025 return( 0 );
4026}
4027
4028int ssl_parse_change_cipher_spec( ssl_context *ssl )
4029{
4030 int ret;
4031
4032 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4033
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4035 {
4036 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4037 return( ret );
4038 }
4039
4040 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4041 {
4042 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004043 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004044 }
4045
4046 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4047 {
4048 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004049 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 }
4051
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004052 /*
4053 * Switch to our negotiated transform and session parameters for inbound
4054 * data.
4055 */
4056 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4057 ssl->transform_in = ssl->transform_negotiate;
4058 ssl->session_in = ssl->session_negotiate;
4059
4060#if defined(POLARSSL_SSL_PROTO_DTLS)
4061 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4062 {
4063#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4064 ssl_dtls_replay_reset( ssl );
4065#endif
4066
4067 /* Increment epoch */
4068 if( ++ssl->in_epoch == 0 )
4069 {
4070 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4071 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4072 }
4073 }
4074 else
4075#endif /* POLARSSL_SSL_PROTO_DTLS */
4076 memset( ssl->in_ctr, 0, 8 );
4077
4078 /*
4079 * Set the in_msg pointer to the correct location based on IV length
4080 */
4081 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4082 {
4083 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4084 ssl->transform_negotiate->fixed_ivlen;
4085 }
4086 else
4087 ssl->in_msg = ssl->in_iv;
4088
4089#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4090 if( ssl_hw_record_activate != NULL )
4091 {
4092 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4093 {
4094 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4095 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4096 }
4097 }
4098#endif
4099
Paul Bakker5121ce52009-01-03 21:22:43 +00004100 ssl->state++;
4101
4102 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4103
4104 return( 0 );
4105}
4106
Paul Bakker41c83d32013-03-20 14:39:14 +01004107void ssl_optimize_checksum( ssl_context *ssl,
4108 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004109{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004110 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004111
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004112#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4113 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004114 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004115 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004116 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004117#endif
4118#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4119#if defined(POLARSSL_SHA512_C)
4120 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4121 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4122 else
4123#endif
4124#if defined(POLARSSL_SHA256_C)
4125 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004126 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004127 else
4128#endif
4129#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004130 {
4131 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004132 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004133 }
Paul Bakker380da532012-04-18 16:10:25 +00004134}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004135
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004136void ssl_reset_checksum( ssl_context *ssl )
4137{
4138#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4139 defined(POLARSSL_SSL_PROTO_TLS1_1)
4140 md5_starts( &ssl->handshake->fin_md5 );
4141 sha1_starts( &ssl->handshake->fin_sha1 );
4142#endif
4143#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4144#if defined(POLARSSL_SHA256_C)
4145 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4146#endif
4147#if defined(POLARSSL_SHA512_C)
4148 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4149#endif
4150#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4151}
4152
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004153static void ssl_update_checksum_start( ssl_context *ssl,
4154 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004155{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004156#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4157 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004158 md5_update( &ssl->handshake->fin_md5 , buf, len );
4159 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004160#endif
4161#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4162#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004163 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004164#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004165#if defined(POLARSSL_SHA512_C)
4166 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004167#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004168#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004169}
4170
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004171#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4172 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004173static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4174 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004175{
Paul Bakker48916f92012-09-16 19:57:18 +00004176 md5_update( &ssl->handshake->fin_md5 , buf, len );
4177 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004178}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004179#endif
Paul Bakker380da532012-04-18 16:10:25 +00004180
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004181#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4182#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004183static void ssl_update_checksum_sha256( ssl_context *ssl,
4184 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004185{
Paul Bakker9e36f042013-06-30 14:34:05 +02004186 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004187}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004188#endif
Paul Bakker380da532012-04-18 16:10:25 +00004189
Paul Bakker9e36f042013-06-30 14:34:05 +02004190#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004191static void ssl_update_checksum_sha384( ssl_context *ssl,
4192 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004193{
Paul Bakker9e36f042013-06-30 14:34:05 +02004194 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004195}
Paul Bakker769075d2012-11-24 11:26:46 +01004196#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004197#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004198
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004199#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004200static void ssl_calc_finished_ssl(
4201 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004202{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004203 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004204 md5_context md5;
4205 sha1_context sha1;
4206
Paul Bakker5121ce52009-01-03 21:22:43 +00004207 unsigned char padbuf[48];
4208 unsigned char md5sum[16];
4209 unsigned char sha1sum[20];
4210
Paul Bakker48916f92012-09-16 19:57:18 +00004211 ssl_session *session = ssl->session_negotiate;
4212 if( !session )
4213 session = ssl->session;
4214
Paul Bakker1ef83d62012-04-11 12:09:53 +00004215 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4216
Paul Bakker48916f92012-09-16 19:57:18 +00004217 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4218 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004219
4220 /*
4221 * SSLv3:
4222 * hash =
4223 * MD5( master + pad2 +
4224 * MD5( handshake + sender + master + pad1 ) )
4225 * + SHA1( master + pad2 +
4226 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004227 */
4228
Paul Bakker90995b52013-06-24 19:20:35 +02004229#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004230 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004231 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004232#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004233
Paul Bakker90995b52013-06-24 19:20:35 +02004234#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004235 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004236 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004237#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004238
Paul Bakker3c2122f2013-06-24 19:03:14 +02004239 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4240 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004241
Paul Bakker1ef83d62012-04-11 12:09:53 +00004242 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004243
Paul Bakker3c2122f2013-06-24 19:03:14 +02004244 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004245 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004246 md5_update( &md5, padbuf, 48 );
4247 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004248
Paul Bakker3c2122f2013-06-24 19:03:14 +02004249 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004250 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004251 sha1_update( &sha1, padbuf, 40 );
4252 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004253
Paul Bakker1ef83d62012-04-11 12:09:53 +00004254 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004255
Paul Bakker1ef83d62012-04-11 12:09:53 +00004256 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004257 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004258 md5_update( &md5, padbuf, 48 );
4259 md5_update( &md5, md5sum, 16 );
4260 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004261
Paul Bakker1ef83d62012-04-11 12:09:53 +00004262 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004263 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004264 sha1_update( &sha1, padbuf , 40 );
4265 sha1_update( &sha1, sha1sum, 20 );
4266 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004267
Paul Bakker1ef83d62012-04-11 12:09:53 +00004268 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004269
Paul Bakker5b4af392014-06-26 12:09:34 +02004270 md5_free( &md5 );
4271 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
Paul Bakker34617722014-06-13 17:20:13 +02004273 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4274 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4275 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004276
4277 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4278}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004279#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004281#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282static void ssl_calc_finished_tls(
4283 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004284{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004285 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004286 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004287 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004288 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004289 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004290
Paul Bakker48916f92012-09-16 19:57:18 +00004291 ssl_session *session = ssl->session_negotiate;
4292 if( !session )
4293 session = ssl->session;
4294
Paul Bakker1ef83d62012-04-11 12:09:53 +00004295 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296
Paul Bakker48916f92012-09-16 19:57:18 +00004297 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4298 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Paul Bakker1ef83d62012-04-11 12:09:53 +00004300 /*
4301 * TLSv1:
4302 * hash = PRF( master, finished_label,
4303 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4304 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004305
Paul Bakker90995b52013-06-24 19:20:35 +02004306#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004307 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4308 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004309#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004310
Paul Bakker90995b52013-06-24 19:20:35 +02004311#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004312 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4313 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004314#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004315
4316 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004317 ? "client finished"
4318 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004319
4320 md5_finish( &md5, padbuf );
4321 sha1_finish( &sha1, padbuf + 16 );
4322
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004323 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004324 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004325
4326 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4327
Paul Bakker5b4af392014-06-26 12:09:34 +02004328 md5_free( &md5 );
4329 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004330
Paul Bakker34617722014-06-13 17:20:13 +02004331 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004332
4333 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4334}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004335#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004336
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004337#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4338#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004339static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004340 ssl_context *ssl, unsigned char *buf, int from )
4341{
4342 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004343 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004344 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004345 unsigned char padbuf[32];
4346
Paul Bakker48916f92012-09-16 19:57:18 +00004347 ssl_session *session = ssl->session_negotiate;
4348 if( !session )
4349 session = ssl->session;
4350
Paul Bakker380da532012-04-18 16:10:25 +00004351 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004352
Paul Bakker9e36f042013-06-30 14:34:05 +02004353 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004354
4355 /*
4356 * TLSv1.2:
4357 * hash = PRF( master, finished_label,
4358 * Hash( handshake ) )[0.11]
4359 */
4360
Paul Bakker9e36f042013-06-30 14:34:05 +02004361#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004362 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004363 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004364#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004365
4366 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004367 ? "client finished"
4368 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004369
Paul Bakker9e36f042013-06-30 14:34:05 +02004370 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004371
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004372 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004373 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004374
4375 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4376
Paul Bakker5b4af392014-06-26 12:09:34 +02004377 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004378
Paul Bakker34617722014-06-13 17:20:13 +02004379 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004380
4381 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4382}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004383#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004384
Paul Bakker9e36f042013-06-30 14:34:05 +02004385#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004386static void ssl_calc_finished_tls_sha384(
4387 ssl_context *ssl, unsigned char *buf, int from )
4388{
4389 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004390 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004391 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004392 unsigned char padbuf[48];
4393
Paul Bakker48916f92012-09-16 19:57:18 +00004394 ssl_session *session = ssl->session_negotiate;
4395 if( !session )
4396 session = ssl->session;
4397
Paul Bakker380da532012-04-18 16:10:25 +00004398 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004399
Paul Bakker9e36f042013-06-30 14:34:05 +02004400 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004401
4402 /*
4403 * TLSv1.2:
4404 * hash = PRF( master, finished_label,
4405 * Hash( handshake ) )[0.11]
4406 */
4407
Paul Bakker9e36f042013-06-30 14:34:05 +02004408#if !defined(POLARSSL_SHA512_ALT)
4409 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4410 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004411#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004412
4413 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004414 ? "client finished"
4415 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004416
Paul Bakker9e36f042013-06-30 14:34:05 +02004417 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004418
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004419 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004420 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004421
4422 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4423
Paul Bakker5b4af392014-06-26 12:09:34 +02004424 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004425
Paul Bakker34617722014-06-13 17:20:13 +02004426 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004427
4428 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4429}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004430#endif /* POLARSSL_SHA512_C */
4431#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004432
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004433static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004434{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004435 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004436
4437 /*
4438 * Free our handshake params
4439 */
4440 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004441 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004442 ssl->handshake = NULL;
4443
4444 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004445 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004446 */
4447 if( ssl->transform )
4448 {
4449 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004450 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004451 }
4452 ssl->transform = ssl->transform_negotiate;
4453 ssl->transform_negotiate = NULL;
4454
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004455 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4456}
4457
4458void ssl_handshake_wrapup( ssl_context *ssl )
4459{
4460 int resume = ssl->handshake->resume;
4461
4462 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4463
4464 if( ssl->renegotiation == SSL_RENEGOTIATION )
4465 {
4466 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4467 ssl->renego_records_seen = 0;
4468 }
4469
4470 /*
4471 * Free the previous session and switch in the current one
4472 */
Paul Bakker0a597072012-09-25 21:55:46 +00004473 if( ssl->session )
4474 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004475#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4476 /* RFC 7366 3.1: keep the EtM state */
4477 ssl->session_negotiate->encrypt_then_mac =
4478 ssl->session->encrypt_then_mac;
4479#endif
4480
Paul Bakker0a597072012-09-25 21:55:46 +00004481 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004482 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004483 }
4484 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004485 ssl->session_negotiate = NULL;
4486
Paul Bakker0a597072012-09-25 21:55:46 +00004487 /*
4488 * Add cache entry
4489 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004490 if( ssl->f_set_cache != NULL &&
4491 ssl->session->length != 0 &&
4492 resume == 0 )
4493 {
Paul Bakker0a597072012-09-25 21:55:46 +00004494 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4495 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004496 }
Paul Bakker0a597072012-09-25 21:55:46 +00004497
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004498#if defined(POLARSSL_SSL_PROTO_DTLS)
4499 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4500 ssl->handshake->flight != NULL )
4501 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004502 /* Cancel handshake timer */
4503 ssl_set_timer( ssl, 0 );
4504
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004505 /* Keep last flight around in case we need to resend it:
4506 * we need the handshake and transform structures for that */
4507 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4508 }
4509 else
4510#endif
4511 ssl_handshake_wrapup_free_hs_transform( ssl );
4512
Paul Bakker48916f92012-09-16 19:57:18 +00004513 ssl->state++;
4514
4515 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4516}
4517
Paul Bakker1ef83d62012-04-11 12:09:53 +00004518int ssl_write_finished( ssl_context *ssl )
4519{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004520 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004521
4522 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4523
Paul Bakker92be97b2013-01-02 17:30:03 +01004524 /*
4525 * Set the out_msg pointer to the correct location based on IV length
4526 */
4527 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4528 {
4529 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4530 ssl->transform_negotiate->fixed_ivlen;
4531 }
4532 else
4533 ssl->out_msg = ssl->out_iv;
4534
Paul Bakker48916f92012-09-16 19:57:18 +00004535 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004536
4537 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004538 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4539
Paul Bakker48916f92012-09-16 19:57:18 +00004540 ssl->verify_data_len = hash_len;
4541 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4542
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 ssl->out_msglen = 4 + hash_len;
4544 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4545 ssl->out_msg[0] = SSL_HS_FINISHED;
4546
4547 /*
4548 * In case of session resuming, invert the client and server
4549 * ChangeCipherSpec messages order.
4550 */
Paul Bakker0a597072012-09-25 21:55:46 +00004551 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004552 {
4553 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004554 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004555 else
4556 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4557 }
4558 else
4559 ssl->state++;
4560
Paul Bakker48916f92012-09-16 19:57:18 +00004561 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004562 * Switch to our negotiated transform and session parameters for outbound
4563 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004564 */
4565 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004566
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004567#if defined(POLARSSL_SSL_PROTO_DTLS)
4568 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4569 {
4570 unsigned char i;
4571
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004572 /* Remember current epoch settings for resending */
4573 ssl->handshake->alt_transform_out = ssl->transform_out;
4574 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4575
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004576 /* Set sequence_number to zero */
4577 memset( ssl->out_ctr + 2, 0, 6 );
4578
4579 /* Increment epoch */
4580 for( i = 2; i > 0; i-- )
4581 if( ++ssl->out_ctr[i - 1] != 0 )
4582 break;
4583
4584 /* The loop goes to its end iff the counter is wrapping */
4585 if( i == 0 )
4586 {
4587 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4588 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4589 }
4590 }
4591 else
4592#endif /* POLARSSL_SSL_PROTO_DTLS */
4593 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004594
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004595 ssl->transform_out = ssl->transform_negotiate;
4596 ssl->session_out = ssl->session_negotiate;
4597
Paul Bakker07eb38b2012-12-19 14:42:06 +01004598#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004599 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004600 {
4601 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4602 {
4603 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4604 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4605 }
4606 }
4607#endif
4608
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004609#if defined(POLARSSL_SSL_PROTO_DTLS)
4610 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4611 ssl_send_flight_completed( ssl );
4612#endif
4613
Paul Bakker5121ce52009-01-03 21:22:43 +00004614 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4615 {
4616 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4617 return( ret );
4618 }
4619
4620 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4621
4622 return( 0 );
4623}
4624
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004625#if defined(POLARSSL_SSL_PROTO_SSL3)
4626#define SSL_MAX_HASH_LEN 36
4627#else
4628#define SSL_MAX_HASH_LEN 12
4629#endif
4630
Paul Bakker5121ce52009-01-03 21:22:43 +00004631int ssl_parse_finished( ssl_context *ssl )
4632{
Paul Bakker23986e52011-04-24 08:57:21 +00004633 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004634 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004635 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004636
4637 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4638
Paul Bakker48916f92012-09-16 19:57:18 +00004639 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004640
Paul Bakker5121ce52009-01-03 21:22:43 +00004641 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4642 {
4643 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4644 return( ret );
4645 }
4646
4647 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4648 {
4649 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004650 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004651 }
4652
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004653 /* There is currently no ciphersuite using another length with TLS 1.2 */
4654#if defined(POLARSSL_SSL_PROTO_SSL3)
4655 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4656 hash_len = 36;
4657 else
4658#endif
4659 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004660
4661 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004662 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004663 {
4664 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004665 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004666 }
4667
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004668 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4669 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004670 {
4671 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004672 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004673 }
4674
Paul Bakker48916f92012-09-16 19:57:18 +00004675 ssl->verify_data_len = hash_len;
4676 memcpy( ssl->peer_verify_data, buf, hash_len );
4677
Paul Bakker0a597072012-09-25 21:55:46 +00004678 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 {
4680 if( ssl->endpoint == SSL_IS_CLIENT )
4681 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4682
4683 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004684 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 }
4686 else
4687 ssl->state++;
4688
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004689#if defined(POLARSSL_SSL_PROTO_DTLS)
4690 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4691 ssl_recv_flight_completed( ssl );
4692#endif
4693
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4695
4696 return( 0 );
4697}
4698
Paul Bakker968afaa2014-07-09 11:09:24 +02004699static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004700{
4701 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4702
4703#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4704 defined(POLARSSL_SSL_PROTO_TLS1_1)
4705 md5_init( &handshake->fin_md5 );
4706 sha1_init( &handshake->fin_sha1 );
4707 md5_starts( &handshake->fin_md5 );
4708 sha1_starts( &handshake->fin_sha1 );
4709#endif
4710#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4711#if defined(POLARSSL_SHA256_C)
4712 sha256_init( &handshake->fin_sha256 );
4713 sha256_starts( &handshake->fin_sha256, 0 );
4714#endif
4715#if defined(POLARSSL_SHA512_C)
4716 sha512_init( &handshake->fin_sha512 );
4717 sha512_starts( &handshake->fin_sha512, 1 );
4718#endif
4719#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4720
4721 handshake->update_checksum = ssl_update_checksum_start;
4722 handshake->sig_alg = SSL_HASH_SHA1;
4723
4724#if defined(POLARSSL_DHM_C)
4725 dhm_init( &handshake->dhm_ctx );
4726#endif
4727#if defined(POLARSSL_ECDH_C)
4728 ecdh_init( &handshake->ecdh_ctx );
4729#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004730}
4731
4732static void ssl_transform_init( ssl_transform *transform )
4733{
4734 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004735
4736 cipher_init( &transform->cipher_ctx_enc );
4737 cipher_init( &transform->cipher_ctx_dec );
4738
4739 md_init( &transform->md_ctx_enc );
4740 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004741}
4742
4743void ssl_session_init( ssl_session *session )
4744{
4745 memset( session, 0, sizeof(ssl_session) );
4746}
4747
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004748static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004749{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004750 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004751 if( ssl->transform_negotiate )
4752 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004753 if( ssl->session_negotiate )
4754 ssl_session_free( ssl->session_negotiate );
4755 if( ssl->handshake )
4756 ssl_handshake_free( ssl->handshake );
4757
4758 /*
4759 * Either the pointers are now NULL or cleared properly and can be freed.
4760 * Now allocate missing structures.
4761 */
4762 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004763 {
4764 ssl->transform_negotiate =
4765 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4766 }
Paul Bakker48916f92012-09-16 19:57:18 +00004767
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004768 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004769 {
4770 ssl->session_negotiate =
4771 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4772 }
Paul Bakker48916f92012-09-16 19:57:18 +00004773
Paul Bakker82788fb2014-10-20 13:59:19 +02004774 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004775 {
4776 ssl->handshake = (ssl_handshake_params *)
4777 polarssl_malloc( sizeof(ssl_handshake_params) );
4778 }
Paul Bakker48916f92012-09-16 19:57:18 +00004779
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004780 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004781 if( ssl->handshake == NULL ||
4782 ssl->transform_negotiate == NULL ||
4783 ssl->session_negotiate == NULL )
4784 {
4785 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004786
4787 polarssl_free( ssl->handshake );
4788 polarssl_free( ssl->transform_negotiate );
4789 polarssl_free( ssl->session_negotiate );
4790
4791 ssl->handshake = NULL;
4792 ssl->transform_negotiate = NULL;
4793 ssl->session_negotiate = NULL;
4794
Paul Bakker48916f92012-09-16 19:57:18 +00004795 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4796 }
4797
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004798 /* Initialize structures */
4799 ssl_session_init( ssl->session_negotiate );
4800 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004801 ssl_handshake_params_init( ssl->handshake );
4802
4803#if defined(POLARSSL_X509_CRT_PARSE_C)
4804 ssl->handshake->key_cert = ssl->key_cert;
4805#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004806
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004807 /*
4808 * We may not know yet if we're using DTLS,
4809 * so always initiliase DTLS-specific fields.
4810 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004811#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004812 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004813
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004814 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004815 if( ssl->endpoint == SSL_IS_CLIENT )
4816 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4817 else
4818 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004819#endif
4820
Paul Bakker48916f92012-09-16 19:57:18 +00004821 return( 0 );
4822}
4823
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004824#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4825/* Dummy cookie callbacks for defaults */
4826static int ssl_cookie_write_dummy( void *ctx,
4827 unsigned char **p, unsigned char *end,
4828 const unsigned char *cli_id, size_t cli_id_len )
4829{
4830 ((void) ctx);
4831 ((void) p);
4832 ((void) end);
4833 ((void) cli_id);
4834 ((void) cli_id_len);
4835
4836 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4837}
4838
4839static int ssl_cookie_check_dummy( void *ctx,
4840 const unsigned char *cookie, size_t cookie_len,
4841 const unsigned char *cli_id, size_t cli_id_len )
4842{
4843 ((void) ctx);
4844 ((void) cookie);
4845 ((void) cookie_len);
4846 ((void) cli_id);
4847 ((void) cli_id_len);
4848
4849 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4850}
4851#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4852
Paul Bakker5121ce52009-01-03 21:22:43 +00004853/*
4854 * Initialize an SSL context
4855 */
4856int ssl_init( ssl_context *ssl )
4857{
Paul Bakker48916f92012-09-16 19:57:18 +00004858 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004859 int len = SSL_BUFFER_LEN;
4860
4861 memset( ssl, 0, sizeof( ssl_context ) );
4862
Paul Bakker62f2dee2012-09-28 07:31:51 +00004863 /*
4864 * Sane defaults
4865 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004866 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4867 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4868 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4869 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004870
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004871 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004872
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004873 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4874
Paul Bakker62f2dee2012-09-28 07:31:51 +00004875#if defined(POLARSSL_DHM_C)
4876 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4877 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4878 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4879 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4880 {
4881 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4882 return( ret );
4883 }
4884#endif
4885
4886 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004887 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004888 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004889 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004890 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004891
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004892 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004893 {
4894 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004895 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004896 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004897 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004898 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004899 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 }
4901
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004902 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4903 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004904
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004905 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4906 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4907
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004908#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4909 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4910#endif
4911
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004912#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4913 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4914#endif
4915
Paul Bakker606b4ba2013-08-14 16:52:14 +02004916#if defined(POLARSSL_SSL_SESSION_TICKETS)
4917 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4918#endif
4919
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004920#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004921 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004922#endif
4923
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004924#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4925 ssl->f_cookie_write = ssl_cookie_write_dummy;
4926 ssl->f_cookie_check = ssl_cookie_check_dummy;
4927#endif
4928
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004929#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4930 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4931#endif
4932
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004933#if defined(POLARSSL_SSL_PROTO_DTLS)
4934 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4935 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4936#endif
4937
Paul Bakker48916f92012-09-16 19:57:18 +00004938 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4939 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004940
4941 return( 0 );
4942}
4943
4944/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004945 * Reset an initialized and used SSL context for re-use while retaining
4946 * all application-set variables, function pointers and data.
4947 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004948int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004949{
Paul Bakker48916f92012-09-16 19:57:18 +00004950 int ret;
4951
Paul Bakker7eb013f2011-10-06 12:37:39 +00004952 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004953 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4954 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4955
4956 ssl->verify_data_len = 0;
4957 memset( ssl->own_verify_data, 0, 36 );
4958 memset( ssl->peer_verify_data, 0, 36 );
4959
Paul Bakker7eb013f2011-10-06 12:37:39 +00004960 ssl->in_offt = NULL;
4961
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004962 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004963 ssl->in_msgtype = 0;
4964 ssl->in_msglen = 0;
4965 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004966#if defined(POLARSSL_SSL_PROTO_DTLS)
4967 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004968 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004969#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004970#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4971 ssl_dtls_replay_reset( ssl );
4972#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004973
4974 ssl->in_hslen = 0;
4975 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004976 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004977
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004978 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004979 ssl->out_msgtype = 0;
4980 ssl->out_msglen = 0;
4981 ssl->out_left = 0;
4982
Paul Bakker48916f92012-09-16 19:57:18 +00004983 ssl->transform_in = NULL;
4984 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004985
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004986 ssl->renego_records_seen = 0;
4987
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004988 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4989 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004990
4991#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004992 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004993 {
4994 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004995 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004996 {
4997 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4998 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4999 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005000 }
5001#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005002
Paul Bakker48916f92012-09-16 19:57:18 +00005003 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005004 {
Paul Bakker48916f92012-09-16 19:57:18 +00005005 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005006 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005007 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005008 }
Paul Bakker48916f92012-09-16 19:57:18 +00005009
Paul Bakkerc0463502013-02-14 11:19:38 +01005010 if( ssl->session )
5011 {
5012 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005013 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005014 ssl->session = NULL;
5015 }
5016
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005017#if defined(POLARSSL_SSL_ALPN)
5018 ssl->alpn_chosen = NULL;
5019#endif
5020
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005021#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005022 polarssl_free( ssl->cli_id );
5023 ssl->cli_id = NULL;
5024 ssl->cli_id_len = 0;
5025#endif
5026
Paul Bakker48916f92012-09-16 19:57:18 +00005027 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5028 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005029
5030 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005031}
5032
Paul Bakkera503a632013-08-14 13:48:06 +02005033#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005034static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5035{
5036 aes_free( &tkeys->enc );
5037 aes_free( &tkeys->dec );
5038
5039 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5040}
5041
Paul Bakker7eb013f2011-10-06 12:37:39 +00005042/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005043 * Allocate and initialize ticket keys
5044 */
5045static int ssl_ticket_keys_init( ssl_context *ssl )
5046{
5047 int ret;
5048 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005049 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005050
5051 if( ssl->ticket_keys != NULL )
5052 return( 0 );
5053
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005054 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
5055 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005056 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5057
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005058 aes_init( &tkeys->enc );
5059 aes_init( &tkeys->dec );
5060
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005061 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005062 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005063 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005064 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005065 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005066 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005067
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005068 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5069 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5070 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5071 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005072 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005073 polarssl_free( tkeys );
5074 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005075 }
5076
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005077 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005078 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005079 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005080 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005081 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005082 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005083
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005084 ssl->ticket_keys = tkeys;
5085
5086 return( 0 );
5087}
Paul Bakkera503a632013-08-14 13:48:06 +02005088#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005089
5090/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005091 * SSL set accessors
5092 */
5093void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5094{
5095 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005096
Paul Bakker606b4ba2013-08-14 16:52:14 +02005097#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005098 if( endpoint == SSL_IS_CLIENT )
5099 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005101}
5102
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005103int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005104{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005105#if defined(POLARSSL_SSL_PROTO_DTLS)
5106 if( transport == SSL_TRANSPORT_DATAGRAM )
5107 {
5108 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005109
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005110 ssl->out_hdr = ssl->out_buf;
5111 ssl->out_ctr = ssl->out_buf + 3;
5112 ssl->out_len = ssl->out_buf + 11;
5113 ssl->out_iv = ssl->out_buf + 13;
5114 ssl->out_msg = ssl->out_buf + 13;
5115
5116 ssl->in_hdr = ssl->in_buf;
5117 ssl->in_ctr = ssl->in_buf + 3;
5118 ssl->in_len = ssl->in_buf + 11;
5119 ssl->in_iv = ssl->in_buf + 13;
5120 ssl->in_msg = ssl->in_buf + 13;
5121
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005122 /* DTLS starts with TLS1.1 */
5123 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5124 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005125
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005126 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5127 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5128
5129 return( 0 );
5130 }
5131#endif
5132
5133 if( transport == SSL_TRANSPORT_STREAM )
5134 {
5135 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005136
5137 ssl->out_ctr = ssl->out_buf;
5138 ssl->out_hdr = ssl->out_buf + 8;
5139 ssl->out_len = ssl->out_buf + 11;
5140 ssl->out_iv = ssl->out_buf + 13;
5141 ssl->out_msg = ssl->out_buf + 13;
5142
5143 ssl->in_ctr = ssl->in_buf;
5144 ssl->in_hdr = ssl->in_buf + 8;
5145 ssl->in_len = ssl->in_buf + 11;
5146 ssl->in_iv = ssl->in_buf + 13;
5147 ssl->in_msg = ssl->in_buf + 13;
5148
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005149 return( 0 );
5150 }
5151
5152 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005153}
5154
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005155#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5156void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5157{
5158 ssl->anti_replay = mode;
5159}
5160#endif
5161
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005162#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5163void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5164{
5165 ssl->badmac_limit = limit;
5166}
5167#endif
5168
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005169#if defined(POLARSSL_SSL_PROTO_DTLS)
5170void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5171{
5172 ssl->hs_timeout_min = min;
5173 ssl->hs_timeout_max = max;
5174}
5175#endif
5176
Paul Bakker5121ce52009-01-03 21:22:43 +00005177void ssl_set_authmode( ssl_context *ssl, int authmode )
5178{
5179 ssl->authmode = authmode;
5180}
5181
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005182#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005183void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005184 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005185 void *p_vrfy )
5186{
5187 ssl->f_vrfy = f_vrfy;
5188 ssl->p_vrfy = p_vrfy;
5189}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005190#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005191
Paul Bakker5121ce52009-01-03 21:22:43 +00005192void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005193 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005194 void *p_rng )
5195{
5196 ssl->f_rng = f_rng;
5197 ssl->p_rng = p_rng;
5198}
5199
5200void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005201 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005202 void *p_dbg )
5203{
5204 ssl->f_dbg = f_dbg;
5205 ssl->p_dbg = p_dbg;
5206}
5207
5208void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005209 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005210 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005211{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005212 if( p_recv != p_send )
5213 {
5214 ssl->f_recv = NULL;
5215 ssl->f_send = NULL;
5216 ssl->p_bio = NULL;
5217 return;
5218 }
5219
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 ssl->f_recv = f_recv;
5221 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005222 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005223}
5224
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005225void ssl_set_bio_timeout( ssl_context *ssl,
5226 void *p_bio,
5227 int (*f_send)(void *, const unsigned char *, size_t),
5228 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005229 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5230 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005231{
5232 ssl->p_bio = p_bio;
5233 ssl->f_send = f_send;
5234 ssl->f_recv = f_recv;
5235 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005236 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005237}
5238
Paul Bakker0a597072012-09-25 21:55:46 +00005239void ssl_set_session_cache( ssl_context *ssl,
5240 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5241 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005242{
Paul Bakker0a597072012-09-25 21:55:46 +00005243 ssl->f_get_cache = f_get_cache;
5244 ssl->p_get_cache = p_get_cache;
5245 ssl->f_set_cache = f_set_cache;
5246 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005247}
5248
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005249int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005250{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005251 int ret;
5252
5253 if( ssl == NULL ||
5254 session == NULL ||
5255 ssl->session_negotiate == NULL ||
5256 ssl->endpoint != SSL_IS_CLIENT )
5257 {
5258 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5259 }
5260
5261 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5262 return( ret );
5263
Paul Bakker0a597072012-09-25 21:55:46 +00005264 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005265
5266 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005267}
5268
Paul Bakkerb68cad62012-08-23 08:34:18 +00005269void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005270{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005271 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5272 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5273 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5274 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5275}
5276
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005277void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5278 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005279 int major, int minor )
5280{
5281 if( major != SSL_MAJOR_VERSION_3 )
5282 return;
5283
5284 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5285 return;
5286
5287 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005288}
5289
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005290#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005291/* Add a new (empty) key_cert entry an return a pointer to it */
5292static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5293{
5294 ssl_key_cert *key_cert, *last;
5295
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005296 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5297 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005298 return( NULL );
5299
5300 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5301
5302 /* Append the new key_cert to the (possibly empty) current list */
5303 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005304 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005305 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005306 if( ssl->handshake != NULL )
5307 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005308 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005309 else
5310 {
5311 last = ssl->key_cert;
5312 while( last->next != NULL )
5313 last = last->next;
5314 last->next = key_cert;
5315 }
5316
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005317 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005318}
5319
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005320void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005321 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005322{
5323 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005324 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005325 ssl->peer_cn = peer_cn;
5326}
5327
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005328int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005329 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005330{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005331 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5332
5333 if( key_cert == NULL )
5334 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5335
5336 key_cert->cert = own_cert;
5337 key_cert->key = pk_key;
5338
5339 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005340}
5341
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005342#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005343int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005344 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005345{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005346 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005347 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005348
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005349 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005350 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5351
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005352 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5353 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005354 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005355
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005356 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005357
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005358 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005359 if( ret != 0 )
5360 return( ret );
5361
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005362 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005363 return( ret );
5364
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005365 key_cert->cert = own_cert;
5366 key_cert->key_own_alloc = 1;
5367
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005368 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005369}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005370#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005371
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005372int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005373 void *rsa_key,
5374 rsa_decrypt_func rsa_decrypt,
5375 rsa_sign_func rsa_sign,
5376 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005377{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005378 int ret;
5379 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005380
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005381 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005382 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5383
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005384 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5385 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005386 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005387
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005388 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005389
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005390 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5391 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5392 return( ret );
5393
5394 key_cert->cert = own_cert;
5395 key_cert->key_own_alloc = 1;
5396
5397 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005398}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005399#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005400
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005401#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005402int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5403 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005404{
Paul Bakker6db455e2013-09-18 17:29:31 +02005405 if( psk == NULL || psk_identity == NULL )
5406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5407
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005408 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005409 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5410
Paul Bakker6db455e2013-09-18 17:29:31 +02005411 if( ssl->psk != NULL )
5412 {
5413 polarssl_free( ssl->psk );
5414 polarssl_free( ssl->psk_identity );
5415 }
5416
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005417 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005418 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005419
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005420 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005421 ssl->psk_identity = (unsigned char *)
5422 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005423
5424 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5425 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5426
5427 memcpy( ssl->psk, psk, ssl->psk_len );
5428 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005429
5430 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005431}
5432
5433void ssl_set_psk_cb( ssl_context *ssl,
5434 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5435 size_t),
5436 void *p_psk )
5437{
5438 ssl->f_psk = f_psk;
5439 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005440}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005441#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005442
Paul Bakker48916f92012-09-16 19:57:18 +00005443#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005444int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005445{
5446 int ret;
5447
Paul Bakker48916f92012-09-16 19:57:18 +00005448 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005449 {
5450 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5451 return( ret );
5452 }
5453
Paul Bakker48916f92012-09-16 19:57:18 +00005454 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005455 {
5456 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5457 return( ret );
5458 }
5459
5460 return( 0 );
5461}
5462
Paul Bakker1b57b062011-01-06 15:48:19 +00005463int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5464{
5465 int ret;
5466
Paul Bakker66d5d072014-06-17 16:39:18 +02005467 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005468 {
5469 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5470 return( ret );
5471 }
5472
Paul Bakker66d5d072014-06-17 16:39:18 +02005473 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005474 {
5475 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5476 return( ret );
5477 }
5478
5479 return( 0 );
5480}
Paul Bakker48916f92012-09-16 19:57:18 +00005481#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005482
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005483#if defined(POLARSSL_SSL_SET_CURVES)
5484/*
5485 * Set the allowed elliptic curves
5486 */
5487void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5488{
5489 ssl->curve_list = curve_list;
5490}
5491#endif
5492
Paul Bakker0be444a2013-08-27 21:55:01 +02005493#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005494int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005495{
5496 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005497 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005498
5499 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005500
5501 if( ssl->hostname_len + 1 == 0 )
5502 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5503
Paul Bakker6e339b52013-07-03 13:37:05 +02005504 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005505
Paul Bakkerb15b8512012-01-13 13:44:06 +00005506 if( ssl->hostname == NULL )
5507 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5508
Paul Bakker3c2122f2013-06-24 19:03:14 +02005509 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005510 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005511
Paul Bakker40ea7de2009-05-03 10:18:48 +00005512 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005513
5514 return( 0 );
5515}
5516
Paul Bakker5701cdc2012-09-27 21:49:42 +00005517void ssl_set_sni( ssl_context *ssl,
5518 int (*f_sni)(void *, ssl_context *,
5519 const unsigned char *, size_t),
5520 void *p_sni )
5521{
5522 ssl->f_sni = f_sni;
5523 ssl->p_sni = p_sni;
5524}
Paul Bakker0be444a2013-08-27 21:55:01 +02005525#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005526
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005527#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005528int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005529{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005530 size_t cur_len, tot_len;
5531 const char **p;
5532
5533 /*
5534 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5535 * truncated". Check lengths now rather than later.
5536 */
5537 tot_len = 0;
5538 for( p = protos; *p != NULL; p++ )
5539 {
5540 cur_len = strlen( *p );
5541 tot_len += cur_len;
5542
5543 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5544 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5545 }
5546
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005547 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005548
5549 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005550}
5551
5552const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5553{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005554 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005555}
5556#endif /* POLARSSL_SSL_ALPN */
5557
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005558static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005559{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005560 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005561 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005562 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005563 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005564 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005565
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005566#if defined(POLARSSL_SSL_PROTO_DTLS)
5567 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5568 minor < SSL_MINOR_VERSION_2 )
5569 {
5570 return( -1 );
5571 }
5572#else
5573 ((void) ssl);
5574#endif
5575
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005576 return( 0 );
5577}
5578
5579int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5580{
5581 if( ssl_check_version( ssl, major, minor ) != 0 )
5582 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5583
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005584 ssl->max_major_ver = major;
5585 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005586
5587 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005588}
5589
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005590int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005591{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005592 if( ssl_check_version( ssl, major, minor ) != 0 )
5593 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005594
5595 ssl->min_major_ver = major;
5596 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005597
5598 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005599}
5600
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005601#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5602void ssl_set_fallback( ssl_context *ssl, char fallback )
5603{
5604 ssl->fallback = fallback;
5605}
5606#endif
5607
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005608#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5609void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5610{
5611 ssl->encrypt_then_mac = etm;
5612}
5613#endif
5614
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005615#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5616void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5617{
5618 ssl->extended_ms = ems;
5619}
5620#endif
5621
Paul Bakker05decb22013-08-15 13:33:48 +02005622#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005623int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5624{
Paul Bakker77e257e2013-12-16 15:29:52 +01005625 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005626 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005627 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005628 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005629 }
5630
5631 ssl->mfl_code = mfl_code;
5632
5633 return( 0 );
5634}
Paul Bakker05decb22013-08-15 13:33:48 +02005635#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005636
Paul Bakker1f2bc622013-08-15 13:45:55 +02005637#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005638int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005639{
5640 if( ssl->endpoint != SSL_IS_CLIENT )
5641 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5642
Paul Bakker8c1ede62013-07-19 14:14:37 +02005643 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005644
5645 return( 0 );
5646}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005647#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005648
Paul Bakker48916f92012-09-16 19:57:18 +00005649void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5650{
5651 ssl->disable_renegotiation = renegotiation;
5652}
5653
5654void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5655{
5656 ssl->allow_legacy_renegotiation = allow_legacy;
5657}
5658
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005659void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5660{
5661 ssl->renego_max_records = max_records;
5662}
5663
Paul Bakkera503a632013-08-14 13:48:06 +02005664#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005665int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5666{
5667 ssl->session_tickets = use_tickets;
5668
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005669 if( ssl->endpoint == SSL_IS_CLIENT )
5670 return( 0 );
5671
5672 if( ssl->f_rng == NULL )
5673 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5674
5675 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005676}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005677
5678void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5679{
5680 ssl->ticket_lifetime = lifetime;
5681}
Paul Bakkera503a632013-08-14 13:48:06 +02005682#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005683
Paul Bakker5121ce52009-01-03 21:22:43 +00005684/*
5685 * SSL get accessors
5686 */
Paul Bakker23986e52011-04-24 08:57:21 +00005687size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005688{
5689 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5690}
5691
Paul Bakkerff60ee62010-03-16 21:09:09 +00005692int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005693{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005694 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005695}
5696
Paul Bakkere3166ce2011-01-27 17:40:50 +00005697const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005698{
Paul Bakker926c8e42013-03-06 10:23:34 +01005699 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005700 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005701
Paul Bakkere3166ce2011-01-27 17:40:50 +00005702 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005703}
5704
Paul Bakker43ca69c2011-01-15 17:35:19 +00005705const char *ssl_get_version( const ssl_context *ssl )
5706{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005707#if defined(POLARSSL_SSL_PROTO_DTLS)
5708 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5709 {
5710 switch( ssl->minor_ver )
5711 {
5712 case SSL_MINOR_VERSION_2:
5713 return( "DTLSv1.0" );
5714
5715 case SSL_MINOR_VERSION_3:
5716 return( "DTLSv1.2" );
5717
5718 default:
5719 return( "unknown (DTLS)" );
5720 }
5721 }
5722#endif
5723
Paul Bakker43ca69c2011-01-15 17:35:19 +00005724 switch( ssl->minor_ver )
5725 {
5726 case SSL_MINOR_VERSION_0:
5727 return( "SSLv3.0" );
5728
5729 case SSL_MINOR_VERSION_1:
5730 return( "TLSv1.0" );
5731
5732 case SSL_MINOR_VERSION_2:
5733 return( "TLSv1.1" );
5734
Paul Bakker1ef83d62012-04-11 12:09:53 +00005735 case SSL_MINOR_VERSION_3:
5736 return( "TLSv1.2" );
5737
Paul Bakker43ca69c2011-01-15 17:35:19 +00005738 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005739 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005740 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005741}
5742
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005743int ssl_get_record_expansion( const ssl_context *ssl )
5744{
5745 int transform_expansion;
5746 const ssl_transform *transform = ssl->transform_out;
5747
5748#if defined(POLARSSL_ZLIB_SUPPORT)
5749 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5750 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5751#endif
5752
5753 if( transform == NULL )
5754 return( ssl_hdr_len( ssl ) );
5755
5756 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5757 {
5758 case POLARSSL_MODE_GCM:
5759 case POLARSSL_MODE_CCM:
5760 case POLARSSL_MODE_STREAM:
5761 transform_expansion = transform->minlen;
5762 break;
5763
5764 case POLARSSL_MODE_CBC:
5765 transform_expansion = transform->maclen
5766 + cipher_get_block_size( &transform->cipher_ctx_enc );
5767 break;
5768
5769 default:
5770 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5771 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5772 }
5773
5774 return( ssl_hdr_len( ssl ) + transform_expansion );
5775}
5776
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005777#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005778const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005779{
5780 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005781 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005782
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005783 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005784}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005785#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005786
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005787int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5788{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005789 if( ssl == NULL ||
5790 dst == NULL ||
5791 ssl->session == NULL ||
5792 ssl->endpoint != SSL_IS_CLIENT )
5793 {
5794 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5795 }
5796
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005797 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005798}
5799
Paul Bakker5121ce52009-01-03 21:22:43 +00005800/*
Paul Bakker1961b702013-01-25 14:49:24 +01005801 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005802 */
Paul Bakker1961b702013-01-25 14:49:24 +01005803int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005804{
Paul Bakker40e46942009-01-03 21:51:57 +00005805 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005806
Paul Bakker40e46942009-01-03 21:51:57 +00005807#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005808 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005809 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005810#endif
5811
Paul Bakker40e46942009-01-03 21:51:57 +00005812#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005813 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005814 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005815#endif
5816
Paul Bakker1961b702013-01-25 14:49:24 +01005817 return( ret );
5818}
5819
5820/*
5821 * Perform the SSL handshake
5822 */
5823int ssl_handshake( ssl_context *ssl )
5824{
5825 int ret = 0;
5826
5827 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5828
5829 while( ssl->state != SSL_HANDSHAKE_OVER )
5830 {
5831 ret = ssl_handshake_step( ssl );
5832
5833 if( ret != 0 )
5834 break;
5835 }
5836
Paul Bakker5121ce52009-01-03 21:22:43 +00005837 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5838
5839 return( ret );
5840}
5841
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005842#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005843/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005844 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005845 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005846static int ssl_write_hello_request( ssl_context *ssl )
5847{
5848 int ret;
5849
5850 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5851
5852 ssl->out_msglen = 4;
5853 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5854 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5855
5856 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5857 {
5858 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5859 return( ret );
5860 }
5861
5862 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5863
5864 return( 0 );
5865}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005866#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005867
5868/*
5869 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005870 * - any side: calling ssl_renegotiate(),
5871 * - client: receiving a HelloRequest during ssl_read(),
5872 * - server: receiving any handshake message on server during ssl_read() after
5873 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005874 * If the handshake doesn't complete due to waiting for I/O, it will continue
5875 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005876 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005877static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005878{
5879 int ret;
5880
5881 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5882
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005883 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5884 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005885
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005886 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5887 * the ServerHello will have message_seq = 1" */
5888#if defined(POLARSSL_SSL_PROTO_DTLS)
5889 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005890 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5891 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005892 if( ssl->endpoint == SSL_IS_SERVER )
5893 ssl->handshake->out_msg_seq = 1;
5894 else
5895 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005896 }
5897#endif
5898
Paul Bakker48916f92012-09-16 19:57:18 +00005899 ssl->state = SSL_HELLO_REQUEST;
5900 ssl->renegotiation = SSL_RENEGOTIATION;
5901
Paul Bakker48916f92012-09-16 19:57:18 +00005902 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5903 {
5904 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5905 return( ret );
5906 }
5907
5908 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5909
5910 return( 0 );
5911}
5912
5913/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005914 * Renegotiate current connection on client,
5915 * or request renegotiation on server
5916 */
5917int ssl_renegotiate( ssl_context *ssl )
5918{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005919 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005920
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005921#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005922 /* On server, just send the request */
5923 if( ssl->endpoint == SSL_IS_SERVER )
5924 {
5925 if( ssl->state != SSL_HANDSHAKE_OVER )
5926 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5927
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005928 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5929
5930 /* Did we already try/start sending HelloRequest? */
5931 if( ssl->out_left != 0 )
5932 return( ssl_flush_output( ssl ) );
5933
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005934 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005935 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005936#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005937
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005938#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005939 /*
5940 * On client, either start the renegotiation process or,
5941 * if already in progress, continue the handshake
5942 */
5943 if( ssl->renegotiation != SSL_RENEGOTIATION )
5944 {
5945 if( ssl->state != SSL_HANDSHAKE_OVER )
5946 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5947
5948 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5949 {
5950 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5951 return( ret );
5952 }
5953 }
5954 else
5955 {
5956 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5957 {
5958 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5959 return( ret );
5960 }
5961 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005962#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005963
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005964 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005965}
5966
5967/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005968 * Receive application data decrypted from the SSL layer
5969 */
Paul Bakker23986e52011-04-24 08:57:21 +00005970int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005971{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005972 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005973 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005974
5975 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5976
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005977#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005978 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005979 {
5980 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5981 return( ret );
5982
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005983 if( ssl->handshake != NULL &&
5984 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5985 {
5986 if( ( ret = ssl_resend( ssl ) ) != 0 )
5987 return( ret );
5988 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005989 }
5990#endif
5991
Paul Bakker5121ce52009-01-03 21:22:43 +00005992 if( ssl->state != SSL_HANDSHAKE_OVER )
5993 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005994 ret = ssl_handshake( ssl );
5995 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5996 {
5997 record_read = 1;
5998 }
5999 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 {
6001 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6002 return( ret );
6003 }
6004 }
6005
6006 if( ssl->in_offt == NULL )
6007 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006008#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006009 /* Start timer if not already running */
6010 if( ssl->time_limit == 0 )
6011 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006012#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006013
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006014 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006015 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006016 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6017 {
6018 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6019 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006020
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006021 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6022 return( ret );
6023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006024 }
6025
6026 if( ssl->in_msglen == 0 &&
6027 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6028 {
6029 /*
6030 * OpenSSL sends empty messages to randomize the IV
6031 */
6032 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6033 {
Paul Bakker831a7552011-05-18 13:32:51 +00006034 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6035 return( 0 );
6036
Paul Bakker5121ce52009-01-03 21:22:43 +00006037 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6038 return( ret );
6039 }
6040 }
6041
Paul Bakker48916f92012-09-16 19:57:18 +00006042 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6043 {
6044 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6045
6046 if( ssl->endpoint == SSL_IS_CLIENT &&
6047 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006048 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006049 {
6050 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006051
6052 /* With DTLS, drop the packet (probably from last handshake) */
6053#if defined(POLARSSL_SSL_PROTO_DTLS)
6054 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6055 return( POLARSSL_ERR_NET_WANT_READ );
6056#endif
6057 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6058 }
6059
6060 if( ssl->endpoint == SSL_IS_SERVER &&
6061 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6062 {
6063 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6064
6065 /* With DTLS, drop the packet (probably from last handshake) */
6066#if defined(POLARSSL_SSL_PROTO_DTLS)
6067 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6068 return( POLARSSL_ERR_NET_WANT_READ );
6069#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006070 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6071 }
6072
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006073 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6074 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006075 ssl->allow_legacy_renegotiation ==
6076 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006077 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006078 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006079
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006080#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006081 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006082 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006083 /*
6084 * SSLv3 does not have a "no_renegotiation" alert
6085 */
6086 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6087 return( ret );
6088 }
6089 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006090#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006091#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6092 defined(POLARSSL_SSL_PROTO_TLS1_2)
6093 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006094 {
6095 if( ( ret = ssl_send_alert_message( ssl,
6096 SSL_ALERT_LEVEL_WARNING,
6097 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6098 {
6099 return( ret );
6100 }
Paul Bakker48916f92012-09-16 19:57:18 +00006101 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006102 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006103#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6104 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006105 {
6106 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006107 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006108 }
Paul Bakker48916f92012-09-16 19:57:18 +00006109 }
6110 else
6111 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006112 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006113#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006114 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6115 ssl->endpoint == SSL_IS_CLIENT )
6116 {
6117 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6118 }
6119#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006120 ret = ssl_start_renegotiation( ssl );
6121 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6122 {
6123 record_read = 1;
6124 }
6125 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006126 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006127 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006128 return( ret );
6129 }
Paul Bakker48916f92012-09-16 19:57:18 +00006130 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006131
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006132 /* If a non-handshake record was read during renego, fallthrough,
6133 * else tell the user they should call ssl_read() again */
6134 if( ! record_read )
6135 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006136 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006137 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6138 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006139
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006140 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006141 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006142 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6143 {
6144 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6145 "but not honored by client" ) );
6146 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6147 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006148 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006149 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006150
6151 /* Fatal and closure alerts handled by ssl_read_record() */
6152 if( ssl->in_msgtype == SSL_MSG_ALERT )
6153 {
6154 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6155 return( POLARSSL_ERR_NET_WANT_READ );
6156 }
6157
6158 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006159 {
6160 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006161 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 }
6163
6164 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006165
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006166#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006167 /* We're going to return something now, cancel timer,
6168 * except if handshake (renegotiation) is in progress */
6169 if( ssl->state == SSL_HANDSHAKE_OVER )
6170 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006171
6172 /* If we requested renego but received AppData, resend HelloRequest.
6173 * Do it now, after setting in_offt, to avoid taking this branch
6174 * again if ssl_write_hello_request() returns WANT_WRITE */
6175#if defined(POLARSSL_SSL_SRV_C)
6176 if( ssl->endpoint == SSL_IS_SERVER &&
6177 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6178 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006179 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006180 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006181 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006182 return( ret );
6183 }
6184 }
6185#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006186#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006187 }
6188
6189 n = ( len < ssl->in_msglen )
6190 ? len : ssl->in_msglen;
6191
6192 memcpy( buf, ssl->in_offt, n );
6193 ssl->in_msglen -= n;
6194
6195 if( ssl->in_msglen == 0 )
6196 /* all bytes consumed */
6197 ssl->in_offt = NULL;
6198 else
6199 /* more data available */
6200 ssl->in_offt += n;
6201
6202 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6203
Paul Bakker23986e52011-04-24 08:57:21 +00006204 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006205}
6206
6207/*
6208 * Send application data to be encrypted by the SSL layer
6209 */
Paul Bakker23986e52011-04-24 08:57:21 +00006210int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006211{
Paul Bakker23986e52011-04-24 08:57:21 +00006212 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006213#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6214 unsigned int max_len;
6215#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006216
6217 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6218
6219 if( ssl->state != SSL_HANDSHAKE_OVER )
6220 {
6221 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6222 {
6223 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6224 return( ret );
6225 }
6226 }
6227
Paul Bakker05decb22013-08-15 13:33:48 +02006228#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006229 /*
6230 * Assume mfl_code is correct since it was checked when set
6231 */
6232 max_len = mfl_code_to_length[ssl->mfl_code];
6233
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006234 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006235 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006236 */
6237 if( ssl->session_out != NULL &&
6238 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6239 {
6240 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6241 }
6242
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006243 if( len > max_len )
6244 {
6245#if defined(POLARSSL_SSL_PROTO_DTLS)
6246 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6247 {
6248 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6249 "maximum fragment length: %d > %d",
6250 len, max_len ) );
6251 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6252 }
6253 else
6254#endif
6255 len = max_len;
6256 }
6257#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006258
Paul Bakker5121ce52009-01-03 21:22:43 +00006259 if( ssl->out_left != 0 )
6260 {
6261 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6262 {
6263 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6264 return( ret );
6265 }
6266 }
Paul Bakker887bd502011-06-08 13:10:54 +00006267 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006268 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006269 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006270 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006271 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006272
6273 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6274 {
6275 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6276 return( ret );
6277 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 }
6279
6280 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6281
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006282 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006283}
6284
6285/*
6286 * Notify the peer that the connection is being closed
6287 */
6288int ssl_close_notify( ssl_context *ssl )
6289{
6290 int ret;
6291
6292 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6293
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006294 if( ssl->out_left != 0 )
6295 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006296
6297 if( ssl->state == SSL_HANDSHAKE_OVER )
6298 {
Paul Bakker48916f92012-09-16 19:57:18 +00006299 if( ( ret = ssl_send_alert_message( ssl,
6300 SSL_ALERT_LEVEL_WARNING,
6301 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006302 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006303 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 return( ret );
6305 }
6306 }
6307
6308 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6309
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006310 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006311}
6312
Paul Bakker48916f92012-09-16 19:57:18 +00006313void ssl_transform_free( ssl_transform *transform )
6314{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006315 if( transform == NULL )
6316 return;
6317
Paul Bakker48916f92012-09-16 19:57:18 +00006318#if defined(POLARSSL_ZLIB_SUPPORT)
6319 deflateEnd( &transform->ctx_deflate );
6320 inflateEnd( &transform->ctx_inflate );
6321#endif
6322
Paul Bakker84bbeb52014-07-01 14:53:22 +02006323 cipher_free( &transform->cipher_ctx_enc );
6324 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006325
Paul Bakker84bbeb52014-07-01 14:53:22 +02006326 md_free( &transform->md_ctx_enc );
6327 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006328
Paul Bakker34617722014-06-13 17:20:13 +02006329 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006330}
6331
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006332#if defined(POLARSSL_X509_CRT_PARSE_C)
6333static void ssl_key_cert_free( ssl_key_cert *key_cert )
6334{
6335 ssl_key_cert *cur = key_cert, *next;
6336
6337 while( cur != NULL )
6338 {
6339 next = cur->next;
6340
6341 if( cur->key_own_alloc )
6342 {
6343 pk_free( cur->key );
6344 polarssl_free( cur->key );
6345 }
6346 polarssl_free( cur );
6347
6348 cur = next;
6349 }
6350}
6351#endif /* POLARSSL_X509_CRT_PARSE_C */
6352
Paul Bakker48916f92012-09-16 19:57:18 +00006353void ssl_handshake_free( ssl_handshake_params *handshake )
6354{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006355 if( handshake == NULL )
6356 return;
6357
Paul Bakker48916f92012-09-16 19:57:18 +00006358#if defined(POLARSSL_DHM_C)
6359 dhm_free( &handshake->dhm_ctx );
6360#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006361#if defined(POLARSSL_ECDH_C)
6362 ecdh_free( &handshake->ecdh_ctx );
6363#endif
6364
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006365#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006366 /* explicit void pointer cast for buggy MS compiler */
6367 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006368#endif
6369
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006370#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6371 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6372 /*
6373 * Free only the linked list wrapper, not the keys themselves
6374 * since the belong to the SNI callback
6375 */
6376 if( handshake->sni_key_cert != NULL )
6377 {
6378 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6379
6380 while( cur != NULL )
6381 {
6382 next = cur->next;
6383 polarssl_free( cur );
6384 cur = next;
6385 }
6386 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006387#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006388
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006389#if defined(POLARSSL_SSL_PROTO_DTLS)
6390 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006391 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006392 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006393#endif
6394
Paul Bakker34617722014-06-13 17:20:13 +02006395 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006396}
6397
6398void ssl_session_free( ssl_session *session )
6399{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006400 if( session == NULL )
6401 return;
6402
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006403#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006404 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006405 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006406 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006407 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006408 }
Paul Bakkered27a042013-04-18 22:46:23 +02006409#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006410
Paul Bakkera503a632013-08-14 13:48:06 +02006411#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006412 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006413#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006414
Paul Bakker34617722014-06-13 17:20:13 +02006415 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006416}
6417
Paul Bakker5121ce52009-01-03 21:22:43 +00006418/*
6419 * Free an SSL context
6420 */
6421void ssl_free( ssl_context *ssl )
6422{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006423 if( ssl == NULL )
6424 return;
6425
Paul Bakker5121ce52009-01-03 21:22:43 +00006426 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6427
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006428 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006429 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006430 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6431 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006432 }
6433
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006434 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006435 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006436 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6437 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006438 }
6439
Paul Bakker16770332013-10-11 09:59:44 +02006440#if defined(POLARSSL_ZLIB_SUPPORT)
6441 if( ssl->compress_buf != NULL )
6442 {
Paul Bakker34617722014-06-13 17:20:13 +02006443 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006444 polarssl_free( ssl->compress_buf );
6445 }
6446#endif
6447
Paul Bakker40e46942009-01-03 21:51:57 +00006448#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006449 mpi_free( &ssl->dhm_P );
6450 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006451#endif
6452
Paul Bakker48916f92012-09-16 19:57:18 +00006453 if( ssl->transform )
6454 {
6455 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006456 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006457 }
6458
6459 if( ssl->handshake )
6460 {
6461 ssl_handshake_free( ssl->handshake );
6462 ssl_transform_free( ssl->transform_negotiate );
6463 ssl_session_free( ssl->session_negotiate );
6464
Paul Bakker6e339b52013-07-03 13:37:05 +02006465 polarssl_free( ssl->handshake );
6466 polarssl_free( ssl->transform_negotiate );
6467 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006468 }
6469
Paul Bakkerc0463502013-02-14 11:19:38 +01006470 if( ssl->session )
6471 {
6472 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006473 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006474 }
6475
Paul Bakkera503a632013-08-14 13:48:06 +02006476#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006477 if( ssl->ticket_keys )
6478 {
6479 ssl_ticket_keys_free( ssl->ticket_keys );
6480 polarssl_free( ssl->ticket_keys );
6481 }
Paul Bakkera503a632013-08-14 13:48:06 +02006482#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006483
Paul Bakker0be444a2013-08-27 21:55:01 +02006484#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006485 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 {
Paul Bakker34617722014-06-13 17:20:13 +02006487 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006488 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 ssl->hostname_len = 0;
6490 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006491#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006492
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006493#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006494 if( ssl->psk != NULL )
6495 {
Paul Bakker34617722014-06-13 17:20:13 +02006496 polarssl_zeroize( ssl->psk, ssl->psk_len );
6497 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006498 polarssl_free( ssl->psk );
6499 polarssl_free( ssl->psk_identity );
6500 ssl->psk_len = 0;
6501 ssl->psk_identity_len = 0;
6502 }
6503#endif
6504
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006505#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006506 ssl_key_cert_free( ssl->key_cert );
6507#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006508
Paul Bakker05ef8352012-05-08 09:17:57 +00006509#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6510 if( ssl_hw_record_finish != NULL )
6511 {
6512 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6513 ssl_hw_record_finish( ssl );
6514 }
6515#endif
6516
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006517#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006518 polarssl_free( ssl->cli_id );
6519#endif
6520
Paul Bakker5121ce52009-01-03 21:22:43 +00006521 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006522
Paul Bakker86f04f42013-02-14 11:20:09 +01006523 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006524 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006525}
6526
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006527#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006528/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006529 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006530 */
6531unsigned char ssl_sig_from_pk( pk_context *pk )
6532{
6533#if defined(POLARSSL_RSA_C)
6534 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6535 return( SSL_SIG_RSA );
6536#endif
6537#if defined(POLARSSL_ECDSA_C)
6538 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6539 return( SSL_SIG_ECDSA );
6540#endif
6541 return( SSL_SIG_ANON );
6542}
6543
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006544pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6545{
6546 switch( sig )
6547 {
6548#if defined(POLARSSL_RSA_C)
6549 case SSL_SIG_RSA:
6550 return( POLARSSL_PK_RSA );
6551#endif
6552#if defined(POLARSSL_ECDSA_C)
6553 case SSL_SIG_ECDSA:
6554 return( POLARSSL_PK_ECDSA );
6555#endif
6556 default:
6557 return( POLARSSL_PK_NONE );
6558 }
6559}
Paul Bakker9af723c2014-05-01 13:03:14 +02006560#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006561
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006562/*
6563 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6564 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006565md_type_t ssl_md_alg_from_hash( unsigned char hash )
6566{
6567 switch( hash )
6568 {
6569#if defined(POLARSSL_MD5_C)
6570 case SSL_HASH_MD5:
6571 return( POLARSSL_MD_MD5 );
6572#endif
6573#if defined(POLARSSL_SHA1_C)
6574 case SSL_HASH_SHA1:
6575 return( POLARSSL_MD_SHA1 );
6576#endif
6577#if defined(POLARSSL_SHA256_C)
6578 case SSL_HASH_SHA224:
6579 return( POLARSSL_MD_SHA224 );
6580 case SSL_HASH_SHA256:
6581 return( POLARSSL_MD_SHA256 );
6582#endif
6583#if defined(POLARSSL_SHA512_C)
6584 case SSL_HASH_SHA384:
6585 return( POLARSSL_MD_SHA384 );
6586 case SSL_HASH_SHA512:
6587 return( POLARSSL_MD_SHA512 );
6588#endif
6589 default:
6590 return( POLARSSL_MD_NONE );
6591 }
6592}
6593
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006594#if defined(POLARSSL_SSL_SET_CURVES)
6595/*
6596 * Check is a curve proposed by the peer is in our list.
6597 * Return 1 if we're willing to use it, 0 otherwise.
6598 */
6599int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6600{
6601 const ecp_group_id *gid;
6602
6603 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6604 if( *gid == grp_id )
6605 return( 1 );
6606
6607 return( 0 );
6608}
Paul Bakker9af723c2014-05-01 13:03:14 +02006609#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006610
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006611#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006612int ssl_check_cert_usage( const x509_crt *cert,
6613 const ssl_ciphersuite_t *ciphersuite,
6614 int cert_endpoint )
6615{
6616#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6617 int usage = 0;
6618#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006619#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6620 const char *ext_oid;
6621 size_t ext_len;
6622#endif
6623
6624#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6625 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6626 ((void) cert);
6627 ((void) cert_endpoint);
6628#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006629
6630#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6631 if( cert_endpoint == SSL_IS_SERVER )
6632 {
6633 /* Server part of the key exchange */
6634 switch( ciphersuite->key_exchange )
6635 {
6636 case POLARSSL_KEY_EXCHANGE_RSA:
6637 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6638 usage = KU_KEY_ENCIPHERMENT;
6639 break;
6640
6641 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6642 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6643 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6644 usage = KU_DIGITAL_SIGNATURE;
6645 break;
6646
6647 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6648 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6649 usage = KU_KEY_AGREEMENT;
6650 break;
6651
6652 /* Don't use default: we want warnings when adding new values */
6653 case POLARSSL_KEY_EXCHANGE_NONE:
6654 case POLARSSL_KEY_EXCHANGE_PSK:
6655 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6656 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6657 usage = 0;
6658 }
6659 }
6660 else
6661 {
6662 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6663 usage = KU_DIGITAL_SIGNATURE;
6664 }
6665
6666 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6667 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006668#else
6669 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006670#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6671
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006672#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6673 if( cert_endpoint == SSL_IS_SERVER )
6674 {
6675 ext_oid = OID_SERVER_AUTH;
6676 ext_len = OID_SIZE( OID_SERVER_AUTH );
6677 }
6678 else
6679 {
6680 ext_oid = OID_CLIENT_AUTH;
6681 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6682 }
6683
6684 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6685 return( -1 );
6686#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6687
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006688 return( 0 );
6689}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006690#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006691
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006692/*
6693 * Convert version numbers to/from wire format
6694 * and, for DTLS, to/from TLS equivalent.
6695 *
6696 * For TLS this is the identity.
6697 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6698 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6699 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6700 */
6701void ssl_write_version( int major, int minor, int transport,
6702 unsigned char ver[2] )
6703{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006704#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006705 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006706 {
6707 if( minor == SSL_MINOR_VERSION_2 )
6708 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6709
6710 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6711 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6712 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006713 else
6714#else
6715 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006716#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006717 {
6718 ver[0] = (unsigned char) major;
6719 ver[1] = (unsigned char) minor;
6720 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006721}
6722
6723void ssl_read_version( int *major, int *minor, int transport,
6724 const unsigned char ver[2] )
6725{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006726#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006727 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006728 {
6729 *major = 255 - ver[0] + 2;
6730 *minor = 255 - ver[1] + 1;
6731
6732 if( *minor == SSL_MINOR_VERSION_1 )
6733 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6734 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006735 else
6736#else
6737 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006738#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006739 {
6740 *major = ver[0];
6741 *minor = ver[1];
6742 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006743}
6744
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006745#endif /* POLARSSL_SSL_TLS_C */