blob: 6c0f3ea29fa820776b7bf92abfa0f9ab17fd4a7f [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
82/*
83 * Timers (WIP)
84 */
85#if defined(POLARSSL_TIMING_C)
86/*
87 * Start a timer.
88 * Passing millisecs = 0 cancels a running timer.
89 * The timer is already running iff time_limit != 0.
90 */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +020091void ssl_set_timer( ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092{
93 ssl->time_limit = millisecs;
94 get_timer( &ssl->time_info, 1 );
95}
96
97/*
98 * Return -1 is timer is expired, 0 if it isn't.
99 */
100int ssl_check_timer( ssl_context *ssl )
101{
102 if( ssl->time_limit != 0 &&
103 get_timer( &ssl->time_info, 0 ) > ssl->time_limit )
104 {
105 return( -1 );
106 }
107
108 return( 0 );
109}
110#endif
111
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200112/*
113 * Double the retransmit timeout value, within the allowed range,
114 * returning -1 if the maximum value has already been reached.
115 */
116static int ssl_double_retransmit_timeout( ssl_context *ssl )
117{
118 uint32_t new_timeout;
119
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200120 if( ssl->handshake->retransmit_timeout >= ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200121 return( -1 );
122
123 new_timeout = 2 * ssl->handshake->retransmit_timeout;
124
125 /* Avoid arithmetic overflow and range overflow */
126 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200127 new_timeout > ssl->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200128 {
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200129 new_timeout = ssl->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200130 }
131
132 ssl->handshake->retransmit_timeout = new_timeout;
133 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
134 ssl->handshake->retransmit_timeout ) );
135
136 return( 0 );
137}
138
139static void ssl_reset_retransmit_timeout( ssl_context *ssl )
140{
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +0200141 ssl->handshake->retransmit_timeout = ssl->hs_timeout_min;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200142 SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
143 ssl->handshake->retransmit_timeout ) );
144}
145
Paul Bakker05decb22013-08-15 13:33:48 +0200146#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200147/*
148 * Convert max_fragment_length codes to length.
149 * RFC 6066 says:
150 * enum{
151 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
152 * } MaxFragmentLength;
153 * and we add 0 -> extension unused
154 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200155static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200156{
157 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
158 512, /* SSL_MAX_FRAG_LEN_512 */
159 1024, /* SSL_MAX_FRAG_LEN_1024 */
160 2048, /* SSL_MAX_FRAG_LEN_2048 */
161 4096, /* SSL_MAX_FRAG_LEN_4096 */
162};
Paul Bakker05decb22013-08-15 13:33:48 +0200163#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200164
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200165static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
166{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200167 ssl_session_free( dst );
168 memcpy( dst, src, sizeof( ssl_session ) );
169
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200171 if( src->peer_cert != NULL )
172 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200173 int ret;
174
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200175 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
176 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200177 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
178
Paul Bakkerb6b09562013-09-18 14:17:41 +0200179 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200180
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200181 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
182 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200183 {
184 polarssl_free( dst->peer_cert );
185 dst->peer_cert = NULL;
186 return( ret );
187 }
188 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200190
Paul Bakkera503a632013-08-14 13:48:06 +0200191#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200192 if( src->ticket != NULL )
193 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200194 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
195 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200196 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
197
198 memcpy( dst->ticket, src->ticket, src->ticket_len );
199 }
Paul Bakkera503a632013-08-14 13:48:06 +0200200#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200201
202 return( 0 );
203}
204
Paul Bakker05ef8352012-05-08 09:17:57 +0000205#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200206int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200207 const unsigned char *key_enc, const unsigned char *key_dec,
208 size_t keylen,
209 const unsigned char *iv_enc, const unsigned char *iv_dec,
210 size_t ivlen,
211 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200212 size_t maclen ) = NULL;
213int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
214int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
215int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
216int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
217int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200218#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000219
Paul Bakker5121ce52009-01-03 21:22:43 +0000220/*
221 * Key material generation
222 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200223#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200224static int ssl3_prf( const unsigned char *secret, size_t slen,
225 const char *label,
226 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000227 unsigned char *dstbuf, size_t dlen )
228{
229 size_t i;
230 md5_context md5;
231 sha1_context sha1;
232 unsigned char padding[16];
233 unsigned char sha1sum[20];
234 ((void)label);
235
Paul Bakker5b4af392014-06-26 12:09:34 +0200236 md5_init( &md5 );
237 sha1_init( &sha1 );
238
Paul Bakker5f70b252012-09-13 14:23:06 +0000239 /*
240 * SSLv3:
241 * block =
242 * MD5( secret + SHA1( 'A' + secret + random ) ) +
243 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
244 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
245 * ...
246 */
247 for( i = 0; i < dlen / 16; i++ )
248 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200249 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000250
251 sha1_starts( &sha1 );
252 sha1_update( &sha1, padding, 1 + i );
253 sha1_update( &sha1, secret, slen );
254 sha1_update( &sha1, random, rlen );
255 sha1_finish( &sha1, sha1sum );
256
257 md5_starts( &md5 );
258 md5_update( &md5, secret, slen );
259 md5_update( &md5, sha1sum, 20 );
260 md5_finish( &md5, dstbuf + i * 16 );
261 }
262
Paul Bakker5b4af392014-06-26 12:09:34 +0200263 md5_free( &md5 );
264 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000265
Paul Bakker34617722014-06-13 17:20:13 +0200266 polarssl_zeroize( padding, sizeof( padding ) );
267 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000268
269 return( 0 );
270}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200271#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000272
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200273#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200274static int tls1_prf( const unsigned char *secret, size_t slen,
275 const char *label,
276 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000277 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000278{
Paul Bakker23986e52011-04-24 08:57:21 +0000279 size_t nb, hs;
280 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200281 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 unsigned char tmp[128];
283 unsigned char h_i[20];
284
285 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000286 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 hs = ( slen + 1 ) / 2;
289 S1 = secret;
290 S2 = secret + slen - hs;
291
292 nb = strlen( label );
293 memcpy( tmp + 20, label, nb );
294 memcpy( tmp + 20 + nb, random, rlen );
295 nb += rlen;
296
297 /*
298 * First compute P_md5(secret,label+random)[0..dlen]
299 */
300 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
301
302 for( i = 0; i < dlen; i += 16 )
303 {
304 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
305 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
306
307 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
308
309 for( j = 0; j < k; j++ )
310 dstbuf[i + j] = h_i[j];
311 }
312
313 /*
314 * XOR out with P_sha1(secret,label+random)[0..dlen]
315 */
316 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
317
318 for( i = 0; i < dlen; i += 20 )
319 {
320 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
321 sha1_hmac( S2, hs, tmp, 20, tmp );
322
323 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
324
325 for( j = 0; j < k; j++ )
326 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
327 }
328
Paul Bakker34617722014-06-13 17:20:13 +0200329 polarssl_zeroize( tmp, sizeof( tmp ) );
330 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332 return( 0 );
333}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200334#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200336#if defined(POLARSSL_SSL_PROTO_TLS1_2)
337#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200338static int tls_prf_sha256( const unsigned char *secret, size_t slen,
339 const char *label,
340 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000341 unsigned char *dstbuf, size_t dlen )
342{
343 size_t nb;
344 size_t i, j, k;
345 unsigned char tmp[128];
346 unsigned char h_i[32];
347
348 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
349 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
350
351 nb = strlen( label );
352 memcpy( tmp + 32, label, nb );
353 memcpy( tmp + 32 + nb, random, rlen );
354 nb += rlen;
355
356 /*
357 * Compute P_<hash>(secret, label + random)[0..dlen]
358 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200359 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000360
361 for( i = 0; i < dlen; i += 32 )
362 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200363 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
364 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000365
366 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
367
368 for( j = 0; j < k; j++ )
369 dstbuf[i + j] = h_i[j];
370 }
371
Paul Bakker34617722014-06-13 17:20:13 +0200372 polarssl_zeroize( tmp, sizeof( tmp ) );
373 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000374
375 return( 0 );
376}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200377#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000378
Paul Bakker9e36f042013-06-30 14:34:05 +0200379#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200380static int tls_prf_sha384( const unsigned char *secret, size_t slen,
381 const char *label,
382 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000383 unsigned char *dstbuf, size_t dlen )
384{
385 size_t nb;
386 size_t i, j, k;
387 unsigned char tmp[128];
388 unsigned char h_i[48];
389
390 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
391 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
392
393 nb = strlen( label );
394 memcpy( tmp + 48, label, nb );
395 memcpy( tmp + 48 + nb, random, rlen );
396 nb += rlen;
397
398 /*
399 * Compute P_<hash>(secret, label + random)[0..dlen]
400 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200401 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000402
403 for( i = 0; i < dlen; i += 48 )
404 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200405 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
406 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000407
408 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
409
410 for( j = 0; j < k; j++ )
411 dstbuf[i + j] = h_i[j];
412 }
413
Paul Bakker34617722014-06-13 17:20:13 +0200414 polarssl_zeroize( tmp, sizeof( tmp ) );
415 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416
417 return( 0 );
418}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419#endif /* POLARSSL_SHA512_C */
420#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000421
Paul Bakker66d5d072014-06-17 16:39:18 +0200422static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200423
424#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
425 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200426static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200427#endif
Paul Bakker380da532012-04-18 16:10:25 +0000428
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200429#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200430static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
431static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432#endif
433
434#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200435static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
436static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200437#endif
438
439#if defined(POLARSSL_SSL_PROTO_TLS1_2)
440#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200441static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
442static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
443static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200444#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100445
Paul Bakker9e36f042013-06-30 14:34:05 +0200446#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200447static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
448static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
449static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100450#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200451#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000452
Paul Bakker5121ce52009-01-03 21:22:43 +0000453int ssl_derive_keys( ssl_context *ssl )
454{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200455 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000456 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 unsigned char keyblk[256];
458 unsigned char *key1;
459 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100460 unsigned char *mac_enc;
461 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200462 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100463 const cipher_info_t *cipher_info;
464 const md_info_t *md_info;
465
Paul Bakker48916f92012-09-16 19:57:18 +0000466 ssl_session *session = ssl->session_negotiate;
467 ssl_transform *transform = ssl->transform_negotiate;
468 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000469
470 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
471
Paul Bakker68884e32013-01-07 18:20:04 +0100472 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
473 if( cipher_info == NULL )
474 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200475 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100476 transform->ciphersuite_info->cipher ) );
477 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
478 }
479
480 md_info = md_info_from_type( transform->ciphersuite_info->mac );
481 if( md_info == NULL )
482 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200483 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100484 transform->ciphersuite_info->mac ) );
485 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
486 }
487
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000489 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000490 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200491#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000492 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000493 {
Paul Bakker48916f92012-09-16 19:57:18 +0000494 handshake->tls_prf = ssl3_prf;
495 handshake->calc_verify = ssl_calc_verify_ssl;
496 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000497 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200498 else
499#endif
500#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
501 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000502 {
Paul Bakker48916f92012-09-16 19:57:18 +0000503 handshake->tls_prf = tls1_prf;
504 handshake->calc_verify = ssl_calc_verify_tls;
505 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000506 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200507 else
508#endif
509#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200510#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200511 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
512 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000513 {
Paul Bakker48916f92012-09-16 19:57:18 +0000514 handshake->tls_prf = tls_prf_sha384;
515 handshake->calc_verify = ssl_calc_verify_tls_sha384;
516 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000517 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000518 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200519#endif
520#if defined(POLARSSL_SHA256_C)
521 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000522 {
Paul Bakker48916f92012-09-16 19:57:18 +0000523 handshake->tls_prf = tls_prf_sha256;
524 handshake->calc_verify = ssl_calc_verify_tls_sha256;
525 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000526 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200527 else
528#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200529#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200530 {
531 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200532 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200533 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000534
535 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * SSLv3:
537 * master =
538 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
539 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
540 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200541 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200542 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 * master = PRF( premaster, "master secret", randbytes )[0..47]
544 */
Paul Bakker0a597072012-09-25 21:55:46 +0000545 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 {
Paul Bakker48916f92012-09-16 19:57:18 +0000547 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
548 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
Paul Bakker48916f92012-09-16 19:57:18 +0000550 handshake->tls_prf( handshake->premaster, handshake->pmslen,
551 "master secret",
552 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Paul Bakker34617722014-06-13 17:20:13 +0200554 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 }
556 else
557 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
558
559 /*
560 * Swap the client and server random values.
561 */
Paul Bakker48916f92012-09-16 19:57:18 +0000562 memcpy( tmp, handshake->randbytes, 64 );
563 memcpy( handshake->randbytes, tmp + 32, 32 );
564 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200565 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567 /*
568 * SSLv3:
569 * key block =
570 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
571 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
572 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
573 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
574 * ...
575 *
576 * TLSv1:
577 * key block = PRF( master, "key expansion", randbytes )
578 */
Paul Bakker48916f92012-09-16 19:57:18 +0000579 handshake->tls_prf( session->master, 48, "key expansion",
580 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
Paul Bakker48916f92012-09-16 19:57:18 +0000582 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
583 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
584 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
585 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
587
Paul Bakker34617722014-06-13 17:20:13 +0200588 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 /*
591 * Determine the appropriate key, IV and MAC length.
592 */
Paul Bakker68884e32013-01-07 18:20:04 +0100593
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200594 transform->keylen = cipher_info->key_length / 8;
595
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200596 if( cipher_info->mode == POLARSSL_MODE_GCM ||
597 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200599 transform->maclen = 0;
600
Paul Bakker68884e32013-01-07 18:20:04 +0100601 transform->ivlen = 12;
602 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200603
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200604 /* Minimum length is expicit IV + tag */
605 transform->minlen = transform->ivlen - transform->fixed_ivlen
606 + ( transform->ciphersuite_info->flags &
607 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100608 }
609 else
610 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200611 int ret;
612
613 /* Initialize HMAC contexts */
614 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
615 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100616 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200617 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
618 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100619 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200621 /* Get MAC length */
622 transform->maclen = md_get_size( md_info );
623
624#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
625 /*
626 * If HMAC is to be truncated, we shall keep the leftmost bytes,
627 * (rfc 6066 page 13 or rfc 2104 section 4),
628 * so we only need to adjust the length here.
629 */
630 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
631 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
632#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
633
634 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100635 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200637 /* Minimum length */
638 if( cipher_info->mode == POLARSSL_MODE_STREAM )
639 transform->minlen = transform->maclen;
640 else
Paul Bakker68884e32013-01-07 18:20:04 +0100641 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200642 /*
643 * GenericBlockCipher:
644 * first multiple of blocklen greater than maclen
645 * + IV except for SSL3 and TLS 1.0
646 */
647 transform->minlen = transform->maclen
648 + cipher_info->block_size
649 - transform->maclen % cipher_info->block_size;
650
651#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
652 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
653 ssl->minor_ver == SSL_MINOR_VERSION_1 )
654 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100655 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200656#endif
657#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
658 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
659 ssl->minor_ver == SSL_MINOR_VERSION_3 )
660 {
661 transform->minlen += transform->ivlen;
662 }
663 else
664#endif
665 {
666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
668 }
Paul Bakker68884e32013-01-07 18:20:04 +0100669 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 }
671
672 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000673 transform->keylen, transform->minlen, transform->ivlen,
674 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
676 /*
677 * Finally setup the cipher contexts, IVs and MAC secrets.
678 */
679 if( ssl->endpoint == SSL_IS_CLIENT )
680 {
Paul Bakker48916f92012-09-16 19:57:18 +0000681 key1 = keyblk + transform->maclen * 2;
682 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000683
Paul Bakker68884e32013-01-07 18:20:04 +0100684 mac_enc = keyblk;
685 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000687 /*
688 * This is not used in TLS v1.1.
689 */
Paul Bakker48916f92012-09-16 19:57:18 +0000690 iv_copy_len = ( transform->fixed_ivlen ) ?
691 transform->fixed_ivlen : transform->ivlen;
692 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
693 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000694 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 }
696 else
697 {
Paul Bakker48916f92012-09-16 19:57:18 +0000698 key1 = keyblk + transform->maclen * 2 + transform->keylen;
699 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
Paul Bakker68884e32013-01-07 18:20:04 +0100701 mac_enc = keyblk + transform->maclen;
702 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000704 /*
705 * This is not used in TLS v1.1.
706 */
Paul Bakker48916f92012-09-16 19:57:18 +0000707 iv_copy_len = ( transform->fixed_ivlen ) ?
708 transform->fixed_ivlen : transform->ivlen;
709 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
710 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000711 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 }
713
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100715 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
716 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100717 if( transform->maclen > sizeof transform->mac_enc )
718 {
719 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200720 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100721 }
722
Paul Bakker68884e32013-01-07 18:20:04 +0100723 memcpy( transform->mac_enc, mac_enc, transform->maclen );
724 memcpy( transform->mac_dec, mac_dec, transform->maclen );
725 }
726 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200727#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200728#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
729 defined(POLARSSL_SSL_PROTO_TLS1_2)
730 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100731 {
732 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
733 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
734 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200735 else
736#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200737 {
738 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200739 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200740 }
Paul Bakker68884e32013-01-07 18:20:04 +0100741
Paul Bakker05ef8352012-05-08 09:17:57 +0000742#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200743 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000744 {
745 int ret = 0;
746
747 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
748
Paul Bakker07eb38b2012-12-19 14:42:06 +0100749 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
750 transform->iv_enc, transform->iv_dec,
751 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100752 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100753 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000754 {
755 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200756 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000757 }
758 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200759#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000760
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200761 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
762 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200764 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
765 return( ret );
766 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200767
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
769 cipher_info ) ) != 0 )
770 {
771 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
772 return( ret );
773 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200774
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200775 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
776 cipher_info->key_length,
777 POLARSSL_ENCRYPT ) ) != 0 )
778 {
779 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
780 return( ret );
781 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200782
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200783 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
784 cipher_info->key_length,
785 POLARSSL_DECRYPT ) ) != 0 )
786 {
787 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
788 return( ret );
789 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200790
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200791#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200792 if( cipher_info->mode == POLARSSL_MODE_CBC )
793 {
794 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
795 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200796 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200797 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
798 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200799 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200800
801 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
802 POLARSSL_PADDING_NONE ) ) != 0 )
803 {
804 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
805 return( ret );
806 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000807 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200808#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Paul Bakker34617722014-06-13 17:20:13 +0200810 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000811
Paul Bakker2770fbd2012-07-03 13:30:23 +0000812#if defined(POLARSSL_ZLIB_SUPPORT)
813 // Initialize compression
814 //
Paul Bakker48916f92012-09-16 19:57:18 +0000815 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000816 {
Paul Bakker16770332013-10-11 09:59:44 +0200817 if( ssl->compress_buf == NULL )
818 {
819 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
820 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
821 if( ssl->compress_buf == NULL )
822 {
823 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
824 SSL_BUFFER_LEN ) );
825 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
826 }
827 }
828
Paul Bakker2770fbd2012-07-03 13:30:23 +0000829 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
830
Paul Bakker48916f92012-09-16 19:57:18 +0000831 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
832 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000833
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200834 if( deflateInit( &transform->ctx_deflate,
835 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000836 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000837 {
838 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
839 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
840 }
841 }
842#endif /* POLARSSL_ZLIB_SUPPORT */
843
Paul Bakker5121ce52009-01-03 21:22:43 +0000844 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
845
846 return( 0 );
847}
848
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200849#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000850void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000851{
852 md5_context md5;
853 sha1_context sha1;
854 unsigned char pad_1[48];
855 unsigned char pad_2[48];
856
Paul Bakker380da532012-04-18 16:10:25 +0000857 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Paul Bakker48916f92012-09-16 19:57:18 +0000859 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
860 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
Paul Bakker380da532012-04-18 16:10:25 +0000862 memset( pad_1, 0x36, 48 );
863 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864
Paul Bakker48916f92012-09-16 19:57:18 +0000865 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000866 md5_update( &md5, pad_1, 48 );
867 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
Paul Bakker380da532012-04-18 16:10:25 +0000869 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000870 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000871 md5_update( &md5, pad_2, 48 );
872 md5_update( &md5, hash, 16 );
873 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
Paul Bakker48916f92012-09-16 19:57:18 +0000875 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000876 sha1_update( &sha1, pad_1, 40 );
877 sha1_finish( &sha1, hash + 16 );
878
879 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000880 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000881 sha1_update( &sha1, pad_2, 40 );
882 sha1_update( &sha1, hash + 16, 20 );
883 sha1_finish( &sha1, hash + 16 );
884
885 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
886 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
887
Paul Bakker5b4af392014-06-26 12:09:34 +0200888 md5_free( &md5 );
889 sha1_free( &sha1 );
890
Paul Bakker380da532012-04-18 16:10:25 +0000891 return;
892}
Paul Bakker9af723c2014-05-01 13:03:14 +0200893#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000894
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200895#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000896void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
897{
898 md5_context md5;
899 sha1_context sha1;
900
901 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
902
Paul Bakker48916f92012-09-16 19:57:18 +0000903 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
904 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000905
Paul Bakker48916f92012-09-16 19:57:18 +0000906 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000907 sha1_finish( &sha1, hash + 16 );
908
909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 md5_free( &md5 );
913 sha1_free( &sha1 );
914
Paul Bakker380da532012-04-18 16:10:25 +0000915 return;
916}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200917#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000918
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200919#if defined(POLARSSL_SSL_PROTO_TLS1_2)
920#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000921void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
922{
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000924
925 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
926
Paul Bakker9e36f042013-06-30 14:34:05 +0200927 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
928 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000929
930 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
931 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
932
Paul Bakker5b4af392014-06-26 12:09:34 +0200933 sha256_free( &sha256 );
934
Paul Bakker380da532012-04-18 16:10:25 +0000935 return;
936}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200937#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000938
Paul Bakker9e36f042013-06-30 14:34:05 +0200939#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000940void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
941{
Paul Bakker9e36f042013-06-30 14:34:05 +0200942 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000943
944 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
945
Paul Bakker9e36f042013-06-30 14:34:05 +0200946 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
947 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000948
Paul Bakkerca4ab492012-04-18 14:23:57 +0000949 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000950 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
951
Paul Bakker5b4af392014-06-26 12:09:34 +0200952 sha512_free( &sha512 );
953
Paul Bakker5121ce52009-01-03 21:22:43 +0000954 return;
955}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200956#endif /* POLARSSL_SHA512_C */
957#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200959#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
961{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 unsigned char *p = ssl->handshake->premaster;
963 unsigned char *end = p + sizeof( ssl->handshake->premaster );
964
965 /*
966 * PMS = struct {
967 * opaque other_secret<0..2^16-1>;
968 * opaque psk<0..2^16-1>;
969 * };
970 * with "other_secret" depending on the particular key exchange
971 */
972#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
973 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
974 {
975 if( end - p < 2 + (int) ssl->psk_len )
976 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
977
978 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
979 *(p++) = (unsigned char)( ssl->psk_len );
980 p += ssl->psk_len;
981 }
982 else
983#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200984#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
985 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
986 {
987 /*
988 * other_secret already set by the ClientKeyExchange message,
989 * and is 48 bytes long
990 */
991 *p++ = 0;
992 *p++ = 48;
993 p += 48;
994 }
995 else
996#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200997#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
998 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
999 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001000 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02001001 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001002
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001003 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001005 p + 2, &len,
1006 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001007 {
1008 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
1009 return( ret );
1010 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001011 *(p++) = (unsigned char)( len >> 8 );
1012 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001013 p += len;
1014
1015 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
1016 }
1017 else
1018#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1019#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1020 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1021 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001022 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001023 size_t zlen;
1024
1025 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001026 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001027 ssl->f_rng, ssl->p_rng ) ) != 0 )
1028 {
1029 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1030 return( ret );
1031 }
1032
1033 *(p++) = (unsigned char)( zlen >> 8 );
1034 *(p++) = (unsigned char)( zlen );
1035 p += zlen;
1036
1037 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1038 }
1039 else
1040#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1041 {
1042 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001043 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001044 }
1045
1046 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001047 if( end - p < 2 + (int) ssl->psk_len )
1048 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1049
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001050 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1051 *(p++) = (unsigned char)( ssl->psk_len );
1052 memcpy( p, ssl->psk, ssl->psk_len );
1053 p += ssl->psk_len;
1054
1055 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1056
1057 return( 0 );
1058}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001059#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001060
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001062/*
1063 * SSLv3.0 MAC functions
1064 */
Paul Bakker68884e32013-01-07 18:20:04 +01001065static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1066 unsigned char *buf, size_t len,
1067 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001068{
1069 unsigned char header[11];
1070 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001071 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001072 int md_size = md_get_size( md_ctx->md_info );
1073 int md_type = md_get_type( md_ctx->md_info );
1074
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001075 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001076 if( md_type == POLARSSL_MD_MD5 )
1077 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001078 else
Paul Bakker68884e32013-01-07 18:20:04 +01001079 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001080
1081 memcpy( header, ctr, 8 );
1082 header[ 8] = (unsigned char) type;
1083 header[ 9] = (unsigned char)( len >> 8 );
1084 header[10] = (unsigned char)( len );
1085
Paul Bakker68884e32013-01-07 18:20:04 +01001086 memset( padding, 0x36, padlen );
1087 md_starts( md_ctx );
1088 md_update( md_ctx, secret, md_size );
1089 md_update( md_ctx, padding, padlen );
1090 md_update( md_ctx, header, 11 );
1091 md_update( md_ctx, buf, len );
1092 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001093
Paul Bakker68884e32013-01-07 18:20:04 +01001094 memset( padding, 0x5C, padlen );
1095 md_starts( md_ctx );
1096 md_update( md_ctx, secret, md_size );
1097 md_update( md_ctx, padding, padlen );
1098 md_update( md_ctx, buf + len, md_size );
1099 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001100}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001101#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001102
Paul Bakker5121ce52009-01-03 21:22:43 +00001103/*
1104 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001105 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001106static int ssl_encrypt_buf( ssl_context *ssl )
1107{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001108 const cipher_mode_t mode = cipher_get_cipher_mode(
1109 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1112
1113 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001114 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001115 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001116#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1117 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1118 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001119 if( mode != POLARSSL_MODE_GCM &&
1120 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001121 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001122#if defined(POLARSSL_SSL_PROTO_SSL3)
1123 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1124 {
1125 ssl_mac( &ssl->transform_out->md_ctx_enc,
1126 ssl->transform_out->mac_enc,
1127 ssl->out_msg, ssl->out_msglen,
1128 ssl->out_ctr, ssl->out_msgtype );
1129 }
1130 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001131#endif
1132#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001133 defined(POLARSSL_SSL_PROTO_TLS1_2)
1134 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1135 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001136 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1137 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1138 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001139 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1140 ssl->out_msg, ssl->out_msglen );
1141 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1142 ssl->out_msg + ssl->out_msglen );
1143 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1144 }
1145 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001146#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 {
1148 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001149 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001150 }
1151
1152 SSL_DEBUG_BUF( 4, "computed mac",
1153 ssl->out_msg + ssl->out_msglen,
1154 ssl->transform_out->maclen );
1155
1156 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001157 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001158#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001160 /*
1161 * Encrypt
1162 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001163#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001164 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001166 int ret;
1167 size_t olen = 0;
1168
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1170 "including %d bytes of padding",
1171 ssl->out_msglen, 0 ) );
1172
1173 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1174 ssl->out_msg, ssl->out_msglen );
1175
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001176 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001177 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 ssl->transform_out->ivlen,
1179 ssl->out_msg, ssl->out_msglen,
1180 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001181 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001182 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001183 return( ret );
1184 }
1185
1186 if( ssl->out_msglen != olen )
1187 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001188 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001189 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001190 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 }
Paul Bakker68884e32013-01-07 18:20:04 +01001192 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001193#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001194#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1195 if( mode == POLARSSL_MODE_GCM ||
1196 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001198 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001199 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200 unsigned char *enc_msg;
1201 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1203 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204
Paul Bakkerca4ab492012-04-18 14:23:57 +00001205 memcpy( add_data, ssl->out_ctr, 8 );
1206 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001207 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1208 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001209 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1210 add_data[12] = ssl->out_msglen & 0xFF;
1211
1212 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1213 add_data, 13 );
1214
Paul Bakker68884e32013-01-07 18:20:04 +01001215 /*
1216 * Generate IV
1217 */
1218 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001219 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1220 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001221 if( ret != 0 )
1222 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001223
Paul Bakker68884e32013-01-07 18:20:04 +01001224 memcpy( ssl->out_iv,
1225 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1226 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001227
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001228 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001229 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001230
Paul Bakker68884e32013-01-07 18:20:04 +01001231 /*
1232 * Fix pointer positions and message length with added IV
1233 */
1234 enc_msg = ssl->out_msg;
1235 enc_msglen = ssl->out_msglen;
1236 ssl->out_msglen += ssl->transform_out->ivlen -
1237 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001238
Paul Bakker68884e32013-01-07 18:20:04 +01001239 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1240 "including %d bytes of padding",
1241 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Paul Bakker68884e32013-01-07 18:20:04 +01001243 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1244 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001245
Paul Bakker68884e32013-01-07 18:20:04 +01001246 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001247 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001248 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001249 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1250 ssl->transform_out->iv_enc,
1251 ssl->transform_out->ivlen,
1252 add_data, 13,
1253 enc_msg, enc_msglen,
1254 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001255 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001256 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001257 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 return( ret );
1259 }
1260
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001261 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001263 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001264 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 }
1266
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001267 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001268
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001272#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001273#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1274 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001275 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001277 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001278 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001279 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001280
Paul Bakker48916f92012-09-16 19:57:18 +00001281 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1282 ssl->transform_out->ivlen;
1283 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 padlen = 0;
1285
1286 for( i = 0; i <= padlen; i++ )
1287 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1288
1289 ssl->out_msglen += padlen + 1;
1290
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001291 enc_msglen = ssl->out_msglen;
1292 enc_msg = ssl->out_msg;
1293
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001294#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001296 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1297 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001298 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001299 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001300 {
1301 /*
1302 * Generate IV
1303 */
Paul Bakker48916f92012-09-16 19:57:18 +00001304 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1305 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001306 if( ret != 0 )
1307 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308
Paul Bakker92be97b2013-01-02 17:30:03 +01001309 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001310 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001311
1312 /*
1313 * Fix pointer positions and message length with added IV
1314 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001315 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001317 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001319#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001320
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001322 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001323 ssl->out_msglen, ssl->transform_out->ivlen,
1324 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
1326 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001327 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001329 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001330 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001331 ssl->transform_out->ivlen,
1332 enc_msg, enc_msglen,
1333 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001334 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001336 return( ret );
1337 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001338
Paul Bakkercca5b812013-08-31 17:40:26 +02001339 if( enc_msglen != olen )
1340 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001341 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001342 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001344
1345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1347 {
1348 /*
1349 * Save IV in SSL3 and TLS1
1350 */
1351 memcpy( ssl->transform_out->iv_enc,
1352 ssl->transform_out->cipher_ctx_enc.iv,
1353 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001355#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001357 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001358#endif /* POLARSSL_CIPHER_MODE_CBC &&
1359 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001360 {
1361 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001362 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1366
1367 return( 0 );
1368}
1369
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001370#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001371
Paul Bakker5121ce52009-01-03 21:22:43 +00001372static int ssl_decrypt_buf( ssl_context *ssl )
1373{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001374 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001375 const cipher_mode_t mode = cipher_get_cipher_mode(
1376 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001377#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1378 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1379 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1380 size_t padlen = 0, correct = 1;
1381#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
1383 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1384
Paul Bakker48916f92012-09-16 19:57:18 +00001385 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 {
1387 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001388 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001389 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 }
1391
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001392#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001393 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001394 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001395 int ret;
1396 size_t olen = 0;
1397
Paul Bakker68884e32013-01-07 18:20:04 +01001398 padlen = 0;
1399
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001400 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001401 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001402 ssl->transform_in->ivlen,
1403 ssl->in_msg, ssl->in_msglen,
1404 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001405 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001406 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001407 return( ret );
1408 }
1409
1410 if( ssl->in_msglen != olen )
1411 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001412 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001413 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001415 }
Paul Bakker68884e32013-01-07 18:20:04 +01001416 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001417#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001418#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1419 if( mode == POLARSSL_MODE_GCM ||
1420 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001421 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001422 int ret;
1423 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001424 unsigned char *dec_msg;
1425 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001426 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001427 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1428 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001429 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1430 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001431
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001432 if( ssl->in_msglen < explicit_iv_len + taglen )
1433 {
1434 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1435 "+ taglen (%d)", ssl->in_msglen,
1436 explicit_iv_len, taglen ) );
1437 return( POLARSSL_ERR_SSL_INVALID_MAC );
1438 }
1439 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1440
Paul Bakker68884e32013-01-07 18:20:04 +01001441 dec_msg = ssl->in_msg;
1442 dec_msg_result = ssl->in_msg;
1443 ssl->in_msglen = dec_msglen;
1444
1445 memcpy( add_data, ssl->in_ctr, 8 );
1446 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001447 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1448 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001449 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1450 add_data[12] = ssl->in_msglen & 0xFF;
1451
1452 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1453 add_data, 13 );
1454
1455 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1456 ssl->in_iv,
1457 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1458
1459 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1460 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001461 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001462
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001463 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001464 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001465 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001466 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1467 ssl->transform_in->iv_dec,
1468 ssl->transform_in->ivlen,
1469 add_data, 13,
1470 dec_msg, dec_msglen,
1471 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001472 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001473 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001474 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1475
1476 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1477 return( POLARSSL_ERR_SSL_INVALID_MAC );
1478
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001479 return( ret );
1480 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001481
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001482 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001483 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001484 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001485 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001486 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001489#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001490#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1491 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001492 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001493 {
Paul Bakker45829992013-01-03 14:52:21 +01001494 /*
1495 * Decrypt and check the padding
1496 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001497 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001498 unsigned char *dec_msg;
1499 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001500 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001501 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001502 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001503
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 /*
Paul Bakker45829992013-01-03 14:52:21 +01001505 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 */
Paul Bakker48916f92012-09-16 19:57:18 +00001507 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 {
1509 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001510 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001511 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001512 }
1513
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001514#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001515 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1516 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001517#endif
Paul Bakker45829992013-01-03 14:52:21 +01001518
1519 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1520 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1521 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001522 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1523 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1524 ssl->transform_in->ivlen,
1525 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001526 return( POLARSSL_ERR_SSL_INVALID_MAC );
1527 }
1528
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001529 dec_msglen = ssl->in_msglen;
1530 dec_msg = ssl->in_msg;
1531 dec_msg_result = ssl->in_msg;
1532
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001533#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001534 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001535 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001536 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001537 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538 {
Paul Bakker48916f92012-09-16 19:57:18 +00001539 dec_msglen -= ssl->transform_in->ivlen;
1540 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541
Paul Bakker48916f92012-09-16 19:57:18 +00001542 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001543 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001544 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001545#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001546
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001547 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001548 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001549 ssl->transform_in->ivlen,
1550 dec_msg, dec_msglen,
1551 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001552 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001553 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001554 return( ret );
1555 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001556
Paul Bakkercca5b812013-08-31 17:40:26 +02001557 if( dec_msglen != olen )
1558 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001559 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001560 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001561 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562
1563#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001564 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1565 {
1566 /*
1567 * Save IV in SSL3 and TLS1
1568 */
1569 memcpy( ssl->transform_in->iv_dec,
1570 ssl->transform_in->cipher_ctx_dec.iv,
1571 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001573#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001574
1575 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001576
1577 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1578 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001579#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001580 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1581 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001582#endif
Paul Bakker45829992013-01-03 14:52:21 +01001583 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001584 correct = 0;
1585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001587#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1589 {
Paul Bakker48916f92012-09-16 19:57:18 +00001590 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001591 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001592#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1594 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001595 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001596#endif
Paul Bakker45829992013-01-03 14:52:21 +01001597 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 }
1599 }
1600 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001601#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001602#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1603 defined(POLARSSL_SSL_PROTO_TLS1_2)
1604 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 {
1606 /*
Paul Bakker45829992013-01-03 14:52:21 +01001607 * TLSv1+: always check the padding up to the first failure
1608 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001610 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001611 size_t padding_idx = ssl->in_msglen - padlen - 1;
1612
Paul Bakker956c9e02013-12-19 14:42:28 +01001613 /*
1614 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001615 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001616 *
Paul Bakker61885c72014-04-25 12:59:03 +02001617 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1618 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001619 *
1620 * In both cases we reset padding_idx to a safe value (0) to
1621 * prevent out-of-buffer reads.
1622 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001623 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001624 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1625 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001626
1627 padding_idx *= correct;
1628
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001629 for( i = 1; i <= 256; i++ )
1630 {
1631 real_count &= ( i <= padlen );
1632 pad_count += real_count *
1633 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1634 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001635
1636 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001637
Paul Bakkerd66f0702013-01-31 16:57:45 +01001638#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001639 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001640 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001641#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001642 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001643 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001644 else
1645#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1646 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001647 {
1648 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001649 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001650 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001652 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001653#endif /* POLARSSL_CIPHER_MODE_CBC &&
1654 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001655 {
1656 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001657 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001658 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
1660 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1661 ssl->in_msg, ssl->in_msglen );
1662
1663 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001664 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1667 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1668 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001669 if( mode != POLARSSL_MODE_GCM &&
1670 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001672 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1673
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001674 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001676 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1677 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001681#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1683 {
1684 ssl_mac( &ssl->transform_in->md_ctx_dec,
1685 ssl->transform_in->mac_dec,
1686 ssl->in_msg, ssl->in_msglen,
1687 ssl->in_ctr, ssl->in_msgtype );
1688 }
1689 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001690#endif /* POLARSSL_SSL_PROTO_SSL3 */
1691#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 defined(POLARSSL_SSL_PROTO_TLS1_2)
1693 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1694 {
1695 /*
1696 * Process MAC and always update for padlen afterwards to make
1697 * total time independent of padlen
1698 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001699 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001700 *
1701 * Known timing attacks:
1702 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1703 *
1704 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1705 * correctly. (We round down instead of up, so -56 is the correct
1706 * value for our calculations instead of -55)
1707 */
1708 size_t j, extra_run = 0;
1709 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1710 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001711
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001713
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001714 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1715 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1716 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001717 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1718 ssl->in_msglen );
1719 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1720 ssl->in_msg + ssl->in_msglen );
1721 for( j = 0; j < extra_run; j++ )
1722 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1725 }
1726 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001727#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001728 POLARSSL_SSL_PROTO_TLS1_2 */
1729 {
1730 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001731 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001732 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001734 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1735 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1736 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001737
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001738 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001739 ssl->transform_in->maclen ) != 0 )
1740 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001741#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001742 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001743#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 correct = 0;
1745 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001746
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001747 /*
1748 * Finally check the correct flag
1749 */
1750 if( correct == 0 )
1751 return( POLARSSL_ERR_SSL_INVALID_MAC );
1752 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001753#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
1755 if( ssl->in_msglen == 0 )
1756 {
1757 ssl->nb_zero++;
1758
1759 /*
1760 * Three or more empty messages may be a DoS attack
1761 * (excessive CPU consumption).
1762 */
1763 if( ssl->nb_zero > 3 )
1764 {
1765 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1766 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001767 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 }
1769 }
1770 else
1771 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001772
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001773#if defined(POLARSSL_SSL_PROTO_DTLS)
1774 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001775 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001776 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001777 }
1778 else
1779#endif
1780 {
1781 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1782 if( ++ssl->in_ctr[i - 1] != 0 )
1783 break;
1784
1785 /* The loop goes to its end iff the counter is wrapping */
1786 if( i == ssl_ep_len( ssl ) )
1787 {
1788 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1789 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1790 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001791 }
1792
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1794
1795 return( 0 );
1796}
1797
Paul Bakker2770fbd2012-07-03 13:30:23 +00001798#if defined(POLARSSL_ZLIB_SUPPORT)
1799/*
1800 * Compression/decompression functions
1801 */
1802static int ssl_compress_buf( ssl_context *ssl )
1803{
1804 int ret;
1805 unsigned char *msg_post = ssl->out_msg;
1806 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001807 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001808
1809 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1810
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001811 if( len_pre == 0 )
1812 return( 0 );
1813
Paul Bakker2770fbd2012-07-03 13:30:23 +00001814 memcpy( msg_pre, ssl->out_msg, len_pre );
1815
1816 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1817 ssl->out_msglen ) );
1818
1819 SSL_DEBUG_BUF( 4, "before compression: output payload",
1820 ssl->out_msg, ssl->out_msglen );
1821
Paul Bakker48916f92012-09-16 19:57:18 +00001822 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1823 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1824 ssl->transform_out->ctx_deflate.next_out = msg_post;
1825 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001828 if( ret != Z_OK )
1829 {
1830 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1831 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1832 }
1833
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001834 ssl->out_msglen = SSL_BUFFER_LEN -
1835 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001836
Paul Bakker2770fbd2012-07-03 13:30:23 +00001837 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1838 ssl->out_msglen ) );
1839
1840 SSL_DEBUG_BUF( 4, "after compression: output payload",
1841 ssl->out_msg, ssl->out_msglen );
1842
1843 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1844
1845 return( 0 );
1846}
1847
1848static int ssl_decompress_buf( ssl_context *ssl )
1849{
1850 int ret;
1851 unsigned char *msg_post = ssl->in_msg;
1852 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001853 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001854
1855 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1856
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001857 if( len_pre == 0 )
1858 return( 0 );
1859
Paul Bakker2770fbd2012-07-03 13:30:23 +00001860 memcpy( msg_pre, ssl->in_msg, len_pre );
1861
1862 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1863 ssl->in_msglen ) );
1864
1865 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1866 ssl->in_msg, ssl->in_msglen );
1867
Paul Bakker48916f92012-09-16 19:57:18 +00001868 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1869 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1870 ssl->transform_in->ctx_inflate.next_out = msg_post;
1871 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001872
Paul Bakker48916f92012-09-16 19:57:18 +00001873 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001874 if( ret != Z_OK )
1875 {
1876 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1877 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1878 }
1879
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001880 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1881 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001882
Paul Bakker2770fbd2012-07-03 13:30:23 +00001883 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1884 ssl->in_msglen ) );
1885
1886 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1887 ssl->in_msg, ssl->in_msglen );
1888
1889 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1890
1891 return( 0 );
1892}
1893#endif /* POLARSSL_ZLIB_SUPPORT */
1894
Paul Bakker5121ce52009-01-03 21:22:43 +00001895/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001896 * Fill the input message buffer by appending data to it.
1897 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001898 *
1899 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1900 * available (from this read and/or a previous one). Otherwise, an error code
1901 * is returned (possibly EOF or WANT_READ).
1902 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001903 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1904 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1905 * since we always read a whole datagram at once.
1906 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001907 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001908 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 */
Paul Bakker23986e52011-04-24 08:57:21 +00001910int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001911{
Paul Bakker23986e52011-04-24 08:57:21 +00001912 int ret;
1913 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
1915 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1916
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001917 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1918 {
1919 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1920 "or ssl_set_bio_timeout()" ) );
1921 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1922 }
1923
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001924 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001925 {
1926 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1927 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1928 }
1929
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001930#if defined(POLARSSL_SSL_PROTO_DTLS)
1931 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001932 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001933 /*
1934 * The point is, we need to always read a full datagram at once, so we
1935 * sometimes read more then requested, and handle the additional data.
1936 * It could be the rest of the current record (while fetching the
1937 * header) and/or some other records in the same datagram.
1938 */
1939
1940 /*
1941 * Move to the next record in the already read datagram if applicable
1942 */
1943 if( ssl->next_record_offset != 0 )
1944 {
1945 if( ssl->in_left < ssl->next_record_offset )
1946 {
1947 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1948 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1949 }
1950
1951 ssl->in_left -= ssl->next_record_offset;
1952
1953 if( ssl->in_left != 0 )
1954 {
1955 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1956 ssl->next_record_offset ) );
1957 memmove( ssl->in_hdr,
1958 ssl->in_hdr + ssl->next_record_offset,
1959 ssl->in_left );
1960 }
1961
1962 ssl->next_record_offset = 0;
1963 }
1964
Paul Bakker5121ce52009-01-03 21:22:43 +00001965 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1966 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001967
1968 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001969 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001970 */
1971 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001972 {
1973 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001974 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001975 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001976
1977 /*
1978 * A record can't be split accross datagrams. If we need to read but
1979 * are not at the beginning of a new record, the caller did something
1980 * wrong.
1981 */
1982 if( ssl->in_left != 0 )
1983 {
1984 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1985 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1986 }
1987
1988 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001989 if( ssl->f_recv_timeout != NULL &&
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001990 ssl->handshake != NULL ) /* No timeout outside handshake */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001991 {
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02001992 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02001993 ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001994 }
1995 else
1996 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001997
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02001998 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001999
Paul Bakker831a7552011-05-18 13:32:51 +00002000 if( ret == 0 )
2001 return( POLARSSL_ERR_SSL_CONN_EOF );
2002
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002003 if( ret == POLARSSL_ERR_NET_TIMEOUT ||
2004 ( ret == POLARSSL_ERR_NET_WANT_READ &&
2005 ssl_check_timer( ssl ) != 0 ) )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002006 {
2007 SSL_DEBUG_MSG( 2, ( "recv timeout" ) );
2008
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002009 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2010 {
2011 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2012 return( POLARSSL_ERR_NET_TIMEOUT );
2013 }
2014
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002015 if( ( ret = ssl_resend( ssl ) ) != 0 )
2016 {
2017 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2018 return( ret );
2019 }
2020
2021 return( POLARSSL_ERR_NET_WANT_READ );
2022 }
2023
Paul Bakker5121ce52009-01-03 21:22:43 +00002024 if( ret < 0 )
2025 return( ret );
2026
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002027 ssl->in_left = ret;
2028 }
2029 else
2030#endif
2031 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002032 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2033 ssl->in_left, nb_want ) );
2034
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002035 while( ssl->in_left < nb_want )
2036 {
2037 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002038 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002039
2040 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2041 ssl->in_left, nb_want ) );
2042 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2043
2044 if( ret == 0 )
2045 return( POLARSSL_ERR_SSL_CONN_EOF );
2046
2047 if( ret < 0 )
2048 return( ret );
2049
2050 ssl->in_left += ret;
2051 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 }
2053
2054 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2055
2056 return( 0 );
2057}
2058
2059/*
2060 * Flush any data not yet written
2061 */
2062int ssl_flush_output( ssl_context *ssl )
2063{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002064 int ret;
2065 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
2067 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2068
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002069 if( ssl->f_send == NULL )
2070 {
2071 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2072 "or ssl_set_bio_timeout()" ) );
2073 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2074 }
2075
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002076 /* Avoid incrementing counter if data is flushed */
2077 if( ssl->out_left == 0 )
2078 {
2079 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2080 return( 0 );
2081 }
2082
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 while( ssl->out_left > 0 )
2084 {
2085 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002086 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002088 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2089 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002090 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002091
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2093
2094 if( ret <= 0 )
2095 return( ret );
2096
2097 ssl->out_left -= ret;
2098 }
2099
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002100 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002101 if( ++ssl->out_ctr[i - 1] != 0 )
2102 break;
2103
2104 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002105 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002106 {
2107 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2108 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2109 }
2110
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2112
2113 return( 0 );
2114}
2115
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002116/*
2117 * Functions to handle the DTLS retransmission state machine
2118 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002119#if defined(POLARSSL_SSL_PROTO_DTLS)
2120/*
2121 * Append current handshake message to current outgoing flight
2122 */
2123static int ssl_flight_append( ssl_context *ssl )
2124{
2125 ssl_flight_item *msg;
2126
2127 /* Allocate space for current message */
2128 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2129 {
2130 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2131 sizeof( ssl_flight_item ) ) );
2132 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2133 }
2134
2135 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2136 {
2137 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2138 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2139 }
2140
2141 /* Copy current handshake message with headers */
2142 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2143 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002144 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002145 msg->next = NULL;
2146
2147 /* Append to the current flight */
2148 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002149 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002150 else
2151 {
2152 ssl_flight_item *cur = ssl->handshake->flight;
2153 while( cur->next != NULL )
2154 cur = cur->next;
2155 cur->next = msg;
2156 }
2157
2158 return( 0 );
2159}
2160
2161/*
2162 * Free the current flight of handshake messages
2163 */
2164static void ssl_flight_free( ssl_flight_item *flight )
2165{
2166 ssl_flight_item *cur = flight;
2167 ssl_flight_item *next;
2168
2169 while( cur != NULL )
2170 {
2171 next = cur->next;
2172
2173 polarssl_free( cur->p );
2174 polarssl_free( cur );
2175
2176 cur = next;
2177 }
2178}
2179
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002180#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2181static void ssl_dtls_replay_reset( ssl_context *ssl );
2182#endif
2183
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002184/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002185 * Swap transform_out and out_ctr with the alternative ones
2186 */
2187static void ssl_swap_epochs( ssl_context *ssl )
2188{
2189 ssl_transform *tmp_transform;
2190 unsigned char tmp_out_ctr[8];
2191
2192 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2193 {
2194 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2195 return;
2196 }
2197
2198 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2199
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002200 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002201 tmp_transform = ssl->transform_out;
2202 ssl->transform_out = ssl->handshake->alt_transform_out;
2203 ssl->handshake->alt_transform_out = tmp_transform;
2204
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002205 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002206 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2207 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2208 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002209
2210 /* Adjust to the newly activated transform */
2211 if( ssl->transform_out != NULL &&
2212 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2213 {
2214 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2215 ssl->transform_out->fixed_ivlen;
2216 }
2217 else
2218 ssl->out_msg = ssl->out_iv;
2219
2220#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2221 if( ssl_hw_record_activate != NULL )
2222 {
2223 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2224 {
2225 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2226 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2227 }
2228 }
2229#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002230}
2231
2232/*
2233 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002234 *
2235 * Need to remember the current message in case flush_output returns
2236 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002237 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002238 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002239int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002240{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002241 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002242
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002243 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2244 {
2245 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2246
2247 ssl->handshake->cur_msg = ssl->handshake->flight;
2248 ssl_swap_epochs( ssl );
2249
2250 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002251
2252 /* Cancel running timer */
2253 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002254 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002255
2256 while( ssl->handshake->cur_msg != NULL )
2257 {
2258 int ret;
2259 ssl_flight_item *cur = ssl->handshake->cur_msg;
2260
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002261 /* Swap epochs before sending Finished: we can't do it after
2262 * sending ChangeCipherSpec, in case write returns WANT_READ.
2263 * Must be done before copying, may change out_msg pointer */
2264 if( cur->type == SSL_MSG_HANDSHAKE &&
2265 cur->p[0] == SSL_HS_FINISHED )
2266 {
2267 ssl_swap_epochs( ssl );
2268 }
2269
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002270 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002271 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002272 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002273
2274 ssl->handshake->cur_msg = cur->next;
2275
2276 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2277
2278 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2279 {
2280 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2281 return( ret );
2282 }
2283 }
2284
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002285 if( ssl->state == SSL_HANDSHAKE_OVER )
2286 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2287 else
2288 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002289
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002290 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002291
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002292 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002293
2294 return( 0 );
2295}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002296
2297/*
2298 * To be called when the last message of an incoming flight is received.
2299 */
2300void ssl_recv_flight_completed( ssl_context *ssl )
2301{
2302 /* We won't need to resend that one any more */
2303 ssl_flight_free( ssl->handshake->flight );
2304 ssl->handshake->flight = NULL;
2305 ssl->handshake->cur_msg = NULL;
2306
2307 /* The next incoming flight will start with this msg_seq */
2308 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2309
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002310 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002311 ssl_set_timer( ssl, 0 );
2312
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002313 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2314 ssl->in_msg[0] == SSL_HS_FINISHED )
2315 {
2316 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2317 }
2318 else
2319 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2320}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002321
2322/*
2323 * To be called when the last message of an outgoing flight is send.
2324 */
2325void ssl_send_flight_completed( ssl_context *ssl )
2326{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002327 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002328 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002329
2330 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2331 ssl->in_msg[0] == SSL_HS_FINISHED )
2332 {
2333 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2334 }
2335 else
2336 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2337}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002338#endif /* POLARSSL_SSL_PROTO_DTLS */
2339
Paul Bakker5121ce52009-01-03 21:22:43 +00002340/*
2341 * Record layer functions
2342 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002343
2344/*
2345 * Write current record.
2346 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2347 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002348int ssl_write_record( ssl_context *ssl )
2349{
Paul Bakker05ef8352012-05-08 09:17:57 +00002350 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002351 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
2353 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2354
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002355#if defined(POLARSSL_SSL_PROTO_DTLS)
2356 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2357 ssl->handshake != NULL &&
2358 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2359 {
2360 ; /* Skip special handshake treatment when resending */
2361 }
2362 else
2363#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002364 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2365 {
2366 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2367 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2368 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2369
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002370 /*
2371 * DTLS has additional fields in the Handshake layer,
2372 * between the length field and the actual payload:
2373 * uint16 message_seq;
2374 * uint24 fragment_offset;
2375 * uint24 fragment_length;
2376 */
2377#if defined(POLARSSL_SSL_PROTO_DTLS)
2378 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2379 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002380 /* Make room for the additional DTLS fields */
2381 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002382 ssl->out_msglen += 8;
2383 len += 8;
2384
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002385 /* Write message_seq and update it, except for HelloRequest */
2386 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2387 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002388 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2389 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2390 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002391 }
2392 else
2393 {
2394 ssl->out_msg[4] = 0;
2395 ssl->out_msg[5] = 0;
2396 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002397
2398 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2399 memset( ssl->out_msg + 6, 0x00, 3 );
2400 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002401 }
2402#endif /* POLARSSL_SSL_PROTO_DTLS */
2403
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002404 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2405 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 }
2407
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002408 /* Save handshake and CCS messages for resending */
2409#if defined(POLARSSL_SSL_PROTO_DTLS)
2410 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2411 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002412 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002413 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2414 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2415 {
2416 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2417 {
2418 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2419 return( ret );
2420 }
2421 }
2422#endif
2423
Paul Bakker2770fbd2012-07-03 13:30:23 +00002424#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002425 if( ssl->transform_out != NULL &&
2426 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002427 {
2428 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2429 {
2430 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2431 return( ret );
2432 }
2433
2434 len = ssl->out_msglen;
2435 }
2436#endif /*POLARSSL_ZLIB_SUPPORT */
2437
Paul Bakker05ef8352012-05-08 09:17:57 +00002438#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002439 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002441 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Paul Bakker05ef8352012-05-08 09:17:57 +00002443 ret = ssl_hw_record_write( ssl );
2444 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2445 {
2446 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002447 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002448 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002449
2450 if( ret == 0 )
2451 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002452 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002453#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002454 if( !done )
2455 {
2456 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002457 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2458 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002459
2460 ssl->out_len[0] = (unsigned char)( len >> 8 );
2461 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002462
Paul Bakker48916f92012-09-16 19:57:18 +00002463 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002464 {
2465 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2466 {
2467 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2468 return( ret );
2469 }
2470
2471 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002472 ssl->out_len[0] = (unsigned char)( len >> 8 );
2473 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002474 }
2475
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002476 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002477
2478 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2479 "version = [%d:%d], msglen = %d",
2480 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002481 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002482
2483 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002484 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 }
2486
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2488 {
2489 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2490 return( ret );
2491 }
2492
2493 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2494
2495 return( 0 );
2496}
2497
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002498#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002499/*
2500 * Mark bits in bitmask (used for DTLS HS reassembly)
2501 */
2502static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2503{
2504 unsigned int start_bits, end_bits;
2505
2506 start_bits = 8 - ( offset % 8 );
2507 if( start_bits != 8 )
2508 {
2509 size_t first_byte_idx = offset / 8;
2510
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002511 /* Special case */
2512 if( len <= start_bits )
2513 {
2514 for( ; len != 0; len-- )
2515 mask[first_byte_idx] |= 1 << ( start_bits - len );
2516
2517 /* Avoid potential issues with offset or len becoming invalid */
2518 return;
2519 }
2520
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002521 offset += start_bits; /* Now offset % 8 == 0 */
2522 len -= start_bits;
2523
2524 for( ; start_bits != 0; start_bits-- )
2525 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2526 }
2527
2528 end_bits = len % 8;
2529 if( end_bits != 0 )
2530 {
2531 size_t last_byte_idx = ( offset + len ) / 8;
2532
2533 len -= end_bits; /* Now len % 8 == 0 */
2534
2535 for( ; end_bits != 0; end_bits-- )
2536 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2537 }
2538
2539 memset( mask + offset / 8, 0xFF, len / 8 );
2540}
2541
2542/*
2543 * Check that bitmask is full
2544 */
2545static int ssl_bitmask_check( unsigned char *mask, size_t len )
2546{
2547 size_t i;
2548
2549 for( i = 0; i < len / 8; i++ )
2550 if( mask[i] != 0xFF )
2551 return( -1 );
2552
2553 for( i = 0; i < len % 8; i++ )
2554 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2555 return( -1 );
2556
2557 return( 0 );
2558}
2559
2560/*
2561 * Reassemble fragmented DTLS handshake messages.
2562 *
2563 * Use a temporary buffer for reassembly, divided in two parts:
2564 * - the first holds the reassembled message (including handshake header),
2565 * - the second holds a bitmask indicating which parts of the message
2566 * (excluding headers) have been received so far.
2567 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002568static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2569{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002570 unsigned char *msg, *bitmask;
2571 size_t frag_len, frag_off;
2572 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2573
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002574 if( ssl->handshake == NULL )
2575 {
2576 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2577 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2578 }
2579
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002580 /*
2581 * For first fragment, check size and allocate buffer
2582 */
2583 if( ssl->handshake->hs_msg == NULL )
2584 {
2585 size_t alloc_len;
2586
2587 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2588 msg_len ) );
2589
2590 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2591 {
2592 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2593 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2594 }
2595
2596 /* The bitmask needs one bit per byte of message excluding header */
2597 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2598
2599 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2600 if( ssl->handshake->hs_msg == NULL )
2601 {
2602 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2603 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2604 }
2605
2606 memset( ssl->handshake->hs_msg, 0, alloc_len );
2607
2608 /* Prepare final header: copy msg_type, length and message_seq,
2609 * then add standardised fragment_offset and fragment_length */
2610 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2611 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2612 memcpy( ssl->handshake->hs_msg + 9,
2613 ssl->handshake->hs_msg + 1, 3 );
2614 }
2615 else
2616 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002617 /* Make sure msg_type and length are consistent */
2618 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002619 {
2620 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2621 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2622 }
2623 }
2624
2625 msg = ssl->handshake->hs_msg + 12;
2626 bitmask = msg + msg_len;
2627
2628 /*
2629 * Check and copy current fragment
2630 */
2631 frag_off = ( ssl->in_msg[6] << 16 ) |
2632 ( ssl->in_msg[7] << 8 ) |
2633 ssl->in_msg[8];
2634 frag_len = ( ssl->in_msg[9] << 16 ) |
2635 ( ssl->in_msg[10] << 8 ) |
2636 ssl->in_msg[11];
2637
2638 if( frag_off + frag_len > msg_len )
2639 {
2640 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2641 frag_off, frag_len, msg_len ) );
2642 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2643 }
2644
2645 if( frag_len + 12 > ssl->in_msglen )
2646 {
2647 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2648 frag_len, ssl->in_msglen ) );
2649 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2650 }
2651
2652 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2653 frag_off, frag_len ) );
2654
2655 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2656 ssl_bitmask_set( bitmask, frag_off, frag_len );
2657
2658 /*
2659 * Do we have the complete message by now?
2660 * If yes, finalize it, else ask to read the next record.
2661 */
2662 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2663 {
2664 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2665 return( POLARSSL_ERR_NET_WANT_READ );
2666 }
2667
2668 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2669
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002670 if( ssl->in_left > ssl->next_record_offset )
2671 {
2672 /*
2673 * We've got more data in the buffer after the current record,
2674 * that we don't want to overwrite. Move it before writing the
2675 * reassembled message, and adjust in_left and next_record_offset.
2676 */
2677 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2678 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2679 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2680
2681 /* First compute and check new lengths */
2682 ssl->next_record_offset = new_remain - ssl->in_hdr;
2683 ssl->in_left = ssl->next_record_offset + remain_len;
2684
2685 if( ssl->in_left > SSL_BUFFER_LEN -
2686 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2687 {
2688 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2689 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2690 }
2691
2692 memmove( new_remain, cur_remain, remain_len );
2693 }
2694
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002695 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2696
2697 polarssl_free( ssl->handshake->hs_msg );
2698 ssl->handshake->hs_msg = NULL;
2699
2700 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2701 ssl->in_msg, ssl->in_hslen );
2702
2703 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002704}
2705#endif /* POLARSSL_SSL_PROTO_DTLS */
2706
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002707static int ssl_prepare_handshake_record( ssl_context *ssl )
2708{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002709 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2710 {
2711 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2712 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002713 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002714 }
2715
2716 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2717 ( ssl->in_msg[1] << 16 ) |
2718 ( ssl->in_msg[2] << 8 ) |
2719 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002720
2721 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2722 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002723 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002724
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002725#if defined(POLARSSL_SSL_PROTO_DTLS)
2726 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002727 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002728 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002729 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002730
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002731 /* ssl->handshake is NULL when receiving ClientHello for renego */
2732 if( ssl->handshake != NULL &&
2733 recv_msg_seq != ssl->handshake->in_msg_seq )
2734 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002735 /* No sane server ever retransmits HelloVerifyRequest */
2736 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2737 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002738 {
2739 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2740 "message_seq = %d, start_of_flight = %d",
2741 recv_msg_seq,
2742 ssl->handshake->in_flight_start_seq ) );
2743
2744 if( ( ret = ssl_resend( ssl ) ) != 0 )
2745 {
2746 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2747 return( ret );
2748 }
2749 }
2750 else
2751 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002752 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002753 "message_seq = %d, expected = %d",
2754 recv_msg_seq,
2755 ssl->handshake->in_msg_seq ) );
2756 }
2757
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002758 return( POLARSSL_ERR_NET_WANT_READ );
2759 }
2760 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002761
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002762 /* Reassemble if current message is fragmented or reassembly is
2763 * already in progress */
2764 if( ssl->in_msglen < ssl->in_hslen ||
2765 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2766 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2767 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002768 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002769 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2770
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002771 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2772 {
2773 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2774 return( ret );
2775 }
2776 }
2777 }
2778 else
2779#endif /* POLARSSL_SSL_PROTO_DTLS */
2780 /* With TLS we don't handle fragmentation (for now) */
2781 if( ssl->in_msglen < ssl->in_hslen )
2782 {
2783 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002784 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002785 }
2786
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002787 if( ssl->state != SSL_HANDSHAKE_OVER )
2788 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2789
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002790 /* Handshake message is complete, increment counter */
2791#if defined(POLARSSL_SSL_PROTO_DTLS)
2792 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2793 ssl->handshake != NULL )
2794 {
2795 ssl->handshake->in_msg_seq++;
2796 }
2797#endif
2798
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002799 return( 0 );
2800}
2801
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002802/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002803 * DTLS anti-replay: RFC 6347 4.1.2.6
2804 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002805 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2806 * Bit n is set iff record number in_window_top - n has been seen.
2807 *
2808 * Usually, in_window_top is the last record number seen and the lsb of
2809 * in_window is set. The only exception is the initial state (record number 0
2810 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002811 */
2812#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2813static void ssl_dtls_replay_reset( ssl_context *ssl )
2814{
2815 ssl->in_window_top = 0;
2816 ssl->in_window = 0;
2817}
2818
2819static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2820{
2821 return( ( (uint64_t) buf[0] << 40 ) |
2822 ( (uint64_t) buf[1] << 32 ) |
2823 ( (uint64_t) buf[2] << 24 ) |
2824 ( (uint64_t) buf[3] << 16 ) |
2825 ( (uint64_t) buf[4] << 8 ) |
2826 ( (uint64_t) buf[5] ) );
2827}
2828
2829/*
2830 * Return 0 if sequence number is acceptable, -1 otherwise
2831 */
2832int ssl_dtls_replay_check( ssl_context *ssl )
2833{
2834 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2835 uint64_t bit;
2836
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002837 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2838 return( 0 );
2839
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002840 if( rec_seqnum > ssl->in_window_top )
2841 return( 0 );
2842
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002843 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002844
2845 if( bit >= 64 )
2846 return( -1 );
2847
2848 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2849 return( -1 );
2850
2851 return( 0 );
2852}
2853
2854/*
2855 * Update replay window on new validated record
2856 */
2857void ssl_dtls_replay_update( ssl_context *ssl )
2858{
2859 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2860
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002861 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2862 return;
2863
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002864 if( rec_seqnum > ssl->in_window_top )
2865 {
2866 /* Update window_top and the contents of the window */
2867 uint64_t shift = rec_seqnum - ssl->in_window_top;
2868
2869 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002870 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002871 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002872 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002873 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002874 ssl->in_window |= 1;
2875 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002876
2877 ssl->in_window_top = rec_seqnum;
2878 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002879 else
2880 {
2881 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002882 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002883
2884 if( bit < 64 ) /* Always true, but be extra sure */
2885 ssl->in_window |= (uint64_t) 1 << bit;
2886 }
2887}
2888#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2889
2890/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002891 * ContentType type;
2892 * ProtocolVersion version;
2893 * uint16 epoch; // DTLS only
2894 * uint48 sequence_number; // DTLS only
2895 * uint16 length;
2896 */
2897static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002899 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002900 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002902 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2903
Paul Bakker5121ce52009-01-03 21:22:43 +00002904 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002905 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002906 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
2908 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2909 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002910 ssl->in_msgtype,
2911 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002913 /* Check record type */
2914 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2915 ssl->in_msgtype != SSL_MSG_ALERT &&
2916 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2917 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2918 {
2919 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002920
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002921 if( ( ret = ssl_send_alert_message( ssl,
2922 SSL_ALERT_LEVEL_FATAL,
2923 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2924 {
2925 return( ret );
2926 }
2927
2928 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2929 }
2930
2931 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002932 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 {
2934 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002935 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 }
2937
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002938 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 {
2940 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002941 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 }
2943
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002944 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002945#if defined(POLARSSL_SSL_PROTO_DTLS)
2946 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2947 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002948 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002949
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002950 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002951 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002952 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002953 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002954 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002955 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002956 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002957
2958#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2959 if( ssl_dtls_replay_check( ssl ) != 0 )
2960 {
2961 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
2962 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2963 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002964#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002965 }
2966#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002967
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002968 /* Check length against the size of our buffer */
2969 if( ssl->in_msglen > SSL_BUFFER_LEN
2970 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002971 {
2972 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2973 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2974 }
2975
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002976 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002977 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002978 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002979 if( ssl->in_msglen < 1 ||
2980 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 {
2982 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002983 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002984 }
2985 }
2986 else
2987 {
Paul Bakker48916f92012-09-16 19:57:18 +00002988 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 {
2990 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002991 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 }
2993
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002994#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002996 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 {
2998 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002999 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003001#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003002#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3003 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003004 /*
3005 * TLS encrypted messages can have up to 256 bytes of padding
3006 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003008 ssl->in_msglen > ssl->transform_in->minlen +
3009 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 {
3011 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003012 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 }
3016
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003017 return( 0 );
3018}
Paul Bakker5121ce52009-01-03 21:22:43 +00003019
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003020/*
3021 * If applicable, decrypt (and decompress) record content
3022 */
3023static int ssl_prepare_record_content( ssl_context *ssl )
3024{
3025 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003026
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003028 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakker05ef8352012-05-08 09:17:57 +00003030#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003031 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003032 {
3033 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3034
3035 ret = ssl_hw_record_read( ssl );
3036 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3037 {
3038 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003039 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003040 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003041
3042 if( ret == 0 )
3043 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003044 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003045#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003046 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 {
3048 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3049 {
3050 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3051 return( ret );
3052 }
3053
3054 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3055 ssl->in_msg, ssl->in_msglen );
3056
3057 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3058 {
3059 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003060 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003061 }
3062 }
3063
Paul Bakker2770fbd2012-07-03 13:30:23 +00003064#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003065 if( ssl->transform_in != NULL &&
3066 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003067 {
3068 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3069 {
3070 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3071 return( ret );
3072 }
3073
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003074 // TODO: what's the purpose of these lines? is in_len used?
3075 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3076 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003077 }
3078#endif /* POLARSSL_ZLIB_SUPPORT */
3079
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003080#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003081 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3082 {
3083 ssl_dtls_replay_update( ssl );
3084 }
3085#endif
3086
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003087 return( 0 );
3088}
3089
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003090static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3091
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003092/*
3093 * Read a record.
3094 *
3095 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3096 * and continue reading until a valid record is found.
3097 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003098int ssl_read_record( ssl_context *ssl )
3099{
3100 int ret;
3101
3102 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3103
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003104 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003105 {
3106 /*
3107 * Get next Handshake message in the current record
3108 */
3109 ssl->in_msglen -= ssl->in_hslen;
3110
3111 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3112 ssl->in_msglen );
3113
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003114 SSL_DEBUG_BUF( 4, "remaining content in record",
3115 ssl->in_msg, ssl->in_msglen );
3116
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003117 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3118 return( ret );
3119
3120 return( 0 );
3121 }
3122
3123 ssl->in_hslen = 0;
3124
3125 /*
3126 * Read the record header and parse it
3127 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003128#if defined(POLARSSL_SSL_PROTO_DTLS)
3129read_record_header:
3130#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003131 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3132 {
3133 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3134 return( ret );
3135 }
3136
3137 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003138 {
3139#if defined(POLARSSL_SSL_PROTO_DTLS)
3140 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3141 {
3142 /* Ignore bad record and get next one; drop the whole datagram
3143 * since current header cannot be trusted to find the next record
3144 * in current datagram */
3145 ssl->next_record_offset = 0;
3146 ssl->in_left = 0;
3147
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003148 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003149 goto read_record_header;
3150 }
3151#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003152 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003153 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003154
3155 /*
3156 * Read and optionally decrypt the message contents
3157 */
3158 if( ( ret = ssl_fetch_input( ssl,
3159 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3160 {
3161 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3162 return( ret );
3163 }
3164
3165 /* Done reading this record, get ready for the next one */
3166#if defined(POLARSSL_SSL_PROTO_DTLS)
3167 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3168 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3169 else
3170#endif
3171 ssl->in_left = 0;
3172
3173 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003174 {
3175#if defined(POLARSSL_SSL_PROTO_DTLS)
3176 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3177 {
3178 /* Silently discard invalid records */
3179 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3180 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3181 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003182 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003183 goto read_record_header;
3184 }
3185
3186 return( ret );
3187 }
3188 else
3189#endif
3190 {
3191 /* Error out (and send alert) on invalid records */
3192#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3193 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3194 {
3195 ssl_send_alert_message( ssl,
3196 SSL_ALERT_LEVEL_FATAL,
3197 SSL_ALERT_MSG_BAD_RECORD_MAC );
3198 }
3199#endif
3200 return( ret );
3201 }
3202 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003203
3204 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003205 * When we sent the last flight of the handshake, we MUST respond to a
3206 * retransmit of the peer's previous flight with a retransmit. (In
3207 * practice, only the Finished message will make it, other messages
3208 * including CCS use the old transform so they're dropped as invalid.)
3209 *
3210 * If the record we received is not a handshake message, however, it
3211 * means the peer received our last flight so we can clean up
3212 * handshake info.
3213 *
3214 * This check needs to be done before prepare_handshake() due to an edge
3215 * case: if the client immediately requests renegotiation, this
3216 * finishes the current handshake first, avoiding the new ClientHello
3217 * being mistaken for an ancient message in the current handshake.
3218 */
3219#if defined(POLARSSL_SSL_PROTO_DTLS)
3220 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3221 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003222 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003223 {
3224 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3225 ssl->in_msg[0] == SSL_HS_FINISHED )
3226 {
3227 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3228
3229 if( ( ret = ssl_resend( ssl ) ) != 0 )
3230 {
3231 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3232 return( ret );
3233 }
3234
3235 return( POLARSSL_ERR_NET_WANT_READ );
3236 }
3237 else
3238 {
3239 ssl_handshake_wrapup_free_hs_transform( ssl );
3240 }
3241 }
3242#endif
3243
3244 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003245 * Handle particular types of records
3246 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003247 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3248 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003249 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3250 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 }
3252
3253 if( ssl->in_msgtype == SSL_MSG_ALERT )
3254 {
3255 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3256 ssl->in_msg[0], ssl->in_msg[1] ) );
3257
3258 /*
3259 * Ignore non-fatal alerts, except close_notify
3260 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003261 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003263 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3264 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003265 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
3267
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003268 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3269 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 {
3271 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003272 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 }
3274 }
3275
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003276#if defined(POLARSSL_SSL_PROTO_DTLS)
3277 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3278 {
3279 /* Drop unexpected ChangeCipherSpec messages */
3280 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3281 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3282 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3283 {
3284 SSL_DEBUG_MSG( 2, ( "dropping unexpected ChangeCipherSpec" ) );
3285 return( POLARSSL_ERR_NET_WANT_READ );
3286 }
3287 }
3288#endif
3289
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3291
3292 return( 0 );
3293}
3294
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003295int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3296{
3297 int ret;
3298
3299 if( ( ret = ssl_send_alert_message( ssl,
3300 SSL_ALERT_LEVEL_FATAL,
3301 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3302 {
3303 return( ret );
3304 }
3305
3306 return( 0 );
3307}
3308
Paul Bakker0a925182012-04-16 06:46:41 +00003309int ssl_send_alert_message( ssl_context *ssl,
3310 unsigned char level,
3311 unsigned char message )
3312{
3313 int ret;
3314
3315 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3316
3317 ssl->out_msgtype = SSL_MSG_ALERT;
3318 ssl->out_msglen = 2;
3319 ssl->out_msg[0] = level;
3320 ssl->out_msg[1] = message;
3321
3322 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3323 {
3324 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3325 return( ret );
3326 }
3327
3328 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3329
3330 return( 0 );
3331}
3332
Paul Bakker5121ce52009-01-03 21:22:43 +00003333/*
3334 * Handshake functions
3335 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003336#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3337 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3338 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3339 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3340 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3341 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3342 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003343int ssl_write_certificate( ssl_context *ssl )
3344{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003345 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003346
3347 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3348
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003349 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003350 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3351 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003352 {
3353 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3354 ssl->state++;
3355 return( 0 );
3356 }
3357
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003360}
3361
3362int ssl_parse_certificate( ssl_context *ssl )
3363{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003364 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3365
3366 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3367
3368 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003369 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3370 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003371 {
3372 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3373 ssl->state++;
3374 return( 0 );
3375 }
3376
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003377 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3378 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003379}
3380#else
3381int ssl_write_certificate( ssl_context *ssl )
3382{
3383 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3384 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003385 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003386 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3387
3388 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3389
3390 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003391 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3392 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003393 {
3394 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3395 ssl->state++;
3396 return( 0 );
3397 }
3398
Paul Bakker5121ce52009-01-03 21:22:43 +00003399 if( ssl->endpoint == SSL_IS_CLIENT )
3400 {
3401 if( ssl->client_auth == 0 )
3402 {
3403 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3404 ssl->state++;
3405 return( 0 );
3406 }
3407
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003408#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003409 /*
3410 * If using SSLv3 and got no cert, send an Alert message
3411 * (otherwise an empty Certificate message will be sent).
3412 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003413 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3415 {
3416 ssl->out_msglen = 2;
3417 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003418 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3419 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003420
3421 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3422 goto write_msg;
3423 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003424#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 }
3426 else /* SSL_IS_SERVER */
3427 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003428 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 {
3430 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003431 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003432 }
3433 }
3434
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003435 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003436
3437 /*
3438 * 0 . 0 handshake type
3439 * 1 . 3 handshake length
3440 * 4 . 6 length of all certs
3441 * 7 . 9 length of cert. 1
3442 * 10 . n-1 peer certificate
3443 * n . n+2 length of cert. 2
3444 * n+3 . ... upper level cert, etc.
3445 */
3446 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003447 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448
Paul Bakker29087132010-03-21 21:03:34 +00003449 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 {
3451 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003452 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 {
3454 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3455 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003456 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 }
3458
3459 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3460 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3461 ssl->out_msg[i + 2] = (unsigned char)( n );
3462
3463 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3464 i += n; crt = crt->next;
3465 }
3466
3467 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3468 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3469 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3470
3471 ssl->out_msglen = i;
3472 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3473 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3474
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003475#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003476write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003478
3479 ssl->state++;
3480
3481 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3482 {
3483 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3484 return( ret );
3485 }
3486
3487 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3488
Paul Bakkered27a042013-04-18 22:46:23 +02003489 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003490}
3491
3492int ssl_parse_certificate( ssl_context *ssl )
3493{
Paul Bakkered27a042013-04-18 22:46:23 +02003494 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003495 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003496 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
3498 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3499
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003500 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003501 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3502 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003503 {
3504 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3505 ssl->state++;
3506 return( 0 );
3507 }
3508
Paul Bakker5121ce52009-01-03 21:22:43 +00003509 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003510 ( ssl->authmode == SSL_VERIFY_NONE ||
3511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003512 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003513 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003514 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3515 ssl->state++;
3516 return( 0 );
3517 }
3518
3519 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3520 {
3521 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3522 return( ret );
3523 }
3524
3525 ssl->state++;
3526
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003527#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 /*
3529 * Check if the client sent an empty certificate
3530 */
3531 if( ssl->endpoint == SSL_IS_SERVER &&
3532 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3533 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003534 if( ssl->in_msglen == 2 &&
3535 ssl->in_msgtype == SSL_MSG_ALERT &&
3536 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3537 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003538 {
3539 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3540
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003541 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003542 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3543 return( 0 );
3544 else
Paul Bakker40e46942009-01-03 21:51:57 +00003545 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 }
3547 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003548#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003549
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003550#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3551 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 if( ssl->endpoint == SSL_IS_SERVER &&
3553 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3554 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003555 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3557 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003558 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 {
3560 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3561
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003562 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003564 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 else
3566 return( 0 );
3567 }
3568 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003569#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3570 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003571
3572 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3573 {
3574 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003575 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 }
3577
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003578 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3579 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003580 {
3581 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003582 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 }
3584
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003585 i = ssl_hs_hdr_len( ssl );
3586
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 /*
3588 * Same message structure as in ssl_write_certificate()
3589 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003590 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003591
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003592 if( ssl->in_msg[i] != 0 ||
3593 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 {
3595 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003596 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 }
3598
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003599 /* In case we tried to reuse a session but it failed */
3600 if( ssl->session_negotiate->peer_cert != NULL )
3601 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003602 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003603 polarssl_free( ssl->session_negotiate->peer_cert );
3604 }
3605
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003606 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3607 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 {
3609 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003610 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003611 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 }
3613
Paul Bakkerb6b09562013-09-18 14:17:41 +02003614 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003615
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003616 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003617
3618 while( i < ssl->in_hslen )
3619 {
3620 if( ssl->in_msg[i] != 0 )
3621 {
3622 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003623 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 }
3625
3626 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3627 | (unsigned int) ssl->in_msg[i + 2];
3628 i += 3;
3629
3630 if( n < 128 || i + n > ssl->in_hslen )
3631 {
3632 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003633 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 }
3635
Paul Bakkerddf26b42013-09-18 13:46:23 +02003636 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3637 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 if( ret != 0 )
3639 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003640 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 return( ret );
3642 }
3643
3644 i += n;
3645 }
3646
Paul Bakker48916f92012-09-16 19:57:18 +00003647 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003649 /*
3650 * On client, make sure the server cert doesn't change during renego to
3651 * avoid "triple handshake" attack: https://secure-resumption.com/
3652 */
3653 if( ssl->endpoint == SSL_IS_CLIENT &&
3654 ssl->renegotiation == SSL_RENEGOTIATION )
3655 {
3656 if( ssl->session->peer_cert == NULL )
3657 {
3658 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3659 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3660 }
3661
3662 if( ssl->session->peer_cert->raw.len !=
3663 ssl->session_negotiate->peer_cert->raw.len ||
3664 memcmp( ssl->session->peer_cert->raw.p,
3665 ssl->session_negotiate->peer_cert->raw.p,
3666 ssl->session->peer_cert->raw.len ) != 0 )
3667 {
3668 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3669 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3670 }
3671 }
3672
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 if( ssl->authmode != SSL_VERIFY_NONE )
3674 {
3675 if( ssl->ca_chain == NULL )
3676 {
3677 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003678 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 }
3680
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003681 /*
3682 * Main check: verify certificate
3683 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003684 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3685 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3686 &ssl->session_negotiate->verify_result,
3687 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003688
3689 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003690 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003691 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003692 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003693
3694 /*
3695 * Secondary checks: always done, but change 'ret' only if it was 0
3696 */
3697
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003698#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003699 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003700 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003701
3702 /* If certificate uses an EC key, make sure the curve is OK */
3703 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3704 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3705 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003706 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003707 if( ret == 0 )
3708 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003709 }
3710 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003711#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003712
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003713 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3714 ciphersuite_info,
3715 ! ssl->endpoint ) != 0 )
3716 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003717 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003718 if( ret == 0 )
3719 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3720 }
3721
Paul Bakker5121ce52009-01-03 21:22:43 +00003722 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3723 ret = 0;
3724 }
3725
3726 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3727
3728 return( ret );
3729}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003730#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3731 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3732 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3733 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3734 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3735 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3736 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003737
3738int ssl_write_change_cipher_spec( ssl_context *ssl )
3739{
3740 int ret;
3741
3742 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3743
3744 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3745 ssl->out_msglen = 1;
3746 ssl->out_msg[0] = 1;
3747
Paul Bakker5121ce52009-01-03 21:22:43 +00003748 ssl->state++;
3749
3750 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3751 {
3752 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3753 return( ret );
3754 }
3755
3756 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3757
3758 return( 0 );
3759}
3760
3761int ssl_parse_change_cipher_spec( ssl_context *ssl )
3762{
3763 int ret;
3764
3765 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3766
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3768 {
3769 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3770 return( ret );
3771 }
3772
3773 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3774 {
3775 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003776 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 }
3778
3779 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3780 {
3781 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003782 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003783 }
3784
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003785 /*
3786 * Switch to our negotiated transform and session parameters for inbound
3787 * data.
3788 */
3789 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3790 ssl->transform_in = ssl->transform_negotiate;
3791 ssl->session_in = ssl->session_negotiate;
3792
3793#if defined(POLARSSL_SSL_PROTO_DTLS)
3794 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3795 {
3796#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3797 ssl_dtls_replay_reset( ssl );
3798#endif
3799
3800 /* Increment epoch */
3801 if( ++ssl->in_epoch == 0 )
3802 {
3803 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3804 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3805 }
3806 }
3807 else
3808#endif /* POLARSSL_SSL_PROTO_DTLS */
3809 memset( ssl->in_ctr, 0, 8 );
3810
3811 /*
3812 * Set the in_msg pointer to the correct location based on IV length
3813 */
3814 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3815 {
3816 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3817 ssl->transform_negotiate->fixed_ivlen;
3818 }
3819 else
3820 ssl->in_msg = ssl->in_iv;
3821
3822#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3823 if( ssl_hw_record_activate != NULL )
3824 {
3825 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3826 {
3827 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3828 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3829 }
3830 }
3831#endif
3832
Paul Bakker5121ce52009-01-03 21:22:43 +00003833 ssl->state++;
3834
3835 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3836
3837 return( 0 );
3838}
3839
Paul Bakker41c83d32013-03-20 14:39:14 +01003840void ssl_optimize_checksum( ssl_context *ssl,
3841 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003842{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003843 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003844
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003845#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3846 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003847 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003848 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003849 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003850#endif
3851#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3852#if defined(POLARSSL_SHA512_C)
3853 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3854 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3855 else
3856#endif
3857#if defined(POLARSSL_SHA256_C)
3858 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003859 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003860 else
3861#endif
3862#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003863 {
3864 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003865 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003866 }
Paul Bakker380da532012-04-18 16:10:25 +00003867}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003868
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003869void ssl_reset_checksum( ssl_context *ssl )
3870{
3871#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3872 defined(POLARSSL_SSL_PROTO_TLS1_1)
3873 md5_starts( &ssl->handshake->fin_md5 );
3874 sha1_starts( &ssl->handshake->fin_sha1 );
3875#endif
3876#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3877#if defined(POLARSSL_SHA256_C)
3878 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3879#endif
3880#if defined(POLARSSL_SHA512_C)
3881 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3882#endif
3883#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3884}
3885
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003886static void ssl_update_checksum_start( ssl_context *ssl,
3887 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003888{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003889#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3890 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003891 md5_update( &ssl->handshake->fin_md5 , buf, len );
3892 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003893#endif
3894#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3895#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003896 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003897#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003898#if defined(POLARSSL_SHA512_C)
3899 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003900#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003901#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003902}
3903
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003904#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3905 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003906static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3907 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003908{
Paul Bakker48916f92012-09-16 19:57:18 +00003909 md5_update( &ssl->handshake->fin_md5 , buf, len );
3910 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003911}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003912#endif
Paul Bakker380da532012-04-18 16:10:25 +00003913
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003914#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3915#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003916static void ssl_update_checksum_sha256( ssl_context *ssl,
3917 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003918{
Paul Bakker9e36f042013-06-30 14:34:05 +02003919 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003920}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003921#endif
Paul Bakker380da532012-04-18 16:10:25 +00003922
Paul Bakker9e36f042013-06-30 14:34:05 +02003923#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003924static void ssl_update_checksum_sha384( ssl_context *ssl,
3925 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003926{
Paul Bakker9e36f042013-06-30 14:34:05 +02003927 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003928}
Paul Bakker769075d2012-11-24 11:26:46 +01003929#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003930#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003931
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003932#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003933static void ssl_calc_finished_ssl(
3934 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003935{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003936 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003937 md5_context md5;
3938 sha1_context sha1;
3939
Paul Bakker5121ce52009-01-03 21:22:43 +00003940 unsigned char padbuf[48];
3941 unsigned char md5sum[16];
3942 unsigned char sha1sum[20];
3943
Paul Bakker48916f92012-09-16 19:57:18 +00003944 ssl_session *session = ssl->session_negotiate;
3945 if( !session )
3946 session = ssl->session;
3947
Paul Bakker1ef83d62012-04-11 12:09:53 +00003948 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3949
Paul Bakker48916f92012-09-16 19:57:18 +00003950 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3951 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
3953 /*
3954 * SSLv3:
3955 * hash =
3956 * MD5( master + pad2 +
3957 * MD5( handshake + sender + master + pad1 ) )
3958 * + SHA1( master + pad2 +
3959 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003960 */
3961
Paul Bakker90995b52013-06-24 19:20:35 +02003962#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003964 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003965#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003966
Paul Bakker90995b52013-06-24 19:20:35 +02003967#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003969 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003970#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003971
Paul Bakker3c2122f2013-06-24 19:03:14 +02003972 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3973 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003974
Paul Bakker1ef83d62012-04-11 12:09:53 +00003975 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003976
Paul Bakker3c2122f2013-06-24 19:03:14 +02003977 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003978 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003979 md5_update( &md5, padbuf, 48 );
3980 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003981
Paul Bakker3c2122f2013-06-24 19:03:14 +02003982 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003983 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003984 sha1_update( &sha1, padbuf, 40 );
3985 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
Paul Bakker1ef83d62012-04-11 12:09:53 +00003987 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003988
Paul Bakker1ef83d62012-04-11 12:09:53 +00003989 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003990 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003991 md5_update( &md5, padbuf, 48 );
3992 md5_update( &md5, md5sum, 16 );
3993 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003994
Paul Bakker1ef83d62012-04-11 12:09:53 +00003995 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003996 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003997 sha1_update( &sha1, padbuf , 40 );
3998 sha1_update( &sha1, sha1sum, 20 );
3999 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004000
Paul Bakker1ef83d62012-04-11 12:09:53 +00004001 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004002
Paul Bakker5b4af392014-06-26 12:09:34 +02004003 md5_free( &md5 );
4004 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
Paul Bakker34617722014-06-13 17:20:13 +02004006 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4007 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4008 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
4010 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4011}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004012#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004014#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004015static void ssl_calc_finished_tls(
4016 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004017{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004019 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004020 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004021 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004022 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004023
Paul Bakker48916f92012-09-16 19:57:18 +00004024 ssl_session *session = ssl->session_negotiate;
4025 if( !session )
4026 session = ssl->session;
4027
Paul Bakker1ef83d62012-04-11 12:09:53 +00004028 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
Paul Bakker48916f92012-09-16 19:57:18 +00004030 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4031 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Paul Bakker1ef83d62012-04-11 12:09:53 +00004033 /*
4034 * TLSv1:
4035 * hash = PRF( master, finished_label,
4036 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4037 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004038
Paul Bakker90995b52013-06-24 19:20:35 +02004039#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004040 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4041 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004042#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004043
Paul Bakker90995b52013-06-24 19:20:35 +02004044#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004045 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4046 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004047#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004048
4049 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004050 ? "client finished"
4051 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004052
4053 md5_finish( &md5, padbuf );
4054 sha1_finish( &sha1, padbuf + 16 );
4055
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004056 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004057 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004058
4059 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4060
Paul Bakker5b4af392014-06-26 12:09:34 +02004061 md5_free( &md5 );
4062 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004063
Paul Bakker34617722014-06-13 17:20:13 +02004064 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004065
4066 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4067}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004068#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004069
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004070#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4071#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004072static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004073 ssl_context *ssl, unsigned char *buf, int from )
4074{
4075 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004076 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004077 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004078 unsigned char padbuf[32];
4079
Paul Bakker48916f92012-09-16 19:57:18 +00004080 ssl_session *session = ssl->session_negotiate;
4081 if( !session )
4082 session = ssl->session;
4083
Paul Bakker380da532012-04-18 16:10:25 +00004084 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004085
Paul Bakker9e36f042013-06-30 14:34:05 +02004086 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004087
4088 /*
4089 * TLSv1.2:
4090 * hash = PRF( master, finished_label,
4091 * Hash( handshake ) )[0.11]
4092 */
4093
Paul Bakker9e36f042013-06-30 14:34:05 +02004094#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004095 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004096 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004097#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004098
4099 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004100 ? "client finished"
4101 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004102
Paul Bakker9e36f042013-06-30 14:34:05 +02004103 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004104
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004105 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004106 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004107
4108 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4109
Paul Bakker5b4af392014-06-26 12:09:34 +02004110 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004111
Paul Bakker34617722014-06-13 17:20:13 +02004112 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004113
4114 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4115}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004116#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004117
Paul Bakker9e36f042013-06-30 14:34:05 +02004118#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004119static void ssl_calc_finished_tls_sha384(
4120 ssl_context *ssl, unsigned char *buf, int from )
4121{
4122 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004123 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004124 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004125 unsigned char padbuf[48];
4126
Paul Bakker48916f92012-09-16 19:57:18 +00004127 ssl_session *session = ssl->session_negotiate;
4128 if( !session )
4129 session = ssl->session;
4130
Paul Bakker380da532012-04-18 16:10:25 +00004131 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004132
Paul Bakker9e36f042013-06-30 14:34:05 +02004133 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004134
4135 /*
4136 * TLSv1.2:
4137 * hash = PRF( master, finished_label,
4138 * Hash( handshake ) )[0.11]
4139 */
4140
Paul Bakker9e36f042013-06-30 14:34:05 +02004141#if !defined(POLARSSL_SHA512_ALT)
4142 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4143 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004144#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004145
4146 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004147 ? "client finished"
4148 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004149
Paul Bakker9e36f042013-06-30 14:34:05 +02004150 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004151
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004152 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004153 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004154
4155 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4156
Paul Bakker5b4af392014-06-26 12:09:34 +02004157 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004158
Paul Bakker34617722014-06-13 17:20:13 +02004159 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004160
4161 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4162}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004163#endif /* POLARSSL_SHA512_C */
4164#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004165
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004166static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004167{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004168 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004169
4170 /*
4171 * Free our handshake params
4172 */
4173 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004174 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004175 ssl->handshake = NULL;
4176
4177 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004178 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004179 */
4180 if( ssl->transform )
4181 {
4182 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004183 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004184 }
4185 ssl->transform = ssl->transform_negotiate;
4186 ssl->transform_negotiate = NULL;
4187
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004188 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4189}
4190
4191void ssl_handshake_wrapup( ssl_context *ssl )
4192{
4193 int resume = ssl->handshake->resume;
4194
4195 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4196
4197 if( ssl->renegotiation == SSL_RENEGOTIATION )
4198 {
4199 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4200 ssl->renego_records_seen = 0;
4201 }
4202
4203 /*
4204 * Free the previous session and switch in the current one
4205 */
Paul Bakker0a597072012-09-25 21:55:46 +00004206 if( ssl->session )
4207 {
4208 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004209 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004210 }
4211 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004212 ssl->session_negotiate = NULL;
4213
Paul Bakker0a597072012-09-25 21:55:46 +00004214 /*
4215 * Add cache entry
4216 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004217 if( ssl->f_set_cache != NULL &&
4218 ssl->session->length != 0 &&
4219 resume == 0 )
4220 {
Paul Bakker0a597072012-09-25 21:55:46 +00004221 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4222 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004223 }
Paul Bakker0a597072012-09-25 21:55:46 +00004224
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004225#if defined(POLARSSL_SSL_PROTO_DTLS)
4226 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4227 ssl->handshake->flight != NULL )
4228 {
4229 /* Keep last flight around in case we need to resend it:
4230 * we need the handshake and transform structures for that */
4231 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4232 }
4233 else
4234#endif
4235 ssl_handshake_wrapup_free_hs_transform( ssl );
4236
Paul Bakker48916f92012-09-16 19:57:18 +00004237 ssl->state++;
4238
4239 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4240}
4241
Paul Bakker1ef83d62012-04-11 12:09:53 +00004242int ssl_write_finished( ssl_context *ssl )
4243{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004244 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004245
4246 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4247
Paul Bakker92be97b2013-01-02 17:30:03 +01004248 /*
4249 * Set the out_msg pointer to the correct location based on IV length
4250 */
4251 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4252 {
4253 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4254 ssl->transform_negotiate->fixed_ivlen;
4255 }
4256 else
4257 ssl->out_msg = ssl->out_iv;
4258
Paul Bakker48916f92012-09-16 19:57:18 +00004259 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004260
4261 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004262 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4263
Paul Bakker48916f92012-09-16 19:57:18 +00004264 ssl->verify_data_len = hash_len;
4265 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4266
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 ssl->out_msglen = 4 + hash_len;
4268 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4269 ssl->out_msg[0] = SSL_HS_FINISHED;
4270
4271 /*
4272 * In case of session resuming, invert the client and server
4273 * ChangeCipherSpec messages order.
4274 */
Paul Bakker0a597072012-09-25 21:55:46 +00004275 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 {
4277 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004278 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 else
4280 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4281 }
4282 else
4283 ssl->state++;
4284
Paul Bakker48916f92012-09-16 19:57:18 +00004285 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004286 * Switch to our negotiated transform and session parameters for outbound
4287 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004288 */
4289 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004290
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004291#if defined(POLARSSL_SSL_PROTO_DTLS)
4292 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4293 {
4294 unsigned char i;
4295
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004296 /* Remember current epoch settings for resending */
4297 ssl->handshake->alt_transform_out = ssl->transform_out;
4298 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4299
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004300 /* Set sequence_number to zero */
4301 memset( ssl->out_ctr + 2, 0, 6 );
4302
4303 /* Increment epoch */
4304 for( i = 2; i > 0; i-- )
4305 if( ++ssl->out_ctr[i - 1] != 0 )
4306 break;
4307
4308 /* The loop goes to its end iff the counter is wrapping */
4309 if( i == 0 )
4310 {
4311 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4312 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4313 }
4314 }
4315 else
4316#endif /* POLARSSL_SSL_PROTO_DTLS */
4317 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004318
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004319 ssl->transform_out = ssl->transform_negotiate;
4320 ssl->session_out = ssl->session_negotiate;
4321
Paul Bakker07eb38b2012-12-19 14:42:06 +01004322#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004323 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004324 {
4325 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4326 {
4327 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4328 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4329 }
4330 }
4331#endif
4332
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004333#if defined(POLARSSL_SSL_PROTO_DTLS)
4334 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4335 ssl_send_flight_completed( ssl );
4336#endif
4337
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4339 {
4340 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4341 return( ret );
4342 }
4343
4344 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4345
4346 return( 0 );
4347}
4348
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004349#if defined(POLARSSL_SSL_PROTO_SSL3)
4350#define SSL_MAX_HASH_LEN 36
4351#else
4352#define SSL_MAX_HASH_LEN 12
4353#endif
4354
Paul Bakker5121ce52009-01-03 21:22:43 +00004355int ssl_parse_finished( ssl_context *ssl )
4356{
Paul Bakker23986e52011-04-24 08:57:21 +00004357 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004358 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004359 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004360
4361 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4362
Paul Bakker48916f92012-09-16 19:57:18 +00004363 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004364
Paul Bakker5121ce52009-01-03 21:22:43 +00004365 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4366 {
4367 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4368 return( ret );
4369 }
4370
4371 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4372 {
4373 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004374 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 }
4376
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004377 /* There is currently no ciphersuite using another length with TLS 1.2 */
4378#if defined(POLARSSL_SSL_PROTO_SSL3)
4379 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4380 hash_len = 36;
4381 else
4382#endif
4383 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004384
4385 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004386 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 {
4388 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004389 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004390 }
4391
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004392 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4393 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004394 {
4395 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004396 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004397 }
4398
Paul Bakker48916f92012-09-16 19:57:18 +00004399 ssl->verify_data_len = hash_len;
4400 memcpy( ssl->peer_verify_data, buf, hash_len );
4401
Paul Bakker0a597072012-09-25 21:55:46 +00004402 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004403 {
4404 if( ssl->endpoint == SSL_IS_CLIENT )
4405 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4406
4407 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004408 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004409 }
4410 else
4411 ssl->state++;
4412
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004413#if defined(POLARSSL_SSL_PROTO_DTLS)
4414 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4415 ssl_recv_flight_completed( ssl );
4416#endif
4417
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4419
4420 return( 0 );
4421}
4422
Paul Bakker968afaa2014-07-09 11:09:24 +02004423static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004424{
4425 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4426
4427#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4428 defined(POLARSSL_SSL_PROTO_TLS1_1)
4429 md5_init( &handshake->fin_md5 );
4430 sha1_init( &handshake->fin_sha1 );
4431 md5_starts( &handshake->fin_md5 );
4432 sha1_starts( &handshake->fin_sha1 );
4433#endif
4434#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4435#if defined(POLARSSL_SHA256_C)
4436 sha256_init( &handshake->fin_sha256 );
4437 sha256_starts( &handshake->fin_sha256, 0 );
4438#endif
4439#if defined(POLARSSL_SHA512_C)
4440 sha512_init( &handshake->fin_sha512 );
4441 sha512_starts( &handshake->fin_sha512, 1 );
4442#endif
4443#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4444
4445 handshake->update_checksum = ssl_update_checksum_start;
4446 handshake->sig_alg = SSL_HASH_SHA1;
4447
4448#if defined(POLARSSL_DHM_C)
4449 dhm_init( &handshake->dhm_ctx );
4450#endif
4451#if defined(POLARSSL_ECDH_C)
4452 ecdh_init( &handshake->ecdh_ctx );
4453#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004454}
4455
4456static void ssl_transform_init( ssl_transform *transform )
4457{
4458 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004459
4460 cipher_init( &transform->cipher_ctx_enc );
4461 cipher_init( &transform->cipher_ctx_dec );
4462
4463 md_init( &transform->md_ctx_enc );
4464 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004465}
4466
4467void ssl_session_init( ssl_session *session )
4468{
4469 memset( session, 0, sizeof(ssl_session) );
4470}
4471
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004472static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004473{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004474 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004475 if( ssl->transform_negotiate )
4476 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004477 if( ssl->session_negotiate )
4478 ssl_session_free( ssl->session_negotiate );
4479 if( ssl->handshake )
4480 ssl_handshake_free( ssl->handshake );
4481
4482 /*
4483 * Either the pointers are now NULL or cleared properly and can be freed.
4484 * Now allocate missing structures.
4485 */
4486 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004487 {
4488 ssl->transform_negotiate =
4489 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4490 }
Paul Bakker48916f92012-09-16 19:57:18 +00004491
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004492 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004493 {
4494 ssl->session_negotiate =
4495 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4496 }
Paul Bakker48916f92012-09-16 19:57:18 +00004497
Paul Bakker82788fb2014-10-20 13:59:19 +02004498 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004499 {
4500 ssl->handshake = (ssl_handshake_params *)
4501 polarssl_malloc( sizeof(ssl_handshake_params) );
4502 }
Paul Bakker48916f92012-09-16 19:57:18 +00004503
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004504 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004505 if( ssl->handshake == NULL ||
4506 ssl->transform_negotiate == NULL ||
4507 ssl->session_negotiate == NULL )
4508 {
4509 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004510
4511 polarssl_free( ssl->handshake );
4512 polarssl_free( ssl->transform_negotiate );
4513 polarssl_free( ssl->session_negotiate );
4514
4515 ssl->handshake = NULL;
4516 ssl->transform_negotiate = NULL;
4517 ssl->session_negotiate = NULL;
4518
Paul Bakker48916f92012-09-16 19:57:18 +00004519 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4520 }
4521
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004522 /* Initialize structures */
4523 ssl_session_init( ssl->session_negotiate );
4524 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004525 ssl_handshake_params_init( ssl->handshake );
4526
4527#if defined(POLARSSL_X509_CRT_PARSE_C)
4528 ssl->handshake->key_cert = ssl->key_cert;
4529#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004530
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004531 /*
4532 * We may not know yet if we're using DTLS,
4533 * so always initiliase DTLS-specific fields.
4534 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004535#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004536 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004537
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004538 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004539 if( ssl->endpoint == SSL_IS_CLIENT )
4540 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4541 else
4542 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004543#endif
4544
Paul Bakker48916f92012-09-16 19:57:18 +00004545 return( 0 );
4546}
4547
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004548#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4549/* Dummy cookie callbacks for defaults */
4550static int ssl_cookie_write_dummy( void *ctx,
4551 unsigned char **p, unsigned char *end,
4552 const unsigned char *cli_id, size_t cli_id_len )
4553{
4554 ((void) ctx);
4555 ((void) p);
4556 ((void) end);
4557 ((void) cli_id);
4558 ((void) cli_id_len);
4559
4560 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4561}
4562
4563static int ssl_cookie_check_dummy( void *ctx,
4564 const unsigned char *cookie, size_t cookie_len,
4565 const unsigned char *cli_id, size_t cli_id_len )
4566{
4567 ((void) ctx);
4568 ((void) cookie);
4569 ((void) cookie_len);
4570 ((void) cli_id);
4571 ((void) cli_id_len);
4572
4573 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4574}
4575#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4576
Paul Bakker5121ce52009-01-03 21:22:43 +00004577/*
4578 * Initialize an SSL context
4579 */
4580int ssl_init( ssl_context *ssl )
4581{
Paul Bakker48916f92012-09-16 19:57:18 +00004582 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 int len = SSL_BUFFER_LEN;
4584
4585 memset( ssl, 0, sizeof( ssl_context ) );
4586
Paul Bakker62f2dee2012-09-28 07:31:51 +00004587 /*
4588 * Sane defaults
4589 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004590 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4591 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4592 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4593 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004594
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004595 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004596
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004597 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4598
Paul Bakker62f2dee2012-09-28 07:31:51 +00004599#if defined(POLARSSL_DHM_C)
4600 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4601 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4602 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4603 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4604 {
4605 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4606 return( ret );
4607 }
4608#endif
4609
4610 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004611 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004612 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004613 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004614 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004616 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 {
4618 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004619 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004620 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004621 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004622 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004623 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 }
4625
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004626 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4627 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004628
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004629 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4630 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4631
Paul Bakker606b4ba2013-08-14 16:52:14 +02004632#if defined(POLARSSL_SSL_SESSION_TICKETS)
4633 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4634#endif
4635
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004636#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004637 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004638#endif
4639
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004640#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4641 ssl->f_cookie_write = ssl_cookie_write_dummy;
4642 ssl->f_cookie_check = ssl_cookie_check_dummy;
4643#endif
4644
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004645#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4646 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4647#endif
4648
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004649#if defined(POLARSSL_SSL_PROTO_DTLS)
4650 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4651 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4652#endif
4653
Paul Bakker48916f92012-09-16 19:57:18 +00004654 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4655 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004656
4657 return( 0 );
4658}
4659
4660/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004661 * Reset an initialized and used SSL context for re-use while retaining
4662 * all application-set variables, function pointers and data.
4663 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004664int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004665{
Paul Bakker48916f92012-09-16 19:57:18 +00004666 int ret;
4667
Paul Bakker7eb013f2011-10-06 12:37:39 +00004668 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004669 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4670 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4671
4672 ssl->verify_data_len = 0;
4673 memset( ssl->own_verify_data, 0, 36 );
4674 memset( ssl->peer_verify_data, 0, 36 );
4675
Paul Bakker7eb013f2011-10-06 12:37:39 +00004676 ssl->in_offt = NULL;
4677
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004678 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004679 ssl->in_msgtype = 0;
4680 ssl->in_msglen = 0;
4681 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004682#if defined(POLARSSL_SSL_PROTO_DTLS)
4683 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004684 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004685#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004686#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4687 ssl_dtls_replay_reset( ssl );
4688#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004689
4690 ssl->in_hslen = 0;
4691 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004692 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004693
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004694 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004695 ssl->out_msgtype = 0;
4696 ssl->out_msglen = 0;
4697 ssl->out_left = 0;
4698
Paul Bakker48916f92012-09-16 19:57:18 +00004699 ssl->transform_in = NULL;
4700 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004701
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004702 ssl->renego_records_seen = 0;
4703
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004704 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4705 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004706
4707#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004708 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004709 {
4710 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004711 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004712 {
4713 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4714 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4715 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004716 }
4717#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004718
Paul Bakker48916f92012-09-16 19:57:18 +00004719 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004720 {
Paul Bakker48916f92012-09-16 19:57:18 +00004721 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004722 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004723 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004724 }
Paul Bakker48916f92012-09-16 19:57:18 +00004725
Paul Bakkerc0463502013-02-14 11:19:38 +01004726 if( ssl->session )
4727 {
4728 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004729 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004730 ssl->session = NULL;
4731 }
4732
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004733#if defined(POLARSSL_SSL_ALPN)
4734 ssl->alpn_chosen = NULL;
4735#endif
4736
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004737#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004738 polarssl_free( ssl->cli_id );
4739 ssl->cli_id = NULL;
4740 ssl->cli_id_len = 0;
4741#endif
4742
Paul Bakker48916f92012-09-16 19:57:18 +00004743 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4744 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004745
4746 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004747}
4748
Paul Bakkera503a632013-08-14 13:48:06 +02004749#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004750static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4751{
4752 aes_free( &tkeys->enc );
4753 aes_free( &tkeys->dec );
4754
4755 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4756}
4757
Paul Bakker7eb013f2011-10-06 12:37:39 +00004758/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004759 * Allocate and initialize ticket keys
4760 */
4761static int ssl_ticket_keys_init( ssl_context *ssl )
4762{
4763 int ret;
4764 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004765 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004766
4767 if( ssl->ticket_keys != NULL )
4768 return( 0 );
4769
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004770 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4771 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004772 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4773
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004774 aes_init( &tkeys->enc );
4775 aes_init( &tkeys->dec );
4776
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004777 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004778 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004779 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004780 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004781 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004782 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004783
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004784 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4785 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4786 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4787 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004788 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004789 polarssl_free( tkeys );
4790 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004791 }
4792
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004793 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004794 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004795 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004796 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004797 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004798 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004799
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004800 ssl->ticket_keys = tkeys;
4801
4802 return( 0 );
4803}
Paul Bakkera503a632013-08-14 13:48:06 +02004804#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004805
4806/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004807 * SSL set accessors
4808 */
4809void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4810{
4811 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004812
Paul Bakker606b4ba2013-08-14 16:52:14 +02004813#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004814 if( endpoint == SSL_IS_CLIENT )
4815 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004816#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004817}
4818
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004819int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004820{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004821#if defined(POLARSSL_SSL_PROTO_DTLS)
4822 if( transport == SSL_TRANSPORT_DATAGRAM )
4823 {
4824 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004825
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004826 ssl->out_hdr = ssl->out_buf;
4827 ssl->out_ctr = ssl->out_buf + 3;
4828 ssl->out_len = ssl->out_buf + 11;
4829 ssl->out_iv = ssl->out_buf + 13;
4830 ssl->out_msg = ssl->out_buf + 13;
4831
4832 ssl->in_hdr = ssl->in_buf;
4833 ssl->in_ctr = ssl->in_buf + 3;
4834 ssl->in_len = ssl->in_buf + 11;
4835 ssl->in_iv = ssl->in_buf + 13;
4836 ssl->in_msg = ssl->in_buf + 13;
4837
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004838 /* DTLS starts with TLS1.1 */
4839 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4840 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004841
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004842 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4843 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4844
4845 return( 0 );
4846 }
4847#endif
4848
4849 if( transport == SSL_TRANSPORT_STREAM )
4850 {
4851 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004852
4853 ssl->out_ctr = ssl->out_buf;
4854 ssl->out_hdr = ssl->out_buf + 8;
4855 ssl->out_len = ssl->out_buf + 11;
4856 ssl->out_iv = ssl->out_buf + 13;
4857 ssl->out_msg = ssl->out_buf + 13;
4858
4859 ssl->in_ctr = ssl->in_buf;
4860 ssl->in_hdr = ssl->in_buf + 8;
4861 ssl->in_len = ssl->in_buf + 11;
4862 ssl->in_iv = ssl->in_buf + 13;
4863 ssl->in_msg = ssl->in_buf + 13;
4864
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004865 return( 0 );
4866 }
4867
4868 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004869}
4870
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004871#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4872void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4873{
4874 ssl->anti_replay = mode;
4875}
4876#endif
4877
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004878#if defined(POLARSSL_SSL_PROTO_DTLS)
4879void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4880{
4881 ssl->hs_timeout_min = min;
4882 ssl->hs_timeout_max = max;
4883}
4884#endif
4885
Paul Bakker5121ce52009-01-03 21:22:43 +00004886void ssl_set_authmode( ssl_context *ssl, int authmode )
4887{
4888 ssl->authmode = authmode;
4889}
4890
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004891#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004892void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004893 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004894 void *p_vrfy )
4895{
4896 ssl->f_vrfy = f_vrfy;
4897 ssl->p_vrfy = p_vrfy;
4898}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004899#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004900
Paul Bakker5121ce52009-01-03 21:22:43 +00004901void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004902 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 void *p_rng )
4904{
4905 ssl->f_rng = f_rng;
4906 ssl->p_rng = p_rng;
4907}
4908
4909void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004910 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 void *p_dbg )
4912{
4913 ssl->f_dbg = f_dbg;
4914 ssl->p_dbg = p_dbg;
4915}
4916
4917void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004918 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004919 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004920{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004921 if( p_recv != p_send )
4922 {
4923 ssl->f_recv = NULL;
4924 ssl->f_send = NULL;
4925 ssl->p_bio = NULL;
4926 return;
4927 }
4928
Paul Bakker5121ce52009-01-03 21:22:43 +00004929 ssl->f_recv = f_recv;
4930 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004931 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004932}
4933
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004934void ssl_set_bio_timeout( ssl_context *ssl,
4935 void *p_bio,
4936 int (*f_send)(void *, const unsigned char *, size_t),
4937 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004938 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4939 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004940{
4941 ssl->p_bio = p_bio;
4942 ssl->f_send = f_send;
4943 ssl->f_recv = f_recv;
4944 ssl->f_recv_timeout = f_recv_timeout;
4945 ssl->timeout = timeout;
4946}
4947
Paul Bakker0a597072012-09-25 21:55:46 +00004948void ssl_set_session_cache( ssl_context *ssl,
4949 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4950 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004951{
Paul Bakker0a597072012-09-25 21:55:46 +00004952 ssl->f_get_cache = f_get_cache;
4953 ssl->p_get_cache = p_get_cache;
4954 ssl->f_set_cache = f_set_cache;
4955 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004956}
4957
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004958int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004959{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004960 int ret;
4961
4962 if( ssl == NULL ||
4963 session == NULL ||
4964 ssl->session_negotiate == NULL ||
4965 ssl->endpoint != SSL_IS_CLIENT )
4966 {
4967 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4968 }
4969
4970 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4971 return( ret );
4972
Paul Bakker0a597072012-09-25 21:55:46 +00004973 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004974
4975 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004976}
4977
Paul Bakkerb68cad62012-08-23 08:34:18 +00004978void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004979{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004980 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4981 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4982 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4983 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4984}
4985
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004986void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4987 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004988 int major, int minor )
4989{
4990 if( major != SSL_MAJOR_VERSION_3 )
4991 return;
4992
4993 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4994 return;
4995
4996 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004997}
4998
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004999#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005000/* Add a new (empty) key_cert entry an return a pointer to it */
5001static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5002{
5003 ssl_key_cert *key_cert, *last;
5004
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005005 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5006 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005007 return( NULL );
5008
5009 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5010
5011 /* Append the new key_cert to the (possibly empty) current list */
5012 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005013 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005014 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005015 if( ssl->handshake != NULL )
5016 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005017 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005018 else
5019 {
5020 last = ssl->key_cert;
5021 while( last->next != NULL )
5022 last = last->next;
5023 last->next = key_cert;
5024 }
5025
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005026 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005027}
5028
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005029void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005030 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005031{
5032 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005033 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005034 ssl->peer_cn = peer_cn;
5035}
5036
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005037int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005038 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005039{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005040 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5041
5042 if( key_cert == NULL )
5043 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5044
5045 key_cert->cert = own_cert;
5046 key_cert->key = pk_key;
5047
5048 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005049}
5050
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005051#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005052int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005053 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005054{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005055 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005056 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005057
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005058 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005059 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5060
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005061 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5062 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005063 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005064
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005065 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005066
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005067 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005068 if( ret != 0 )
5069 return( ret );
5070
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005071 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005072 return( ret );
5073
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005074 key_cert->cert = own_cert;
5075 key_cert->key_own_alloc = 1;
5076
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005077 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005078}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005079#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005080
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005081int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005082 void *rsa_key,
5083 rsa_decrypt_func rsa_decrypt,
5084 rsa_sign_func rsa_sign,
5085 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005086{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005087 int ret;
5088 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005089
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005090 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005091 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5092
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005093 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5094 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005095 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005096
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005097 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005098
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005099 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5100 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5101 return( ret );
5102
5103 key_cert->cert = own_cert;
5104 key_cert->key_own_alloc = 1;
5105
5106 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005107}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005108#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005109
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005110#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005111int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5112 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005113{
Paul Bakker6db455e2013-09-18 17:29:31 +02005114 if( psk == NULL || psk_identity == NULL )
5115 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5116
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005117 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005118 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5119
Paul Bakker6db455e2013-09-18 17:29:31 +02005120 if( ssl->psk != NULL )
5121 {
5122 polarssl_free( ssl->psk );
5123 polarssl_free( ssl->psk_identity );
5124 }
5125
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005126 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005127 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005128
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005129 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005130 ssl->psk_identity = (unsigned char *)
5131 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005132
5133 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5134 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5135
5136 memcpy( ssl->psk, psk, ssl->psk_len );
5137 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005138
5139 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005140}
5141
5142void ssl_set_psk_cb( ssl_context *ssl,
5143 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5144 size_t),
5145 void *p_psk )
5146{
5147 ssl->f_psk = f_psk;
5148 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005149}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005150#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005151
Paul Bakker48916f92012-09-16 19:57:18 +00005152#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005153int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005154{
5155 int ret;
5156
Paul Bakker48916f92012-09-16 19:57:18 +00005157 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005158 {
5159 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5160 return( ret );
5161 }
5162
Paul Bakker48916f92012-09-16 19:57:18 +00005163 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005164 {
5165 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5166 return( ret );
5167 }
5168
5169 return( 0 );
5170}
5171
Paul Bakker1b57b062011-01-06 15:48:19 +00005172int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5173{
5174 int ret;
5175
Paul Bakker66d5d072014-06-17 16:39:18 +02005176 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005177 {
5178 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5179 return( ret );
5180 }
5181
Paul Bakker66d5d072014-06-17 16:39:18 +02005182 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005183 {
5184 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5185 return( ret );
5186 }
5187
5188 return( 0 );
5189}
Paul Bakker48916f92012-09-16 19:57:18 +00005190#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005191
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005192#if defined(POLARSSL_SSL_SET_CURVES)
5193/*
5194 * Set the allowed elliptic curves
5195 */
5196void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5197{
5198 ssl->curve_list = curve_list;
5199}
5200#endif
5201
Paul Bakker0be444a2013-08-27 21:55:01 +02005202#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005203int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005204{
5205 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005206 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005207
5208 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005209
5210 if( ssl->hostname_len + 1 == 0 )
5211 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5212
Paul Bakker6e339b52013-07-03 13:37:05 +02005213 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005214
Paul Bakkerb15b8512012-01-13 13:44:06 +00005215 if( ssl->hostname == NULL )
5216 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5217
Paul Bakker3c2122f2013-06-24 19:03:14 +02005218 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005219 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005220
Paul Bakker40ea7de2009-05-03 10:18:48 +00005221 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005222
5223 return( 0 );
5224}
5225
Paul Bakker5701cdc2012-09-27 21:49:42 +00005226void ssl_set_sni( ssl_context *ssl,
5227 int (*f_sni)(void *, ssl_context *,
5228 const unsigned char *, size_t),
5229 void *p_sni )
5230{
5231 ssl->f_sni = f_sni;
5232 ssl->p_sni = p_sni;
5233}
Paul Bakker0be444a2013-08-27 21:55:01 +02005234#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005235
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005236#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005237int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005238{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005239 size_t cur_len, tot_len;
5240 const char **p;
5241
5242 /*
5243 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5244 * truncated". Check lengths now rather than later.
5245 */
5246 tot_len = 0;
5247 for( p = protos; *p != NULL; p++ )
5248 {
5249 cur_len = strlen( *p );
5250 tot_len += cur_len;
5251
5252 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5253 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5254 }
5255
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005256 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005257
5258 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005259}
5260
5261const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5262{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005263 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005264}
5265#endif /* POLARSSL_SSL_ALPN */
5266
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005267static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005268{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005269 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5270 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5271 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5272 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005273 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005274 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005275 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005276
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005277 return( 0 );
5278}
5279
5280int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5281{
5282 if( ssl_check_version( ssl, major, minor ) != 0 )
5283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5284
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005285 ssl->max_major_ver = major;
5286 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005287
5288 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005289}
5290
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005291int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005292{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005293 if( ssl_check_version( ssl, major, minor ) != 0 )
5294 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005295
5296 ssl->min_major_ver = major;
5297 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005298
5299 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005300}
5301
Paul Bakker05decb22013-08-15 13:33:48 +02005302#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005303int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5304{
Paul Bakker77e257e2013-12-16 15:29:52 +01005305 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005306 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005307 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005308 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005309 }
5310
5311 ssl->mfl_code = mfl_code;
5312
5313 return( 0 );
5314}
Paul Bakker05decb22013-08-15 13:33:48 +02005315#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005316
Paul Bakker1f2bc622013-08-15 13:45:55 +02005317#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005318int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005319{
5320 if( ssl->endpoint != SSL_IS_CLIENT )
5321 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5322
Paul Bakker8c1ede62013-07-19 14:14:37 +02005323 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005324
5325 return( 0 );
5326}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005327#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005328
Paul Bakker48916f92012-09-16 19:57:18 +00005329void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5330{
5331 ssl->disable_renegotiation = renegotiation;
5332}
5333
5334void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5335{
5336 ssl->allow_legacy_renegotiation = allow_legacy;
5337}
5338
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005339void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5340{
5341 ssl->renego_max_records = max_records;
5342}
5343
Paul Bakkera503a632013-08-14 13:48:06 +02005344#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005345int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5346{
5347 ssl->session_tickets = use_tickets;
5348
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005349 if( ssl->endpoint == SSL_IS_CLIENT )
5350 return( 0 );
5351
5352 if( ssl->f_rng == NULL )
5353 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5354
5355 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005356}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005357
5358void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5359{
5360 ssl->ticket_lifetime = lifetime;
5361}
Paul Bakkera503a632013-08-14 13:48:06 +02005362#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005363
Paul Bakker5121ce52009-01-03 21:22:43 +00005364/*
5365 * SSL get accessors
5366 */
Paul Bakker23986e52011-04-24 08:57:21 +00005367size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005368{
5369 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5370}
5371
Paul Bakkerff60ee62010-03-16 21:09:09 +00005372int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005373{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005374 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005375}
5376
Paul Bakkere3166ce2011-01-27 17:40:50 +00005377const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005378{
Paul Bakker926c8e42013-03-06 10:23:34 +01005379 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005380 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005381
Paul Bakkere3166ce2011-01-27 17:40:50 +00005382 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005383}
5384
Paul Bakker43ca69c2011-01-15 17:35:19 +00005385const char *ssl_get_version( const ssl_context *ssl )
5386{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005387#if defined(POLARSSL_SSL_PROTO_DTLS)
5388 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5389 {
5390 switch( ssl->minor_ver )
5391 {
5392 case SSL_MINOR_VERSION_2:
5393 return( "DTLSv1.0" );
5394
5395 case SSL_MINOR_VERSION_3:
5396 return( "DTLSv1.2" );
5397
5398 default:
5399 return( "unknown (DTLS)" );
5400 }
5401 }
5402#endif
5403
Paul Bakker43ca69c2011-01-15 17:35:19 +00005404 switch( ssl->minor_ver )
5405 {
5406 case SSL_MINOR_VERSION_0:
5407 return( "SSLv3.0" );
5408
5409 case SSL_MINOR_VERSION_1:
5410 return( "TLSv1.0" );
5411
5412 case SSL_MINOR_VERSION_2:
5413 return( "TLSv1.1" );
5414
Paul Bakker1ef83d62012-04-11 12:09:53 +00005415 case SSL_MINOR_VERSION_3:
5416 return( "TLSv1.2" );
5417
Paul Bakker43ca69c2011-01-15 17:35:19 +00005418 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005419 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005420 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005421}
5422
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005423#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005424const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005425{
5426 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005427 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005428
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005429 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005430}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005431#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005432
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005433int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5434{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005435 if( ssl == NULL ||
5436 dst == NULL ||
5437 ssl->session == NULL ||
5438 ssl->endpoint != SSL_IS_CLIENT )
5439 {
5440 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5441 }
5442
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005443 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005444}
5445
Paul Bakker5121ce52009-01-03 21:22:43 +00005446/*
Paul Bakker1961b702013-01-25 14:49:24 +01005447 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005448 */
Paul Bakker1961b702013-01-25 14:49:24 +01005449int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005450{
Paul Bakker40e46942009-01-03 21:51:57 +00005451 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005452
Paul Bakker40e46942009-01-03 21:51:57 +00005453#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005455 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005456#endif
5457
Paul Bakker40e46942009-01-03 21:51:57 +00005458#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005459 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005460 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005461#endif
5462
Paul Bakker1961b702013-01-25 14:49:24 +01005463 return( ret );
5464}
5465
5466/*
5467 * Perform the SSL handshake
5468 */
5469int ssl_handshake( ssl_context *ssl )
5470{
5471 int ret = 0;
5472
5473 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5474
5475 while( ssl->state != SSL_HANDSHAKE_OVER )
5476 {
5477 ret = ssl_handshake_step( ssl );
5478
5479 if( ret != 0 )
5480 break;
5481 }
5482
Paul Bakker5121ce52009-01-03 21:22:43 +00005483 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5484
5485 return( ret );
5486}
5487
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005488#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005489/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005490 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005491 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005492static int ssl_write_hello_request( ssl_context *ssl )
5493{
5494 int ret;
5495
5496 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5497
5498 ssl->out_msglen = 4;
5499 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5500 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5501
5502 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5503 {
5504 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5505 return( ret );
5506 }
5507
5508 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5509
5510 return( 0 );
5511}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005512#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005513
5514/*
5515 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005516 * - any side: calling ssl_renegotiate(),
5517 * - client: receiving a HelloRequest during ssl_read(),
5518 * - server: receiving any handshake message on server during ssl_read() after
5519 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005520 * If the handshake doesn't complete due to waiting for I/O, it will continue
5521 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005522 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005523static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005524{
5525 int ret;
5526
5527 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5528
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005529 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5530 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005531
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005532 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5533 * the ServerHello will have message_seq = 1" */
5534#if defined(POLARSSL_SSL_PROTO_DTLS)
5535 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005536 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5537 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005538 if( ssl->endpoint == SSL_IS_SERVER )
5539 ssl->handshake->out_msg_seq = 1;
5540 else
5541 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005542 }
5543#endif
5544
Paul Bakker48916f92012-09-16 19:57:18 +00005545 ssl->state = SSL_HELLO_REQUEST;
5546 ssl->renegotiation = SSL_RENEGOTIATION;
5547
Paul Bakker48916f92012-09-16 19:57:18 +00005548 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5549 {
5550 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5551 return( ret );
5552 }
5553
5554 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5555
5556 return( 0 );
5557}
5558
5559/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005560 * Renegotiate current connection on client,
5561 * or request renegotiation on server
5562 */
5563int ssl_renegotiate( ssl_context *ssl )
5564{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005565 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005566
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005567#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005568 /* On server, just send the request */
5569 if( ssl->endpoint == SSL_IS_SERVER )
5570 {
5571 if( ssl->state != SSL_HANDSHAKE_OVER )
5572 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5573
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005574 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5575
5576 /* Did we already try/start sending HelloRequest? */
5577 if( ssl->out_left != 0 )
5578 return( ssl_flush_output( ssl ) );
5579
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005580 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005581 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005582#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005583
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005584#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005585 /*
5586 * On client, either start the renegotiation process or,
5587 * if already in progress, continue the handshake
5588 */
5589 if( ssl->renegotiation != SSL_RENEGOTIATION )
5590 {
5591 if( ssl->state != SSL_HANDSHAKE_OVER )
5592 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5593
5594 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5595 {
5596 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5597 return( ret );
5598 }
5599 }
5600 else
5601 {
5602 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5603 {
5604 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5605 return( ret );
5606 }
5607 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005608#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005609
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005610 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005611}
5612
5613/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005614 * Receive application data decrypted from the SSL layer
5615 */
Paul Bakker23986e52011-04-24 08:57:21 +00005616int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005617{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005618 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005619 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005620
5621 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5622
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005623#if defined(POLARSSL_SSL_PROTO_DTLS)
5624 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5625 ssl->handshake != NULL &&
5626 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5627 {
5628 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5629 return( ret );
5630
5631 if( ( ret = ssl_resend( ssl ) ) != 0 )
5632 return( ret );
5633 }
5634#endif
5635
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 if( ssl->state != SSL_HANDSHAKE_OVER )
5637 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005638 ret = ssl_handshake( ssl );
5639 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5640 {
5641 record_read = 1;
5642 }
5643 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 {
5645 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5646 return( ret );
5647 }
5648 }
5649
5650 if( ssl->in_offt == NULL )
5651 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005652 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005653 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005654 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5655 {
5656 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5657 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005658
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005659 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5660 return( ret );
5661 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005662 }
5663
5664 if( ssl->in_msglen == 0 &&
5665 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5666 {
5667 /*
5668 * OpenSSL sends empty messages to randomize the IV
5669 */
5670 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5671 {
Paul Bakker831a7552011-05-18 13:32:51 +00005672 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5673 return( 0 );
5674
Paul Bakker5121ce52009-01-03 21:22:43 +00005675 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5676 return( ret );
5677 }
5678 }
5679
Paul Bakker48916f92012-09-16 19:57:18 +00005680 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5681 {
5682 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5683
5684 if( ssl->endpoint == SSL_IS_CLIENT &&
5685 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005686 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005687 {
5688 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005689
5690 /* With DTLS, drop the packet (probably from last handshake) */
5691#if defined(POLARSSL_SSL_PROTO_DTLS)
5692 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5693 return( POLARSSL_ERR_NET_WANT_READ );
5694#endif
5695 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5696 }
5697
5698 if( ssl->endpoint == SSL_IS_SERVER &&
5699 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5700 {
5701 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5702
5703 /* With DTLS, drop the packet (probably from last handshake) */
5704#if defined(POLARSSL_SSL_PROTO_DTLS)
5705 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5706 return( POLARSSL_ERR_NET_WANT_READ );
5707#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005708 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5709 }
5710
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005711 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5712 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005713 ssl->allow_legacy_renegotiation ==
5714 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005715 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005716 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005717
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005718#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005719 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005720 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005721 /*
5722 * SSLv3 does not have a "no_renegotiation" alert
5723 */
5724 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5725 return( ret );
5726 }
5727 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005728#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005729#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5730 defined(POLARSSL_SSL_PROTO_TLS1_2)
5731 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005732 {
5733 if( ( ret = ssl_send_alert_message( ssl,
5734 SSL_ALERT_LEVEL_WARNING,
5735 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5736 {
5737 return( ret );
5738 }
Paul Bakker48916f92012-09-16 19:57:18 +00005739 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005740 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005741#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5742 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005743 {
5744 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005745 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005746 }
Paul Bakker48916f92012-09-16 19:57:18 +00005747 }
5748 else
5749 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005750#if defined(POLARSSL_SSL_PROTO_DTLS)
5751 /* DTLS clients need to know renego is server-initiated */
5752 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5753 ssl->endpoint == SSL_IS_CLIENT )
5754 {
5755 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5756 }
5757#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005758 ret = ssl_start_renegotiation( ssl );
5759 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5760 {
5761 record_read = 1;
5762 }
5763 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005764 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005765 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005766 return( ret );
5767 }
Paul Bakker48916f92012-09-16 19:57:18 +00005768 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005769
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005770 /* If a non-handshake record was read during renego, fallthrough,
5771 * else tell the user they should call ssl_read() again */
5772 if( ! record_read )
5773 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005774 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005775 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5776 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005777 ssl->renego_records_seen++;
5778
5779 if( ssl->renego_max_records >= 0 &&
5780 ssl->renego_records_seen > ssl->renego_max_records )
5781 {
5782 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5783 "but not honored by client" ) );
5784 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5785 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005786 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005787
5788 /* Fatal and closure alerts handled by ssl_read_record() */
5789 if( ssl->in_msgtype == SSL_MSG_ALERT )
5790 {
5791 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5792 return( POLARSSL_ERR_NET_WANT_READ );
5793 }
5794
5795 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005796 {
5797 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005798 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005799 }
5800
5801 ssl->in_offt = ssl->in_msg;
5802 }
5803
5804 n = ( len < ssl->in_msglen )
5805 ? len : ssl->in_msglen;
5806
5807 memcpy( buf, ssl->in_offt, n );
5808 ssl->in_msglen -= n;
5809
5810 if( ssl->in_msglen == 0 )
5811 /* all bytes consumed */
5812 ssl->in_offt = NULL;
5813 else
5814 /* more data available */
5815 ssl->in_offt += n;
5816
5817 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5818
Paul Bakker23986e52011-04-24 08:57:21 +00005819 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005820}
5821
5822/*
5823 * Send application data to be encrypted by the SSL layer
5824 */
Paul Bakker23986e52011-04-24 08:57:21 +00005825int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005826{
Paul Bakker23986e52011-04-24 08:57:21 +00005827 int ret;
5828 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005829 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005830
5831 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5832
5833 if( ssl->state != SSL_HANDSHAKE_OVER )
5834 {
5835 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5836 {
5837 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5838 return( ret );
5839 }
5840 }
5841
Paul Bakker05decb22013-08-15 13:33:48 +02005842#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005843 /*
5844 * Assume mfl_code is correct since it was checked when set
5845 */
5846 max_len = mfl_code_to_length[ssl->mfl_code];
5847
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005848 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005849 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005850 */
5851 if( ssl->session_out != NULL &&
5852 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5853 {
5854 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5855 }
Paul Bakker05decb22013-08-15 13:33:48 +02005856#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005857
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005858 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005859
Paul Bakker5121ce52009-01-03 21:22:43 +00005860 if( ssl->out_left != 0 )
5861 {
5862 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5863 {
5864 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5865 return( ret );
5866 }
5867 }
Paul Bakker887bd502011-06-08 13:10:54 +00005868 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005869 {
Paul Bakker887bd502011-06-08 13:10:54 +00005870 ssl->out_msglen = n;
5871 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5872 memcpy( ssl->out_msg, buf, n );
5873
5874 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5875 {
5876 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5877 return( ret );
5878 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005879 }
5880
5881 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5882
Paul Bakker23986e52011-04-24 08:57:21 +00005883 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005884}
5885
5886/*
5887 * Notify the peer that the connection is being closed
5888 */
5889int ssl_close_notify( ssl_context *ssl )
5890{
5891 int ret;
5892
5893 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5894
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005895 if( ssl->out_left != 0 )
5896 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005897
5898 if( ssl->state == SSL_HANDSHAKE_OVER )
5899 {
Paul Bakker48916f92012-09-16 19:57:18 +00005900 if( ( ret = ssl_send_alert_message( ssl,
5901 SSL_ALERT_LEVEL_WARNING,
5902 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005903 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005904 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005905 return( ret );
5906 }
5907 }
5908
5909 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5910
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005911 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005912}
5913
Paul Bakker48916f92012-09-16 19:57:18 +00005914void ssl_transform_free( ssl_transform *transform )
5915{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005916 if( transform == NULL )
5917 return;
5918
Paul Bakker48916f92012-09-16 19:57:18 +00005919#if defined(POLARSSL_ZLIB_SUPPORT)
5920 deflateEnd( &transform->ctx_deflate );
5921 inflateEnd( &transform->ctx_inflate );
5922#endif
5923
Paul Bakker84bbeb52014-07-01 14:53:22 +02005924 cipher_free( &transform->cipher_ctx_enc );
5925 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005926
Paul Bakker84bbeb52014-07-01 14:53:22 +02005927 md_free( &transform->md_ctx_enc );
5928 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005929
Paul Bakker34617722014-06-13 17:20:13 +02005930 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005931}
5932
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005933#if defined(POLARSSL_X509_CRT_PARSE_C)
5934static void ssl_key_cert_free( ssl_key_cert *key_cert )
5935{
5936 ssl_key_cert *cur = key_cert, *next;
5937
5938 while( cur != NULL )
5939 {
5940 next = cur->next;
5941
5942 if( cur->key_own_alloc )
5943 {
5944 pk_free( cur->key );
5945 polarssl_free( cur->key );
5946 }
5947 polarssl_free( cur );
5948
5949 cur = next;
5950 }
5951}
5952#endif /* POLARSSL_X509_CRT_PARSE_C */
5953
Paul Bakker48916f92012-09-16 19:57:18 +00005954void ssl_handshake_free( ssl_handshake_params *handshake )
5955{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005956 if( handshake == NULL )
5957 return;
5958
Paul Bakker48916f92012-09-16 19:57:18 +00005959#if defined(POLARSSL_DHM_C)
5960 dhm_free( &handshake->dhm_ctx );
5961#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005962#if defined(POLARSSL_ECDH_C)
5963 ecdh_free( &handshake->ecdh_ctx );
5964#endif
5965
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005966#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005967 /* explicit void pointer cast for buggy MS compiler */
5968 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005969#endif
5970
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005971#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5972 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5973 /*
5974 * Free only the linked list wrapper, not the keys themselves
5975 * since the belong to the SNI callback
5976 */
5977 if( handshake->sni_key_cert != NULL )
5978 {
5979 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5980
5981 while( cur != NULL )
5982 {
5983 next = cur->next;
5984 polarssl_free( cur );
5985 cur = next;
5986 }
5987 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005988#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005989
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005990#if defined(POLARSSL_SSL_PROTO_DTLS)
5991 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02005992 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02005993 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005994#endif
5995
Paul Bakker34617722014-06-13 17:20:13 +02005996 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005997}
5998
5999void ssl_session_free( ssl_session *session )
6000{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006001 if( session == NULL )
6002 return;
6003
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006004#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006005 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006006 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006007 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006008 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006009 }
Paul Bakkered27a042013-04-18 22:46:23 +02006010#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006011
Paul Bakkera503a632013-08-14 13:48:06 +02006012#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006013 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006014#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006015
Paul Bakker34617722014-06-13 17:20:13 +02006016 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006017}
6018
Paul Bakker5121ce52009-01-03 21:22:43 +00006019/*
6020 * Free an SSL context
6021 */
6022void ssl_free( ssl_context *ssl )
6023{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006024 if( ssl == NULL )
6025 return;
6026
Paul Bakker5121ce52009-01-03 21:22:43 +00006027 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6028
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006029 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006030 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006031 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6032 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 }
6034
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006035 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006037 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6038 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 }
6040
Paul Bakker16770332013-10-11 09:59:44 +02006041#if defined(POLARSSL_ZLIB_SUPPORT)
6042 if( ssl->compress_buf != NULL )
6043 {
Paul Bakker34617722014-06-13 17:20:13 +02006044 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006045 polarssl_free( ssl->compress_buf );
6046 }
6047#endif
6048
Paul Bakker40e46942009-01-03 21:51:57 +00006049#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006050 mpi_free( &ssl->dhm_P );
6051 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006052#endif
6053
Paul Bakker48916f92012-09-16 19:57:18 +00006054 if( ssl->transform )
6055 {
6056 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006057 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006058 }
6059
6060 if( ssl->handshake )
6061 {
6062 ssl_handshake_free( ssl->handshake );
6063 ssl_transform_free( ssl->transform_negotiate );
6064 ssl_session_free( ssl->session_negotiate );
6065
Paul Bakker6e339b52013-07-03 13:37:05 +02006066 polarssl_free( ssl->handshake );
6067 polarssl_free( ssl->transform_negotiate );
6068 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006069 }
6070
Paul Bakkerc0463502013-02-14 11:19:38 +01006071 if( ssl->session )
6072 {
6073 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006074 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006075 }
6076
Paul Bakkera503a632013-08-14 13:48:06 +02006077#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006078 if( ssl->ticket_keys )
6079 {
6080 ssl_ticket_keys_free( ssl->ticket_keys );
6081 polarssl_free( ssl->ticket_keys );
6082 }
Paul Bakkera503a632013-08-14 13:48:06 +02006083#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006084
Paul Bakker0be444a2013-08-27 21:55:01 +02006085#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006086 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006087 {
Paul Bakker34617722014-06-13 17:20:13 +02006088 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006089 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006090 ssl->hostname_len = 0;
6091 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006092#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006093
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006094#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006095 if( ssl->psk != NULL )
6096 {
Paul Bakker34617722014-06-13 17:20:13 +02006097 polarssl_zeroize( ssl->psk, ssl->psk_len );
6098 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006099 polarssl_free( ssl->psk );
6100 polarssl_free( ssl->psk_identity );
6101 ssl->psk_len = 0;
6102 ssl->psk_identity_len = 0;
6103 }
6104#endif
6105
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006106#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006107 ssl_key_cert_free( ssl->key_cert );
6108#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006109
Paul Bakker05ef8352012-05-08 09:17:57 +00006110#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6111 if( ssl_hw_record_finish != NULL )
6112 {
6113 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6114 ssl_hw_record_finish( ssl );
6115 }
6116#endif
6117
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006118#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006119 polarssl_free( ssl->cli_id );
6120#endif
6121
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006123
Paul Bakker86f04f42013-02-14 11:20:09 +01006124 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006125 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006126}
6127
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006128#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006129/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006130 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006131 */
6132unsigned char ssl_sig_from_pk( pk_context *pk )
6133{
6134#if defined(POLARSSL_RSA_C)
6135 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6136 return( SSL_SIG_RSA );
6137#endif
6138#if defined(POLARSSL_ECDSA_C)
6139 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6140 return( SSL_SIG_ECDSA );
6141#endif
6142 return( SSL_SIG_ANON );
6143}
6144
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006145pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6146{
6147 switch( sig )
6148 {
6149#if defined(POLARSSL_RSA_C)
6150 case SSL_SIG_RSA:
6151 return( POLARSSL_PK_RSA );
6152#endif
6153#if defined(POLARSSL_ECDSA_C)
6154 case SSL_SIG_ECDSA:
6155 return( POLARSSL_PK_ECDSA );
6156#endif
6157 default:
6158 return( POLARSSL_PK_NONE );
6159 }
6160}
Paul Bakker9af723c2014-05-01 13:03:14 +02006161#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006162
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006163/*
6164 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6165 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006166md_type_t ssl_md_alg_from_hash( unsigned char hash )
6167{
6168 switch( hash )
6169 {
6170#if defined(POLARSSL_MD5_C)
6171 case SSL_HASH_MD5:
6172 return( POLARSSL_MD_MD5 );
6173#endif
6174#if defined(POLARSSL_SHA1_C)
6175 case SSL_HASH_SHA1:
6176 return( POLARSSL_MD_SHA1 );
6177#endif
6178#if defined(POLARSSL_SHA256_C)
6179 case SSL_HASH_SHA224:
6180 return( POLARSSL_MD_SHA224 );
6181 case SSL_HASH_SHA256:
6182 return( POLARSSL_MD_SHA256 );
6183#endif
6184#if defined(POLARSSL_SHA512_C)
6185 case SSL_HASH_SHA384:
6186 return( POLARSSL_MD_SHA384 );
6187 case SSL_HASH_SHA512:
6188 return( POLARSSL_MD_SHA512 );
6189#endif
6190 default:
6191 return( POLARSSL_MD_NONE );
6192 }
6193}
6194
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006195#if defined(POLARSSL_SSL_SET_CURVES)
6196/*
6197 * Check is a curve proposed by the peer is in our list.
6198 * Return 1 if we're willing to use it, 0 otherwise.
6199 */
6200int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6201{
6202 const ecp_group_id *gid;
6203
6204 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6205 if( *gid == grp_id )
6206 return( 1 );
6207
6208 return( 0 );
6209}
Paul Bakker9af723c2014-05-01 13:03:14 +02006210#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006211
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006212#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006213int ssl_check_cert_usage( const x509_crt *cert,
6214 const ssl_ciphersuite_t *ciphersuite,
6215 int cert_endpoint )
6216{
6217#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6218 int usage = 0;
6219#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006220#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6221 const char *ext_oid;
6222 size_t ext_len;
6223#endif
6224
6225#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6226 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6227 ((void) cert);
6228 ((void) cert_endpoint);
6229#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006230
6231#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6232 if( cert_endpoint == SSL_IS_SERVER )
6233 {
6234 /* Server part of the key exchange */
6235 switch( ciphersuite->key_exchange )
6236 {
6237 case POLARSSL_KEY_EXCHANGE_RSA:
6238 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6239 usage = KU_KEY_ENCIPHERMENT;
6240 break;
6241
6242 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6243 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6244 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6245 usage = KU_DIGITAL_SIGNATURE;
6246 break;
6247
6248 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6249 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6250 usage = KU_KEY_AGREEMENT;
6251 break;
6252
6253 /* Don't use default: we want warnings when adding new values */
6254 case POLARSSL_KEY_EXCHANGE_NONE:
6255 case POLARSSL_KEY_EXCHANGE_PSK:
6256 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6257 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6258 usage = 0;
6259 }
6260 }
6261 else
6262 {
6263 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6264 usage = KU_DIGITAL_SIGNATURE;
6265 }
6266
6267 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6268 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006269#else
6270 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006271#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6272
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006273#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6274 if( cert_endpoint == SSL_IS_SERVER )
6275 {
6276 ext_oid = OID_SERVER_AUTH;
6277 ext_len = OID_SIZE( OID_SERVER_AUTH );
6278 }
6279 else
6280 {
6281 ext_oid = OID_CLIENT_AUTH;
6282 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6283 }
6284
6285 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6286 return( -1 );
6287#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6288
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006289 return( 0 );
6290}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006291#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006292
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006293/*
6294 * Convert version numbers to/from wire format
6295 * and, for DTLS, to/from TLS equivalent.
6296 *
6297 * For TLS this is the identity.
6298 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6299 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6300 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6301 */
6302void ssl_write_version( int major, int minor, int transport,
6303 unsigned char ver[2] )
6304{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006305#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006306 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006307 {
6308 if( minor == SSL_MINOR_VERSION_2 )
6309 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6310
6311 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6312 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6313 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006314 else
6315#else
6316 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006317#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006318 {
6319 ver[0] = (unsigned char) major;
6320 ver[1] = (unsigned char) minor;
6321 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006322}
6323
6324void ssl_read_version( int *major, int *minor, int transport,
6325 const unsigned char ver[2] )
6326{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006327#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006328 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006329 {
6330 *major = 255 - ver[0] + 2;
6331 *minor = 255 - ver[1] + 1;
6332
6333 if( *minor == SSL_MINOR_VERSION_1 )
6334 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6335 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006336 else
6337#else
6338 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006339#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006340 {
6341 *major = ver[0];
6342 *minor = ver[1];
6343 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006344}
6345
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006346#endif /* POLARSSL_SSL_TLS_C */