blob: 089d17ea720511a0e6e90e30399175dbb3ae088b [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é-Gonnard6b651412014-10-01 18:29:03 +02001933 uint32_t timeout;
1934
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001935 /*
1936 * The point is, we need to always read a full datagram at once, so we
1937 * sometimes read more then requested, and handle the additional data.
1938 * It could be the rest of the current record (while fetching the
1939 * header) and/or some other records in the same datagram.
1940 */
1941
1942 /*
1943 * Move to the next record in the already read datagram if applicable
1944 */
1945 if( ssl->next_record_offset != 0 )
1946 {
1947 if( ssl->in_left < ssl->next_record_offset )
1948 {
1949 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1950 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1951 }
1952
1953 ssl->in_left -= ssl->next_record_offset;
1954
1955 if( ssl->in_left != 0 )
1956 {
1957 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1958 ssl->next_record_offset ) );
1959 memmove( ssl->in_hdr,
1960 ssl->in_hdr + ssl->next_record_offset,
1961 ssl->in_left );
1962 }
1963
1964 ssl->next_record_offset = 0;
1965 }
1966
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1968 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001969
1970 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001971 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001972 */
1973 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001974 {
1975 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001976 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001977 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001978
1979 /*
1980 * A record can't be split accross datagrams. If we need to read but
1981 * are not at the beginning of a new record, the caller did something
1982 * wrong.
1983 */
1984 if( ssl->in_left != 0 )
1985 {
1986 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1987 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1988 }
1989
1990 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001991
1992 if( ssl->state != SSL_HANDSHAKE_OVER )
1993 timeout = ssl->handshake->retransmit_timeout;
1994 else
1995 timeout = ssl->read_timeout;
1996
1997 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
1998
1999 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2000 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, timeout );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002001 else
2002 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002003
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002004 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
Paul Bakker831a7552011-05-18 13:32:51 +00002006 if( ret == 0 )
2007 return( POLARSSL_ERR_SSL_CONN_EOF );
2008
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002009 if( ret == POLARSSL_ERR_NET_TIMEOUT ||
2010 ( ret == POLARSSL_ERR_NET_WANT_READ &&
2011 ssl_check_timer( ssl ) != 0 ) )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002012 {
2013 SSL_DEBUG_MSG( 2, ( "recv timeout" ) );
2014
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002015 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002016 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002017 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2018 {
2019 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2020 return( POLARSSL_ERR_NET_TIMEOUT );
2021 }
2022
2023 if( ( ret = ssl_resend( ssl ) ) != 0 )
2024 {
2025 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2026 return( ret );
2027 }
2028
2029 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002030 }
2031
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002032 return( POLARSSL_ERR_NET_TIMEOUT );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002033 }
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 if( ret < 0 )
2036 return( ret );
2037
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002038 ssl->in_left = ret;
2039 }
2040 else
2041#endif
2042 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002043 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2044 ssl->in_left, nb_want ) );
2045
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002046 while( ssl->in_left < nb_want )
2047 {
2048 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002049 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002050
2051 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2052 ssl->in_left, nb_want ) );
2053 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2054
2055 if( ret == 0 )
2056 return( POLARSSL_ERR_SSL_CONN_EOF );
2057
2058 if( ret < 0 )
2059 return( ret );
2060
2061 ssl->in_left += ret;
2062 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 }
2064
2065 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2066
2067 return( 0 );
2068}
2069
2070/*
2071 * Flush any data not yet written
2072 */
2073int ssl_flush_output( ssl_context *ssl )
2074{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002075 int ret;
2076 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
2078 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2079
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002080 if( ssl->f_send == NULL )
2081 {
2082 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2083 "or ssl_set_bio_timeout()" ) );
2084 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2085 }
2086
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002087 /* Avoid incrementing counter if data is flushed */
2088 if( ssl->out_left == 0 )
2089 {
2090 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2091 return( 0 );
2092 }
2093
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 while( ssl->out_left > 0 )
2095 {
2096 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002097 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002099 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2100 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002101 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002102
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2104
2105 if( ret <= 0 )
2106 return( ret );
2107
2108 ssl->out_left -= ret;
2109 }
2110
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002111 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002112 if( ++ssl->out_ctr[i - 1] != 0 )
2113 break;
2114
2115 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002116 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002117 {
2118 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2119 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2120 }
2121
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2123
2124 return( 0 );
2125}
2126
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002127/*
2128 * Functions to handle the DTLS retransmission state machine
2129 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002130#if defined(POLARSSL_SSL_PROTO_DTLS)
2131/*
2132 * Append current handshake message to current outgoing flight
2133 */
2134static int ssl_flight_append( ssl_context *ssl )
2135{
2136 ssl_flight_item *msg;
2137
2138 /* Allocate space for current message */
2139 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2140 {
2141 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2142 sizeof( ssl_flight_item ) ) );
2143 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2144 }
2145
2146 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2147 {
2148 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2149 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2150 }
2151
2152 /* Copy current handshake message with headers */
2153 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2154 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002155 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002156 msg->next = NULL;
2157
2158 /* Append to the current flight */
2159 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002160 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002161 else
2162 {
2163 ssl_flight_item *cur = ssl->handshake->flight;
2164 while( cur->next != NULL )
2165 cur = cur->next;
2166 cur->next = msg;
2167 }
2168
2169 return( 0 );
2170}
2171
2172/*
2173 * Free the current flight of handshake messages
2174 */
2175static void ssl_flight_free( ssl_flight_item *flight )
2176{
2177 ssl_flight_item *cur = flight;
2178 ssl_flight_item *next;
2179
2180 while( cur != NULL )
2181 {
2182 next = cur->next;
2183
2184 polarssl_free( cur->p );
2185 polarssl_free( cur );
2186
2187 cur = next;
2188 }
2189}
2190
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002191#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2192static void ssl_dtls_replay_reset( ssl_context *ssl );
2193#endif
2194
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002195/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002196 * Swap transform_out and out_ctr with the alternative ones
2197 */
2198static void ssl_swap_epochs( ssl_context *ssl )
2199{
2200 ssl_transform *tmp_transform;
2201 unsigned char tmp_out_ctr[8];
2202
2203 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2204 {
2205 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2206 return;
2207 }
2208
2209 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2210
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002211 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002212 tmp_transform = ssl->transform_out;
2213 ssl->transform_out = ssl->handshake->alt_transform_out;
2214 ssl->handshake->alt_transform_out = tmp_transform;
2215
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002216 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002217 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2218 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2219 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002220
2221 /* Adjust to the newly activated transform */
2222 if( ssl->transform_out != NULL &&
2223 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2224 {
2225 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2226 ssl->transform_out->fixed_ivlen;
2227 }
2228 else
2229 ssl->out_msg = ssl->out_iv;
2230
2231#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2232 if( ssl_hw_record_activate != NULL )
2233 {
2234 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2235 {
2236 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2237 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2238 }
2239 }
2240#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002241}
2242
2243/*
2244 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002245 *
2246 * Need to remember the current message in case flush_output returns
2247 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002248 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002249 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002250int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002251{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002252 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002253
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002254 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2255 {
2256 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2257
2258 ssl->handshake->cur_msg = ssl->handshake->flight;
2259 ssl_swap_epochs( ssl );
2260
2261 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002262
2263 /* Cancel running timer */
2264 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002265 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002266
2267 while( ssl->handshake->cur_msg != NULL )
2268 {
2269 int ret;
2270 ssl_flight_item *cur = ssl->handshake->cur_msg;
2271
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002272 /* Swap epochs before sending Finished: we can't do it after
2273 * sending ChangeCipherSpec, in case write returns WANT_READ.
2274 * Must be done before copying, may change out_msg pointer */
2275 if( cur->type == SSL_MSG_HANDSHAKE &&
2276 cur->p[0] == SSL_HS_FINISHED )
2277 {
2278 ssl_swap_epochs( ssl );
2279 }
2280
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002281 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002282 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002283 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002284
2285 ssl->handshake->cur_msg = cur->next;
2286
2287 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2288
2289 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2290 {
2291 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2292 return( ret );
2293 }
2294 }
2295
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002296 if( ssl->state == SSL_HANDSHAKE_OVER )
2297 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2298 else
2299 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002300
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002301 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002302
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002303 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002304
2305 return( 0 );
2306}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002307
2308/*
2309 * To be called when the last message of an incoming flight is received.
2310 */
2311void ssl_recv_flight_completed( ssl_context *ssl )
2312{
2313 /* We won't need to resend that one any more */
2314 ssl_flight_free( ssl->handshake->flight );
2315 ssl->handshake->flight = NULL;
2316 ssl->handshake->cur_msg = NULL;
2317
2318 /* The next incoming flight will start with this msg_seq */
2319 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2320
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002321 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002322 ssl_set_timer( ssl, 0 );
2323
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002324 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2325 ssl->in_msg[0] == SSL_HS_FINISHED )
2326 {
2327 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2328 }
2329 else
2330 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2331}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002332
2333/*
2334 * To be called when the last message of an outgoing flight is send.
2335 */
2336void ssl_send_flight_completed( ssl_context *ssl )
2337{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002338 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002339 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002340
2341 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2342 ssl->in_msg[0] == SSL_HS_FINISHED )
2343 {
2344 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2345 }
2346 else
2347 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2348}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002349#endif /* POLARSSL_SSL_PROTO_DTLS */
2350
Paul Bakker5121ce52009-01-03 21:22:43 +00002351/*
2352 * Record layer functions
2353 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002354
2355/*
2356 * Write current record.
2357 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2358 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002359int ssl_write_record( ssl_context *ssl )
2360{
Paul Bakker05ef8352012-05-08 09:17:57 +00002361 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002362 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002363
2364 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2365
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002366#if defined(POLARSSL_SSL_PROTO_DTLS)
2367 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2368 ssl->handshake != NULL &&
2369 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2370 {
2371 ; /* Skip special handshake treatment when resending */
2372 }
2373 else
2374#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2376 {
2377 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2378 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2379 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2380
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002381 /*
2382 * DTLS has additional fields in the Handshake layer,
2383 * between the length field and the actual payload:
2384 * uint16 message_seq;
2385 * uint24 fragment_offset;
2386 * uint24 fragment_length;
2387 */
2388#if defined(POLARSSL_SSL_PROTO_DTLS)
2389 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2390 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002391 /* Make room for the additional DTLS fields */
2392 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002393 ssl->out_msglen += 8;
2394 len += 8;
2395
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002396 /* Write message_seq and update it, except for HelloRequest */
2397 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2398 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002399 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2400 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2401 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002402 }
2403 else
2404 {
2405 ssl->out_msg[4] = 0;
2406 ssl->out_msg[5] = 0;
2407 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002408
2409 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2410 memset( ssl->out_msg + 6, 0x00, 3 );
2411 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002412 }
2413#endif /* POLARSSL_SSL_PROTO_DTLS */
2414
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002415 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2416 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 }
2418
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002419 /* Save handshake and CCS messages for resending */
2420#if defined(POLARSSL_SSL_PROTO_DTLS)
2421 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2422 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002423 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002424 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2425 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2426 {
2427 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2428 {
2429 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2430 return( ret );
2431 }
2432 }
2433#endif
2434
Paul Bakker2770fbd2012-07-03 13:30:23 +00002435#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002436 if( ssl->transform_out != NULL &&
2437 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002438 {
2439 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2440 {
2441 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2442 return( ret );
2443 }
2444
2445 len = ssl->out_msglen;
2446 }
2447#endif /*POLARSSL_ZLIB_SUPPORT */
2448
Paul Bakker05ef8352012-05-08 09:17:57 +00002449#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002450 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002452 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
Paul Bakker05ef8352012-05-08 09:17:57 +00002454 ret = ssl_hw_record_write( ssl );
2455 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2456 {
2457 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002458 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002459 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002460
2461 if( ret == 0 )
2462 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002463 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002464#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002465 if( !done )
2466 {
2467 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002468 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2469 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002470
2471 ssl->out_len[0] = (unsigned char)( len >> 8 );
2472 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002473
Paul Bakker48916f92012-09-16 19:57:18 +00002474 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002475 {
2476 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2477 {
2478 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2479 return( ret );
2480 }
2481
2482 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002483 ssl->out_len[0] = (unsigned char)( len >> 8 );
2484 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002485 }
2486
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002487 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002488
2489 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2490 "version = [%d:%d], msglen = %d",
2491 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002492 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002493
2494 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002495 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002496 }
2497
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2499 {
2500 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2501 return( ret );
2502 }
2503
2504 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2505
2506 return( 0 );
2507}
2508
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002509#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002510/*
2511 * Mark bits in bitmask (used for DTLS HS reassembly)
2512 */
2513static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2514{
2515 unsigned int start_bits, end_bits;
2516
2517 start_bits = 8 - ( offset % 8 );
2518 if( start_bits != 8 )
2519 {
2520 size_t first_byte_idx = offset / 8;
2521
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002522 /* Special case */
2523 if( len <= start_bits )
2524 {
2525 for( ; len != 0; len-- )
2526 mask[first_byte_idx] |= 1 << ( start_bits - len );
2527
2528 /* Avoid potential issues with offset or len becoming invalid */
2529 return;
2530 }
2531
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002532 offset += start_bits; /* Now offset % 8 == 0 */
2533 len -= start_bits;
2534
2535 for( ; start_bits != 0; start_bits-- )
2536 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2537 }
2538
2539 end_bits = len % 8;
2540 if( end_bits != 0 )
2541 {
2542 size_t last_byte_idx = ( offset + len ) / 8;
2543
2544 len -= end_bits; /* Now len % 8 == 0 */
2545
2546 for( ; end_bits != 0; end_bits-- )
2547 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2548 }
2549
2550 memset( mask + offset / 8, 0xFF, len / 8 );
2551}
2552
2553/*
2554 * Check that bitmask is full
2555 */
2556static int ssl_bitmask_check( unsigned char *mask, size_t len )
2557{
2558 size_t i;
2559
2560 for( i = 0; i < len / 8; i++ )
2561 if( mask[i] != 0xFF )
2562 return( -1 );
2563
2564 for( i = 0; i < len % 8; i++ )
2565 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2566 return( -1 );
2567
2568 return( 0 );
2569}
2570
2571/*
2572 * Reassemble fragmented DTLS handshake messages.
2573 *
2574 * Use a temporary buffer for reassembly, divided in two parts:
2575 * - the first holds the reassembled message (including handshake header),
2576 * - the second holds a bitmask indicating which parts of the message
2577 * (excluding headers) have been received so far.
2578 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002579static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2580{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002581 unsigned char *msg, *bitmask;
2582 size_t frag_len, frag_off;
2583 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2584
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002585 if( ssl->handshake == NULL )
2586 {
2587 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2588 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2589 }
2590
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002591 /*
2592 * For first fragment, check size and allocate buffer
2593 */
2594 if( ssl->handshake->hs_msg == NULL )
2595 {
2596 size_t alloc_len;
2597
2598 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2599 msg_len ) );
2600
2601 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2602 {
2603 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2604 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2605 }
2606
2607 /* The bitmask needs one bit per byte of message excluding header */
2608 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2609
2610 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2611 if( ssl->handshake->hs_msg == NULL )
2612 {
2613 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2614 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2615 }
2616
2617 memset( ssl->handshake->hs_msg, 0, alloc_len );
2618
2619 /* Prepare final header: copy msg_type, length and message_seq,
2620 * then add standardised fragment_offset and fragment_length */
2621 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2622 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2623 memcpy( ssl->handshake->hs_msg + 9,
2624 ssl->handshake->hs_msg + 1, 3 );
2625 }
2626 else
2627 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002628 /* Make sure msg_type and length are consistent */
2629 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002630 {
2631 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2632 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2633 }
2634 }
2635
2636 msg = ssl->handshake->hs_msg + 12;
2637 bitmask = msg + msg_len;
2638
2639 /*
2640 * Check and copy current fragment
2641 */
2642 frag_off = ( ssl->in_msg[6] << 16 ) |
2643 ( ssl->in_msg[7] << 8 ) |
2644 ssl->in_msg[8];
2645 frag_len = ( ssl->in_msg[9] << 16 ) |
2646 ( ssl->in_msg[10] << 8 ) |
2647 ssl->in_msg[11];
2648
2649 if( frag_off + frag_len > msg_len )
2650 {
2651 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2652 frag_off, frag_len, msg_len ) );
2653 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2654 }
2655
2656 if( frag_len + 12 > ssl->in_msglen )
2657 {
2658 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2659 frag_len, ssl->in_msglen ) );
2660 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2661 }
2662
2663 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2664 frag_off, frag_len ) );
2665
2666 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2667 ssl_bitmask_set( bitmask, frag_off, frag_len );
2668
2669 /*
2670 * Do we have the complete message by now?
2671 * If yes, finalize it, else ask to read the next record.
2672 */
2673 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2674 {
2675 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2676 return( POLARSSL_ERR_NET_WANT_READ );
2677 }
2678
2679 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2680
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002681 if( ssl->in_left > ssl->next_record_offset )
2682 {
2683 /*
2684 * We've got more data in the buffer after the current record,
2685 * that we don't want to overwrite. Move it before writing the
2686 * reassembled message, and adjust in_left and next_record_offset.
2687 */
2688 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2689 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2690 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2691
2692 /* First compute and check new lengths */
2693 ssl->next_record_offset = new_remain - ssl->in_hdr;
2694 ssl->in_left = ssl->next_record_offset + remain_len;
2695
2696 if( ssl->in_left > SSL_BUFFER_LEN -
2697 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2698 {
2699 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2700 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2701 }
2702
2703 memmove( new_remain, cur_remain, remain_len );
2704 }
2705
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002706 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2707
2708 polarssl_free( ssl->handshake->hs_msg );
2709 ssl->handshake->hs_msg = NULL;
2710
2711 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2712 ssl->in_msg, ssl->in_hslen );
2713
2714 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002715}
2716#endif /* POLARSSL_SSL_PROTO_DTLS */
2717
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002718static int ssl_prepare_handshake_record( ssl_context *ssl )
2719{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002720 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2723 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002724 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002725 }
2726
2727 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2728 ( ssl->in_msg[1] << 16 ) |
2729 ( ssl->in_msg[2] << 8 ) |
2730 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002731
2732 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2733 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002734 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002735
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002736#if defined(POLARSSL_SSL_PROTO_DTLS)
2737 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002738 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002739 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002740 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002741
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002742 /* ssl->handshake is NULL when receiving ClientHello for renego */
2743 if( ssl->handshake != NULL &&
2744 recv_msg_seq != ssl->handshake->in_msg_seq )
2745 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002746 /* No sane server ever retransmits HelloVerifyRequest */
2747 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2748 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002749 {
2750 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2751 "message_seq = %d, start_of_flight = %d",
2752 recv_msg_seq,
2753 ssl->handshake->in_flight_start_seq ) );
2754
2755 if( ( ret = ssl_resend( ssl ) ) != 0 )
2756 {
2757 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2758 return( ret );
2759 }
2760 }
2761 else
2762 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002763 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002764 "message_seq = %d, expected = %d",
2765 recv_msg_seq,
2766 ssl->handshake->in_msg_seq ) );
2767 }
2768
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002769 return( POLARSSL_ERR_NET_WANT_READ );
2770 }
2771 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002772
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002773 /* Reassemble if current message is fragmented or reassembly is
2774 * already in progress */
2775 if( ssl->in_msglen < ssl->in_hslen ||
2776 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2777 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2778 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002779 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002780 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2781
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002782 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2783 {
2784 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2785 return( ret );
2786 }
2787 }
2788 }
2789 else
2790#endif /* POLARSSL_SSL_PROTO_DTLS */
2791 /* With TLS we don't handle fragmentation (for now) */
2792 if( ssl->in_msglen < ssl->in_hslen )
2793 {
2794 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002795 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002796 }
2797
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002798 if( ssl->state != SSL_HANDSHAKE_OVER )
2799 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2800
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002801 /* Handshake message is complete, increment counter */
2802#if defined(POLARSSL_SSL_PROTO_DTLS)
2803 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2804 ssl->handshake != NULL )
2805 {
2806 ssl->handshake->in_msg_seq++;
2807 }
2808#endif
2809
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002810 return( 0 );
2811}
2812
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002813/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002814 * DTLS anti-replay: RFC 6347 4.1.2.6
2815 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002816 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2817 * Bit n is set iff record number in_window_top - n has been seen.
2818 *
2819 * Usually, in_window_top is the last record number seen and the lsb of
2820 * in_window is set. The only exception is the initial state (record number 0
2821 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002822 */
2823#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2824static void ssl_dtls_replay_reset( ssl_context *ssl )
2825{
2826 ssl->in_window_top = 0;
2827 ssl->in_window = 0;
2828}
2829
2830static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2831{
2832 return( ( (uint64_t) buf[0] << 40 ) |
2833 ( (uint64_t) buf[1] << 32 ) |
2834 ( (uint64_t) buf[2] << 24 ) |
2835 ( (uint64_t) buf[3] << 16 ) |
2836 ( (uint64_t) buf[4] << 8 ) |
2837 ( (uint64_t) buf[5] ) );
2838}
2839
2840/*
2841 * Return 0 if sequence number is acceptable, -1 otherwise
2842 */
2843int ssl_dtls_replay_check( ssl_context *ssl )
2844{
2845 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2846 uint64_t bit;
2847
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002848 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2849 return( 0 );
2850
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002851 if( rec_seqnum > ssl->in_window_top )
2852 return( 0 );
2853
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002854 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002855
2856 if( bit >= 64 )
2857 return( -1 );
2858
2859 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2860 return( -1 );
2861
2862 return( 0 );
2863}
2864
2865/*
2866 * Update replay window on new validated record
2867 */
2868void ssl_dtls_replay_update( ssl_context *ssl )
2869{
2870 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2871
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002872 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2873 return;
2874
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002875 if( rec_seqnum > ssl->in_window_top )
2876 {
2877 /* Update window_top and the contents of the window */
2878 uint64_t shift = rec_seqnum - ssl->in_window_top;
2879
2880 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002881 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002882 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002883 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002884 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002885 ssl->in_window |= 1;
2886 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002887
2888 ssl->in_window_top = rec_seqnum;
2889 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002890 else
2891 {
2892 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002893 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002894
2895 if( bit < 64 ) /* Always true, but be extra sure */
2896 ssl->in_window |= (uint64_t) 1 << bit;
2897 }
2898}
2899#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2900
2901/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002902 * ContentType type;
2903 * ProtocolVersion version;
2904 * uint16 epoch; // DTLS only
2905 * uint48 sequence_number; // DTLS only
2906 * uint16 length;
2907 */
2908static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002909{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002910 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002911 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002913 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2914
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002916 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002917 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
2919 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2920 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002921 ssl->in_msgtype,
2922 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002924 /* Check record type */
2925 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2926 ssl->in_msgtype != SSL_MSG_ALERT &&
2927 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2928 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2929 {
2930 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002931
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002932 if( ( ret = ssl_send_alert_message( ssl,
2933 SSL_ALERT_LEVEL_FATAL,
2934 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2935 {
2936 return( ret );
2937 }
2938
2939 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2940 }
2941
2942 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002943 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 {
2945 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002946 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 }
2948
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002949 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 {
2951 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002952 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
2954
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002955 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002956#if defined(POLARSSL_SSL_PROTO_DTLS)
2957 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2958 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002959 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002960
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002961 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002962 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002963 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002964 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002965 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002966 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002967 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002968
2969#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2970 if( ssl_dtls_replay_check( ssl ) != 0 )
2971 {
2972 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
2973 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2974 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002975#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002976 }
2977#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002978
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002979 /* Check length against the size of our buffer */
2980 if( ssl->in_msglen > SSL_BUFFER_LEN
2981 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002982 {
2983 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2984 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2985 }
2986
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002987 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002988 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002989 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002990 if( ssl->in_msglen < 1 ||
2991 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992 {
2993 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002994 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 }
2996 }
2997 else
2998 {
Paul Bakker48916f92012-09-16 19:57:18 +00002999 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 {
3001 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003002 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 }
3004
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003006 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003007 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 {
3009 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003010 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003013#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3014 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003015 /*
3016 * TLS encrypted messages can have up to 256 bytes of padding
3017 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003019 ssl->in_msglen > ssl->transform_in->minlen +
3020 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021 {
3022 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003023 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003025#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003026 }
3027
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003028 return( 0 );
3029}
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003031/*
3032 * If applicable, decrypt (and decompress) record content
3033 */
3034static int ssl_prepare_record_content( ssl_context *ssl )
3035{
3036 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003037
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003039 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Paul Bakker05ef8352012-05-08 09:17:57 +00003041#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003042 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003043 {
3044 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3045
3046 ret = ssl_hw_record_read( ssl );
3047 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3048 {
3049 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003050 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003051 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003052
3053 if( ret == 0 )
3054 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003055 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003056#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003057 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003058 {
3059 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3060 {
3061 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3062 return( ret );
3063 }
3064
3065 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3066 ssl->in_msg, ssl->in_msglen );
3067
3068 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3069 {
3070 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003071 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072 }
3073 }
3074
Paul Bakker2770fbd2012-07-03 13:30:23 +00003075#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003076 if( ssl->transform_in != NULL &&
3077 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003078 {
3079 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3080 {
3081 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3082 return( ret );
3083 }
3084
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003085 // TODO: what's the purpose of these lines? is in_len used?
3086 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3087 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003088 }
3089#endif /* POLARSSL_ZLIB_SUPPORT */
3090
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003091#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003092 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3093 {
3094 ssl_dtls_replay_update( ssl );
3095 }
3096#endif
3097
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003098 return( 0 );
3099}
3100
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003101static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3102
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003103/*
3104 * Read a record.
3105 *
3106 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3107 * and continue reading until a valid record is found.
3108 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003109int ssl_read_record( ssl_context *ssl )
3110{
3111 int ret;
3112
3113 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3114
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003115 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003116 {
3117 /*
3118 * Get next Handshake message in the current record
3119 */
3120 ssl->in_msglen -= ssl->in_hslen;
3121
3122 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3123 ssl->in_msglen );
3124
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003125 SSL_DEBUG_BUF( 4, "remaining content in record",
3126 ssl->in_msg, ssl->in_msglen );
3127
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003128 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3129 return( ret );
3130
3131 return( 0 );
3132 }
3133
3134 ssl->in_hslen = 0;
3135
3136 /*
3137 * Read the record header and parse it
3138 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003139#if defined(POLARSSL_SSL_PROTO_DTLS)
3140read_record_header:
3141#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003142 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3143 {
3144 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3145 return( ret );
3146 }
3147
3148 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003149 {
3150#if defined(POLARSSL_SSL_PROTO_DTLS)
3151 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3152 {
3153 /* Ignore bad record and get next one; drop the whole datagram
3154 * since current header cannot be trusted to find the next record
3155 * in current datagram */
3156 ssl->next_record_offset = 0;
3157 ssl->in_left = 0;
3158
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003159 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003160 goto read_record_header;
3161 }
3162#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003163 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003164 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003165
3166 /*
3167 * Read and optionally decrypt the message contents
3168 */
3169 if( ( ret = ssl_fetch_input( ssl,
3170 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3171 {
3172 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3173 return( ret );
3174 }
3175
3176 /* Done reading this record, get ready for the next one */
3177#if defined(POLARSSL_SSL_PROTO_DTLS)
3178 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3179 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3180 else
3181#endif
3182 ssl->in_left = 0;
3183
3184 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003185 {
3186#if defined(POLARSSL_SSL_PROTO_DTLS)
3187 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3188 {
3189 /* Silently discard invalid records */
3190 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3191 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3192 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003193 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003194 goto read_record_header;
3195 }
3196
3197 return( ret );
3198 }
3199 else
3200#endif
3201 {
3202 /* Error out (and send alert) on invalid records */
3203#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3204 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3205 {
3206 ssl_send_alert_message( ssl,
3207 SSL_ALERT_LEVEL_FATAL,
3208 SSL_ALERT_MSG_BAD_RECORD_MAC );
3209 }
3210#endif
3211 return( ret );
3212 }
3213 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003214
3215 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003216 * When we sent the last flight of the handshake, we MUST respond to a
3217 * retransmit of the peer's previous flight with a retransmit. (In
3218 * practice, only the Finished message will make it, other messages
3219 * including CCS use the old transform so they're dropped as invalid.)
3220 *
3221 * If the record we received is not a handshake message, however, it
3222 * means the peer received our last flight so we can clean up
3223 * handshake info.
3224 *
3225 * This check needs to be done before prepare_handshake() due to an edge
3226 * case: if the client immediately requests renegotiation, this
3227 * finishes the current handshake first, avoiding the new ClientHello
3228 * being mistaken for an ancient message in the current handshake.
3229 */
3230#if defined(POLARSSL_SSL_PROTO_DTLS)
3231 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3232 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003233 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003234 {
3235 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3236 ssl->in_msg[0] == SSL_HS_FINISHED )
3237 {
3238 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3239
3240 if( ( ret = ssl_resend( ssl ) ) != 0 )
3241 {
3242 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3243 return( ret );
3244 }
3245
3246 return( POLARSSL_ERR_NET_WANT_READ );
3247 }
3248 else
3249 {
3250 ssl_handshake_wrapup_free_hs_transform( ssl );
3251 }
3252 }
3253#endif
3254
3255 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003256 * Handle particular types of records
3257 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3259 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003260 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3261 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003262 }
3263
3264 if( ssl->in_msgtype == SSL_MSG_ALERT )
3265 {
3266 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3267 ssl->in_msg[0], ssl->in_msg[1] ) );
3268
3269 /*
3270 * Ignore non-fatal alerts, except close_notify
3271 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003272 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003274 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3275 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003276 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 }
3278
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003279 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3280 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 {
3282 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003283 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285 }
3286
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003287#if defined(POLARSSL_SSL_PROTO_DTLS)
3288 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3289 {
3290 /* Drop unexpected ChangeCipherSpec messages */
3291 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3292 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3293 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3294 {
3295 SSL_DEBUG_MSG( 2, ( "dropping unexpected ChangeCipherSpec" ) );
3296 return( POLARSSL_ERR_NET_WANT_READ );
3297 }
3298 }
3299#endif
3300
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3302
3303 return( 0 );
3304}
3305
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003306int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3307{
3308 int ret;
3309
3310 if( ( ret = ssl_send_alert_message( ssl,
3311 SSL_ALERT_LEVEL_FATAL,
3312 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3313 {
3314 return( ret );
3315 }
3316
3317 return( 0 );
3318}
3319
Paul Bakker0a925182012-04-16 06:46:41 +00003320int ssl_send_alert_message( ssl_context *ssl,
3321 unsigned char level,
3322 unsigned char message )
3323{
3324 int ret;
3325
3326 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3327
3328 ssl->out_msgtype = SSL_MSG_ALERT;
3329 ssl->out_msglen = 2;
3330 ssl->out_msg[0] = level;
3331 ssl->out_msg[1] = message;
3332
3333 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3334 {
3335 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3336 return( ret );
3337 }
3338
3339 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3340
3341 return( 0 );
3342}
3343
Paul Bakker5121ce52009-01-03 21:22:43 +00003344/*
3345 * Handshake functions
3346 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003347#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3348 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3349 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3350 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3351 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3352 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3353 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003354int ssl_write_certificate( ssl_context *ssl )
3355{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003356 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003357
3358 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3359
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003360 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003361 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3362 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003363 {
3364 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3365 ssl->state++;
3366 return( 0 );
3367 }
3368
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003369 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3370 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003371}
3372
3373int ssl_parse_certificate( ssl_context *ssl )
3374{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003375 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3376
3377 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3378
3379 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003380 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3381 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003382 {
3383 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3384 ssl->state++;
3385 return( 0 );
3386 }
3387
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003388 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3389 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003390}
3391#else
3392int ssl_write_certificate( ssl_context *ssl )
3393{
3394 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3395 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003396 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003397 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3398
3399 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3400
3401 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3403 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003404 {
3405 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3406 ssl->state++;
3407 return( 0 );
3408 }
3409
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 if( ssl->endpoint == SSL_IS_CLIENT )
3411 {
3412 if( ssl->client_auth == 0 )
3413 {
3414 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3415 ssl->state++;
3416 return( 0 );
3417 }
3418
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003419#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 /*
3421 * If using SSLv3 and got no cert, send an Alert message
3422 * (otherwise an empty Certificate message will be sent).
3423 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003424 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3426 {
3427 ssl->out_msglen = 2;
3428 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003429 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3430 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
3432 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3433 goto write_msg;
3434 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003435#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 }
3437 else /* SSL_IS_SERVER */
3438 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003439 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003440 {
3441 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003442 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 }
3444 }
3445
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003446 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
3448 /*
3449 * 0 . 0 handshake type
3450 * 1 . 3 handshake length
3451 * 4 . 6 length of all certs
3452 * 7 . 9 length of cert. 1
3453 * 10 . n-1 peer certificate
3454 * n . n+2 length of cert. 2
3455 * n+3 . ... upper level cert, etc.
3456 */
3457 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003458 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459
Paul Bakker29087132010-03-21 21:03:34 +00003460 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 {
3462 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003463 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003464 {
3465 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3466 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003467 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 }
3469
3470 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3471 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3472 ssl->out_msg[i + 2] = (unsigned char)( n );
3473
3474 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3475 i += n; crt = crt->next;
3476 }
3477
3478 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3479 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3480 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3481
3482 ssl->out_msglen = i;
3483 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3484 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3485
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003486#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003487write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003488#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003489
3490 ssl->state++;
3491
3492 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3493 {
3494 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3495 return( ret );
3496 }
3497
3498 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3499
Paul Bakkered27a042013-04-18 22:46:23 +02003500 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003501}
3502
3503int ssl_parse_certificate( ssl_context *ssl )
3504{
Paul Bakkered27a042013-04-18 22:46:23 +02003505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003506 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003507 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
3509 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3510
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003511 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003514 {
3515 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3516 ssl->state++;
3517 return( 0 );
3518 }
3519
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003521 ( ssl->authmode == SSL_VERIFY_NONE ||
3522 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003523 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003524 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3526 ssl->state++;
3527 return( 0 );
3528 }
3529
3530 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3531 {
3532 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3533 return( ret );
3534 }
3535
3536 ssl->state++;
3537
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003538#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 /*
3540 * Check if the client sent an empty certificate
3541 */
3542 if( ssl->endpoint == SSL_IS_SERVER &&
3543 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3544 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003545 if( ssl->in_msglen == 2 &&
3546 ssl->in_msgtype == SSL_MSG_ALERT &&
3547 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3548 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 {
3550 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3551
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003552 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3554 return( 0 );
3555 else
Paul Bakker40e46942009-01-03 21:51:57 +00003556 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 }
3558 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003559#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003561#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3562 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003563 if( ssl->endpoint == SSL_IS_SERVER &&
3564 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3565 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003566 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3568 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003569 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003570 {
3571 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3572
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003573 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003575 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 else
3577 return( 0 );
3578 }
3579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003580#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3581 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
3583 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3584 {
3585 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003586 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003587 }
3588
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003589 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3590 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 {
3592 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003593 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 }
3595
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003596 i = ssl_hs_hdr_len( ssl );
3597
Paul Bakker5121ce52009-01-03 21:22:43 +00003598 /*
3599 * Same message structure as in ssl_write_certificate()
3600 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003601 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003602
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003603 if( ssl->in_msg[i] != 0 ||
3604 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 {
3606 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003607 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 }
3609
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003610 /* In case we tried to reuse a session but it failed */
3611 if( ssl->session_negotiate->peer_cert != NULL )
3612 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003613 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003614 polarssl_free( ssl->session_negotiate->peer_cert );
3615 }
3616
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003617 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3618 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003619 {
3620 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003621 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003622 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003623 }
3624
Paul Bakkerb6b09562013-09-18 14:17:41 +02003625 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003626
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003627 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003628
3629 while( i < ssl->in_hslen )
3630 {
3631 if( ssl->in_msg[i] != 0 )
3632 {
3633 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003634 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 }
3636
3637 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3638 | (unsigned int) ssl->in_msg[i + 2];
3639 i += 3;
3640
3641 if( n < 128 || i + n > ssl->in_hslen )
3642 {
3643 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 }
3646
Paul Bakkerddf26b42013-09-18 13:46:23 +02003647 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3648 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649 if( ret != 0 )
3650 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003651 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 return( ret );
3653 }
3654
3655 i += n;
3656 }
3657
Paul Bakker48916f92012-09-16 19:57:18 +00003658 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003659
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003660 /*
3661 * On client, make sure the server cert doesn't change during renego to
3662 * avoid "triple handshake" attack: https://secure-resumption.com/
3663 */
3664 if( ssl->endpoint == SSL_IS_CLIENT &&
3665 ssl->renegotiation == SSL_RENEGOTIATION )
3666 {
3667 if( ssl->session->peer_cert == NULL )
3668 {
3669 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3670 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3671 }
3672
3673 if( ssl->session->peer_cert->raw.len !=
3674 ssl->session_negotiate->peer_cert->raw.len ||
3675 memcmp( ssl->session->peer_cert->raw.p,
3676 ssl->session_negotiate->peer_cert->raw.p,
3677 ssl->session->peer_cert->raw.len ) != 0 )
3678 {
3679 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3680 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3681 }
3682 }
3683
Paul Bakker5121ce52009-01-03 21:22:43 +00003684 if( ssl->authmode != SSL_VERIFY_NONE )
3685 {
3686 if( ssl->ca_chain == NULL )
3687 {
3688 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003689 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003690 }
3691
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003692 /*
3693 * Main check: verify certificate
3694 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003695 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3696 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3697 &ssl->session_negotiate->verify_result,
3698 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003699
3700 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003701 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003703 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003704
3705 /*
3706 * Secondary checks: always done, but change 'ret' only if it was 0
3707 */
3708
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003709#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003710 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003711 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003712
3713 /* If certificate uses an EC key, make sure the curve is OK */
3714 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3715 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3716 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003717 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003718 if( ret == 0 )
3719 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003720 }
3721 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003722#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003723
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003724 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3725 ciphersuite_info,
3726 ! ssl->endpoint ) != 0 )
3727 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003728 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003729 if( ret == 0 )
3730 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3731 }
3732
Paul Bakker5121ce52009-01-03 21:22:43 +00003733 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3734 ret = 0;
3735 }
3736
3737 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3738
3739 return( ret );
3740}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003741#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3742 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3743 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3744 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3745 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3746 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3747 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
3749int ssl_write_change_cipher_spec( ssl_context *ssl )
3750{
3751 int ret;
3752
3753 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3754
3755 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3756 ssl->out_msglen = 1;
3757 ssl->out_msg[0] = 1;
3758
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 ssl->state++;
3760
3761 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3762 {
3763 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3764 return( ret );
3765 }
3766
3767 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3768
3769 return( 0 );
3770}
3771
3772int ssl_parse_change_cipher_spec( ssl_context *ssl )
3773{
3774 int ret;
3775
3776 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3777
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3779 {
3780 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3781 return( ret );
3782 }
3783
3784 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3785 {
3786 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003787 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003788 }
3789
3790 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3791 {
3792 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003793 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 }
3795
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003796 /*
3797 * Switch to our negotiated transform and session parameters for inbound
3798 * data.
3799 */
3800 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3801 ssl->transform_in = ssl->transform_negotiate;
3802 ssl->session_in = ssl->session_negotiate;
3803
3804#if defined(POLARSSL_SSL_PROTO_DTLS)
3805 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3806 {
3807#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3808 ssl_dtls_replay_reset( ssl );
3809#endif
3810
3811 /* Increment epoch */
3812 if( ++ssl->in_epoch == 0 )
3813 {
3814 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3815 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3816 }
3817 }
3818 else
3819#endif /* POLARSSL_SSL_PROTO_DTLS */
3820 memset( ssl->in_ctr, 0, 8 );
3821
3822 /*
3823 * Set the in_msg pointer to the correct location based on IV length
3824 */
3825 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3826 {
3827 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3828 ssl->transform_negotiate->fixed_ivlen;
3829 }
3830 else
3831 ssl->in_msg = ssl->in_iv;
3832
3833#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3834 if( ssl_hw_record_activate != NULL )
3835 {
3836 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3837 {
3838 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3839 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3840 }
3841 }
3842#endif
3843
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 ssl->state++;
3845
3846 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3847
3848 return( 0 );
3849}
3850
Paul Bakker41c83d32013-03-20 14:39:14 +01003851void ssl_optimize_checksum( ssl_context *ssl,
3852 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003853{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003854 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003855
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003856#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3857 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003858 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003859 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003860 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003861#endif
3862#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3863#if defined(POLARSSL_SHA512_C)
3864 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3865 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3866 else
3867#endif
3868#if defined(POLARSSL_SHA256_C)
3869 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003870 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003871 else
3872#endif
3873#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003874 {
3875 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003876 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003877 }
Paul Bakker380da532012-04-18 16:10:25 +00003878}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003879
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003880void ssl_reset_checksum( ssl_context *ssl )
3881{
3882#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3883 defined(POLARSSL_SSL_PROTO_TLS1_1)
3884 md5_starts( &ssl->handshake->fin_md5 );
3885 sha1_starts( &ssl->handshake->fin_sha1 );
3886#endif
3887#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3888#if defined(POLARSSL_SHA256_C)
3889 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3890#endif
3891#if defined(POLARSSL_SHA512_C)
3892 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3893#endif
3894#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3895}
3896
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003897static void ssl_update_checksum_start( ssl_context *ssl,
3898 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003899{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003900#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3901 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003902 md5_update( &ssl->handshake->fin_md5 , buf, len );
3903 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003904#endif
3905#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3906#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003907 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003908#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003909#if defined(POLARSSL_SHA512_C)
3910 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003911#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003912#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003913}
3914
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003915#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3916 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003917static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3918 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003919{
Paul Bakker48916f92012-09-16 19:57:18 +00003920 md5_update( &ssl->handshake->fin_md5 , buf, len );
3921 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003922}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003923#endif
Paul Bakker380da532012-04-18 16:10:25 +00003924
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3926#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003927static void ssl_update_checksum_sha256( ssl_context *ssl,
3928 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003929{
Paul Bakker9e36f042013-06-30 14:34:05 +02003930 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003931}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003932#endif
Paul Bakker380da532012-04-18 16:10:25 +00003933
Paul Bakker9e36f042013-06-30 14:34:05 +02003934#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003935static void ssl_update_checksum_sha384( ssl_context *ssl,
3936 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003937{
Paul Bakker9e36f042013-06-30 14:34:05 +02003938 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003939}
Paul Bakker769075d2012-11-24 11:26:46 +01003940#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003941#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003942
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003943#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003944static void ssl_calc_finished_ssl(
3945 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003946{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003947 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003948 md5_context md5;
3949 sha1_context sha1;
3950
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 unsigned char padbuf[48];
3952 unsigned char md5sum[16];
3953 unsigned char sha1sum[20];
3954
Paul Bakker48916f92012-09-16 19:57:18 +00003955 ssl_session *session = ssl->session_negotiate;
3956 if( !session )
3957 session = ssl->session;
3958
Paul Bakker1ef83d62012-04-11 12:09:53 +00003959 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3960
Paul Bakker48916f92012-09-16 19:57:18 +00003961 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3962 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
3964 /*
3965 * SSLv3:
3966 * hash =
3967 * MD5( master + pad2 +
3968 * MD5( handshake + sender + master + pad1 ) )
3969 * + SHA1( master + pad2 +
3970 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003971 */
3972
Paul Bakker90995b52013-06-24 19:20:35 +02003973#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003974 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003975 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003976#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003977
Paul Bakker90995b52013-06-24 19:20:35 +02003978#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003980 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003981#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003982
Paul Bakker3c2122f2013-06-24 19:03:14 +02003983 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3984 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003985
Paul Bakker1ef83d62012-04-11 12:09:53 +00003986 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987
Paul Bakker3c2122f2013-06-24 19:03:14 +02003988 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003989 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003990 md5_update( &md5, padbuf, 48 );
3991 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003992
Paul Bakker3c2122f2013-06-24 19:03:14 +02003993 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003994 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003995 sha1_update( &sha1, padbuf, 40 );
3996 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003997
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003999
Paul Bakker1ef83d62012-04-11 12:09:53 +00004000 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004001 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004002 md5_update( &md5, padbuf, 48 );
4003 md5_update( &md5, md5sum, 16 );
4004 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
Paul Bakker1ef83d62012-04-11 12:09:53 +00004006 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004007 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004008 sha1_update( &sha1, padbuf , 40 );
4009 sha1_update( &sha1, sha1sum, 20 );
4010 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
Paul Bakker1ef83d62012-04-11 12:09:53 +00004012 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Paul Bakker5b4af392014-06-26 12:09:34 +02004014 md5_free( &md5 );
4015 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016
Paul Bakker34617722014-06-13 17:20:13 +02004017 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4018 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4019 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004020
4021 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4022}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004023#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004024
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004025#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004026static void ssl_calc_finished_tls(
4027 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004028{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004029 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004030 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004031 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004032 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004033 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004034
Paul Bakker48916f92012-09-16 19:57:18 +00004035 ssl_session *session = ssl->session_negotiate;
4036 if( !session )
4037 session = ssl->session;
4038
Paul Bakker1ef83d62012-04-11 12:09:53 +00004039 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Paul Bakker48916f92012-09-16 19:57:18 +00004041 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4042 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004043
Paul Bakker1ef83d62012-04-11 12:09:53 +00004044 /*
4045 * TLSv1:
4046 * hash = PRF( master, finished_label,
4047 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4048 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004049
Paul Bakker90995b52013-06-24 19:20:35 +02004050#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004051 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4052 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004053#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004054
Paul Bakker90995b52013-06-24 19:20:35 +02004055#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004056 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4057 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004058#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004059
4060 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004061 ? "client finished"
4062 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004063
4064 md5_finish( &md5, padbuf );
4065 sha1_finish( &sha1, padbuf + 16 );
4066
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004067 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004068 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004069
4070 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4071
Paul Bakker5b4af392014-06-26 12:09:34 +02004072 md5_free( &md5 );
4073 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004074
Paul Bakker34617722014-06-13 17:20:13 +02004075 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004076
4077 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4078}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004079#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004080
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004081#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4082#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004083static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004084 ssl_context *ssl, unsigned char *buf, int from )
4085{
4086 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004087 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004088 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004089 unsigned char padbuf[32];
4090
Paul Bakker48916f92012-09-16 19:57:18 +00004091 ssl_session *session = ssl->session_negotiate;
4092 if( !session )
4093 session = ssl->session;
4094
Paul Bakker380da532012-04-18 16:10:25 +00004095 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004096
Paul Bakker9e36f042013-06-30 14:34:05 +02004097 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004098
4099 /*
4100 * TLSv1.2:
4101 * hash = PRF( master, finished_label,
4102 * Hash( handshake ) )[0.11]
4103 */
4104
Paul Bakker9e36f042013-06-30 14:34:05 +02004105#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004106 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004107 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004108#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004109
4110 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004111 ? "client finished"
4112 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004113
Paul Bakker9e36f042013-06-30 14:34:05 +02004114 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004115
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004116 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004117 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004118
4119 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4120
Paul Bakker5b4af392014-06-26 12:09:34 +02004121 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004122
Paul Bakker34617722014-06-13 17:20:13 +02004123 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004124
4125 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4126}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004127#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004128
Paul Bakker9e36f042013-06-30 14:34:05 +02004129#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004130static void ssl_calc_finished_tls_sha384(
4131 ssl_context *ssl, unsigned char *buf, int from )
4132{
4133 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004134 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004135 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004136 unsigned char padbuf[48];
4137
Paul Bakker48916f92012-09-16 19:57:18 +00004138 ssl_session *session = ssl->session_negotiate;
4139 if( !session )
4140 session = ssl->session;
4141
Paul Bakker380da532012-04-18 16:10:25 +00004142 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004143
Paul Bakker9e36f042013-06-30 14:34:05 +02004144 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004145
4146 /*
4147 * TLSv1.2:
4148 * hash = PRF( master, finished_label,
4149 * Hash( handshake ) )[0.11]
4150 */
4151
Paul Bakker9e36f042013-06-30 14:34:05 +02004152#if !defined(POLARSSL_SHA512_ALT)
4153 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4154 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004155#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004156
4157 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004158 ? "client finished"
4159 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004160
Paul Bakker9e36f042013-06-30 14:34:05 +02004161 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004162
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004163 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004164 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004165
4166 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4167
Paul Bakker5b4af392014-06-26 12:09:34 +02004168 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004169
Paul Bakker34617722014-06-13 17:20:13 +02004170 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004171
4172 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4173}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004174#endif /* POLARSSL_SHA512_C */
4175#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004176
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004177static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004178{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004179 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004180
4181 /*
4182 * Free our handshake params
4183 */
4184 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004185 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004186 ssl->handshake = NULL;
4187
4188 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004189 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004190 */
4191 if( ssl->transform )
4192 {
4193 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004194 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004195 }
4196 ssl->transform = ssl->transform_negotiate;
4197 ssl->transform_negotiate = NULL;
4198
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004199 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4200}
4201
4202void ssl_handshake_wrapup( ssl_context *ssl )
4203{
4204 int resume = ssl->handshake->resume;
4205
4206 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4207
4208 if( ssl->renegotiation == SSL_RENEGOTIATION )
4209 {
4210 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4211 ssl->renego_records_seen = 0;
4212 }
4213
4214 /*
4215 * Free the previous session and switch in the current one
4216 */
Paul Bakker0a597072012-09-25 21:55:46 +00004217 if( ssl->session )
4218 {
4219 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004220 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004221 }
4222 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004223 ssl->session_negotiate = NULL;
4224
Paul Bakker0a597072012-09-25 21:55:46 +00004225 /*
4226 * Add cache entry
4227 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004228 if( ssl->f_set_cache != NULL &&
4229 ssl->session->length != 0 &&
4230 resume == 0 )
4231 {
Paul Bakker0a597072012-09-25 21:55:46 +00004232 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4233 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004234 }
Paul Bakker0a597072012-09-25 21:55:46 +00004235
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004236#if defined(POLARSSL_SSL_PROTO_DTLS)
4237 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4238 ssl->handshake->flight != NULL )
4239 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004240 /* Cancel handshake timer */
4241 ssl_set_timer( ssl, 0 );
4242
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004243 /* Keep last flight around in case we need to resend it:
4244 * we need the handshake and transform structures for that */
4245 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4246 }
4247 else
4248#endif
4249 ssl_handshake_wrapup_free_hs_transform( ssl );
4250
Paul Bakker48916f92012-09-16 19:57:18 +00004251 ssl->state++;
4252
4253 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4254}
4255
Paul Bakker1ef83d62012-04-11 12:09:53 +00004256int ssl_write_finished( ssl_context *ssl )
4257{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004258 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004259
4260 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4261
Paul Bakker92be97b2013-01-02 17:30:03 +01004262 /*
4263 * Set the out_msg pointer to the correct location based on IV length
4264 */
4265 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4266 {
4267 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4268 ssl->transform_negotiate->fixed_ivlen;
4269 }
4270 else
4271 ssl->out_msg = ssl->out_iv;
4272
Paul Bakker48916f92012-09-16 19:57:18 +00004273 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004274
4275 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4277
Paul Bakker48916f92012-09-16 19:57:18 +00004278 ssl->verify_data_len = hash_len;
4279 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4280
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 ssl->out_msglen = 4 + hash_len;
4282 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4283 ssl->out_msg[0] = SSL_HS_FINISHED;
4284
4285 /*
4286 * In case of session resuming, invert the client and server
4287 * ChangeCipherSpec messages order.
4288 */
Paul Bakker0a597072012-09-25 21:55:46 +00004289 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004290 {
4291 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004292 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 else
4294 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4295 }
4296 else
4297 ssl->state++;
4298
Paul Bakker48916f92012-09-16 19:57:18 +00004299 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004300 * Switch to our negotiated transform and session parameters for outbound
4301 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004302 */
4303 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004304
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004305#if defined(POLARSSL_SSL_PROTO_DTLS)
4306 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4307 {
4308 unsigned char i;
4309
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004310 /* Remember current epoch settings for resending */
4311 ssl->handshake->alt_transform_out = ssl->transform_out;
4312 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4313
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004314 /* Set sequence_number to zero */
4315 memset( ssl->out_ctr + 2, 0, 6 );
4316
4317 /* Increment epoch */
4318 for( i = 2; i > 0; i-- )
4319 if( ++ssl->out_ctr[i - 1] != 0 )
4320 break;
4321
4322 /* The loop goes to its end iff the counter is wrapping */
4323 if( i == 0 )
4324 {
4325 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4326 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4327 }
4328 }
4329 else
4330#endif /* POLARSSL_SSL_PROTO_DTLS */
4331 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004332
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004333 ssl->transform_out = ssl->transform_negotiate;
4334 ssl->session_out = ssl->session_negotiate;
4335
Paul Bakker07eb38b2012-12-19 14:42:06 +01004336#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004337 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004338 {
4339 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4340 {
4341 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4342 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4343 }
4344 }
4345#endif
4346
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004347#if defined(POLARSSL_SSL_PROTO_DTLS)
4348 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4349 ssl_send_flight_completed( ssl );
4350#endif
4351
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4353 {
4354 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4355 return( ret );
4356 }
4357
4358 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4359
4360 return( 0 );
4361}
4362
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004363#if defined(POLARSSL_SSL_PROTO_SSL3)
4364#define SSL_MAX_HASH_LEN 36
4365#else
4366#define SSL_MAX_HASH_LEN 12
4367#endif
4368
Paul Bakker5121ce52009-01-03 21:22:43 +00004369int ssl_parse_finished( ssl_context *ssl )
4370{
Paul Bakker23986e52011-04-24 08:57:21 +00004371 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004372 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004373 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004374
4375 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4376
Paul Bakker48916f92012-09-16 19:57:18 +00004377 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004378
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4380 {
4381 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4382 return( ret );
4383 }
4384
4385 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4386 {
4387 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004388 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004389 }
4390
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004391 /* There is currently no ciphersuite using another length with TLS 1.2 */
4392#if defined(POLARSSL_SSL_PROTO_SSL3)
4393 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4394 hash_len = 36;
4395 else
4396#endif
4397 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004398
4399 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004400 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 {
4402 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004403 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004404 }
4405
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004406 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4407 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004408 {
4409 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004410 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 }
4412
Paul Bakker48916f92012-09-16 19:57:18 +00004413 ssl->verify_data_len = hash_len;
4414 memcpy( ssl->peer_verify_data, buf, hash_len );
4415
Paul Bakker0a597072012-09-25 21:55:46 +00004416 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 {
4418 if( ssl->endpoint == SSL_IS_CLIENT )
4419 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4420
4421 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004422 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 }
4424 else
4425 ssl->state++;
4426
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004427#if defined(POLARSSL_SSL_PROTO_DTLS)
4428 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4429 ssl_recv_flight_completed( ssl );
4430#endif
4431
Paul Bakker5121ce52009-01-03 21:22:43 +00004432 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4433
4434 return( 0 );
4435}
4436
Paul Bakker968afaa2014-07-09 11:09:24 +02004437static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004438{
4439 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4440
4441#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4442 defined(POLARSSL_SSL_PROTO_TLS1_1)
4443 md5_init( &handshake->fin_md5 );
4444 sha1_init( &handshake->fin_sha1 );
4445 md5_starts( &handshake->fin_md5 );
4446 sha1_starts( &handshake->fin_sha1 );
4447#endif
4448#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4449#if defined(POLARSSL_SHA256_C)
4450 sha256_init( &handshake->fin_sha256 );
4451 sha256_starts( &handshake->fin_sha256, 0 );
4452#endif
4453#if defined(POLARSSL_SHA512_C)
4454 sha512_init( &handshake->fin_sha512 );
4455 sha512_starts( &handshake->fin_sha512, 1 );
4456#endif
4457#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4458
4459 handshake->update_checksum = ssl_update_checksum_start;
4460 handshake->sig_alg = SSL_HASH_SHA1;
4461
4462#if defined(POLARSSL_DHM_C)
4463 dhm_init( &handshake->dhm_ctx );
4464#endif
4465#if defined(POLARSSL_ECDH_C)
4466 ecdh_init( &handshake->ecdh_ctx );
4467#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004468}
4469
4470static void ssl_transform_init( ssl_transform *transform )
4471{
4472 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004473
4474 cipher_init( &transform->cipher_ctx_enc );
4475 cipher_init( &transform->cipher_ctx_dec );
4476
4477 md_init( &transform->md_ctx_enc );
4478 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004479}
4480
4481void ssl_session_init( ssl_session *session )
4482{
4483 memset( session, 0, sizeof(ssl_session) );
4484}
4485
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004486static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004487{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004488 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004489 if( ssl->transform_negotiate )
4490 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004491 if( ssl->session_negotiate )
4492 ssl_session_free( ssl->session_negotiate );
4493 if( ssl->handshake )
4494 ssl_handshake_free( ssl->handshake );
4495
4496 /*
4497 * Either the pointers are now NULL or cleared properly and can be freed.
4498 * Now allocate missing structures.
4499 */
4500 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004501 {
4502 ssl->transform_negotiate =
4503 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4504 }
Paul Bakker48916f92012-09-16 19:57:18 +00004505
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004506 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004507 {
4508 ssl->session_negotiate =
4509 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4510 }
Paul Bakker48916f92012-09-16 19:57:18 +00004511
Paul Bakker82788fb2014-10-20 13:59:19 +02004512 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004513 {
4514 ssl->handshake = (ssl_handshake_params *)
4515 polarssl_malloc( sizeof(ssl_handshake_params) );
4516 }
Paul Bakker48916f92012-09-16 19:57:18 +00004517
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004518 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004519 if( ssl->handshake == NULL ||
4520 ssl->transform_negotiate == NULL ||
4521 ssl->session_negotiate == NULL )
4522 {
4523 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004524
4525 polarssl_free( ssl->handshake );
4526 polarssl_free( ssl->transform_negotiate );
4527 polarssl_free( ssl->session_negotiate );
4528
4529 ssl->handshake = NULL;
4530 ssl->transform_negotiate = NULL;
4531 ssl->session_negotiate = NULL;
4532
Paul Bakker48916f92012-09-16 19:57:18 +00004533 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4534 }
4535
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004536 /* Initialize structures */
4537 ssl_session_init( ssl->session_negotiate );
4538 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004539 ssl_handshake_params_init( ssl->handshake );
4540
4541#if defined(POLARSSL_X509_CRT_PARSE_C)
4542 ssl->handshake->key_cert = ssl->key_cert;
4543#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004544
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004545 /*
4546 * We may not know yet if we're using DTLS,
4547 * so always initiliase DTLS-specific fields.
4548 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004549#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004550 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004551
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004552 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004553 if( ssl->endpoint == SSL_IS_CLIENT )
4554 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4555 else
4556 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004557#endif
4558
Paul Bakker48916f92012-09-16 19:57:18 +00004559 return( 0 );
4560}
4561
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004562#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4563/* Dummy cookie callbacks for defaults */
4564static int ssl_cookie_write_dummy( void *ctx,
4565 unsigned char **p, unsigned char *end,
4566 const unsigned char *cli_id, size_t cli_id_len )
4567{
4568 ((void) ctx);
4569 ((void) p);
4570 ((void) end);
4571 ((void) cli_id);
4572 ((void) cli_id_len);
4573
4574 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4575}
4576
4577static int ssl_cookie_check_dummy( void *ctx,
4578 const unsigned char *cookie, size_t cookie_len,
4579 const unsigned char *cli_id, size_t cli_id_len )
4580{
4581 ((void) ctx);
4582 ((void) cookie);
4583 ((void) cookie_len);
4584 ((void) cli_id);
4585 ((void) cli_id_len);
4586
4587 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4588}
4589#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4590
Paul Bakker5121ce52009-01-03 21:22:43 +00004591/*
4592 * Initialize an SSL context
4593 */
4594int ssl_init( ssl_context *ssl )
4595{
Paul Bakker48916f92012-09-16 19:57:18 +00004596 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004597 int len = SSL_BUFFER_LEN;
4598
4599 memset( ssl, 0, sizeof( ssl_context ) );
4600
Paul Bakker62f2dee2012-09-28 07:31:51 +00004601 /*
4602 * Sane defaults
4603 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004604 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4605 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4606 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4607 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004608
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004609 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004610
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004611 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4612
Paul Bakker62f2dee2012-09-28 07:31:51 +00004613#if defined(POLARSSL_DHM_C)
4614 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4615 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4616 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4617 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4618 {
4619 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4620 return( ret );
4621 }
4622#endif
4623
4624 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004625 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004626 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004627 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004628 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004629
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004630 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004631 {
4632 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004633 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004634 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004635 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004636 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004637 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004638 }
4639
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004640 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4641 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004642
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004643 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4644 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4645
Paul Bakker606b4ba2013-08-14 16:52:14 +02004646#if defined(POLARSSL_SSL_SESSION_TICKETS)
4647 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4648#endif
4649
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004650#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004651 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004652#endif
4653
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004654#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4655 ssl->f_cookie_write = ssl_cookie_write_dummy;
4656 ssl->f_cookie_check = ssl_cookie_check_dummy;
4657#endif
4658
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004659#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4660 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4661#endif
4662
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004663#if defined(POLARSSL_SSL_PROTO_DTLS)
4664 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4665 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4666#endif
4667
Paul Bakker48916f92012-09-16 19:57:18 +00004668 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4669 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004670
4671 return( 0 );
4672}
4673
4674/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004675 * Reset an initialized and used SSL context for re-use while retaining
4676 * all application-set variables, function pointers and data.
4677 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004678int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004679{
Paul Bakker48916f92012-09-16 19:57:18 +00004680 int ret;
4681
Paul Bakker7eb013f2011-10-06 12:37:39 +00004682 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004683 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4684 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4685
4686 ssl->verify_data_len = 0;
4687 memset( ssl->own_verify_data, 0, 36 );
4688 memset( ssl->peer_verify_data, 0, 36 );
4689
Paul Bakker7eb013f2011-10-06 12:37:39 +00004690 ssl->in_offt = NULL;
4691
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004692 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004693 ssl->in_msgtype = 0;
4694 ssl->in_msglen = 0;
4695 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004696#if defined(POLARSSL_SSL_PROTO_DTLS)
4697 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004698 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004699#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004700#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4701 ssl_dtls_replay_reset( ssl );
4702#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004703
4704 ssl->in_hslen = 0;
4705 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004706 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004707
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004708 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004709 ssl->out_msgtype = 0;
4710 ssl->out_msglen = 0;
4711 ssl->out_left = 0;
4712
Paul Bakker48916f92012-09-16 19:57:18 +00004713 ssl->transform_in = NULL;
4714 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004715
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004716 ssl->renego_records_seen = 0;
4717
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004718 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4719 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004720
4721#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004722 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004723 {
4724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004725 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004726 {
4727 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4728 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4729 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004730 }
4731#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004732
Paul Bakker48916f92012-09-16 19:57:18 +00004733 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004734 {
Paul Bakker48916f92012-09-16 19:57:18 +00004735 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004736 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004737 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004738 }
Paul Bakker48916f92012-09-16 19:57:18 +00004739
Paul Bakkerc0463502013-02-14 11:19:38 +01004740 if( ssl->session )
4741 {
4742 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004743 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004744 ssl->session = NULL;
4745 }
4746
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004747#if defined(POLARSSL_SSL_ALPN)
4748 ssl->alpn_chosen = NULL;
4749#endif
4750
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004751#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004752 polarssl_free( ssl->cli_id );
4753 ssl->cli_id = NULL;
4754 ssl->cli_id_len = 0;
4755#endif
4756
Paul Bakker48916f92012-09-16 19:57:18 +00004757 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4758 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004759
4760 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004761}
4762
Paul Bakkera503a632013-08-14 13:48:06 +02004763#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004764static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4765{
4766 aes_free( &tkeys->enc );
4767 aes_free( &tkeys->dec );
4768
4769 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4770}
4771
Paul Bakker7eb013f2011-10-06 12:37:39 +00004772/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004773 * Allocate and initialize ticket keys
4774 */
4775static int ssl_ticket_keys_init( ssl_context *ssl )
4776{
4777 int ret;
4778 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004779 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004780
4781 if( ssl->ticket_keys != NULL )
4782 return( 0 );
4783
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004784 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4785 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004786 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4787
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004788 aes_init( &tkeys->enc );
4789 aes_init( &tkeys->dec );
4790
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004791 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004792 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004793 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004794 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004795 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004796 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004797
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004798 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4799 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4800 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4801 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004802 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004803 polarssl_free( tkeys );
4804 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004805 }
4806
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004807 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004808 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004809 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004810 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004811 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004812 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004813
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004814 ssl->ticket_keys = tkeys;
4815
4816 return( 0 );
4817}
Paul Bakkera503a632013-08-14 13:48:06 +02004818#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004819
4820/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 * SSL set accessors
4822 */
4823void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4824{
4825 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004826
Paul Bakker606b4ba2013-08-14 16:52:14 +02004827#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004828 if( endpoint == SSL_IS_CLIENT )
4829 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004830#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004831}
4832
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004833int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004834{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004835#if defined(POLARSSL_SSL_PROTO_DTLS)
4836 if( transport == SSL_TRANSPORT_DATAGRAM )
4837 {
4838 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004839
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004840 ssl->out_hdr = ssl->out_buf;
4841 ssl->out_ctr = ssl->out_buf + 3;
4842 ssl->out_len = ssl->out_buf + 11;
4843 ssl->out_iv = ssl->out_buf + 13;
4844 ssl->out_msg = ssl->out_buf + 13;
4845
4846 ssl->in_hdr = ssl->in_buf;
4847 ssl->in_ctr = ssl->in_buf + 3;
4848 ssl->in_len = ssl->in_buf + 11;
4849 ssl->in_iv = ssl->in_buf + 13;
4850 ssl->in_msg = ssl->in_buf + 13;
4851
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004852 /* DTLS starts with TLS1.1 */
4853 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4854 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004855
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004856 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4857 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4858
4859 return( 0 );
4860 }
4861#endif
4862
4863 if( transport == SSL_TRANSPORT_STREAM )
4864 {
4865 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004866
4867 ssl->out_ctr = ssl->out_buf;
4868 ssl->out_hdr = ssl->out_buf + 8;
4869 ssl->out_len = ssl->out_buf + 11;
4870 ssl->out_iv = ssl->out_buf + 13;
4871 ssl->out_msg = ssl->out_buf + 13;
4872
4873 ssl->in_ctr = ssl->in_buf;
4874 ssl->in_hdr = ssl->in_buf + 8;
4875 ssl->in_len = ssl->in_buf + 11;
4876 ssl->in_iv = ssl->in_buf + 13;
4877 ssl->in_msg = ssl->in_buf + 13;
4878
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004879 return( 0 );
4880 }
4881
4882 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004883}
4884
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004885#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4886void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4887{
4888 ssl->anti_replay = mode;
4889}
4890#endif
4891
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004892#if defined(POLARSSL_SSL_PROTO_DTLS)
4893void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4894{
4895 ssl->hs_timeout_min = min;
4896 ssl->hs_timeout_max = max;
4897}
4898#endif
4899
Paul Bakker5121ce52009-01-03 21:22:43 +00004900void ssl_set_authmode( ssl_context *ssl, int authmode )
4901{
4902 ssl->authmode = authmode;
4903}
4904
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004905#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004906void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004907 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004908 void *p_vrfy )
4909{
4910 ssl->f_vrfy = f_vrfy;
4911 ssl->p_vrfy = p_vrfy;
4912}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004913#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004914
Paul Bakker5121ce52009-01-03 21:22:43 +00004915void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004916 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004917 void *p_rng )
4918{
4919 ssl->f_rng = f_rng;
4920 ssl->p_rng = p_rng;
4921}
4922
4923void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004924 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 void *p_dbg )
4926{
4927 ssl->f_dbg = f_dbg;
4928 ssl->p_dbg = p_dbg;
4929}
4930
4931void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004932 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004933 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004934{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004935 if( p_recv != p_send )
4936 {
4937 ssl->f_recv = NULL;
4938 ssl->f_send = NULL;
4939 ssl->p_bio = NULL;
4940 return;
4941 }
4942
Paul Bakker5121ce52009-01-03 21:22:43 +00004943 ssl->f_recv = f_recv;
4944 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004945 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004946}
4947
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004948void ssl_set_bio_timeout( ssl_context *ssl,
4949 void *p_bio,
4950 int (*f_send)(void *, const unsigned char *, size_t),
4951 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004952 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4953 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004954{
4955 ssl->p_bio = p_bio;
4956 ssl->f_send = f_send;
4957 ssl->f_recv = f_recv;
4958 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004959 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004960}
4961
Paul Bakker0a597072012-09-25 21:55:46 +00004962void ssl_set_session_cache( ssl_context *ssl,
4963 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4964 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004965{
Paul Bakker0a597072012-09-25 21:55:46 +00004966 ssl->f_get_cache = f_get_cache;
4967 ssl->p_get_cache = p_get_cache;
4968 ssl->f_set_cache = f_set_cache;
4969 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004970}
4971
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004972int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004973{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004974 int ret;
4975
4976 if( ssl == NULL ||
4977 session == NULL ||
4978 ssl->session_negotiate == NULL ||
4979 ssl->endpoint != SSL_IS_CLIENT )
4980 {
4981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4982 }
4983
4984 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4985 return( ret );
4986
Paul Bakker0a597072012-09-25 21:55:46 +00004987 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004988
4989 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004990}
4991
Paul Bakkerb68cad62012-08-23 08:34:18 +00004992void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004993{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004994 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4995 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4996 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4997 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4998}
4999
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005000void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5001 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005002 int major, int minor )
5003{
5004 if( major != SSL_MAJOR_VERSION_3 )
5005 return;
5006
5007 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5008 return;
5009
5010 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005011}
5012
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005013#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005014/* Add a new (empty) key_cert entry an return a pointer to it */
5015static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5016{
5017 ssl_key_cert *key_cert, *last;
5018
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005019 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5020 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005021 return( NULL );
5022
5023 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5024
5025 /* Append the new key_cert to the (possibly empty) current list */
5026 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005027 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005028 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005029 if( ssl->handshake != NULL )
5030 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005031 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005032 else
5033 {
5034 last = ssl->key_cert;
5035 while( last->next != NULL )
5036 last = last->next;
5037 last->next = key_cert;
5038 }
5039
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005040 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005041}
5042
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005043void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005044 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005045{
5046 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005047 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 ssl->peer_cn = peer_cn;
5049}
5050
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005051int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005052 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005053{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005054 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5055
5056 if( key_cert == NULL )
5057 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5058
5059 key_cert->cert = own_cert;
5060 key_cert->key = pk_key;
5061
5062 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005063}
5064
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005065#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005066int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005067 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005068{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005069 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005070 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005071
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005072 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005073 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5074
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005075 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5076 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005077 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005078
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005079 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005080
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005081 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005082 if( ret != 0 )
5083 return( ret );
5084
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005085 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005086 return( ret );
5087
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005088 key_cert->cert = own_cert;
5089 key_cert->key_own_alloc = 1;
5090
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005091 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005092}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005093#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005094
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005095int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005096 void *rsa_key,
5097 rsa_decrypt_func rsa_decrypt,
5098 rsa_sign_func rsa_sign,
5099 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005100{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005101 int ret;
5102 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005103
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005104 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005105 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5106
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005107 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5108 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005109 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005110
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005111 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005112
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005113 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5114 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5115 return( ret );
5116
5117 key_cert->cert = own_cert;
5118 key_cert->key_own_alloc = 1;
5119
5120 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005121}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005122#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005123
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005124#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005125int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5126 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005127{
Paul Bakker6db455e2013-09-18 17:29:31 +02005128 if( psk == NULL || psk_identity == NULL )
5129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5130
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005131 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005132 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5133
Paul Bakker6db455e2013-09-18 17:29:31 +02005134 if( ssl->psk != NULL )
5135 {
5136 polarssl_free( ssl->psk );
5137 polarssl_free( ssl->psk_identity );
5138 }
5139
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005140 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005141 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005142
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005143 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005144 ssl->psk_identity = (unsigned char *)
5145 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005146
5147 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5149
5150 memcpy( ssl->psk, psk, ssl->psk_len );
5151 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005152
5153 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005154}
5155
5156void ssl_set_psk_cb( ssl_context *ssl,
5157 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5158 size_t),
5159 void *p_psk )
5160{
5161 ssl->f_psk = f_psk;
5162 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005163}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005164#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005165
Paul Bakker48916f92012-09-16 19:57:18 +00005166#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005167int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005168{
5169 int ret;
5170
Paul Bakker48916f92012-09-16 19:57:18 +00005171 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005172 {
5173 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5174 return( ret );
5175 }
5176
Paul Bakker48916f92012-09-16 19:57:18 +00005177 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 {
5179 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5180 return( ret );
5181 }
5182
5183 return( 0 );
5184}
5185
Paul Bakker1b57b062011-01-06 15:48:19 +00005186int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5187{
5188 int ret;
5189
Paul Bakker66d5d072014-06-17 16:39:18 +02005190 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005191 {
5192 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5193 return( ret );
5194 }
5195
Paul Bakker66d5d072014-06-17 16:39:18 +02005196 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005197 {
5198 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5199 return( ret );
5200 }
5201
5202 return( 0 );
5203}
Paul Bakker48916f92012-09-16 19:57:18 +00005204#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005205
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005206#if defined(POLARSSL_SSL_SET_CURVES)
5207/*
5208 * Set the allowed elliptic curves
5209 */
5210void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5211{
5212 ssl->curve_list = curve_list;
5213}
5214#endif
5215
Paul Bakker0be444a2013-08-27 21:55:01 +02005216#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005217int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005218{
5219 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005220 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005221
5222 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005223
5224 if( ssl->hostname_len + 1 == 0 )
5225 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5226
Paul Bakker6e339b52013-07-03 13:37:05 +02005227 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005228
Paul Bakkerb15b8512012-01-13 13:44:06 +00005229 if( ssl->hostname == NULL )
5230 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5231
Paul Bakker3c2122f2013-06-24 19:03:14 +02005232 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005233 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005234
Paul Bakker40ea7de2009-05-03 10:18:48 +00005235 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
5237 return( 0 );
5238}
5239
Paul Bakker5701cdc2012-09-27 21:49:42 +00005240void ssl_set_sni( ssl_context *ssl,
5241 int (*f_sni)(void *, ssl_context *,
5242 const unsigned char *, size_t),
5243 void *p_sni )
5244{
5245 ssl->f_sni = f_sni;
5246 ssl->p_sni = p_sni;
5247}
Paul Bakker0be444a2013-08-27 21:55:01 +02005248#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005249
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005250#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005251int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005252{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005253 size_t cur_len, tot_len;
5254 const char **p;
5255
5256 /*
5257 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5258 * truncated". Check lengths now rather than later.
5259 */
5260 tot_len = 0;
5261 for( p = protos; *p != NULL; p++ )
5262 {
5263 cur_len = strlen( *p );
5264 tot_len += cur_len;
5265
5266 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5267 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5268 }
5269
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005270 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005271
5272 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005273}
5274
5275const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5276{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005277 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005278}
5279#endif /* POLARSSL_SSL_ALPN */
5280
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005281static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005282{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005283 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5284 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5285 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5286 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005287 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005288 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005289 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005290
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005291 return( 0 );
5292}
5293
5294int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5295{
5296 if( ssl_check_version( ssl, major, minor ) != 0 )
5297 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5298
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005299 ssl->max_major_ver = major;
5300 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005301
5302 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005303}
5304
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005305int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005306{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005307 if( ssl_check_version( ssl, major, minor ) != 0 )
5308 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005309
5310 ssl->min_major_ver = major;
5311 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005312
5313 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005314}
5315
Paul Bakker05decb22013-08-15 13:33:48 +02005316#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005317int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5318{
Paul Bakker77e257e2013-12-16 15:29:52 +01005319 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005320 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005321 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005322 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005323 }
5324
5325 ssl->mfl_code = mfl_code;
5326
5327 return( 0 );
5328}
Paul Bakker05decb22013-08-15 13:33:48 +02005329#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005330
Paul Bakker1f2bc622013-08-15 13:45:55 +02005331#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005332int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005333{
5334 if( ssl->endpoint != SSL_IS_CLIENT )
5335 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5336
Paul Bakker8c1ede62013-07-19 14:14:37 +02005337 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005338
5339 return( 0 );
5340}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005341#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005342
Paul Bakker48916f92012-09-16 19:57:18 +00005343void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5344{
5345 ssl->disable_renegotiation = renegotiation;
5346}
5347
5348void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5349{
5350 ssl->allow_legacy_renegotiation = allow_legacy;
5351}
5352
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005353void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5354{
5355 ssl->renego_max_records = max_records;
5356}
5357
Paul Bakkera503a632013-08-14 13:48:06 +02005358#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005359int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5360{
5361 ssl->session_tickets = use_tickets;
5362
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005363 if( ssl->endpoint == SSL_IS_CLIENT )
5364 return( 0 );
5365
5366 if( ssl->f_rng == NULL )
5367 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5368
5369 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005370}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005371
5372void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5373{
5374 ssl->ticket_lifetime = lifetime;
5375}
Paul Bakkera503a632013-08-14 13:48:06 +02005376#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005377
Paul Bakker5121ce52009-01-03 21:22:43 +00005378/*
5379 * SSL get accessors
5380 */
Paul Bakker23986e52011-04-24 08:57:21 +00005381size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005382{
5383 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5384}
5385
Paul Bakkerff60ee62010-03-16 21:09:09 +00005386int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005387{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005388 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005389}
5390
Paul Bakkere3166ce2011-01-27 17:40:50 +00005391const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005392{
Paul Bakker926c8e42013-03-06 10:23:34 +01005393 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005394 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005395
Paul Bakkere3166ce2011-01-27 17:40:50 +00005396 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005397}
5398
Paul Bakker43ca69c2011-01-15 17:35:19 +00005399const char *ssl_get_version( const ssl_context *ssl )
5400{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005401#if defined(POLARSSL_SSL_PROTO_DTLS)
5402 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5403 {
5404 switch( ssl->minor_ver )
5405 {
5406 case SSL_MINOR_VERSION_2:
5407 return( "DTLSv1.0" );
5408
5409 case SSL_MINOR_VERSION_3:
5410 return( "DTLSv1.2" );
5411
5412 default:
5413 return( "unknown (DTLS)" );
5414 }
5415 }
5416#endif
5417
Paul Bakker43ca69c2011-01-15 17:35:19 +00005418 switch( ssl->minor_ver )
5419 {
5420 case SSL_MINOR_VERSION_0:
5421 return( "SSLv3.0" );
5422
5423 case SSL_MINOR_VERSION_1:
5424 return( "TLSv1.0" );
5425
5426 case SSL_MINOR_VERSION_2:
5427 return( "TLSv1.1" );
5428
Paul Bakker1ef83d62012-04-11 12:09:53 +00005429 case SSL_MINOR_VERSION_3:
5430 return( "TLSv1.2" );
5431
Paul Bakker43ca69c2011-01-15 17:35:19 +00005432 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005433 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005434 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005435}
5436
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005437#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005438const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005439{
5440 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005441 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005442
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005443 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005444}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005445#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005446
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005447int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5448{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005449 if( ssl == NULL ||
5450 dst == NULL ||
5451 ssl->session == NULL ||
5452 ssl->endpoint != SSL_IS_CLIENT )
5453 {
5454 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5455 }
5456
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005457 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005458}
5459
Paul Bakker5121ce52009-01-03 21:22:43 +00005460/*
Paul Bakker1961b702013-01-25 14:49:24 +01005461 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005462 */
Paul Bakker1961b702013-01-25 14:49:24 +01005463int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005464{
Paul Bakker40e46942009-01-03 21:51:57 +00005465 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005466
Paul Bakker40e46942009-01-03 21:51:57 +00005467#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005468 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005469 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005470#endif
5471
Paul Bakker40e46942009-01-03 21:51:57 +00005472#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005473 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005474 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005475#endif
5476
Paul Bakker1961b702013-01-25 14:49:24 +01005477 return( ret );
5478}
5479
5480/*
5481 * Perform the SSL handshake
5482 */
5483int ssl_handshake( ssl_context *ssl )
5484{
5485 int ret = 0;
5486
5487 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5488
5489 while( ssl->state != SSL_HANDSHAKE_OVER )
5490 {
5491 ret = ssl_handshake_step( ssl );
5492
5493 if( ret != 0 )
5494 break;
5495 }
5496
Paul Bakker5121ce52009-01-03 21:22:43 +00005497 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5498
5499 return( ret );
5500}
5501
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005502#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005503/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005504 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005505 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005506static int ssl_write_hello_request( ssl_context *ssl )
5507{
5508 int ret;
5509
5510 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5511
5512 ssl->out_msglen = 4;
5513 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5514 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5515
5516 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5517 {
5518 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5519 return( ret );
5520 }
5521
5522 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5523
5524 return( 0 );
5525}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005526#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005527
5528/*
5529 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005530 * - any side: calling ssl_renegotiate(),
5531 * - client: receiving a HelloRequest during ssl_read(),
5532 * - server: receiving any handshake message on server during ssl_read() after
5533 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005534 * If the handshake doesn't complete due to waiting for I/O, it will continue
5535 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005536 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005537static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005538{
5539 int ret;
5540
5541 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5542
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005543 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5544 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005545
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005546 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5547 * the ServerHello will have message_seq = 1" */
5548#if defined(POLARSSL_SSL_PROTO_DTLS)
5549 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005550 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5551 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005552 if( ssl->endpoint == SSL_IS_SERVER )
5553 ssl->handshake->out_msg_seq = 1;
5554 else
5555 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005556 }
5557#endif
5558
Paul Bakker48916f92012-09-16 19:57:18 +00005559 ssl->state = SSL_HELLO_REQUEST;
5560 ssl->renegotiation = SSL_RENEGOTIATION;
5561
Paul Bakker48916f92012-09-16 19:57:18 +00005562 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5563 {
5564 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5565 return( ret );
5566 }
5567
5568 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5569
5570 return( 0 );
5571}
5572
5573/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005574 * Renegotiate current connection on client,
5575 * or request renegotiation on server
5576 */
5577int ssl_renegotiate( ssl_context *ssl )
5578{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005579 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005580
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005581#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005582 /* On server, just send the request */
5583 if( ssl->endpoint == SSL_IS_SERVER )
5584 {
5585 if( ssl->state != SSL_HANDSHAKE_OVER )
5586 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5587
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005588 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5589
5590 /* Did we already try/start sending HelloRequest? */
5591 if( ssl->out_left != 0 )
5592 return( ssl_flush_output( ssl ) );
5593
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005594 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005595 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005596#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005597
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005598#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005599 /*
5600 * On client, either start the renegotiation process or,
5601 * if already in progress, continue the handshake
5602 */
5603 if( ssl->renegotiation != SSL_RENEGOTIATION )
5604 {
5605 if( ssl->state != SSL_HANDSHAKE_OVER )
5606 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5607
5608 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5609 {
5610 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5611 return( ret );
5612 }
5613 }
5614 else
5615 {
5616 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5617 {
5618 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5619 return( ret );
5620 }
5621 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005622#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005623
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005624 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005625}
5626
5627/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005628 * Receive application data decrypted from the SSL layer
5629 */
Paul Bakker23986e52011-04-24 08:57:21 +00005630int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005631{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005632 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005633 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005634
5635 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5636
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005637#if defined(POLARSSL_SSL_PROTO_DTLS)
5638 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5639 ssl->handshake != NULL &&
5640 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5641 {
5642 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5643 return( ret );
5644
5645 if( ( ret = ssl_resend( ssl ) ) != 0 )
5646 return( ret );
5647 }
5648#endif
5649
Paul Bakker5121ce52009-01-03 21:22:43 +00005650 if( ssl->state != SSL_HANDSHAKE_OVER )
5651 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005652 ret = ssl_handshake( ssl );
5653 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5654 {
5655 record_read = 1;
5656 }
5657 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005658 {
5659 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5660 return( ret );
5661 }
5662 }
5663
5664 if( ssl->in_offt == NULL )
5665 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005666 /* Start timer if not already running */
5667 if( ssl->time_limit == 0 )
5668 ssl_set_timer( ssl, ssl->read_timeout );
5669
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005670 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005671 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005672 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5673 {
5674 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5675 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005676
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005677 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5678 return( ret );
5679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005680 }
5681
5682 if( ssl->in_msglen == 0 &&
5683 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5684 {
5685 /*
5686 * OpenSSL sends empty messages to randomize the IV
5687 */
5688 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5689 {
Paul Bakker831a7552011-05-18 13:32:51 +00005690 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5691 return( 0 );
5692
Paul Bakker5121ce52009-01-03 21:22:43 +00005693 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5694 return( ret );
5695 }
5696 }
5697
Paul Bakker48916f92012-09-16 19:57:18 +00005698 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5699 {
5700 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5701
5702 if( ssl->endpoint == SSL_IS_CLIENT &&
5703 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005704 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005705 {
5706 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005707
5708 /* With DTLS, drop the packet (probably from last handshake) */
5709#if defined(POLARSSL_SSL_PROTO_DTLS)
5710 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5711 return( POLARSSL_ERR_NET_WANT_READ );
5712#endif
5713 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5714 }
5715
5716 if( ssl->endpoint == SSL_IS_SERVER &&
5717 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5718 {
5719 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5720
5721 /* With DTLS, drop the packet (probably from last handshake) */
5722#if defined(POLARSSL_SSL_PROTO_DTLS)
5723 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5724 return( POLARSSL_ERR_NET_WANT_READ );
5725#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005726 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5727 }
5728
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005729 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5730 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005731 ssl->allow_legacy_renegotiation ==
5732 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005733 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005734 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005735
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005736#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005737 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005738 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005739 /*
5740 * SSLv3 does not have a "no_renegotiation" alert
5741 */
5742 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5743 return( ret );
5744 }
5745 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005746#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005747#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5748 defined(POLARSSL_SSL_PROTO_TLS1_2)
5749 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005750 {
5751 if( ( ret = ssl_send_alert_message( ssl,
5752 SSL_ALERT_LEVEL_WARNING,
5753 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5754 {
5755 return( ret );
5756 }
Paul Bakker48916f92012-09-16 19:57:18 +00005757 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005758 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005759#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5760 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005761 {
5762 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005763 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005764 }
Paul Bakker48916f92012-09-16 19:57:18 +00005765 }
5766 else
5767 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005768#if defined(POLARSSL_SSL_PROTO_DTLS)
5769 /* DTLS clients need to know renego is server-initiated */
5770 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5771 ssl->endpoint == SSL_IS_CLIENT )
5772 {
5773 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5774 }
5775#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005776 ret = ssl_start_renegotiation( ssl );
5777 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5778 {
5779 record_read = 1;
5780 }
5781 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005782 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005783 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005784 return( ret );
5785 }
Paul Bakker48916f92012-09-16 19:57:18 +00005786 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005787
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005788 /* If a non-handshake record was read during renego, fallthrough,
5789 * else tell the user they should call ssl_read() again */
5790 if( ! record_read )
5791 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005792 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005793 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5794 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005795 ssl->renego_records_seen++;
5796
5797 if( ssl->renego_max_records >= 0 &&
5798 ssl->renego_records_seen > ssl->renego_max_records )
5799 {
5800 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5801 "but not honored by client" ) );
5802 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5803 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005804 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005805
5806 /* Fatal and closure alerts handled by ssl_read_record() */
5807 if( ssl->in_msgtype == SSL_MSG_ALERT )
5808 {
5809 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5810 return( POLARSSL_ERR_NET_WANT_READ );
5811 }
5812
5813 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005814 {
5815 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005816 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005817 }
5818
5819 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005820
5821 /* We're going to return something now, cancel timer */
5822 ssl_set_timer( ssl, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005823 }
5824
5825 n = ( len < ssl->in_msglen )
5826 ? len : ssl->in_msglen;
5827
5828 memcpy( buf, ssl->in_offt, n );
5829 ssl->in_msglen -= n;
5830
5831 if( ssl->in_msglen == 0 )
5832 /* all bytes consumed */
5833 ssl->in_offt = NULL;
5834 else
5835 /* more data available */
5836 ssl->in_offt += n;
5837
5838 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5839
Paul Bakker23986e52011-04-24 08:57:21 +00005840 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005841}
5842
5843/*
5844 * Send application data to be encrypted by the SSL layer
5845 */
Paul Bakker23986e52011-04-24 08:57:21 +00005846int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005847{
Paul Bakker23986e52011-04-24 08:57:21 +00005848 int ret;
5849 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005850 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005851
5852 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5853
5854 if( ssl->state != SSL_HANDSHAKE_OVER )
5855 {
5856 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5857 {
5858 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5859 return( ret );
5860 }
5861 }
5862
Paul Bakker05decb22013-08-15 13:33:48 +02005863#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005864 /*
5865 * Assume mfl_code is correct since it was checked when set
5866 */
5867 max_len = mfl_code_to_length[ssl->mfl_code];
5868
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005869 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005870 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005871 */
5872 if( ssl->session_out != NULL &&
5873 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5874 {
5875 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5876 }
Paul Bakker05decb22013-08-15 13:33:48 +02005877#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005878
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005879 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005880
Paul Bakker5121ce52009-01-03 21:22:43 +00005881 if( ssl->out_left != 0 )
5882 {
5883 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5884 {
5885 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5886 return( ret );
5887 }
5888 }
Paul Bakker887bd502011-06-08 13:10:54 +00005889 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005890 {
Paul Bakker887bd502011-06-08 13:10:54 +00005891 ssl->out_msglen = n;
5892 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5893 memcpy( ssl->out_msg, buf, n );
5894
5895 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5896 {
5897 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5898 return( ret );
5899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005900 }
5901
5902 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5903
Paul Bakker23986e52011-04-24 08:57:21 +00005904 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005905}
5906
5907/*
5908 * Notify the peer that the connection is being closed
5909 */
5910int ssl_close_notify( ssl_context *ssl )
5911{
5912 int ret;
5913
5914 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5915
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005916 if( ssl->out_left != 0 )
5917 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005918
5919 if( ssl->state == SSL_HANDSHAKE_OVER )
5920 {
Paul Bakker48916f92012-09-16 19:57:18 +00005921 if( ( ret = ssl_send_alert_message( ssl,
5922 SSL_ALERT_LEVEL_WARNING,
5923 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005924 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005925 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926 return( ret );
5927 }
5928 }
5929
5930 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5931
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005932 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005933}
5934
Paul Bakker48916f92012-09-16 19:57:18 +00005935void ssl_transform_free( ssl_transform *transform )
5936{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005937 if( transform == NULL )
5938 return;
5939
Paul Bakker48916f92012-09-16 19:57:18 +00005940#if defined(POLARSSL_ZLIB_SUPPORT)
5941 deflateEnd( &transform->ctx_deflate );
5942 inflateEnd( &transform->ctx_inflate );
5943#endif
5944
Paul Bakker84bbeb52014-07-01 14:53:22 +02005945 cipher_free( &transform->cipher_ctx_enc );
5946 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005947
Paul Bakker84bbeb52014-07-01 14:53:22 +02005948 md_free( &transform->md_ctx_enc );
5949 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005950
Paul Bakker34617722014-06-13 17:20:13 +02005951 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005952}
5953
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005954#if defined(POLARSSL_X509_CRT_PARSE_C)
5955static void ssl_key_cert_free( ssl_key_cert *key_cert )
5956{
5957 ssl_key_cert *cur = key_cert, *next;
5958
5959 while( cur != NULL )
5960 {
5961 next = cur->next;
5962
5963 if( cur->key_own_alloc )
5964 {
5965 pk_free( cur->key );
5966 polarssl_free( cur->key );
5967 }
5968 polarssl_free( cur );
5969
5970 cur = next;
5971 }
5972}
5973#endif /* POLARSSL_X509_CRT_PARSE_C */
5974
Paul Bakker48916f92012-09-16 19:57:18 +00005975void ssl_handshake_free( ssl_handshake_params *handshake )
5976{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005977 if( handshake == NULL )
5978 return;
5979
Paul Bakker48916f92012-09-16 19:57:18 +00005980#if defined(POLARSSL_DHM_C)
5981 dhm_free( &handshake->dhm_ctx );
5982#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005983#if defined(POLARSSL_ECDH_C)
5984 ecdh_free( &handshake->ecdh_ctx );
5985#endif
5986
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005987#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005988 /* explicit void pointer cast for buggy MS compiler */
5989 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005990#endif
5991
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005992#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5993 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5994 /*
5995 * Free only the linked list wrapper, not the keys themselves
5996 * since the belong to the SNI callback
5997 */
5998 if( handshake->sni_key_cert != NULL )
5999 {
6000 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6001
6002 while( cur != NULL )
6003 {
6004 next = cur->next;
6005 polarssl_free( cur );
6006 cur = next;
6007 }
6008 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006009#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006010
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006011#if defined(POLARSSL_SSL_PROTO_DTLS)
6012 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006013 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006014 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006015#endif
6016
Paul Bakker34617722014-06-13 17:20:13 +02006017 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006018}
6019
6020void ssl_session_free( ssl_session *session )
6021{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006022 if( session == NULL )
6023 return;
6024
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006025#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006026 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006027 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006028 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006029 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006030 }
Paul Bakkered27a042013-04-18 22:46:23 +02006031#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006032
Paul Bakkera503a632013-08-14 13:48:06 +02006033#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006034 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006035#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006036
Paul Bakker34617722014-06-13 17:20:13 +02006037 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006038}
6039
Paul Bakker5121ce52009-01-03 21:22:43 +00006040/*
6041 * Free an SSL context
6042 */
6043void ssl_free( ssl_context *ssl )
6044{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006045 if( ssl == NULL )
6046 return;
6047
Paul Bakker5121ce52009-01-03 21:22:43 +00006048 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6049
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006050 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006051 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006052 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6053 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006054 }
6055
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006056 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006058 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6059 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006060 }
6061
Paul Bakker16770332013-10-11 09:59:44 +02006062#if defined(POLARSSL_ZLIB_SUPPORT)
6063 if( ssl->compress_buf != NULL )
6064 {
Paul Bakker34617722014-06-13 17:20:13 +02006065 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006066 polarssl_free( ssl->compress_buf );
6067 }
6068#endif
6069
Paul Bakker40e46942009-01-03 21:51:57 +00006070#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006071 mpi_free( &ssl->dhm_P );
6072 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006073#endif
6074
Paul Bakker48916f92012-09-16 19:57:18 +00006075 if( ssl->transform )
6076 {
6077 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006078 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006079 }
6080
6081 if( ssl->handshake )
6082 {
6083 ssl_handshake_free( ssl->handshake );
6084 ssl_transform_free( ssl->transform_negotiate );
6085 ssl_session_free( ssl->session_negotiate );
6086
Paul Bakker6e339b52013-07-03 13:37:05 +02006087 polarssl_free( ssl->handshake );
6088 polarssl_free( ssl->transform_negotiate );
6089 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006090 }
6091
Paul Bakkerc0463502013-02-14 11:19:38 +01006092 if( ssl->session )
6093 {
6094 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006095 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006096 }
6097
Paul Bakkera503a632013-08-14 13:48:06 +02006098#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006099 if( ssl->ticket_keys )
6100 {
6101 ssl_ticket_keys_free( ssl->ticket_keys );
6102 polarssl_free( ssl->ticket_keys );
6103 }
Paul Bakkera503a632013-08-14 13:48:06 +02006104#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006105
Paul Bakker0be444a2013-08-27 21:55:01 +02006106#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006107 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006108 {
Paul Bakker34617722014-06-13 17:20:13 +02006109 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006110 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 ssl->hostname_len = 0;
6112 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006113#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006114
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006115#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006116 if( ssl->psk != NULL )
6117 {
Paul Bakker34617722014-06-13 17:20:13 +02006118 polarssl_zeroize( ssl->psk, ssl->psk_len );
6119 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006120 polarssl_free( ssl->psk );
6121 polarssl_free( ssl->psk_identity );
6122 ssl->psk_len = 0;
6123 ssl->psk_identity_len = 0;
6124 }
6125#endif
6126
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006128 ssl_key_cert_free( ssl->key_cert );
6129#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006130
Paul Bakker05ef8352012-05-08 09:17:57 +00006131#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6132 if( ssl_hw_record_finish != NULL )
6133 {
6134 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6135 ssl_hw_record_finish( ssl );
6136 }
6137#endif
6138
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006139#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006140 polarssl_free( ssl->cli_id );
6141#endif
6142
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006144
Paul Bakker86f04f42013-02-14 11:20:09 +01006145 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006146 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006147}
6148
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006149#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006150/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006151 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006152 */
6153unsigned char ssl_sig_from_pk( pk_context *pk )
6154{
6155#if defined(POLARSSL_RSA_C)
6156 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6157 return( SSL_SIG_RSA );
6158#endif
6159#if defined(POLARSSL_ECDSA_C)
6160 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6161 return( SSL_SIG_ECDSA );
6162#endif
6163 return( SSL_SIG_ANON );
6164}
6165
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006166pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6167{
6168 switch( sig )
6169 {
6170#if defined(POLARSSL_RSA_C)
6171 case SSL_SIG_RSA:
6172 return( POLARSSL_PK_RSA );
6173#endif
6174#if defined(POLARSSL_ECDSA_C)
6175 case SSL_SIG_ECDSA:
6176 return( POLARSSL_PK_ECDSA );
6177#endif
6178 default:
6179 return( POLARSSL_PK_NONE );
6180 }
6181}
Paul Bakker9af723c2014-05-01 13:03:14 +02006182#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006183
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006184/*
6185 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6186 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006187md_type_t ssl_md_alg_from_hash( unsigned char hash )
6188{
6189 switch( hash )
6190 {
6191#if defined(POLARSSL_MD5_C)
6192 case SSL_HASH_MD5:
6193 return( POLARSSL_MD_MD5 );
6194#endif
6195#if defined(POLARSSL_SHA1_C)
6196 case SSL_HASH_SHA1:
6197 return( POLARSSL_MD_SHA1 );
6198#endif
6199#if defined(POLARSSL_SHA256_C)
6200 case SSL_HASH_SHA224:
6201 return( POLARSSL_MD_SHA224 );
6202 case SSL_HASH_SHA256:
6203 return( POLARSSL_MD_SHA256 );
6204#endif
6205#if defined(POLARSSL_SHA512_C)
6206 case SSL_HASH_SHA384:
6207 return( POLARSSL_MD_SHA384 );
6208 case SSL_HASH_SHA512:
6209 return( POLARSSL_MD_SHA512 );
6210#endif
6211 default:
6212 return( POLARSSL_MD_NONE );
6213 }
6214}
6215
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006216#if defined(POLARSSL_SSL_SET_CURVES)
6217/*
6218 * Check is a curve proposed by the peer is in our list.
6219 * Return 1 if we're willing to use it, 0 otherwise.
6220 */
6221int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6222{
6223 const ecp_group_id *gid;
6224
6225 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6226 if( *gid == grp_id )
6227 return( 1 );
6228
6229 return( 0 );
6230}
Paul Bakker9af723c2014-05-01 13:03:14 +02006231#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006232
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006233#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006234int ssl_check_cert_usage( const x509_crt *cert,
6235 const ssl_ciphersuite_t *ciphersuite,
6236 int cert_endpoint )
6237{
6238#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6239 int usage = 0;
6240#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006241#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6242 const char *ext_oid;
6243 size_t ext_len;
6244#endif
6245
6246#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6247 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6248 ((void) cert);
6249 ((void) cert_endpoint);
6250#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006251
6252#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6253 if( cert_endpoint == SSL_IS_SERVER )
6254 {
6255 /* Server part of the key exchange */
6256 switch( ciphersuite->key_exchange )
6257 {
6258 case POLARSSL_KEY_EXCHANGE_RSA:
6259 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6260 usage = KU_KEY_ENCIPHERMENT;
6261 break;
6262
6263 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6264 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6265 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6266 usage = KU_DIGITAL_SIGNATURE;
6267 break;
6268
6269 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6270 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6271 usage = KU_KEY_AGREEMENT;
6272 break;
6273
6274 /* Don't use default: we want warnings when adding new values */
6275 case POLARSSL_KEY_EXCHANGE_NONE:
6276 case POLARSSL_KEY_EXCHANGE_PSK:
6277 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6278 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6279 usage = 0;
6280 }
6281 }
6282 else
6283 {
6284 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6285 usage = KU_DIGITAL_SIGNATURE;
6286 }
6287
6288 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6289 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006290#else
6291 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006292#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6293
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006294#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6295 if( cert_endpoint == SSL_IS_SERVER )
6296 {
6297 ext_oid = OID_SERVER_AUTH;
6298 ext_len = OID_SIZE( OID_SERVER_AUTH );
6299 }
6300 else
6301 {
6302 ext_oid = OID_CLIENT_AUTH;
6303 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6304 }
6305
6306 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6307 return( -1 );
6308#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6309
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006310 return( 0 );
6311}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006312#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006313
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006314/*
6315 * Convert version numbers to/from wire format
6316 * and, for DTLS, to/from TLS equivalent.
6317 *
6318 * For TLS this is the identity.
6319 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6320 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6321 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6322 */
6323void ssl_write_version( int major, int minor, int transport,
6324 unsigned char ver[2] )
6325{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006326#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006327 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006328 {
6329 if( minor == SSL_MINOR_VERSION_2 )
6330 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6331
6332 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6333 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6334 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006335 else
6336#else
6337 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006338#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006339 {
6340 ver[0] = (unsigned char) major;
6341 ver[1] = (unsigned char) minor;
6342 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006343}
6344
6345void ssl_read_version( int *major, int *minor, int transport,
6346 const unsigned char ver[2] )
6347{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006348#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006349 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006350 {
6351 *major = 255 - ver[0] + 2;
6352 *minor = 255 - ver[1] + 1;
6353
6354 if( *minor == SSL_MINOR_VERSION_1 )
6355 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6356 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006357 else
6358#else
6359 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006360#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006361 {
6362 *major = ver[0];
6363 *minor = ver[1];
6364 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006365}
6366
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006367#endif /* POLARSSL_SSL_TLS_C */