blob: 43f7f1464b08b571c833cec9e81639dafc3f6e94 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020082#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083/*
84 * Start a timer.
85 * Passing millisecs = 0 cancels a running timer.
86 * The timer is already running iff time_limit != 0.
87 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020088static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
90 ssl->time_limit = millisecs;
91 get_timer( &ssl->time_info, 1 );
92}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020097static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
99 if( ssl->time_limit != 0 &&
100 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
101 {
102 return( -1 );
103 }
104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200108/*
109 * Double the retransmit timeout value, within the allowed range,
110 * returning -1 if the maximum value has already been reached.
111 */
112static int ssl_double_retransmit_timeout( ssl_context *ssl )
113{
114 uint32_t new_timeout;
115
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200116 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200117 return( -1 );
118
119 new_timeout = 2 * ssl->handshake->retransmit_timeout;
120
121 /* Avoid arithmetic overflow and range overflow */
122 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200125 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200126 }
127
128 ssl->handshake->retransmit_timeout = new_timeout;
129 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
130 ssl->handshake->retransmit_timeout ) );
131
132 return( 0 );
133}
134
135static void ssl_reset_retransmit_timeout( ssl_context *ssl )
136{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200137 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200138 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
139 ssl->handshake->retransmit_timeout ) );
140}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200141#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200142
Paul Bakker05decb22013-08-15 13:33:48 +0200143#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200144/*
145 * Convert max_fragment_length codes to length.
146 * RFC 6066 says:
147 * enum{
148 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
149 * } MaxFragmentLength;
150 * and we add 0 -> extension unused
151 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200152static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200153{
154 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
155 512, /* SSL_MAX_FRAG_LEN_512 */
156 1024, /* SSL_MAX_FRAG_LEN_1024 */
157 2048, /* SSL_MAX_FRAG_LEN_2048 */
158 4096, /* SSL_MAX_FRAG_LEN_4096 */
159};
Paul Bakker05decb22013-08-15 13:33:48 +0200160#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200161
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
163{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200164 ssl_session_free( dst );
165 memcpy( dst, src, sizeof( ssl_session ) );
166
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200168 if( src->peer_cert != NULL )
169 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200170 int ret;
171
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
173 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200174 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
175
Paul Bakkerb6b09562013-09-18 14:17:41 +0200176 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200177
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200178 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
179 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 {
181 polarssl_free( dst->peer_cert );
182 dst->peer_cert = NULL;
183 return( ret );
184 }
185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187
Paul Bakkera503a632013-08-14 13:48:06 +0200188#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200189 if( src->ticket != NULL )
190 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200191 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
192 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200193 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
194
195 memcpy( dst->ticket, src->ticket, src->ticket_len );
196 }
Paul Bakkera503a632013-08-14 13:48:06 +0200197#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200198
199 return( 0 );
200}
201
Paul Bakker05ef8352012-05-08 09:17:57 +0000202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200203int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200204 const unsigned char *key_enc, const unsigned char *key_dec,
205 size_t keylen,
206 const unsigned char *iv_enc, const unsigned char *iv_dec,
207 size_t ivlen,
208 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200209 size_t maclen ) = NULL;
210int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
211int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
213int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
214int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200215#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000216
Paul Bakker5121ce52009-01-03 21:22:43 +0000217/*
218 * Key material generation
219 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200220#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200221static int ssl3_prf( const unsigned char *secret, size_t slen,
222 const char *label,
223 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000224 unsigned char *dstbuf, size_t dlen )
225{
226 size_t i;
227 md5_context md5;
228 sha1_context sha1;
229 unsigned char padding[16];
230 unsigned char sha1sum[20];
231 ((void)label);
232
Paul Bakker5b4af392014-06-26 12:09:34 +0200233 md5_init( &md5 );
234 sha1_init( &sha1 );
235
Paul Bakker5f70b252012-09-13 14:23:06 +0000236 /*
237 * SSLv3:
238 * block =
239 * MD5( secret + SHA1( 'A' + secret + random ) ) +
240 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
241 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
242 * ...
243 */
244 for( i = 0; i < dlen / 16; i++ )
245 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000247
248 sha1_starts( &sha1 );
249 sha1_update( &sha1, padding, 1 + i );
250 sha1_update( &sha1, secret, slen );
251 sha1_update( &sha1, random, rlen );
252 sha1_finish( &sha1, sha1sum );
253
254 md5_starts( &md5 );
255 md5_update( &md5, secret, slen );
256 md5_update( &md5, sha1sum, 20 );
257 md5_finish( &md5, dstbuf + i * 16 );
258 }
259
Paul Bakker5b4af392014-06-26 12:09:34 +0200260 md5_free( &md5 );
261 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000262
Paul Bakker34617722014-06-13 17:20:13 +0200263 polarssl_zeroize( padding, sizeof( padding ) );
264 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000265
266 return( 0 );
267}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000269
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200270#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200271static int tls1_prf( const unsigned char *secret, size_t slen,
272 const char *label,
273 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000274 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000275{
Paul Bakker23986e52011-04-24 08:57:21 +0000276 size_t nb, hs;
277 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200278 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char tmp[128];
280 unsigned char h_i[20];
281
282 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 hs = ( slen + 1 ) / 2;
286 S1 = secret;
287 S2 = secret + slen - hs;
288
289 nb = strlen( label );
290 memcpy( tmp + 20, label, nb );
291 memcpy( tmp + 20 + nb, random, rlen );
292 nb += rlen;
293
294 /*
295 * First compute P_md5(secret,label+random)[0..dlen]
296 */
297 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
298
299 for( i = 0; i < dlen; i += 16 )
300 {
301 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
302 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
303
304 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
305
306 for( j = 0; j < k; j++ )
307 dstbuf[i + j] = h_i[j];
308 }
309
310 /*
311 * XOR out with P_sha1(secret,label+random)[0..dlen]
312 */
313 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
314
315 for( i = 0; i < dlen; i += 20 )
316 {
317 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
318 sha1_hmac( S2, hs, tmp, 20, tmp );
319
320 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
321
322 for( j = 0; j < k; j++ )
323 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
324 }
325
Paul Bakker34617722014-06-13 17:20:13 +0200326 polarssl_zeroize( tmp, sizeof( tmp ) );
327 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 return( 0 );
330}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200331#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#if defined(POLARSSL_SSL_PROTO_TLS1_2)
334#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200335static int tls_prf_sha256( const unsigned char *secret, size_t slen,
336 const char *label,
337 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000338 unsigned char *dstbuf, size_t dlen )
339{
340 size_t nb;
341 size_t i, j, k;
342 unsigned char tmp[128];
343 unsigned char h_i[32];
344
345 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
346 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
347
348 nb = strlen( label );
349 memcpy( tmp + 32, label, nb );
350 memcpy( tmp + 32 + nb, random, rlen );
351 nb += rlen;
352
353 /*
354 * Compute P_<hash>(secret, label + random)[0..dlen]
355 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200356 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000357
358 for( i = 0; i < dlen; i += 32 )
359 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200360 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
361 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000362
363 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
364
365 for( j = 0; j < k; j++ )
366 dstbuf[i + j] = h_i[j];
367 }
368
Paul Bakker34617722014-06-13 17:20:13 +0200369 polarssl_zeroize( tmp, sizeof( tmp ) );
370 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000371
372 return( 0 );
373}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200374#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker9e36f042013-06-30 14:34:05 +0200376#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200377static int tls_prf_sha384( const unsigned char *secret, size_t slen,
378 const char *label,
379 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000380 unsigned char *dstbuf, size_t dlen )
381{
382 size_t nb;
383 size_t i, j, k;
384 unsigned char tmp[128];
385 unsigned char h_i[48];
386
387 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389
390 nb = strlen( label );
391 memcpy( tmp + 48, label, nb );
392 memcpy( tmp + 48 + nb, random, rlen );
393 nb += rlen;
394
395 /*
396 * Compute P_<hash>(secret, label + random)[0..dlen]
397 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200398 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399
400 for( i = 0; i < dlen; i += 48 )
401 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200402 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
403 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404
405 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
406
407 for( j = 0; j < k; j++ )
408 dstbuf[i + j] = h_i[j];
409 }
410
Paul Bakker34617722014-06-13 17:20:13 +0200411 polarssl_zeroize( tmp, sizeof( tmp ) );
412 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413
414 return( 0 );
415}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200416#endif /* POLARSSL_SHA512_C */
417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418
Paul Bakker66d5d072014-06-17 16:39:18 +0200419static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200420
421#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
422 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200423static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#endif
Paul Bakker380da532012-04-18 16:10:25 +0000425
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200427static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
428static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#endif
430
431#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200432static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
433static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435
436#if defined(POLARSSL_SSL_PROTO_TLS1_2)
437#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200438static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
439static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
440static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200441#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100442
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200444static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
445static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
446static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100447#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200448#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
Paul Bakker5121ce52009-01-03 21:22:43 +0000450int ssl_derive_keys( ssl_context *ssl )
451{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200452 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 unsigned char keyblk[256];
455 unsigned char *key1;
456 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100457 unsigned char *mac_enc;
458 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200459 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100460 const cipher_info_t *cipher_info;
461 const md_info_t *md_info;
462
Paul Bakker48916f92012-09-16 19:57:18 +0000463 ssl_session *session = ssl->session_negotiate;
464 ssl_transform *transform = ssl->transform_negotiate;
465 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
468
Paul Bakker68884e32013-01-07 18:20:04 +0100469 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
470 if( cipher_info == NULL )
471 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200472 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100473 transform->ciphersuite_info->cipher ) );
474 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
475 }
476
477 md_info = md_info_from_type( transform->ciphersuite_info->mac );
478 if( md_info == NULL )
479 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200480 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100481 transform->ciphersuite_info->mac ) );
482 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
483 }
484
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000486 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200488#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000489 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000490 {
Paul Bakker48916f92012-09-16 19:57:18 +0000491 handshake->tls_prf = ssl3_prf;
492 handshake->calc_verify = ssl_calc_verify_ssl;
493 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200495 else
496#endif
497#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
498 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000499 {
Paul Bakker48916f92012-09-16 19:57:18 +0000500 handshake->tls_prf = tls1_prf;
501 handshake->calc_verify = ssl_calc_verify_tls;
502 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000503 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200504 else
505#endif
506#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200507#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200508 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
509 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000510 {
Paul Bakker48916f92012-09-16 19:57:18 +0000511 handshake->tls_prf = tls_prf_sha384;
512 handshake->calc_verify = ssl_calc_verify_tls_sha384;
513 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000514 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000515 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516#endif
517#if defined(POLARSSL_SHA256_C)
518 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000519 {
Paul Bakker48916f92012-09-16 19:57:18 +0000520 handshake->tls_prf = tls_prf_sha256;
521 handshake->calc_verify = ssl_calc_verify_tls_sha256;
522 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000523 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200524 else
525#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200526#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200527 {
528 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200529 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200530 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000531
532 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * SSLv3:
534 * master =
535 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
536 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
537 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200538 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * master = PRF( premaster, "master secret", randbytes )[0..47]
541 */
Paul Bakker0a597072012-09-25 21:55:46 +0000542 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 {
Paul Bakker48916f92012-09-16 19:57:18 +0000544 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
545 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200547#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
548 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200549 {
550 unsigned char session_hash[48];
551 size_t hash_len;
552
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200553 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200554
555 ssl->handshake->calc_verify( ssl, session_hash );
556
557#if defined(POLARSSL_SSL_PROTO_TLS1_2)
558 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
559 {
560#if defined(POLARSSL_SHA512_C)
561 if( ssl->transform_negotiate->ciphersuite_info->mac ==
562 POLARSSL_MD_SHA384 )
563 {
564 hash_len = 48;
565 }
566 else
567#endif
568 hash_len = 32;
569 }
570 else
571#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
572 hash_len = 36;
573
574 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
575
576 handshake->tls_prf( handshake->premaster, handshake->pmslen,
577 "extended master secret",
578 session_hash, hash_len, session->master, 48 );
579
580 }
581 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200582#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000583 handshake->tls_prf( handshake->premaster, handshake->pmslen,
584 "master secret",
585 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200587
Paul Bakker34617722014-06-13 17:20:13 +0200588 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 }
590 else
591 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
592
593 /*
594 * Swap the client and server random values.
595 */
Paul Bakker48916f92012-09-16 19:57:18 +0000596 memcpy( tmp, handshake->randbytes, 64 );
597 memcpy( handshake->randbytes, tmp + 32, 32 );
598 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200599 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601 /*
602 * SSLv3:
603 * key block =
604 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
605 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
606 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
607 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
608 * ...
609 *
610 * TLSv1:
611 * key block = PRF( master, "key expansion", randbytes )
612 */
Paul Bakker48916f92012-09-16 19:57:18 +0000613 handshake->tls_prf( session->master, 48, "key expansion",
614 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
Paul Bakker48916f92012-09-16 19:57:18 +0000616 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
617 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
618 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
619 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
621
Paul Bakker34617722014-06-13 17:20:13 +0200622 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
624 /*
625 * Determine the appropriate key, IV and MAC length.
626 */
Paul Bakker68884e32013-01-07 18:20:04 +0100627
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200628 transform->keylen = cipher_info->key_length / 8;
629
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200630 if( cipher_info->mode == POLARSSL_MODE_GCM ||
631 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200633 transform->maclen = 0;
634
Paul Bakker68884e32013-01-07 18:20:04 +0100635 transform->ivlen = 12;
636 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200637
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200638 /* Minimum length is expicit IV + tag */
639 transform->minlen = transform->ivlen - transform->fixed_ivlen
640 + ( transform->ciphersuite_info->flags &
641 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100642 }
643 else
644 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200645 int ret;
646
647 /* Initialize HMAC contexts */
648 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
649 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100650 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200651 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
652 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100653 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200655 /* Get MAC length */
656 transform->maclen = md_get_size( md_info );
657
658#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
659 /*
660 * If HMAC is to be truncated, we shall keep the leftmost bytes,
661 * (rfc 6066 page 13 or rfc 2104 section 4),
662 * so we only need to adjust the length here.
663 */
664 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
665 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
666#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
667
668 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100669 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200671 /* Minimum length */
672 if( cipher_info->mode == POLARSSL_MODE_STREAM )
673 transform->minlen = transform->maclen;
674 else
Paul Bakker68884e32013-01-07 18:20:04 +0100675 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200676 /*
677 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100678 * 1. if EtM is in use: one block plus MAC
679 * otherwise: * first multiple of blocklen greater than maclen
680 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200681 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100682#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
683 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
684 {
685 transform->minlen = transform->maclen
686 + cipher_info->block_size;
687 }
688 else
689#endif
690 {
691 transform->minlen = transform->maclen
692 + cipher_info->block_size
693 - transform->maclen % cipher_info->block_size;
694 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200695
696#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
697 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
698 ssl->minor_ver == SSL_MINOR_VERSION_1 )
699 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100700 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200701#endif
702#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
703 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
704 ssl->minor_ver == SSL_MINOR_VERSION_3 )
705 {
706 transform->minlen += transform->ivlen;
707 }
708 else
709#endif
710 {
711 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
712 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
713 }
Paul Bakker68884e32013-01-07 18:20:04 +0100714 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 }
716
717 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000718 transform->keylen, transform->minlen, transform->ivlen,
719 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721 /*
722 * Finally setup the cipher contexts, IVs and MAC secrets.
723 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100724#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000725 if( ssl->endpoint == SSL_IS_CLIENT )
726 {
Paul Bakker48916f92012-09-16 19:57:18 +0000727 key1 = keyblk + transform->maclen * 2;
728 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000729
Paul Bakker68884e32013-01-07 18:20:04 +0100730 mac_enc = keyblk;
731 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000733 /*
734 * This is not used in TLS v1.1.
735 */
Paul Bakker48916f92012-09-16 19:57:18 +0000736 iv_copy_len = ( transform->fixed_ivlen ) ?
737 transform->fixed_ivlen : transform->ivlen;
738 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
739 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000740 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000741 }
742 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100743#endif /* POLARSSL_SSL_CLI_C */
744#if defined(POLARSSL_SSL_SRV_C)
745 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 {
Paul Bakker48916f92012-09-16 19:57:18 +0000747 key1 = keyblk + transform->maclen * 2 + transform->keylen;
748 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
Paul Bakker68884e32013-01-07 18:20:04 +0100750 mac_enc = keyblk + transform->maclen;
751 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000753 /*
754 * This is not used in TLS v1.1.
755 */
Paul Bakker48916f92012-09-16 19:57:18 +0000756 iv_copy_len = ( transform->fixed_ivlen ) ?
757 transform->fixed_ivlen : transform->ivlen;
758 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
759 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100762 else
763#endif /* POLARSSL_SSL_SRV_C */
764 {
765 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
766 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
767 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100770 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
771 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100772 if( transform->maclen > sizeof transform->mac_enc )
773 {
774 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200775 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100776 }
777
Paul Bakker68884e32013-01-07 18:20:04 +0100778 memcpy( transform->mac_enc, mac_enc, transform->maclen );
779 memcpy( transform->mac_dec, mac_dec, transform->maclen );
780 }
781 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200782#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
784 defined(POLARSSL_SSL_PROTO_TLS1_2)
785 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100786 {
787 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
788 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
789 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790 else
791#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200792 {
793 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200794 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200795 }
Paul Bakker68884e32013-01-07 18:20:04 +0100796
Paul Bakker05ef8352012-05-08 09:17:57 +0000797#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200798 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000799 {
800 int ret = 0;
801
802 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
803
Paul Bakker07eb38b2012-12-19 14:42:06 +0100804 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
805 transform->iv_enc, transform->iv_dec,
806 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100807 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100808 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000809 {
810 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200811 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000812 }
813 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200814#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000815
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200816 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
817 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000818 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200819 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
820 return( ret );
821 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200822
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200823 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
824 cipher_info ) ) != 0 )
825 {
826 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
827 return( ret );
828 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200829
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200830 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
831 cipher_info->key_length,
832 POLARSSL_ENCRYPT ) ) != 0 )
833 {
834 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
835 return( ret );
836 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200837
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200838 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
839 cipher_info->key_length,
840 POLARSSL_DECRYPT ) ) != 0 )
841 {
842 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
843 return( ret );
844 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200845
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200846#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200847 if( cipher_info->mode == POLARSSL_MODE_CBC )
848 {
849 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
850 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200851 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200852 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
853 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200854 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200855
856 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
857 POLARSSL_PADDING_NONE ) ) != 0 )
858 {
859 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
860 return( ret );
861 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200863#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Paul Bakker34617722014-06-13 17:20:13 +0200865 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000866
Paul Bakker2770fbd2012-07-03 13:30:23 +0000867#if defined(POLARSSL_ZLIB_SUPPORT)
868 // Initialize compression
869 //
Paul Bakker48916f92012-09-16 19:57:18 +0000870 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000871 {
Paul Bakker16770332013-10-11 09:59:44 +0200872 if( ssl->compress_buf == NULL )
873 {
874 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
875 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
876 if( ssl->compress_buf == NULL )
877 {
878 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
879 SSL_BUFFER_LEN ) );
880 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
881 }
882 }
883
Paul Bakker2770fbd2012-07-03 13:30:23 +0000884 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
885
Paul Bakker48916f92012-09-16 19:57:18 +0000886 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
887 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000888
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200889 if( deflateInit( &transform->ctx_deflate,
890 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000891 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000892 {
893 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
894 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
895 }
896 }
897#endif /* POLARSSL_ZLIB_SUPPORT */
898
Paul Bakker5121ce52009-01-03 21:22:43 +0000899 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
900
901 return( 0 );
902}
903
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200904#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000905void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000906{
907 md5_context md5;
908 sha1_context sha1;
909 unsigned char pad_1[48];
910 unsigned char pad_2[48];
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000913
Paul Bakker48916f92012-09-16 19:57:18 +0000914 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
915 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
Paul Bakker380da532012-04-18 16:10:25 +0000917 memset( pad_1, 0x36, 48 );
918 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakker48916f92012-09-16 19:57:18 +0000920 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000921 md5_update( &md5, pad_1, 48 );
922 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000923
Paul Bakker380da532012-04-18 16:10:25 +0000924 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000925 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000926 md5_update( &md5, pad_2, 48 );
927 md5_update( &md5, hash, 16 );
928 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Paul Bakker48916f92012-09-16 19:57:18 +0000930 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000931 sha1_update( &sha1, pad_1, 40 );
932 sha1_finish( &sha1, hash + 16 );
933
934 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000935 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000936 sha1_update( &sha1, pad_2, 40 );
937 sha1_update( &sha1, hash + 16, 20 );
938 sha1_finish( &sha1, hash + 16 );
939
940 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
941 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
942
Paul Bakker5b4af392014-06-26 12:09:34 +0200943 md5_free( &md5 );
944 sha1_free( &sha1 );
945
Paul Bakker380da532012-04-18 16:10:25 +0000946 return;
947}
Paul Bakker9af723c2014-05-01 13:03:14 +0200948#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000949
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200950#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000951void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
952{
953 md5_context md5;
954 sha1_context sha1;
955
956 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
957
Paul Bakker48916f92012-09-16 19:57:18 +0000958 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
959 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000960
Paul Bakker48916f92012-09-16 19:57:18 +0000961 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000962 sha1_finish( &sha1, hash + 16 );
963
964 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
965 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
966
Paul Bakker5b4af392014-06-26 12:09:34 +0200967 md5_free( &md5 );
968 sha1_free( &sha1 );
969
Paul Bakker380da532012-04-18 16:10:25 +0000970 return;
971}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200972#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000973
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200974#if defined(POLARSSL_SSL_PROTO_TLS1_2)
975#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000976void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
977{
Paul Bakker9e36f042013-06-30 14:34:05 +0200978 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000979
980 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
981
Paul Bakker9e36f042013-06-30 14:34:05 +0200982 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
983 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000984
985 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
986 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
987
Paul Bakker5b4af392014-06-26 12:09:34 +0200988 sha256_free( &sha256 );
989
Paul Bakker380da532012-04-18 16:10:25 +0000990 return;
991}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200992#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000993
Paul Bakker9e36f042013-06-30 14:34:05 +0200994#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000995void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
996{
Paul Bakker9e36f042013-06-30 14:34:05 +0200997 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000998
999 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
1000
Paul Bakker9e36f042013-06-30 14:34:05 +02001001 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
1002 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
Paul Bakkerca4ab492012-04-18 14:23:57 +00001004 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001005 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
1006
Paul Bakker5b4af392014-06-26 12:09:34 +02001007 sha512_free( &sha512 );
1008
Paul Bakker5121ce52009-01-03 21:22:43 +00001009 return;
1010}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001011#endif /* POLARSSL_SHA512_C */
1012#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001014#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
1016{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001017 unsigned char *p = ssl->handshake->premaster;
1018 unsigned char *end = p + sizeof( ssl->handshake->premaster );
1019
1020 /*
1021 * PMS = struct {
1022 * opaque other_secret<0..2^16-1>;
1023 * opaque psk<0..2^16-1>;
1024 * };
1025 * with "other_secret" depending on the particular key exchange
1026 */
1027#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
1028 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
1029 {
1030 if( end - p < 2 + (int) ssl->psk_len )
1031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1032
1033 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1034 *(p++) = (unsigned char)( ssl->psk_len );
1035 p += ssl->psk_len;
1036 }
1037 else
1038#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001039#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1040 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1041 {
1042 /*
1043 * other_secret already set by the ClientKeyExchange message,
1044 * and is 48 bytes long
1045 */
1046 *p++ = 0;
1047 *p++ = 48;
1048 p += 48;
1049 }
1050 else
1051#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001052#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1053 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
1054 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001055 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001056 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001057
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001058 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001059 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001060 p + 2, &len,
1061 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001062 {
1063 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1064 return( ret );
1065 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001066 *(p++) = (unsigned char)( len >> 8 );
1067 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001068 p += len;
1069
1070 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1071 }
1072 else
1073#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1074#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1075 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1076 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001077 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001078 size_t zlen;
1079
1080 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001081 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001082 ssl->f_rng, ssl->p_rng ) ) != 0 )
1083 {
1084 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1085 return( ret );
1086 }
1087
1088 *(p++) = (unsigned char)( zlen >> 8 );
1089 *(p++) = (unsigned char)( zlen );
1090 p += zlen;
1091
1092 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1093 }
1094 else
1095#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1096 {
1097 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001098 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001099 }
1100
1101 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001102 if( end - p < 2 + (int) ssl->psk_len )
1103 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1104
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001105 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1106 *(p++) = (unsigned char)( ssl->psk_len );
1107 memcpy( p, ssl->psk, ssl->psk_len );
1108 p += ssl->psk_len;
1109
1110 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1111
1112 return( 0 );
1113}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001114#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001115
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001116#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001117/*
1118 * SSLv3.0 MAC functions
1119 */
Paul Bakker68884e32013-01-07 18:20:04 +01001120static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1121 unsigned char *buf, size_t len,
1122 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001123{
1124 unsigned char header[11];
1125 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001126 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001127 int md_size = md_get_size( md_ctx->md_info );
1128 int md_type = md_get_type( md_ctx->md_info );
1129
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001130 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001131 if( md_type == POLARSSL_MD_MD5 )
1132 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001133 else
Paul Bakker68884e32013-01-07 18:20:04 +01001134 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001135
1136 memcpy( header, ctr, 8 );
1137 header[ 8] = (unsigned char) type;
1138 header[ 9] = (unsigned char)( len >> 8 );
1139 header[10] = (unsigned char)( len );
1140
Paul Bakker68884e32013-01-07 18:20:04 +01001141 memset( padding, 0x36, padlen );
1142 md_starts( md_ctx );
1143 md_update( md_ctx, secret, md_size );
1144 md_update( md_ctx, padding, padlen );
1145 md_update( md_ctx, header, 11 );
1146 md_update( md_ctx, buf, len );
1147 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001148
Paul Bakker68884e32013-01-07 18:20:04 +01001149 memset( padding, 0x5C, padlen );
1150 md_starts( md_ctx );
1151 md_update( md_ctx, secret, md_size );
1152 md_update( md_ctx, padding, padlen );
1153 md_update( md_ctx, buf + len, md_size );
1154 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001155}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001156#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001157
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001158#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1159 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1160 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1161#define POLARSSL_SOME_MODES_USE_MAC
1162#endif
1163
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001164/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001166 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001167static int ssl_encrypt_buf( ssl_context *ssl )
1168{
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001169 cipher_mode_t mode;
1170 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
1172 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1173
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001174 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1175 {
1176 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1177 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1178 }
1179
1180 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1181
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001182 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1183 ssl->out_msg, ssl->out_msglen );
1184
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001186 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001188#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001189 if( mode == POLARSSL_MODE_STREAM ||
1190 ( mode == POLARSSL_MODE_CBC
1191#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1192 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1193#endif
1194 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001196#if defined(POLARSSL_SSL_PROTO_SSL3)
1197 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1198 {
1199 ssl_mac( &ssl->transform_out->md_ctx_enc,
1200 ssl->transform_out->mac_enc,
1201 ssl->out_msg, ssl->out_msglen,
1202 ssl->out_ctr, ssl->out_msgtype );
1203 }
1204 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001205#endif
1206#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001207 defined(POLARSSL_SSL_PROTO_TLS1_2)
1208 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1209 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001210 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1211 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1212 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001213 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1214 ssl->out_msg, ssl->out_msglen );
1215 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1216 ssl->out_msg + ssl->out_msglen );
1217 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1218 }
1219 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001220#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001221 {
1222 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001223 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001224 }
1225
1226 SSL_DEBUG_BUF( 4, "computed mac",
1227 ssl->out_msg + ssl->out_msglen,
1228 ssl->transform_out->maclen );
1229
1230 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001231 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001232 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001233#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001234
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001235 /*
1236 * Encrypt
1237 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001238#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001239 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001240 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001241 int ret;
1242 size_t olen = 0;
1243
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1245 "including %d bytes of padding",
1246 ssl->out_msglen, 0 ) );
1247
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001248 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001249 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001250 ssl->transform_out->ivlen,
1251 ssl->out_msg, ssl->out_msglen,
1252 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001253 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001254 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001255 return( ret );
1256 }
1257
1258 if( ssl->out_msglen != olen )
1259 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001260 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001261 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 }
Paul Bakker68884e32013-01-07 18:20:04 +01001264 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001265#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001266#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1267 if( mode == POLARSSL_MODE_GCM ||
1268 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001269 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001270 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001271 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001272 unsigned char *enc_msg;
1273 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1275 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001276
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 memcpy( add_data, ssl->out_ctr, 8 );
1278 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001279 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1280 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001281 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1282 add_data[12] = ssl->out_msglen & 0xFF;
1283
1284 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1285 add_data, 13 );
1286
Paul Bakker68884e32013-01-07 18:20:04 +01001287 /*
1288 * Generate IV
1289 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001290#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001291 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001292 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1293 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001294 if( ret != 0 )
1295 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001296
Paul Bakker68884e32013-01-07 18:20:04 +01001297 memcpy( ssl->out_iv,
1298 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1299 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001300#else
1301 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1302 {
1303 /* Reminder if we ever add an AEAD mode with a different size */
1304 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1305 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1306 }
1307
1308 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1309 ssl->out_ctr, 8 );
1310 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1311#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001312
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001313 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001314 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001315
Paul Bakker68884e32013-01-07 18:20:04 +01001316 /*
1317 * Fix pointer positions and message length with added IV
1318 */
1319 enc_msg = ssl->out_msg;
1320 enc_msglen = ssl->out_msglen;
1321 ssl->out_msglen += ssl->transform_out->ivlen -
1322 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001323
Paul Bakker68884e32013-01-07 18:20:04 +01001324 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1325 "including %d bytes of padding",
1326 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001327
Paul Bakker68884e32013-01-07 18:20:04 +01001328 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001329 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001330 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001331 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1332 ssl->transform_out->iv_enc,
1333 ssl->transform_out->ivlen,
1334 add_data, 13,
1335 enc_msg, enc_msglen,
1336 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001337 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001338 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001339 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001340 return( ret );
1341 }
1342
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001343 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001344 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001345 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001346 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001347 }
1348
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001349 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001350 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001351
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001352 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001355#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001356#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1357 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001358 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001360 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001361 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001362 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001363
Paul Bakker48916f92012-09-16 19:57:18 +00001364 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1365 ssl->transform_out->ivlen;
1366 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 padlen = 0;
1368
1369 for( i = 0; i <= padlen; i++ )
1370 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1371
1372 ssl->out_msglen += padlen + 1;
1373
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001374 enc_msglen = ssl->out_msglen;
1375 enc_msg = ssl->out_msg;
1376
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001377#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001378 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001379 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1380 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001381 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001382 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001383 {
1384 /*
1385 * Generate IV
1386 */
Paul Bakker48916f92012-09-16 19:57:18 +00001387 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1388 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001389 if( ret != 0 )
1390 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001391
Paul Bakker92be97b2013-01-02 17:30:03 +01001392 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001393 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001394
1395 /*
1396 * Fix pointer positions and message length with added IV
1397 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001398 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001399 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001400 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001401 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001402#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001403
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001405 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001406 ssl->out_msglen, ssl->transform_out->ivlen,
1407 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001408
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001409 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001410 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001411 ssl->transform_out->ivlen,
1412 enc_msg, enc_msglen,
1413 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001414 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001415 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001416 return( ret );
1417 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001418
Paul Bakkercca5b812013-08-31 17:40:26 +02001419 if( enc_msglen != olen )
1420 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001421 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001422 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001423 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001424
1425#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001426 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1427 {
1428 /*
1429 * Save IV in SSL3 and TLS1
1430 */
1431 memcpy( ssl->transform_out->iv_enc,
1432 ssl->transform_out->cipher_ctx_enc.iv,
1433 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001435#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001436
1437#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001438 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001439 {
1440 /*
1441 * MAC(MAC_write_key, seq_num +
1442 * TLSCipherText.type +
1443 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001444 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001445 * IV + // except for TLS 1.0
1446 * ENC(content + padding + padding_length));
1447 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001448 unsigned char pseudo_hdr[13];
1449
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001450 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1451
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001452 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1453 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001454 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1455 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1456
1457 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001458
1459 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1460 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1461 ssl->out_iv, ssl->out_msglen );
1462 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1463 ssl->out_iv + ssl->out_msglen );
1464 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1465
1466 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001467 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001468 }
1469#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001471 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001472#endif /* POLARSSL_CIPHER_MODE_CBC &&
1473 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001474 {
1475 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001476 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001479 /* Make extra sure authentication was performed, exactly once */
1480 if( auth_done != 1 )
1481 {
1482 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1483 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1484 }
1485
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1487
1488 return( 0 );
1489}
1490
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001491#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001492
Paul Bakker5121ce52009-01-03 21:22:43 +00001493static int ssl_decrypt_buf( ssl_context *ssl )
1494{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001495 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001496 cipher_mode_t mode;
1497 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001498#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001499 size_t padlen = 0, correct = 1;
1500#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001501
1502 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1503
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001504 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1505 {
1506 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1507 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1508 }
1509
1510 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1511
Paul Bakker48916f92012-09-16 19:57:18 +00001512 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 {
1514 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001515 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001516 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001517 }
1518
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001519#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001520 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001521 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001522 int ret;
1523 size_t olen = 0;
1524
Paul Bakker68884e32013-01-07 18:20:04 +01001525 padlen = 0;
1526
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001527 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001528 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001529 ssl->transform_in->ivlen,
1530 ssl->in_msg, ssl->in_msglen,
1531 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001532 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001533 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001534 return( ret );
1535 }
1536
1537 if( ssl->in_msglen != olen )
1538 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001539 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001540 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 }
Paul Bakker68884e32013-01-07 18:20:04 +01001543 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001544#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001545#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1546 if( mode == POLARSSL_MODE_GCM ||
1547 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001548 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001549 int ret;
1550 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 unsigned char *dec_msg;
1552 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001553 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001554 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1555 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001556 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1557 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001558
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001559 if( ssl->in_msglen < explicit_iv_len + taglen )
1560 {
1561 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1562 "+ taglen (%d)", ssl->in_msglen,
1563 explicit_iv_len, taglen ) );
1564 return( POLARSSL_ERR_SSL_INVALID_MAC );
1565 }
1566 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1567
Paul Bakker68884e32013-01-07 18:20:04 +01001568 dec_msg = ssl->in_msg;
1569 dec_msg_result = ssl->in_msg;
1570 ssl->in_msglen = dec_msglen;
1571
1572 memcpy( add_data, ssl->in_ctr, 8 );
1573 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001574 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1575 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001576 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1577 add_data[12] = ssl->in_msglen & 0xFF;
1578
1579 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1580 add_data, 13 );
1581
1582 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1583 ssl->in_iv,
1584 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1585
1586 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1587 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001588 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001589
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001590 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001591 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001592 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001593 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1594 ssl->transform_in->iv_dec,
1595 ssl->transform_in->ivlen,
1596 add_data, 13,
1597 dec_msg, dec_msglen,
1598 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001599 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001600 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001601 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1602
1603 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1604 return( POLARSSL_ERR_SSL_INVALID_MAC );
1605
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001606 return( ret );
1607 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001608 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001609
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001610 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001611 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001612 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001613 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001614 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001615 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001616 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001617#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001618#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1619 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001620 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 {
Paul Bakker45829992013-01-03 14:52:21 +01001622 /*
1623 * Decrypt and check the padding
1624 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001625 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001626 unsigned char *dec_msg;
1627 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001628 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001629 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001630 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001631
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 /*
Paul Bakker45829992013-01-03 14:52:21 +01001633 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001635#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001636 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1637 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001638#endif
Paul Bakker45829992013-01-03 14:52:21 +01001639
1640 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1641 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1642 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001643 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1644 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1645 ssl->transform_in->ivlen,
1646 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001647 return( POLARSSL_ERR_SSL_INVALID_MAC );
1648 }
1649
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001650 dec_msglen = ssl->in_msglen;
1651 dec_msg = ssl->in_msg;
1652 dec_msg_result = ssl->in_msg;
1653
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001654 /*
1655 * Authenticate before decrypt if enabled
1656 */
1657#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001658 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001659 {
1660 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001661 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001662
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001663 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1664
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001665 dec_msglen -= ssl->transform_in->maclen;
1666 ssl->in_msglen -= ssl->transform_in->maclen;
1667
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001668 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1669 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1670 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1671 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1672
1673 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1674
1675 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001676 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1677 ssl->in_iv, ssl->in_msglen );
1678 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1679 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1680
1681 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1682 ssl->transform_in->maclen );
1683 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1684 ssl->transform_in->maclen );
1685
1686 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1687 ssl->transform_in->maclen ) != 0 )
1688 {
1689 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1690
1691 return( POLARSSL_ERR_SSL_INVALID_MAC );
1692 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001693 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001694 }
1695#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1696
1697 /*
1698 * Check length sanity
1699 */
1700 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1701 {
1702 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1703 ssl->in_msglen, ssl->transform_in->ivlen ) );
1704 return( POLARSSL_ERR_SSL_INVALID_MAC );
1705 }
1706
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001707#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001708 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001709 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001710 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001711 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001712 {
Paul Bakker48916f92012-09-16 19:57:18 +00001713 dec_msglen -= ssl->transform_in->ivlen;
1714 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001715
Paul Bakker48916f92012-09-16 19:57:18 +00001716 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001717 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001719#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001720
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001721 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001722 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001723 ssl->transform_in->ivlen,
1724 dec_msg, dec_msglen,
1725 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001726 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001727 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001728 return( ret );
1729 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001730
Paul Bakkercca5b812013-08-31 17:40:26 +02001731 if( dec_msglen != olen )
1732 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001733 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001734 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001735 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001736
1737#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001738 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1739 {
1740 /*
1741 * Save IV in SSL3 and TLS1
1742 */
1743 memcpy( ssl->transform_in->iv_dec,
1744 ssl->transform_in->cipher_ctx_dec.iv,
1745 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001747#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
1749 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001750
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001751 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001752 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001753 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001754#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001755 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1756 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001757#endif
Paul Bakker45829992013-01-03 14:52:21 +01001758 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001759 correct = 0;
1760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001762#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001763 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1764 {
Paul Bakker48916f92012-09-16 19:57:18 +00001765 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001766 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001767#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1769 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001770 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001771#endif
Paul Bakker45829992013-01-03 14:52:21 +01001772 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773 }
1774 }
1775 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001776#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001777#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1778 defined(POLARSSL_SSL_PROTO_TLS1_2)
1779 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001780 {
1781 /*
Paul Bakker45829992013-01-03 14:52:21 +01001782 * TLSv1+: always check the padding up to the first failure
1783 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001784 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001785 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001786 size_t padding_idx = ssl->in_msglen - padlen - 1;
1787
Paul Bakker956c9e02013-12-19 14:42:28 +01001788 /*
1789 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001790 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001791 *
Paul Bakker61885c72014-04-25 12:59:03 +02001792 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1793 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001794 *
1795 * In both cases we reset padding_idx to a safe value (0) to
1796 * prevent out-of-buffer reads.
1797 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001798 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001799 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1800 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001801
1802 padding_idx *= correct;
1803
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001804 for( i = 1; i <= 256; i++ )
1805 {
1806 real_count &= ( i <= padlen );
1807 pad_count += real_count *
1808 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1809 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001810
1811 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001812
Paul Bakkerd66f0702013-01-31 16:57:45 +01001813#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001814 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001815 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001816#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001817 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001818 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001819 else
1820#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1821 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001822 {
1823 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001824 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001825 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001826
1827 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001829 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001830#endif /* POLARSSL_CIPHER_MODE_CBC &&
1831 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001832 {
1833 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001834 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001835 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
1837 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1838 ssl->in_msg, ssl->in_msglen );
1839
1840 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001841 * Authenticate if not done yet.
1842 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001843 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001844#if defined(POLARSSL_SOME_MODES_USE_MAC)
1845 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001846 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001847 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1848
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001849 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001851 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1852 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001854 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001856#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1858 {
1859 ssl_mac( &ssl->transform_in->md_ctx_dec,
1860 ssl->transform_in->mac_dec,
1861 ssl->in_msg, ssl->in_msglen,
1862 ssl->in_ctr, ssl->in_msgtype );
1863 }
1864 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001865#endif /* POLARSSL_SSL_PROTO_SSL3 */
1866#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001867 defined(POLARSSL_SSL_PROTO_TLS1_2)
1868 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1869 {
1870 /*
1871 * Process MAC and always update for padlen afterwards to make
1872 * total time independent of padlen
1873 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001874 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001875 *
1876 * Known timing attacks:
1877 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1878 *
1879 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1880 * correctly. (We round down instead of up, so -56 is the correct
1881 * value for our calculations instead of -55)
1882 */
1883 size_t j, extra_run = 0;
1884 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1885 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001886
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001887 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001888
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001889 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1890 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1891 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001892 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1893 ssl->in_msglen );
1894 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1895 ssl->in_msg + ssl->in_msglen );
1896 for( j = 0; j < extra_run; j++ )
1897 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001898
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001899 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1900 }
1901 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001902#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001903 POLARSSL_SSL_PROTO_TLS1_2 */
1904 {
1905 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001906 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001907 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001908
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001909 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1910 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1911 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001913 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001914 ssl->transform_in->maclen ) != 0 )
1915 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001916#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001917 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001918#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001919 correct = 0;
1920 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001921 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001923 /*
1924 * Finally check the correct flag
1925 */
1926 if( correct == 0 )
1927 return( POLARSSL_ERR_SSL_INVALID_MAC );
1928 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001929#endif /* POLARSSL_SOME_MODES_USE_MAC */
1930
1931 /* Make extra sure authentication was performed, exactly once */
1932 if( auth_done != 1 )
1933 {
1934 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1935 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1936 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001937
1938 if( ssl->in_msglen == 0 )
1939 {
1940 ssl->nb_zero++;
1941
1942 /*
1943 * Three or more empty messages may be a DoS attack
1944 * (excessive CPU consumption).
1945 */
1946 if( ssl->nb_zero > 3 )
1947 {
1948 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1949 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001950 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 }
1952 }
1953 else
1954 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001955
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001956#if defined(POLARSSL_SSL_PROTO_DTLS)
1957 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001958 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001959 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001960 }
1961 else
1962#endif
1963 {
1964 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1965 if( ++ssl->in_ctr[i - 1] != 0 )
1966 break;
1967
1968 /* The loop goes to its end iff the counter is wrapping */
1969 if( i == ssl_ep_len( ssl ) )
1970 {
1971 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1972 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1973 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001974 }
1975
Paul Bakker5121ce52009-01-03 21:22:43 +00001976 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1977
1978 return( 0 );
1979}
1980
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001981#undef MAC_NONE
1982#undef MAC_PLAINTEXT
1983#undef MAC_CIPHERTEXT
1984
Paul Bakker2770fbd2012-07-03 13:30:23 +00001985#if defined(POLARSSL_ZLIB_SUPPORT)
1986/*
1987 * Compression/decompression functions
1988 */
1989static int ssl_compress_buf( ssl_context *ssl )
1990{
1991 int ret;
1992 unsigned char *msg_post = ssl->out_msg;
1993 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001994 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001995
1996 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1997
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001998 if( len_pre == 0 )
1999 return( 0 );
2000
Paul Bakker2770fbd2012-07-03 13:30:23 +00002001 memcpy( msg_pre, ssl->out_msg, len_pre );
2002
2003 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
2004 ssl->out_msglen ) );
2005
2006 SSL_DEBUG_BUF( 4, "before compression: output payload",
2007 ssl->out_msg, ssl->out_msglen );
2008
Paul Bakker48916f92012-09-16 19:57:18 +00002009 ssl->transform_out->ctx_deflate.next_in = msg_pre;
2010 ssl->transform_out->ctx_deflate.avail_in = len_pre;
2011 ssl->transform_out->ctx_deflate.next_out = msg_post;
2012 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002013
Paul Bakker48916f92012-09-16 19:57:18 +00002014 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002015 if( ret != Z_OK )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
2018 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2019 }
2020
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002021 ssl->out_msglen = SSL_BUFFER_LEN -
2022 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002023
Paul Bakker2770fbd2012-07-03 13:30:23 +00002024 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
2025 ssl->out_msglen ) );
2026
2027 SSL_DEBUG_BUF( 4, "after compression: output payload",
2028 ssl->out_msg, ssl->out_msglen );
2029
2030 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
2031
2032 return( 0 );
2033}
2034
2035static int ssl_decompress_buf( ssl_context *ssl )
2036{
2037 int ret;
2038 unsigned char *msg_post = ssl->in_msg;
2039 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02002040 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002041
2042 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
2043
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02002044 if( len_pre == 0 )
2045 return( 0 );
2046
Paul Bakker2770fbd2012-07-03 13:30:23 +00002047 memcpy( msg_pre, ssl->in_msg, len_pre );
2048
2049 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
2050 ssl->in_msglen ) );
2051
2052 SSL_DEBUG_BUF( 4, "before decompression: input payload",
2053 ssl->in_msg, ssl->in_msglen );
2054
Paul Bakker48916f92012-09-16 19:57:18 +00002055 ssl->transform_in->ctx_inflate.next_in = msg_pre;
2056 ssl->transform_in->ctx_inflate.avail_in = len_pre;
2057 ssl->transform_in->ctx_inflate.next_out = msg_post;
2058 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002059
Paul Bakker48916f92012-09-16 19:57:18 +00002060 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002061 if( ret != Z_OK )
2062 {
2063 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
2064 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
2065 }
2066
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002067 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
2068 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00002069
Paul Bakker2770fbd2012-07-03 13:30:23 +00002070 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
2071 ssl->in_msglen ) );
2072
2073 SSL_DEBUG_BUF( 4, "after decompression: input payload",
2074 ssl->in_msg, ssl->in_msglen );
2075
2076 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2077
2078 return( 0 );
2079}
2080#endif /* POLARSSL_ZLIB_SUPPORT */
2081
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002082#if defined(POLARSSL_SSL_SRV_C)
2083static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002084
2085#if defined(POLARSSL_SSL_PROTO_DTLS)
2086static int ssl_resend_hello_request( ssl_context *ssl )
2087{
2088 /* If renegotiation is not enforced, retransmit until we would reach max
2089 * timeout if we were using the usual handshake doubling scheme */
2090 if( ssl->renego_max_records < 0 )
2091 {
2092 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
2093 unsigned char doublings = 1;
2094
2095 while( ratio != 0 )
2096 {
2097 ++doublings;
2098 ratio >>= 1;
2099 }
2100
2101 if( ++ssl->renego_records_seen > doublings )
2102 {
2103 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
2104 return( 0 );
2105 }
2106 }
2107
2108 return( ssl_write_hello_request( ssl ) );
2109}
2110#endif
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002111#endif
2112
Paul Bakker5121ce52009-01-03 21:22:43 +00002113/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002114 * Fill the input message buffer by appending data to it.
2115 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002116 *
2117 * If we return 0, is it guaranteed that (at least) nb_want bytes are
2118 * available (from this read and/or a previous one). Otherwise, an error code
2119 * is returned (possibly EOF or WANT_READ).
2120 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002121 * With stream transport (TLS) on success ssl->in_left == nb_want, but
2122 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
2123 * since we always read a whole datagram at once.
2124 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002125 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002126 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 */
Paul Bakker23986e52011-04-24 08:57:21 +00002128int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129{
Paul Bakker23986e52011-04-24 08:57:21 +00002130 int ret;
2131 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132
2133 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2134
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002135 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
2136 {
2137 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2138 "or ssl_set_bio_timeout()" ) );
2139 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2140 }
2141
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002142 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002143 {
2144 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2145 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2146 }
2147
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002148#if defined(POLARSSL_SSL_PROTO_DTLS)
2149 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002151 uint32_t timeout;
2152
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002153 /*
2154 * The point is, we need to always read a full datagram at once, so we
2155 * sometimes read more then requested, and handle the additional data.
2156 * It could be the rest of the current record (while fetching the
2157 * header) and/or some other records in the same datagram.
2158 */
2159
2160 /*
2161 * Move to the next record in the already read datagram if applicable
2162 */
2163 if( ssl->next_record_offset != 0 )
2164 {
2165 if( ssl->in_left < ssl->next_record_offset )
2166 {
2167 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2168 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2169 }
2170
2171 ssl->in_left -= ssl->next_record_offset;
2172
2173 if( ssl->in_left != 0 )
2174 {
2175 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
2176 ssl->next_record_offset ) );
2177 memmove( ssl->in_hdr,
2178 ssl->in_hdr + ssl->next_record_offset,
2179 ssl->in_left );
2180 }
2181
2182 ssl->next_record_offset = 0;
2183 }
2184
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2186 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002187
2188 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002189 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002190 */
2191 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002192 {
2193 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002194 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002195 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002196
2197 /*
2198 * A record can't be split accross datagrams. If we need to read but
2199 * are not at the beginning of a new record, the caller did something
2200 * wrong.
2201 */
2202 if( ssl->in_left != 0 )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2205 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2206 }
2207
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002208 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002209
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002210 /*
2211 * Don't even try to read if time's out already.
2212 * This avoids by-passing the timer when repeatedly receiving messages
2213 * that will end up being dropped.
2214 */
2215 if( ssl_check_timer( ssl ) != 0 )
2216 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002217 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002218 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002219 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2220
2221 if( ssl->state != SSL_HANDSHAKE_OVER )
2222 timeout = ssl->handshake->retransmit_timeout;
2223 else
2224 timeout = ssl->read_timeout;
2225
2226 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2227
2228 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2229 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2230 timeout );
2231 else
2232 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2233
2234 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2235
2236 if( ret == 0 )
2237 return( POLARSSL_ERR_SSL_CONN_EOF );
2238 }
2239
2240 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2241 {
2242 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2243 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002244
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002245 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002246 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002247 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2248 {
2249 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2250 return( POLARSSL_ERR_NET_TIMEOUT );
2251 }
2252
2253 if( ( ret = ssl_resend( ssl ) ) != 0 )
2254 {
2255 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2256 return( ret );
2257 }
2258
2259 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002260 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002261#if defined(POLARSSL_SSL_SRV_C)
2262 else if( ssl->endpoint == SSL_IS_SERVER &&
2263 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2264 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002265 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002266 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002267 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002268 return( ret );
2269 }
2270
2271 return( POLARSSL_ERR_NET_WANT_READ );
2272 }
2273#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002274 }
2275
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 if( ret < 0 )
2277 return( ret );
2278
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002279 ssl->in_left = ret;
2280 }
2281 else
2282#endif
2283 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002284 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2285 ssl->in_left, nb_want ) );
2286
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002287 while( ssl->in_left < nb_want )
2288 {
2289 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002290 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002291
2292 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2293 ssl->in_left, nb_want ) );
2294 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2295
2296 if( ret == 0 )
2297 return( POLARSSL_ERR_SSL_CONN_EOF );
2298
2299 if( ret < 0 )
2300 return( ret );
2301
2302 ssl->in_left += ret;
2303 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002304 }
2305
2306 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2307
2308 return( 0 );
2309}
2310
2311/*
2312 * Flush any data not yet written
2313 */
2314int ssl_flush_output( ssl_context *ssl )
2315{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002316 int ret;
2317 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002318
2319 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2320
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002321 if( ssl->f_send == NULL )
2322 {
2323 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2324 "or ssl_set_bio_timeout()" ) );
2325 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2326 }
2327
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002328 /* Avoid incrementing counter if data is flushed */
2329 if( ssl->out_left == 0 )
2330 {
2331 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2332 return( 0 );
2333 }
2334
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 while( ssl->out_left > 0 )
2336 {
2337 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002338 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002340 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2341 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002342 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002343
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2345
2346 if( ret <= 0 )
2347 return( ret );
2348
2349 ssl->out_left -= ret;
2350 }
2351
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002352 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002353 if( ++ssl->out_ctr[i - 1] != 0 )
2354 break;
2355
2356 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002357 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002358 {
2359 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2360 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2361 }
2362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2364
2365 return( 0 );
2366}
2367
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002368/*
2369 * Functions to handle the DTLS retransmission state machine
2370 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002371#if defined(POLARSSL_SSL_PROTO_DTLS)
2372/*
2373 * Append current handshake message to current outgoing flight
2374 */
2375static int ssl_flight_append( ssl_context *ssl )
2376{
2377 ssl_flight_item *msg;
2378
2379 /* Allocate space for current message */
2380 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2381 {
2382 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2383 sizeof( ssl_flight_item ) ) );
2384 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2385 }
2386
2387 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2388 {
2389 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002390 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002391 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2392 }
2393
2394 /* Copy current handshake message with headers */
2395 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2396 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002397 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002398 msg->next = NULL;
2399
2400 /* Append to the current flight */
2401 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002402 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002403 else
2404 {
2405 ssl_flight_item *cur = ssl->handshake->flight;
2406 while( cur->next != NULL )
2407 cur = cur->next;
2408 cur->next = msg;
2409 }
2410
2411 return( 0 );
2412}
2413
2414/*
2415 * Free the current flight of handshake messages
2416 */
2417static void ssl_flight_free( ssl_flight_item *flight )
2418{
2419 ssl_flight_item *cur = flight;
2420 ssl_flight_item *next;
2421
2422 while( cur != NULL )
2423 {
2424 next = cur->next;
2425
2426 polarssl_free( cur->p );
2427 polarssl_free( cur );
2428
2429 cur = next;
2430 }
2431}
2432
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002433#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2434static void ssl_dtls_replay_reset( ssl_context *ssl );
2435#endif
2436
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002437/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002438 * Swap transform_out and out_ctr with the alternative ones
2439 */
2440static void ssl_swap_epochs( ssl_context *ssl )
2441{
2442 ssl_transform *tmp_transform;
2443 unsigned char tmp_out_ctr[8];
2444
2445 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2446 {
2447 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2448 return;
2449 }
2450
2451 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2452
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002453 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002454 tmp_transform = ssl->transform_out;
2455 ssl->transform_out = ssl->handshake->alt_transform_out;
2456 ssl->handshake->alt_transform_out = tmp_transform;
2457
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002458 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002459 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2460 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2461 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002462
2463 /* Adjust to the newly activated transform */
2464 if( ssl->transform_out != NULL &&
2465 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2466 {
2467 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2468 ssl->transform_out->fixed_ivlen;
2469 }
2470 else
2471 ssl->out_msg = ssl->out_iv;
2472
2473#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2474 if( ssl_hw_record_activate != NULL )
2475 {
2476 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2477 {
2478 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2479 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2480 }
2481 }
2482#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002483}
2484
2485/*
2486 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002487 *
2488 * Need to remember the current message in case flush_output returns
2489 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002490 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002491 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002492int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002493{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002494 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002495
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002496 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2497 {
2498 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2499
2500 ssl->handshake->cur_msg = ssl->handshake->flight;
2501 ssl_swap_epochs( ssl );
2502
2503 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2504 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002505
2506 while( ssl->handshake->cur_msg != NULL )
2507 {
2508 int ret;
2509 ssl_flight_item *cur = ssl->handshake->cur_msg;
2510
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002511 /* Swap epochs before sending Finished: we can't do it after
2512 * sending ChangeCipherSpec, in case write returns WANT_READ.
2513 * Must be done before copying, may change out_msg pointer */
2514 if( cur->type == SSL_MSG_HANDSHAKE &&
2515 cur->p[0] == SSL_HS_FINISHED )
2516 {
2517 ssl_swap_epochs( ssl );
2518 }
2519
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002520 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002521 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002522 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002523
2524 ssl->handshake->cur_msg = cur->next;
2525
2526 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2527
2528 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2529 {
2530 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2531 return( ret );
2532 }
2533 }
2534
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002535 if( ssl->state == SSL_HANDSHAKE_OVER )
2536 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2537 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002538 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002539 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002540 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2541 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002542
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002543 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002544
2545 return( 0 );
2546}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002547
2548/*
2549 * To be called when the last message of an incoming flight is received.
2550 */
2551void ssl_recv_flight_completed( ssl_context *ssl )
2552{
2553 /* We won't need to resend that one any more */
2554 ssl_flight_free( ssl->handshake->flight );
2555 ssl->handshake->flight = NULL;
2556 ssl->handshake->cur_msg = NULL;
2557
2558 /* The next incoming flight will start with this msg_seq */
2559 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2560
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002561 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002562 ssl_set_timer( ssl, 0 );
2563
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002564 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2565 ssl->in_msg[0] == SSL_HS_FINISHED )
2566 {
2567 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2568 }
2569 else
2570 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2571}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002572
2573/*
2574 * To be called when the last message of an outgoing flight is send.
2575 */
2576void ssl_send_flight_completed( ssl_context *ssl )
2577{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002578 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002579 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002580
2581 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2582 ssl->in_msg[0] == SSL_HS_FINISHED )
2583 {
2584 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2585 }
2586 else
2587 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2588}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002589#endif /* POLARSSL_SSL_PROTO_DTLS */
2590
Paul Bakker5121ce52009-01-03 21:22:43 +00002591/*
2592 * Record layer functions
2593 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002594
2595/*
2596 * Write current record.
2597 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2598 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002599int ssl_write_record( ssl_context *ssl )
2600{
Paul Bakker05ef8352012-05-08 09:17:57 +00002601 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002602 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002603
2604 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2605
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002606#if defined(POLARSSL_SSL_PROTO_DTLS)
2607 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2608 ssl->handshake != NULL &&
2609 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2610 {
2611 ; /* Skip special handshake treatment when resending */
2612 }
2613 else
2614#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2616 {
2617 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2618 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2619 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2620
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002621 /*
2622 * DTLS has additional fields in the Handshake layer,
2623 * between the length field and the actual payload:
2624 * uint16 message_seq;
2625 * uint24 fragment_offset;
2626 * uint24 fragment_length;
2627 */
2628#if defined(POLARSSL_SSL_PROTO_DTLS)
2629 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2630 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002631 /* Make room for the additional DTLS fields */
2632 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002633 ssl->out_msglen += 8;
2634 len += 8;
2635
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002636 /* Write message_seq and update it, except for HelloRequest */
2637 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2638 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002639 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2640 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2641 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002642 }
2643 else
2644 {
2645 ssl->out_msg[4] = 0;
2646 ssl->out_msg[5] = 0;
2647 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002648
2649 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2650 memset( ssl->out_msg + 6, 0x00, 3 );
2651 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002652 }
2653#endif /* POLARSSL_SSL_PROTO_DTLS */
2654
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002655 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2656 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 }
2658
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002659 /* Save handshake and CCS messages for resending */
2660#if defined(POLARSSL_SSL_PROTO_DTLS)
2661 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2662 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002663 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002664 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2665 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2666 {
2667 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2668 {
2669 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2670 return( ret );
2671 }
2672 }
2673#endif
2674
Paul Bakker2770fbd2012-07-03 13:30:23 +00002675#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002676 if( ssl->transform_out != NULL &&
2677 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002678 {
2679 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2680 {
2681 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2682 return( ret );
2683 }
2684
2685 len = ssl->out_msglen;
2686 }
2687#endif /*POLARSSL_ZLIB_SUPPORT */
2688
Paul Bakker05ef8352012-05-08 09:17:57 +00002689#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002690 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002692 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Paul Bakker05ef8352012-05-08 09:17:57 +00002694 ret = ssl_hw_record_write( ssl );
2695 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2696 {
2697 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002698 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002699 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002700
2701 if( ret == 0 )
2702 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002703 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002704#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002705 if( !done )
2706 {
2707 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002708 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2709 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002710
2711 ssl->out_len[0] = (unsigned char)( len >> 8 );
2712 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002713
Paul Bakker48916f92012-09-16 19:57:18 +00002714 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002715 {
2716 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2717 {
2718 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2719 return( ret );
2720 }
2721
2722 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002723 ssl->out_len[0] = (unsigned char)( len >> 8 );
2724 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002725 }
2726
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002727 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002728
2729 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2730 "version = [%d:%d], msglen = %d",
2731 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002732 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002733
2734 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002735 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002736 }
2737
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2739 {
2740 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2741 return( ret );
2742 }
2743
2744 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2745
2746 return( 0 );
2747}
2748
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002749#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002750/*
2751 * Mark bits in bitmask (used for DTLS HS reassembly)
2752 */
2753static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2754{
2755 unsigned int start_bits, end_bits;
2756
2757 start_bits = 8 - ( offset % 8 );
2758 if( start_bits != 8 )
2759 {
2760 size_t first_byte_idx = offset / 8;
2761
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002762 /* Special case */
2763 if( len <= start_bits )
2764 {
2765 for( ; len != 0; len-- )
2766 mask[first_byte_idx] |= 1 << ( start_bits - len );
2767
2768 /* Avoid potential issues with offset or len becoming invalid */
2769 return;
2770 }
2771
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002772 offset += start_bits; /* Now offset % 8 == 0 */
2773 len -= start_bits;
2774
2775 for( ; start_bits != 0; start_bits-- )
2776 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2777 }
2778
2779 end_bits = len % 8;
2780 if( end_bits != 0 )
2781 {
2782 size_t last_byte_idx = ( offset + len ) / 8;
2783
2784 len -= end_bits; /* Now len % 8 == 0 */
2785
2786 for( ; end_bits != 0; end_bits-- )
2787 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2788 }
2789
2790 memset( mask + offset / 8, 0xFF, len / 8 );
2791}
2792
2793/*
2794 * Check that bitmask is full
2795 */
2796static int ssl_bitmask_check( unsigned char *mask, size_t len )
2797{
2798 size_t i;
2799
2800 for( i = 0; i < len / 8; i++ )
2801 if( mask[i] != 0xFF )
2802 return( -1 );
2803
2804 for( i = 0; i < len % 8; i++ )
2805 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2806 return( -1 );
2807
2808 return( 0 );
2809}
2810
2811/*
2812 * Reassemble fragmented DTLS handshake messages.
2813 *
2814 * Use a temporary buffer for reassembly, divided in two parts:
2815 * - the first holds the reassembled message (including handshake header),
2816 * - the second holds a bitmask indicating which parts of the message
2817 * (excluding headers) have been received so far.
2818 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002819static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2820{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002821 unsigned char *msg, *bitmask;
2822 size_t frag_len, frag_off;
2823 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2824
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002825 if( ssl->handshake == NULL )
2826 {
2827 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2828 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2829 }
2830
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002831 /*
2832 * For first fragment, check size and allocate buffer
2833 */
2834 if( ssl->handshake->hs_msg == NULL )
2835 {
2836 size_t alloc_len;
2837
2838 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2839 msg_len ) );
2840
2841 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2842 {
2843 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2844 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2845 }
2846
2847 /* The bitmask needs one bit per byte of message excluding header */
2848 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2849
2850 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2851 if( ssl->handshake->hs_msg == NULL )
2852 {
2853 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2854 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2855 }
2856
2857 memset( ssl->handshake->hs_msg, 0, alloc_len );
2858
2859 /* Prepare final header: copy msg_type, length and message_seq,
2860 * then add standardised fragment_offset and fragment_length */
2861 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2862 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2863 memcpy( ssl->handshake->hs_msg + 9,
2864 ssl->handshake->hs_msg + 1, 3 );
2865 }
2866 else
2867 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002868 /* Make sure msg_type and length are consistent */
2869 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002870 {
2871 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2872 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2873 }
2874 }
2875
2876 msg = ssl->handshake->hs_msg + 12;
2877 bitmask = msg + msg_len;
2878
2879 /*
2880 * Check and copy current fragment
2881 */
2882 frag_off = ( ssl->in_msg[6] << 16 ) |
2883 ( ssl->in_msg[7] << 8 ) |
2884 ssl->in_msg[8];
2885 frag_len = ( ssl->in_msg[9] << 16 ) |
2886 ( ssl->in_msg[10] << 8 ) |
2887 ssl->in_msg[11];
2888
2889 if( frag_off + frag_len > msg_len )
2890 {
2891 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2892 frag_off, frag_len, msg_len ) );
2893 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2894 }
2895
2896 if( frag_len + 12 > ssl->in_msglen )
2897 {
2898 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2899 frag_len, ssl->in_msglen ) );
2900 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2901 }
2902
2903 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2904 frag_off, frag_len ) );
2905
2906 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2907 ssl_bitmask_set( bitmask, frag_off, frag_len );
2908
2909 /*
2910 * Do we have the complete message by now?
2911 * If yes, finalize it, else ask to read the next record.
2912 */
2913 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2914 {
2915 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2916 return( POLARSSL_ERR_NET_WANT_READ );
2917 }
2918
2919 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2920
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002921 if( frag_len + 12 < ssl->in_msglen )
2922 {
2923 /*
2924 * We'got more handshake messages in the same record.
2925 * This case is not handled now because no know implementation does
2926 * that and it's hard to test, so we prefer to fail cleanly for now.
2927 */
2928 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2929 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2930 }
2931
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002932 if( ssl->in_left > ssl->next_record_offset )
2933 {
2934 /*
2935 * We've got more data in the buffer after the current record,
2936 * that we don't want to overwrite. Move it before writing the
2937 * reassembled message, and adjust in_left and next_record_offset.
2938 */
2939 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2940 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2941 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2942
2943 /* First compute and check new lengths */
2944 ssl->next_record_offset = new_remain - ssl->in_hdr;
2945 ssl->in_left = ssl->next_record_offset + remain_len;
2946
2947 if( ssl->in_left > SSL_BUFFER_LEN -
2948 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2949 {
2950 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2951 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2952 }
2953
2954 memmove( new_remain, cur_remain, remain_len );
2955 }
2956
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002957 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2958
2959 polarssl_free( ssl->handshake->hs_msg );
2960 ssl->handshake->hs_msg = NULL;
2961
2962 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2963 ssl->in_msg, ssl->in_hslen );
2964
2965 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002966}
2967#endif /* POLARSSL_SSL_PROTO_DTLS */
2968
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002969static int ssl_prepare_handshake_record( ssl_context *ssl )
2970{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002971 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2972 {
2973 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2974 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002975 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002976 }
2977
2978 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2979 ( ssl->in_msg[1] << 16 ) |
2980 ( ssl->in_msg[2] << 8 ) |
2981 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002982
2983 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2984 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002985 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002986
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002987#if defined(POLARSSL_SSL_PROTO_DTLS)
2988 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002989 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002990 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002991 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002992
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002993 /* ssl->handshake is NULL when receiving ClientHello for renego */
2994 if( ssl->handshake != NULL &&
2995 recv_msg_seq != ssl->handshake->in_msg_seq )
2996 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002997 /* Retransmit only on last message from previous flight, to avoid
2998 * too many retransmissions.
2999 * Besides, No sane server ever retransmits HelloVerifyRequest */
3000 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02003001 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003002 {
3003 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
3004 "message_seq = %d, start_of_flight = %d",
3005 recv_msg_seq,
3006 ssl->handshake->in_flight_start_seq ) );
3007
3008 if( ( ret = ssl_resend( ssl ) ) != 0 )
3009 {
3010 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3011 return( ret );
3012 }
3013 }
3014 else
3015 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003016 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003017 "message_seq = %d, expected = %d",
3018 recv_msg_seq,
3019 ssl->handshake->in_msg_seq ) );
3020 }
3021
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003022 return( POLARSSL_ERR_NET_WANT_READ );
3023 }
3024 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003025
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003026 /* Reassemble if current message is fragmented or reassembly is
3027 * already in progress */
3028 if( ssl->in_msglen < ssl->in_hslen ||
3029 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
3030 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
3031 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003032 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003033 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
3034
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02003035 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
3036 {
3037 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
3038 return( ret );
3039 }
3040 }
3041 }
3042 else
3043#endif /* POLARSSL_SSL_PROTO_DTLS */
3044 /* With TLS we don't handle fragmentation (for now) */
3045 if( ssl->in_msglen < ssl->in_hslen )
3046 {
3047 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02003048 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003049 }
3050
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003051 if( ssl->state != SSL_HANDSHAKE_OVER )
3052 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
3053
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003054 /* Handshake message is complete, increment counter */
3055#if defined(POLARSSL_SSL_PROTO_DTLS)
3056 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3057 ssl->handshake != NULL )
3058 {
3059 ssl->handshake->in_msg_seq++;
3060 }
3061#endif
3062
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003063 return( 0 );
3064}
3065
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003066/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003067 * DTLS anti-replay: RFC 6347 4.1.2.6
3068 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003069 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
3070 * Bit n is set iff record number in_window_top - n has been seen.
3071 *
3072 * Usually, in_window_top is the last record number seen and the lsb of
3073 * in_window is set. The only exception is the initial state (record number 0
3074 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003075 */
3076#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3077static void ssl_dtls_replay_reset( ssl_context *ssl )
3078{
3079 ssl->in_window_top = 0;
3080 ssl->in_window = 0;
3081}
3082
3083static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
3084{
3085 return( ( (uint64_t) buf[0] << 40 ) |
3086 ( (uint64_t) buf[1] << 32 ) |
3087 ( (uint64_t) buf[2] << 24 ) |
3088 ( (uint64_t) buf[3] << 16 ) |
3089 ( (uint64_t) buf[4] << 8 ) |
3090 ( (uint64_t) buf[5] ) );
3091}
3092
3093/*
3094 * Return 0 if sequence number is acceptable, -1 otherwise
3095 */
3096int ssl_dtls_replay_check( ssl_context *ssl )
3097{
3098 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3099 uint64_t bit;
3100
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003101 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3102 return( 0 );
3103
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003104 if( rec_seqnum > ssl->in_window_top )
3105 return( 0 );
3106
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003107 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003108
3109 if( bit >= 64 )
3110 return( -1 );
3111
3112 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
3113 return( -1 );
3114
3115 return( 0 );
3116}
3117
3118/*
3119 * Update replay window on new validated record
3120 */
3121void ssl_dtls_replay_update( ssl_context *ssl )
3122{
3123 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
3124
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02003125 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
3126 return;
3127
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003128 if( rec_seqnum > ssl->in_window_top )
3129 {
3130 /* Update window_top and the contents of the window */
3131 uint64_t shift = rec_seqnum - ssl->in_window_top;
3132
3133 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003134 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003135 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003136 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003137 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003138 ssl->in_window |= 1;
3139 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003140
3141 ssl->in_window_top = rec_seqnum;
3142 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003143 else
3144 {
3145 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003146 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02003147
3148 if( bit < 64 ) /* Always true, but be extra sure */
3149 ssl->in_window |= (uint64_t) 1 << bit;
3150 }
3151}
3152#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
3153
3154/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003155 * ContentType type;
3156 * ProtocolVersion version;
3157 * uint16 epoch; // DTLS only
3158 * uint48 sequence_number; // DTLS only
3159 * uint16 length;
3160 */
3161static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003162{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003163 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003164 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00003165
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003166 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
3167
Paul Bakker5121ce52009-01-03 21:22:43 +00003168 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003169 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003170 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003171
3172 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
3173 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003174 ssl->in_msgtype,
3175 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003176
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003177 /* Check record type */
3178 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
3179 ssl->in_msgtype != SSL_MSG_ALERT &&
3180 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
3181 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
3182 {
3183 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003184
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003185 if( ( ret = ssl_send_alert_message( ssl,
3186 SSL_ALERT_LEVEL_FATAL,
3187 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
3188 {
3189 return( ret );
3190 }
3191
3192 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3193 }
3194
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003195#if defined(POLARSSL_SSL_PROTO_DTLS)
3196 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3197 {
3198 /* Drop unexpected ChangeCipherSpec messages */
3199 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3200 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3201 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3202 {
3203 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3204 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3205 }
3206
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003207 /* Drop unexpected ApplicationData records,
3208 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003209 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003210 ssl->state != SSL_HANDSHAKE_OVER &&
3211 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
3212 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003213 {
3214 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3215 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3216 }
3217 }
3218#endif
3219
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003220 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003221 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 {
3223 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003224 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225 }
3226
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003227 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 {
3229 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003230 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 }
3232
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003233 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003234#if defined(POLARSSL_SSL_PROTO_DTLS)
3235 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3236 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003237 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003238
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003239 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003240 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003241 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003242 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003243 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003244 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003245 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003246
3247#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3248 if( ssl_dtls_replay_check( ssl ) != 0 )
3249 {
3250 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3251 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3252 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003253#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003254 }
3255#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003256
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003257 /* Check length against the size of our buffer */
3258 if( ssl->in_msglen > SSL_BUFFER_LEN
3259 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003260 {
3261 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3262 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3263 }
3264
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003265 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003266 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003268 if( ssl->in_msglen < 1 ||
3269 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 {
3271 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003272 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 }
3274 }
3275 else
3276 {
Paul Bakker48916f92012-09-16 19:57:18 +00003277 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 {
3279 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003280 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 }
3282
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003283#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003285 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003286 {
3287 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003290#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003291#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3292 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003293 /*
3294 * TLS encrypted messages can have up to 256 bytes of padding
3295 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003296 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003297 ssl->in_msglen > ssl->transform_in->minlen +
3298 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 {
3300 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003301 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003303#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003304 }
3305
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003306 return( 0 );
3307}
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003309/*
3310 * If applicable, decrypt (and decompress) record content
3311 */
3312static int ssl_prepare_record_content( ssl_context *ssl )
3313{
3314 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003315
Paul Bakker5121ce52009-01-03 21:22:43 +00003316 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003317 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003318
Paul Bakker05ef8352012-05-08 09:17:57 +00003319#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003320 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003321 {
3322 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3323
3324 ret = ssl_hw_record_read( ssl );
3325 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3326 {
3327 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003328 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003329 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003330
3331 if( ret == 0 )
3332 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003333 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003334#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003335 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 {
3337 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3338 {
3339 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3340 return( ret );
3341 }
3342
3343 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3344 ssl->in_msg, ssl->in_msglen );
3345
3346 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3347 {
3348 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003349 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 }
3351 }
3352
Paul Bakker2770fbd2012-07-03 13:30:23 +00003353#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003354 if( ssl->transform_in != NULL &&
3355 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003356 {
3357 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3358 {
3359 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3360 return( ret );
3361 }
3362
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003363 // TODO: what's the purpose of these lines? is in_len used?
3364 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3365 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003366 }
3367#endif /* POLARSSL_ZLIB_SUPPORT */
3368
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003369#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003370 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3371 {
3372 ssl_dtls_replay_update( ssl );
3373 }
3374#endif
3375
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003376 return( 0 );
3377}
3378
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003379static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3380
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003381/*
3382 * Read a record.
3383 *
3384 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3385 * and continue reading until a valid record is found.
3386 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003387int ssl_read_record( ssl_context *ssl )
3388{
3389 int ret;
3390
3391 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3392
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003393 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003394 {
3395 /*
3396 * Get next Handshake message in the current record
3397 */
3398 ssl->in_msglen -= ssl->in_hslen;
3399
3400 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3401 ssl->in_msglen );
3402
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003403 SSL_DEBUG_BUF( 4, "remaining content in record",
3404 ssl->in_msg, ssl->in_msglen );
3405
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003406 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3407 return( ret );
3408
3409 return( 0 );
3410 }
3411
3412 ssl->in_hslen = 0;
3413
3414 /*
3415 * Read the record header and parse it
3416 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003417#if defined(POLARSSL_SSL_PROTO_DTLS)
3418read_record_header:
3419#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003420 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3421 {
3422 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3423 return( ret );
3424 }
3425
3426 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003427 {
3428#if defined(POLARSSL_SSL_PROTO_DTLS)
3429 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3430 {
3431 /* Ignore bad record and get next one; drop the whole datagram
3432 * since current header cannot be trusted to find the next record
3433 * in current datagram */
3434 ssl->next_record_offset = 0;
3435 ssl->in_left = 0;
3436
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003437 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003438 goto read_record_header;
3439 }
3440#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003441 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003442 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003443
3444 /*
3445 * Read and optionally decrypt the message contents
3446 */
3447 if( ( ret = ssl_fetch_input( ssl,
3448 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3449 {
3450 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3451 return( ret );
3452 }
3453
3454 /* Done reading this record, get ready for the next one */
3455#if defined(POLARSSL_SSL_PROTO_DTLS)
3456 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3457 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3458 else
3459#endif
3460 ssl->in_left = 0;
3461
3462 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003463 {
3464#if defined(POLARSSL_SSL_PROTO_DTLS)
3465 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3466 {
3467 /* Silently discard invalid records */
3468 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3469 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3470 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003471#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3472 if( ssl->badmac_limit != 0 &&
3473 ++ssl->badmac_seen >= ssl->badmac_limit )
3474 {
3475 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3476 return( POLARSSL_ERR_SSL_INVALID_MAC );
3477 }
3478#endif
3479
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003480 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003481 goto read_record_header;
3482 }
3483
3484 return( ret );
3485 }
3486 else
3487#endif
3488 {
3489 /* Error out (and send alert) on invalid records */
3490#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3491 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3492 {
3493 ssl_send_alert_message( ssl,
3494 SSL_ALERT_LEVEL_FATAL,
3495 SSL_ALERT_MSG_BAD_RECORD_MAC );
3496 }
3497#endif
3498 return( ret );
3499 }
3500 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003501
3502 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003503 * When we sent the last flight of the handshake, we MUST respond to a
3504 * retransmit of the peer's previous flight with a retransmit. (In
3505 * practice, only the Finished message will make it, other messages
3506 * including CCS use the old transform so they're dropped as invalid.)
3507 *
3508 * If the record we received is not a handshake message, however, it
3509 * means the peer received our last flight so we can clean up
3510 * handshake info.
3511 *
3512 * This check needs to be done before prepare_handshake() due to an edge
3513 * case: if the client immediately requests renegotiation, this
3514 * finishes the current handshake first, avoiding the new ClientHello
3515 * being mistaken for an ancient message in the current handshake.
3516 */
3517#if defined(POLARSSL_SSL_PROTO_DTLS)
3518 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3519 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003520 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003521 {
3522 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3523 ssl->in_msg[0] == SSL_HS_FINISHED )
3524 {
3525 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3526
3527 if( ( ret = ssl_resend( ssl ) ) != 0 )
3528 {
3529 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3530 return( ret );
3531 }
3532
3533 return( POLARSSL_ERR_NET_WANT_READ );
3534 }
3535 else
3536 {
3537 ssl_handshake_wrapup_free_hs_transform( ssl );
3538 }
3539 }
3540#endif
3541
3542 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003543 * Handle particular types of records
3544 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3546 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003547 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3548 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 }
3550
3551 if( ssl->in_msgtype == SSL_MSG_ALERT )
3552 {
3553 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3554 ssl->in_msg[0], ssl->in_msg[1] ) );
3555
3556 /*
3557 * Ignore non-fatal alerts, except close_notify
3558 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003559 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003560 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3562 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003563 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003564 }
3565
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003566 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3567 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003568 {
3569 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003570 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 }
3572 }
3573
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3575
3576 return( 0 );
3577}
3578
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003579int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3580{
3581 int ret;
3582
3583 if( ( ret = ssl_send_alert_message( ssl,
3584 SSL_ALERT_LEVEL_FATAL,
3585 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3586 {
3587 return( ret );
3588 }
3589
3590 return( 0 );
3591}
3592
Paul Bakker0a925182012-04-16 06:46:41 +00003593int ssl_send_alert_message( ssl_context *ssl,
3594 unsigned char level,
3595 unsigned char message )
3596{
3597 int ret;
3598
3599 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3600
3601 ssl->out_msgtype = SSL_MSG_ALERT;
3602 ssl->out_msglen = 2;
3603 ssl->out_msg[0] = level;
3604 ssl->out_msg[1] = message;
3605
3606 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3607 {
3608 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3609 return( ret );
3610 }
3611
3612 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3613
3614 return( 0 );
3615}
3616
Paul Bakker5121ce52009-01-03 21:22:43 +00003617/*
3618 * Handshake functions
3619 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003620#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3621 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3622 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3623 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3624 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3625 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3626 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003627int ssl_write_certificate( ssl_context *ssl )
3628{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003629 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003630
3631 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3632
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003633 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003634 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3635 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003636 {
3637 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3638 ssl->state++;
3639 return( 0 );
3640 }
3641
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003642 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3643 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003644}
3645
3646int ssl_parse_certificate( ssl_context *ssl )
3647{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003648 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3649
3650 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3651
3652 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003653 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3654 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003655 {
3656 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3657 ssl->state++;
3658 return( 0 );
3659 }
3660
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3662 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003663}
3664#else
3665int ssl_write_certificate( ssl_context *ssl )
3666{
3667 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3668 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003669 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003670 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3671
3672 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3673
3674 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003675 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3676 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003677 {
3678 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3679 ssl->state++;
3680 return( 0 );
3681 }
3682
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003683#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 if( ssl->endpoint == SSL_IS_CLIENT )
3685 {
3686 if( ssl->client_auth == 0 )
3687 {
3688 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3689 ssl->state++;
3690 return( 0 );
3691 }
3692
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003693#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003694 /*
3695 * If using SSLv3 and got no cert, send an Alert message
3696 * (otherwise an empty Certificate message will be sent).
3697 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003698 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3700 {
3701 ssl->out_msglen = 2;
3702 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003703 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3704 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003705
3706 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3707 goto write_msg;
3708 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003709#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003711#endif /* POLARSSL_SSL_CLI_C */
3712#if defined(POLARSSL_SSL_SRV_C)
3713 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003715 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 {
3717 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003718 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003719 }
3720 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003721#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003723 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003724
3725 /*
3726 * 0 . 0 handshake type
3727 * 1 . 3 handshake length
3728 * 4 . 6 length of all certs
3729 * 7 . 9 length of cert. 1
3730 * 10 . n-1 peer certificate
3731 * n . n+2 length of cert. 2
3732 * n+3 . ... upper level cert, etc.
3733 */
3734 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003735 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003736
Paul Bakker29087132010-03-21 21:03:34 +00003737 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 {
3739 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003740 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 {
3742 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3743 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003744 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003745 }
3746
3747 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3748 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3749 ssl->out_msg[i + 2] = (unsigned char)( n );
3750
3751 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3752 i += n; crt = crt->next;
3753 }
3754
3755 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3756 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3757 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3758
3759 ssl->out_msglen = i;
3760 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3761 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3762
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003763#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003764write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003765#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003766
3767 ssl->state++;
3768
3769 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3770 {
3771 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3772 return( ret );
3773 }
3774
3775 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3776
Paul Bakkered27a042013-04-18 22:46:23 +02003777 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778}
3779
3780int ssl_parse_certificate( ssl_context *ssl )
3781{
Paul Bakkered27a042013-04-18 22:46:23 +02003782 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003783 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003784 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003785
3786 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3787
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003788 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003789 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3790 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003791 {
3792 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3793 ssl->state++;
3794 return( 0 );
3795 }
3796
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003797#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003798 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003799 ( ssl->authmode == SSL_VERIFY_NONE ||
3800 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003802 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003803 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3804 ssl->state++;
3805 return( 0 );
3806 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003808
3809 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3810 {
3811 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3812 return( ret );
3813 }
3814
3815 ssl->state++;
3816
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003817#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003818#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003819 /*
3820 * Check if the client sent an empty certificate
3821 */
3822 if( ssl->endpoint == SSL_IS_SERVER &&
3823 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3824 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003825 if( ssl->in_msglen == 2 &&
3826 ssl->in_msgtype == SSL_MSG_ALERT &&
3827 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3828 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003829 {
3830 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3831
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003832 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003833 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3834 return( 0 );
3835 else
Paul Bakker40e46942009-01-03 21:51:57 +00003836 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003837 }
3838 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003839#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003840
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003841#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3842 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003843 if( ssl->endpoint == SSL_IS_SERVER &&
3844 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3845 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003846 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003847 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3848 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003849 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 {
3851 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3852
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003853 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003854 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003855 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 else
3857 return( 0 );
3858 }
3859 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003860#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3861 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003862#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003863
3864 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3865 {
3866 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003867 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 }
3869
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003870 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3871 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003872 {
3873 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003874 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 }
3876
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003877 i = ssl_hs_hdr_len( ssl );
3878
Paul Bakker5121ce52009-01-03 21:22:43 +00003879 /*
3880 * Same message structure as in ssl_write_certificate()
3881 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003882 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003883
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003884 if( ssl->in_msg[i] != 0 ||
3885 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 {
3887 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003888 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003889 }
3890
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003891 /* In case we tried to reuse a session but it failed */
3892 if( ssl->session_negotiate->peer_cert != NULL )
3893 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003894 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003895 polarssl_free( ssl->session_negotiate->peer_cert );
3896 }
3897
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003898 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3899 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 {
3901 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003902 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003903 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 }
3905
Paul Bakkerb6b09562013-09-18 14:17:41 +02003906 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003908 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003909
3910 while( i < ssl->in_hslen )
3911 {
3912 if( ssl->in_msg[i] != 0 )
3913 {
3914 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003915 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003916 }
3917
3918 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3919 | (unsigned int) ssl->in_msg[i + 2];
3920 i += 3;
3921
3922 if( n < 128 || i + n > ssl->in_hslen )
3923 {
3924 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003925 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003926 }
3927
Paul Bakkerddf26b42013-09-18 13:46:23 +02003928 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3929 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003930 if( ret != 0 )
3931 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003932 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 return( ret );
3934 }
3935
3936 i += n;
3937 }
3938
Paul Bakker48916f92012-09-16 19:57:18 +00003939 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003941 /*
3942 * On client, make sure the server cert doesn't change during renego to
3943 * avoid "triple handshake" attack: https://secure-resumption.com/
3944 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01003945#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003946 if( ssl->endpoint == SSL_IS_CLIENT &&
3947 ssl->renegotiation == SSL_RENEGOTIATION )
3948 {
3949 if( ssl->session->peer_cert == NULL )
3950 {
3951 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3952 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3953 }
3954
3955 if( ssl->session->peer_cert->raw.len !=
3956 ssl->session_negotiate->peer_cert->raw.len ||
3957 memcmp( ssl->session->peer_cert->raw.p,
3958 ssl->session_negotiate->peer_cert->raw.p,
3959 ssl->session->peer_cert->raw.len ) != 0 )
3960 {
3961 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3962 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3963 }
3964 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01003965#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003966
Paul Bakker5121ce52009-01-03 21:22:43 +00003967 if( ssl->authmode != SSL_VERIFY_NONE )
3968 {
3969 if( ssl->ca_chain == NULL )
3970 {
3971 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003972 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003973 }
3974
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003975 /*
3976 * Main check: verify certificate
3977 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003978 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3979 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3980 &ssl->session_negotiate->verify_result,
3981 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982
3983 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003984 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003985 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003986 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003987
3988 /*
3989 * Secondary checks: always done, but change 'ret' only if it was 0
3990 */
3991
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003992#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003993 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003994 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003995
3996 /* If certificate uses an EC key, make sure the curve is OK */
3997 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3998 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3999 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004000 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004001 if( ret == 0 )
4002 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004003 }
4004 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004005#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004007 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
4008 ciphersuite_info,
4009 ! ssl->endpoint ) != 0 )
4010 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02004011 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004012 if( ret == 0 )
4013 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
4014 }
4015
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 if( ssl->authmode != SSL_VERIFY_REQUIRED )
4017 ret = 0;
4018 }
4019
4020 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
4021
4022 return( ret );
4023}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01004024#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
4025 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
4026 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
4027 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
4028 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
4029 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
4030 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00004031
4032int ssl_write_change_cipher_spec( ssl_context *ssl )
4033{
4034 int ret;
4035
4036 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
4037
4038 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
4039 ssl->out_msglen = 1;
4040 ssl->out_msg[0] = 1;
4041
Paul Bakker5121ce52009-01-03 21:22:43 +00004042 ssl->state++;
4043
4044 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4045 {
4046 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4047 return( ret );
4048 }
4049
4050 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
4051
4052 return( 0 );
4053}
4054
4055int ssl_parse_change_cipher_spec( ssl_context *ssl )
4056{
4057 int ret;
4058
4059 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
4060
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4062 {
4063 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4064 return( ret );
4065 }
4066
4067 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
4068 {
4069 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004070 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004071 }
4072
4073 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
4074 {
4075 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004076 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00004077 }
4078
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004079 /*
4080 * Switch to our negotiated transform and session parameters for inbound
4081 * data.
4082 */
4083 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
4084 ssl->transform_in = ssl->transform_negotiate;
4085 ssl->session_in = ssl->session_negotiate;
4086
4087#if defined(POLARSSL_SSL_PROTO_DTLS)
4088 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4089 {
4090#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4091 ssl_dtls_replay_reset( ssl );
4092#endif
4093
4094 /* Increment epoch */
4095 if( ++ssl->in_epoch == 0 )
4096 {
4097 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4098 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4099 }
4100 }
4101 else
4102#endif /* POLARSSL_SSL_PROTO_DTLS */
4103 memset( ssl->in_ctr, 0, 8 );
4104
4105 /*
4106 * Set the in_msg pointer to the correct location based on IV length
4107 */
4108 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4109 {
4110 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
4111 ssl->transform_negotiate->fixed_ivlen;
4112 }
4113 else
4114 ssl->in_msg = ssl->in_iv;
4115
4116#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4117 if( ssl_hw_record_activate != NULL )
4118 {
4119 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
4120 {
4121 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4122 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4123 }
4124 }
4125#endif
4126
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 ssl->state++;
4128
4129 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
4130
4131 return( 0 );
4132}
4133
Paul Bakker41c83d32013-03-20 14:39:14 +01004134void ssl_optimize_checksum( ssl_context *ssl,
4135 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00004136{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02004137 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01004138
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004139#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4140 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00004141 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00004142 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00004143 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004144#endif
4145#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4146#if defined(POLARSSL_SHA512_C)
4147 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
4148 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
4149 else
4150#endif
4151#if defined(POLARSSL_SHA256_C)
4152 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00004153 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004154 else
4155#endif
4156#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004157 {
4158 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004159 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004160 }
Paul Bakker380da532012-04-18 16:10:25 +00004161}
Paul Bakkerf7abd422013-04-16 13:15:56 +02004162
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02004163void ssl_reset_checksum( ssl_context *ssl )
4164{
4165#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4166 defined(POLARSSL_SSL_PROTO_TLS1_1)
4167 md5_starts( &ssl->handshake->fin_md5 );
4168 sha1_starts( &ssl->handshake->fin_sha1 );
4169#endif
4170#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4171#if defined(POLARSSL_SHA256_C)
4172 sha256_starts( &ssl->handshake->fin_sha256, 0 );
4173#endif
4174#if defined(POLARSSL_SHA512_C)
4175 sha512_starts( &ssl->handshake->fin_sha512, 1 );
4176#endif
4177#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4178}
4179
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004180static void ssl_update_checksum_start( ssl_context *ssl,
4181 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004182{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004183#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4184 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00004185 md5_update( &ssl->handshake->fin_md5 , buf, len );
4186 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004187#endif
4188#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4189#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02004190 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004191#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02004192#if defined(POLARSSL_SHA512_C)
4193 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01004194#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004195#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004196}
4197
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004198#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4199 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004200static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4201 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004202{
Paul Bakker48916f92012-09-16 19:57:18 +00004203 md5_update( &ssl->handshake->fin_md5 , buf, len );
4204 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004206#endif
Paul Bakker380da532012-04-18 16:10:25 +00004207
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004208#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4209#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004210static void ssl_update_checksum_sha256( ssl_context *ssl,
4211 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004212{
Paul Bakker9e36f042013-06-30 14:34:05 +02004213 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004214}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004215#endif
Paul Bakker380da532012-04-18 16:10:25 +00004216
Paul Bakker9e36f042013-06-30 14:34:05 +02004217#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004218static void ssl_update_checksum_sha384( ssl_context *ssl,
4219 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004220{
Paul Bakker9e36f042013-06-30 14:34:05 +02004221 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004222}
Paul Bakker769075d2012-11-24 11:26:46 +01004223#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004224#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004225
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004226#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004227static void ssl_calc_finished_ssl(
4228 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004229{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004230 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004231 md5_context md5;
4232 sha1_context sha1;
4233
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 unsigned char padbuf[48];
4235 unsigned char md5sum[16];
4236 unsigned char sha1sum[20];
4237
Paul Bakker48916f92012-09-16 19:57:18 +00004238 ssl_session *session = ssl->session_negotiate;
4239 if( !session )
4240 session = ssl->session;
4241
Paul Bakker1ef83d62012-04-11 12:09:53 +00004242 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4243
Paul Bakker48916f92012-09-16 19:57:18 +00004244 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4245 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004246
4247 /*
4248 * SSLv3:
4249 * hash =
4250 * MD5( master + pad2 +
4251 * MD5( handshake + sender + master + pad1 ) )
4252 * + SHA1( master + pad2 +
4253 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004254 */
4255
Paul Bakker90995b52013-06-24 19:20:35 +02004256#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004258 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004259#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004260
Paul Bakker90995b52013-06-24 19:20:35 +02004261#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004262 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004263 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004264#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004265
Paul Bakker3c2122f2013-06-24 19:03:14 +02004266 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4267 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004268
Paul Bakker1ef83d62012-04-11 12:09:53 +00004269 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004270
Paul Bakker3c2122f2013-06-24 19:03:14 +02004271 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004272 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004273 md5_update( &md5, padbuf, 48 );
4274 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004275
Paul Bakker3c2122f2013-06-24 19:03:14 +02004276 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004277 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004278 sha1_update( &sha1, padbuf, 40 );
4279 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004280
Paul Bakker1ef83d62012-04-11 12:09:53 +00004281 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004282
Paul Bakker1ef83d62012-04-11 12:09:53 +00004283 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004284 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004285 md5_update( &md5, padbuf, 48 );
4286 md5_update( &md5, md5sum, 16 );
4287 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004288
Paul Bakker1ef83d62012-04-11 12:09:53 +00004289 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004290 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004291 sha1_update( &sha1, padbuf , 40 );
4292 sha1_update( &sha1, sha1sum, 20 );
4293 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
Paul Bakker1ef83d62012-04-11 12:09:53 +00004295 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004296
Paul Bakker5b4af392014-06-26 12:09:34 +02004297 md5_free( &md5 );
4298 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Paul Bakker34617722014-06-13 17:20:13 +02004300 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4301 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4302 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004303
4304 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4305}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004306#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004307
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004308#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004309static void ssl_calc_finished_tls(
4310 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004312 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004313 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004314 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004316 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004317
Paul Bakker48916f92012-09-16 19:57:18 +00004318 ssl_session *session = ssl->session_negotiate;
4319 if( !session )
4320 session = ssl->session;
4321
Paul Bakker1ef83d62012-04-11 12:09:53 +00004322 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004323
Paul Bakker48916f92012-09-16 19:57:18 +00004324 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4325 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004326
Paul Bakker1ef83d62012-04-11 12:09:53 +00004327 /*
4328 * TLSv1:
4329 * hash = PRF( master, finished_label,
4330 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4331 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004332
Paul Bakker90995b52013-06-24 19:20:35 +02004333#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004334 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4335 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004336#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004337
Paul Bakker90995b52013-06-24 19:20:35 +02004338#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004339 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4340 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004341#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004342
4343 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004344 ? "client finished"
4345 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004346
4347 md5_finish( &md5, padbuf );
4348 sha1_finish( &sha1, padbuf + 16 );
4349
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004350 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004351 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004352
4353 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4354
Paul Bakker5b4af392014-06-26 12:09:34 +02004355 md5_free( &md5 );
4356 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004357
Paul Bakker34617722014-06-13 17:20:13 +02004358 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004359
4360 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4361}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004362#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004363
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004364#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4365#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004366static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004367 ssl_context *ssl, unsigned char *buf, int from )
4368{
4369 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004370 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004371 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004372 unsigned char padbuf[32];
4373
Paul Bakker48916f92012-09-16 19:57:18 +00004374 ssl_session *session = ssl->session_negotiate;
4375 if( !session )
4376 session = ssl->session;
4377
Paul Bakker380da532012-04-18 16:10:25 +00004378 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004379
Paul Bakker9e36f042013-06-30 14:34:05 +02004380 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004381
4382 /*
4383 * TLSv1.2:
4384 * hash = PRF( master, finished_label,
4385 * Hash( handshake ) )[0.11]
4386 */
4387
Paul Bakker9e36f042013-06-30 14:34:05 +02004388#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004389 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004390 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004391#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004392
4393 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004394 ? "client finished"
4395 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004396
Paul Bakker9e36f042013-06-30 14:34:05 +02004397 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004398
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004399 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004400 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004401
4402 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4403
Paul Bakker5b4af392014-06-26 12:09:34 +02004404 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004405
Paul Bakker34617722014-06-13 17:20:13 +02004406 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004407
4408 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4409}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004410#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004411
Paul Bakker9e36f042013-06-30 14:34:05 +02004412#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004413static void ssl_calc_finished_tls_sha384(
4414 ssl_context *ssl, unsigned char *buf, int from )
4415{
4416 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004417 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004418 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004419 unsigned char padbuf[48];
4420
Paul Bakker48916f92012-09-16 19:57:18 +00004421 ssl_session *session = ssl->session_negotiate;
4422 if( !session )
4423 session = ssl->session;
4424
Paul Bakker380da532012-04-18 16:10:25 +00004425 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004426
Paul Bakker9e36f042013-06-30 14:34:05 +02004427 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004428
4429 /*
4430 * TLSv1.2:
4431 * hash = PRF( master, finished_label,
4432 * Hash( handshake ) )[0.11]
4433 */
4434
Paul Bakker9e36f042013-06-30 14:34:05 +02004435#if !defined(POLARSSL_SHA512_ALT)
4436 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4437 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004438#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004439
4440 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004441 ? "client finished"
4442 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004443
Paul Bakker9e36f042013-06-30 14:34:05 +02004444 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004445
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004446 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004447 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004448
4449 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4450
Paul Bakker5b4af392014-06-26 12:09:34 +02004451 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004452
Paul Bakker34617722014-06-13 17:20:13 +02004453 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004454
4455 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4456}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004457#endif /* POLARSSL_SHA512_C */
4458#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004459
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004460static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004461{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004462 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004463
4464 /*
4465 * Free our handshake params
4466 */
4467 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004468 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004469 ssl->handshake = NULL;
4470
4471 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004472 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004473 */
4474 if( ssl->transform )
4475 {
4476 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004477 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004478 }
4479 ssl->transform = ssl->transform_negotiate;
4480 ssl->transform_negotiate = NULL;
4481
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004482 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4483}
4484
4485void ssl_handshake_wrapup( ssl_context *ssl )
4486{
4487 int resume = ssl->handshake->resume;
4488
4489 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4490
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004491#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004492 if( ssl->renegotiation == SSL_RENEGOTIATION )
4493 {
4494 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4495 ssl->renego_records_seen = 0;
4496 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004497#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004498
4499 /*
4500 * Free the previous session and switch in the current one
4501 */
Paul Bakker0a597072012-09-25 21:55:46 +00004502 if( ssl->session )
4503 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01004504#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4505 /* RFC 7366 3.1: keep the EtM state */
4506 ssl->session_negotiate->encrypt_then_mac =
4507 ssl->session->encrypt_then_mac;
4508#endif
4509
Paul Bakker0a597072012-09-25 21:55:46 +00004510 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004511 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004512 }
4513 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004514 ssl->session_negotiate = NULL;
4515
Paul Bakker0a597072012-09-25 21:55:46 +00004516 /*
4517 * Add cache entry
4518 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004519 if( ssl->f_set_cache != NULL &&
4520 ssl->session->length != 0 &&
4521 resume == 0 )
4522 {
Paul Bakker0a597072012-09-25 21:55:46 +00004523 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4524 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004525 }
Paul Bakker0a597072012-09-25 21:55:46 +00004526
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004527#if defined(POLARSSL_SSL_PROTO_DTLS)
4528 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4529 ssl->handshake->flight != NULL )
4530 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004531 /* Cancel handshake timer */
4532 ssl_set_timer( ssl, 0 );
4533
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004534 /* Keep last flight around in case we need to resend it:
4535 * we need the handshake and transform structures for that */
4536 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4537 }
4538 else
4539#endif
4540 ssl_handshake_wrapup_free_hs_transform( ssl );
4541
Paul Bakker48916f92012-09-16 19:57:18 +00004542 ssl->state++;
4543
4544 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4545}
4546
Paul Bakker1ef83d62012-04-11 12:09:53 +00004547int ssl_write_finished( ssl_context *ssl )
4548{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004549 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004550
4551 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4552
Paul Bakker92be97b2013-01-02 17:30:03 +01004553 /*
4554 * Set the out_msg pointer to the correct location based on IV length
4555 */
4556 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4557 {
4558 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4559 ssl->transform_negotiate->fixed_ivlen;
4560 }
4561 else
4562 ssl->out_msg = ssl->out_iv;
4563
Paul Bakker48916f92012-09-16 19:57:18 +00004564 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004565
4566 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4568
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004569#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004570 ssl->verify_data_len = hash_len;
4571 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004572#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004573
Paul Bakker5121ce52009-01-03 21:22:43 +00004574 ssl->out_msglen = 4 + hash_len;
4575 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4576 ssl->out_msg[0] = SSL_HS_FINISHED;
4577
4578 /*
4579 * In case of session resuming, invert the client and server
4580 * ChangeCipherSpec messages order.
4581 */
Paul Bakker0a597072012-09-25 21:55:46 +00004582 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004584#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004586 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004587#endif
4588#if defined(POLARSSL_SSL_SRV_C)
4589 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00004590 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004591#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004592 }
4593 else
4594 ssl->state++;
4595
Paul Bakker48916f92012-09-16 19:57:18 +00004596 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004597 * Switch to our negotiated transform and session parameters for outbound
4598 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004599 */
4600 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004601
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004602#if defined(POLARSSL_SSL_PROTO_DTLS)
4603 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4604 {
4605 unsigned char i;
4606
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004607 /* Remember current epoch settings for resending */
4608 ssl->handshake->alt_transform_out = ssl->transform_out;
4609 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4610
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004611 /* Set sequence_number to zero */
4612 memset( ssl->out_ctr + 2, 0, 6 );
4613
4614 /* Increment epoch */
4615 for( i = 2; i > 0; i-- )
4616 if( ++ssl->out_ctr[i - 1] != 0 )
4617 break;
4618
4619 /* The loop goes to its end iff the counter is wrapping */
4620 if( i == 0 )
4621 {
4622 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4623 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4624 }
4625 }
4626 else
4627#endif /* POLARSSL_SSL_PROTO_DTLS */
4628 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004629
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004630 ssl->transform_out = ssl->transform_negotiate;
4631 ssl->session_out = ssl->session_negotiate;
4632
Paul Bakker07eb38b2012-12-19 14:42:06 +01004633#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004634 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004635 {
4636 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4637 {
4638 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4639 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4640 }
4641 }
4642#endif
4643
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004644#if defined(POLARSSL_SSL_PROTO_DTLS)
4645 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4646 ssl_send_flight_completed( ssl );
4647#endif
4648
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4650 {
4651 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4652 return( ret );
4653 }
4654
4655 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4656
4657 return( 0 );
4658}
4659
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004660#if defined(POLARSSL_SSL_PROTO_SSL3)
4661#define SSL_MAX_HASH_LEN 36
4662#else
4663#define SSL_MAX_HASH_LEN 12
4664#endif
4665
Paul Bakker5121ce52009-01-03 21:22:43 +00004666int ssl_parse_finished( ssl_context *ssl )
4667{
Paul Bakker23986e52011-04-24 08:57:21 +00004668 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004669 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004670 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004671
4672 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4673
Paul Bakker48916f92012-09-16 19:57:18 +00004674 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004675
Paul Bakker5121ce52009-01-03 21:22:43 +00004676 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4677 {
4678 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4679 return( ret );
4680 }
4681
4682 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4683 {
4684 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004685 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004686 }
4687
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004688 /* There is currently no ciphersuite using another length with TLS 1.2 */
4689#if defined(POLARSSL_SSL_PROTO_SSL3)
4690 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4691 hash_len = 36;
4692 else
4693#endif
4694 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004695
4696 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004697 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 {
4699 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004700 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701 }
4702
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004703 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4704 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 {
4706 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004707 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004708 }
4709
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004710#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004711 ssl->verify_data_len = hash_len;
4712 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004713#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004714
Paul Bakker0a597072012-09-25 21:55:46 +00004715 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004716 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004717#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004718 if( ssl->endpoint == SSL_IS_CLIENT )
4719 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004720#endif
4721#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004723 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004724#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004725 }
4726 else
4727 ssl->state++;
4728
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004729#if defined(POLARSSL_SSL_PROTO_DTLS)
4730 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4731 ssl_recv_flight_completed( ssl );
4732#endif
4733
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4735
4736 return( 0 );
4737}
4738
Paul Bakker968afaa2014-07-09 11:09:24 +02004739static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004740{
4741 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4742
4743#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4744 defined(POLARSSL_SSL_PROTO_TLS1_1)
4745 md5_init( &handshake->fin_md5 );
4746 sha1_init( &handshake->fin_sha1 );
4747 md5_starts( &handshake->fin_md5 );
4748 sha1_starts( &handshake->fin_sha1 );
4749#endif
4750#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4751#if defined(POLARSSL_SHA256_C)
4752 sha256_init( &handshake->fin_sha256 );
4753 sha256_starts( &handshake->fin_sha256, 0 );
4754#endif
4755#if defined(POLARSSL_SHA512_C)
4756 sha512_init( &handshake->fin_sha512 );
4757 sha512_starts( &handshake->fin_sha512, 1 );
4758#endif
4759#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4760
4761 handshake->update_checksum = ssl_update_checksum_start;
4762 handshake->sig_alg = SSL_HASH_SHA1;
4763
4764#if defined(POLARSSL_DHM_C)
4765 dhm_init( &handshake->dhm_ctx );
4766#endif
4767#if defined(POLARSSL_ECDH_C)
4768 ecdh_init( &handshake->ecdh_ctx );
4769#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004770}
4771
4772static void ssl_transform_init( ssl_transform *transform )
4773{
4774 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004775
4776 cipher_init( &transform->cipher_ctx_enc );
4777 cipher_init( &transform->cipher_ctx_dec );
4778
4779 md_init( &transform->md_ctx_enc );
4780 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004781}
4782
4783void ssl_session_init( ssl_session *session )
4784{
4785 memset( session, 0, sizeof(ssl_session) );
4786}
4787
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004788static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004789{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004790 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004791 if( ssl->transform_negotiate )
4792 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004793 if( ssl->session_negotiate )
4794 ssl_session_free( ssl->session_negotiate );
4795 if( ssl->handshake )
4796 ssl_handshake_free( ssl->handshake );
4797
4798 /*
4799 * Either the pointers are now NULL or cleared properly and can be freed.
4800 * Now allocate missing structures.
4801 */
4802 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004803 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01004804 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
4805 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004806 }
Paul Bakker48916f92012-09-16 19:57:18 +00004807
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004808 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004809 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01004810 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
4811 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004812 }
Paul Bakker48916f92012-09-16 19:57:18 +00004813
Paul Bakker82788fb2014-10-20 13:59:19 +02004814 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004815 {
4816 ssl->handshake = (ssl_handshake_params *)
4817 polarssl_malloc( sizeof(ssl_handshake_params) );
4818 }
Paul Bakker48916f92012-09-16 19:57:18 +00004819
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004820 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004821 if( ssl->handshake == NULL ||
4822 ssl->transform_negotiate == NULL ||
4823 ssl->session_negotiate == NULL )
4824 {
4825 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004826
4827 polarssl_free( ssl->handshake );
4828 polarssl_free( ssl->transform_negotiate );
4829 polarssl_free( ssl->session_negotiate );
4830
4831 ssl->handshake = NULL;
4832 ssl->transform_negotiate = NULL;
4833 ssl->session_negotiate = NULL;
4834
Paul Bakker48916f92012-09-16 19:57:18 +00004835 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4836 }
4837
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004838 /* Initialize structures */
4839 ssl_session_init( ssl->session_negotiate );
4840 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004841 ssl_handshake_params_init( ssl->handshake );
4842
4843#if defined(POLARSSL_X509_CRT_PARSE_C)
4844 ssl->handshake->key_cert = ssl->key_cert;
4845#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004846
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004847 /*
4848 * We may not know yet if we're using DTLS,
4849 * so always initiliase DTLS-specific fields.
4850 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004851#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004852 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004853
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004854 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004855 if( ssl->endpoint == SSL_IS_CLIENT )
4856 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4857 else
4858 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004859#endif
4860
Paul Bakker48916f92012-09-16 19:57:18 +00004861 return( 0 );
4862}
4863
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004864#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4865/* Dummy cookie callbacks for defaults */
4866static int ssl_cookie_write_dummy( void *ctx,
4867 unsigned char **p, unsigned char *end,
4868 const unsigned char *cli_id, size_t cli_id_len )
4869{
4870 ((void) ctx);
4871 ((void) p);
4872 ((void) end);
4873 ((void) cli_id);
4874 ((void) cli_id_len);
4875
4876 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4877}
4878
4879static int ssl_cookie_check_dummy( void *ctx,
4880 const unsigned char *cookie, size_t cookie_len,
4881 const unsigned char *cli_id, size_t cli_id_len )
4882{
4883 ((void) ctx);
4884 ((void) cookie);
4885 ((void) cookie_len);
4886 ((void) cli_id);
4887 ((void) cli_id_len);
4888
4889 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4890}
4891#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4892
Paul Bakker5121ce52009-01-03 21:22:43 +00004893/*
4894 * Initialize an SSL context
4895 */
4896int ssl_init( ssl_context *ssl )
4897{
Paul Bakker48916f92012-09-16 19:57:18 +00004898 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004899 int len = SSL_BUFFER_LEN;
4900
4901 memset( ssl, 0, sizeof( ssl_context ) );
4902
Paul Bakker62f2dee2012-09-28 07:31:51 +00004903 /*
4904 * Sane defaults
4905 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004906 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4907 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4908 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4909 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004910
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004911 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004912
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004913#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004914 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004915 memset( ssl->renego_period, 0xFF, 7 );
4916 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004917#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004918
Paul Bakker62f2dee2012-09-28 07:31:51 +00004919#if defined(POLARSSL_DHM_C)
4920 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4921 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4922 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4923 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4924 {
4925 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4926 return( ret );
4927 }
4928#endif
4929
4930 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004931 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004932 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004933 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004934 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004935
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004936 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004937 {
4938 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004939 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004940 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004941 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004942 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004943 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004944 }
4945
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004946 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4947 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004948
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004949 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4950 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4951
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004952#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4953 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
4954#endif
4955
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004956#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4957 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
4958#endif
4959
Paul Bakker606b4ba2013-08-14 16:52:14 +02004960#if defined(POLARSSL_SSL_SESSION_TICKETS)
4961 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4962#endif
4963
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004964#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004965 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004966#endif
4967
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004968#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4969 ssl->f_cookie_write = ssl_cookie_write_dummy;
4970 ssl->f_cookie_check = ssl_cookie_check_dummy;
4971#endif
4972
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004973#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4974 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4975#endif
4976
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004977#if defined(POLARSSL_SSL_PROTO_DTLS)
4978 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4979 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4980#endif
4981
Paul Bakker48916f92012-09-16 19:57:18 +00004982 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4983 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004984
4985 return( 0 );
4986}
4987
4988/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004989 * Reset an initialized and used SSL context for re-use while retaining
4990 * all application-set variables, function pointers and data.
4991 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004992int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004993{
Paul Bakker48916f92012-09-16 19:57:18 +00004994 int ret;
4995
Paul Bakker7eb013f2011-10-06 12:37:39 +00004996 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004997
4998#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004999 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005000 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00005001
5002 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01005003 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
5004 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005005#endif
5006 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00005007
Paul Bakker7eb013f2011-10-06 12:37:39 +00005008 ssl->in_offt = NULL;
5009
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005010 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005011 ssl->in_msgtype = 0;
5012 ssl->in_msglen = 0;
5013 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005014#if defined(POLARSSL_SSL_PROTO_DTLS)
5015 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02005016 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005017#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005018#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5019 ssl_dtls_replay_reset( ssl );
5020#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00005021
5022 ssl->in_hslen = 0;
5023 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005024 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005025
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005026 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005027 ssl->out_msgtype = 0;
5028 ssl->out_msglen = 0;
5029 ssl->out_left = 0;
5030
Paul Bakker48916f92012-09-16 19:57:18 +00005031 ssl->transform_in = NULL;
5032 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00005033
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005034 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
5035 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00005036
5037#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02005038 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005039 {
5040 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01005041 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005042 {
5043 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
5044 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
5045 }
Paul Bakker05ef8352012-05-08 09:17:57 +00005046 }
5047#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00005048
Paul Bakker48916f92012-09-16 19:57:18 +00005049 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005050 {
Paul Bakker48916f92012-09-16 19:57:18 +00005051 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005052 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005053 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00005054 }
Paul Bakker48916f92012-09-16 19:57:18 +00005055
Paul Bakkerc0463502013-02-14 11:19:38 +01005056 if( ssl->session )
5057 {
5058 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005059 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005060 ssl->session = NULL;
5061 }
5062
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005063#if defined(POLARSSL_SSL_ALPN)
5064 ssl->alpn_chosen = NULL;
5065#endif
5066
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02005067#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02005068 polarssl_free( ssl->cli_id );
5069 ssl->cli_id = NULL;
5070 ssl->cli_id_len = 0;
5071#endif
5072
Paul Bakker48916f92012-09-16 19:57:18 +00005073 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5074 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005075
5076 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00005077}
5078
Paul Bakkera503a632013-08-14 13:48:06 +02005079#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005080static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
5081{
5082 aes_free( &tkeys->enc );
5083 aes_free( &tkeys->dec );
5084
5085 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
5086}
5087
Paul Bakker7eb013f2011-10-06 12:37:39 +00005088/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005089 * Allocate and initialize ticket keys
5090 */
5091static int ssl_ticket_keys_init( ssl_context *ssl )
5092{
5093 int ret;
5094 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005095 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005096
5097 if( ssl->ticket_keys != NULL )
5098 return( 0 );
5099
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005100 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
5101 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005102 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5103
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005104 aes_init( &tkeys->enc );
5105 aes_init( &tkeys->dec );
5106
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005107 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005108 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005109 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005110 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005111 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005112 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005113
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005114 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
5115 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
5116 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
5117 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005118 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005119 polarssl_free( tkeys );
5120 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02005121 }
5122
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005123 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01005124 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005125 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005126 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005127 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01005128 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02005129
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005130 ssl->ticket_keys = tkeys;
5131
5132 return( 0 );
5133}
Paul Bakkera503a632013-08-14 13:48:06 +02005134#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005135
5136/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005137 * SSL set accessors
5138 */
5139void ssl_set_endpoint( ssl_context *ssl, int endpoint )
5140{
5141 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005142
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005143#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
5144 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005145 if( endpoint == SSL_IS_CLIENT )
5146 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02005147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005148}
5149
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005150int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005151{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005152#if defined(POLARSSL_SSL_PROTO_DTLS)
5153 if( transport == SSL_TRANSPORT_DATAGRAM )
5154 {
5155 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005156
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005157 ssl->out_hdr = ssl->out_buf;
5158 ssl->out_ctr = ssl->out_buf + 3;
5159 ssl->out_len = ssl->out_buf + 11;
5160 ssl->out_iv = ssl->out_buf + 13;
5161 ssl->out_msg = ssl->out_buf + 13;
5162
5163 ssl->in_hdr = ssl->in_buf;
5164 ssl->in_ctr = ssl->in_buf + 3;
5165 ssl->in_len = ssl->in_buf + 11;
5166 ssl->in_iv = ssl->in_buf + 13;
5167 ssl->in_msg = ssl->in_buf + 13;
5168
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005169 /* DTLS starts with TLS1.1 */
5170 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
5171 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005172
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005173 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
5174 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
5175
5176 return( 0 );
5177 }
5178#endif
5179
5180 if( transport == SSL_TRANSPORT_STREAM )
5181 {
5182 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01005183
5184 ssl->out_ctr = ssl->out_buf;
5185 ssl->out_hdr = ssl->out_buf + 8;
5186 ssl->out_len = ssl->out_buf + 11;
5187 ssl->out_iv = ssl->out_buf + 13;
5188 ssl->out_msg = ssl->out_buf + 13;
5189
5190 ssl->in_ctr = ssl->in_buf;
5191 ssl->in_hdr = ssl->in_buf + 8;
5192 ssl->in_len = ssl->in_buf + 11;
5193 ssl->in_iv = ssl->in_buf + 13;
5194 ssl->in_msg = ssl->in_buf + 13;
5195
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005196 return( 0 );
5197 }
5198
5199 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01005200}
5201
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02005202#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
5203void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
5204{
5205 ssl->anti_replay = mode;
5206}
5207#endif
5208
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005209#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
5210void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
5211{
5212 ssl->badmac_limit = limit;
5213}
5214#endif
5215
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02005216#if defined(POLARSSL_SSL_PROTO_DTLS)
5217void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
5218{
5219 ssl->hs_timeout_min = min;
5220 ssl->hs_timeout_max = max;
5221}
5222#endif
5223
Paul Bakker5121ce52009-01-03 21:22:43 +00005224void ssl_set_authmode( ssl_context *ssl, int authmode )
5225{
5226 ssl->authmode = authmode;
5227}
5228
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005229#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005230void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005231 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005232 void *p_vrfy )
5233{
5234 ssl->f_vrfy = f_vrfy;
5235 ssl->p_vrfy = p_vrfy;
5236}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005237#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005238
Paul Bakker5121ce52009-01-03 21:22:43 +00005239void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005240 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005241 void *p_rng )
5242{
5243 ssl->f_rng = f_rng;
5244 ssl->p_rng = p_rng;
5245}
5246
5247void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005248 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 void *p_dbg )
5250{
5251 ssl->f_dbg = f_dbg;
5252 ssl->p_dbg = p_dbg;
5253}
5254
5255void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005256 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005257 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005258{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005259 if( p_recv != p_send )
5260 {
5261 ssl->f_recv = NULL;
5262 ssl->f_send = NULL;
5263 ssl->p_bio = NULL;
5264 return;
5265 }
5266
Paul Bakker5121ce52009-01-03 21:22:43 +00005267 ssl->f_recv = f_recv;
5268 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005269 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005270}
5271
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005272void ssl_set_bio_timeout( ssl_context *ssl,
5273 void *p_bio,
5274 int (*f_send)(void *, const unsigned char *, size_t),
5275 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005276 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5277 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005278{
5279 ssl->p_bio = p_bio;
5280 ssl->f_send = f_send;
5281 ssl->f_recv = f_recv;
5282 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005283 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005284}
5285
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005286#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005287void ssl_set_session_cache( ssl_context *ssl,
5288 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5289 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005290{
Paul Bakker0a597072012-09-25 21:55:46 +00005291 ssl->f_get_cache = f_get_cache;
5292 ssl->p_get_cache = p_get_cache;
5293 ssl->f_set_cache = f_set_cache;
5294 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005295}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005296#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005297
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005298#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005299int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005300{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005301 int ret;
5302
5303 if( ssl == NULL ||
5304 session == NULL ||
5305 ssl->session_negotiate == NULL ||
5306 ssl->endpoint != SSL_IS_CLIENT )
5307 {
5308 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5309 }
5310
5311 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5312 return( ret );
5313
Paul Bakker0a597072012-09-25 21:55:46 +00005314 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005315
5316 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005317}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005318#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005319
Paul Bakkerb68cad62012-08-23 08:34:18 +00005320void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005321{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005322 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5323 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5324 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5325 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5326}
5327
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005328void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5329 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005330 int major, int minor )
5331{
5332 if( major != SSL_MAJOR_VERSION_3 )
5333 return;
5334
5335 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5336 return;
5337
5338 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005339}
5340
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005341#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005342/* Add a new (empty) key_cert entry an return a pointer to it */
5343static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5344{
5345 ssl_key_cert *key_cert, *last;
5346
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005347 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5348 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005349 return( NULL );
5350
5351 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5352
5353 /* Append the new key_cert to the (possibly empty) current list */
5354 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005355 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005356 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005357 if( ssl->handshake != NULL )
5358 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005359 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005360 else
5361 {
5362 last = ssl->key_cert;
5363 while( last->next != NULL )
5364 last = last->next;
5365 last->next = key_cert;
5366 }
5367
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005368 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005369}
5370
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005371void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005372 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005373{
5374 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005375 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005376 ssl->peer_cn = peer_cn;
5377}
5378
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005379int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005380 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005381{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005382 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5383
5384 if( key_cert == NULL )
5385 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5386
5387 key_cert->cert = own_cert;
5388 key_cert->key = pk_key;
5389
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005390 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005391}
5392
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005393#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005394int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005395 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005396{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005397 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005398 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005399
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005400 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005401 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5402
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005403 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5404 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005405 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005406
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005407 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005408
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005409 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005410 if( ret != 0 )
5411 return( ret );
5412
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005413 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005414 return( ret );
5415
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005416 key_cert->cert = own_cert;
5417 key_cert->key_own_alloc = 1;
5418
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005419 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005420}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005421#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005422
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005423int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005424 void *rsa_key,
5425 rsa_decrypt_func rsa_decrypt,
5426 rsa_sign_func rsa_sign,
5427 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005428{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005429 int ret;
5430 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005431
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005432 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005433 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5434
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005435 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5436 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005437 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005438
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005439 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005440
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005441 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5442 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5443 return( ret );
5444
5445 key_cert->cert = own_cert;
5446 key_cert->key_own_alloc = 1;
5447
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01005448 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00005449}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005450#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005451
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005452#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005453int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5454 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005455{
Paul Bakker6db455e2013-09-18 17:29:31 +02005456 if( psk == NULL || psk_identity == NULL )
5457 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5458
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005459 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005460 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5461
Paul Bakker6db455e2013-09-18 17:29:31 +02005462 if( ssl->psk != NULL )
5463 {
5464 polarssl_free( ssl->psk );
5465 polarssl_free( ssl->psk_identity );
5466 }
5467
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005468 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005469 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005470
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005471 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005472 ssl->psk_identity = (unsigned char *)
5473 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005474
5475 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5476 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5477
5478 memcpy( ssl->psk, psk, ssl->psk_len );
5479 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005480
5481 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005482}
5483
5484void ssl_set_psk_cb( ssl_context *ssl,
5485 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5486 size_t),
5487 void *p_psk )
5488{
5489 ssl->f_psk = f_psk;
5490 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005491}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005492#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005493
Paul Bakker48916f92012-09-16 19:57:18 +00005494#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005495int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005496{
5497 int ret;
5498
Paul Bakker48916f92012-09-16 19:57:18 +00005499 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005500 {
5501 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5502 return( ret );
5503 }
5504
Paul Bakker48916f92012-09-16 19:57:18 +00005505 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005506 {
5507 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5508 return( ret );
5509 }
5510
5511 return( 0 );
5512}
5513
Paul Bakker1b57b062011-01-06 15:48:19 +00005514int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5515{
5516 int ret;
5517
Paul Bakker66d5d072014-06-17 16:39:18 +02005518 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005519 {
5520 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5521 return( ret );
5522 }
5523
Paul Bakker66d5d072014-06-17 16:39:18 +02005524 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005525 {
5526 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5527 return( ret );
5528 }
5529
5530 return( 0 );
5531}
Paul Bakker48916f92012-09-16 19:57:18 +00005532#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005533
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005534#if defined(POLARSSL_SSL_SET_CURVES)
5535/*
5536 * Set the allowed elliptic curves
5537 */
5538void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5539{
5540 ssl->curve_list = curve_list;
5541}
5542#endif
5543
Paul Bakker0be444a2013-08-27 21:55:01 +02005544#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005545int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005546{
5547 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005548 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005549
5550 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005551
5552 if( ssl->hostname_len + 1 == 0 )
5553 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5554
Paul Bakker6e339b52013-07-03 13:37:05 +02005555 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005556
Paul Bakkerb15b8512012-01-13 13:44:06 +00005557 if( ssl->hostname == NULL )
5558 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5559
Paul Bakker3c2122f2013-06-24 19:03:14 +02005560 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005561 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005562
Paul Bakker40ea7de2009-05-03 10:18:48 +00005563 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005564
5565 return( 0 );
5566}
5567
Paul Bakker5701cdc2012-09-27 21:49:42 +00005568void ssl_set_sni( ssl_context *ssl,
5569 int (*f_sni)(void *, ssl_context *,
5570 const unsigned char *, size_t),
5571 void *p_sni )
5572{
5573 ssl->f_sni = f_sni;
5574 ssl->p_sni = p_sni;
5575}
Paul Bakker0be444a2013-08-27 21:55:01 +02005576#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005577
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005578#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005579int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005580{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005581 size_t cur_len, tot_len;
5582 const char **p;
5583
5584 /*
5585 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5586 * truncated". Check lengths now rather than later.
5587 */
5588 tot_len = 0;
5589 for( p = protos; *p != NULL; p++ )
5590 {
5591 cur_len = strlen( *p );
5592 tot_len += cur_len;
5593
5594 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5595 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5596 }
5597
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005598 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005599
5600 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005601}
5602
5603const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5604{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005605 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005606}
5607#endif /* POLARSSL_SSL_ALPN */
5608
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005609static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005610{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005611 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005612 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005613 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005614 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005615 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005616
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005617#if defined(POLARSSL_SSL_PROTO_DTLS)
5618 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5619 minor < SSL_MINOR_VERSION_2 )
5620 {
5621 return( -1 );
5622 }
5623#else
5624 ((void) ssl);
5625#endif
5626
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005627 return( 0 );
5628}
5629
5630int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5631{
5632 if( ssl_check_version( ssl, major, minor ) != 0 )
5633 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5634
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005635 ssl->max_major_ver = major;
5636 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005637
5638 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005639}
5640
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005641int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005642{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005643 if( ssl_check_version( ssl, major, minor ) != 0 )
5644 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005645
5646 ssl->min_major_ver = major;
5647 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005648
5649 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005650}
5651
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02005652#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
5653void ssl_set_fallback( ssl_context *ssl, char fallback )
5654{
5655 ssl->fallback = fallback;
5656}
5657#endif
5658
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01005659#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
5660void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
5661{
5662 ssl->encrypt_then_mac = etm;
5663}
5664#endif
5665
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02005666#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
5667void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
5668{
5669 ssl->extended_ms = ems;
5670}
5671#endif
5672
Paul Bakker05decb22013-08-15 13:33:48 +02005673#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005674int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5675{
Paul Bakker77e257e2013-12-16 15:29:52 +01005676 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005677 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005678 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005679 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005680 }
5681
5682 ssl->mfl_code = mfl_code;
5683
5684 return( 0 );
5685}
Paul Bakker05decb22013-08-15 13:33:48 +02005686#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005687
Paul Bakker1f2bc622013-08-15 13:45:55 +02005688#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005689int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005690{
5691 if( ssl->endpoint != SSL_IS_CLIENT )
5692 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5693
Paul Bakker8c1ede62013-07-19 14:14:37 +02005694 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005695
5696 return( 0 );
5697}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005698#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005699
Paul Bakker48916f92012-09-16 19:57:18 +00005700void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5701{
5702 ssl->allow_legacy_renegotiation = allow_legacy;
5703}
5704
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005705#if defined(POLARSSL_SSL_RENEGOTIATION)
5706void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5707{
5708 ssl->disable_renegotiation = renegotiation;
5709}
5710
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005711void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5712{
5713 ssl->renego_max_records = max_records;
5714}
5715
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01005716void ssl_set_renegotiation_period( ssl_context *ssl,
5717 const unsigned char period[8] )
5718{
5719 memcpy( ssl->renego_period, period, 8 );
5720}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005721#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00005722
Paul Bakkera503a632013-08-14 13:48:06 +02005723#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005724int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5725{
5726 ssl->session_tickets = use_tickets;
5727
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005728#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005729 if( ssl->endpoint == SSL_IS_CLIENT )
5730 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005731#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005732
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01005733 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
5734 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005735
5736 if( ssl->f_rng == NULL )
5737 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5738
5739 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005740}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005741
5742void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5743{
5744 ssl->ticket_lifetime = lifetime;
5745}
Paul Bakkera503a632013-08-14 13:48:06 +02005746#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005747
Paul Bakker5121ce52009-01-03 21:22:43 +00005748/*
5749 * SSL get accessors
5750 */
5751size_t ssl_get_bytes_avail( const ssl_context *ssl )
5752{
5753 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5754}
5755
Paul Bakkerff60ee62010-03-16 21:09:09 +00005756int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005757{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005758 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005759}
5760
Paul Bakkere3166ce2011-01-27 17:40:50 +00005761const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005762{
Paul Bakker926c8e42013-03-06 10:23:34 +01005763 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005764 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005765
Paul Bakkere3166ce2011-01-27 17:40:50 +00005766 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005767}
5768
Paul Bakker43ca69c2011-01-15 17:35:19 +00005769const char *ssl_get_version( const ssl_context *ssl )
5770{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005771#if defined(POLARSSL_SSL_PROTO_DTLS)
5772 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5773 {
5774 switch( ssl->minor_ver )
5775 {
5776 case SSL_MINOR_VERSION_2:
5777 return( "DTLSv1.0" );
5778
5779 case SSL_MINOR_VERSION_3:
5780 return( "DTLSv1.2" );
5781
5782 default:
5783 return( "unknown (DTLS)" );
5784 }
5785 }
5786#endif
5787
Paul Bakker43ca69c2011-01-15 17:35:19 +00005788 switch( ssl->minor_ver )
5789 {
5790 case SSL_MINOR_VERSION_0:
5791 return( "SSLv3.0" );
5792
5793 case SSL_MINOR_VERSION_1:
5794 return( "TLSv1.0" );
5795
5796 case SSL_MINOR_VERSION_2:
5797 return( "TLSv1.1" );
5798
Paul Bakker1ef83d62012-04-11 12:09:53 +00005799 case SSL_MINOR_VERSION_3:
5800 return( "TLSv1.2" );
5801
Paul Bakker43ca69c2011-01-15 17:35:19 +00005802 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005803 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005804 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005805}
5806
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005807int ssl_get_record_expansion( const ssl_context *ssl )
5808{
5809 int transform_expansion;
5810 const ssl_transform *transform = ssl->transform_out;
5811
5812#if defined(POLARSSL_ZLIB_SUPPORT)
5813 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5814 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5815#endif
5816
5817 if( transform == NULL )
5818 return( ssl_hdr_len( ssl ) );
5819
5820 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5821 {
5822 case POLARSSL_MODE_GCM:
5823 case POLARSSL_MODE_CCM:
5824 case POLARSSL_MODE_STREAM:
5825 transform_expansion = transform->minlen;
5826 break;
5827
5828 case POLARSSL_MODE_CBC:
5829 transform_expansion = transform->maclen
5830 + cipher_get_block_size( &transform->cipher_ctx_enc );
5831 break;
5832
5833 default:
5834 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5835 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5836 }
5837
5838 return( ssl_hdr_len( ssl ) + transform_expansion );
5839}
5840
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005841#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005842const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005843{
5844 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005845 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005846
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005847 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005848}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005849#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005850
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005851#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005852int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5853{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005854 if( ssl == NULL ||
5855 dst == NULL ||
5856 ssl->session == NULL ||
5857 ssl->endpoint != SSL_IS_CLIENT )
5858 {
5859 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5860 }
5861
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005862 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005863}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01005864#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005865
Paul Bakker5121ce52009-01-03 21:22:43 +00005866/*
Paul Bakker1961b702013-01-25 14:49:24 +01005867 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005868 */
Paul Bakker1961b702013-01-25 14:49:24 +01005869int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005870{
Paul Bakker40e46942009-01-03 21:51:57 +00005871 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005872
Paul Bakker40e46942009-01-03 21:51:57 +00005873#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005874 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005875 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005876#endif
Paul Bakker40e46942009-01-03 21:51:57 +00005877#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005878 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005879 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005880#endif
5881
Paul Bakker1961b702013-01-25 14:49:24 +01005882 return( ret );
5883}
5884
5885/*
5886 * Perform the SSL handshake
5887 */
5888int ssl_handshake( ssl_context *ssl )
5889{
5890 int ret = 0;
5891
5892 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5893
5894 while( ssl->state != SSL_HANDSHAKE_OVER )
5895 {
5896 ret = ssl_handshake_step( ssl );
5897
5898 if( ret != 0 )
5899 break;
5900 }
5901
Paul Bakker5121ce52009-01-03 21:22:43 +00005902 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5903
5904 return( ret );
5905}
5906
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01005907#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005908#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005909/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005910 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005911 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005912static int ssl_write_hello_request( ssl_context *ssl )
5913{
5914 int ret;
5915
5916 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5917
5918 ssl->out_msglen = 4;
5919 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5920 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5921
5922 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5923 {
5924 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5925 return( ret );
5926 }
5927
5928 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5929
5930 return( 0 );
5931}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005932#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005933
5934/*
5935 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005936 * - any side: calling ssl_renegotiate(),
5937 * - client: receiving a HelloRequest during ssl_read(),
5938 * - server: receiving any handshake message on server during ssl_read() after
5939 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005940 * If the handshake doesn't complete due to waiting for I/O, it will continue
5941 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005942 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005943static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005944{
5945 int ret;
5946
5947 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5948
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005949 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5950 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005951
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005952 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5953 * the ServerHello will have message_seq = 1" */
5954#if defined(POLARSSL_SSL_PROTO_DTLS)
5955 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005956 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5957 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005958 if( ssl->endpoint == SSL_IS_SERVER )
5959 ssl->handshake->out_msg_seq = 1;
5960 else
5961 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005962 }
5963#endif
5964
Paul Bakker48916f92012-09-16 19:57:18 +00005965 ssl->state = SSL_HELLO_REQUEST;
5966 ssl->renegotiation = SSL_RENEGOTIATION;
5967
Paul Bakker48916f92012-09-16 19:57:18 +00005968 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5969 {
5970 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5971 return( ret );
5972 }
5973
5974 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5975
5976 return( 0 );
5977}
5978
5979/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005980 * Renegotiate current connection on client,
5981 * or request renegotiation on server
5982 */
5983int ssl_renegotiate( ssl_context *ssl )
5984{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005985 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005986
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005987#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005988 /* On server, just send the request */
5989 if( ssl->endpoint == SSL_IS_SERVER )
5990 {
5991 if( ssl->state != SSL_HANDSHAKE_OVER )
5992 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5993
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005994 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5995
5996 /* Did we already try/start sending HelloRequest? */
5997 if( ssl->out_left != 0 )
5998 return( ssl_flush_output( ssl ) );
5999
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006000 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006001 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006002#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006003
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006004#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006005 /*
6006 * On client, either start the renegotiation process or,
6007 * if already in progress, continue the handshake
6008 */
6009 if( ssl->renegotiation != SSL_RENEGOTIATION )
6010 {
6011 if( ssl->state != SSL_HANDSHAKE_OVER )
6012 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6013
6014 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
6015 {
6016 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
6017 return( ret );
6018 }
6019 }
6020 else
6021 {
6022 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6023 {
6024 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6025 return( ret );
6026 }
6027 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006028#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006029
Paul Bakker37ce0ff2013-10-31 14:32:04 +01006030 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01006031}
6032
6033/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006034 * Check record counters and renegotiate if they're above the limit.
6035 */
6036static int ssl_check_ctr_renegotiate( ssl_context *ssl )
6037{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006038 if( ssl->state != SSL_HANDSHAKE_OVER ||
6039 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
6040 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
6041 {
6042 return( 0 );
6043 }
6044
6045 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006046 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
6047 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006048 {
6049 return( 0 );
6050 }
6051
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01006052 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006053 return( ssl_renegotiate( ssl ) );
6054}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006055#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00006056
6057/*
6058 * Receive application data decrypted from the SSL layer
6059 */
Paul Bakker23986e52011-04-24 08:57:21 +00006060int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006061{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006062 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00006063 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00006064
6065 SSL_DEBUG_MSG( 2, ( "=> read" ) );
6066
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006067#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006068 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006069 {
6070 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6071 return( ret );
6072
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006073 if( ssl->handshake != NULL &&
6074 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
6075 {
6076 if( ( ret = ssl_resend( ssl ) ) != 0 )
6077 return( ret );
6078 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006079 }
6080#endif
6081
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006082#if defined(POLARSSL_SSL_RENEGOTIATION)
6083 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6084 {
6085 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6086 return( ret );
6087 }
6088#endif
6089
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 if( ssl->state != SSL_HANDSHAKE_OVER )
6091 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006092 ret = ssl_handshake( ssl );
6093 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6094 {
6095 record_read = 1;
6096 }
6097 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006098 {
6099 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6100 return( ret );
6101 }
6102 }
6103
6104 if( ssl->in_offt == NULL )
6105 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006106#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006107 /* Start timer if not already running */
6108 if( ssl->time_limit == 0 )
6109 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006110#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006111
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006112 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006114 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6115 {
6116 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6117 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00006118
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006119 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6120 return( ret );
6121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 }
6123
6124 if( ssl->in_msglen == 0 &&
6125 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
6126 {
6127 /*
6128 * OpenSSL sends empty messages to randomize the IV
6129 */
6130 if( ( ret = ssl_read_record( ssl ) ) != 0 )
6131 {
Paul Bakker831a7552011-05-18 13:32:51 +00006132 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
6133 return( 0 );
6134
Paul Bakker5121ce52009-01-03 21:22:43 +00006135 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
6136 return( ret );
6137 }
6138 }
6139
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006140#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00006141 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
6142 {
6143 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
6144
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006145#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006146 if( ssl->endpoint == SSL_IS_CLIENT &&
6147 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00006148 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006149 {
6150 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006151
6152 /* With DTLS, drop the packet (probably from last handshake) */
6153#if defined(POLARSSL_SSL_PROTO_DTLS)
6154 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6155 return( POLARSSL_ERR_NET_WANT_READ );
6156#endif
6157 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6158 }
6159
6160 if( ssl->endpoint == SSL_IS_SERVER &&
6161 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
6162 {
6163 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
6164
6165 /* With DTLS, drop the packet (probably from last handshake) */
6166#if defined(POLARSSL_SSL_PROTO_DTLS)
6167 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6168 return( POLARSSL_ERR_NET_WANT_READ );
6169#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006170 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6171 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006172#endif
Paul Bakker48916f92012-09-16 19:57:18 +00006173
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006174 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
6175 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006176 ssl->allow_legacy_renegotiation ==
6177 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00006178 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02006179 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006180
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006181#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006182 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006183 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006184 /*
6185 * SSLv3 does not have a "no_renegotiation" alert
6186 */
6187 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
6188 return( ret );
6189 }
6190 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006191#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006192#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
6193 defined(POLARSSL_SSL_PROTO_TLS1_2)
6194 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006195 {
6196 if( ( ret = ssl_send_alert_message( ssl,
6197 SSL_ALERT_LEVEL_WARNING,
6198 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
6199 {
6200 return( ret );
6201 }
Paul Bakker48916f92012-09-16 19:57:18 +00006202 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006203 else
Paul Bakker9af723c2014-05-01 13:03:14 +02006204#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
6205 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02006206 {
6207 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02006208 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02006209 }
Paul Bakker48916f92012-09-16 19:57:18 +00006210 }
6211 else
6212 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006213 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02006214#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02006215 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
6216 ssl->endpoint == SSL_IS_CLIENT )
6217 {
6218 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
6219 }
6220#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006221 ret = ssl_start_renegotiation( ssl );
6222 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
6223 {
6224 record_read = 1;
6225 }
6226 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00006227 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01006228 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00006229 return( ret );
6230 }
Paul Bakker48916f92012-09-16 19:57:18 +00006231 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006232
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02006233 /* If a non-handshake record was read during renego, fallthrough,
6234 * else tell the user they should call ssl_read() again */
6235 if( ! record_read )
6236 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00006237 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006238 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6239 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006240
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006241 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006242 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006243 if( ++ssl->renego_records_seen > ssl->renego_max_records )
6244 {
6245 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
6246 "but not honored by client" ) );
6247 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
6248 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02006249 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01006250 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01006251#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02006252
6253 /* Fatal and closure alerts handled by ssl_read_record() */
6254 if( ssl->in_msgtype == SSL_MSG_ALERT )
6255 {
6256 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
6257 return( POLARSSL_ERR_NET_WANT_READ );
6258 }
6259
6260 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00006261 {
6262 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00006263 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006264 }
6265
6266 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02006267
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02006268#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02006269 /* We're going to return something now, cancel timer,
6270 * except if handshake (renegotiation) is in progress */
6271 if( ssl->state == SSL_HANDSHAKE_OVER )
6272 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006273
6274 /* If we requested renego but received AppData, resend HelloRequest.
6275 * Do it now, after setting in_offt, to avoid taking this branch
6276 * again if ssl_write_hello_request() returns WANT_WRITE */
6277#if defined(POLARSSL_SSL_SRV_C)
6278 if( ssl->endpoint == SSL_IS_SERVER &&
6279 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
6280 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006281 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006282 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02006283 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02006284 return( ret );
6285 }
6286 }
6287#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02006288#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006289 }
6290
6291 n = ( len < ssl->in_msglen )
6292 ? len : ssl->in_msglen;
6293
6294 memcpy( buf, ssl->in_offt, n );
6295 ssl->in_msglen -= n;
6296
6297 if( ssl->in_msglen == 0 )
6298 /* all bytes consumed */
6299 ssl->in_offt = NULL;
6300 else
6301 /* more data available */
6302 ssl->in_offt += n;
6303
6304 SSL_DEBUG_MSG( 2, ( "<= read" ) );
6305
Paul Bakker23986e52011-04-24 08:57:21 +00006306 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00006307}
6308
6309/*
6310 * Send application data to be encrypted by the SSL layer
6311 */
Paul Bakker23986e52011-04-24 08:57:21 +00006312int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006313{
Paul Bakker23986e52011-04-24 08:57:21 +00006314 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006315#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6316 unsigned int max_len;
6317#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006318
6319 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6320
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01006321#if defined(POLARSSL_SSL_RENEGOTIATION)
6322 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
6323 {
6324 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
6325 return( ret );
6326 }
6327#endif
6328
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 if( ssl->state != SSL_HANDSHAKE_OVER )
6330 {
6331 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6332 {
6333 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6334 return( ret );
6335 }
6336 }
6337
Paul Bakker05decb22013-08-15 13:33:48 +02006338#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006339 /*
6340 * Assume mfl_code is correct since it was checked when set
6341 */
6342 max_len = mfl_code_to_length[ssl->mfl_code];
6343
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006344 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006345 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006346 */
6347 if( ssl->session_out != NULL &&
6348 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6349 {
6350 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6351 }
6352
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006353 if( len > max_len )
6354 {
6355#if defined(POLARSSL_SSL_PROTO_DTLS)
6356 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6357 {
6358 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6359 "maximum fragment length: %d > %d",
6360 len, max_len ) );
6361 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6362 }
6363 else
6364#endif
6365 len = max_len;
6366 }
6367#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006368
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 if( ssl->out_left != 0 )
6370 {
6371 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6372 {
6373 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6374 return( ret );
6375 }
6376 }
Paul Bakker887bd502011-06-08 13:10:54 +00006377 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006378 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006379 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006380 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006381 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006382
6383 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6384 {
6385 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6386 return( ret );
6387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006388 }
6389
6390 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6391
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006392 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006393}
6394
6395/*
6396 * Notify the peer that the connection is being closed
6397 */
6398int ssl_close_notify( ssl_context *ssl )
6399{
6400 int ret;
6401
6402 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6403
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006404 if( ssl->out_left != 0 )
6405 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
6407 if( ssl->state == SSL_HANDSHAKE_OVER )
6408 {
Paul Bakker48916f92012-09-16 19:57:18 +00006409 if( ( ret = ssl_send_alert_message( ssl,
6410 SSL_ALERT_LEVEL_WARNING,
6411 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006413 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006414 return( ret );
6415 }
6416 }
6417
6418 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6419
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006420 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006421}
6422
Paul Bakker48916f92012-09-16 19:57:18 +00006423void ssl_transform_free( ssl_transform *transform )
6424{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006425 if( transform == NULL )
6426 return;
6427
Paul Bakker48916f92012-09-16 19:57:18 +00006428#if defined(POLARSSL_ZLIB_SUPPORT)
6429 deflateEnd( &transform->ctx_deflate );
6430 inflateEnd( &transform->ctx_inflate );
6431#endif
6432
Paul Bakker84bbeb52014-07-01 14:53:22 +02006433 cipher_free( &transform->cipher_ctx_enc );
6434 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006435
Paul Bakker84bbeb52014-07-01 14:53:22 +02006436 md_free( &transform->md_ctx_enc );
6437 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006438
Paul Bakker34617722014-06-13 17:20:13 +02006439 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006440}
6441
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006442#if defined(POLARSSL_X509_CRT_PARSE_C)
6443static void ssl_key_cert_free( ssl_key_cert *key_cert )
6444{
6445 ssl_key_cert *cur = key_cert, *next;
6446
6447 while( cur != NULL )
6448 {
6449 next = cur->next;
6450
6451 if( cur->key_own_alloc )
6452 {
6453 pk_free( cur->key );
6454 polarssl_free( cur->key );
6455 }
6456 polarssl_free( cur );
6457
6458 cur = next;
6459 }
6460}
6461#endif /* POLARSSL_X509_CRT_PARSE_C */
6462
Paul Bakker48916f92012-09-16 19:57:18 +00006463void ssl_handshake_free( ssl_handshake_params *handshake )
6464{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006465 if( handshake == NULL )
6466 return;
6467
Paul Bakker48916f92012-09-16 19:57:18 +00006468#if defined(POLARSSL_DHM_C)
6469 dhm_free( &handshake->dhm_ctx );
6470#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006471#if defined(POLARSSL_ECDH_C)
6472 ecdh_free( &handshake->ecdh_ctx );
6473#endif
6474
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006475#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006476 /* explicit void pointer cast for buggy MS compiler */
6477 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006478#endif
6479
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006480#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6481 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6482 /*
6483 * Free only the linked list wrapper, not the keys themselves
6484 * since the belong to the SNI callback
6485 */
6486 if( handshake->sni_key_cert != NULL )
6487 {
6488 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6489
6490 while( cur != NULL )
6491 {
6492 next = cur->next;
6493 polarssl_free( cur );
6494 cur = next;
6495 }
6496 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006497#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006498
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006499#if defined(POLARSSL_SSL_PROTO_DTLS)
6500 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006501 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006502 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006503#endif
6504
Paul Bakker34617722014-06-13 17:20:13 +02006505 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006506}
6507
6508void ssl_session_free( ssl_session *session )
6509{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006510 if( session == NULL )
6511 return;
6512
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006513#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006514 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006515 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006516 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006517 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006518 }
Paul Bakkered27a042013-04-18 22:46:23 +02006519#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006520
Paul Bakkera503a632013-08-14 13:48:06 +02006521#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006522 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006523#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006524
Paul Bakker34617722014-06-13 17:20:13 +02006525 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006526}
6527
Paul Bakker5121ce52009-01-03 21:22:43 +00006528/*
6529 * Free an SSL context
6530 */
6531void ssl_free( ssl_context *ssl )
6532{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006533 if( ssl == NULL )
6534 return;
6535
Paul Bakker5121ce52009-01-03 21:22:43 +00006536 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6537
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006538 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006540 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6541 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 }
6543
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006544 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006545 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006546 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6547 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006548 }
6549
Paul Bakker16770332013-10-11 09:59:44 +02006550#if defined(POLARSSL_ZLIB_SUPPORT)
6551 if( ssl->compress_buf != NULL )
6552 {
Paul Bakker34617722014-06-13 17:20:13 +02006553 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006554 polarssl_free( ssl->compress_buf );
6555 }
6556#endif
6557
Paul Bakker40e46942009-01-03 21:51:57 +00006558#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006559 mpi_free( &ssl->dhm_P );
6560 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006561#endif
6562
Paul Bakker48916f92012-09-16 19:57:18 +00006563 if( ssl->transform )
6564 {
6565 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006566 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006567 }
6568
6569 if( ssl->handshake )
6570 {
6571 ssl_handshake_free( ssl->handshake );
6572 ssl_transform_free( ssl->transform_negotiate );
6573 ssl_session_free( ssl->session_negotiate );
6574
Paul Bakker6e339b52013-07-03 13:37:05 +02006575 polarssl_free( ssl->handshake );
6576 polarssl_free( ssl->transform_negotiate );
6577 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006578 }
6579
Paul Bakkerc0463502013-02-14 11:19:38 +01006580 if( ssl->session )
6581 {
6582 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006583 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006584 }
6585
Paul Bakkera503a632013-08-14 13:48:06 +02006586#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006587 if( ssl->ticket_keys )
6588 {
6589 ssl_ticket_keys_free( ssl->ticket_keys );
6590 polarssl_free( ssl->ticket_keys );
6591 }
Paul Bakkera503a632013-08-14 13:48:06 +02006592#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006593
Paul Bakker0be444a2013-08-27 21:55:01 +02006594#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006595 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 {
Paul Bakker34617722014-06-13 17:20:13 +02006597 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006598 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006599 ssl->hostname_len = 0;
6600 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006601#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006602
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006603#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006604 if( ssl->psk != NULL )
6605 {
Paul Bakker34617722014-06-13 17:20:13 +02006606 polarssl_zeroize( ssl->psk, ssl->psk_len );
6607 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006608 polarssl_free( ssl->psk );
6609 polarssl_free( ssl->psk_identity );
6610 ssl->psk_len = 0;
6611 ssl->psk_identity_len = 0;
6612 }
6613#endif
6614
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006615#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006616 ssl_key_cert_free( ssl->key_cert );
6617#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006618
Paul Bakker05ef8352012-05-08 09:17:57 +00006619#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6620 if( ssl_hw_record_finish != NULL )
6621 {
6622 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6623 ssl_hw_record_finish( ssl );
6624 }
6625#endif
6626
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006627#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006628 polarssl_free( ssl->cli_id );
6629#endif
6630
Paul Bakker5121ce52009-01-03 21:22:43 +00006631 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006632
Paul Bakker86f04f42013-02-14 11:20:09 +01006633 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006634 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006635}
6636
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006637#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006638/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006639 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006640 */
6641unsigned char ssl_sig_from_pk( pk_context *pk )
6642{
6643#if defined(POLARSSL_RSA_C)
6644 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6645 return( SSL_SIG_RSA );
6646#endif
6647#if defined(POLARSSL_ECDSA_C)
6648 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6649 return( SSL_SIG_ECDSA );
6650#endif
6651 return( SSL_SIG_ANON );
6652}
6653
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006654pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6655{
6656 switch( sig )
6657 {
6658#if defined(POLARSSL_RSA_C)
6659 case SSL_SIG_RSA:
6660 return( POLARSSL_PK_RSA );
6661#endif
6662#if defined(POLARSSL_ECDSA_C)
6663 case SSL_SIG_ECDSA:
6664 return( POLARSSL_PK_ECDSA );
6665#endif
6666 default:
6667 return( POLARSSL_PK_NONE );
6668 }
6669}
Paul Bakker9af723c2014-05-01 13:03:14 +02006670#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006671
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006672/*
6673 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6674 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006675md_type_t ssl_md_alg_from_hash( unsigned char hash )
6676{
6677 switch( hash )
6678 {
6679#if defined(POLARSSL_MD5_C)
6680 case SSL_HASH_MD5:
6681 return( POLARSSL_MD_MD5 );
6682#endif
6683#if defined(POLARSSL_SHA1_C)
6684 case SSL_HASH_SHA1:
6685 return( POLARSSL_MD_SHA1 );
6686#endif
6687#if defined(POLARSSL_SHA256_C)
6688 case SSL_HASH_SHA224:
6689 return( POLARSSL_MD_SHA224 );
6690 case SSL_HASH_SHA256:
6691 return( POLARSSL_MD_SHA256 );
6692#endif
6693#if defined(POLARSSL_SHA512_C)
6694 case SSL_HASH_SHA384:
6695 return( POLARSSL_MD_SHA384 );
6696 case SSL_HASH_SHA512:
6697 return( POLARSSL_MD_SHA512 );
6698#endif
6699 default:
6700 return( POLARSSL_MD_NONE );
6701 }
6702}
6703
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006704#if defined(POLARSSL_SSL_SET_CURVES)
6705/*
6706 * Check is a curve proposed by the peer is in our list.
6707 * Return 1 if we're willing to use it, 0 otherwise.
6708 */
6709int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6710{
6711 const ecp_group_id *gid;
6712
6713 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6714 if( *gid == grp_id )
6715 return( 1 );
6716
6717 return( 0 );
6718}
Paul Bakker9af723c2014-05-01 13:03:14 +02006719#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006720
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006721#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006722int ssl_check_cert_usage( const x509_crt *cert,
6723 const ssl_ciphersuite_t *ciphersuite,
6724 int cert_endpoint )
6725{
6726#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6727 int usage = 0;
6728#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006729#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6730 const char *ext_oid;
6731 size_t ext_len;
6732#endif
6733
6734#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6735 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6736 ((void) cert);
6737 ((void) cert_endpoint);
6738#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006739
6740#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6741 if( cert_endpoint == SSL_IS_SERVER )
6742 {
6743 /* Server part of the key exchange */
6744 switch( ciphersuite->key_exchange )
6745 {
6746 case POLARSSL_KEY_EXCHANGE_RSA:
6747 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6748 usage = KU_KEY_ENCIPHERMENT;
6749 break;
6750
6751 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6752 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6753 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6754 usage = KU_DIGITAL_SIGNATURE;
6755 break;
6756
6757 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6758 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6759 usage = KU_KEY_AGREEMENT;
6760 break;
6761
6762 /* Don't use default: we want warnings when adding new values */
6763 case POLARSSL_KEY_EXCHANGE_NONE:
6764 case POLARSSL_KEY_EXCHANGE_PSK:
6765 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6766 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6767 usage = 0;
6768 }
6769 }
6770 else
6771 {
6772 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6773 usage = KU_DIGITAL_SIGNATURE;
6774 }
6775
6776 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6777 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006778#else
6779 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006780#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6781
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006782#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6783 if( cert_endpoint == SSL_IS_SERVER )
6784 {
6785 ext_oid = OID_SERVER_AUTH;
6786 ext_len = OID_SIZE( OID_SERVER_AUTH );
6787 }
6788 else
6789 {
6790 ext_oid = OID_CLIENT_AUTH;
6791 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6792 }
6793
6794 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6795 return( -1 );
6796#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6797
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006798 return( 0 );
6799}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006800#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006801
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006802/*
6803 * Convert version numbers to/from wire format
6804 * and, for DTLS, to/from TLS equivalent.
6805 *
6806 * For TLS this is the identity.
6807 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6808 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6809 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6810 */
6811void ssl_write_version( int major, int minor, int transport,
6812 unsigned char ver[2] )
6813{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006814#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006815 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006816 {
6817 if( minor == SSL_MINOR_VERSION_2 )
6818 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6819
6820 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6821 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6822 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006823 else
6824#else
6825 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006826#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006827 {
6828 ver[0] = (unsigned char) major;
6829 ver[1] = (unsigned char) minor;
6830 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006831}
6832
6833void ssl_read_version( int *major, int *minor, int transport,
6834 const unsigned char ver[2] )
6835{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006836#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006837 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006838 {
6839 *major = 255 - ver[0] + 2;
6840 *minor = 255 - ver[1] + 1;
6841
6842 if( *minor == SSL_MINOR_VERSION_1 )
6843 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6844 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006845 else
6846#else
6847 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006848#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006849 {
6850 *major = ver[0];
6851 *minor = ver[1];
6852 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006853}
6854
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006855#endif /* POLARSSL_SSL_TLS_C */