blob: 454916135a62068e8e2ad984587441e354f3e3f3 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +020082#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083/*
84 * Start a timer.
85 * Passing millisecs = 0 cancels a running timer.
86 * The timer is already running iff time_limit != 0.
87 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020088static void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089{
90 ssl->time_limit = millisecs;
91 get_timer( &ssl->time_info, 1 );
92}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard46fb9422014-10-02 18:10:40 +020097static int ssl_check_timer( ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
99 if( ssl->time_limit != 0 &&
100 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
101 {
102 return( -1 );
103 }
104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200108/*
109 * Double the retransmit timeout value, within the allowed range,
110 * returning -1 if the maximum value has already been reached.
111 */
112static int ssl_double_retransmit_timeout( ssl_context *ssl )
113{
114 uint32_t new_timeout;
115
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200116 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200117 return( -1 );
118
119 new_timeout = 2 * ssl->handshake->retransmit_timeout;
120
121 /* Avoid arithmetic overflow and range overflow */
122 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200123 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200124 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200125 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200126 }
127
128 ssl->handshake->retransmit_timeout = new_timeout;
129 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
130 ssl->handshake->retransmit_timeout ) );
131
132 return( 0 );
133}
134
135static void ssl_reset_retransmit_timeout( ssl_context *ssl )
136{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200137 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200138 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
139 ssl->handshake->retransmit_timeout ) );
140}
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +0200141#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200142
Paul Bakker05decb22013-08-15 13:33:48 +0200143#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200144/*
145 * Convert max_fragment_length codes to length.
146 * RFC 6066 says:
147 * enum{
148 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
149 * } MaxFragmentLength;
150 * and we add 0 -> extension unused
151 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200152static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200153{
154 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
155 512, /* SSL_MAX_FRAG_LEN_512 */
156 1024, /* SSL_MAX_FRAG_LEN_1024 */
157 2048, /* SSL_MAX_FRAG_LEN_2048 */
158 4096, /* SSL_MAX_FRAG_LEN_4096 */
159};
Paul Bakker05decb22013-08-15 13:33:48 +0200160#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200161
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
163{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200164 ssl_session_free( dst );
165 memcpy( dst, src, sizeof( ssl_session ) );
166
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200168 if( src->peer_cert != NULL )
169 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200170 int ret;
171
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
173 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200174 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
175
Paul Bakkerb6b09562013-09-18 14:17:41 +0200176 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200177
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200178 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
179 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180 {
181 polarssl_free( dst->peer_cert );
182 dst->peer_cert = NULL;
183 return( ret );
184 }
185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200187
Paul Bakkera503a632013-08-14 13:48:06 +0200188#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200189 if( src->ticket != NULL )
190 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200191 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
192 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200193 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
194
195 memcpy( dst->ticket, src->ticket, src->ticket_len );
196 }
Paul Bakkera503a632013-08-14 13:48:06 +0200197#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200198
199 return( 0 );
200}
201
Paul Bakker05ef8352012-05-08 09:17:57 +0000202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200203int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200204 const unsigned char *key_enc, const unsigned char *key_dec,
205 size_t keylen,
206 const unsigned char *iv_enc, const unsigned char *iv_dec,
207 size_t ivlen,
208 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200209 size_t maclen ) = NULL;
210int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
211int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
212int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
213int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
214int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200215#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000216
Paul Bakker5121ce52009-01-03 21:22:43 +0000217/*
218 * Key material generation
219 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200220#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200221static int ssl3_prf( const unsigned char *secret, size_t slen,
222 const char *label,
223 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000224 unsigned char *dstbuf, size_t dlen )
225{
226 size_t i;
227 md5_context md5;
228 sha1_context sha1;
229 unsigned char padding[16];
230 unsigned char sha1sum[20];
231 ((void)label);
232
Paul Bakker5b4af392014-06-26 12:09:34 +0200233 md5_init( &md5 );
234 sha1_init( &sha1 );
235
Paul Bakker5f70b252012-09-13 14:23:06 +0000236 /*
237 * SSLv3:
238 * block =
239 * MD5( secret + SHA1( 'A' + secret + random ) ) +
240 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
241 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
242 * ...
243 */
244 for( i = 0; i < dlen / 16; i++ )
245 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200246 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000247
248 sha1_starts( &sha1 );
249 sha1_update( &sha1, padding, 1 + i );
250 sha1_update( &sha1, secret, slen );
251 sha1_update( &sha1, random, rlen );
252 sha1_finish( &sha1, sha1sum );
253
254 md5_starts( &md5 );
255 md5_update( &md5, secret, slen );
256 md5_update( &md5, sha1sum, 20 );
257 md5_finish( &md5, dstbuf + i * 16 );
258 }
259
Paul Bakker5b4af392014-06-26 12:09:34 +0200260 md5_free( &md5 );
261 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000262
Paul Bakker34617722014-06-13 17:20:13 +0200263 polarssl_zeroize( padding, sizeof( padding ) );
264 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000265
266 return( 0 );
267}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200268#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000269
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200270#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200271static int tls1_prf( const unsigned char *secret, size_t slen,
272 const char *label,
273 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000274 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000275{
Paul Bakker23986e52011-04-24 08:57:21 +0000276 size_t nb, hs;
277 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200278 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char tmp[128];
280 unsigned char h_i[20];
281
282 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 hs = ( slen + 1 ) / 2;
286 S1 = secret;
287 S2 = secret + slen - hs;
288
289 nb = strlen( label );
290 memcpy( tmp + 20, label, nb );
291 memcpy( tmp + 20 + nb, random, rlen );
292 nb += rlen;
293
294 /*
295 * First compute P_md5(secret,label+random)[0..dlen]
296 */
297 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
298
299 for( i = 0; i < dlen; i += 16 )
300 {
301 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
302 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
303
304 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
305
306 for( j = 0; j < k; j++ )
307 dstbuf[i + j] = h_i[j];
308 }
309
310 /*
311 * XOR out with P_sha1(secret,label+random)[0..dlen]
312 */
313 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
314
315 for( i = 0; i < dlen; i += 20 )
316 {
317 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
318 sha1_hmac( S2, hs, tmp, 20, tmp );
319
320 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
321
322 for( j = 0; j < k; j++ )
323 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
324 }
325
Paul Bakker34617722014-06-13 17:20:13 +0200326 polarssl_zeroize( tmp, sizeof( tmp ) );
327 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329 return( 0 );
330}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200331#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200333#if defined(POLARSSL_SSL_PROTO_TLS1_2)
334#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200335static int tls_prf_sha256( const unsigned char *secret, size_t slen,
336 const char *label,
337 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000338 unsigned char *dstbuf, size_t dlen )
339{
340 size_t nb;
341 size_t i, j, k;
342 unsigned char tmp[128];
343 unsigned char h_i[32];
344
345 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
346 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
347
348 nb = strlen( label );
349 memcpy( tmp + 32, label, nb );
350 memcpy( tmp + 32 + nb, random, rlen );
351 nb += rlen;
352
353 /*
354 * Compute P_<hash>(secret, label + random)[0..dlen]
355 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200356 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000357
358 for( i = 0; i < dlen; i += 32 )
359 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200360 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
361 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000362
363 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
364
365 for( j = 0; j < k; j++ )
366 dstbuf[i + j] = h_i[j];
367 }
368
Paul Bakker34617722014-06-13 17:20:13 +0200369 polarssl_zeroize( tmp, sizeof( tmp ) );
370 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000371
372 return( 0 );
373}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200374#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker9e36f042013-06-30 14:34:05 +0200376#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200377static int tls_prf_sha384( const unsigned char *secret, size_t slen,
378 const char *label,
379 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000380 unsigned char *dstbuf, size_t dlen )
381{
382 size_t nb;
383 size_t i, j, k;
384 unsigned char tmp[128];
385 unsigned char h_i[48];
386
387 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
388 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
389
390 nb = strlen( label );
391 memcpy( tmp + 48, label, nb );
392 memcpy( tmp + 48 + nb, random, rlen );
393 nb += rlen;
394
395 /*
396 * Compute P_<hash>(secret, label + random)[0..dlen]
397 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200398 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000399
400 for( i = 0; i < dlen; i += 48 )
401 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200402 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
403 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000404
405 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
406
407 for( j = 0; j < k; j++ )
408 dstbuf[i + j] = h_i[j];
409 }
410
Paul Bakker34617722014-06-13 17:20:13 +0200411 polarssl_zeroize( tmp, sizeof( tmp ) );
412 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000413
414 return( 0 );
415}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200416#endif /* POLARSSL_SHA512_C */
417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418
Paul Bakker66d5d072014-06-17 16:39:18 +0200419static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200420
421#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
422 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200423static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#endif
Paul Bakker380da532012-04-18 16:10:25 +0000425
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200427static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
428static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#endif
430
431#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200432static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
433static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434#endif
435
436#if defined(POLARSSL_SSL_PROTO_TLS1_2)
437#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200438static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
439static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
440static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200441#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100442
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200444static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
445static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
446static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100447#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200448#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000449
Paul Bakker5121ce52009-01-03 21:22:43 +0000450int ssl_derive_keys( ssl_context *ssl )
451{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200452 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000453 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 unsigned char keyblk[256];
455 unsigned char *key1;
456 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100457 unsigned char *mac_enc;
458 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200459 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100460 const cipher_info_t *cipher_info;
461 const md_info_t *md_info;
462
Paul Bakker48916f92012-09-16 19:57:18 +0000463 ssl_session *session = ssl->session_negotiate;
464 ssl_transform *transform = ssl->transform_negotiate;
465 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
467 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
468
Paul Bakker68884e32013-01-07 18:20:04 +0100469 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
470 if( cipher_info == NULL )
471 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200472 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100473 transform->ciphersuite_info->cipher ) );
474 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
475 }
476
477 md_info = md_info_from_type( transform->ciphersuite_info->mac );
478 if( md_info == NULL )
479 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200480 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100481 transform->ciphersuite_info->mac ) );
482 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
483 }
484
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000486 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000487 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200488#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000489 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000490 {
Paul Bakker48916f92012-09-16 19:57:18 +0000491 handshake->tls_prf = ssl3_prf;
492 handshake->calc_verify = ssl_calc_verify_ssl;
493 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200495 else
496#endif
497#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
498 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000499 {
Paul Bakker48916f92012-09-16 19:57:18 +0000500 handshake->tls_prf = tls1_prf;
501 handshake->calc_verify = ssl_calc_verify_tls;
502 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000503 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200504 else
505#endif
506#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200507#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200508 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
509 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000510 {
Paul Bakker48916f92012-09-16 19:57:18 +0000511 handshake->tls_prf = tls_prf_sha384;
512 handshake->calc_verify = ssl_calc_verify_tls_sha384;
513 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000514 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000515 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200516#endif
517#if defined(POLARSSL_SHA256_C)
518 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000519 {
Paul Bakker48916f92012-09-16 19:57:18 +0000520 handshake->tls_prf = tls_prf_sha256;
521 handshake->calc_verify = ssl_calc_verify_tls_sha256;
522 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000523 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200524 else
525#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200526#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200527 {
528 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200529 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200530 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000531
532 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * SSLv3:
534 * master =
535 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
536 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
537 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200538 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200539 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 * master = PRF( premaster, "master secret", randbytes )[0..47]
541 */
Paul Bakker0a597072012-09-25 21:55:46 +0000542 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 {
Paul Bakker48916f92012-09-16 19:57:18 +0000544 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
545 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
Paul Bakker48916f92012-09-16 19:57:18 +0000547 handshake->tls_prf( handshake->premaster, handshake->pmslen,
548 "master secret",
549 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
Paul Bakker34617722014-06-13 17:20:13 +0200551 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 }
553 else
554 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
555
556 /*
557 * Swap the client and server random values.
558 */
Paul Bakker48916f92012-09-16 19:57:18 +0000559 memcpy( tmp, handshake->randbytes, 64 );
560 memcpy( handshake->randbytes, tmp + 32, 32 );
561 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200562 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 /*
565 * SSLv3:
566 * key block =
567 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
568 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
569 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
570 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
571 * ...
572 *
573 * TLSv1:
574 * key block = PRF( master, "key expansion", randbytes )
575 */
Paul Bakker48916f92012-09-16 19:57:18 +0000576 handshake->tls_prf( session->master, 48, "key expansion",
577 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Paul Bakker48916f92012-09-16 19:57:18 +0000579 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
580 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
581 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
582 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
584
Paul Bakker34617722014-06-13 17:20:13 +0200585 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 /*
588 * Determine the appropriate key, IV and MAC length.
589 */
Paul Bakker68884e32013-01-07 18:20:04 +0100590
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200591 transform->keylen = cipher_info->key_length / 8;
592
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200593 if( cipher_info->mode == POLARSSL_MODE_GCM ||
594 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200596 transform->maclen = 0;
597
Paul Bakker68884e32013-01-07 18:20:04 +0100598 transform->ivlen = 12;
599 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200600
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200601 /* Minimum length is expicit IV + tag */
602 transform->minlen = transform->ivlen - transform->fixed_ivlen
603 + ( transform->ciphersuite_info->flags &
604 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100605 }
606 else
607 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200608 int ret;
609
610 /* Initialize HMAC contexts */
611 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
612 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100613 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200614 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
615 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100616 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200618 /* Get MAC length */
619 transform->maclen = md_get_size( md_info );
620
621#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
622 /*
623 * If HMAC is to be truncated, we shall keep the leftmost bytes,
624 * (rfc 6066 page 13 or rfc 2104 section 4),
625 * so we only need to adjust the length here.
626 */
627 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
628 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
629#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
630
631 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100632 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200634 /* Minimum length */
635 if( cipher_info->mode == POLARSSL_MODE_STREAM )
636 transform->minlen = transform->maclen;
637 else
Paul Bakker68884e32013-01-07 18:20:04 +0100638 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200639 /*
640 * GenericBlockCipher:
641 * first multiple of blocklen greater than maclen
642 * + IV except for SSL3 and TLS 1.0
643 */
644 transform->minlen = transform->maclen
645 + cipher_info->block_size
646 - transform->maclen % cipher_info->block_size;
647
648#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
649 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
650 ssl->minor_ver == SSL_MINOR_VERSION_1 )
651 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100652 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200653#endif
654#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
655 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
656 ssl->minor_ver == SSL_MINOR_VERSION_3 )
657 {
658 transform->minlen += transform->ivlen;
659 }
660 else
661#endif
662 {
663 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
664 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
665 }
Paul Bakker68884e32013-01-07 18:20:04 +0100666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668
669 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000670 transform->keylen, transform->minlen, transform->ivlen,
671 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 /*
674 * Finally setup the cipher contexts, IVs and MAC secrets.
675 */
676 if( ssl->endpoint == SSL_IS_CLIENT )
677 {
Paul Bakker48916f92012-09-16 19:57:18 +0000678 key1 = keyblk + transform->maclen * 2;
679 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000680
Paul Bakker68884e32013-01-07 18:20:04 +0100681 mac_enc = keyblk;
682 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000684 /*
685 * This is not used in TLS v1.1.
686 */
Paul Bakker48916f92012-09-16 19:57:18 +0000687 iv_copy_len = ( transform->fixed_ivlen ) ?
688 transform->fixed_ivlen : transform->ivlen;
689 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
690 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000691 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 }
693 else
694 {
Paul Bakker48916f92012-09-16 19:57:18 +0000695 key1 = keyblk + transform->maclen * 2 + transform->keylen;
696 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000697
Paul Bakker68884e32013-01-07 18:20:04 +0100698 mac_enc = keyblk + transform->maclen;
699 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000701 /*
702 * This is not used in TLS v1.1.
703 */
Paul Bakker48916f92012-09-16 19:57:18 +0000704 iv_copy_len = ( transform->fixed_ivlen ) ?
705 transform->fixed_ivlen : transform->ivlen;
706 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
707 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000708 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709 }
710
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200711#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100712 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
713 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100714 if( transform->maclen > sizeof transform->mac_enc )
715 {
716 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200717 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100718 }
719
Paul Bakker68884e32013-01-07 18:20:04 +0100720 memcpy( transform->mac_enc, mac_enc, transform->maclen );
721 memcpy( transform->mac_dec, mac_dec, transform->maclen );
722 }
723 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200724#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200725#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
726 defined(POLARSSL_SSL_PROTO_TLS1_2)
727 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100728 {
729 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
730 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200732 else
733#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200734 {
735 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200736 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200737 }
Paul Bakker68884e32013-01-07 18:20:04 +0100738
Paul Bakker05ef8352012-05-08 09:17:57 +0000739#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200740 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000741 {
742 int ret = 0;
743
744 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
745
Paul Bakker07eb38b2012-12-19 14:42:06 +0100746 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
747 transform->iv_enc, transform->iv_dec,
748 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100749 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100750 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000751 {
752 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200753 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000754 }
755 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200756#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000757
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200758 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
759 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200761 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
762 return( ret );
763 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200764
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200765 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
766 cipher_info ) ) != 0 )
767 {
768 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
769 return( ret );
770 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200772 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
773 cipher_info->key_length,
774 POLARSSL_ENCRYPT ) ) != 0 )
775 {
776 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
777 return( ret );
778 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200779
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200780 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
781 cipher_info->key_length,
782 POLARSSL_DECRYPT ) ) != 0 )
783 {
784 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
785 return( ret );
786 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200787
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200788#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200789 if( cipher_info->mode == POLARSSL_MODE_CBC )
790 {
791 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
792 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200793 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200794 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
795 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200796 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200797
798 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
799 POLARSSL_PADDING_NONE ) ) != 0 )
800 {
801 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
802 return( ret );
803 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200805#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
Paul Bakker34617722014-06-13 17:20:13 +0200807 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Paul Bakker2770fbd2012-07-03 13:30:23 +0000809#if defined(POLARSSL_ZLIB_SUPPORT)
810 // Initialize compression
811 //
Paul Bakker48916f92012-09-16 19:57:18 +0000812 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000813 {
Paul Bakker16770332013-10-11 09:59:44 +0200814 if( ssl->compress_buf == NULL )
815 {
816 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
817 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
818 if( ssl->compress_buf == NULL )
819 {
820 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
821 SSL_BUFFER_LEN ) );
822 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
823 }
824 }
825
Paul Bakker2770fbd2012-07-03 13:30:23 +0000826 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
827
Paul Bakker48916f92012-09-16 19:57:18 +0000828 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
829 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000830
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200831 if( deflateInit( &transform->ctx_deflate,
832 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000833 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000834 {
835 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
836 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
837 }
838 }
839#endif /* POLARSSL_ZLIB_SUPPORT */
840
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
842
843 return( 0 );
844}
845
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200846#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000847void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000848{
849 md5_context md5;
850 sha1_context sha1;
851 unsigned char pad_1[48];
852 unsigned char pad_2[48];
853
Paul Bakker380da532012-04-18 16:10:25 +0000854 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker48916f92012-09-16 19:57:18 +0000856 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
857 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker380da532012-04-18 16:10:25 +0000859 memset( pad_1, 0x36, 48 );
860 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Paul Bakker48916f92012-09-16 19:57:18 +0000862 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000863 md5_update( &md5, pad_1, 48 );
864 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000865
Paul Bakker380da532012-04-18 16:10:25 +0000866 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000867 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000868 md5_update( &md5, pad_2, 48 );
869 md5_update( &md5, hash, 16 );
870 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Paul Bakker48916f92012-09-16 19:57:18 +0000872 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000873 sha1_update( &sha1, pad_1, 40 );
874 sha1_finish( &sha1, hash + 16 );
875
876 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000877 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_update( &sha1, pad_2, 40 );
879 sha1_update( &sha1, hash + 16, 20 );
880 sha1_finish( &sha1, hash + 16 );
881
882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 md5_free( &md5 );
886 sha1_free( &sha1 );
887
Paul Bakker380da532012-04-18 16:10:25 +0000888 return;
889}
Paul Bakker9af723c2014-05-01 13:03:14 +0200890#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000891
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200892#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000893void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
894{
895 md5_context md5;
896 sha1_context sha1;
897
898 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
899
Paul Bakker48916f92012-09-16 19:57:18 +0000900 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
901 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000902
Paul Bakker48916f92012-09-16 19:57:18 +0000903 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000904 sha1_finish( &sha1, hash + 16 );
905
906 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
907 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
908
Paul Bakker5b4af392014-06-26 12:09:34 +0200909 md5_free( &md5 );
910 sha1_free( &sha1 );
911
Paul Bakker380da532012-04-18 16:10:25 +0000912 return;
913}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200914#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000915
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#if defined(POLARSSL_SSL_PROTO_TLS1_2)
917#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000918void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
919{
Paul Bakker9e36f042013-06-30 14:34:05 +0200920 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000921
922 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
923
Paul Bakker9e36f042013-06-30 14:34:05 +0200924 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
925 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000926
927 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
928 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
929
Paul Bakker5b4af392014-06-26 12:09:34 +0200930 sha256_free( &sha256 );
931
Paul Bakker380da532012-04-18 16:10:25 +0000932 return;
933}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200934#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000935
Paul Bakker9e36f042013-06-30 14:34:05 +0200936#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000937void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
938{
Paul Bakker9e36f042013-06-30 14:34:05 +0200939 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000940
941 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
942
Paul Bakker9e36f042013-06-30 14:34:05 +0200943 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
944 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000945
Paul Bakkerca4ab492012-04-18 14:23:57 +0000946 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
948
Paul Bakker5b4af392014-06-26 12:09:34 +0200949 sha512_free( &sha512 );
950
Paul Bakker5121ce52009-01-03 21:22:43 +0000951 return;
952}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200953#endif /* POLARSSL_SHA512_C */
954#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200956#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
958{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200959 unsigned char *p = ssl->handshake->premaster;
960 unsigned char *end = p + sizeof( ssl->handshake->premaster );
961
962 /*
963 * PMS = struct {
964 * opaque other_secret<0..2^16-1>;
965 * opaque psk<0..2^16-1>;
966 * };
967 * with "other_secret" depending on the particular key exchange
968 */
969#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
970 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
971 {
972 if( end - p < 2 + (int) ssl->psk_len )
973 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
974
975 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
976 *(p++) = (unsigned char)( ssl->psk_len );
977 p += ssl->psk_len;
978 }
979 else
980#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200981#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
982 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
983 {
984 /*
985 * other_secret already set by the ClientKeyExchange message,
986 * and is 48 bytes long
987 */
988 *p++ = 0;
989 *p++ = 48;
990 p += 48;
991 }
992 else
993#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
995 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
996 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200997 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200998 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200999
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001000 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001001 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001002 p + 2, &len,
1003 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 {
1005 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1006 return( ret );
1007 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001008 *(p++) = (unsigned char)( len >> 8 );
1009 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 p += len;
1011
1012 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1013 }
1014 else
1015#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1016#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1017 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1018 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001019 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020 size_t zlen;
1021
1022 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001023 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001024 ssl->f_rng, ssl->p_rng ) ) != 0 )
1025 {
1026 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1027 return( ret );
1028 }
1029
1030 *(p++) = (unsigned char)( zlen >> 8 );
1031 *(p++) = (unsigned char)( zlen );
1032 p += zlen;
1033
1034 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1035 }
1036 else
1037#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1038 {
1039 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001040 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001041 }
1042
1043 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001044 if( end - p < 2 + (int) ssl->psk_len )
1045 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1046
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001047 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1048 *(p++) = (unsigned char)( ssl->psk_len );
1049 memcpy( p, ssl->psk, ssl->psk_len );
1050 p += ssl->psk_len;
1051
1052 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1053
1054 return( 0 );
1055}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001056#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001057
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001058#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001059/*
1060 * SSLv3.0 MAC functions
1061 */
Paul Bakker68884e32013-01-07 18:20:04 +01001062static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1063 unsigned char *buf, size_t len,
1064 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001065{
1066 unsigned char header[11];
1067 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001068 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001069 int md_size = md_get_size( md_ctx->md_info );
1070 int md_type = md_get_type( md_ctx->md_info );
1071
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001072 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001073 if( md_type == POLARSSL_MD_MD5 )
1074 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001075 else
Paul Bakker68884e32013-01-07 18:20:04 +01001076 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001077
1078 memcpy( header, ctr, 8 );
1079 header[ 8] = (unsigned char) type;
1080 header[ 9] = (unsigned char)( len >> 8 );
1081 header[10] = (unsigned char)( len );
1082
Paul Bakker68884e32013-01-07 18:20:04 +01001083 memset( padding, 0x36, padlen );
1084 md_starts( md_ctx );
1085 md_update( md_ctx, secret, md_size );
1086 md_update( md_ctx, padding, padlen );
1087 md_update( md_ctx, header, 11 );
1088 md_update( md_ctx, buf, len );
1089 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakker68884e32013-01-07 18:20:04 +01001091 memset( padding, 0x5C, padlen );
1092 md_starts( md_ctx );
1093 md_update( md_ctx, secret, md_size );
1094 md_update( md_ctx, padding, padlen );
1095 md_update( md_ctx, buf + len, md_size );
1096 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001097}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001098#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001099
Paul Bakker5121ce52009-01-03 21:22:43 +00001100/*
1101 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001102 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001103static int ssl_encrypt_buf( ssl_context *ssl )
1104{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001105 const cipher_mode_t mode = cipher_get_cipher_mode(
1106 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
1110 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001111 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001113#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1114 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1115 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001116 if( mode != POLARSSL_MODE_GCM &&
1117 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001119#if defined(POLARSSL_SSL_PROTO_SSL3)
1120 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1121 {
1122 ssl_mac( &ssl->transform_out->md_ctx_enc,
1123 ssl->transform_out->mac_enc,
1124 ssl->out_msg, ssl->out_msglen,
1125 ssl->out_ctr, ssl->out_msgtype );
1126 }
1127 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001128#endif
1129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001130 defined(POLARSSL_SSL_PROTO_TLS1_2)
1131 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1132 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001133 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001136 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg, ssl->out_msglen );
1138 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg + ssl->out_msglen );
1140 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1141 }
1142 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001143#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001144 {
1145 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 }
1148
1149 SSL_DEBUG_BUF( 4, "computed mac",
1150 ssl->out_msg + ssl->out_msglen,
1151 ssl->transform_out->maclen );
1152
1153 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001154 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001155#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 /*
1158 * Encrypt
1159 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001161 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001163 int ret;
1164 size_t olen = 0;
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1167 "including %d bytes of padding",
1168 ssl->out_msglen, 0 ) );
1169
1170 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1171 ssl->out_msg, ssl->out_msglen );
1172
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 ssl->transform_out->ivlen,
1176 ssl->out_msg, ssl->out_msglen,
1177 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001178 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001179 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180 return( ret );
1181 }
1182
1183 if( ssl->out_msglen != olen )
1184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1192 if( mode == POLARSSL_MODE_GCM ||
1193 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 unsigned char *enc_msg;
1198 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1200 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 memcpy( add_data, ssl->out_ctr, 8 );
1203 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001204 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1205 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1207 add_data[12] = ssl->out_msglen & 0xFF;
1208
1209 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1210 add_data, 13 );
1211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
1213 * Generate IV
1214 */
1215 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001216 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1217 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001218 if( ret != 0 )
1219 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Paul Bakker68884e32013-01-07 18:20:04 +01001221 memcpy( ssl->out_iv,
1222 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1223 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001224
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001225 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001226 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001227
Paul Bakker68884e32013-01-07 18:20:04 +01001228 /*
1229 * Fix pointer positions and message length with added IV
1230 */
1231 enc_msg = ssl->out_msg;
1232 enc_msglen = ssl->out_msglen;
1233 ssl->out_msglen += ssl->transform_out->ivlen -
1234 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001235
Paul Bakker68884e32013-01-07 18:20:04 +01001236 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1237 "including %d bytes of padding",
1238 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1241 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Paul Bakker68884e32013-01-07 18:20:04 +01001243 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001244 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001245 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001246 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1247 ssl->transform_out->iv_enc,
1248 ssl->transform_out->ivlen,
1249 add_data, 13,
1250 enc_msg, enc_msglen,
1251 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001252 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001253 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 return( ret );
1256 }
1257
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001258 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001259 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001260 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001261 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 }
1263
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001265
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001266 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001270#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1271 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001272 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001274 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001275 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001276 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001277
Paul Bakker48916f92012-09-16 19:57:18 +00001278 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1279 ssl->transform_out->ivlen;
1280 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 padlen = 0;
1282
1283 for( i = 0; i <= padlen; i++ )
1284 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1285
1286 ssl->out_msglen += padlen + 1;
1287
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288 enc_msglen = ssl->out_msglen;
1289 enc_msg = ssl->out_msg;
1290
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001291#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001293 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1294 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001296 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001297 {
1298 /*
1299 * Generate IV
1300 */
Paul Bakker48916f92012-09-16 19:57:18 +00001301 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1302 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001303 if( ret != 0 )
1304 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305
Paul Bakker92be97b2013-01-02 17:30:03 +01001306 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001307 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308
1309 /*
1310 * Fix pointer positions and message length with added IV
1311 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001312 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001316#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001320 ssl->out_msglen, ssl->transform_out->ivlen,
1321 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001324 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001327 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001328 ssl->transform_out->ivlen,
1329 enc_msg, enc_msglen,
1330 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001333 return( ret );
1334 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001335
Paul Bakkercca5b812013-08-31 17:40:26 +02001336 if( enc_msglen != olen )
1337 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001341
1342#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1344 {
1345 /*
1346 * Save IV in SSL3 and TLS1
1347 */
1348 memcpy( ssl->transform_out->iv_enc,
1349 ssl->transform_out->cipher_ctx_enc.iv,
1350 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001354 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001355#endif /* POLARSSL_CIPHER_MODE_CBC &&
1356 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001357 {
1358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1363
1364 return( 0 );
1365}
1366
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001367#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001368
Paul Bakker5121ce52009-01-03 21:22:43 +00001369static int ssl_decrypt_buf( ssl_context *ssl )
1370{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001371 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001372 const cipher_mode_t mode = cipher_get_cipher_mode(
1373 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001374#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1375 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1376 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1377 size_t padlen = 0, correct = 1;
1378#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001379
1380 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 {
1384 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001385 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001386 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 }
1388
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001389#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001390 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001392 int ret;
1393 size_t olen = 0;
1394
Paul Bakker68884e32013-01-07 18:20:04 +01001395 padlen = 0;
1396
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001397 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001398 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001399 ssl->transform_in->ivlen,
1400 ssl->in_msg, ssl->in_msglen,
1401 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001402 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001403 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001404 return( ret );
1405 }
1406
1407 if( ssl->in_msglen != olen )
1408 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001409 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001410 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001411 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001412 }
Paul Bakker68884e32013-01-07 18:20:04 +01001413 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001414#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001415#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1416 if( mode == POLARSSL_MODE_GCM ||
1417 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001418 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001419 int ret;
1420 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 unsigned char *dec_msg;
1422 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1425 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001426 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1427 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001428
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001429 if( ssl->in_msglen < explicit_iv_len + taglen )
1430 {
1431 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1432 "+ taglen (%d)", ssl->in_msglen,
1433 explicit_iv_len, taglen ) );
1434 return( POLARSSL_ERR_SSL_INVALID_MAC );
1435 }
1436 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1437
Paul Bakker68884e32013-01-07 18:20:04 +01001438 dec_msg = ssl->in_msg;
1439 dec_msg_result = ssl->in_msg;
1440 ssl->in_msglen = dec_msglen;
1441
1442 memcpy( add_data, ssl->in_ctr, 8 );
1443 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001444 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1445 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001446 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1447 add_data[12] = ssl->in_msglen & 0xFF;
1448
1449 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1450 add_data, 13 );
1451
1452 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1453 ssl->in_iv,
1454 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1455
1456 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1457 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001458 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001459
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001460 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001461 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001463 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1464 ssl->transform_in->iv_dec,
1465 ssl->transform_in->ivlen,
1466 add_data, 13,
1467 dec_msg, dec_msglen,
1468 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001469 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001470 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001471 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1472
1473 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1474 return( POLARSSL_ERR_SSL_INVALID_MAC );
1475
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001476 return( ret );
1477 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001478
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001479 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001480 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001481 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001482 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001483 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001486#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001487#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1488 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001489 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001490 {
Paul Bakker45829992013-01-03 14:52:21 +01001491 /*
1492 * Decrypt and check the padding
1493 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001494 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001495 unsigned char *dec_msg;
1496 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001497 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001498 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001499 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 /*
Paul Bakker45829992013-01-03 14:52:21 +01001502 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 */
Paul Bakker48916f92012-09-16 19:57:18 +00001504 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 {
1506 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001507 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001508 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 }
1510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001512 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1513 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001514#endif
Paul Bakker45829992013-01-03 14:52:21 +01001515
1516 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1517 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1518 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001519 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1520 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1521 ssl->transform_in->ivlen,
1522 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001523 return( POLARSSL_ERR_SSL_INVALID_MAC );
1524 }
1525
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001526 dec_msglen = ssl->in_msglen;
1527 dec_msg = ssl->in_msg;
1528 dec_msg_result = ssl->in_msg;
1529
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001530#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001531 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001532 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001533 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001534 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001535 {
Paul Bakker48916f92012-09-16 19:57:18 +00001536 dec_msglen -= ssl->transform_in->ivlen;
1537 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538
Paul Bakker48916f92012-09-16 19:57:18 +00001539 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001540 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001542#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001544 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001545 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001546 ssl->transform_in->ivlen,
1547 dec_msg, dec_msglen,
1548 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001549 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001550 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001551 return( ret );
1552 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001553
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 if( dec_msglen != olen )
1555 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001556 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001557 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001558 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559
1560#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1562 {
1563 /*
1564 * Save IV in SSL3 and TLS1
1565 */
1566 memcpy( ssl->transform_in->iv_dec,
1567 ssl->transform_in->cipher_ctx_dec.iv,
1568 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001570#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001571
1572 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001573
1574 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1575 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001577 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1578 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001579#endif
Paul Bakker45829992013-01-03 14:52:21 +01001580 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001581 correct = 0;
1582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001584#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001585 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1586 {
Paul Bakker48916f92012-09-16 19:57:18 +00001587 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001589#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1591 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001592 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001593#endif
Paul Bakker45829992013-01-03 14:52:21 +01001594 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
1596 }
1597 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001598#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001599#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1600 defined(POLARSSL_SSL_PROTO_TLS1_2)
1601 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 {
1603 /*
Paul Bakker45829992013-01-03 14:52:21 +01001604 * TLSv1+: always check the padding up to the first failure
1605 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001607 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001608 size_t padding_idx = ssl->in_msglen - padlen - 1;
1609
Paul Bakker956c9e02013-12-19 14:42:28 +01001610 /*
1611 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001612 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001613 *
Paul Bakker61885c72014-04-25 12:59:03 +02001614 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1615 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001616 *
1617 * In both cases we reset padding_idx to a safe value (0) to
1618 * prevent out-of-buffer reads.
1619 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001620 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001621 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1622 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001623
1624 padding_idx *= correct;
1625
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001626 for( i = 1; i <= 256; i++ )
1627 {
1628 real_count &= ( i <= padlen );
1629 pad_count += real_count *
1630 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1631 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001632
1633 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001634
Paul Bakkerd66f0702013-01-31 16:57:45 +01001635#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001636 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001637 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001638#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001639 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001641 else
1642#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1643 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001644 {
1645 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001646 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001649 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001650#endif /* POLARSSL_CIPHER_MODE_CBC &&
1651 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001652 {
1653 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001654 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1658 ssl->in_msg, ssl->in_msglen );
1659
1660 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001661 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1664 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1665 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001666 if( mode != POLARSSL_MODE_GCM &&
1667 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001669 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1670
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001673 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1674 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001678#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1680 {
1681 ssl_mac( &ssl->transform_in->md_ctx_dec,
1682 ssl->transform_in->mac_dec,
1683 ssl->in_msg, ssl->in_msglen,
1684 ssl->in_ctr, ssl->in_msgtype );
1685 }
1686 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001687#endif /* POLARSSL_SSL_PROTO_SSL3 */
1688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001689 defined(POLARSSL_SSL_PROTO_TLS1_2)
1690 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1691 {
1692 /*
1693 * Process MAC and always update for padlen afterwards to make
1694 * total time independent of padlen
1695 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001696 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001697 *
1698 * Known timing attacks:
1699 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1700 *
1701 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1702 * correctly. (We round down instead of up, so -56 is the correct
1703 * value for our calculations instead of -55)
1704 */
1705 size_t j, extra_run = 0;
1706 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1707 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001708
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001709 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001710
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001711 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1712 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1713 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001714 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1715 ssl->in_msglen );
1716 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1717 ssl->in_msg + ssl->in_msglen );
1718 for( j = 0; j < extra_run; j++ )
1719 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1722 }
1723 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001724#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001725 POLARSSL_SSL_PROTO_TLS1_2 */
1726 {
1727 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001728 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001729 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001731 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1732 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1733 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001735 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001736 ssl->transform_in->maclen ) != 0 )
1737 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001738#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001740#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001741 correct = 0;
1742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 /*
1745 * Finally check the correct flag
1746 */
1747 if( correct == 0 )
1748 return( POLARSSL_ERR_SSL_INVALID_MAC );
1749 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001750#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001751
1752 if( ssl->in_msglen == 0 )
1753 {
1754 ssl->nb_zero++;
1755
1756 /*
1757 * Three or more empty messages may be a DoS attack
1758 * (excessive CPU consumption).
1759 */
1760 if( ssl->nb_zero > 3 )
1761 {
1762 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1763 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001764 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765 }
1766 }
1767 else
1768 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001769
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001770#if defined(POLARSSL_SSL_PROTO_DTLS)
1771 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001772 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001773 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001774 }
1775 else
1776#endif
1777 {
1778 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1779 if( ++ssl->in_ctr[i - 1] != 0 )
1780 break;
1781
1782 /* The loop goes to its end iff the counter is wrapping */
1783 if( i == ssl_ep_len( ssl ) )
1784 {
1785 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1786 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1787 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001788 }
1789
Paul Bakker5121ce52009-01-03 21:22:43 +00001790 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1791
1792 return( 0 );
1793}
1794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795#if defined(POLARSSL_ZLIB_SUPPORT)
1796/*
1797 * Compression/decompression functions
1798 */
1799static int ssl_compress_buf( ssl_context *ssl )
1800{
1801 int ret;
1802 unsigned char *msg_post = ssl->out_msg;
1803 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001804 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805
1806 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1807
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001808 if( len_pre == 0 )
1809 return( 0 );
1810
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811 memcpy( msg_pre, ssl->out_msg, len_pre );
1812
1813 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1814 ssl->out_msglen ) );
1815
1816 SSL_DEBUG_BUF( 4, "before compression: output payload",
1817 ssl->out_msg, ssl->out_msglen );
1818
Paul Bakker48916f92012-09-16 19:57:18 +00001819 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1820 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1821 ssl->transform_out->ctx_deflate.next_out = msg_post;
1822 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001823
Paul Bakker48916f92012-09-16 19:57:18 +00001824 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001825 if( ret != Z_OK )
1826 {
1827 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1828 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1829 }
1830
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001831 ssl->out_msglen = SSL_BUFFER_LEN -
1832 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833
Paul Bakker2770fbd2012-07-03 13:30:23 +00001834 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1835 ssl->out_msglen ) );
1836
1837 SSL_DEBUG_BUF( 4, "after compression: output payload",
1838 ssl->out_msg, ssl->out_msglen );
1839
1840 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1841
1842 return( 0 );
1843}
1844
1845static int ssl_decompress_buf( ssl_context *ssl )
1846{
1847 int ret;
1848 unsigned char *msg_post = ssl->in_msg;
1849 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001850 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851
1852 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1853
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001854 if( len_pre == 0 )
1855 return( 0 );
1856
Paul Bakker2770fbd2012-07-03 13:30:23 +00001857 memcpy( msg_pre, ssl->in_msg, len_pre );
1858
1859 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1860 ssl->in_msglen ) );
1861
1862 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1863 ssl->in_msg, ssl->in_msglen );
1864
Paul Bakker48916f92012-09-16 19:57:18 +00001865 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1866 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1867 ssl->transform_in->ctx_inflate.next_out = msg_post;
1868 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001869
Paul Bakker48916f92012-09-16 19:57:18 +00001870 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001871 if( ret != Z_OK )
1872 {
1873 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1874 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1875 }
1876
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001877 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1878 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879
Paul Bakker2770fbd2012-07-03 13:30:23 +00001880 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1881 ssl->in_msglen ) );
1882
1883 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1884 ssl->in_msg, ssl->in_msglen );
1885
1886 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1887
1888 return( 0 );
1889}
1890#endif /* POLARSSL_ZLIB_SUPPORT */
1891
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001892#if defined(POLARSSL_SSL_SRV_C)
1893static int ssl_write_hello_request( ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02001894
1895#if defined(POLARSSL_SSL_PROTO_DTLS)
1896static int ssl_resend_hello_request( ssl_context *ssl )
1897{
1898 /* If renegotiation is not enforced, retransmit until we would reach max
1899 * timeout if we were using the usual handshake doubling scheme */
1900 if( ssl->renego_max_records < 0 )
1901 {
1902 uint32_t ratio = ssl->hs_timeout_max / ssl->hs_timeout_min + 1;
1903 unsigned char doublings = 1;
1904
1905 while( ratio != 0 )
1906 {
1907 ++doublings;
1908 ratio >>= 1;
1909 }
1910
1911 if( ++ssl->renego_records_seen > doublings )
1912 {
1913 SSL_DEBUG_MSG( 0, ( "no longer retransmitting hello request" ) );
1914 return( 0 );
1915 }
1916 }
1917
1918 return( ssl_write_hello_request( ssl ) );
1919}
1920#endif
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02001921#endif
1922
Paul Bakker5121ce52009-01-03 21:22:43 +00001923/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001924 * Fill the input message buffer by appending data to it.
1925 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001926 *
1927 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1928 * available (from this read and/or a previous one). Otherwise, an error code
1929 * is returned (possibly EOF or WANT_READ).
1930 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001931 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1932 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1933 * since we always read a whole datagram at once.
1934 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001935 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001936 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 */
Paul Bakker23986e52011-04-24 08:57:21 +00001938int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001939{
Paul Bakker23986e52011-04-24 08:57:21 +00001940 int ret;
1941 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1944
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001945 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1946 {
1947 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1948 "or ssl_set_bio_timeout()" ) );
1949 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1950 }
1951
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001952 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001953 {
1954 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1956 }
1957
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001958#if defined(POLARSSL_SSL_PROTO_DTLS)
1959 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001961 uint32_t timeout;
1962
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001963 /*
1964 * The point is, we need to always read a full datagram at once, so we
1965 * sometimes read more then requested, and handle the additional data.
1966 * It could be the rest of the current record (while fetching the
1967 * header) and/or some other records in the same datagram.
1968 */
1969
1970 /*
1971 * Move to the next record in the already read datagram if applicable
1972 */
1973 if( ssl->next_record_offset != 0 )
1974 {
1975 if( ssl->in_left < ssl->next_record_offset )
1976 {
1977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1979 }
1980
1981 ssl->in_left -= ssl->next_record_offset;
1982
1983 if( ssl->in_left != 0 )
1984 {
1985 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1986 ssl->next_record_offset ) );
1987 memmove( ssl->in_hdr,
1988 ssl->in_hdr + ssl->next_record_offset,
1989 ssl->in_left );
1990 }
1991
1992 ssl->next_record_offset = 0;
1993 }
1994
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1996 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001997
1998 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001999 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002000 */
2001 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002002 {
2003 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002004 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002005 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002006
2007 /*
2008 * A record can't be split accross datagrams. If we need to read but
2009 * are not at the beginning of a new record, the caller did something
2010 * wrong.
2011 */
2012 if( ssl->in_left != 0 )
2013 {
2014 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2015 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2016 }
2017
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002018 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002019
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002020 /*
2021 * Don't even try to read if time's out already.
2022 * This avoids by-passing the timer when repeatedly receiving messages
2023 * that will end up being dropped.
2024 */
2025 if( ssl_check_timer( ssl ) != 0 )
2026 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002027 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002028 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002029 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2030
2031 if( ssl->state != SSL_HANDSHAKE_OVER )
2032 timeout = ssl->handshake->retransmit_timeout;
2033 else
2034 timeout = ssl->read_timeout;
2035
2036 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2037
2038 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2039 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2040 timeout );
2041 else
2042 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2043
2044 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2045
2046 if( ret == 0 )
2047 return( POLARSSL_ERR_SSL_CONN_EOF );
2048 }
2049
2050 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2051 {
2052 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2053 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002054
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002055 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002056 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002057 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2058 {
2059 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2060 return( POLARSSL_ERR_NET_TIMEOUT );
2061 }
2062
2063 if( ( ret = ssl_resend( ssl ) ) != 0 )
2064 {
2065 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2066 return( ret );
2067 }
2068
2069 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002070 }
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002071#if defined(POLARSSL_SSL_SRV_C)
2072 else if( ssl->endpoint == SSL_IS_SERVER &&
2073 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
2074 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002075 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002076 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02002077 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02002078 return( ret );
2079 }
2080
2081 return( POLARSSL_ERR_NET_WANT_READ );
2082 }
2083#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002084 }
2085
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 if( ret < 0 )
2087 return( ret );
2088
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002089 ssl->in_left = ret;
2090 }
2091 else
2092#endif
2093 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002094 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2095 ssl->in_left, nb_want ) );
2096
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002097 while( ssl->in_left < nb_want )
2098 {
2099 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002100 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002101
2102 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2103 ssl->in_left, nb_want ) );
2104 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2105
2106 if( ret == 0 )
2107 return( POLARSSL_ERR_SSL_CONN_EOF );
2108
2109 if( ret < 0 )
2110 return( ret );
2111
2112 ssl->in_left += ret;
2113 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 }
2115
2116 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2117
2118 return( 0 );
2119}
2120
2121/*
2122 * Flush any data not yet written
2123 */
2124int ssl_flush_output( ssl_context *ssl )
2125{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002126 int ret;
2127 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002128
2129 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2130
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002131 if( ssl->f_send == NULL )
2132 {
2133 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2134 "or ssl_set_bio_timeout()" ) );
2135 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2136 }
2137
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002138 /* Avoid incrementing counter if data is flushed */
2139 if( ssl->out_left == 0 )
2140 {
2141 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2142 return( 0 );
2143 }
2144
Paul Bakker5121ce52009-01-03 21:22:43 +00002145 while( ssl->out_left > 0 )
2146 {
2147 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002148 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002149
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002150 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2151 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002152 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002153
Paul Bakker5121ce52009-01-03 21:22:43 +00002154 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2155
2156 if( ret <= 0 )
2157 return( ret );
2158
2159 ssl->out_left -= ret;
2160 }
2161
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002162 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002163 if( ++ssl->out_ctr[i - 1] != 0 )
2164 break;
2165
2166 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002167 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002168 {
2169 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2170 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2171 }
2172
Paul Bakker5121ce52009-01-03 21:22:43 +00002173 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2174
2175 return( 0 );
2176}
2177
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002178/*
2179 * Functions to handle the DTLS retransmission state machine
2180 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002181#if defined(POLARSSL_SSL_PROTO_DTLS)
2182/*
2183 * Append current handshake message to current outgoing flight
2184 */
2185static int ssl_flight_append( ssl_context *ssl )
2186{
2187 ssl_flight_item *msg;
2188
2189 /* Allocate space for current message */
2190 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2191 {
2192 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2193 sizeof( ssl_flight_item ) ) );
2194 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2195 }
2196
2197 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2198 {
2199 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard6b875fc2014-10-17 14:02:33 +02002200 polarssl_free( msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002201 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2202 }
2203
2204 /* Copy current handshake message with headers */
2205 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2206 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002207 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002208 msg->next = NULL;
2209
2210 /* Append to the current flight */
2211 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002212 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002213 else
2214 {
2215 ssl_flight_item *cur = ssl->handshake->flight;
2216 while( cur->next != NULL )
2217 cur = cur->next;
2218 cur->next = msg;
2219 }
2220
2221 return( 0 );
2222}
2223
2224/*
2225 * Free the current flight of handshake messages
2226 */
2227static void ssl_flight_free( ssl_flight_item *flight )
2228{
2229 ssl_flight_item *cur = flight;
2230 ssl_flight_item *next;
2231
2232 while( cur != NULL )
2233 {
2234 next = cur->next;
2235
2236 polarssl_free( cur->p );
2237 polarssl_free( cur );
2238
2239 cur = next;
2240 }
2241}
2242
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002243#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2244static void ssl_dtls_replay_reset( ssl_context *ssl );
2245#endif
2246
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002247/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002248 * Swap transform_out and out_ctr with the alternative ones
2249 */
2250static void ssl_swap_epochs( ssl_context *ssl )
2251{
2252 ssl_transform *tmp_transform;
2253 unsigned char tmp_out_ctr[8];
2254
2255 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2256 {
2257 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2258 return;
2259 }
2260
2261 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2262
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002263 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002264 tmp_transform = ssl->transform_out;
2265 ssl->transform_out = ssl->handshake->alt_transform_out;
2266 ssl->handshake->alt_transform_out = tmp_transform;
2267
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002268 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002269 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2270 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2271 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002272
2273 /* Adjust to the newly activated transform */
2274 if( ssl->transform_out != NULL &&
2275 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2276 {
2277 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2278 ssl->transform_out->fixed_ivlen;
2279 }
2280 else
2281 ssl->out_msg = ssl->out_iv;
2282
2283#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2284 if( ssl_hw_record_activate != NULL )
2285 {
2286 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2287 {
2288 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2289 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2290 }
2291 }
2292#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002293}
2294
2295/*
2296 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002297 *
2298 * Need to remember the current message in case flush_output returns
2299 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002300 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002301 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002302int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002303{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002304 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002305
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002306 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2307 {
2308 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2309
2310 ssl->handshake->cur_msg = ssl->handshake->flight;
2311 ssl_swap_epochs( ssl );
2312
2313 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2314 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002315
2316 while( ssl->handshake->cur_msg != NULL )
2317 {
2318 int ret;
2319 ssl_flight_item *cur = ssl->handshake->cur_msg;
2320
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002321 /* Swap epochs before sending Finished: we can't do it after
2322 * sending ChangeCipherSpec, in case write returns WANT_READ.
2323 * Must be done before copying, may change out_msg pointer */
2324 if( cur->type == SSL_MSG_HANDSHAKE &&
2325 cur->p[0] == SSL_HS_FINISHED )
2326 {
2327 ssl_swap_epochs( ssl );
2328 }
2329
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002330 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002331 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002332 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002333
2334 ssl->handshake->cur_msg = cur->next;
2335
2336 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2337
2338 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2339 {
2340 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2341 return( ret );
2342 }
2343 }
2344
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002345 if( ssl->state == SSL_HANDSHAKE_OVER )
2346 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2347 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002348 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002349 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002350 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2351 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002352
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002353 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002354
2355 return( 0 );
2356}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002357
2358/*
2359 * To be called when the last message of an incoming flight is received.
2360 */
2361void ssl_recv_flight_completed( ssl_context *ssl )
2362{
2363 /* We won't need to resend that one any more */
2364 ssl_flight_free( ssl->handshake->flight );
2365 ssl->handshake->flight = NULL;
2366 ssl->handshake->cur_msg = NULL;
2367
2368 /* The next incoming flight will start with this msg_seq */
2369 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2370
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002371 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002372 ssl_set_timer( ssl, 0 );
2373
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002374 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2375 ssl->in_msg[0] == SSL_HS_FINISHED )
2376 {
2377 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2378 }
2379 else
2380 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2381}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002382
2383/*
2384 * To be called when the last message of an outgoing flight is send.
2385 */
2386void ssl_send_flight_completed( ssl_context *ssl )
2387{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002388 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002389 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002390
2391 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2392 ssl->in_msg[0] == SSL_HS_FINISHED )
2393 {
2394 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2395 }
2396 else
2397 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2398}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002399#endif /* POLARSSL_SSL_PROTO_DTLS */
2400
Paul Bakker5121ce52009-01-03 21:22:43 +00002401/*
2402 * Record layer functions
2403 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002404
2405/*
2406 * Write current record.
2407 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2408 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002409int ssl_write_record( ssl_context *ssl )
2410{
Paul Bakker05ef8352012-05-08 09:17:57 +00002411 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002412 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
2414 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2415
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002416#if defined(POLARSSL_SSL_PROTO_DTLS)
2417 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2418 ssl->handshake != NULL &&
2419 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2420 {
2421 ; /* Skip special handshake treatment when resending */
2422 }
2423 else
2424#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2426 {
2427 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2428 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2429 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2430
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002431 /*
2432 * DTLS has additional fields in the Handshake layer,
2433 * between the length field and the actual payload:
2434 * uint16 message_seq;
2435 * uint24 fragment_offset;
2436 * uint24 fragment_length;
2437 */
2438#if defined(POLARSSL_SSL_PROTO_DTLS)
2439 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2440 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002441 /* Make room for the additional DTLS fields */
2442 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002443 ssl->out_msglen += 8;
2444 len += 8;
2445
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002446 /* Write message_seq and update it, except for HelloRequest */
2447 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2448 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002449 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2450 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2451 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002452 }
2453 else
2454 {
2455 ssl->out_msg[4] = 0;
2456 ssl->out_msg[5] = 0;
2457 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002458
2459 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2460 memset( ssl->out_msg + 6, 0x00, 3 );
2461 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002462 }
2463#endif /* POLARSSL_SSL_PROTO_DTLS */
2464
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002465 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2466 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
2468
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002469 /* Save handshake and CCS messages for resending */
2470#if defined(POLARSSL_SSL_PROTO_DTLS)
2471 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2472 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002473 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002474 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2475 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2476 {
2477 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2478 {
2479 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2480 return( ret );
2481 }
2482 }
2483#endif
2484
Paul Bakker2770fbd2012-07-03 13:30:23 +00002485#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002486 if( ssl->transform_out != NULL &&
2487 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002488 {
2489 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2490 {
2491 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2492 return( ret );
2493 }
2494
2495 len = ssl->out_msglen;
2496 }
2497#endif /*POLARSSL_ZLIB_SUPPORT */
2498
Paul Bakker05ef8352012-05-08 09:17:57 +00002499#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002500 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002502 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002503
Paul Bakker05ef8352012-05-08 09:17:57 +00002504 ret = ssl_hw_record_write( ssl );
2505 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2506 {
2507 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002508 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002509 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002510
2511 if( ret == 0 )
2512 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002513 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002514#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002515 if( !done )
2516 {
2517 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002518 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2519 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002520
2521 ssl->out_len[0] = (unsigned char)( len >> 8 );
2522 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002523
Paul Bakker48916f92012-09-16 19:57:18 +00002524 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002525 {
2526 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2527 {
2528 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2529 return( ret );
2530 }
2531
2532 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002533 ssl->out_len[0] = (unsigned char)( len >> 8 );
2534 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002535 }
2536
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002537 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002538
2539 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2540 "version = [%d:%d], msglen = %d",
2541 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002542 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002543
2544 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002545 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002546 }
2547
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2549 {
2550 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2551 return( ret );
2552 }
2553
2554 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2555
2556 return( 0 );
2557}
2558
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002559#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002560/*
2561 * Mark bits in bitmask (used for DTLS HS reassembly)
2562 */
2563static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2564{
2565 unsigned int start_bits, end_bits;
2566
2567 start_bits = 8 - ( offset % 8 );
2568 if( start_bits != 8 )
2569 {
2570 size_t first_byte_idx = offset / 8;
2571
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002572 /* Special case */
2573 if( len <= start_bits )
2574 {
2575 for( ; len != 0; len-- )
2576 mask[first_byte_idx] |= 1 << ( start_bits - len );
2577
2578 /* Avoid potential issues with offset or len becoming invalid */
2579 return;
2580 }
2581
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002582 offset += start_bits; /* Now offset % 8 == 0 */
2583 len -= start_bits;
2584
2585 for( ; start_bits != 0; start_bits-- )
2586 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2587 }
2588
2589 end_bits = len % 8;
2590 if( end_bits != 0 )
2591 {
2592 size_t last_byte_idx = ( offset + len ) / 8;
2593
2594 len -= end_bits; /* Now len % 8 == 0 */
2595
2596 for( ; end_bits != 0; end_bits-- )
2597 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2598 }
2599
2600 memset( mask + offset / 8, 0xFF, len / 8 );
2601}
2602
2603/*
2604 * Check that bitmask is full
2605 */
2606static int ssl_bitmask_check( unsigned char *mask, size_t len )
2607{
2608 size_t i;
2609
2610 for( i = 0; i < len / 8; i++ )
2611 if( mask[i] != 0xFF )
2612 return( -1 );
2613
2614 for( i = 0; i < len % 8; i++ )
2615 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2616 return( -1 );
2617
2618 return( 0 );
2619}
2620
2621/*
2622 * Reassemble fragmented DTLS handshake messages.
2623 *
2624 * Use a temporary buffer for reassembly, divided in two parts:
2625 * - the first holds the reassembled message (including handshake header),
2626 * - the second holds a bitmask indicating which parts of the message
2627 * (excluding headers) have been received so far.
2628 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002629static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2630{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002631 unsigned char *msg, *bitmask;
2632 size_t frag_len, frag_off;
2633 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2634
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002635 if( ssl->handshake == NULL )
2636 {
2637 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2638 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2639 }
2640
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002641 /*
2642 * For first fragment, check size and allocate buffer
2643 */
2644 if( ssl->handshake->hs_msg == NULL )
2645 {
2646 size_t alloc_len;
2647
2648 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2649 msg_len ) );
2650
2651 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2654 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2655 }
2656
2657 /* The bitmask needs one bit per byte of message excluding header */
2658 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2659
2660 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2661 if( ssl->handshake->hs_msg == NULL )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2664 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2665 }
2666
2667 memset( ssl->handshake->hs_msg, 0, alloc_len );
2668
2669 /* Prepare final header: copy msg_type, length and message_seq,
2670 * then add standardised fragment_offset and fragment_length */
2671 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2672 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2673 memcpy( ssl->handshake->hs_msg + 9,
2674 ssl->handshake->hs_msg + 1, 3 );
2675 }
2676 else
2677 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002678 /* Make sure msg_type and length are consistent */
2679 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002680 {
2681 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2682 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2683 }
2684 }
2685
2686 msg = ssl->handshake->hs_msg + 12;
2687 bitmask = msg + msg_len;
2688
2689 /*
2690 * Check and copy current fragment
2691 */
2692 frag_off = ( ssl->in_msg[6] << 16 ) |
2693 ( ssl->in_msg[7] << 8 ) |
2694 ssl->in_msg[8];
2695 frag_len = ( ssl->in_msg[9] << 16 ) |
2696 ( ssl->in_msg[10] << 8 ) |
2697 ssl->in_msg[11];
2698
2699 if( frag_off + frag_len > msg_len )
2700 {
2701 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2702 frag_off, frag_len, msg_len ) );
2703 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2704 }
2705
2706 if( frag_len + 12 > ssl->in_msglen )
2707 {
2708 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2709 frag_len, ssl->in_msglen ) );
2710 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2711 }
2712
2713 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2714 frag_off, frag_len ) );
2715
2716 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2717 ssl_bitmask_set( bitmask, frag_off, frag_len );
2718
2719 /*
2720 * Do we have the complete message by now?
2721 * If yes, finalize it, else ask to read the next record.
2722 */
2723 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2724 {
2725 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2726 return( POLARSSL_ERR_NET_WANT_READ );
2727 }
2728
2729 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2730
Manuel Pégourié-Gonnard23cad332014-10-13 17:06:41 +02002731 if( frag_len + 12 < ssl->in_msglen )
2732 {
2733 /*
2734 * We'got more handshake messages in the same record.
2735 * This case is not handled now because no know implementation does
2736 * that and it's hard to test, so we prefer to fail cleanly for now.
2737 */
2738 SSL_DEBUG_MSG( 1, ( "last fragment not alone in its record" ) );
2739 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2740 }
2741
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002742 if( ssl->in_left > ssl->next_record_offset )
2743 {
2744 /*
2745 * We've got more data in the buffer after the current record,
2746 * that we don't want to overwrite. Move it before writing the
2747 * reassembled message, and adjust in_left and next_record_offset.
2748 */
2749 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2750 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2751 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2752
2753 /* First compute and check new lengths */
2754 ssl->next_record_offset = new_remain - ssl->in_hdr;
2755 ssl->in_left = ssl->next_record_offset + remain_len;
2756
2757 if( ssl->in_left > SSL_BUFFER_LEN -
2758 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2759 {
2760 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2761 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2762 }
2763
2764 memmove( new_remain, cur_remain, remain_len );
2765 }
2766
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002767 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2768
2769 polarssl_free( ssl->handshake->hs_msg );
2770 ssl->handshake->hs_msg = NULL;
2771
2772 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2773 ssl->in_msg, ssl->in_hslen );
2774
2775 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002776}
2777#endif /* POLARSSL_SSL_PROTO_DTLS */
2778
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002779static int ssl_prepare_handshake_record( ssl_context *ssl )
2780{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002781 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2782 {
2783 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2784 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002785 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002786 }
2787
2788 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2789 ( ssl->in_msg[1] << 16 ) |
2790 ( ssl->in_msg[2] << 8 ) |
2791 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002792
2793 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2794 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002795 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002796
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002797#if defined(POLARSSL_SSL_PROTO_DTLS)
2798 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002799 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002800 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002801 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002802
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002803 /* ssl->handshake is NULL when receiving ClientHello for renego */
2804 if( ssl->handshake != NULL &&
2805 recv_msg_seq != ssl->handshake->in_msg_seq )
2806 {
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02002807 /* Retransmit only on last message from previous flight, to avoid
2808 * too many retransmissions.
2809 * Besides, No sane server ever retransmits HelloVerifyRequest */
2810 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002811 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002812 {
2813 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2814 "message_seq = %d, start_of_flight = %d",
2815 recv_msg_seq,
2816 ssl->handshake->in_flight_start_seq ) );
2817
2818 if( ( ret = ssl_resend( ssl ) ) != 0 )
2819 {
2820 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2821 return( ret );
2822 }
2823 }
2824 else
2825 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002826 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002827 "message_seq = %d, expected = %d",
2828 recv_msg_seq,
2829 ssl->handshake->in_msg_seq ) );
2830 }
2831
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002832 return( POLARSSL_ERR_NET_WANT_READ );
2833 }
2834 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002835
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002836 /* Reassemble if current message is fragmented or reassembly is
2837 * already in progress */
2838 if( ssl->in_msglen < ssl->in_hslen ||
2839 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2840 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2841 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002842 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002843 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2844
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002845 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2846 {
2847 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2848 return( ret );
2849 }
2850 }
2851 }
2852 else
2853#endif /* POLARSSL_SSL_PROTO_DTLS */
2854 /* With TLS we don't handle fragmentation (for now) */
2855 if( ssl->in_msglen < ssl->in_hslen )
2856 {
2857 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002858 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002859 }
2860
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002861 if( ssl->state != SSL_HANDSHAKE_OVER )
2862 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2863
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002864 /* Handshake message is complete, increment counter */
2865#if defined(POLARSSL_SSL_PROTO_DTLS)
2866 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2867 ssl->handshake != NULL )
2868 {
2869 ssl->handshake->in_msg_seq++;
2870 }
2871#endif
2872
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002873 return( 0 );
2874}
2875
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002876/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002877 * DTLS anti-replay: RFC 6347 4.1.2.6
2878 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002879 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2880 * Bit n is set iff record number in_window_top - n has been seen.
2881 *
2882 * Usually, in_window_top is the last record number seen and the lsb of
2883 * in_window is set. The only exception is the initial state (record number 0
2884 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002885 */
2886#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2887static void ssl_dtls_replay_reset( ssl_context *ssl )
2888{
2889 ssl->in_window_top = 0;
2890 ssl->in_window = 0;
2891}
2892
2893static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2894{
2895 return( ( (uint64_t) buf[0] << 40 ) |
2896 ( (uint64_t) buf[1] << 32 ) |
2897 ( (uint64_t) buf[2] << 24 ) |
2898 ( (uint64_t) buf[3] << 16 ) |
2899 ( (uint64_t) buf[4] << 8 ) |
2900 ( (uint64_t) buf[5] ) );
2901}
2902
2903/*
2904 * Return 0 if sequence number is acceptable, -1 otherwise
2905 */
2906int ssl_dtls_replay_check( ssl_context *ssl )
2907{
2908 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2909 uint64_t bit;
2910
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002911 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2912 return( 0 );
2913
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002914 if( rec_seqnum > ssl->in_window_top )
2915 return( 0 );
2916
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002917 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002918
2919 if( bit >= 64 )
2920 return( -1 );
2921
2922 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2923 return( -1 );
2924
2925 return( 0 );
2926}
2927
2928/*
2929 * Update replay window on new validated record
2930 */
2931void ssl_dtls_replay_update( ssl_context *ssl )
2932{
2933 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2934
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002935 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2936 return;
2937
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002938 if( rec_seqnum > ssl->in_window_top )
2939 {
2940 /* Update window_top and the contents of the window */
2941 uint64_t shift = rec_seqnum - ssl->in_window_top;
2942
2943 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002944 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002945 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002946 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002947 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002948 ssl->in_window |= 1;
2949 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002950
2951 ssl->in_window_top = rec_seqnum;
2952 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002953 else
2954 {
2955 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002956 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002957
2958 if( bit < 64 ) /* Always true, but be extra sure */
2959 ssl->in_window |= (uint64_t) 1 << bit;
2960 }
2961}
2962#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2963
2964/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002965 * ContentType type;
2966 * ProtocolVersion version;
2967 * uint16 epoch; // DTLS only
2968 * uint48 sequence_number; // DTLS only
2969 * uint16 length;
2970 */
2971static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002972{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002973 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002974 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002976 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2977
Paul Bakker5121ce52009-01-03 21:22:43 +00002978 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002979 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002980 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
2982 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2983 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002984 ssl->in_msgtype,
2985 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002987 /* Check record type */
2988 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2989 ssl->in_msgtype != SSL_MSG_ALERT &&
2990 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2991 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2992 {
2993 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002994
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002995 if( ( ret = ssl_send_alert_message( ssl,
2996 SSL_ALERT_LEVEL_FATAL,
2997 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2998 {
2999 return( ret );
3000 }
3001
3002 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3003 }
3004
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003005#if defined(POLARSSL_SSL_PROTO_DTLS)
3006 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3007 {
3008 /* Drop unexpected ChangeCipherSpec messages */
3009 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3010 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3011 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3012 {
3013 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
3014 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3015 }
3016
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003017 /* Drop unexpected ApplicationData records,
3018 * except at the beginning of renegotiations */
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003019 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02003020 ssl->state != SSL_HANDSHAKE_OVER &&
3021 ! ( ssl->renegotiation == SSL_RENEGOTIATION &&
3022 ssl->state == SSL_SERVER_HELLO ) )
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02003023 {
3024 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
3025 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3026 }
3027 }
3028#endif
3029
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003030 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003031 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003032 {
3033 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003034 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035 }
3036
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003037 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 {
3039 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 }
3042
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003043 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003044#if defined(POLARSSL_SSL_PROTO_DTLS)
3045 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3046 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003047 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003048
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003049 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003050 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003051 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003052 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003053 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003054 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003055 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003056
3057#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3058 if( ssl_dtls_replay_check( ssl ) != 0 )
3059 {
3060 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3061 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3062 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003063#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003064 }
3065#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003066
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003067 /* Check length against the size of our buffer */
3068 if( ssl->in_msglen > SSL_BUFFER_LEN
3069 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003070 {
3071 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3072 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3073 }
3074
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003075 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003076 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003077 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003078 if( ssl->in_msglen < 1 ||
3079 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 {
3081 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003083 }
3084 }
3085 else
3086 {
Paul Bakker48916f92012-09-16 19:57:18 +00003087 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 {
3089 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 }
3092
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003093#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003095 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 {
3097 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003098 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003099 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003100#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003101#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3102 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 /*
3104 * TLS encrypted messages can have up to 256 bytes of padding
3105 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003107 ssl->in_msglen > ssl->transform_in->minlen +
3108 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 {
3110 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003111 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003112 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 }
3115
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003116 return( 0 );
3117}
Paul Bakker5121ce52009-01-03 21:22:43 +00003118
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003119/*
3120 * If applicable, decrypt (and decompress) record content
3121 */
3122static int ssl_prepare_record_content( ssl_context *ssl )
3123{
3124 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003125
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003127 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003128
Paul Bakker05ef8352012-05-08 09:17:57 +00003129#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003130 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003131 {
3132 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3133
3134 ret = ssl_hw_record_read( ssl );
3135 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3136 {
3137 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003138 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003139 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003140
3141 if( ret == 0 )
3142 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003143 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003144#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003145 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 {
3147 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3148 {
3149 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3150 return( ret );
3151 }
3152
3153 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3154 ssl->in_msg, ssl->in_msglen );
3155
3156 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3157 {
3158 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003159 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003160 }
3161 }
3162
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003164 if( ssl->transform_in != NULL &&
3165 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003166 {
3167 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3168 {
3169 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3170 return( ret );
3171 }
3172
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003173 // TODO: what's the purpose of these lines? is in_len used?
3174 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3175 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003176 }
3177#endif /* POLARSSL_ZLIB_SUPPORT */
3178
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003179#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003180 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3181 {
3182 ssl_dtls_replay_update( ssl );
3183 }
3184#endif
3185
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003186 return( 0 );
3187}
3188
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003189static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3190
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003191/*
3192 * Read a record.
3193 *
3194 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3195 * and continue reading until a valid record is found.
3196 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003197int ssl_read_record( ssl_context *ssl )
3198{
3199 int ret;
3200
3201 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3202
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003203 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003204 {
3205 /*
3206 * Get next Handshake message in the current record
3207 */
3208 ssl->in_msglen -= ssl->in_hslen;
3209
3210 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3211 ssl->in_msglen );
3212
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003213 SSL_DEBUG_BUF( 4, "remaining content in record",
3214 ssl->in_msg, ssl->in_msglen );
3215
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003216 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3217 return( ret );
3218
3219 return( 0 );
3220 }
3221
3222 ssl->in_hslen = 0;
3223
3224 /*
3225 * Read the record header and parse it
3226 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003227#if defined(POLARSSL_SSL_PROTO_DTLS)
3228read_record_header:
3229#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003230 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3231 {
3232 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3233 return( ret );
3234 }
3235
3236 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003237 {
3238#if defined(POLARSSL_SSL_PROTO_DTLS)
3239 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3240 {
3241 /* Ignore bad record and get next one; drop the whole datagram
3242 * since current header cannot be trusted to find the next record
3243 * in current datagram */
3244 ssl->next_record_offset = 0;
3245 ssl->in_left = 0;
3246
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003247 SSL_DEBUG_MSG( 1, ( "discarding invalid record (header)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003248 goto read_record_header;
3249 }
3250#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003251 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003252 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003253
3254 /*
3255 * Read and optionally decrypt the message contents
3256 */
3257 if( ( ret = ssl_fetch_input( ssl,
3258 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3259 {
3260 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3261 return( ret );
3262 }
3263
3264 /* Done reading this record, get ready for the next one */
3265#if defined(POLARSSL_SSL_PROTO_DTLS)
3266 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3267 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3268 else
3269#endif
3270 ssl->in_left = 0;
3271
3272 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003273 {
3274#if defined(POLARSSL_SSL_PROTO_DTLS)
3275 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3276 {
3277 /* Silently discard invalid records */
3278 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3279 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3280 {
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02003281#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
3282 if( ssl->badmac_limit != 0 &&
3283 ++ssl->badmac_seen >= ssl->badmac_limit )
3284 {
3285 SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
3286 return( POLARSSL_ERR_SSL_INVALID_MAC );
3287 }
3288#endif
3289
Manuel Pégourié-Gonnard74a13782014-10-14 22:34:08 +02003290 SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003291 goto read_record_header;
3292 }
3293
3294 return( ret );
3295 }
3296 else
3297#endif
3298 {
3299 /* Error out (and send alert) on invalid records */
3300#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3301 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3302 {
3303 ssl_send_alert_message( ssl,
3304 SSL_ALERT_LEVEL_FATAL,
3305 SSL_ALERT_MSG_BAD_RECORD_MAC );
3306 }
3307#endif
3308 return( ret );
3309 }
3310 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003311
3312 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003313 * When we sent the last flight of the handshake, we MUST respond to a
3314 * retransmit of the peer's previous flight with a retransmit. (In
3315 * practice, only the Finished message will make it, other messages
3316 * including CCS use the old transform so they're dropped as invalid.)
3317 *
3318 * If the record we received is not a handshake message, however, it
3319 * means the peer received our last flight so we can clean up
3320 * handshake info.
3321 *
3322 * This check needs to be done before prepare_handshake() due to an edge
3323 * case: if the client immediately requests renegotiation, this
3324 * finishes the current handshake first, avoiding the new ClientHello
3325 * being mistaken for an ancient message in the current handshake.
3326 */
3327#if defined(POLARSSL_SSL_PROTO_DTLS)
3328 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3329 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003330 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003331 {
3332 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3333 ssl->in_msg[0] == SSL_HS_FINISHED )
3334 {
3335 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3336
3337 if( ( ret = ssl_resend( ssl ) ) != 0 )
3338 {
3339 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3340 return( ret );
3341 }
3342
3343 return( POLARSSL_ERR_NET_WANT_READ );
3344 }
3345 else
3346 {
3347 ssl_handshake_wrapup_free_hs_transform( ssl );
3348 }
3349 }
3350#endif
3351
3352 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003353 * Handle particular types of records
3354 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3356 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003357 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3358 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003359 }
3360
3361 if( ssl->in_msgtype == SSL_MSG_ALERT )
3362 {
3363 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3364 ssl->in_msg[0], ssl->in_msg[1] ) );
3365
3366 /*
3367 * Ignore non-fatal alerts, except close_notify
3368 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003369 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003371 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3372 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003373 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 }
3375
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003376 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3377 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003378 {
3379 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003380 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 }
3382 }
3383
Paul Bakker5121ce52009-01-03 21:22:43 +00003384 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3385
3386 return( 0 );
3387}
3388
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003389int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3390{
3391 int ret;
3392
3393 if( ( ret = ssl_send_alert_message( ssl,
3394 SSL_ALERT_LEVEL_FATAL,
3395 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3396 {
3397 return( ret );
3398 }
3399
3400 return( 0 );
3401}
3402
Paul Bakker0a925182012-04-16 06:46:41 +00003403int ssl_send_alert_message( ssl_context *ssl,
3404 unsigned char level,
3405 unsigned char message )
3406{
3407 int ret;
3408
3409 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3410
3411 ssl->out_msgtype = SSL_MSG_ALERT;
3412 ssl->out_msglen = 2;
3413 ssl->out_msg[0] = level;
3414 ssl->out_msg[1] = message;
3415
3416 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3417 {
3418 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3419 return( ret );
3420 }
3421
3422 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3423
3424 return( 0 );
3425}
3426
Paul Bakker5121ce52009-01-03 21:22:43 +00003427/*
3428 * Handshake functions
3429 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003430#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3431 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3432 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3433 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3434 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3435 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3436 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003437int ssl_write_certificate( ssl_context *ssl )
3438{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003439 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003440
3441 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3442
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003443 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003444 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3445 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003446 {
3447 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3448 ssl->state++;
3449 return( 0 );
3450 }
3451
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003454}
3455
3456int ssl_parse_certificate( ssl_context *ssl )
3457{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003458 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3459
3460 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3461
3462 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003465 {
3466 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3467 ssl->state++;
3468 return( 0 );
3469 }
3470
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003471 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3472 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003473}
3474#else
3475int ssl_write_certificate( ssl_context *ssl )
3476{
3477 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3478 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003479 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003480 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3481
3482 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3483
3484 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003485 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3486 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003487 {
3488 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3489 ssl->state++;
3490 return( 0 );
3491 }
3492
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 if( ssl->endpoint == SSL_IS_CLIENT )
3494 {
3495 if( ssl->client_auth == 0 )
3496 {
3497 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3498 ssl->state++;
3499 return( 0 );
3500 }
3501
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003502#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 /*
3504 * If using SSLv3 and got no cert, send an Alert message
3505 * (otherwise an empty Certificate message will be sent).
3506 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003507 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3509 {
3510 ssl->out_msglen = 2;
3511 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003512 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3513 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003514
3515 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3516 goto write_msg;
3517 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003518#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003519 }
3520 else /* SSL_IS_SERVER */
3521 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003522 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 {
3524 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003525 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003526 }
3527 }
3528
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003529 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003530
3531 /*
3532 * 0 . 0 handshake type
3533 * 1 . 3 handshake length
3534 * 4 . 6 length of all certs
3535 * 7 . 9 length of cert. 1
3536 * 10 . n-1 peer certificate
3537 * n . n+2 length of cert. 2
3538 * n+3 . ... upper level cert, etc.
3539 */
3540 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003541 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
Paul Bakker29087132010-03-21 21:03:34 +00003543 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 {
3545 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003546 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 {
3548 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3549 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003550 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 }
3552
3553 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3554 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3555 ssl->out_msg[i + 2] = (unsigned char)( n );
3556
3557 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3558 i += n; crt = crt->next;
3559 }
3560
3561 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3562 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3563 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3564
3565 ssl->out_msglen = i;
3566 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3567 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3568
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003569#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003570write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003571#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003572
3573 ssl->state++;
3574
3575 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3576 {
3577 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3578 return( ret );
3579 }
3580
3581 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3582
Paul Bakkered27a042013-04-18 22:46:23 +02003583 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003584}
3585
3586int ssl_parse_certificate( ssl_context *ssl )
3587{
Paul Bakkered27a042013-04-18 22:46:23 +02003588 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003589 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003590 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003591
3592 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3593
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003594 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003595 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3596 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003597 {
3598 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3599 ssl->state++;
3600 return( 0 );
3601 }
3602
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003604 ( ssl->authmode == SSL_VERIFY_NONE ||
3605 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003607 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3609 ssl->state++;
3610 return( 0 );
3611 }
3612
3613 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3614 {
3615 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3616 return( ret );
3617 }
3618
3619 ssl->state++;
3620
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003621#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 /*
3623 * Check if the client sent an empty certificate
3624 */
3625 if( ssl->endpoint == SSL_IS_SERVER &&
3626 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3627 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003628 if( ssl->in_msglen == 2 &&
3629 ssl->in_msgtype == SSL_MSG_ALERT &&
3630 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3631 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003632 {
3633 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3634
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003635 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003636 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3637 return( 0 );
3638 else
Paul Bakker40e46942009-01-03 21:51:57 +00003639 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003640 }
3641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003642#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003643
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003644#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3645 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 if( ssl->endpoint == SSL_IS_SERVER &&
3647 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3648 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003649 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3651 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003652 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 {
3654 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3655
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003656 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003658 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 else
3660 return( 0 );
3661 }
3662 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003663#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3664 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
3666 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3667 {
3668 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003669 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 }
3671
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003672 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3673 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674 {
3675 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003676 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003677 }
3678
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003679 i = ssl_hs_hdr_len( ssl );
3680
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 /*
3682 * Same message structure as in ssl_write_certificate()
3683 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003684 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003685
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003686 if( ssl->in_msg[i] != 0 ||
3687 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688 {
3689 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003690 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 }
3692
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003693 /* In case we tried to reuse a session but it failed */
3694 if( ssl->session_negotiate->peer_cert != NULL )
3695 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003696 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003697 polarssl_free( ssl->session_negotiate->peer_cert );
3698 }
3699
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003700 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3701 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 {
3703 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003704 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003705 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 }
3707
Paul Bakkerb6b09562013-09-18 14:17:41 +02003708 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003710 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003711
3712 while( i < ssl->in_hslen )
3713 {
3714 if( ssl->in_msg[i] != 0 )
3715 {
3716 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003717 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 }
3719
3720 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3721 | (unsigned int) ssl->in_msg[i + 2];
3722 i += 3;
3723
3724 if( n < 128 || i + n > ssl->in_hslen )
3725 {
3726 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003727 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 }
3729
Paul Bakkerddf26b42013-09-18 13:46:23 +02003730 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3731 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732 if( ret != 0 )
3733 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003734 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003735 return( ret );
3736 }
3737
3738 i += n;
3739 }
3740
Paul Bakker48916f92012-09-16 19:57:18 +00003741 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003743 /*
3744 * On client, make sure the server cert doesn't change during renego to
3745 * avoid "triple handshake" attack: https://secure-resumption.com/
3746 */
3747 if( ssl->endpoint == SSL_IS_CLIENT &&
3748 ssl->renegotiation == SSL_RENEGOTIATION )
3749 {
3750 if( ssl->session->peer_cert == NULL )
3751 {
3752 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3753 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3754 }
3755
3756 if( ssl->session->peer_cert->raw.len !=
3757 ssl->session_negotiate->peer_cert->raw.len ||
3758 memcmp( ssl->session->peer_cert->raw.p,
3759 ssl->session_negotiate->peer_cert->raw.p,
3760 ssl->session->peer_cert->raw.len ) != 0 )
3761 {
3762 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3763 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3764 }
3765 }
3766
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 if( ssl->authmode != SSL_VERIFY_NONE )
3768 {
3769 if( ssl->ca_chain == NULL )
3770 {
3771 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003772 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 }
3774
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003775 /*
3776 * Main check: verify certificate
3777 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003778 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3779 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3780 &ssl->session_negotiate->verify_result,
3781 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003782
3783 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003784 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003785 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003786 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003787
3788 /*
3789 * Secondary checks: always done, but change 'ret' only if it was 0
3790 */
3791
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003792#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003793 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003794 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003795
3796 /* If certificate uses an EC key, make sure the curve is OK */
3797 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3798 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3799 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003800 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003801 if( ret == 0 )
3802 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003803 }
3804 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003805#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003807 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3808 ciphersuite_info,
3809 ! ssl->endpoint ) != 0 )
3810 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003811 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003812 if( ret == 0 )
3813 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3814 }
3815
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3817 ret = 0;
3818 }
3819
3820 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3821
3822 return( ret );
3823}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003824#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3825 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3826 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3827 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3828 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3829 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3830 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003831
3832int ssl_write_change_cipher_spec( ssl_context *ssl )
3833{
3834 int ret;
3835
3836 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3837
3838 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3839 ssl->out_msglen = 1;
3840 ssl->out_msg[0] = 1;
3841
Paul Bakker5121ce52009-01-03 21:22:43 +00003842 ssl->state++;
3843
3844 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3845 {
3846 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3847 return( ret );
3848 }
3849
3850 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3851
3852 return( 0 );
3853}
3854
3855int ssl_parse_change_cipher_spec( ssl_context *ssl )
3856{
3857 int ret;
3858
3859 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3860
Paul Bakker5121ce52009-01-03 21:22:43 +00003861 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3862 {
3863 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3864 return( ret );
3865 }
3866
3867 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3868 {
3869 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003870 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 }
3872
3873 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3874 {
3875 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003876 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 }
3878
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003879 /*
3880 * Switch to our negotiated transform and session parameters for inbound
3881 * data.
3882 */
3883 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3884 ssl->transform_in = ssl->transform_negotiate;
3885 ssl->session_in = ssl->session_negotiate;
3886
3887#if defined(POLARSSL_SSL_PROTO_DTLS)
3888 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3889 {
3890#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3891 ssl_dtls_replay_reset( ssl );
3892#endif
3893
3894 /* Increment epoch */
3895 if( ++ssl->in_epoch == 0 )
3896 {
3897 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3898 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3899 }
3900 }
3901 else
3902#endif /* POLARSSL_SSL_PROTO_DTLS */
3903 memset( ssl->in_ctr, 0, 8 );
3904
3905 /*
3906 * Set the in_msg pointer to the correct location based on IV length
3907 */
3908 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3909 {
3910 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3911 ssl->transform_negotiate->fixed_ivlen;
3912 }
3913 else
3914 ssl->in_msg = ssl->in_iv;
3915
3916#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3917 if( ssl_hw_record_activate != NULL )
3918 {
3919 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3920 {
3921 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3922 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3923 }
3924 }
3925#endif
3926
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 ssl->state++;
3928
3929 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3930
3931 return( 0 );
3932}
3933
Paul Bakker41c83d32013-03-20 14:39:14 +01003934void ssl_optimize_checksum( ssl_context *ssl,
3935 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003936{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003937 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003938
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003939#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3940 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003941 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003942 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003943 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003944#endif
3945#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3946#if defined(POLARSSL_SHA512_C)
3947 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3948 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3949 else
3950#endif
3951#if defined(POLARSSL_SHA256_C)
3952 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003953 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003954 else
3955#endif
3956#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003957 {
3958 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003959 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003960 }
Paul Bakker380da532012-04-18 16:10:25 +00003961}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003962
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003963void ssl_reset_checksum( ssl_context *ssl )
3964{
3965#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3966 defined(POLARSSL_SSL_PROTO_TLS1_1)
3967 md5_starts( &ssl->handshake->fin_md5 );
3968 sha1_starts( &ssl->handshake->fin_sha1 );
3969#endif
3970#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3971#if defined(POLARSSL_SHA256_C)
3972 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3973#endif
3974#if defined(POLARSSL_SHA512_C)
3975 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3976#endif
3977#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3978}
3979
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003980static void ssl_update_checksum_start( ssl_context *ssl,
3981 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003982{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003983#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3984 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003985 md5_update( &ssl->handshake->fin_md5 , buf, len );
3986 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003987#endif
3988#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3989#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003990 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003991#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003992#if defined(POLARSSL_SHA512_C)
3993 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003994#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003995#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003996}
3997
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003998#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3999 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004000static void ssl_update_checksum_md5sha1( ssl_context *ssl,
4001 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004002{
Paul Bakker48916f92012-09-16 19:57:18 +00004003 md5_update( &ssl->handshake->fin_md5 , buf, len );
4004 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004005}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004006#endif
Paul Bakker380da532012-04-18 16:10:25 +00004007
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004008#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4009#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004010static void ssl_update_checksum_sha256( ssl_context *ssl,
4011 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004012{
Paul Bakker9e36f042013-06-30 14:34:05 +02004013 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004014}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004015#endif
Paul Bakker380da532012-04-18 16:10:25 +00004016
Paul Bakker9e36f042013-06-30 14:34:05 +02004017#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004018static void ssl_update_checksum_sha384( ssl_context *ssl,
4019 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00004020{
Paul Bakker9e36f042013-06-30 14:34:05 +02004021 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00004022}
Paul Bakker769075d2012-11-24 11:26:46 +01004023#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004024#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00004025
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004026#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004027static void ssl_calc_finished_ssl(
4028 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004029{
Paul Bakker3c2122f2013-06-24 19:03:14 +02004030 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004031 md5_context md5;
4032 sha1_context sha1;
4033
Paul Bakker5121ce52009-01-03 21:22:43 +00004034 unsigned char padbuf[48];
4035 unsigned char md5sum[16];
4036 unsigned char sha1sum[20];
4037
Paul Bakker48916f92012-09-16 19:57:18 +00004038 ssl_session *session = ssl->session_negotiate;
4039 if( !session )
4040 session = ssl->session;
4041
Paul Bakker1ef83d62012-04-11 12:09:53 +00004042 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
4043
Paul Bakker48916f92012-09-16 19:57:18 +00004044 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4045 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004046
4047 /*
4048 * SSLv3:
4049 * hash =
4050 * MD5( master + pad2 +
4051 * MD5( handshake + sender + master + pad1 ) )
4052 * + SHA1( master + pad2 +
4053 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00004054 */
4055
Paul Bakker90995b52013-06-24 19:20:35 +02004056#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004057 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004058 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004059#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004060
Paul Bakker90995b52013-06-24 19:20:35 +02004061#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004063 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004065
Paul Bakker3c2122f2013-06-24 19:03:14 +02004066 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4067 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004068
Paul Bakker1ef83d62012-04-11 12:09:53 +00004069 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004070
Paul Bakker3c2122f2013-06-24 19:03:14 +02004071 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004072 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004073 md5_update( &md5, padbuf, 48 );
4074 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004075
Paul Bakker3c2122f2013-06-24 19:03:14 +02004076 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004077 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004078 sha1_update( &sha1, padbuf, 40 );
4079 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004080
Paul Bakker1ef83d62012-04-11 12:09:53 +00004081 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004082
Paul Bakker1ef83d62012-04-11 12:09:53 +00004083 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004084 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004085 md5_update( &md5, padbuf, 48 );
4086 md5_update( &md5, md5sum, 16 );
4087 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004088
Paul Bakker1ef83d62012-04-11 12:09:53 +00004089 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004090 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004091 sha1_update( &sha1, padbuf , 40 );
4092 sha1_update( &sha1, sha1sum, 20 );
4093 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004094
Paul Bakker1ef83d62012-04-11 12:09:53 +00004095 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096
Paul Bakker5b4af392014-06-26 12:09:34 +02004097 md5_free( &md5 );
4098 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004099
Paul Bakker34617722014-06-13 17:20:13 +02004100 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4101 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4102 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004103
4104 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4105}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004106#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004107
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004108#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004109static void ssl_calc_finished_tls(
4110 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004111{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004112 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004113 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004115 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004116 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004117
Paul Bakker48916f92012-09-16 19:57:18 +00004118 ssl_session *session = ssl->session_negotiate;
4119 if( !session )
4120 session = ssl->session;
4121
Paul Bakker1ef83d62012-04-11 12:09:53 +00004122 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004123
Paul Bakker48916f92012-09-16 19:57:18 +00004124 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4125 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004126
Paul Bakker1ef83d62012-04-11 12:09:53 +00004127 /*
4128 * TLSv1:
4129 * hash = PRF( master, finished_label,
4130 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4131 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004132
Paul Bakker90995b52013-06-24 19:20:35 +02004133#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004134 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4135 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004136#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004137
Paul Bakker90995b52013-06-24 19:20:35 +02004138#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004139 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4140 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004141#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004142
4143 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004144 ? "client finished"
4145 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004146
4147 md5_finish( &md5, padbuf );
4148 sha1_finish( &sha1, padbuf + 16 );
4149
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004150 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004151 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004152
4153 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4154
Paul Bakker5b4af392014-06-26 12:09:34 +02004155 md5_free( &md5 );
4156 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004157
Paul Bakker34617722014-06-13 17:20:13 +02004158 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004159
4160 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4161}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004162#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004163
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004164#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4165#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004166static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004167 ssl_context *ssl, unsigned char *buf, int from )
4168{
4169 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004170 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004171 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004172 unsigned char padbuf[32];
4173
Paul Bakker48916f92012-09-16 19:57:18 +00004174 ssl_session *session = ssl->session_negotiate;
4175 if( !session )
4176 session = ssl->session;
4177
Paul Bakker380da532012-04-18 16:10:25 +00004178 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004179
Paul Bakker9e36f042013-06-30 14:34:05 +02004180 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004181
4182 /*
4183 * TLSv1.2:
4184 * hash = PRF( master, finished_label,
4185 * Hash( handshake ) )[0.11]
4186 */
4187
Paul Bakker9e36f042013-06-30 14:34:05 +02004188#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004189 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004190 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004191#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004192
4193 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004194 ? "client finished"
4195 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004196
Paul Bakker9e36f042013-06-30 14:34:05 +02004197 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004198
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004199 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004200 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004201
4202 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4203
Paul Bakker5b4af392014-06-26 12:09:34 +02004204 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004205
Paul Bakker34617722014-06-13 17:20:13 +02004206 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004207
4208 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4209}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004210#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004211
Paul Bakker9e36f042013-06-30 14:34:05 +02004212#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004213static void ssl_calc_finished_tls_sha384(
4214 ssl_context *ssl, unsigned char *buf, int from )
4215{
4216 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004217 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004218 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004219 unsigned char padbuf[48];
4220
Paul Bakker48916f92012-09-16 19:57:18 +00004221 ssl_session *session = ssl->session_negotiate;
4222 if( !session )
4223 session = ssl->session;
4224
Paul Bakker380da532012-04-18 16:10:25 +00004225 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004226
Paul Bakker9e36f042013-06-30 14:34:05 +02004227 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004228
4229 /*
4230 * TLSv1.2:
4231 * hash = PRF( master, finished_label,
4232 * Hash( handshake ) )[0.11]
4233 */
4234
Paul Bakker9e36f042013-06-30 14:34:05 +02004235#if !defined(POLARSSL_SHA512_ALT)
4236 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4237 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004238#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004239
4240 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004241 ? "client finished"
4242 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004243
Paul Bakker9e36f042013-06-30 14:34:05 +02004244 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004245
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004246 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004247 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004248
4249 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4250
Paul Bakker5b4af392014-06-26 12:09:34 +02004251 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004252
Paul Bakker34617722014-06-13 17:20:13 +02004253 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004254
4255 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004257#endif /* POLARSSL_SHA512_C */
4258#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004259
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004260static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004261{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004262 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004263
4264 /*
4265 * Free our handshake params
4266 */
4267 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004268 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004269 ssl->handshake = NULL;
4270
4271 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004272 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004273 */
4274 if( ssl->transform )
4275 {
4276 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004277 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004278 }
4279 ssl->transform = ssl->transform_negotiate;
4280 ssl->transform_negotiate = NULL;
4281
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004282 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4283}
4284
4285void ssl_handshake_wrapup( ssl_context *ssl )
4286{
4287 int resume = ssl->handshake->resume;
4288
4289 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4290
4291 if( ssl->renegotiation == SSL_RENEGOTIATION )
4292 {
4293 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4294 ssl->renego_records_seen = 0;
4295 }
4296
4297 /*
4298 * Free the previous session and switch in the current one
4299 */
Paul Bakker0a597072012-09-25 21:55:46 +00004300 if( ssl->session )
4301 {
4302 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004303 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004304 }
4305 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004306 ssl->session_negotiate = NULL;
4307
Paul Bakker0a597072012-09-25 21:55:46 +00004308 /*
4309 * Add cache entry
4310 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004311 if( ssl->f_set_cache != NULL &&
4312 ssl->session->length != 0 &&
4313 resume == 0 )
4314 {
Paul Bakker0a597072012-09-25 21:55:46 +00004315 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4316 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004317 }
Paul Bakker0a597072012-09-25 21:55:46 +00004318
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004319#if defined(POLARSSL_SSL_PROTO_DTLS)
4320 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4321 ssl->handshake->flight != NULL )
4322 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004323 /* Cancel handshake timer */
4324 ssl_set_timer( ssl, 0 );
4325
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004326 /* Keep last flight around in case we need to resend it:
4327 * we need the handshake and transform structures for that */
4328 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4329 }
4330 else
4331#endif
4332 ssl_handshake_wrapup_free_hs_transform( ssl );
4333
Paul Bakker48916f92012-09-16 19:57:18 +00004334 ssl->state++;
4335
4336 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4337}
4338
Paul Bakker1ef83d62012-04-11 12:09:53 +00004339int ssl_write_finished( ssl_context *ssl )
4340{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004341 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004342
4343 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4344
Paul Bakker92be97b2013-01-02 17:30:03 +01004345 /*
4346 * Set the out_msg pointer to the correct location based on IV length
4347 */
4348 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4349 {
4350 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4351 ssl->transform_negotiate->fixed_ivlen;
4352 }
4353 else
4354 ssl->out_msg = ssl->out_iv;
4355
Paul Bakker48916f92012-09-16 19:57:18 +00004356 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004357
4358 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4360
Paul Bakker48916f92012-09-16 19:57:18 +00004361 ssl->verify_data_len = hash_len;
4362 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4363
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 ssl->out_msglen = 4 + hash_len;
4365 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4366 ssl->out_msg[0] = SSL_HS_FINISHED;
4367
4368 /*
4369 * In case of session resuming, invert the client and server
4370 * ChangeCipherSpec messages order.
4371 */
Paul Bakker0a597072012-09-25 21:55:46 +00004372 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 {
4374 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004375 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 else
4377 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4378 }
4379 else
4380 ssl->state++;
4381
Paul Bakker48916f92012-09-16 19:57:18 +00004382 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004383 * Switch to our negotiated transform and session parameters for outbound
4384 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004385 */
4386 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004387
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004388#if defined(POLARSSL_SSL_PROTO_DTLS)
4389 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4390 {
4391 unsigned char i;
4392
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004393 /* Remember current epoch settings for resending */
4394 ssl->handshake->alt_transform_out = ssl->transform_out;
4395 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4396
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004397 /* Set sequence_number to zero */
4398 memset( ssl->out_ctr + 2, 0, 6 );
4399
4400 /* Increment epoch */
4401 for( i = 2; i > 0; i-- )
4402 if( ++ssl->out_ctr[i - 1] != 0 )
4403 break;
4404
4405 /* The loop goes to its end iff the counter is wrapping */
4406 if( i == 0 )
4407 {
4408 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4409 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4410 }
4411 }
4412 else
4413#endif /* POLARSSL_SSL_PROTO_DTLS */
4414 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004415
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004416 ssl->transform_out = ssl->transform_negotiate;
4417 ssl->session_out = ssl->session_negotiate;
4418
Paul Bakker07eb38b2012-12-19 14:42:06 +01004419#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004420 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004421 {
4422 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4423 {
4424 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4425 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4426 }
4427 }
4428#endif
4429
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004430#if defined(POLARSSL_SSL_PROTO_DTLS)
4431 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4432 ssl_send_flight_completed( ssl );
4433#endif
4434
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4436 {
4437 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4438 return( ret );
4439 }
4440
4441 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4442
4443 return( 0 );
4444}
4445
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004446#if defined(POLARSSL_SSL_PROTO_SSL3)
4447#define SSL_MAX_HASH_LEN 36
4448#else
4449#define SSL_MAX_HASH_LEN 12
4450#endif
4451
Paul Bakker5121ce52009-01-03 21:22:43 +00004452int ssl_parse_finished( ssl_context *ssl )
4453{
Paul Bakker23986e52011-04-24 08:57:21 +00004454 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004455 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004456 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004457
4458 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4459
Paul Bakker48916f92012-09-16 19:57:18 +00004460 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004461
Paul Bakker5121ce52009-01-03 21:22:43 +00004462 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4463 {
4464 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4465 return( ret );
4466 }
4467
4468 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4469 {
4470 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004471 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004472 }
4473
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004474 /* There is currently no ciphersuite using another length with TLS 1.2 */
4475#if defined(POLARSSL_SSL_PROTO_SSL3)
4476 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4477 hash_len = 36;
4478 else
4479#endif
4480 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004481
4482 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004483 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 {
4485 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004486 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 }
4488
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004489 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4490 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 {
4492 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004493 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004494 }
4495
Paul Bakker48916f92012-09-16 19:57:18 +00004496 ssl->verify_data_len = hash_len;
4497 memcpy( ssl->peer_verify_data, buf, hash_len );
4498
Paul Bakker0a597072012-09-25 21:55:46 +00004499 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004500 {
4501 if( ssl->endpoint == SSL_IS_CLIENT )
4502 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4503
4504 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004505 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004506 }
4507 else
4508 ssl->state++;
4509
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004510#if defined(POLARSSL_SSL_PROTO_DTLS)
4511 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4512 ssl_recv_flight_completed( ssl );
4513#endif
4514
Paul Bakker5121ce52009-01-03 21:22:43 +00004515 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4516
4517 return( 0 );
4518}
4519
Paul Bakker968afaa2014-07-09 11:09:24 +02004520static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004521{
4522 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4523
4524#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4525 defined(POLARSSL_SSL_PROTO_TLS1_1)
4526 md5_init( &handshake->fin_md5 );
4527 sha1_init( &handshake->fin_sha1 );
4528 md5_starts( &handshake->fin_md5 );
4529 sha1_starts( &handshake->fin_sha1 );
4530#endif
4531#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4532#if defined(POLARSSL_SHA256_C)
4533 sha256_init( &handshake->fin_sha256 );
4534 sha256_starts( &handshake->fin_sha256, 0 );
4535#endif
4536#if defined(POLARSSL_SHA512_C)
4537 sha512_init( &handshake->fin_sha512 );
4538 sha512_starts( &handshake->fin_sha512, 1 );
4539#endif
4540#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4541
4542 handshake->update_checksum = ssl_update_checksum_start;
4543 handshake->sig_alg = SSL_HASH_SHA1;
4544
4545#if defined(POLARSSL_DHM_C)
4546 dhm_init( &handshake->dhm_ctx );
4547#endif
4548#if defined(POLARSSL_ECDH_C)
4549 ecdh_init( &handshake->ecdh_ctx );
4550#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004551}
4552
4553static void ssl_transform_init( ssl_transform *transform )
4554{
4555 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004556
4557 cipher_init( &transform->cipher_ctx_enc );
4558 cipher_init( &transform->cipher_ctx_dec );
4559
4560 md_init( &transform->md_ctx_enc );
4561 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004562}
4563
4564void ssl_session_init( ssl_session *session )
4565{
4566 memset( session, 0, sizeof(ssl_session) );
4567}
4568
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004569static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004570{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004571 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004572 if( ssl->transform_negotiate )
4573 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004574 if( ssl->session_negotiate )
4575 ssl_session_free( ssl->session_negotiate );
4576 if( ssl->handshake )
4577 ssl_handshake_free( ssl->handshake );
4578
4579 /*
4580 * Either the pointers are now NULL or cleared properly and can be freed.
4581 * Now allocate missing structures.
4582 */
4583 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004584 {
4585 ssl->transform_negotiate =
4586 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4587 }
Paul Bakker48916f92012-09-16 19:57:18 +00004588
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004589 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004590 {
4591 ssl->session_negotiate =
4592 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4593 }
Paul Bakker48916f92012-09-16 19:57:18 +00004594
Paul Bakker82788fb2014-10-20 13:59:19 +02004595 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004596 {
4597 ssl->handshake = (ssl_handshake_params *)
4598 polarssl_malloc( sizeof(ssl_handshake_params) );
4599 }
Paul Bakker48916f92012-09-16 19:57:18 +00004600
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004601 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004602 if( ssl->handshake == NULL ||
4603 ssl->transform_negotiate == NULL ||
4604 ssl->session_negotiate == NULL )
4605 {
4606 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004607
4608 polarssl_free( ssl->handshake );
4609 polarssl_free( ssl->transform_negotiate );
4610 polarssl_free( ssl->session_negotiate );
4611
4612 ssl->handshake = NULL;
4613 ssl->transform_negotiate = NULL;
4614 ssl->session_negotiate = NULL;
4615
Paul Bakker48916f92012-09-16 19:57:18 +00004616 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4617 }
4618
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004619 /* Initialize structures */
4620 ssl_session_init( ssl->session_negotiate );
4621 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004622 ssl_handshake_params_init( ssl->handshake );
4623
4624#if defined(POLARSSL_X509_CRT_PARSE_C)
4625 ssl->handshake->key_cert = ssl->key_cert;
4626#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004627
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004628 /*
4629 * We may not know yet if we're using DTLS,
4630 * so always initiliase DTLS-specific fields.
4631 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004632#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004633 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004634
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004635 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004636 if( ssl->endpoint == SSL_IS_CLIENT )
4637 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4638 else
4639 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004640#endif
4641
Paul Bakker48916f92012-09-16 19:57:18 +00004642 return( 0 );
4643}
4644
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004645#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4646/* Dummy cookie callbacks for defaults */
4647static int ssl_cookie_write_dummy( void *ctx,
4648 unsigned char **p, unsigned char *end,
4649 const unsigned char *cli_id, size_t cli_id_len )
4650{
4651 ((void) ctx);
4652 ((void) p);
4653 ((void) end);
4654 ((void) cli_id);
4655 ((void) cli_id_len);
4656
4657 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4658}
4659
4660static int ssl_cookie_check_dummy( void *ctx,
4661 const unsigned char *cookie, size_t cookie_len,
4662 const unsigned char *cli_id, size_t cli_id_len )
4663{
4664 ((void) ctx);
4665 ((void) cookie);
4666 ((void) cookie_len);
4667 ((void) cli_id);
4668 ((void) cli_id_len);
4669
4670 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4671}
4672#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4673
Paul Bakker5121ce52009-01-03 21:22:43 +00004674/*
4675 * Initialize an SSL context
4676 */
4677int ssl_init( ssl_context *ssl )
4678{
Paul Bakker48916f92012-09-16 19:57:18 +00004679 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004680 int len = SSL_BUFFER_LEN;
4681
4682 memset( ssl, 0, sizeof( ssl_context ) );
4683
Paul Bakker62f2dee2012-09-28 07:31:51 +00004684 /*
4685 * Sane defaults
4686 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004687 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4688 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4689 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4690 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004691
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004692 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004693
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004694 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4695
Paul Bakker62f2dee2012-09-28 07:31:51 +00004696#if defined(POLARSSL_DHM_C)
4697 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4698 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4699 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4700 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4701 {
4702 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4703 return( ret );
4704 }
4705#endif
4706
4707 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004708 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004709 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004710 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004711 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004712
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004713 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004714 {
4715 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004716 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004717 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004718 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004719 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004720 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 }
4722
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004723 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4724 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004725
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004726 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4727 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4728
Paul Bakker606b4ba2013-08-14 16:52:14 +02004729#if defined(POLARSSL_SSL_SESSION_TICKETS)
4730 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4731#endif
4732
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004733#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004734 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004735#endif
4736
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004737#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4738 ssl->f_cookie_write = ssl_cookie_write_dummy;
4739 ssl->f_cookie_check = ssl_cookie_check_dummy;
4740#endif
4741
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004742#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4743 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4744#endif
4745
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004746#if defined(POLARSSL_SSL_PROTO_DTLS)
4747 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4748 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4749#endif
4750
Paul Bakker48916f92012-09-16 19:57:18 +00004751 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4752 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004753
4754 return( 0 );
4755}
4756
4757/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004758 * Reset an initialized and used SSL context for re-use while retaining
4759 * all application-set variables, function pointers and data.
4760 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004761int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004762{
Paul Bakker48916f92012-09-16 19:57:18 +00004763 int ret;
4764
Paul Bakker7eb013f2011-10-06 12:37:39 +00004765 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004766 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4767 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4768
4769 ssl->verify_data_len = 0;
4770 memset( ssl->own_verify_data, 0, 36 );
4771 memset( ssl->peer_verify_data, 0, 36 );
4772
Paul Bakker7eb013f2011-10-06 12:37:39 +00004773 ssl->in_offt = NULL;
4774
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004775 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004776 ssl->in_msgtype = 0;
4777 ssl->in_msglen = 0;
4778 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004779#if defined(POLARSSL_SSL_PROTO_DTLS)
4780 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004781 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004782#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004783#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4784 ssl_dtls_replay_reset( ssl );
4785#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004786
4787 ssl->in_hslen = 0;
4788 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004789 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004790
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004791 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004792 ssl->out_msgtype = 0;
4793 ssl->out_msglen = 0;
4794 ssl->out_left = 0;
4795
Paul Bakker48916f92012-09-16 19:57:18 +00004796 ssl->transform_in = NULL;
4797 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004798
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004799 ssl->renego_records_seen = 0;
4800
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004801 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4802 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004803
4804#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004805 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004806 {
4807 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004808 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004809 {
4810 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4811 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4812 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004813 }
4814#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004815
Paul Bakker48916f92012-09-16 19:57:18 +00004816 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004817 {
Paul Bakker48916f92012-09-16 19:57:18 +00004818 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004819 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004820 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004821 }
Paul Bakker48916f92012-09-16 19:57:18 +00004822
Paul Bakkerc0463502013-02-14 11:19:38 +01004823 if( ssl->session )
4824 {
4825 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004826 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004827 ssl->session = NULL;
4828 }
4829
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004830#if defined(POLARSSL_SSL_ALPN)
4831 ssl->alpn_chosen = NULL;
4832#endif
4833
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004834#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004835 polarssl_free( ssl->cli_id );
4836 ssl->cli_id = NULL;
4837 ssl->cli_id_len = 0;
4838#endif
4839
Paul Bakker48916f92012-09-16 19:57:18 +00004840 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4841 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004842
4843 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004844}
4845
Paul Bakkera503a632013-08-14 13:48:06 +02004846#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004847static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4848{
4849 aes_free( &tkeys->enc );
4850 aes_free( &tkeys->dec );
4851
4852 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4853}
4854
Paul Bakker7eb013f2011-10-06 12:37:39 +00004855/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004856 * Allocate and initialize ticket keys
4857 */
4858static int ssl_ticket_keys_init( ssl_context *ssl )
4859{
4860 int ret;
4861 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004862 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004863
4864 if( ssl->ticket_keys != NULL )
4865 return( 0 );
4866
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004867 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4868 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004869 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4870
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004871 aes_init( &tkeys->enc );
4872 aes_init( &tkeys->dec );
4873
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004874 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004875 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004876 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004877 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004878 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004879 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004880
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004881 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4882 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4883 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4884 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004885 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004886 polarssl_free( tkeys );
4887 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004888 }
4889
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004890 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004891 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004892 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004893 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004894 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004895 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004896
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004897 ssl->ticket_keys = tkeys;
4898
4899 return( 0 );
4900}
Paul Bakkera503a632013-08-14 13:48:06 +02004901#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004902
4903/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 * SSL set accessors
4905 */
4906void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4907{
4908 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004909
Paul Bakker606b4ba2013-08-14 16:52:14 +02004910#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004911 if( endpoint == SSL_IS_CLIENT )
4912 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004913#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004914}
4915
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004916int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004917{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004918#if defined(POLARSSL_SSL_PROTO_DTLS)
4919 if( transport == SSL_TRANSPORT_DATAGRAM )
4920 {
4921 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004922
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004923 ssl->out_hdr = ssl->out_buf;
4924 ssl->out_ctr = ssl->out_buf + 3;
4925 ssl->out_len = ssl->out_buf + 11;
4926 ssl->out_iv = ssl->out_buf + 13;
4927 ssl->out_msg = ssl->out_buf + 13;
4928
4929 ssl->in_hdr = ssl->in_buf;
4930 ssl->in_ctr = ssl->in_buf + 3;
4931 ssl->in_len = ssl->in_buf + 11;
4932 ssl->in_iv = ssl->in_buf + 13;
4933 ssl->in_msg = ssl->in_buf + 13;
4934
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004935 /* DTLS starts with TLS1.1 */
4936 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4937 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004938
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004939 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4940 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4941
4942 return( 0 );
4943 }
4944#endif
4945
4946 if( transport == SSL_TRANSPORT_STREAM )
4947 {
4948 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004949
4950 ssl->out_ctr = ssl->out_buf;
4951 ssl->out_hdr = ssl->out_buf + 8;
4952 ssl->out_len = ssl->out_buf + 11;
4953 ssl->out_iv = ssl->out_buf + 13;
4954 ssl->out_msg = ssl->out_buf + 13;
4955
4956 ssl->in_ctr = ssl->in_buf;
4957 ssl->in_hdr = ssl->in_buf + 8;
4958 ssl->in_len = ssl->in_buf + 11;
4959 ssl->in_iv = ssl->in_buf + 13;
4960 ssl->in_msg = ssl->in_buf + 13;
4961
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004962 return( 0 );
4963 }
4964
4965 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004966}
4967
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004968#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4969void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4970{
4971 ssl->anti_replay = mode;
4972}
4973#endif
4974
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02004975#if defined(POLARSSL_SSL_DTLS_BADMAC_LIMIT)
4976void ssl_set_dtls_badmac_limit( ssl_context *ssl, unsigned limit )
4977{
4978 ssl->badmac_limit = limit;
4979}
4980#endif
4981
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004982#if defined(POLARSSL_SSL_PROTO_DTLS)
4983void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4984{
4985 ssl->hs_timeout_min = min;
4986 ssl->hs_timeout_max = max;
4987}
4988#endif
4989
Paul Bakker5121ce52009-01-03 21:22:43 +00004990void ssl_set_authmode( ssl_context *ssl, int authmode )
4991{
4992 ssl->authmode = authmode;
4993}
4994
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004995#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004996void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004997 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004998 void *p_vrfy )
4999{
5000 ssl->f_vrfy = f_vrfy;
5001 ssl->p_vrfy = p_vrfy;
5002}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005003#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00005004
Paul Bakker5121ce52009-01-03 21:22:43 +00005005void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00005006 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00005007 void *p_rng )
5008{
5009 ssl->f_rng = f_rng;
5010 ssl->p_rng = p_rng;
5011}
5012
5013void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00005014 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00005015 void *p_dbg )
5016{
5017 ssl->f_dbg = f_dbg;
5018 ssl->p_dbg = p_dbg;
5019}
5020
5021void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00005022 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00005023 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00005024{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005025 if( p_recv != p_send )
5026 {
5027 ssl->f_recv = NULL;
5028 ssl->f_send = NULL;
5029 ssl->p_bio = NULL;
5030 return;
5031 }
5032
Paul Bakker5121ce52009-01-03 21:22:43 +00005033 ssl->f_recv = f_recv;
5034 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02005035 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00005036}
5037
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005038void ssl_set_bio_timeout( ssl_context *ssl,
5039 void *p_bio,
5040 int (*f_send)(void *, const unsigned char *, size_t),
5041 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02005042 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
5043 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005044{
5045 ssl->p_bio = p_bio;
5046 ssl->f_send = f_send;
5047 ssl->f_recv = f_recv;
5048 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02005049 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02005050}
5051
Paul Bakker0a597072012-09-25 21:55:46 +00005052void ssl_set_session_cache( ssl_context *ssl,
5053 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
5054 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00005055{
Paul Bakker0a597072012-09-25 21:55:46 +00005056 ssl->f_get_cache = f_get_cache;
5057 ssl->p_get_cache = p_get_cache;
5058 ssl->f_set_cache = f_set_cache;
5059 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00005060}
5061
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005062int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00005063{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005064 int ret;
5065
5066 if( ssl == NULL ||
5067 session == NULL ||
5068 ssl->session_negotiate == NULL ||
5069 ssl->endpoint != SSL_IS_CLIENT )
5070 {
5071 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5072 }
5073
5074 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5075 return( ret );
5076
Paul Bakker0a597072012-09-25 21:55:46 +00005077 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005078
5079 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005080}
5081
Paul Bakkerb68cad62012-08-23 08:34:18 +00005082void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005083{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005084 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5085 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5086 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5087 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5088}
5089
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005090void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5091 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005092 int major, int minor )
5093{
5094 if( major != SSL_MAJOR_VERSION_3 )
5095 return;
5096
5097 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5098 return;
5099
5100 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005101}
5102
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005103#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005104/* Add a new (empty) key_cert entry an return a pointer to it */
5105static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5106{
5107 ssl_key_cert *key_cert, *last;
5108
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005109 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5110 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005111 return( NULL );
5112
5113 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5114
5115 /* Append the new key_cert to the (possibly empty) current list */
5116 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005117 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005118 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005119 if( ssl->handshake != NULL )
5120 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005121 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005122 else
5123 {
5124 last = ssl->key_cert;
5125 while( last->next != NULL )
5126 last = last->next;
5127 last->next = key_cert;
5128 }
5129
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005130 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005131}
5132
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005133void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005134 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005135{
5136 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005137 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005138 ssl->peer_cn = peer_cn;
5139}
5140
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005141int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005142 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005143{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005144 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5145
5146 if( key_cert == NULL )
5147 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5148
5149 key_cert->cert = own_cert;
5150 key_cert->key = pk_key;
5151
5152 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005153}
5154
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005155#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005156int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005157 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005158{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005159 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005160 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005161
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005162 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005163 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5164
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005165 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5166 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005167 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005168
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005169 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005170
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005171 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005172 if( ret != 0 )
5173 return( ret );
5174
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005175 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005176 return( ret );
5177
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005178 key_cert->cert = own_cert;
5179 key_cert->key_own_alloc = 1;
5180
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005181 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005182}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005183#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005184
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005185int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005186 void *rsa_key,
5187 rsa_decrypt_func rsa_decrypt,
5188 rsa_sign_func rsa_sign,
5189 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005190{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005191 int ret;
5192 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005193
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005194 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005195 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5196
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005197 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5198 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005199 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005200
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005201 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005202
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005203 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5204 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5205 return( ret );
5206
5207 key_cert->cert = own_cert;
5208 key_cert->key_own_alloc = 1;
5209
5210 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005211}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005212#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005213
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005214#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005215int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5216 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005217{
Paul Bakker6db455e2013-09-18 17:29:31 +02005218 if( psk == NULL || psk_identity == NULL )
5219 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5220
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005221 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005222 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5223
Paul Bakker6db455e2013-09-18 17:29:31 +02005224 if( ssl->psk != NULL )
5225 {
5226 polarssl_free( ssl->psk );
5227 polarssl_free( ssl->psk_identity );
5228 }
5229
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005230 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005231 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005232
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005233 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005234 ssl->psk_identity = (unsigned char *)
5235 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005236
5237 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5238 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5239
5240 memcpy( ssl->psk, psk, ssl->psk_len );
5241 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005242
5243 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005244}
5245
5246void ssl_set_psk_cb( ssl_context *ssl,
5247 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5248 size_t),
5249 void *p_psk )
5250{
5251 ssl->f_psk = f_psk;
5252 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005253}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005254#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005255
Paul Bakker48916f92012-09-16 19:57:18 +00005256#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005257int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005258{
5259 int ret;
5260
Paul Bakker48916f92012-09-16 19:57:18 +00005261 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005262 {
5263 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5264 return( ret );
5265 }
5266
Paul Bakker48916f92012-09-16 19:57:18 +00005267 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005268 {
5269 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5270 return( ret );
5271 }
5272
5273 return( 0 );
5274}
5275
Paul Bakker1b57b062011-01-06 15:48:19 +00005276int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5277{
5278 int ret;
5279
Paul Bakker66d5d072014-06-17 16:39:18 +02005280 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005281 {
5282 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5283 return( ret );
5284 }
5285
Paul Bakker66d5d072014-06-17 16:39:18 +02005286 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005287 {
5288 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5289 return( ret );
5290 }
5291
5292 return( 0 );
5293}
Paul Bakker48916f92012-09-16 19:57:18 +00005294#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005295
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005296#if defined(POLARSSL_SSL_SET_CURVES)
5297/*
5298 * Set the allowed elliptic curves
5299 */
5300void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5301{
5302 ssl->curve_list = curve_list;
5303}
5304#endif
5305
Paul Bakker0be444a2013-08-27 21:55:01 +02005306#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005307int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005308{
5309 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005310 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005311
5312 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005313
5314 if( ssl->hostname_len + 1 == 0 )
5315 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5316
Paul Bakker6e339b52013-07-03 13:37:05 +02005317 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005318
Paul Bakkerb15b8512012-01-13 13:44:06 +00005319 if( ssl->hostname == NULL )
5320 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5321
Paul Bakker3c2122f2013-06-24 19:03:14 +02005322 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005323 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005324
Paul Bakker40ea7de2009-05-03 10:18:48 +00005325 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005326
5327 return( 0 );
5328}
5329
Paul Bakker5701cdc2012-09-27 21:49:42 +00005330void ssl_set_sni( ssl_context *ssl,
5331 int (*f_sni)(void *, ssl_context *,
5332 const unsigned char *, size_t),
5333 void *p_sni )
5334{
5335 ssl->f_sni = f_sni;
5336 ssl->p_sni = p_sni;
5337}
Paul Bakker0be444a2013-08-27 21:55:01 +02005338#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005339
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005340#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005341int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005342{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005343 size_t cur_len, tot_len;
5344 const char **p;
5345
5346 /*
5347 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5348 * truncated". Check lengths now rather than later.
5349 */
5350 tot_len = 0;
5351 for( p = protos; *p != NULL; p++ )
5352 {
5353 cur_len = strlen( *p );
5354 tot_len += cur_len;
5355
5356 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5357 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5358 }
5359
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005360 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005361
5362 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005363}
5364
5365const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5366{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005367 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005368}
5369#endif /* POLARSSL_SSL_ALPN */
5370
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005371static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005372{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005373 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005374 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005375 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005376 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005377 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005378
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005379#if defined(POLARSSL_SSL_PROTO_DTLS)
5380 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5381 minor < SSL_MINOR_VERSION_2 )
5382 {
5383 return( -1 );
5384 }
5385#else
5386 ((void) ssl);
5387#endif
5388
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005389 return( 0 );
5390}
5391
5392int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5393{
5394 if( ssl_check_version( ssl, major, minor ) != 0 )
5395 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5396
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005397 ssl->max_major_ver = major;
5398 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005399
5400 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005401}
5402
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005403int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005404{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005405 if( ssl_check_version( ssl, major, minor ) != 0 )
5406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005407
5408 ssl->min_major_ver = major;
5409 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005410
5411 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005412}
5413
Paul Bakker05decb22013-08-15 13:33:48 +02005414#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005415int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5416{
Paul Bakker77e257e2013-12-16 15:29:52 +01005417 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005418 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005419 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005420 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005421 }
5422
5423 ssl->mfl_code = mfl_code;
5424
5425 return( 0 );
5426}
Paul Bakker05decb22013-08-15 13:33:48 +02005427#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005428
Paul Bakker1f2bc622013-08-15 13:45:55 +02005429#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005430int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005431{
5432 if( ssl->endpoint != SSL_IS_CLIENT )
5433 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5434
Paul Bakker8c1ede62013-07-19 14:14:37 +02005435 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005436
5437 return( 0 );
5438}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005439#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005440
Paul Bakker48916f92012-09-16 19:57:18 +00005441void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5442{
5443 ssl->disable_renegotiation = renegotiation;
5444}
5445
5446void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5447{
5448 ssl->allow_legacy_renegotiation = allow_legacy;
5449}
5450
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005451void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5452{
5453 ssl->renego_max_records = max_records;
5454}
5455
Paul Bakkera503a632013-08-14 13:48:06 +02005456#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005457int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5458{
5459 ssl->session_tickets = use_tickets;
5460
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005461 if( ssl->endpoint == SSL_IS_CLIENT )
5462 return( 0 );
5463
5464 if( ssl->f_rng == NULL )
5465 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5466
5467 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005468}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005469
5470void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5471{
5472 ssl->ticket_lifetime = lifetime;
5473}
Paul Bakkera503a632013-08-14 13:48:06 +02005474#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005475
Paul Bakker5121ce52009-01-03 21:22:43 +00005476/*
5477 * SSL get accessors
5478 */
Paul Bakker23986e52011-04-24 08:57:21 +00005479size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480{
5481 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5482}
5483
Paul Bakkerff60ee62010-03-16 21:09:09 +00005484int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005485{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005486 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005487}
5488
Paul Bakkere3166ce2011-01-27 17:40:50 +00005489const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005490{
Paul Bakker926c8e42013-03-06 10:23:34 +01005491 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005492 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005493
Paul Bakkere3166ce2011-01-27 17:40:50 +00005494 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005495}
5496
Paul Bakker43ca69c2011-01-15 17:35:19 +00005497const char *ssl_get_version( const ssl_context *ssl )
5498{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005499#if defined(POLARSSL_SSL_PROTO_DTLS)
5500 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5501 {
5502 switch( ssl->minor_ver )
5503 {
5504 case SSL_MINOR_VERSION_2:
5505 return( "DTLSv1.0" );
5506
5507 case SSL_MINOR_VERSION_3:
5508 return( "DTLSv1.2" );
5509
5510 default:
5511 return( "unknown (DTLS)" );
5512 }
5513 }
5514#endif
5515
Paul Bakker43ca69c2011-01-15 17:35:19 +00005516 switch( ssl->minor_ver )
5517 {
5518 case SSL_MINOR_VERSION_0:
5519 return( "SSLv3.0" );
5520
5521 case SSL_MINOR_VERSION_1:
5522 return( "TLSv1.0" );
5523
5524 case SSL_MINOR_VERSION_2:
5525 return( "TLSv1.1" );
5526
Paul Bakker1ef83d62012-04-11 12:09:53 +00005527 case SSL_MINOR_VERSION_3:
5528 return( "TLSv1.2" );
5529
Paul Bakker43ca69c2011-01-15 17:35:19 +00005530 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005531 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005532 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005533}
5534
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02005535int ssl_get_record_expansion( const ssl_context *ssl )
5536{
5537 int transform_expansion;
5538 const ssl_transform *transform = ssl->transform_out;
5539
5540#if defined(POLARSSL_ZLIB_SUPPORT)
5541 if( ssl->session_out->compression != SSL_COMPRESS_NULL )
5542 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
5543#endif
5544
5545 if( transform == NULL )
5546 return( ssl_hdr_len( ssl ) );
5547
5548 switch( cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
5549 {
5550 case POLARSSL_MODE_GCM:
5551 case POLARSSL_MODE_CCM:
5552 case POLARSSL_MODE_STREAM:
5553 transform_expansion = transform->minlen;
5554 break;
5555
5556 case POLARSSL_MODE_CBC:
5557 transform_expansion = transform->maclen
5558 + cipher_get_block_size( &transform->cipher_ctx_enc );
5559 break;
5560
5561 default:
5562 SSL_DEBUG_MSG( 0, ( "should never happen" ) );
5563 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
5564 }
5565
5566 return( ssl_hdr_len( ssl ) + transform_expansion );
5567}
5568
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005569#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005570const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005571{
5572 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005573 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005574
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005575 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005576}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005577#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005578
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005579int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5580{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005581 if( ssl == NULL ||
5582 dst == NULL ||
5583 ssl->session == NULL ||
5584 ssl->endpoint != SSL_IS_CLIENT )
5585 {
5586 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5587 }
5588
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005589 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005590}
5591
Paul Bakker5121ce52009-01-03 21:22:43 +00005592/*
Paul Bakker1961b702013-01-25 14:49:24 +01005593 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005594 */
Paul Bakker1961b702013-01-25 14:49:24 +01005595int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005596{
Paul Bakker40e46942009-01-03 21:51:57 +00005597 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005598
Paul Bakker40e46942009-01-03 21:51:57 +00005599#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005600 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005601 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005602#endif
5603
Paul Bakker40e46942009-01-03 21:51:57 +00005604#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005605 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005606 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005607#endif
5608
Paul Bakker1961b702013-01-25 14:49:24 +01005609 return( ret );
5610}
5611
5612/*
5613 * Perform the SSL handshake
5614 */
5615int ssl_handshake( ssl_context *ssl )
5616{
5617 int ret = 0;
5618
5619 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5620
5621 while( ssl->state != SSL_HANDSHAKE_OVER )
5622 {
5623 ret = ssl_handshake_step( ssl );
5624
5625 if( ret != 0 )
5626 break;
5627 }
5628
Paul Bakker5121ce52009-01-03 21:22:43 +00005629 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5630
5631 return( ret );
5632}
5633
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005634#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005635/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005636 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005637 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005638static int ssl_write_hello_request( ssl_context *ssl )
5639{
5640 int ret;
5641
5642 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5643
5644 ssl->out_msglen = 4;
5645 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5646 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5647
5648 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5649 {
5650 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5651 return( ret );
5652 }
5653
5654 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5655
5656 return( 0 );
5657}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005658#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005659
5660/*
5661 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005662 * - any side: calling ssl_renegotiate(),
5663 * - client: receiving a HelloRequest during ssl_read(),
5664 * - server: receiving any handshake message on server during ssl_read() after
5665 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005666 * If the handshake doesn't complete due to waiting for I/O, it will continue
5667 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005668 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005669static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005670{
5671 int ret;
5672
5673 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5674
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005675 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5676 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005677
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005678 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5679 * the ServerHello will have message_seq = 1" */
5680#if defined(POLARSSL_SSL_PROTO_DTLS)
5681 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005682 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5683 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005684 if( ssl->endpoint == SSL_IS_SERVER )
5685 ssl->handshake->out_msg_seq = 1;
5686 else
5687 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005688 }
5689#endif
5690
Paul Bakker48916f92012-09-16 19:57:18 +00005691 ssl->state = SSL_HELLO_REQUEST;
5692 ssl->renegotiation = SSL_RENEGOTIATION;
5693
Paul Bakker48916f92012-09-16 19:57:18 +00005694 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5695 {
5696 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5697 return( ret );
5698 }
5699
5700 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5701
5702 return( 0 );
5703}
5704
5705/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005706 * Renegotiate current connection on client,
5707 * or request renegotiation on server
5708 */
5709int ssl_renegotiate( ssl_context *ssl )
5710{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005711 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005712
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005713#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005714 /* On server, just send the request */
5715 if( ssl->endpoint == SSL_IS_SERVER )
5716 {
5717 if( ssl->state != SSL_HANDSHAKE_OVER )
5718 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5719
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005720 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5721
5722 /* Did we already try/start sending HelloRequest? */
5723 if( ssl->out_left != 0 )
5724 return( ssl_flush_output( ssl ) );
5725
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005726 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005727 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005728#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005729
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005730#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005731 /*
5732 * On client, either start the renegotiation process or,
5733 * if already in progress, continue the handshake
5734 */
5735 if( ssl->renegotiation != SSL_RENEGOTIATION )
5736 {
5737 if( ssl->state != SSL_HANDSHAKE_OVER )
5738 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5739
5740 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5741 {
5742 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5743 return( ret );
5744 }
5745 }
5746 else
5747 {
5748 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5749 {
5750 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5751 return( ret );
5752 }
5753 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005754#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005755
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005756 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005757}
5758
5759/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005760 * Receive application data decrypted from the SSL layer
5761 */
Paul Bakker23986e52011-04-24 08:57:21 +00005762int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005763{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005764 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005765 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005766
5767 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5768
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005769#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005770 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005771 {
5772 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5773 return( ret );
5774
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005775 if( ssl->handshake != NULL &&
5776 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5777 {
5778 if( ( ret = ssl_resend( ssl ) ) != 0 )
5779 return( ret );
5780 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005781 }
5782#endif
5783
Paul Bakker5121ce52009-01-03 21:22:43 +00005784 if( ssl->state != SSL_HANDSHAKE_OVER )
5785 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005786 ret = ssl_handshake( ssl );
5787 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5788 {
5789 record_read = 1;
5790 }
5791 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005792 {
5793 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5794 return( ret );
5795 }
5796 }
5797
5798 if( ssl->in_offt == NULL )
5799 {
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005800#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005801 /* Start timer if not already running */
5802 if( ssl->time_limit == 0 )
5803 ssl_set_timer( ssl, ssl->read_timeout );
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005804#endif
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005805
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005806 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005807 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005808 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5809 {
5810 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5811 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005812
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005813 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5814 return( ret );
5815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 }
5817
5818 if( ssl->in_msglen == 0 &&
5819 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5820 {
5821 /*
5822 * OpenSSL sends empty messages to randomize the IV
5823 */
5824 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5825 {
Paul Bakker831a7552011-05-18 13:32:51 +00005826 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5827 return( 0 );
5828
Paul Bakker5121ce52009-01-03 21:22:43 +00005829 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5830 return( ret );
5831 }
5832 }
5833
Paul Bakker48916f92012-09-16 19:57:18 +00005834 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5835 {
5836 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5837
5838 if( ssl->endpoint == SSL_IS_CLIENT &&
5839 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005840 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005841 {
5842 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005843
5844 /* With DTLS, drop the packet (probably from last handshake) */
5845#if defined(POLARSSL_SSL_PROTO_DTLS)
5846 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5847 return( POLARSSL_ERR_NET_WANT_READ );
5848#endif
5849 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5850 }
5851
5852 if( ssl->endpoint == SSL_IS_SERVER &&
5853 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5854 {
5855 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5856
5857 /* With DTLS, drop the packet (probably from last handshake) */
5858#if defined(POLARSSL_SSL_PROTO_DTLS)
5859 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5860 return( POLARSSL_ERR_NET_WANT_READ );
5861#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005862 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5863 }
5864
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005865 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5866 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005867 ssl->allow_legacy_renegotiation ==
5868 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005869 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005870 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005871
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005872#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005873 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005874 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005875 /*
5876 * SSLv3 does not have a "no_renegotiation" alert
5877 */
5878 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5879 return( ret );
5880 }
5881 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005882#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005883#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5884 defined(POLARSSL_SSL_PROTO_TLS1_2)
5885 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005886 {
5887 if( ( ret = ssl_send_alert_message( ssl,
5888 SSL_ALERT_LEVEL_WARNING,
5889 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5890 {
5891 return( ret );
5892 }
Paul Bakker48916f92012-09-16 19:57:18 +00005893 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005894 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005895#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5896 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005897 {
5898 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005899 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005900 }
Paul Bakker48916f92012-09-16 19:57:18 +00005901 }
5902 else
5903 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005904 /* DTLS clients need to know renego is server-initiated */
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02005905#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005906 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5907 ssl->endpoint == SSL_IS_CLIENT )
5908 {
5909 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5910 }
5911#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005912 ret = ssl_start_renegotiation( ssl );
5913 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5914 {
5915 record_read = 1;
5916 }
5917 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005918 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005919 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005920 return( ret );
5921 }
Paul Bakker48916f92012-09-16 19:57:18 +00005922 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005923
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005924 /* If a non-handshake record was read during renego, fallthrough,
5925 * else tell the user they should call ssl_read() again */
5926 if( ! record_read )
5927 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005928 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005929 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5930 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005931
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005932 if( ssl->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005933 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005934 if( ++ssl->renego_records_seen > ssl->renego_max_records )
5935 {
5936 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5937 "but not honored by client" ) );
5938 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5939 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005940 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005941 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005942
5943 /* Fatal and closure alerts handled by ssl_read_record() */
5944 if( ssl->in_msgtype == SSL_MSG_ALERT )
5945 {
5946 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5947 return( POLARSSL_ERR_NET_WANT_READ );
5948 }
5949
5950 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005951 {
5952 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005953 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005954 }
5955
5956 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005957
Manuel Pégourié-Gonnard8e704f02014-10-14 20:03:35 +02005958#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02005959 /* We're going to return something now, cancel timer,
5960 * except if handshake (renegotiation) is in progress */
5961 if( ssl->state == SSL_HANDSHAKE_OVER )
5962 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005963
5964 /* If we requested renego but received AppData, resend HelloRequest.
5965 * Do it now, after setting in_offt, to avoid taking this branch
5966 * again if ssl_write_hello_request() returns WANT_WRITE */
5967#if defined(POLARSSL_SSL_SRV_C)
5968 if( ssl->endpoint == SSL_IS_SERVER &&
5969 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5970 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005971 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005972 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02005973 SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02005974 return( ret );
5975 }
5976 }
5977#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnardf1e9b092014-10-02 18:08:53 +02005978#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005979 }
5980
5981 n = ( len < ssl->in_msglen )
5982 ? len : ssl->in_msglen;
5983
5984 memcpy( buf, ssl->in_offt, n );
5985 ssl->in_msglen -= n;
5986
5987 if( ssl->in_msglen == 0 )
5988 /* all bytes consumed */
5989 ssl->in_offt = NULL;
5990 else
5991 /* more data available */
5992 ssl->in_offt += n;
5993
5994 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5995
Paul Bakker23986e52011-04-24 08:57:21 +00005996 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005997}
5998
5999/*
6000 * Send application data to be encrypted by the SSL layer
6001 */
Paul Bakker23986e52011-04-24 08:57:21 +00006002int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00006003{
Paul Bakker23986e52011-04-24 08:57:21 +00006004 int ret;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006005#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
6006 unsigned int max_len;
6007#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006008
6009 SSL_DEBUG_MSG( 2, ( "=> write" ) );
6010
6011 if( ssl->state != SSL_HANDSHAKE_OVER )
6012 {
6013 if( ( ret = ssl_handshake( ssl ) ) != 0 )
6014 {
6015 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
6016 return( ret );
6017 }
6018 }
6019
Paul Bakker05decb22013-08-15 13:33:48 +02006020#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02006021 /*
6022 * Assume mfl_code is correct since it was checked when set
6023 */
6024 max_len = mfl_code_to_length[ssl->mfl_code];
6025
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006026 /*
Paul Bakker05decb22013-08-15 13:33:48 +02006027 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02006028 */
6029 if( ssl->session_out != NULL &&
6030 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
6031 {
6032 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
6033 }
6034
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006035 if( len > max_len )
6036 {
6037#if defined(POLARSSL_SSL_PROTO_DTLS)
6038 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
6039 {
6040 SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
6041 "maximum fragment length: %d > %d",
6042 len, max_len ) );
6043 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
6044 }
6045 else
6046#endif
6047 len = max_len;
6048 }
6049#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker887bd502011-06-08 13:10:54 +00006050
Paul Bakker5121ce52009-01-03 21:22:43 +00006051 if( ssl->out_left != 0 )
6052 {
6053 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
6054 {
6055 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
6056 return( ret );
6057 }
6058 }
Paul Bakker887bd502011-06-08 13:10:54 +00006059 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00006060 {
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006061 ssl->out_msglen = len;
Paul Bakker887bd502011-06-08 13:10:54 +00006062 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006063 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00006064
6065 if( ( ret = ssl_write_record( ssl ) ) != 0 )
6066 {
6067 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
6068 return( ret );
6069 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 }
6071
6072 SSL_DEBUG_MSG( 2, ( "<= write" ) );
6073
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02006074 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00006075}
6076
6077/*
6078 * Notify the peer that the connection is being closed
6079 */
6080int ssl_close_notify( ssl_context *ssl )
6081{
6082 int ret;
6083
6084 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
6085
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006086 if( ssl->out_left != 0 )
6087 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006088
6089 if( ssl->state == SSL_HANDSHAKE_OVER )
6090 {
Paul Bakker48916f92012-09-16 19:57:18 +00006091 if( ( ret = ssl_send_alert_message( ssl,
6092 SSL_ALERT_LEVEL_WARNING,
6093 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006094 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006095 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006096 return( ret );
6097 }
6098 }
6099
6100 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
6101
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02006102 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00006103}
6104
Paul Bakker48916f92012-09-16 19:57:18 +00006105void ssl_transform_free( ssl_transform *transform )
6106{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006107 if( transform == NULL )
6108 return;
6109
Paul Bakker48916f92012-09-16 19:57:18 +00006110#if defined(POLARSSL_ZLIB_SUPPORT)
6111 deflateEnd( &transform->ctx_deflate );
6112 inflateEnd( &transform->ctx_inflate );
6113#endif
6114
Paul Bakker84bbeb52014-07-01 14:53:22 +02006115 cipher_free( &transform->cipher_ctx_enc );
6116 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02006117
Paul Bakker84bbeb52014-07-01 14:53:22 +02006118 md_free( &transform->md_ctx_enc );
6119 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02006120
Paul Bakker34617722014-06-13 17:20:13 +02006121 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006122}
6123
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006124#if defined(POLARSSL_X509_CRT_PARSE_C)
6125static void ssl_key_cert_free( ssl_key_cert *key_cert )
6126{
6127 ssl_key_cert *cur = key_cert, *next;
6128
6129 while( cur != NULL )
6130 {
6131 next = cur->next;
6132
6133 if( cur->key_own_alloc )
6134 {
6135 pk_free( cur->key );
6136 polarssl_free( cur->key );
6137 }
6138 polarssl_free( cur );
6139
6140 cur = next;
6141 }
6142}
6143#endif /* POLARSSL_X509_CRT_PARSE_C */
6144
Paul Bakker48916f92012-09-16 19:57:18 +00006145void ssl_handshake_free( ssl_handshake_params *handshake )
6146{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006147 if( handshake == NULL )
6148 return;
6149
Paul Bakker48916f92012-09-16 19:57:18 +00006150#if defined(POLARSSL_DHM_C)
6151 dhm_free( &handshake->dhm_ctx );
6152#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02006153#if defined(POLARSSL_ECDH_C)
6154 ecdh_free( &handshake->ecdh_ctx );
6155#endif
6156
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006157#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006158 /* explicit void pointer cast for buggy MS compiler */
6159 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006160#endif
6161
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006162#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6163 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6164 /*
6165 * Free only the linked list wrapper, not the keys themselves
6166 * since the belong to the SNI callback
6167 */
6168 if( handshake->sni_key_cert != NULL )
6169 {
6170 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6171
6172 while( cur != NULL )
6173 {
6174 next = cur->next;
6175 polarssl_free( cur );
6176 cur = next;
6177 }
6178 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006179#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006180
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006181#if defined(POLARSSL_SSL_PROTO_DTLS)
6182 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006183 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006184 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006185#endif
6186
Paul Bakker34617722014-06-13 17:20:13 +02006187 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006188}
6189
6190void ssl_session_free( ssl_session *session )
6191{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006192 if( session == NULL )
6193 return;
6194
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006195#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006196 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006197 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006198 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006199 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006200 }
Paul Bakkered27a042013-04-18 22:46:23 +02006201#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006202
Paul Bakkera503a632013-08-14 13:48:06 +02006203#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006204 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006205#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006206
Paul Bakker34617722014-06-13 17:20:13 +02006207 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006208}
6209
Paul Bakker5121ce52009-01-03 21:22:43 +00006210/*
6211 * Free an SSL context
6212 */
6213void ssl_free( ssl_context *ssl )
6214{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006215 if( ssl == NULL )
6216 return;
6217
Paul Bakker5121ce52009-01-03 21:22:43 +00006218 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6219
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006220 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006222 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6223 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006224 }
6225
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006226 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006227 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006228 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6229 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 }
6231
Paul Bakker16770332013-10-11 09:59:44 +02006232#if defined(POLARSSL_ZLIB_SUPPORT)
6233 if( ssl->compress_buf != NULL )
6234 {
Paul Bakker34617722014-06-13 17:20:13 +02006235 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006236 polarssl_free( ssl->compress_buf );
6237 }
6238#endif
6239
Paul Bakker40e46942009-01-03 21:51:57 +00006240#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006241 mpi_free( &ssl->dhm_P );
6242 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243#endif
6244
Paul Bakker48916f92012-09-16 19:57:18 +00006245 if( ssl->transform )
6246 {
6247 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006248 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006249 }
6250
6251 if( ssl->handshake )
6252 {
6253 ssl_handshake_free( ssl->handshake );
6254 ssl_transform_free( ssl->transform_negotiate );
6255 ssl_session_free( ssl->session_negotiate );
6256
Paul Bakker6e339b52013-07-03 13:37:05 +02006257 polarssl_free( ssl->handshake );
6258 polarssl_free( ssl->transform_negotiate );
6259 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006260 }
6261
Paul Bakkerc0463502013-02-14 11:19:38 +01006262 if( ssl->session )
6263 {
6264 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006265 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006266 }
6267
Paul Bakkera503a632013-08-14 13:48:06 +02006268#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006269 if( ssl->ticket_keys )
6270 {
6271 ssl_ticket_keys_free( ssl->ticket_keys );
6272 polarssl_free( ssl->ticket_keys );
6273 }
Paul Bakkera503a632013-08-14 13:48:06 +02006274#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006275
Paul Bakker0be444a2013-08-27 21:55:01 +02006276#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006277 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006278 {
Paul Bakker34617722014-06-13 17:20:13 +02006279 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006280 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006281 ssl->hostname_len = 0;
6282 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006283#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006284
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006285#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006286 if( ssl->psk != NULL )
6287 {
Paul Bakker34617722014-06-13 17:20:13 +02006288 polarssl_zeroize( ssl->psk, ssl->psk_len );
6289 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006290 polarssl_free( ssl->psk );
6291 polarssl_free( ssl->psk_identity );
6292 ssl->psk_len = 0;
6293 ssl->psk_identity_len = 0;
6294 }
6295#endif
6296
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006297#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006298 ssl_key_cert_free( ssl->key_cert );
6299#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006300
Paul Bakker05ef8352012-05-08 09:17:57 +00006301#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6302 if( ssl_hw_record_finish != NULL )
6303 {
6304 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6305 ssl_hw_record_finish( ssl );
6306 }
6307#endif
6308
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006309#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006310 polarssl_free( ssl->cli_id );
6311#endif
6312
Paul Bakker5121ce52009-01-03 21:22:43 +00006313 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006314
Paul Bakker86f04f42013-02-14 11:20:09 +01006315 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006316 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006317}
6318
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006319#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006320/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006321 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006322 */
6323unsigned char ssl_sig_from_pk( pk_context *pk )
6324{
6325#if defined(POLARSSL_RSA_C)
6326 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6327 return( SSL_SIG_RSA );
6328#endif
6329#if defined(POLARSSL_ECDSA_C)
6330 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6331 return( SSL_SIG_ECDSA );
6332#endif
6333 return( SSL_SIG_ANON );
6334}
6335
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006336pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6337{
6338 switch( sig )
6339 {
6340#if defined(POLARSSL_RSA_C)
6341 case SSL_SIG_RSA:
6342 return( POLARSSL_PK_RSA );
6343#endif
6344#if defined(POLARSSL_ECDSA_C)
6345 case SSL_SIG_ECDSA:
6346 return( POLARSSL_PK_ECDSA );
6347#endif
6348 default:
6349 return( POLARSSL_PK_NONE );
6350 }
6351}
Paul Bakker9af723c2014-05-01 13:03:14 +02006352#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006353
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006354/*
6355 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6356 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006357md_type_t ssl_md_alg_from_hash( unsigned char hash )
6358{
6359 switch( hash )
6360 {
6361#if defined(POLARSSL_MD5_C)
6362 case SSL_HASH_MD5:
6363 return( POLARSSL_MD_MD5 );
6364#endif
6365#if defined(POLARSSL_SHA1_C)
6366 case SSL_HASH_SHA1:
6367 return( POLARSSL_MD_SHA1 );
6368#endif
6369#if defined(POLARSSL_SHA256_C)
6370 case SSL_HASH_SHA224:
6371 return( POLARSSL_MD_SHA224 );
6372 case SSL_HASH_SHA256:
6373 return( POLARSSL_MD_SHA256 );
6374#endif
6375#if defined(POLARSSL_SHA512_C)
6376 case SSL_HASH_SHA384:
6377 return( POLARSSL_MD_SHA384 );
6378 case SSL_HASH_SHA512:
6379 return( POLARSSL_MD_SHA512 );
6380#endif
6381 default:
6382 return( POLARSSL_MD_NONE );
6383 }
6384}
6385
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006386#if defined(POLARSSL_SSL_SET_CURVES)
6387/*
6388 * Check is a curve proposed by the peer is in our list.
6389 * Return 1 if we're willing to use it, 0 otherwise.
6390 */
6391int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6392{
6393 const ecp_group_id *gid;
6394
6395 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6396 if( *gid == grp_id )
6397 return( 1 );
6398
6399 return( 0 );
6400}
Paul Bakker9af723c2014-05-01 13:03:14 +02006401#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006402
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006403#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006404int ssl_check_cert_usage( const x509_crt *cert,
6405 const ssl_ciphersuite_t *ciphersuite,
6406 int cert_endpoint )
6407{
6408#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6409 int usage = 0;
6410#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006411#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6412 const char *ext_oid;
6413 size_t ext_len;
6414#endif
6415
6416#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6417 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6418 ((void) cert);
6419 ((void) cert_endpoint);
6420#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006421
6422#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6423 if( cert_endpoint == SSL_IS_SERVER )
6424 {
6425 /* Server part of the key exchange */
6426 switch( ciphersuite->key_exchange )
6427 {
6428 case POLARSSL_KEY_EXCHANGE_RSA:
6429 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6430 usage = KU_KEY_ENCIPHERMENT;
6431 break;
6432
6433 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6434 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6435 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6436 usage = KU_DIGITAL_SIGNATURE;
6437 break;
6438
6439 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6440 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6441 usage = KU_KEY_AGREEMENT;
6442 break;
6443
6444 /* Don't use default: we want warnings when adding new values */
6445 case POLARSSL_KEY_EXCHANGE_NONE:
6446 case POLARSSL_KEY_EXCHANGE_PSK:
6447 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6448 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6449 usage = 0;
6450 }
6451 }
6452 else
6453 {
6454 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6455 usage = KU_DIGITAL_SIGNATURE;
6456 }
6457
6458 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6459 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006460#else
6461 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006462#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6463
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006464#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6465 if( cert_endpoint == SSL_IS_SERVER )
6466 {
6467 ext_oid = OID_SERVER_AUTH;
6468 ext_len = OID_SIZE( OID_SERVER_AUTH );
6469 }
6470 else
6471 {
6472 ext_oid = OID_CLIENT_AUTH;
6473 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6474 }
6475
6476 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6477 return( -1 );
6478#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6479
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006480 return( 0 );
6481}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006482#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006483
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006484/*
6485 * Convert version numbers to/from wire format
6486 * and, for DTLS, to/from TLS equivalent.
6487 *
6488 * For TLS this is the identity.
6489 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6490 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6491 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6492 */
6493void ssl_write_version( int major, int minor, int transport,
6494 unsigned char ver[2] )
6495{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006496#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006497 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006498 {
6499 if( minor == SSL_MINOR_VERSION_2 )
6500 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6501
6502 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6503 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6504 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006505 else
6506#else
6507 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006508#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006509 {
6510 ver[0] = (unsigned char) major;
6511 ver[1] = (unsigned char) minor;
6512 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006513}
6514
6515void ssl_read_version( int *major, int *minor, int transport,
6516 const unsigned char ver[2] )
6517{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006518#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006519 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006520 {
6521 *major = 255 - ver[0] + 2;
6522 *minor = 255 - ver[1] + 1;
6523
6524 if( *minor == SSL_MINOR_VERSION_1 )
6525 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6526 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006527 else
6528#else
6529 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006530#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006531 {
6532 *major = ver[0];
6533 *minor = ver[1];
6534 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006535}
6536
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006537#endif /* POLARSSL_SSL_TLS_C */