blob: bfaefd46d19fca385318153c4415b08bbaadfc90 [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
Paul Bakker48916f92012-09-16 19:57:18 +0000547 handshake->tls_prf( handshake->premaster, handshake->pmslen,
548 "master secret",
549 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Paul Bakker34617722014-06-13 17:20:13 +0200551 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553 else
554 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
555
556 /*
557 * Swap the client and server random values.
558 */
Paul Bakker48916f92012-09-16 19:57:18 +0000559 memcpy( tmp, handshake->randbytes, 64 );
560 memcpy( handshake->randbytes, tmp + 32, 32 );
561 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200562 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 /*
565 * SSLv3:
566 * key block =
567 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
568 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
569 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
570 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
571 * ...
572 *
573 * TLSv1:
574 * key block = PRF( master, "key expansion", randbytes )
575 */
Paul Bakker48916f92012-09-16 19:57:18 +0000576 handshake->tls_prf( session->master, 48, "key expansion",
577 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Paul Bakker48916f92012-09-16 19:57:18 +0000579 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
580 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
581 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
582 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
584
Paul Bakker34617722014-06-13 17:20:13 +0200585 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 /*
588 * Determine the appropriate key, IV and MAC length.
589 */
Paul Bakker68884e32013-01-07 18:20:04 +0100590
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200591 transform->keylen = cipher_info->key_length / 8;
592
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200593 if( cipher_info->mode == POLARSSL_MODE_GCM ||
594 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200596 transform->maclen = 0;
597
Paul Bakker68884e32013-01-07 18:20:04 +0100598 transform->ivlen = 12;
599 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200600
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200601 /* Minimum length is expicit IV + tag */
602 transform->minlen = transform->ivlen - transform->fixed_ivlen
603 + ( transform->ciphersuite_info->flags &
604 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100605 }
606 else
607 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200608 int ret;
609
610 /* Initialize HMAC contexts */
611 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
612 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200614 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
615 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100616 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200618 /* Get MAC length */
619 transform->maclen = md_get_size( md_info );
620
621#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
622 /*
623 * If HMAC is to be truncated, we shall keep the leftmost bytes,
624 * (rfc 6066 page 13 or rfc 2104 section 4),
625 * so we only need to adjust the length here.
626 */
627 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
628 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
629#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
630
631 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100632 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200634 /* Minimum length */
635 if( cipher_info->mode == POLARSSL_MODE_STREAM )
636 transform->minlen = transform->maclen;
637 else
Paul Bakker68884e32013-01-07 18:20:04 +0100638 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200639 /*
640 * GenericBlockCipher:
641 * first multiple of blocklen greater than maclen
642 * + IV except for SSL3 and TLS 1.0
643 */
644 transform->minlen = transform->maclen
645 + cipher_info->block_size
646 - transform->maclen % cipher_info->block_size;
647
648#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
649 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
650 ssl->minor_ver == SSL_MINOR_VERSION_1 )
651 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100652 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200653#endif
654#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
655 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
656 ssl->minor_ver == SSL_MINOR_VERSION_3 )
657 {
658 transform->minlen += transform->ivlen;
659 }
660 else
661#endif
662 {
663 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
664 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
665 }
Paul Bakker68884e32013-01-07 18:20:04 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668
669 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000670 transform->keylen, transform->minlen, transform->ivlen,
671 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 /*
674 * Finally setup the cipher contexts, IVs and MAC secrets.
675 */
676 if( ssl->endpoint == SSL_IS_CLIENT )
677 {
Paul Bakker48916f92012-09-16 19:57:18 +0000678 key1 = keyblk + transform->maclen * 2;
679 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Paul Bakker68884e32013-01-07 18:20:04 +0100681 mac_enc = keyblk;
682 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000684 /*
685 * This is not used in TLS v1.1.
686 */
Paul Bakker48916f92012-09-16 19:57:18 +0000687 iv_copy_len = ( transform->fixed_ivlen ) ?
688 transform->fixed_ivlen : transform->ivlen;
689 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
690 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 }
693 else
694 {
Paul Bakker48916f92012-09-16 19:57:18 +0000695 key1 = keyblk + transform->maclen * 2 + transform->keylen;
696 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Paul Bakker68884e32013-01-07 18:20:04 +0100698 mac_enc = keyblk + transform->maclen;
699 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000701 /*
702 * This is not used in TLS v1.1.
703 */
Paul Bakker48916f92012-09-16 19:57:18 +0000704 iv_copy_len = ( transform->fixed_ivlen ) ?
705 transform->fixed_ivlen : transform->ivlen;
706 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
707 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000708 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 }
710
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100712 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
713 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100714 if( transform->maclen > sizeof transform->mac_enc )
715 {
716 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200717 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100718 }
719
Paul Bakker68884e32013-01-07 18:20:04 +0100720 memcpy( transform->mac_enc, mac_enc, transform->maclen );
721 memcpy( transform->mac_dec, mac_dec, transform->maclen );
722 }
723 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200724#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
726 defined(POLARSSL_SSL_PROTO_TLS1_2)
727 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100728 {
729 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
730 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200732 else
733#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200734 {
735 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200736 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200737 }
Paul Bakker68884e32013-01-07 18:20:04 +0100738
Paul Bakker05ef8352012-05-08 09:17:57 +0000739#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200740 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000741 {
742 int ret = 0;
743
744 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
745
Paul Bakker07eb38b2012-12-19 14:42:06 +0100746 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
747 transform->iv_enc, transform->iv_dec,
748 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100749 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100750 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000751 {
752 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200753 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000754 }
755 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200756#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000757
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200758 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
759 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200761 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
762 return( ret );
763 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200764
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200765 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
766 cipher_info ) ) != 0 )
767 {
768 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
769 return( ret );
770 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200772 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
773 cipher_info->key_length,
774 POLARSSL_ENCRYPT ) ) != 0 )
775 {
776 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
777 return( ret );
778 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200779
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200780 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
781 cipher_info->key_length,
782 POLARSSL_DECRYPT ) ) != 0 )
783 {
784 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
785 return( ret );
786 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200787
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200788#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200789 if( cipher_info->mode == POLARSSL_MODE_CBC )
790 {
791 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
792 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200793 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200794 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
795 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200796 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200797
798 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
799 POLARSSL_PADDING_NONE ) ) != 0 )
800 {
801 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
802 return( ret );
803 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200805#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Paul Bakker34617722014-06-13 17:20:13 +0200807 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Paul Bakker2770fbd2012-07-03 13:30:23 +0000809#if defined(POLARSSL_ZLIB_SUPPORT)
810 // Initialize compression
811 //
Paul Bakker48916f92012-09-16 19:57:18 +0000812 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000813 {
Paul Bakker16770332013-10-11 09:59:44 +0200814 if( ssl->compress_buf == NULL )
815 {
816 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
817 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
818 if( ssl->compress_buf == NULL )
819 {
820 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
821 SSL_BUFFER_LEN ) );
822 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
823 }
824 }
825
Paul Bakker2770fbd2012-07-03 13:30:23 +0000826 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
827
Paul Bakker48916f92012-09-16 19:57:18 +0000828 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
829 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000830
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200831 if( deflateInit( &transform->ctx_deflate,
832 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000833 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000834 {
835 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
836 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
837 }
838 }
839#endif /* POLARSSL_ZLIB_SUPPORT */
840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
842
843 return( 0 );
844}
845
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200846#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000847void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000848{
849 md5_context md5;
850 sha1_context sha1;
851 unsigned char pad_1[48];
852 unsigned char pad_2[48];
853
Paul Bakker380da532012-04-18 16:10:25 +0000854 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker48916f92012-09-16 19:57:18 +0000856 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
857 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker380da532012-04-18 16:10:25 +0000859 memset( pad_1, 0x36, 48 );
860 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Paul Bakker48916f92012-09-16 19:57:18 +0000862 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000863 md5_update( &md5, pad_1, 48 );
864 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000865
Paul Bakker380da532012-04-18 16:10:25 +0000866 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000867 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000868 md5_update( &md5, pad_2, 48 );
869 md5_update( &md5, hash, 16 );
870 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Paul Bakker48916f92012-09-16 19:57:18 +0000872 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000873 sha1_update( &sha1, pad_1, 40 );
874 sha1_finish( &sha1, hash + 16 );
875
876 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000877 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_update( &sha1, pad_2, 40 );
879 sha1_update( &sha1, hash + 16, 20 );
880 sha1_finish( &sha1, hash + 16 );
881
882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 md5_free( &md5 );
886 sha1_free( &sha1 );
887
Paul Bakker380da532012-04-18 16:10:25 +0000888 return;
889}
Paul Bakker9af723c2014-05-01 13:03:14 +0200890#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000891
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200892#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000893void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
894{
895 md5_context md5;
896 sha1_context sha1;
897
898 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
899
Paul Bakker48916f92012-09-16 19:57:18 +0000900 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
901 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000902
Paul Bakker48916f92012-09-16 19:57:18 +0000903 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000904 sha1_finish( &sha1, hash + 16 );
905
906 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
907 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
908
Paul Bakker5b4af392014-06-26 12:09:34 +0200909 md5_free( &md5 );
910 sha1_free( &sha1 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#if defined(POLARSSL_SSL_PROTO_TLS1_2)
917#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000918void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
919{
Paul Bakker9e36f042013-06-30 14:34:05 +0200920 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000921
922 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
923
Paul Bakker9e36f042013-06-30 14:34:05 +0200924 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
925 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000926
927 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
928 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
929
Paul Bakker5b4af392014-06-26 12:09:34 +0200930 sha256_free( &sha256 );
931
Paul Bakker380da532012-04-18 16:10:25 +0000932 return;
933}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200934#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000935
Paul Bakker9e36f042013-06-30 14:34:05 +0200936#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000937void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
938{
Paul Bakker9e36f042013-06-30 14:34:05 +0200939 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000940
941 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
942
Paul Bakker9e36f042013-06-30 14:34:05 +0200943 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
944 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Paul Bakkerca4ab492012-04-18 14:23:57 +0000946 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
948
Paul Bakker5b4af392014-06-26 12:09:34 +0200949 sha512_free( &sha512 );
950
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 return;
952}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200953#endif /* POLARSSL_SHA512_C */
954#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200956#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
958{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200959 unsigned char *p = ssl->handshake->premaster;
960 unsigned char *end = p + sizeof( ssl->handshake->premaster );
961
962 /*
963 * PMS = struct {
964 * opaque other_secret<0..2^16-1>;
965 * opaque psk<0..2^16-1>;
966 * };
967 * with "other_secret" depending on the particular key exchange
968 */
969#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
970 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
971 {
972 if( end - p < 2 + (int) ssl->psk_len )
973 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
974
975 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
976 *(p++) = (unsigned char)( ssl->psk_len );
977 p += ssl->psk_len;
978 }
979 else
980#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200981#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
982 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
983 {
984 /*
985 * other_secret already set by the ClientKeyExchange message,
986 * and is 48 bytes long
987 */
988 *p++ = 0;
989 *p++ = 48;
990 p += 48;
991 }
992 else
993#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
995 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
996 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200997 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200998 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200999
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001000 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001001 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001002 p + 2, &len,
1003 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 {
1005 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1006 return( ret );
1007 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001008 *(p++) = (unsigned char)( len >> 8 );
1009 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 p += len;
1011
1012 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1013 }
1014 else
1015#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1016#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1017 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1018 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001019 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020 size_t zlen;
1021
1022 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001023 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001024 ssl->f_rng, ssl->p_rng ) ) != 0 )
1025 {
1026 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1027 return( ret );
1028 }
1029
1030 *(p++) = (unsigned char)( zlen >> 8 );
1031 *(p++) = (unsigned char)( zlen );
1032 p += zlen;
1033
1034 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1035 }
1036 else
1037#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1038 {
1039 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001040 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001041 }
1042
1043 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001044 if( end - p < 2 + (int) ssl->psk_len )
1045 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1046
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001047 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1048 *(p++) = (unsigned char)( ssl->psk_len );
1049 memcpy( p, ssl->psk, ssl->psk_len );
1050 p += ssl->psk_len;
1051
1052 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1053
1054 return( 0 );
1055}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001056#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001057
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001058#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001059/*
1060 * SSLv3.0 MAC functions
1061 */
Paul Bakker68884e32013-01-07 18:20:04 +01001062static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1063 unsigned char *buf, size_t len,
1064 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001065{
1066 unsigned char header[11];
1067 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001068 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001069 int md_size = md_get_size( md_ctx->md_info );
1070 int md_type = md_get_type( md_ctx->md_info );
1071
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001072 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001073 if( md_type == POLARSSL_MD_MD5 )
1074 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001075 else
Paul Bakker68884e32013-01-07 18:20:04 +01001076 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
1078 memcpy( header, ctr, 8 );
1079 header[ 8] = (unsigned char) type;
1080 header[ 9] = (unsigned char)( len >> 8 );
1081 header[10] = (unsigned char)( len );
1082
Paul Bakker68884e32013-01-07 18:20:04 +01001083 memset( padding, 0x36, padlen );
1084 md_starts( md_ctx );
1085 md_update( md_ctx, secret, md_size );
1086 md_update( md_ctx, padding, padlen );
1087 md_update( md_ctx, header, 11 );
1088 md_update( md_ctx, buf, len );
1089 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker68884e32013-01-07 18:20:04 +01001091 memset( padding, 0x5C, padlen );
1092 md_starts( md_ctx );
1093 md_update( md_ctx, secret, md_size );
1094 md_update( md_ctx, padding, padlen );
1095 md_update( md_ctx, buf + len, md_size );
1096 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001097}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001098#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001099
Paul Bakker5121ce52009-01-03 21:22:43 +00001100/*
1101 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001102 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001103static int ssl_encrypt_buf( ssl_context *ssl )
1104{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001105 const cipher_mode_t mode = cipher_get_cipher_mode(
1106 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
1110 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001111 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001113#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1114 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1115 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001116 if( mode != POLARSSL_MODE_GCM &&
1117 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001119#if defined(POLARSSL_SSL_PROTO_SSL3)
1120 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1121 {
1122 ssl_mac( &ssl->transform_out->md_ctx_enc,
1123 ssl->transform_out->mac_enc,
1124 ssl->out_msg, ssl->out_msglen,
1125 ssl->out_ctr, ssl->out_msgtype );
1126 }
1127 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001128#endif
1129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130 defined(POLARSSL_SSL_PROTO_TLS1_2)
1131 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1132 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001133 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001136 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg, ssl->out_msglen );
1138 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg + ssl->out_msglen );
1140 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1141 }
1142 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001143#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001144 {
1145 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 }
1148
1149 SSL_DEBUG_BUF( 4, "computed mac",
1150 ssl->out_msg + ssl->out_msglen,
1151 ssl->transform_out->maclen );
1152
1153 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001154 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001155#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 /*
1158 * Encrypt
1159 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001161 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001163 int ret;
1164 size_t olen = 0;
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1167 "including %d bytes of padding",
1168 ssl->out_msglen, 0 ) );
1169
1170 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1171 ssl->out_msg, ssl->out_msglen );
1172
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 ssl->transform_out->ivlen,
1176 ssl->out_msg, ssl->out_msglen,
1177 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001178 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001179 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180 return( ret );
1181 }
1182
1183 if( ssl->out_msglen != olen )
1184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1192 if( mode == POLARSSL_MODE_GCM ||
1193 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 unsigned char *enc_msg;
1198 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1200 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 memcpy( add_data, ssl->out_ctr, 8 );
1203 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001204 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1205 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1207 add_data[12] = ssl->out_msglen & 0xFF;
1208
1209 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1210 add_data, 13 );
1211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
1213 * Generate IV
1214 */
1215 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001216 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1217 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001218 if( ret != 0 )
1219 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Paul Bakker68884e32013-01-07 18:20:04 +01001221 memcpy( ssl->out_iv,
1222 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1223 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001224
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001225 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001226 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001227
Paul Bakker68884e32013-01-07 18:20:04 +01001228 /*
1229 * Fix pointer positions and message length with added IV
1230 */
1231 enc_msg = ssl->out_msg;
1232 enc_msglen = ssl->out_msglen;
1233 ssl->out_msglen += ssl->transform_out->ivlen -
1234 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001235
Paul Bakker68884e32013-01-07 18:20:04 +01001236 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1237 "including %d bytes of padding",
1238 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1241 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Paul Bakker68884e32013-01-07 18:20:04 +01001243 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001244 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001245 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001246 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1247 ssl->transform_out->iv_enc,
1248 ssl->transform_out->ivlen,
1249 add_data, 13,
1250 enc_msg, enc_msglen,
1251 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001252 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001253 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 return( ret );
1256 }
1257
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001258 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001259 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001260 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001261 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 }
1263
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001265
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001266 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001270#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1271 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001272 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001274 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001275 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001276 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001277
Paul Bakker48916f92012-09-16 19:57:18 +00001278 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1279 ssl->transform_out->ivlen;
1280 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 padlen = 0;
1282
1283 for( i = 0; i <= padlen; i++ )
1284 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1285
1286 ssl->out_msglen += padlen + 1;
1287
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288 enc_msglen = ssl->out_msglen;
1289 enc_msg = ssl->out_msg;
1290
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001291#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001293 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1294 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001296 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001297 {
1298 /*
1299 * Generate IV
1300 */
Paul Bakker48916f92012-09-16 19:57:18 +00001301 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1302 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001303 if( ret != 0 )
1304 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305
Paul Bakker92be97b2013-01-02 17:30:03 +01001306 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001307 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308
1309 /*
1310 * Fix pointer positions and message length with added IV
1311 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001312 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001316#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001320 ssl->out_msglen, ssl->transform_out->ivlen,
1321 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001324 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001327 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001328 ssl->transform_out->ivlen,
1329 enc_msg, enc_msglen,
1330 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001333 return( ret );
1334 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001335
Paul Bakkercca5b812013-08-31 17:40:26 +02001336 if( enc_msglen != olen )
1337 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001341
1342#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1344 {
1345 /*
1346 * Save IV in SSL3 and TLS1
1347 */
1348 memcpy( ssl->transform_out->iv_enc,
1349 ssl->transform_out->cipher_ctx_enc.iv,
1350 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001354 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001355#endif /* POLARSSL_CIPHER_MODE_CBC &&
1356 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001357 {
1358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1363
1364 return( 0 );
1365}
1366
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001367#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001368
Paul Bakker5121ce52009-01-03 21:22:43 +00001369static int ssl_decrypt_buf( ssl_context *ssl )
1370{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001371 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001372 const cipher_mode_t mode = cipher_get_cipher_mode(
1373 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001374#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1375 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1376 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1377 size_t padlen = 0, correct = 1;
1378#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
1380 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 {
1384 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001385 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001386 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 }
1388
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001389#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001390 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001392 int ret;
1393 size_t olen = 0;
1394
Paul Bakker68884e32013-01-07 18:20:04 +01001395 padlen = 0;
1396
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001397 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001398 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001399 ssl->transform_in->ivlen,
1400 ssl->in_msg, ssl->in_msglen,
1401 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001402 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001403 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001404 return( ret );
1405 }
1406
1407 if( ssl->in_msglen != olen )
1408 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001409 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001410 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
Paul Bakker68884e32013-01-07 18:20:04 +01001413 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001414#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001415#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1416 if( mode == POLARSSL_MODE_GCM ||
1417 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001418 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001419 int ret;
1420 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 unsigned char *dec_msg;
1422 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1425 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001426 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1427 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001428
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001429 if( ssl->in_msglen < explicit_iv_len + taglen )
1430 {
1431 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1432 "+ taglen (%d)", ssl->in_msglen,
1433 explicit_iv_len, taglen ) );
1434 return( POLARSSL_ERR_SSL_INVALID_MAC );
1435 }
1436 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1437
Paul Bakker68884e32013-01-07 18:20:04 +01001438 dec_msg = ssl->in_msg;
1439 dec_msg_result = ssl->in_msg;
1440 ssl->in_msglen = dec_msglen;
1441
1442 memcpy( add_data, ssl->in_ctr, 8 );
1443 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001444 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1445 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001446 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1447 add_data[12] = ssl->in_msglen & 0xFF;
1448
1449 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1450 add_data, 13 );
1451
1452 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1453 ssl->in_iv,
1454 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1455
1456 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1457 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001458 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001459
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001460 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001461 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001463 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1464 ssl->transform_in->iv_dec,
1465 ssl->transform_in->ivlen,
1466 add_data, 13,
1467 dec_msg, dec_msglen,
1468 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001469 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001471 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1472
1473 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1474 return( POLARSSL_ERR_SSL_INVALID_MAC );
1475
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001476 return( ret );
1477 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001479 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001480 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001481 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001482 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001483 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001487#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1488 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001489 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
Paul Bakker45829992013-01-03 14:52:21 +01001491 /*
1492 * Decrypt and check the padding
1493 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001494 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001495 unsigned char *dec_msg;
1496 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001497 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001498 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001499 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 /*
Paul Bakker45829992013-01-03 14:52:21 +01001502 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 */
Paul Bakker48916f92012-09-16 19:57:18 +00001504 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 {
1506 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001507 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001508 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 }
1510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001512 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1513 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001514#endif
Paul Bakker45829992013-01-03 14:52:21 +01001515
1516 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1517 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1518 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001519 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1520 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1521 ssl->transform_in->ivlen,
1522 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001523 return( POLARSSL_ERR_SSL_INVALID_MAC );
1524 }
1525
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526 dec_msglen = ssl->in_msglen;
1527 dec_msg = ssl->in_msg;
1528 dec_msg_result = ssl->in_msg;
1529
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001530#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001531 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001532 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001533 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001534 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535 {
Paul Bakker48916f92012-09-16 19:57:18 +00001536 dec_msglen -= ssl->transform_in->ivlen;
1537 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538
Paul Bakker48916f92012-09-16 19:57:18 +00001539 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001540 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001542#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001544 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001545 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001546 ssl->transform_in->ivlen,
1547 dec_msg, dec_msglen,
1548 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001550 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001551 return( ret );
1552 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001553
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 if( dec_msglen != olen )
1555 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001557 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559
1560#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1562 {
1563 /*
1564 * Save IV in SSL3 and TLS1
1565 */
1566 memcpy( ssl->transform_in->iv_dec,
1567 ssl->transform_in->cipher_ctx_dec.iv,
1568 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001573
1574 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1575 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001577 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1578 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001579#endif
Paul Bakker45829992013-01-03 14:52:21 +01001580 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001581 correct = 0;
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001584#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1586 {
Paul Bakker48916f92012-09-16 19:57:18 +00001587 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001589#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1591 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001592 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001593#endif
Paul Bakker45829992013-01-03 14:52:21 +01001594 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
1596 }
1597 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001598#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001599#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1600 defined(POLARSSL_SSL_PROTO_TLS1_2)
1601 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 {
1603 /*
Paul Bakker45829992013-01-03 14:52:21 +01001604 * TLSv1+: always check the padding up to the first failure
1605 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001607 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001608 size_t padding_idx = ssl->in_msglen - padlen - 1;
1609
Paul Bakker956c9e02013-12-19 14:42:28 +01001610 /*
1611 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001612 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001613 *
Paul Bakker61885c72014-04-25 12:59:03 +02001614 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1615 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001616 *
1617 * In both cases we reset padding_idx to a safe value (0) to
1618 * prevent out-of-buffer reads.
1619 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001620 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001621 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1622 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001623
1624 padding_idx *= correct;
1625
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001626 for( i = 1; i <= 256; i++ )
1627 {
1628 real_count &= ( i <= padlen );
1629 pad_count += real_count *
1630 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1631 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001632
1633 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634
Paul Bakkerd66f0702013-01-31 16:57:45 +01001635#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001636 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001637 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001638#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001639 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001641 else
1642#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1643 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001644 {
1645 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001646 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001649 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001650#endif /* POLARSSL_CIPHER_MODE_CBC &&
1651 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001652 {
1653 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001654 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1658 ssl->in_msg, ssl->in_msglen );
1659
1660 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001661 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1664 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1665 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001666 if( mode != POLARSSL_MODE_GCM &&
1667 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001669 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1670
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001673 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1674 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001678#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1680 {
1681 ssl_mac( &ssl->transform_in->md_ctx_dec,
1682 ssl->transform_in->mac_dec,
1683 ssl->in_msg, ssl->in_msglen,
1684 ssl->in_ctr, ssl->in_msgtype );
1685 }
1686 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001687#endif /* POLARSSL_SSL_PROTO_SSL3 */
1688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001689 defined(POLARSSL_SSL_PROTO_TLS1_2)
1690 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1691 {
1692 /*
1693 * Process MAC and always update for padlen afterwards to make
1694 * total time independent of padlen
1695 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001696 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001697 *
1698 * Known timing attacks:
1699 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1700 *
1701 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1702 * correctly. (We round down instead of up, so -56 is the correct
1703 * value for our calculations instead of -55)
1704 */
1705 size_t j, extra_run = 0;
1706 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1707 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001708
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001709 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001710
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001711 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1712 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1713 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1715 ssl->in_msglen );
1716 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1717 ssl->in_msg + ssl->in_msglen );
1718 for( j = 0; j < extra_run; j++ )
1719 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1722 }
1723 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001724#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 POLARSSL_SSL_PROTO_TLS1_2 */
1726 {
1727 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001728 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1732 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1733 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001735 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 ssl->transform_in->maclen ) != 0 )
1737 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 correct = 0;
1742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 /*
1745 * Finally check the correct flag
1746 */
1747 if( correct == 0 )
1748 return( POLARSSL_ERR_SSL_INVALID_MAC );
1749 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001750#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 if( ssl->in_msglen == 0 )
1753 {
1754 ssl->nb_zero++;
1755
1756 /*
1757 * Three or more empty messages may be a DoS attack
1758 * (excessive CPU consumption).
1759 */
1760 if( ssl->nb_zero > 3 )
1761 {
1762 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1763 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001764 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
1766 }
1767 else
1768 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001769
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001770#if defined(POLARSSL_SSL_PROTO_DTLS)
1771 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001772 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001773 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001774 }
1775 else
1776#endif
1777 {
1778 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1779 if( ++ssl->in_ctr[i - 1] != 0 )
1780 break;
1781
1782 /* The loop goes to its end iff the counter is wrapping */
1783 if( i == ssl_ep_len( ssl ) )
1784 {
1785 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1786 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1787 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001788 }
1789
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1791
1792 return( 0 );
1793}
1794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795#if defined(POLARSSL_ZLIB_SUPPORT)
1796/*
1797 * Compression/decompression functions
1798 */
1799static int ssl_compress_buf( ssl_context *ssl )
1800{
1801 int ret;
1802 unsigned char *msg_post = ssl->out_msg;
1803 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001804 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805
1806 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1807
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001808 if( len_pre == 0 )
1809 return( 0 );
1810
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811 memcpy( msg_pre, ssl->out_msg, len_pre );
1812
1813 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1814 ssl->out_msglen ) );
1815
1816 SSL_DEBUG_BUF( 4, "before compression: output payload",
1817 ssl->out_msg, ssl->out_msglen );
1818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1820 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1821 ssl->transform_out->ctx_deflate.next_out = msg_post;
1822 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823
Paul Bakker48916f92012-09-16 19:57:18 +00001824 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001825 if( ret != Z_OK )
1826 {
1827 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1828 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1829 }
1830
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001831 ssl->out_msglen = SSL_BUFFER_LEN -
1832 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1835 ssl->out_msglen ) );
1836
1837 SSL_DEBUG_BUF( 4, "after compression: output payload",
1838 ssl->out_msg, ssl->out_msglen );
1839
1840 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1841
1842 return( 0 );
1843}
1844
1845static int ssl_decompress_buf( ssl_context *ssl )
1846{
1847 int ret;
1848 unsigned char *msg_post = ssl->in_msg;
1849 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001850 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851
1852 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1853
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001854 if( len_pre == 0 )
1855 return( 0 );
1856
Paul Bakker2770fbd2012-07-03 13:30:23 +00001857 memcpy( msg_pre, ssl->in_msg, len_pre );
1858
1859 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1860 ssl->in_msglen ) );
1861
1862 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1863 ssl->in_msg, ssl->in_msglen );
1864
Paul Bakker48916f92012-09-16 19:57:18 +00001865 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1866 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1867 ssl->transform_in->ctx_inflate.next_out = msg_post;
1868 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001869
Paul Bakker48916f92012-09-16 19:57:18 +00001870 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001871 if( ret != Z_OK )
1872 {
1873 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1874 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1875 }
1876
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001877 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1878 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
Paul Bakker2770fbd2012-07-03 13:30:23 +00001880 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1881 ssl->in_msglen ) );
1882
1883 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1884 ssl->in_msg, ssl->in_msglen );
1885
1886 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1887
1888 return( 0 );
1889}
1890#endif /* POLARSSL_ZLIB_SUPPORT */
1891
Paul Bakker5121ce52009-01-03 21:22:43 +00001892/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001893 * Fill the input message buffer by appending data to it.
1894 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001895 *
1896 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1897 * available (from this read and/or a previous one). Otherwise, an error code
1898 * is returned (possibly EOF or WANT_READ).
1899 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001900 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1901 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1902 * since we always read a whole datagram at once.
1903 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001904 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001905 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 */
Paul Bakker23986e52011-04-24 08:57:21 +00001907int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001908{
Paul Bakker23986e52011-04-24 08:57:21 +00001909 int ret;
1910 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
1912 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1913
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001914 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1915 {
1916 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1917 "or ssl_set_bio_timeout()" ) );
1918 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1919 }
1920
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001921 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001922 {
1923 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1924 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1925 }
1926
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001927#if defined(POLARSSL_SSL_PROTO_DTLS)
1928 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001930 uint32_t timeout;
1931
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001932 /*
1933 * The point is, we need to always read a full datagram at once, so we
1934 * sometimes read more then requested, and handle the additional data.
1935 * It could be the rest of the current record (while fetching the
1936 * header) and/or some other records in the same datagram.
1937 */
1938
1939 /*
1940 * Move to the next record in the already read datagram if applicable
1941 */
1942 if( ssl->next_record_offset != 0 )
1943 {
1944 if( ssl->in_left < ssl->next_record_offset )
1945 {
1946 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1947 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1948 }
1949
1950 ssl->in_left -= ssl->next_record_offset;
1951
1952 if( ssl->in_left != 0 )
1953 {
1954 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1955 ssl->next_record_offset ) );
1956 memmove( ssl->in_hdr,
1957 ssl->in_hdr + ssl->next_record_offset,
1958 ssl->in_left );
1959 }
1960
1961 ssl->next_record_offset = 0;
1962 }
1963
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1965 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001966
1967 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001968 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001969 */
1970 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001971 {
1972 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001973 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001974 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001975
1976 /*
1977 * A record can't be split accross datagrams. If we need to read but
1978 * are not at the beginning of a new record, the caller did something
1979 * wrong.
1980 */
1981 if( ssl->in_left != 0 )
1982 {
1983 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1984 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1985 }
1986
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001987 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001988
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001989 /*
1990 * Don't even try to read if time's out already.
1991 * This avoids by-passing the timer when repeatedly receiving messages
1992 * that will end up being dropped.
1993 */
1994 if( ssl_check_timer( ssl ) != 0 )
1995 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001996 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001997 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001998 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
1999
2000 if( ssl->state != SSL_HANDSHAKE_OVER )
2001 timeout = ssl->handshake->retransmit_timeout;
2002 else
2003 timeout = ssl->read_timeout;
2004
2005 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2006
2007 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2008 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2009 timeout );
2010 else
2011 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2012
2013 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2014
2015 if( ret == 0 )
2016 return( POLARSSL_ERR_SSL_CONN_EOF );
2017 }
2018
2019 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2020 {
2021 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2022 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002023
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002024 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002025 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002026 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2027 {
2028 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2029 return( POLARSSL_ERR_NET_TIMEOUT );
2030 }
2031
2032 if( ( ret = ssl_resend( ssl ) ) != 0 )
2033 {
2034 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2035 return( ret );
2036 }
2037
2038 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002039 }
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002040 }
2041
Paul Bakker5121ce52009-01-03 21:22:43 +00002042 if( ret < 0 )
2043 return( ret );
2044
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002045 ssl->in_left = ret;
2046 }
2047 else
2048#endif
2049 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002050 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2051 ssl->in_left, nb_want ) );
2052
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002053 while( ssl->in_left < nb_want )
2054 {
2055 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002056 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002057
2058 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2059 ssl->in_left, nb_want ) );
2060 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2061
2062 if( ret == 0 )
2063 return( POLARSSL_ERR_SSL_CONN_EOF );
2064
2065 if( ret < 0 )
2066 return( ret );
2067
2068 ssl->in_left += ret;
2069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 }
2071
2072 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2073
2074 return( 0 );
2075}
2076
2077/*
2078 * Flush any data not yet written
2079 */
2080int ssl_flush_output( ssl_context *ssl )
2081{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002082 int ret;
2083 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
2085 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2086
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002087 if( ssl->f_send == NULL )
2088 {
2089 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2090 "or ssl_set_bio_timeout()" ) );
2091 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2092 }
2093
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002094 /* Avoid incrementing counter if data is flushed */
2095 if( ssl->out_left == 0 )
2096 {
2097 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2098 return( 0 );
2099 }
2100
Paul Bakker5121ce52009-01-03 21:22:43 +00002101 while( ssl->out_left > 0 )
2102 {
2103 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002104 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002106 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2107 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002108 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002109
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2111
2112 if( ret <= 0 )
2113 return( ret );
2114
2115 ssl->out_left -= ret;
2116 }
2117
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002118 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002119 if( ++ssl->out_ctr[i - 1] != 0 )
2120 break;
2121
2122 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002123 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002124 {
2125 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2126 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2127 }
2128
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2130
2131 return( 0 );
2132}
2133
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002134/*
2135 * Functions to handle the DTLS retransmission state machine
2136 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002137#if defined(POLARSSL_SSL_PROTO_DTLS)
2138/*
2139 * Append current handshake message to current outgoing flight
2140 */
2141static int ssl_flight_append( ssl_context *ssl )
2142{
2143 ssl_flight_item *msg;
2144
2145 /* Allocate space for current message */
2146 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2147 {
2148 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2149 sizeof( ssl_flight_item ) ) );
2150 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2151 }
2152
2153 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2154 {
2155 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2156 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2157 }
2158
2159 /* Copy current handshake message with headers */
2160 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2161 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002162 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002163 msg->next = NULL;
2164
2165 /* Append to the current flight */
2166 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002167 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002168 else
2169 {
2170 ssl_flight_item *cur = ssl->handshake->flight;
2171 while( cur->next != NULL )
2172 cur = cur->next;
2173 cur->next = msg;
2174 }
2175
2176 return( 0 );
2177}
2178
2179/*
2180 * Free the current flight of handshake messages
2181 */
2182static void ssl_flight_free( ssl_flight_item *flight )
2183{
2184 ssl_flight_item *cur = flight;
2185 ssl_flight_item *next;
2186
2187 while( cur != NULL )
2188 {
2189 next = cur->next;
2190
2191 polarssl_free( cur->p );
2192 polarssl_free( cur );
2193
2194 cur = next;
2195 }
2196}
2197
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002198#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2199static void ssl_dtls_replay_reset( ssl_context *ssl );
2200#endif
2201
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002202/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002203 * Swap transform_out and out_ctr with the alternative ones
2204 */
2205static void ssl_swap_epochs( ssl_context *ssl )
2206{
2207 ssl_transform *tmp_transform;
2208 unsigned char tmp_out_ctr[8];
2209
2210 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2211 {
2212 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2213 return;
2214 }
2215
2216 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2217
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002218 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002219 tmp_transform = ssl->transform_out;
2220 ssl->transform_out = ssl->handshake->alt_transform_out;
2221 ssl->handshake->alt_transform_out = tmp_transform;
2222
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002223 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002224 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2225 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2226 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002227
2228 /* Adjust to the newly activated transform */
2229 if( ssl->transform_out != NULL &&
2230 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2231 {
2232 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2233 ssl->transform_out->fixed_ivlen;
2234 }
2235 else
2236 ssl->out_msg = ssl->out_iv;
2237
2238#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2239 if( ssl_hw_record_activate != NULL )
2240 {
2241 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2242 {
2243 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2244 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2245 }
2246 }
2247#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002248}
2249
2250/*
2251 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002252 *
2253 * Need to remember the current message in case flush_output returns
2254 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002255 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002256 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002257int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002258{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002259 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002260
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002261 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2262 {
2263 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2264
2265 ssl->handshake->cur_msg = ssl->handshake->flight;
2266 ssl_swap_epochs( ssl );
2267
2268 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2269 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002270
2271 while( ssl->handshake->cur_msg != NULL )
2272 {
2273 int ret;
2274 ssl_flight_item *cur = ssl->handshake->cur_msg;
2275
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002276 /* Swap epochs before sending Finished: we can't do it after
2277 * sending ChangeCipherSpec, in case write returns WANT_READ.
2278 * Must be done before copying, may change out_msg pointer */
2279 if( cur->type == SSL_MSG_HANDSHAKE &&
2280 cur->p[0] == SSL_HS_FINISHED )
2281 {
2282 ssl_swap_epochs( ssl );
2283 }
2284
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002285 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002286 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002287 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002288
2289 ssl->handshake->cur_msg = cur->next;
2290
2291 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2292
2293 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2294 {
2295 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2296 return( ret );
2297 }
2298 }
2299
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002300 if( ssl->state == SSL_HANDSHAKE_OVER )
2301 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2302 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002303 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002304 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002305 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2306 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002307
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002308 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002309
2310 return( 0 );
2311}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002312
2313/*
2314 * To be called when the last message of an incoming flight is received.
2315 */
2316void ssl_recv_flight_completed( ssl_context *ssl )
2317{
2318 /* We won't need to resend that one any more */
2319 ssl_flight_free( ssl->handshake->flight );
2320 ssl->handshake->flight = NULL;
2321 ssl->handshake->cur_msg = NULL;
2322
2323 /* The next incoming flight will start with this msg_seq */
2324 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2325
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002326 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002327 ssl_set_timer( ssl, 0 );
2328
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002329 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2330 ssl->in_msg[0] == SSL_HS_FINISHED )
2331 {
2332 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2333 }
2334 else
2335 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2336}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002337
2338/*
2339 * To be called when the last message of an outgoing flight is send.
2340 */
2341void ssl_send_flight_completed( ssl_context *ssl )
2342{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002343 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002344 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002345
2346 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2347 ssl->in_msg[0] == SSL_HS_FINISHED )
2348 {
2349 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2350 }
2351 else
2352 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2353}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002354#endif /* POLARSSL_SSL_PROTO_DTLS */
2355
Paul Bakker5121ce52009-01-03 21:22:43 +00002356/*
2357 * Record layer functions
2358 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002359
2360/*
2361 * Write current record.
2362 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2363 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002364int ssl_write_record( ssl_context *ssl )
2365{
Paul Bakker05ef8352012-05-08 09:17:57 +00002366 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002367 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002368
2369 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2370
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002371#if defined(POLARSSL_SSL_PROTO_DTLS)
2372 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2373 ssl->handshake != NULL &&
2374 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2375 {
2376 ; /* Skip special handshake treatment when resending */
2377 }
2378 else
2379#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2381 {
2382 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2383 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2384 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2385
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002386 /*
2387 * DTLS has additional fields in the Handshake layer,
2388 * between the length field and the actual payload:
2389 * uint16 message_seq;
2390 * uint24 fragment_offset;
2391 * uint24 fragment_length;
2392 */
2393#if defined(POLARSSL_SSL_PROTO_DTLS)
2394 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2395 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002396 /* Make room for the additional DTLS fields */
2397 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002398 ssl->out_msglen += 8;
2399 len += 8;
2400
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002401 /* Write message_seq and update it, except for HelloRequest */
2402 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2403 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002404 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2405 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2406 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002407 }
2408 else
2409 {
2410 ssl->out_msg[4] = 0;
2411 ssl->out_msg[5] = 0;
2412 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002413
2414 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2415 memset( ssl->out_msg + 6, 0x00, 3 );
2416 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002417 }
2418#endif /* POLARSSL_SSL_PROTO_DTLS */
2419
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002420 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2421 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
2423
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002424 /* Save handshake and CCS messages for resending */
2425#if defined(POLARSSL_SSL_PROTO_DTLS)
2426 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2427 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002428 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002429 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2430 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2431 {
2432 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2433 {
2434 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2435 return( ret );
2436 }
2437 }
2438#endif
2439
Paul Bakker2770fbd2012-07-03 13:30:23 +00002440#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002441 if( ssl->transform_out != NULL &&
2442 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443 {
2444 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2445 {
2446 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2447 return( ret );
2448 }
2449
2450 len = ssl->out_msglen;
2451 }
2452#endif /*POLARSSL_ZLIB_SUPPORT */
2453
Paul Bakker05ef8352012-05-08 09:17:57 +00002454#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002455 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002457 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002458
Paul Bakker05ef8352012-05-08 09:17:57 +00002459 ret = ssl_hw_record_write( ssl );
2460 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2461 {
2462 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002463 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002464 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002465
2466 if( ret == 0 )
2467 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002468 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002469#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002470 if( !done )
2471 {
2472 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002473 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2474 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002475
2476 ssl->out_len[0] = (unsigned char)( len >> 8 );
2477 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002478
Paul Bakker48916f92012-09-16 19:57:18 +00002479 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002480 {
2481 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2482 {
2483 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2484 return( ret );
2485 }
2486
2487 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002488 ssl->out_len[0] = (unsigned char)( len >> 8 );
2489 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002490 }
2491
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002492 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002493
2494 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2495 "version = [%d:%d], msglen = %d",
2496 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002497 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002498
2499 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002500 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 }
2502
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2504 {
2505 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2506 return( ret );
2507 }
2508
2509 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2510
2511 return( 0 );
2512}
2513
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002514#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002515/*
2516 * Mark bits in bitmask (used for DTLS HS reassembly)
2517 */
2518static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2519{
2520 unsigned int start_bits, end_bits;
2521
2522 start_bits = 8 - ( offset % 8 );
2523 if( start_bits != 8 )
2524 {
2525 size_t first_byte_idx = offset / 8;
2526
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002527 /* Special case */
2528 if( len <= start_bits )
2529 {
2530 for( ; len != 0; len-- )
2531 mask[first_byte_idx] |= 1 << ( start_bits - len );
2532
2533 /* Avoid potential issues with offset or len becoming invalid */
2534 return;
2535 }
2536
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002537 offset += start_bits; /* Now offset % 8 == 0 */
2538 len -= start_bits;
2539
2540 for( ; start_bits != 0; start_bits-- )
2541 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2542 }
2543
2544 end_bits = len % 8;
2545 if( end_bits != 0 )
2546 {
2547 size_t last_byte_idx = ( offset + len ) / 8;
2548
2549 len -= end_bits; /* Now len % 8 == 0 */
2550
2551 for( ; end_bits != 0; end_bits-- )
2552 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2553 }
2554
2555 memset( mask + offset / 8, 0xFF, len / 8 );
2556}
2557
2558/*
2559 * Check that bitmask is full
2560 */
2561static int ssl_bitmask_check( unsigned char *mask, size_t len )
2562{
2563 size_t i;
2564
2565 for( i = 0; i < len / 8; i++ )
2566 if( mask[i] != 0xFF )
2567 return( -1 );
2568
2569 for( i = 0; i < len % 8; i++ )
2570 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2571 return( -1 );
2572
2573 return( 0 );
2574}
2575
2576/*
2577 * Reassemble fragmented DTLS handshake messages.
2578 *
2579 * Use a temporary buffer for reassembly, divided in two parts:
2580 * - the first holds the reassembled message (including handshake header),
2581 * - the second holds a bitmask indicating which parts of the message
2582 * (excluding headers) have been received so far.
2583 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002584static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2585{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002586 unsigned char *msg, *bitmask;
2587 size_t frag_len, frag_off;
2588 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2589
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002590 if( ssl->handshake == NULL )
2591 {
2592 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2593 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2594 }
2595
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002596 /*
2597 * For first fragment, check size and allocate buffer
2598 */
2599 if( ssl->handshake->hs_msg == NULL )
2600 {
2601 size_t alloc_len;
2602
2603 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2604 msg_len ) );
2605
2606 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2607 {
2608 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2609 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2610 }
2611
2612 /* The bitmask needs one bit per byte of message excluding header */
2613 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2614
2615 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2616 if( ssl->handshake->hs_msg == NULL )
2617 {
2618 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2619 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2620 }
2621
2622 memset( ssl->handshake->hs_msg, 0, alloc_len );
2623
2624 /* Prepare final header: copy msg_type, length and message_seq,
2625 * then add standardised fragment_offset and fragment_length */
2626 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2627 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2628 memcpy( ssl->handshake->hs_msg + 9,
2629 ssl->handshake->hs_msg + 1, 3 );
2630 }
2631 else
2632 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002633 /* Make sure msg_type and length are consistent */
2634 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002635 {
2636 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2637 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2638 }
2639 }
2640
2641 msg = ssl->handshake->hs_msg + 12;
2642 bitmask = msg + msg_len;
2643
2644 /*
2645 * Check and copy current fragment
2646 */
2647 frag_off = ( ssl->in_msg[6] << 16 ) |
2648 ( ssl->in_msg[7] << 8 ) |
2649 ssl->in_msg[8];
2650 frag_len = ( ssl->in_msg[9] << 16 ) |
2651 ( ssl->in_msg[10] << 8 ) |
2652 ssl->in_msg[11];
2653
2654 if( frag_off + frag_len > msg_len )
2655 {
2656 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2657 frag_off, frag_len, msg_len ) );
2658 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2659 }
2660
2661 if( frag_len + 12 > ssl->in_msglen )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2664 frag_len, ssl->in_msglen ) );
2665 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2666 }
2667
2668 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2669 frag_off, frag_len ) );
2670
2671 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2672 ssl_bitmask_set( bitmask, frag_off, frag_len );
2673
2674 /*
2675 * Do we have the complete message by now?
2676 * If yes, finalize it, else ask to read the next record.
2677 */
2678 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2679 {
2680 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2681 return( POLARSSL_ERR_NET_WANT_READ );
2682 }
2683
2684 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2685
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002686 if( frag_len + 12 < ssl->in_msglen )
2687 {
2688 /*
2689 * We'got more handshake messages in the same record.
2690 * This case is not handled now because no know implementation does
2691 * that and it's hard to test, so we prefer to fail cleanly for now.
2692 */
2693 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2694 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2695 }
2696
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002697 if( ssl->in_left > ssl->next_record_offset )
2698 {
2699 /*
2700 * We've got more data in the buffer after the current record,
2701 * that we don't want to overwrite. Move it before writing the
2702 * reassembled message, and adjust in_left and next_record_offset.
2703 */
2704 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2705 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2706 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2707
2708 /* First compute and check new lengths */
2709 ssl->next_record_offset = new_remain - ssl->in_hdr;
2710 ssl->in_left = ssl->next_record_offset + remain_len;
2711
2712 if( ssl->in_left > SSL_BUFFER_LEN -
2713 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2714 {
2715 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2716 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2717 }
2718
2719 memmove( new_remain, cur_remain, remain_len );
2720 }
2721
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002722 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2723
2724 polarssl_free( ssl->handshake->hs_msg );
2725 ssl->handshake->hs_msg = NULL;
2726
2727 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2728 ssl->in_msg, ssl->in_hslen );
2729
2730 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002731}
2732#endif /* POLARSSL_SSL_PROTO_DTLS */
2733
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002734static int ssl_prepare_handshake_record( ssl_context *ssl )
2735{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002736 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2737 {
2738 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2739 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002740 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002741 }
2742
2743 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2744 ( ssl->in_msg[1] << 16 ) |
2745 ( ssl->in_msg[2] << 8 ) |
2746 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002747
2748 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2749 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002750 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002751
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002752#if defined(POLARSSL_SSL_PROTO_DTLS)
2753 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002754 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002755 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002756 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002757
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002758 /* ssl->handshake is NULL when receiving ClientHello for renego */
2759 if( ssl->handshake != NULL &&
2760 recv_msg_seq != ssl->handshake->in_msg_seq )
2761 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002762 /* Retransmit only on last message from previous flight, to avoid
2763 * too many retransmissions.
2764 * Besides, No sane server ever retransmits HelloVerifyRequest */
2765 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002766 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002767 {
2768 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2769 "message_seq = %d, start_of_flight = %d",
2770 recv_msg_seq,
2771 ssl->handshake->in_flight_start_seq ) );
2772
2773 if( ( ret = ssl_resend( ssl ) ) != 0 )
2774 {
2775 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2776 return( ret );
2777 }
2778 }
2779 else
2780 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002781 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002782 "message_seq = %d, expected = %d",
2783 recv_msg_seq,
2784 ssl->handshake->in_msg_seq ) );
2785 }
2786
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002787 return( POLARSSL_ERR_NET_WANT_READ );
2788 }
2789 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002790
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002791 /* Reassemble if current message is fragmented or reassembly is
2792 * already in progress */
2793 if( ssl->in_msglen < ssl->in_hslen ||
2794 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2795 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2796 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002797 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002798 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2799
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002800 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2801 {
2802 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2803 return( ret );
2804 }
2805 }
2806 }
2807 else
2808#endif /* POLARSSL_SSL_PROTO_DTLS */
2809 /* With TLS we don't handle fragmentation (for now) */
2810 if( ssl->in_msglen < ssl->in_hslen )
2811 {
2812 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002813 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002814 }
2815
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002816 if( ssl->state != SSL_HANDSHAKE_OVER )
2817 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2818
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002819 /* Handshake message is complete, increment counter */
2820#if defined(POLARSSL_SSL_PROTO_DTLS)
2821 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2822 ssl->handshake != NULL )
2823 {
2824 ssl->handshake->in_msg_seq++;
2825 }
2826#endif
2827
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002828 return( 0 );
2829}
2830
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002831/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002832 * DTLS anti-replay: RFC 6347 4.1.2.6
2833 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002834 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2835 * Bit n is set iff record number in_window_top - n has been seen.
2836 *
2837 * Usually, in_window_top is the last record number seen and the lsb of
2838 * in_window is set. The only exception is the initial state (record number 0
2839 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002840 */
2841#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2842static void ssl_dtls_replay_reset( ssl_context *ssl )
2843{
2844 ssl->in_window_top = 0;
2845 ssl->in_window = 0;
2846}
2847
2848static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2849{
2850 return( ( (uint64_t) buf[0] << 40 ) |
2851 ( (uint64_t) buf[1] << 32 ) |
2852 ( (uint64_t) buf[2] << 24 ) |
2853 ( (uint64_t) buf[3] << 16 ) |
2854 ( (uint64_t) buf[4] << 8 ) |
2855 ( (uint64_t) buf[5] ) );
2856}
2857
2858/*
2859 * Return 0 if sequence number is acceptable, -1 otherwise
2860 */
2861int ssl_dtls_replay_check( ssl_context *ssl )
2862{
2863 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2864 uint64_t bit;
2865
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002866 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2867 return( 0 );
2868
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002869 if( rec_seqnum > ssl->in_window_top )
2870 return( 0 );
2871
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002872 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002873
2874 if( bit >= 64 )
2875 return( -1 );
2876
2877 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2878 return( -1 );
2879
2880 return( 0 );
2881}
2882
2883/*
2884 * Update replay window on new validated record
2885 */
2886void ssl_dtls_replay_update( ssl_context *ssl )
2887{
2888 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2889
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002890 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2891 return;
2892
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002893 if( rec_seqnum > ssl->in_window_top )
2894 {
2895 /* Update window_top and the contents of the window */
2896 uint64_t shift = rec_seqnum - ssl->in_window_top;
2897
2898 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002899 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002900 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002901 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002902 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002903 ssl->in_window |= 1;
2904 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002905
2906 ssl->in_window_top = rec_seqnum;
2907 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002908 else
2909 {
2910 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002911 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002912
2913 if( bit < 64 ) /* Always true, but be extra sure */
2914 ssl->in_window |= (uint64_t) 1 << bit;
2915 }
2916}
2917#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2918
2919/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002920 * ContentType type;
2921 * ProtocolVersion version;
2922 * uint16 epoch; // DTLS only
2923 * uint48 sequence_number; // DTLS only
2924 * uint16 length;
2925 */
2926static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002928 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002929 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002931 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2932
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002934 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002935 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
2937 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2938 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002939 ssl->in_msgtype,
2940 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002942 /* Check record type */
2943 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2944 ssl->in_msgtype != SSL_MSG_ALERT &&
2945 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2946 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2947 {
2948 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002949
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002950 if( ( ret = ssl_send_alert_message( ssl,
2951 SSL_ALERT_LEVEL_FATAL,
2952 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2953 {
2954 return( ret );
2955 }
2956
2957 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2958 }
2959
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002960#if defined(POLARSSL_SSL_PROTO_DTLS)
2961 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2962 {
2963 /* Drop unexpected ChangeCipherSpec messages */
2964 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2965 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2966 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2967 {
2968 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2969 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2970 }
2971
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002972 /* Drop unexpected ApplicationData records,
2973 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002974 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02002975 ssl->state != SSL_HANDSHAKE_OVER &&
2976 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
2977 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002978 {
2979 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2980 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2981 }
2982 }
2983#endif
2984
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002985 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002986 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002987 {
2988 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002989 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990 }
2991
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002992 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002993 {
2994 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002995 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002996 }
2997
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002998 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002999#if defined(POLARSSL_SSL_PROTO_DTLS)
3000 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3001 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003002 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003003
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003004 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003005 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003006 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003007 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003008 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003009 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003010 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003011
3012#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3013 if( ssl_dtls_replay_check( ssl ) != 0 )
3014 {
3015 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3016 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3017 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003018#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003019 }
3020#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003021
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003022 /* Check length against the size of our buffer */
3023 if( ssl->in_msglen > SSL_BUFFER_LEN
3024 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003025 {
3026 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3027 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3028 }
3029
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003030 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003031 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003033 if( ssl->in_msglen < 1 ||
3034 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 {
3036 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003037 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 }
3039 }
3040 else
3041 {
Paul Bakker48916f92012-09-16 19:57:18 +00003042 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 {
3044 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003045 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 }
3047
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003048#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003049 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003050 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 {
3052 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003053 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003055#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003056#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3057 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 /*
3059 * TLS encrypted messages can have up to 256 bytes of padding
3060 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003062 ssl->in_msglen > ssl->transform_in->minlen +
3063 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003064 {
3065 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003066 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 }
3070
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003071 return( 0 );
3072}
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003074/*
3075 * If applicable, decrypt (and decompress) record content
3076 */
3077static int ssl_prepare_record_content( ssl_context *ssl )
3078{
3079 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003080
Paul Bakker5121ce52009-01-03 21:22:43 +00003081 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003082 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083
Paul Bakker05ef8352012-05-08 09:17:57 +00003084#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003085 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003086 {
3087 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3088
3089 ret = ssl_hw_record_read( ssl );
3090 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3091 {
3092 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003093 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003094 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003095
3096 if( ret == 0 )
3097 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003098 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003099#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003100 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101 {
3102 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3103 {
3104 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3105 return( ret );
3106 }
3107
3108 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3109 ssl->in_msg, ssl->in_msglen );
3110
3111 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3112 {
3113 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003114 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 }
3116 }
3117
Paul Bakker2770fbd2012-07-03 13:30:23 +00003118#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003119 if( ssl->transform_in != NULL &&
3120 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003121 {
3122 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3123 {
3124 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3125 return( ret );
3126 }
3127
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003128 // TODO: what's the purpose of these lines? is in_len used?
3129 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3130 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003131 }
3132#endif /* POLARSSL_ZLIB_SUPPORT */
3133
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003134#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003135 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3136 {
3137 ssl_dtls_replay_update( ssl );
3138 }
3139#endif
3140
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003141 return( 0 );
3142}
3143
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003144static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3145
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003146/*
3147 * Read a record.
3148 *
3149 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3150 * and continue reading until a valid record is found.
3151 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003152int ssl_read_record( ssl_context *ssl )
3153{
3154 int ret;
3155
3156 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3157
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003158 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003159 {
3160 /*
3161 * Get next Handshake message in the current record
3162 */
3163 ssl->in_msglen -= ssl->in_hslen;
3164
3165 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3166 ssl->in_msglen );
3167
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003168 SSL_DEBUG_BUF( 4, "remaining content in record",
3169 ssl->in_msg, ssl->in_msglen );
3170
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003171 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3172 return( ret );
3173
3174 return( 0 );
3175 }
3176
3177 ssl->in_hslen = 0;
3178
3179 /*
3180 * Read the record header and parse it
3181 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003182#if defined(POLARSSL_SSL_PROTO_DTLS)
3183read_record_header:
3184#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003185 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3186 {
3187 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3188 return( ret );
3189 }
3190
3191 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003192 {
3193#if defined(POLARSSL_SSL_PROTO_DTLS)
3194 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3195 {
3196 /* Ignore bad record and get next one; drop the whole datagram
3197 * since current header cannot be trusted to find the next record
3198 * in current datagram */
3199 ssl->next_record_offset = 0;
3200 ssl->in_left = 0;
3201
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003202 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003203 goto read_record_header;
3204 }
3205#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003206 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003207 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003208
3209 /*
3210 * Read and optionally decrypt the message contents
3211 */
3212 if( ( ret = ssl_fetch_input( ssl,
3213 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3216 return( ret );
3217 }
3218
3219 /* Done reading this record, get ready for the next one */
3220#if defined(POLARSSL_SSL_PROTO_DTLS)
3221 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3222 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3223 else
3224#endif
3225 ssl->in_left = 0;
3226
3227 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003228 {
3229#if defined(POLARSSL_SSL_PROTO_DTLS)
3230 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3231 {
3232 /* Silently discard invalid records */
3233 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3234 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3235 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003236#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3237 if( ssl->badmac_limit != 0 &&
3238 ++ssl->badmac_seen >= ssl->badmac_limit )
3239 {
3240 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3241 return( POLARSSL_ERR_SSL_INVALID_MAC );
3242 }
3243#endif
3244
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003245 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003246 goto read_record_header;
3247 }
3248
3249 return( ret );
3250 }
3251 else
3252#endif
3253 {
3254 /* Error out (and send alert) on invalid records */
3255#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3256 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3257 {
3258 ssl_send_alert_message( ssl,
3259 SSL_ALERT_LEVEL_FATAL,
3260 SSL_ALERT_MSG_BAD_RECORD_MAC );
3261 }
3262#endif
3263 return( ret );
3264 }
3265 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003266
3267 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003268 * When we sent the last flight of the handshake, we MUST respond to a
3269 * retransmit of the peer's previous flight with a retransmit. (In
3270 * practice, only the Finished message will make it, other messages
3271 * including CCS use the old transform so they're dropped as invalid.)
3272 *
3273 * If the record we received is not a handshake message, however, it
3274 * means the peer received our last flight so we can clean up
3275 * handshake info.
3276 *
3277 * This check needs to be done before prepare_handshake() due to an edge
3278 * case: if the client immediately requests renegotiation, this
3279 * finishes the current handshake first, avoiding the new ClientHello
3280 * being mistaken for an ancient message in the current handshake.
3281 */
3282#if defined(POLARSSL_SSL_PROTO_DTLS)
3283 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3284 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003285 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003286 {
3287 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3288 ssl->in_msg[0] == SSL_HS_FINISHED )
3289 {
3290 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3291
3292 if( ( ret = ssl_resend( ssl ) ) != 0 )
3293 {
3294 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3295 return( ret );
3296 }
3297
3298 return( POLARSSL_ERR_NET_WANT_READ );
3299 }
3300 else
3301 {
3302 ssl_handshake_wrapup_free_hs_transform( ssl );
3303 }
3304 }
3305#endif
3306
3307 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003308 * Handle particular types of records
3309 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3311 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003312 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3313 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 }
3315
3316 if( ssl->in_msgtype == SSL_MSG_ALERT )
3317 {
3318 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3319 ssl->in_msg[0], ssl->in_msg[1] ) );
3320
3321 /*
3322 * Ignore non-fatal alerts, except close_notify
3323 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003324 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003326 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3327 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003328 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003329 }
3330
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003331 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3332 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 {
3334 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003335 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 }
3337 }
3338
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3340
3341 return( 0 );
3342}
3343
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003344int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3345{
3346 int ret;
3347
3348 if( ( ret = ssl_send_alert_message( ssl,
3349 SSL_ALERT_LEVEL_FATAL,
3350 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3351 {
3352 return( ret );
3353 }
3354
3355 return( 0 );
3356}
3357
Paul Bakker0a925182012-04-16 06:46:41 +00003358int ssl_send_alert_message( ssl_context *ssl,
3359 unsigned char level,
3360 unsigned char message )
3361{
3362 int ret;
3363
3364 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3365
3366 ssl->out_msgtype = SSL_MSG_ALERT;
3367 ssl->out_msglen = 2;
3368 ssl->out_msg[0] = level;
3369 ssl->out_msg[1] = message;
3370
3371 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3372 {
3373 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3374 return( ret );
3375 }
3376
3377 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3378
3379 return( 0 );
3380}
3381
Paul Bakker5121ce52009-01-03 21:22:43 +00003382/*
3383 * Handshake functions
3384 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003385#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3386 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3387 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3388 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3389 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3390 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3391 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003392int ssl_write_certificate( ssl_context *ssl )
3393{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003394 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003395
3396 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3397
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003399 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3400 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003401 {
3402 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3403 ssl->state++;
3404 return( 0 );
3405 }
3406
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409}
3410
3411int ssl_parse_certificate( ssl_context *ssl )
3412{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003413 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3414
3415 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3416
3417 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003418 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3419 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003420 {
3421 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3422 ssl->state++;
3423 return( 0 );
3424 }
3425
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003426 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3427 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003428}
3429#else
3430int ssl_write_certificate( ssl_context *ssl )
3431{
3432 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3433 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003434 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003435 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3436
3437 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3438
3439 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003440 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3441 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003442 {
3443 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3444 ssl->state++;
3445 return( 0 );
3446 }
3447
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 if( ssl->endpoint == SSL_IS_CLIENT )
3449 {
3450 if( ssl->client_auth == 0 )
3451 {
3452 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3453 ssl->state++;
3454 return( 0 );
3455 }
3456
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003457#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 /*
3459 * If using SSLv3 and got no cert, send an Alert message
3460 * (otherwise an empty Certificate message will be sent).
3461 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003462 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3464 {
3465 ssl->out_msglen = 2;
3466 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003467 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3468 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003469
3470 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3471 goto write_msg;
3472 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003473#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 }
3475 else /* SSL_IS_SERVER */
3476 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003477 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003478 {
3479 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003480 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 }
3482 }
3483
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003484 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003485
3486 /*
3487 * 0 . 0 handshake type
3488 * 1 . 3 handshake length
3489 * 4 . 6 length of all certs
3490 * 7 . 9 length of cert. 1
3491 * 10 . n-1 peer certificate
3492 * n . n+2 length of cert. 2
3493 * n+3 . ... upper level cert, etc.
3494 */
3495 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003496 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
Paul Bakker29087132010-03-21 21:03:34 +00003498 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 {
3500 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003501 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003502 {
3503 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3504 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003505 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 }
3507
3508 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3509 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3510 ssl->out_msg[i + 2] = (unsigned char)( n );
3511
3512 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3513 i += n; crt = crt->next;
3514 }
3515
3516 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3517 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3518 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3519
3520 ssl->out_msglen = i;
3521 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3522 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3523
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003524#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003525write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003526#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
3528 ssl->state++;
3529
3530 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3531 {
3532 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3533 return( ret );
3534 }
3535
3536 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3537
Paul Bakkered27a042013-04-18 22:46:23 +02003538 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003539}
3540
3541int ssl_parse_certificate( ssl_context *ssl )
3542{
Paul Bakkered27a042013-04-18 22:46:23 +02003543 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003544 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003545 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
3547 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3548
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003549 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003550 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3551 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003552 {
3553 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3554 ssl->state++;
3555 return( 0 );
3556 }
3557
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003559 ( ssl->authmode == SSL_VERIFY_NONE ||
3560 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003562 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3564 ssl->state++;
3565 return( 0 );
3566 }
3567
3568 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3569 {
3570 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3571 return( ret );
3572 }
3573
3574 ssl->state++;
3575
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003576#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003577 /*
3578 * Check if the client sent an empty certificate
3579 */
3580 if( ssl->endpoint == SSL_IS_SERVER &&
3581 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3582 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003583 if( ssl->in_msglen == 2 &&
3584 ssl->in_msgtype == SSL_MSG_ALERT &&
3585 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3586 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 {
3588 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3589
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003590 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3592 return( 0 );
3593 else
Paul Bakker40e46942009-01-03 21:51:57 +00003594 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 }
3596 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003597#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003599#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3600 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 if( ssl->endpoint == SSL_IS_SERVER &&
3602 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3603 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003604 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3606 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003607 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 {
3609 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3610
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003611 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003613 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 else
3615 return( 0 );
3616 }
3617 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003618#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3619 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003620
3621 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3622 {
3623 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003624 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003625 }
3626
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003627 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3628 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 {
3630 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003631 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 }
3633
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003634 i = ssl_hs_hdr_len( ssl );
3635
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 /*
3637 * Same message structure as in ssl_write_certificate()
3638 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003639 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003640
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003641 if( ssl->in_msg[i] != 0 ||
3642 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 {
3644 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003645 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 }
3647
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003648 /* In case we tried to reuse a session but it failed */
3649 if( ssl->session_negotiate->peer_cert != NULL )
3650 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003651 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003652 polarssl_free( ssl->session_negotiate->peer_cert );
3653 }
3654
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003655 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3656 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 {
3658 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003659 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003660 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 }
3662
Paul Bakkerb6b09562013-09-18 14:17:41 +02003663 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003664
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003665 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003666
3667 while( i < ssl->in_hslen )
3668 {
3669 if( ssl->in_msg[i] != 0 )
3670 {
3671 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003672 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 }
3674
3675 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3676 | (unsigned int) ssl->in_msg[i + 2];
3677 i += 3;
3678
3679 if( n < 128 || i + n > ssl->in_hslen )
3680 {
3681 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003682 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 }
3684
Paul Bakkerddf26b42013-09-18 13:46:23 +02003685 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3686 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 if( ret != 0 )
3688 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003689 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690 return( ret );
3691 }
3692
3693 i += n;
3694 }
3695
Paul Bakker48916f92012-09-16 19:57:18 +00003696 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003697
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003698 /*
3699 * On client, make sure the server cert doesn't change during renego to
3700 * avoid "triple handshake" attack: https://secure-resumption.com/
3701 */
3702 if( ssl->endpoint == SSL_IS_CLIENT &&
3703 ssl->renegotiation == SSL_RENEGOTIATION )
3704 {
3705 if( ssl->session->peer_cert == NULL )
3706 {
3707 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3708 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3709 }
3710
3711 if( ssl->session->peer_cert->raw.len !=
3712 ssl->session_negotiate->peer_cert->raw.len ||
3713 memcmp( ssl->session->peer_cert->raw.p,
3714 ssl->session_negotiate->peer_cert->raw.p,
3715 ssl->session->peer_cert->raw.len ) != 0 )
3716 {
3717 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3718 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3719 }
3720 }
3721
Paul Bakker5121ce52009-01-03 21:22:43 +00003722 if( ssl->authmode != SSL_VERIFY_NONE )
3723 {
3724 if( ssl->ca_chain == NULL )
3725 {
3726 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003727 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 }
3729
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003730 /*
3731 * Main check: verify certificate
3732 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003733 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3734 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3735 &ssl->session_negotiate->verify_result,
3736 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
3738 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003739 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003740 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003741 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003742
3743 /*
3744 * Secondary checks: always done, but change 'ret' only if it was 0
3745 */
3746
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003747#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003748 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003749 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003750
3751 /* If certificate uses an EC key, make sure the curve is OK */
3752 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3753 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3754 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003755 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003756 if( ret == 0 )
3757 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003758 }
3759 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003760#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003761
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003762 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3763 ciphersuite_info,
3764 ! ssl->endpoint ) != 0 )
3765 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003766 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003767 if( ret == 0 )
3768 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3769 }
3770
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3772 ret = 0;
3773 }
3774
3775 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3776
3777 return( ret );
3778}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003779#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3780 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3781 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3782 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3783 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3784 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3785 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
3787int ssl_write_change_cipher_spec( ssl_context *ssl )
3788{
3789 int ret;
3790
3791 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3792
3793 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3794 ssl->out_msglen = 1;
3795 ssl->out_msg[0] = 1;
3796
Paul Bakker5121ce52009-01-03 21:22:43 +00003797 ssl->state++;
3798
3799 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3800 {
3801 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3802 return( ret );
3803 }
3804
3805 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3806
3807 return( 0 );
3808}
3809
3810int ssl_parse_change_cipher_spec( ssl_context *ssl )
3811{
3812 int ret;
3813
3814 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3815
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3817 {
3818 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3819 return( ret );
3820 }
3821
3822 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3823 {
3824 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003825 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003826 }
3827
3828 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3829 {
3830 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003831 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003832 }
3833
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003834 /*
3835 * Switch to our negotiated transform and session parameters for inbound
3836 * data.
3837 */
3838 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3839 ssl->transform_in = ssl->transform_negotiate;
3840 ssl->session_in = ssl->session_negotiate;
3841
3842#if defined(POLARSSL_SSL_PROTO_DTLS)
3843 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3844 {
3845#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3846 ssl_dtls_replay_reset( ssl );
3847#endif
3848
3849 /* Increment epoch */
3850 if( ++ssl->in_epoch == 0 )
3851 {
3852 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3853 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3854 }
3855 }
3856 else
3857#endif /* POLARSSL_SSL_PROTO_DTLS */
3858 memset( ssl->in_ctr, 0, 8 );
3859
3860 /*
3861 * Set the in_msg pointer to the correct location based on IV length
3862 */
3863 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3864 {
3865 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3866 ssl->transform_negotiate->fixed_ivlen;
3867 }
3868 else
3869 ssl->in_msg = ssl->in_iv;
3870
3871#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3872 if( ssl_hw_record_activate != NULL )
3873 {
3874 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3875 {
3876 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3877 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3878 }
3879 }
3880#endif
3881
Paul Bakker5121ce52009-01-03 21:22:43 +00003882 ssl->state++;
3883
3884 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3885
3886 return( 0 );
3887}
3888
Paul Bakker41c83d32013-03-20 14:39:14 +01003889void ssl_optimize_checksum( ssl_context *ssl,
3890 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003891{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003892 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003893
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003894#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3895 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003896 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003897 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003898 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003899#endif
3900#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3901#if defined(POLARSSL_SHA512_C)
3902 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3903 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3904 else
3905#endif
3906#if defined(POLARSSL_SHA256_C)
3907 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003908 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003909 else
3910#endif
3911#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003912 {
3913 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003914 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003915 }
Paul Bakker380da532012-04-18 16:10:25 +00003916}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003917
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003918void ssl_reset_checksum( ssl_context *ssl )
3919{
3920#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3921 defined(POLARSSL_SSL_PROTO_TLS1_1)
3922 md5_starts( &ssl->handshake->fin_md5 );
3923 sha1_starts( &ssl->handshake->fin_sha1 );
3924#endif
3925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3926#if defined(POLARSSL_SHA256_C)
3927 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3928#endif
3929#if defined(POLARSSL_SHA512_C)
3930 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3931#endif
3932#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3933}
3934
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003935static void ssl_update_checksum_start( ssl_context *ssl,
3936 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003937{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003938#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3939 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003940 md5_update( &ssl->handshake->fin_md5 , buf, len );
3941 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003942#endif
3943#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3944#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003945 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003946#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003947#if defined(POLARSSL_SHA512_C)
3948 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003949#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003950#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003951}
3952
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003953#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3954 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003955static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3956 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003957{
Paul Bakker48916f92012-09-16 19:57:18 +00003958 md5_update( &ssl->handshake->fin_md5 , buf, len );
3959 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003960}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003961#endif
Paul Bakker380da532012-04-18 16:10:25 +00003962
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003963#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3964#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003965static void ssl_update_checksum_sha256( ssl_context *ssl,
3966 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003967{
Paul Bakker9e36f042013-06-30 14:34:05 +02003968 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003969}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003970#endif
Paul Bakker380da532012-04-18 16:10:25 +00003971
Paul Bakker9e36f042013-06-30 14:34:05 +02003972#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003973static void ssl_update_checksum_sha384( ssl_context *ssl,
3974 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003975{
Paul Bakker9e36f042013-06-30 14:34:05 +02003976 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003977}
Paul Bakker769075d2012-11-24 11:26:46 +01003978#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003979#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003980
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003981#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003982static void ssl_calc_finished_ssl(
3983 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003984{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003985 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003986 md5_context md5;
3987 sha1_context sha1;
3988
Paul Bakker5121ce52009-01-03 21:22:43 +00003989 unsigned char padbuf[48];
3990 unsigned char md5sum[16];
3991 unsigned char sha1sum[20];
3992
Paul Bakker48916f92012-09-16 19:57:18 +00003993 ssl_session *session = ssl->session_negotiate;
3994 if( !session )
3995 session = ssl->session;
3996
Paul Bakker1ef83d62012-04-11 12:09:53 +00003997 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3998
Paul Bakker48916f92012-09-16 19:57:18 +00003999 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4000 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004001
4002 /*
4003 * SSLv3:
4004 * hash =
4005 * MD5( master + pad2 +
4006 * MD5( handshake + sender + master + pad1 ) )
4007 * + SHA1( master + pad2 +
4008 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004009 */
4010
Paul Bakker90995b52013-06-24 19:20:35 +02004011#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004012 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004014#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakker90995b52013-06-24 19:20:35 +02004016#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004017 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004019#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
Paul Bakker3c2122f2013-06-24 19:03:14 +02004021 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4022 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004023
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004025
Paul Bakker3c2122f2013-06-24 19:03:14 +02004026 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004027 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004028 md5_update( &md5, padbuf, 48 );
4029 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004030
Paul Bakker3c2122f2013-06-24 19:03:14 +02004031 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004032 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004033 sha1_update( &sha1, padbuf, 40 );
4034 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004035
Paul Bakker1ef83d62012-04-11 12:09:53 +00004036 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004037
Paul Bakker1ef83d62012-04-11 12:09:53 +00004038 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004039 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004040 md5_update( &md5, padbuf, 48 );
4041 md5_update( &md5, md5sum, 16 );
4042 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043
Paul Bakker1ef83d62012-04-11 12:09:53 +00004044 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004045 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004046 sha1_update( &sha1, padbuf , 40 );
4047 sha1_update( &sha1, sha1sum, 20 );
4048 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004049
Paul Bakker1ef83d62012-04-11 12:09:53 +00004050 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004051
Paul Bakker5b4af392014-06-26 12:09:34 +02004052 md5_free( &md5 );
4053 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004054
Paul Bakker34617722014-06-13 17:20:13 +02004055 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4056 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4057 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004058
4059 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4060}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004061#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004062
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004063#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004064static void ssl_calc_finished_tls(
4065 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004066{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004067 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004068 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004069 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004070 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004071 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004072
Paul Bakker48916f92012-09-16 19:57:18 +00004073 ssl_session *session = ssl->session_negotiate;
4074 if( !session )
4075 session = ssl->session;
4076
Paul Bakker1ef83d62012-04-11 12:09:53 +00004077 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004078
Paul Bakker48916f92012-09-16 19:57:18 +00004079 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4080 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004081
Paul Bakker1ef83d62012-04-11 12:09:53 +00004082 /*
4083 * TLSv1:
4084 * hash = PRF( master, finished_label,
4085 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4086 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004087
Paul Bakker90995b52013-06-24 19:20:35 +02004088#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004089 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4090 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004091#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004092
Paul Bakker90995b52013-06-24 19:20:35 +02004093#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004094 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4095 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004096#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004097
4098 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004099 ? "client finished"
4100 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004101
4102 md5_finish( &md5, padbuf );
4103 sha1_finish( &sha1, padbuf + 16 );
4104
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004105 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004106 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004107
4108 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4109
Paul Bakker5b4af392014-06-26 12:09:34 +02004110 md5_free( &md5 );
4111 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004112
Paul Bakker34617722014-06-13 17:20:13 +02004113 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114
4115 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4116}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004117#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004118
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004119#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4120#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004121static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004122 ssl_context *ssl, unsigned char *buf, int from )
4123{
4124 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004125 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004126 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004127 unsigned char padbuf[32];
4128
Paul Bakker48916f92012-09-16 19:57:18 +00004129 ssl_session *session = ssl->session_negotiate;
4130 if( !session )
4131 session = ssl->session;
4132
Paul Bakker380da532012-04-18 16:10:25 +00004133 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004134
Paul Bakker9e36f042013-06-30 14:34:05 +02004135 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136
4137 /*
4138 * TLSv1.2:
4139 * hash = PRF( master, finished_label,
4140 * Hash( handshake ) )[0.11]
4141 */
4142
Paul Bakker9e36f042013-06-30 14:34:05 +02004143#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004144 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004145 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004146#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004147
4148 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004149 ? "client finished"
4150 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004151
Paul Bakker9e36f042013-06-30 14:34:05 +02004152 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004153
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004154 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004155 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004156
4157 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4158
Paul Bakker5b4af392014-06-26 12:09:34 +02004159 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004160
Paul Bakker34617722014-06-13 17:20:13 +02004161 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004162
4163 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4164}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004165#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004166
Paul Bakker9e36f042013-06-30 14:34:05 +02004167#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004168static void ssl_calc_finished_tls_sha384(
4169 ssl_context *ssl, unsigned char *buf, int from )
4170{
4171 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004172 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004173 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004174 unsigned char padbuf[48];
4175
Paul Bakker48916f92012-09-16 19:57:18 +00004176 ssl_session *session = ssl->session_negotiate;
4177 if( !session )
4178 session = ssl->session;
4179
Paul Bakker380da532012-04-18 16:10:25 +00004180 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004181
Paul Bakker9e36f042013-06-30 14:34:05 +02004182 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004183
4184 /*
4185 * TLSv1.2:
4186 * hash = PRF( master, finished_label,
4187 * Hash( handshake ) )[0.11]
4188 */
4189
Paul Bakker9e36f042013-06-30 14:34:05 +02004190#if !defined(POLARSSL_SHA512_ALT)
4191 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4192 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004193#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004194
4195 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004196 ? "client finished"
4197 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004198
Paul Bakker9e36f042013-06-30 14:34:05 +02004199 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004200
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004201 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004202 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004203
4204 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4205
Paul Bakker5b4af392014-06-26 12:09:34 +02004206 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004207
Paul Bakker34617722014-06-13 17:20:13 +02004208 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004209
4210 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4211}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004212#endif /* POLARSSL_SHA512_C */
4213#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004214
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004215static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004216{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004217 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004218
4219 /*
4220 * Free our handshake params
4221 */
4222 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004223 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004224 ssl->handshake = NULL;
4225
4226 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004227 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004228 */
4229 if( ssl->transform )
4230 {
4231 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004232 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004233 }
4234 ssl->transform = ssl->transform_negotiate;
4235 ssl->transform_negotiate = NULL;
4236
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004237 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4238}
4239
4240void ssl_handshake_wrapup( ssl_context *ssl )
4241{
4242 int resume = ssl->handshake->resume;
4243
4244 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4245
4246 if( ssl->renegotiation == SSL_RENEGOTIATION )
4247 {
4248 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4249 ssl->renego_records_seen = 0;
4250 }
4251
4252 /*
4253 * Free the previous session and switch in the current one
4254 */
Paul Bakker0a597072012-09-25 21:55:46 +00004255 if( ssl->session )
4256 {
4257 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004258 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004259 }
4260 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004261 ssl->session_negotiate = NULL;
4262
Paul Bakker0a597072012-09-25 21:55:46 +00004263 /*
4264 * Add cache entry
4265 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004266 if( ssl->f_set_cache != NULL &&
4267 ssl->session->length != 0 &&
4268 resume == 0 )
4269 {
Paul Bakker0a597072012-09-25 21:55:46 +00004270 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4271 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004272 }
Paul Bakker0a597072012-09-25 21:55:46 +00004273
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004274#if defined(POLARSSL_SSL_PROTO_DTLS)
4275 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4276 ssl->handshake->flight != NULL )
4277 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004278 /* Cancel handshake timer */
4279 ssl_set_timer( ssl, 0 );
4280
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004281 /* Keep last flight around in case we need to resend it:
4282 * we need the handshake and transform structures for that */
4283 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4284 }
4285 else
4286#endif
4287 ssl_handshake_wrapup_free_hs_transform( ssl );
4288
Paul Bakker48916f92012-09-16 19:57:18 +00004289 ssl->state++;
4290
4291 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4292}
4293
Paul Bakker1ef83d62012-04-11 12:09:53 +00004294int ssl_write_finished( ssl_context *ssl )
4295{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004296 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004297
4298 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4299
Paul Bakker92be97b2013-01-02 17:30:03 +01004300 /*
4301 * Set the out_msg pointer to the correct location based on IV length
4302 */
4303 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4304 {
4305 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4306 ssl->transform_negotiate->fixed_ivlen;
4307 }
4308 else
4309 ssl->out_msg = ssl->out_iv;
4310
Paul Bakker48916f92012-09-16 19:57:18 +00004311 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004312
4313 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004314 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4315
Paul Bakker48916f92012-09-16 19:57:18 +00004316 ssl->verify_data_len = hash_len;
4317 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4318
Paul Bakker5121ce52009-01-03 21:22:43 +00004319 ssl->out_msglen = 4 + hash_len;
4320 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4321 ssl->out_msg[0] = SSL_HS_FINISHED;
4322
4323 /*
4324 * In case of session resuming, invert the client and server
4325 * ChangeCipherSpec messages order.
4326 */
Paul Bakker0a597072012-09-25 21:55:46 +00004327 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 {
4329 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004330 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 else
4332 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4333 }
4334 else
4335 ssl->state++;
4336
Paul Bakker48916f92012-09-16 19:57:18 +00004337 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004338 * Switch to our negotiated transform and session parameters for outbound
4339 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004340 */
4341 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004342
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004343#if defined(POLARSSL_SSL_PROTO_DTLS)
4344 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4345 {
4346 unsigned char i;
4347
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004348 /* Remember current epoch settings for resending */
4349 ssl->handshake->alt_transform_out = ssl->transform_out;
4350 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4351
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004352 /* Set sequence_number to zero */
4353 memset( ssl->out_ctr + 2, 0, 6 );
4354
4355 /* Increment epoch */
4356 for( i = 2; i > 0; i-- )
4357 if( ++ssl->out_ctr[i - 1] != 0 )
4358 break;
4359
4360 /* The loop goes to its end iff the counter is wrapping */
4361 if( i == 0 )
4362 {
4363 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4364 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4365 }
4366 }
4367 else
4368#endif /* POLARSSL_SSL_PROTO_DTLS */
4369 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004370
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004371 ssl->transform_out = ssl->transform_negotiate;
4372 ssl->session_out = ssl->session_negotiate;
4373
Paul Bakker07eb38b2012-12-19 14:42:06 +01004374#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004375 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004376 {
4377 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4378 {
4379 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4380 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4381 }
4382 }
4383#endif
4384
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004385#if defined(POLARSSL_SSL_PROTO_DTLS)
4386 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4387 ssl_send_flight_completed( ssl );
4388#endif
4389
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4391 {
4392 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4393 return( ret );
4394 }
4395
4396 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4397
4398 return( 0 );
4399}
4400
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004401#if defined(POLARSSL_SSL_PROTO_SSL3)
4402#define SSL_MAX_HASH_LEN 36
4403#else
4404#define SSL_MAX_HASH_LEN 12
4405#endif
4406
Paul Bakker5121ce52009-01-03 21:22:43 +00004407int ssl_parse_finished( ssl_context *ssl )
4408{
Paul Bakker23986e52011-04-24 08:57:21 +00004409 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004410 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004411 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004412
4413 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4414
Paul Bakker48916f92012-09-16 19:57:18 +00004415 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004416
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4418 {
4419 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4420 return( ret );
4421 }
4422
4423 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4424 {
4425 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004426 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 }
4428
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004429 /* There is currently no ciphersuite using another length with TLS 1.2 */
4430#if defined(POLARSSL_SSL_PROTO_SSL3)
4431 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4432 hash_len = 36;
4433 else
4434#endif
4435 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004436
4437 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004438 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004439 {
4440 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004441 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442 }
4443
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004444 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4445 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004446 {
4447 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004448 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004449 }
4450
Paul Bakker48916f92012-09-16 19:57:18 +00004451 ssl->verify_data_len = hash_len;
4452 memcpy( ssl->peer_verify_data, buf, hash_len );
4453
Paul Bakker0a597072012-09-25 21:55:46 +00004454 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 {
4456 if( ssl->endpoint == SSL_IS_CLIENT )
4457 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4458
4459 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004460 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004461 }
4462 else
4463 ssl->state++;
4464
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004465#if defined(POLARSSL_SSL_PROTO_DTLS)
4466 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4467 ssl_recv_flight_completed( ssl );
4468#endif
4469
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4471
4472 return( 0 );
4473}
4474
Paul Bakker968afaa2014-07-09 11:09:24 +02004475static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004476{
4477 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4478
4479#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4480 defined(POLARSSL_SSL_PROTO_TLS1_1)
4481 md5_init( &handshake->fin_md5 );
4482 sha1_init( &handshake->fin_sha1 );
4483 md5_starts( &handshake->fin_md5 );
4484 sha1_starts( &handshake->fin_sha1 );
4485#endif
4486#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4487#if defined(POLARSSL_SHA256_C)
4488 sha256_init( &handshake->fin_sha256 );
4489 sha256_starts( &handshake->fin_sha256, 0 );
4490#endif
4491#if defined(POLARSSL_SHA512_C)
4492 sha512_init( &handshake->fin_sha512 );
4493 sha512_starts( &handshake->fin_sha512, 1 );
4494#endif
4495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4496
4497 handshake->update_checksum = ssl_update_checksum_start;
4498 handshake->sig_alg = SSL_HASH_SHA1;
4499
4500#if defined(POLARSSL_DHM_C)
4501 dhm_init( &handshake->dhm_ctx );
4502#endif
4503#if defined(POLARSSL_ECDH_C)
4504 ecdh_init( &handshake->ecdh_ctx );
4505#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004506}
4507
4508static void ssl_transform_init( ssl_transform *transform )
4509{
4510 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004511
4512 cipher_init( &transform->cipher_ctx_enc );
4513 cipher_init( &transform->cipher_ctx_dec );
4514
4515 md_init( &transform->md_ctx_enc );
4516 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004517}
4518
4519void ssl_session_init( ssl_session *session )
4520{
4521 memset( session, 0, sizeof(ssl_session) );
4522}
4523
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004524static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004525{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004526 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004527 if( ssl->transform_negotiate )
4528 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004529 if( ssl->session_negotiate )
4530 ssl_session_free( ssl->session_negotiate );
4531 if( ssl->handshake )
4532 ssl_handshake_free( ssl->handshake );
4533
4534 /*
4535 * Either the pointers are now NULL or cleared properly and can be freed.
4536 * Now allocate missing structures.
4537 */
4538 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004539 {
4540 ssl->transform_negotiate =
4541 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4542 }
Paul Bakker48916f92012-09-16 19:57:18 +00004543
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004544 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004545 {
4546 ssl->session_negotiate =
4547 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4548 }
Paul Bakker48916f92012-09-16 19:57:18 +00004549
Paul Bakker82788fb2014-10-20 13:59:19 +02004550 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004551 {
4552 ssl->handshake = (ssl_handshake_params *)
4553 polarssl_malloc( sizeof(ssl_handshake_params) );
4554 }
Paul Bakker48916f92012-09-16 19:57:18 +00004555
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004556 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004557 if( ssl->handshake == NULL ||
4558 ssl->transform_negotiate == NULL ||
4559 ssl->session_negotiate == NULL )
4560 {
4561 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004562
4563 polarssl_free( ssl->handshake );
4564 polarssl_free( ssl->transform_negotiate );
4565 polarssl_free( ssl->session_negotiate );
4566
4567 ssl->handshake = NULL;
4568 ssl->transform_negotiate = NULL;
4569 ssl->session_negotiate = NULL;
4570
Paul Bakker48916f92012-09-16 19:57:18 +00004571 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4572 }
4573
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004574 /* Initialize structures */
4575 ssl_session_init( ssl->session_negotiate );
4576 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004577 ssl_handshake_params_init( ssl->handshake );
4578
4579#if defined(POLARSSL_X509_CRT_PARSE_C)
4580 ssl->handshake->key_cert = ssl->key_cert;
4581#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004582
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004583 /*
4584 * We may not know yet if we're using DTLS,
4585 * so always initiliase DTLS-specific fields.
4586 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004587#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004588 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004589
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004590 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004591 if( ssl->endpoint == SSL_IS_CLIENT )
4592 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4593 else
4594 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004595#endif
4596
Paul Bakker48916f92012-09-16 19:57:18 +00004597 return( 0 );
4598}
4599
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004600#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4601/* Dummy cookie callbacks for defaults */
4602static int ssl_cookie_write_dummy( void *ctx,
4603 unsigned char **p, unsigned char *end,
4604 const unsigned char *cli_id, size_t cli_id_len )
4605{
4606 ((void) ctx);
4607 ((void) p);
4608 ((void) end);
4609 ((void) cli_id);
4610 ((void) cli_id_len);
4611
4612 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4613}
4614
4615static int ssl_cookie_check_dummy( void *ctx,
4616 const unsigned char *cookie, size_t cookie_len,
4617 const unsigned char *cli_id, size_t cli_id_len )
4618{
4619 ((void) ctx);
4620 ((void) cookie);
4621 ((void) cookie_len);
4622 ((void) cli_id);
4623 ((void) cli_id_len);
4624
4625 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4626}
4627#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4628
Paul Bakker5121ce52009-01-03 21:22:43 +00004629/*
4630 * Initialize an SSL context
4631 */
4632int ssl_init( ssl_context *ssl )
4633{
Paul Bakker48916f92012-09-16 19:57:18 +00004634 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004635 int len = SSL_BUFFER_LEN;
4636
4637 memset( ssl, 0, sizeof( ssl_context ) );
4638
Paul Bakker62f2dee2012-09-28 07:31:51 +00004639 /*
4640 * Sane defaults
4641 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004642 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4643 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4644 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4645 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004646
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004647 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004648
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004649 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4650
Paul Bakker62f2dee2012-09-28 07:31:51 +00004651#if defined(POLARSSL_DHM_C)
4652 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4653 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4654 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4655 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4656 {
4657 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4658 return( ret );
4659 }
4660#endif
4661
4662 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004663 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004664 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004665 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004666 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004667
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004668 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004669 {
4670 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004671 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004672 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004673 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004674 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004675 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004676 }
4677
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004678 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4679 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004680
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004681 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4682 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4683
Paul Bakker606b4ba2013-08-14 16:52:14 +02004684#if defined(POLARSSL_SSL_SESSION_TICKETS)
4685 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4686#endif
4687
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004688#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004689 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004690#endif
4691
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004692#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4693 ssl->f_cookie_write = ssl_cookie_write_dummy;
4694 ssl->f_cookie_check = ssl_cookie_check_dummy;
4695#endif
4696
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004697#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4698 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4699#endif
4700
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004701#if defined(POLARSSL_SSL_PROTO_DTLS)
4702 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4703 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4704#endif
4705
Paul Bakker48916f92012-09-16 19:57:18 +00004706 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4707 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004708
4709 return( 0 );
4710}
4711
4712/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004713 * Reset an initialized and used SSL context for re-use while retaining
4714 * all application-set variables, function pointers and data.
4715 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004716int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004717{
Paul Bakker48916f92012-09-16 19:57:18 +00004718 int ret;
4719
Paul Bakker7eb013f2011-10-06 12:37:39 +00004720 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004721 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4722 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4723
4724 ssl->verify_data_len = 0;
4725 memset( ssl->own_verify_data, 0, 36 );
4726 memset( ssl->peer_verify_data, 0, 36 );
4727
Paul Bakker7eb013f2011-10-06 12:37:39 +00004728 ssl->in_offt = NULL;
4729
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004730 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004731 ssl->in_msgtype = 0;
4732 ssl->in_msglen = 0;
4733 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004734#if defined(POLARSSL_SSL_PROTO_DTLS)
4735 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004736 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004737#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004738#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4739 ssl_dtls_replay_reset( ssl );
4740#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004741
4742 ssl->in_hslen = 0;
4743 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004744 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004745
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004746 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004747 ssl->out_msgtype = 0;
4748 ssl->out_msglen = 0;
4749 ssl->out_left = 0;
4750
Paul Bakker48916f92012-09-16 19:57:18 +00004751 ssl->transform_in = NULL;
4752 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004753
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004754 ssl->renego_records_seen = 0;
4755
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004756 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4757 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004758
4759#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004760 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004761 {
4762 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004763 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004764 {
4765 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4766 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4767 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004768 }
4769#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004770
Paul Bakker48916f92012-09-16 19:57:18 +00004771 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004772 {
Paul Bakker48916f92012-09-16 19:57:18 +00004773 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004774 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004775 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004776 }
Paul Bakker48916f92012-09-16 19:57:18 +00004777
Paul Bakkerc0463502013-02-14 11:19:38 +01004778 if( ssl->session )
4779 {
4780 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004781 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004782 ssl->session = NULL;
4783 }
4784
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004785#if defined(POLARSSL_SSL_ALPN)
4786 ssl->alpn_chosen = NULL;
4787#endif
4788
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004789#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004790 polarssl_free( ssl->cli_id );
4791 ssl->cli_id = NULL;
4792 ssl->cli_id_len = 0;
4793#endif
4794
Paul Bakker48916f92012-09-16 19:57:18 +00004795 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4796 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004797
4798 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004799}
4800
Paul Bakkera503a632013-08-14 13:48:06 +02004801#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004802static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4803{
4804 aes_free( &tkeys->enc );
4805 aes_free( &tkeys->dec );
4806
4807 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4808}
4809
Paul Bakker7eb013f2011-10-06 12:37:39 +00004810/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004811 * Allocate and initialize ticket keys
4812 */
4813static int ssl_ticket_keys_init( ssl_context *ssl )
4814{
4815 int ret;
4816 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004817 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004818
4819 if( ssl->ticket_keys != NULL )
4820 return( 0 );
4821
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004822 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4823 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004824 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4825
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004826 aes_init( &tkeys->enc );
4827 aes_init( &tkeys->dec );
4828
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004829 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004830 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004831 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004832 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004833 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004834 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004835
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004836 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4837 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4838 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4839 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004840 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004841 polarssl_free( tkeys );
4842 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004843 }
4844
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004845 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004846 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004847 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004848 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004849 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004850 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004851
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004852 ssl->ticket_keys = tkeys;
4853
4854 return( 0 );
4855}
Paul Bakkera503a632013-08-14 13:48:06 +02004856#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004857
4858/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004859 * SSL set accessors
4860 */
4861void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4862{
4863 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004864
Paul Bakker606b4ba2013-08-14 16:52:14 +02004865#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004866 if( endpoint == SSL_IS_CLIENT )
4867 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004868#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004869}
4870
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004871int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004872{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004873#if defined(POLARSSL_SSL_PROTO_DTLS)
4874 if( transport == SSL_TRANSPORT_DATAGRAM )
4875 {
4876 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004877
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004878 ssl->out_hdr = ssl->out_buf;
4879 ssl->out_ctr = ssl->out_buf + 3;
4880 ssl->out_len = ssl->out_buf + 11;
4881 ssl->out_iv = ssl->out_buf + 13;
4882 ssl->out_msg = ssl->out_buf + 13;
4883
4884 ssl->in_hdr = ssl->in_buf;
4885 ssl->in_ctr = ssl->in_buf + 3;
4886 ssl->in_len = ssl->in_buf + 11;
4887 ssl->in_iv = ssl->in_buf + 13;
4888 ssl->in_msg = ssl->in_buf + 13;
4889
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004890 /* DTLS starts with TLS1.1 */
4891 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4892 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004893
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004894 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4895 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4896
4897 return( 0 );
4898 }
4899#endif
4900
4901 if( transport == SSL_TRANSPORT_STREAM )
4902 {
4903 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004904
4905 ssl->out_ctr = ssl->out_buf;
4906 ssl->out_hdr = ssl->out_buf + 8;
4907 ssl->out_len = ssl->out_buf + 11;
4908 ssl->out_iv = ssl->out_buf + 13;
4909 ssl->out_msg = ssl->out_buf + 13;
4910
4911 ssl->in_ctr = ssl->in_buf;
4912 ssl->in_hdr = ssl->in_buf + 8;
4913 ssl->in_len = ssl->in_buf + 11;
4914 ssl->in_iv = ssl->in_buf + 13;
4915 ssl->in_msg = ssl->in_buf + 13;
4916
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004917 return( 0 );
4918 }
4919
4920 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004921}
4922
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004923#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4924void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4925{
4926 ssl->anti_replay = mode;
4927}
4928#endif
4929
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004930#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
4931void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
4932{
4933 ssl->badmac_limit = limit;
4934}
4935#endif
4936
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004937#if defined(POLARSSL_SSL_PROTO_DTLS)
4938void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4939{
4940 ssl->hs_timeout_min = min;
4941 ssl->hs_timeout_max = max;
4942}
4943#endif
4944
Paul Bakker5121ce52009-01-03 21:22:43 +00004945void ssl_set_authmode( ssl_context *ssl, int authmode )
4946{
4947 ssl->authmode = authmode;
4948}
4949
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004950#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004951void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004952 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004953 void *p_vrfy )
4954{
4955 ssl->f_vrfy = f_vrfy;
4956 ssl->p_vrfy = p_vrfy;
4957}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004958#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004959
Paul Bakker5121ce52009-01-03 21:22:43 +00004960void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004961 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004962 void *p_rng )
4963{
4964 ssl->f_rng = f_rng;
4965 ssl->p_rng = p_rng;
4966}
4967
4968void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004969 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004970 void *p_dbg )
4971{
4972 ssl->f_dbg = f_dbg;
4973 ssl->p_dbg = p_dbg;
4974}
4975
4976void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004977 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004978 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004979{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004980 if( p_recv != p_send )
4981 {
4982 ssl->f_recv = NULL;
4983 ssl->f_send = NULL;
4984 ssl->p_bio = NULL;
4985 return;
4986 }
4987
Paul Bakker5121ce52009-01-03 21:22:43 +00004988 ssl->f_recv = f_recv;
4989 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004990 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004991}
4992
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004993void ssl_set_bio_timeout( ssl_context *ssl,
4994 void *p_bio,
4995 int (*f_send)(void *, const unsigned char *, size_t),
4996 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004997 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4998 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004999{
5000 ssl->p_bio = p_bio;
5001 ssl->f_send = f_send;
5002 ssl->f_recv = f_recv;
5003 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005004 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005005}
5006
Paul Bakker0a597072012-09-25 21:55:46 +00005007void ssl_set_session_cache( ssl_context *ssl,
5008 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5009 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005010{
Paul Bakker0a597072012-09-25 21:55:46 +00005011 ssl->f_get_cache = f_get_cache;
5012 ssl->p_get_cache = p_get_cache;
5013 ssl->f_set_cache = f_set_cache;
5014 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005015}
5016
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005017int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005018{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005019 int ret;
5020
5021 if( ssl == NULL ||
5022 session == NULL ||
5023 ssl->session_negotiate == NULL ||
5024 ssl->endpoint != SSL_IS_CLIENT )
5025 {
5026 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5027 }
5028
5029 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5030 return( ret );
5031
Paul Bakker0a597072012-09-25 21:55:46 +00005032 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005033
5034 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005035}
5036
Paul Bakkerb68cad62012-08-23 08:34:18 +00005037void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005038{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005039 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5040 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5041 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5042 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5043}
5044
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005045void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5046 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005047 int major, int minor )
5048{
5049 if( major != SSL_MAJOR_VERSION_3 )
5050 return;
5051
5052 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5053 return;
5054
5055 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005056}
5057
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005058#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005059/* Add a new (empty) key_cert entry an return a pointer to it */
5060static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5061{
5062 ssl_key_cert *key_cert, *last;
5063
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005064 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5065 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005066 return( NULL );
5067
5068 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5069
5070 /* Append the new key_cert to the (possibly empty) current list */
5071 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005072 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005073 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005074 if( ssl->handshake != NULL )
5075 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005076 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005077 else
5078 {
5079 last = ssl->key_cert;
5080 while( last->next != NULL )
5081 last = last->next;
5082 last->next = key_cert;
5083 }
5084
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005085 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005086}
5087
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005088void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005089 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005090{
5091 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005092 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005093 ssl->peer_cn = peer_cn;
5094}
5095
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005096int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005097 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005099 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5100
5101 if( key_cert == NULL )
5102 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5103
5104 key_cert->cert = own_cert;
5105 key_cert->key = pk_key;
5106
5107 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005108}
5109
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005110#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005111int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005112 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005113{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005114 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005115 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005116
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005117 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005118 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5119
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005120 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5121 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005122 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005123
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005124 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005125
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005126 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005127 if( ret != 0 )
5128 return( ret );
5129
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005130 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005131 return( ret );
5132
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005133 key_cert->cert = own_cert;
5134 key_cert->key_own_alloc = 1;
5135
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005136 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005137}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005138#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005139
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005140int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005141 void *rsa_key,
5142 rsa_decrypt_func rsa_decrypt,
5143 rsa_sign_func rsa_sign,
5144 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005145{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005146 int ret;
5147 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005148
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005149 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005150 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5151
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005152 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5153 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005154 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005155
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005156 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005157
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005158 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5159 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5160 return( ret );
5161
5162 key_cert->cert = own_cert;
5163 key_cert->key_own_alloc = 1;
5164
5165 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005166}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005167#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005168
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005169#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005170int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5171 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005172{
Paul Bakker6db455e2013-09-18 17:29:31 +02005173 if( psk == NULL || psk_identity == NULL )
5174 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5175
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005176 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005177 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5178
Paul Bakker6db455e2013-09-18 17:29:31 +02005179 if( ssl->psk != NULL )
5180 {
5181 polarssl_free( ssl->psk );
5182 polarssl_free( ssl->psk_identity );
5183 }
5184
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005185 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005186 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005187
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005188 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005189 ssl->psk_identity = (unsigned char *)
5190 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005191
5192 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5193 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5194
5195 memcpy( ssl->psk, psk, ssl->psk_len );
5196 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005197
5198 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005199}
5200
5201void ssl_set_psk_cb( ssl_context *ssl,
5202 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5203 size_t),
5204 void *p_psk )
5205{
5206 ssl->f_psk = f_psk;
5207 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005208}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005209#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005210
Paul Bakker48916f92012-09-16 19:57:18 +00005211#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005212int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005213{
5214 int ret;
5215
Paul Bakker48916f92012-09-16 19:57:18 +00005216 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005217 {
5218 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5219 return( ret );
5220 }
5221
Paul Bakker48916f92012-09-16 19:57:18 +00005222 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005223 {
5224 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5225 return( ret );
5226 }
5227
5228 return( 0 );
5229}
5230
Paul Bakker1b57b062011-01-06 15:48:19 +00005231int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5232{
5233 int ret;
5234
Paul Bakker66d5d072014-06-17 16:39:18 +02005235 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005236 {
5237 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5238 return( ret );
5239 }
5240
Paul Bakker66d5d072014-06-17 16:39:18 +02005241 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005242 {
5243 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5244 return( ret );
5245 }
5246
5247 return( 0 );
5248}
Paul Bakker48916f92012-09-16 19:57:18 +00005249#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005250
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005251#if defined(POLARSSL_SSL_SET_CURVES)
5252/*
5253 * Set the allowed elliptic curves
5254 */
5255void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5256{
5257 ssl->curve_list = curve_list;
5258}
5259#endif
5260
Paul Bakker0be444a2013-08-27 21:55:01 +02005261#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005262int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005263{
5264 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005265 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005266
5267 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005268
5269 if( ssl->hostname_len + 1 == 0 )
5270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5271
Paul Bakker6e339b52013-07-03 13:37:05 +02005272 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005273
Paul Bakkerb15b8512012-01-13 13:44:06 +00005274 if( ssl->hostname == NULL )
5275 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5276
Paul Bakker3c2122f2013-06-24 19:03:14 +02005277 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005278 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005279
Paul Bakker40ea7de2009-05-03 10:18:48 +00005280 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005281
5282 return( 0 );
5283}
5284
Paul Bakker5701cdc2012-09-27 21:49:42 +00005285void ssl_set_sni( ssl_context *ssl,
5286 int (*f_sni)(void *, ssl_context *,
5287 const unsigned char *, size_t),
5288 void *p_sni )
5289{
5290 ssl->f_sni = f_sni;
5291 ssl->p_sni = p_sni;
5292}
Paul Bakker0be444a2013-08-27 21:55:01 +02005293#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005294
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005295#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005296int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005297{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005298 size_t cur_len, tot_len;
5299 const char **p;
5300
5301 /*
5302 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5303 * truncated". Check lengths now rather than later.
5304 */
5305 tot_len = 0;
5306 for( p = protos; *p != NULL; p++ )
5307 {
5308 cur_len = strlen( *p );
5309 tot_len += cur_len;
5310
5311 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5313 }
5314
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005315 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005316
5317 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005318}
5319
5320const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5321{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005322 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005323}
5324#endif /* POLARSSL_SSL_ALPN */
5325
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005326static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005327{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005328 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005329 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005330 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005331 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005332 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005333
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005334#if defined(POLARSSL_SSL_PROTO_DTLS)
5335 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5336 minor < SSL_MINOR_VERSION_2 )
5337 {
5338 return( -1 );
5339 }
5340#else
5341 ((void) ssl);
5342#endif
5343
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005344 return( 0 );
5345}
5346
5347int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5348{
5349 if( ssl_check_version( ssl, major, minor ) != 0 )
5350 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5351
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005352 ssl->max_major_ver = major;
5353 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005354
5355 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005356}
5357
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005358int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005359{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005360 if( ssl_check_version( ssl, major, minor ) != 0 )
5361 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005362
5363 ssl->min_major_ver = major;
5364 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005365
5366 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005367}
5368
Paul Bakker05decb22013-08-15 13:33:48 +02005369#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005370int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5371{
Paul Bakker77e257e2013-12-16 15:29:52 +01005372 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005373 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005374 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005375 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005376 }
5377
5378 ssl->mfl_code = mfl_code;
5379
5380 return( 0 );
5381}
Paul Bakker05decb22013-08-15 13:33:48 +02005382#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005383
Paul Bakker1f2bc622013-08-15 13:45:55 +02005384#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005385int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005386{
5387 if( ssl->endpoint != SSL_IS_CLIENT )
5388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5389
Paul Bakker8c1ede62013-07-19 14:14:37 +02005390 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005391
5392 return( 0 );
5393}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005394#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005395
Paul Bakker48916f92012-09-16 19:57:18 +00005396void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5397{
5398 ssl->disable_renegotiation = renegotiation;
5399}
5400
5401void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5402{
5403 ssl->allow_legacy_renegotiation = allow_legacy;
5404}
5405
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005406void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5407{
5408 ssl->renego_max_records = max_records;
5409}
5410
Paul Bakkera503a632013-08-14 13:48:06 +02005411#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005412int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5413{
5414 ssl->session_tickets = use_tickets;
5415
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005416 if( ssl->endpoint == SSL_IS_CLIENT )
5417 return( 0 );
5418
5419 if( ssl->f_rng == NULL )
5420 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5421
5422 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005423}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005424
5425void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5426{
5427 ssl->ticket_lifetime = lifetime;
5428}
Paul Bakkera503a632013-08-14 13:48:06 +02005429#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005430
Paul Bakker5121ce52009-01-03 21:22:43 +00005431/*
5432 * SSL get accessors
5433 */
Paul Bakker23986e52011-04-24 08:57:21 +00005434size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005435{
5436 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5437}
5438
Paul Bakkerff60ee62010-03-16 21:09:09 +00005439int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005440{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005441 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005442}
5443
Paul Bakkere3166ce2011-01-27 17:40:50 +00005444const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005445{
Paul Bakker926c8e42013-03-06 10:23:34 +01005446 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005447 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005448
Paul Bakkere3166ce2011-01-27 17:40:50 +00005449 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005450}
5451
Paul Bakker43ca69c2011-01-15 17:35:19 +00005452const char *ssl_get_version( const ssl_context *ssl )
5453{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005454#if defined(POLARSSL_SSL_PROTO_DTLS)
5455 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5456 {
5457 switch( ssl->minor_ver )
5458 {
5459 case SSL_MINOR_VERSION_2:
5460 return( "DTLSv1.0" );
5461
5462 case SSL_MINOR_VERSION_3:
5463 return( "DTLSv1.2" );
5464
5465 default:
5466 return( "unknown (DTLS)" );
5467 }
5468 }
5469#endif
5470
Paul Bakker43ca69c2011-01-15 17:35:19 +00005471 switch( ssl->minor_ver )
5472 {
5473 case SSL_MINOR_VERSION_0:
5474 return( "SSLv3.0" );
5475
5476 case SSL_MINOR_VERSION_1:
5477 return( "TLSv1.0" );
5478
5479 case SSL_MINOR_VERSION_2:
5480 return( "TLSv1.1" );
5481
Paul Bakker1ef83d62012-04-11 12:09:53 +00005482 case SSL_MINOR_VERSION_3:
5483 return( "TLSv1.2" );
5484
Paul Bakker43ca69c2011-01-15 17:35:19 +00005485 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005486 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005487 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005488}
5489
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005490int ssl_get_record_expansion( const ssl_context *ssl )
5491{
5492 int transform_expansion;
5493 const ssl_transform *transform = ssl->transform_out;
5494
5495#if defined(POLARSSL_ZLIB_SUPPORT)
5496 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5497 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5498#endif
5499
5500 if( transform == NULL )
5501 return( ssl_hdr_len( ssl ) );
5502
5503 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5504 {
5505 case POLARSSL_MODE_GCM:
5506 case POLARSSL_MODE_CCM:
5507 case POLARSSL_MODE_STREAM:
5508 transform_expansion = transform->minlen;
5509 break;
5510
5511 case POLARSSL_MODE_CBC:
5512 transform_expansion = transform->maclen
5513 + cipher_get_block_size( &transform->cipher_ctx_enc );
5514 break;
5515
5516 default:
5517 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5518 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5519 }
5520
5521 return( ssl_hdr_len( ssl ) + transform_expansion );
5522}
5523
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005524#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005525const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005526{
5527 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005528 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005529
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005530 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005531}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005532#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005533
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005534int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5535{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005536 if( ssl == NULL ||
5537 dst == NULL ||
5538 ssl->session == NULL ||
5539 ssl->endpoint != SSL_IS_CLIENT )
5540 {
5541 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5542 }
5543
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005544 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005545}
5546
Paul Bakker5121ce52009-01-03 21:22:43 +00005547/*
Paul Bakker1961b702013-01-25 14:49:24 +01005548 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005549 */
Paul Bakker1961b702013-01-25 14:49:24 +01005550int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005551{
Paul Bakker40e46942009-01-03 21:51:57 +00005552 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005553
Paul Bakker40e46942009-01-03 21:51:57 +00005554#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005555 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005556 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005557#endif
5558
Paul Bakker40e46942009-01-03 21:51:57 +00005559#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005560 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005561 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005562#endif
5563
Paul Bakker1961b702013-01-25 14:49:24 +01005564 return( ret );
5565}
5566
5567/*
5568 * Perform the SSL handshake
5569 */
5570int ssl_handshake( ssl_context *ssl )
5571{
5572 int ret = 0;
5573
5574 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5575
5576 while( ssl->state != SSL_HANDSHAKE_OVER )
5577 {
5578 ret = ssl_handshake_step( ssl );
5579
5580 if( ret != 0 )
5581 break;
5582 }
5583
Paul Bakker5121ce52009-01-03 21:22:43 +00005584 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5585
5586 return( ret );
5587}
5588
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005589#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005590/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005591 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005592 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005593static int ssl_write_hello_request( ssl_context *ssl )
5594{
5595 int ret;
5596
5597 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5598
5599 ssl->out_msglen = 4;
5600 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5601 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5602
5603 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5604 {
5605 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5606 return( ret );
5607 }
5608
5609 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5610
5611 return( 0 );
5612}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005613#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005614
5615/*
5616 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005617 * - any side: calling ssl_renegotiate(),
5618 * - client: receiving a HelloRequest during ssl_read(),
5619 * - server: receiving any handshake message on server during ssl_read() after
5620 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005621 * If the handshake doesn't complete due to waiting for I/O, it will continue
5622 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005623 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005624static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005625{
5626 int ret;
5627
5628 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5629
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005630 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5631 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005632
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005633 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5634 * the ServerHello will have message_seq = 1" */
5635#if defined(POLARSSL_SSL_PROTO_DTLS)
5636 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005637 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5638 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005639 if( ssl->endpoint == SSL_IS_SERVER )
5640 ssl->handshake->out_msg_seq = 1;
5641 else
5642 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005643 }
5644#endif
5645
Paul Bakker48916f92012-09-16 19:57:18 +00005646 ssl->state = SSL_HELLO_REQUEST;
5647 ssl->renegotiation = SSL_RENEGOTIATION;
5648
Paul Bakker48916f92012-09-16 19:57:18 +00005649 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5650 {
5651 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5652 return( ret );
5653 }
5654
5655 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5656
5657 return( 0 );
5658}
5659
5660/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005661 * Renegotiate current connection on client,
5662 * or request renegotiation on server
5663 */
5664int ssl_renegotiate( ssl_context *ssl )
5665{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005666 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005667
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005668#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005669 /* On server, just send the request */
5670 if( ssl->endpoint == SSL_IS_SERVER )
5671 {
5672 if( ssl->state != SSL_HANDSHAKE_OVER )
5673 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5674
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005675 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5676
5677 /* Did we already try/start sending HelloRequest? */
5678 if( ssl->out_left != 0 )
5679 return( ssl_flush_output( ssl ) );
5680
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005681 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005682 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005683#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005684
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005685#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005686 /*
5687 * On client, either start the renegotiation process or,
5688 * if already in progress, continue the handshake
5689 */
5690 if( ssl->renegotiation != SSL_RENEGOTIATION )
5691 {
5692 if( ssl->state != SSL_HANDSHAKE_OVER )
5693 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5694
5695 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5696 {
5697 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5698 return( ret );
5699 }
5700 }
5701 else
5702 {
5703 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5704 {
5705 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5706 return( ret );
5707 }
5708 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005709#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005710
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005711 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005712}
5713
5714/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005715 * Receive application data decrypted from the SSL layer
5716 */
Paul Bakker23986e52011-04-24 08:57:21 +00005717int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005718{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005719 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005720 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005721
5722 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5723
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005724#if defined(POLARSSL_SSL_PROTO_DTLS)
5725 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5726 ssl->handshake != NULL &&
5727 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5728 {
5729 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5730 return( ret );
5731
5732 if( ( ret = ssl_resend( ssl ) ) != 0 )
5733 return( ret );
5734 }
5735#endif
5736
Paul Bakker5121ce52009-01-03 21:22:43 +00005737 if( ssl->state != SSL_HANDSHAKE_OVER )
5738 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005739 ret = ssl_handshake( ssl );
5740 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5741 {
5742 record_read = 1;
5743 }
5744 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005745 {
5746 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5747 return( ret );
5748 }
5749 }
5750
5751 if( ssl->in_offt == NULL )
5752 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005753#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005754 /* Start timer if not already running */
5755 if( ssl->time_limit == 0 )
5756 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005757#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005758
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005759 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005761 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5762 {
5763 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5764 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005765
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005766 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5767 return( ret );
5768 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005769 }
5770
5771 if( ssl->in_msglen == 0 &&
5772 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5773 {
5774 /*
5775 * OpenSSL sends empty messages to randomize the IV
5776 */
5777 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5778 {
Paul Bakker831a7552011-05-18 13:32:51 +00005779 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5780 return( 0 );
5781
Paul Bakker5121ce52009-01-03 21:22:43 +00005782 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5783 return( ret );
5784 }
5785 }
5786
Paul Bakker48916f92012-09-16 19:57:18 +00005787 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5788 {
5789 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5790
5791 if( ssl->endpoint == SSL_IS_CLIENT &&
5792 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005793 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005794 {
5795 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005796
5797 /* With DTLS, drop the packet (probably from last handshake) */
5798#if defined(POLARSSL_SSL_PROTO_DTLS)
5799 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5800 return( POLARSSL_ERR_NET_WANT_READ );
5801#endif
5802 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5803 }
5804
5805 if( ssl->endpoint == SSL_IS_SERVER &&
5806 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5807 {
5808 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5809
5810 /* With DTLS, drop the packet (probably from last handshake) */
5811#if defined(POLARSSL_SSL_PROTO_DTLS)
5812 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5813 return( POLARSSL_ERR_NET_WANT_READ );
5814#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005815 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5816 }
5817
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005818 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5819 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005820 ssl->allow_legacy_renegotiation ==
5821 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005822 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005823 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005824
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005825#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005826 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005827 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005828 /*
5829 * SSLv3 does not have a "no_renegotiation" alert
5830 */
5831 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5832 return( ret );
5833 }
5834 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005835#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005836#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5837 defined(POLARSSL_SSL_PROTO_TLS1_2)
5838 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005839 {
5840 if( ( ret = ssl_send_alert_message( ssl,
5841 SSL_ALERT_LEVEL_WARNING,
5842 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5843 {
5844 return( ret );
5845 }
Paul Bakker48916f92012-09-16 19:57:18 +00005846 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005847 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005848#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5849 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005850 {
5851 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005852 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005853 }
Paul Bakker48916f92012-09-16 19:57:18 +00005854 }
5855 else
5856 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005857 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005858#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005859 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5860 ssl->endpoint == SSL_IS_CLIENT )
5861 {
5862 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5863 }
5864#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005865 ret = ssl_start_renegotiation( ssl );
5866 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5867 {
5868 record_read = 1;
5869 }
5870 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005871 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005872 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005873 return( ret );
5874 }
Paul Bakker48916f92012-09-16 19:57:18 +00005875 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005876
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005877 /* If a non-handshake record was read during renego, fallthrough,
5878 * else tell the user they should call ssl_read() again */
5879 if( ! record_read )
5880 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005881 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005882 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5883 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005884 ssl->renego_records_seen++;
5885
5886 if( ssl->renego_max_records >= 0 &&
5887 ssl->renego_records_seen > ssl->renego_max_records )
5888 {
5889 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5890 "but not honored by client" ) );
5891 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5892 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005893 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005894
5895 /* Fatal and closure alerts handled by ssl_read_record() */
5896 if( ssl->in_msgtype == SSL_MSG_ALERT )
5897 {
5898 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5899 return( POLARSSL_ERR_NET_WANT_READ );
5900 }
5901
5902 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 {
5904 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005905 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005906 }
5907
5908 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005909
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005910#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005911 /* We're going to return something now, cancel timer,
5912 * except if handshake (renegotiation) is in progress */
5913 if( ssl->state == SSL_HANDSHAKE_OVER )
5914 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005915#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
5917
5918 n = ( len < ssl->in_msglen )
5919 ? len : ssl->in_msglen;
5920
5921 memcpy( buf, ssl->in_offt, n );
5922 ssl->in_msglen -= n;
5923
5924 if( ssl->in_msglen == 0 )
5925 /* all bytes consumed */
5926 ssl->in_offt = NULL;
5927 else
5928 /* more data available */
5929 ssl->in_offt += n;
5930
5931 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5932
Paul Bakker23986e52011-04-24 08:57:21 +00005933 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005934}
5935
5936/*
5937 * Send application data to be encrypted by the SSL layer
5938 */
Paul Bakker23986e52011-04-24 08:57:21 +00005939int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005940{
Paul Bakker23986e52011-04-24 08:57:21 +00005941 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005942#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
5943 unsigned int max_len;
5944#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005945
5946 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5947
5948 if( ssl->state != SSL_HANDSHAKE_OVER )
5949 {
5950 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5951 {
5952 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5953 return( ret );
5954 }
5955 }
5956
Paul Bakker05decb22013-08-15 13:33:48 +02005957#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005958 /*
5959 * Assume mfl_code is correct since it was checked when set
5960 */
5961 max_len = mfl_code_to_length[ssl->mfl_code];
5962
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005963 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005964 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005965 */
5966 if( ssl->session_out != NULL &&
5967 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5968 {
5969 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5970 }
5971
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005972 if( len > max_len )
5973 {
5974#if defined(POLARSSL_SSL_PROTO_DTLS)
5975 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5976 {
5977 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
5978 "maximum fragment length: %d > %d",
5979 len, max_len ) );
5980 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5981 }
5982 else
5983#endif
5984 len = max_len;
5985 }
5986#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00005987
Paul Bakker5121ce52009-01-03 21:22:43 +00005988 if( ssl->out_left != 0 )
5989 {
5990 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5991 {
5992 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5993 return( ret );
5994 }
5995 }
Paul Bakker887bd502011-06-08 13:10:54 +00005996 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005997 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02005998 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00005999 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006000 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006001
6002 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6003 {
6004 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6005 return( ret );
6006 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006007 }
6008
6009 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6010
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006011 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006012}
6013
6014/*
6015 * Notify the peer that the connection is being closed
6016 */
6017int ssl_close_notify( ssl_context *ssl )
6018{
6019 int ret;
6020
6021 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6022
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006023 if( ssl->out_left != 0 )
6024 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006025
6026 if( ssl->state == SSL_HANDSHAKE_OVER )
6027 {
Paul Bakker48916f92012-09-16 19:57:18 +00006028 if( ( ret = ssl_send_alert_message( ssl,
6029 SSL_ALERT_LEVEL_WARNING,
6030 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006031 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006032 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 return( ret );
6034 }
6035 }
6036
6037 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6038
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006039 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006040}
6041
Paul Bakker48916f92012-09-16 19:57:18 +00006042void ssl_transform_free( ssl_transform *transform )
6043{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006044 if( transform == NULL )
6045 return;
6046
Paul Bakker48916f92012-09-16 19:57:18 +00006047#if defined(POLARSSL_ZLIB_SUPPORT)
6048 deflateEnd( &transform->ctx_deflate );
6049 inflateEnd( &transform->ctx_inflate );
6050#endif
6051
Paul Bakker84bbeb52014-07-01 14:53:22 +02006052 cipher_free( &transform->cipher_ctx_enc );
6053 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006054
Paul Bakker84bbeb52014-07-01 14:53:22 +02006055 md_free( &transform->md_ctx_enc );
6056 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006057
Paul Bakker34617722014-06-13 17:20:13 +02006058 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006059}
6060
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006061#if defined(POLARSSL_X509_CRT_PARSE_C)
6062static void ssl_key_cert_free( ssl_key_cert *key_cert )
6063{
6064 ssl_key_cert *cur = key_cert, *next;
6065
6066 while( cur != NULL )
6067 {
6068 next = cur->next;
6069
6070 if( cur->key_own_alloc )
6071 {
6072 pk_free( cur->key );
6073 polarssl_free( cur->key );
6074 }
6075 polarssl_free( cur );
6076
6077 cur = next;
6078 }
6079}
6080#endif /* POLARSSL_X509_CRT_PARSE_C */
6081
Paul Bakker48916f92012-09-16 19:57:18 +00006082void ssl_handshake_free( ssl_handshake_params *handshake )
6083{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006084 if( handshake == NULL )
6085 return;
6086
Paul Bakker48916f92012-09-16 19:57:18 +00006087#if defined(POLARSSL_DHM_C)
6088 dhm_free( &handshake->dhm_ctx );
6089#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006090#if defined(POLARSSL_ECDH_C)
6091 ecdh_free( &handshake->ecdh_ctx );
6092#endif
6093
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006094#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006095 /* explicit void pointer cast for buggy MS compiler */
6096 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006097#endif
6098
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006099#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6100 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6101 /*
6102 * Free only the linked list wrapper, not the keys themselves
6103 * since the belong to the SNI callback
6104 */
6105 if( handshake->sni_key_cert != NULL )
6106 {
6107 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6108
6109 while( cur != NULL )
6110 {
6111 next = cur->next;
6112 polarssl_free( cur );
6113 cur = next;
6114 }
6115 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006116#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006117
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006118#if defined(POLARSSL_SSL_PROTO_DTLS)
6119 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006120 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006121 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006122#endif
6123
Paul Bakker34617722014-06-13 17:20:13 +02006124 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006125}
6126
6127void ssl_session_free( ssl_session *session )
6128{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006129 if( session == NULL )
6130 return;
6131
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006132#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006133 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006134 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006135 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006136 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006137 }
Paul Bakkered27a042013-04-18 22:46:23 +02006138#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006139
Paul Bakkera503a632013-08-14 13:48:06 +02006140#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006141 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006142#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006143
Paul Bakker34617722014-06-13 17:20:13 +02006144 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006145}
6146
Paul Bakker5121ce52009-01-03 21:22:43 +00006147/*
6148 * Free an SSL context
6149 */
6150void ssl_free( ssl_context *ssl )
6151{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006152 if( ssl == NULL )
6153 return;
6154
Paul Bakker5121ce52009-01-03 21:22:43 +00006155 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6156
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006157 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006158 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006159 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6160 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006161 }
6162
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006163 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006164 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006165 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6166 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006167 }
6168
Paul Bakker16770332013-10-11 09:59:44 +02006169#if defined(POLARSSL_ZLIB_SUPPORT)
6170 if( ssl->compress_buf != NULL )
6171 {
Paul Bakker34617722014-06-13 17:20:13 +02006172 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006173 polarssl_free( ssl->compress_buf );
6174 }
6175#endif
6176
Paul Bakker40e46942009-01-03 21:51:57 +00006177#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006178 mpi_free( &ssl->dhm_P );
6179 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006180#endif
6181
Paul Bakker48916f92012-09-16 19:57:18 +00006182 if( ssl->transform )
6183 {
6184 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006185 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006186 }
6187
6188 if( ssl->handshake )
6189 {
6190 ssl_handshake_free( ssl->handshake );
6191 ssl_transform_free( ssl->transform_negotiate );
6192 ssl_session_free( ssl->session_negotiate );
6193
Paul Bakker6e339b52013-07-03 13:37:05 +02006194 polarssl_free( ssl->handshake );
6195 polarssl_free( ssl->transform_negotiate );
6196 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006197 }
6198
Paul Bakkerc0463502013-02-14 11:19:38 +01006199 if( ssl->session )
6200 {
6201 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006202 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006203 }
6204
Paul Bakkera503a632013-08-14 13:48:06 +02006205#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006206 if( ssl->ticket_keys )
6207 {
6208 ssl_ticket_keys_free( ssl->ticket_keys );
6209 polarssl_free( ssl->ticket_keys );
6210 }
Paul Bakkera503a632013-08-14 13:48:06 +02006211#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006212
Paul Bakker0be444a2013-08-27 21:55:01 +02006213#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006214 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006215 {
Paul Bakker34617722014-06-13 17:20:13 +02006216 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006217 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006218 ssl->hostname_len = 0;
6219 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006221
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006222#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006223 if( ssl->psk != NULL )
6224 {
Paul Bakker34617722014-06-13 17:20:13 +02006225 polarssl_zeroize( ssl->psk, ssl->psk_len );
6226 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006227 polarssl_free( ssl->psk );
6228 polarssl_free( ssl->psk_identity );
6229 ssl->psk_len = 0;
6230 ssl->psk_identity_len = 0;
6231 }
6232#endif
6233
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006234#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006235 ssl_key_cert_free( ssl->key_cert );
6236#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006237
Paul Bakker05ef8352012-05-08 09:17:57 +00006238#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6239 if( ssl_hw_record_finish != NULL )
6240 {
6241 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6242 ssl_hw_record_finish( ssl );
6243 }
6244#endif
6245
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006246#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006247 polarssl_free( ssl->cli_id );
6248#endif
6249
Paul Bakker5121ce52009-01-03 21:22:43 +00006250 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006251
Paul Bakker86f04f42013-02-14 11:20:09 +01006252 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006253 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006254}
6255
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006256#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006257/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006258 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006259 */
6260unsigned char ssl_sig_from_pk( pk_context *pk )
6261{
6262#if defined(POLARSSL_RSA_C)
6263 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6264 return( SSL_SIG_RSA );
6265#endif
6266#if defined(POLARSSL_ECDSA_C)
6267 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6268 return( SSL_SIG_ECDSA );
6269#endif
6270 return( SSL_SIG_ANON );
6271}
6272
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006273pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6274{
6275 switch( sig )
6276 {
6277#if defined(POLARSSL_RSA_C)
6278 case SSL_SIG_RSA:
6279 return( POLARSSL_PK_RSA );
6280#endif
6281#if defined(POLARSSL_ECDSA_C)
6282 case SSL_SIG_ECDSA:
6283 return( POLARSSL_PK_ECDSA );
6284#endif
6285 default:
6286 return( POLARSSL_PK_NONE );
6287 }
6288}
Paul Bakker9af723c2014-05-01 13:03:14 +02006289#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006290
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006291/*
6292 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6293 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006294md_type_t ssl_md_alg_from_hash( unsigned char hash )
6295{
6296 switch( hash )
6297 {
6298#if defined(POLARSSL_MD5_C)
6299 case SSL_HASH_MD5:
6300 return( POLARSSL_MD_MD5 );
6301#endif
6302#if defined(POLARSSL_SHA1_C)
6303 case SSL_HASH_SHA1:
6304 return( POLARSSL_MD_SHA1 );
6305#endif
6306#if defined(POLARSSL_SHA256_C)
6307 case SSL_HASH_SHA224:
6308 return( POLARSSL_MD_SHA224 );
6309 case SSL_HASH_SHA256:
6310 return( POLARSSL_MD_SHA256 );
6311#endif
6312#if defined(POLARSSL_SHA512_C)
6313 case SSL_HASH_SHA384:
6314 return( POLARSSL_MD_SHA384 );
6315 case SSL_HASH_SHA512:
6316 return( POLARSSL_MD_SHA512 );
6317#endif
6318 default:
6319 return( POLARSSL_MD_NONE );
6320 }
6321}
6322
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006323#if defined(POLARSSL_SSL_SET_CURVES)
6324/*
6325 * Check is a curve proposed by the peer is in our list.
6326 * Return 1 if we're willing to use it, 0 otherwise.
6327 */
6328int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6329{
6330 const ecp_group_id *gid;
6331
6332 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6333 if( *gid == grp_id )
6334 return( 1 );
6335
6336 return( 0 );
6337}
Paul Bakker9af723c2014-05-01 13:03:14 +02006338#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006339
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006340#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006341int ssl_check_cert_usage( const x509_crt *cert,
6342 const ssl_ciphersuite_t *ciphersuite,
6343 int cert_endpoint )
6344{
6345#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6346 int usage = 0;
6347#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006348#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6349 const char *ext_oid;
6350 size_t ext_len;
6351#endif
6352
6353#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6354 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6355 ((void) cert);
6356 ((void) cert_endpoint);
6357#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006358
6359#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6360 if( cert_endpoint == SSL_IS_SERVER )
6361 {
6362 /* Server part of the key exchange */
6363 switch( ciphersuite->key_exchange )
6364 {
6365 case POLARSSL_KEY_EXCHANGE_RSA:
6366 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6367 usage = KU_KEY_ENCIPHERMENT;
6368 break;
6369
6370 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6371 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6372 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6373 usage = KU_DIGITAL_SIGNATURE;
6374 break;
6375
6376 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6377 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6378 usage = KU_KEY_AGREEMENT;
6379 break;
6380
6381 /* Don't use default: we want warnings when adding new values */
6382 case POLARSSL_KEY_EXCHANGE_NONE:
6383 case POLARSSL_KEY_EXCHANGE_PSK:
6384 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6385 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6386 usage = 0;
6387 }
6388 }
6389 else
6390 {
6391 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6392 usage = KU_DIGITAL_SIGNATURE;
6393 }
6394
6395 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6396 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006397#else
6398 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006399#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6400
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006401#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6402 if( cert_endpoint == SSL_IS_SERVER )
6403 {
6404 ext_oid = OID_SERVER_AUTH;
6405 ext_len = OID_SIZE( OID_SERVER_AUTH );
6406 }
6407 else
6408 {
6409 ext_oid = OID_CLIENT_AUTH;
6410 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6411 }
6412
6413 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6414 return( -1 );
6415#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6416
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006417 return( 0 );
6418}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006419#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006420
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006421/*
6422 * Convert version numbers to/from wire format
6423 * and, for DTLS, to/from TLS equivalent.
6424 *
6425 * For TLS this is the identity.
6426 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6427 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6428 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6429 */
6430void ssl_write_version( int major, int minor, int transport,
6431 unsigned char ver[2] )
6432{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006433#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006434 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006435 {
6436 if( minor == SSL_MINOR_VERSION_2 )
6437 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6438
6439 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6440 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6441 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006442 else
6443#else
6444 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006445#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006446 {
6447 ver[0] = (unsigned char) major;
6448 ver[1] = (unsigned char) minor;
6449 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006450}
6451
6452void ssl_read_version( int *major, int *minor, int transport,
6453 const unsigned char ver[2] )
6454{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006455#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006456 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006457 {
6458 *major = 255 - ver[0] + 2;
6459 *minor = 255 - ver[1] + 1;
6460
6461 if( *minor == SSL_MINOR_VERSION_1 )
6462 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6463 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006464 else
6465#else
6466 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006467#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006468 {
6469 *major = ver[0];
6470 *minor = ver[1];
6471 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006472}
6473
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006474#endif /* POLARSSL_SSL_TLS_C */