blob: b32205259d7acd868e6a5f25645144c3c7bf8e52 [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
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002942#if defined(POLARSSL_SSL_PROTO_DTLS)
2943 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2944 {
2945 /* Drop unexpected ChangeCipherSpec messages */
2946 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2947 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2948 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2949 {
2950 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2951 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2952 }
2953
2954 /* Drop unexpected ApplicationData records */
2955 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
2956 ssl->state != SSL_HANDSHAKE_OVER )
2957 {
2958 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2959 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2960 }
2961 }
2962#endif
2963
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002964 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002965 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 {
2967 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002968 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969 }
2970
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002971 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 {
2973 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002974 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002975 }
2976
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002977 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002978#if defined(POLARSSL_SSL_PROTO_DTLS)
2979 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2980 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002981 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002982
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002983 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002984 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002985 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002986 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002987 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002988 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002989 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002990
2991#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2992 if( ssl_dtls_replay_check( ssl ) != 0 )
2993 {
2994 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
2995 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2996 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002997#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002998 }
2999#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003000
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003001 /* Check length against the size of our buffer */
3002 if( ssl->in_msglen > SSL_BUFFER_LEN
3003 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003004 {
3005 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3006 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3007 }
3008
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003009 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003010 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003011 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003012 if( ssl->in_msglen < 1 ||
3013 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 {
3015 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003016 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 }
3018 }
3019 else
3020 {
Paul Bakker48916f92012-09-16 19:57:18 +00003021 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 {
3023 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003024 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 }
3026
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003028 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003029 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003030 {
3031 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003032 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003034#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3036 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003037 /*
3038 * TLS encrypted messages can have up to 256 bytes of padding
3039 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003040 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003041 ssl->in_msglen > ssl->transform_in->minlen +
3042 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 {
3044 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003045 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003048 }
3049
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003050 return( 0 );
3051}
Paul Bakker5121ce52009-01-03 21:22:43 +00003052
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003053/*
3054 * If applicable, decrypt (and decompress) record content
3055 */
3056static int ssl_prepare_record_content( ssl_context *ssl )
3057{
3058 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003059
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003061 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakker05ef8352012-05-08 09:17:57 +00003063#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003064 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003065 {
3066 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3067
3068 ret = ssl_hw_record_read( ssl );
3069 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3070 {
3071 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003072 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003073 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003074
3075 if( ret == 0 )
3076 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003077 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003078#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003079 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 {
3081 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3082 {
3083 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3084 return( ret );
3085 }
3086
3087 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3088 ssl->in_msg, ssl->in_msglen );
3089
3090 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3091 {
3092 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003093 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 }
3095 }
3096
Paul Bakker2770fbd2012-07-03 13:30:23 +00003097#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003098 if( ssl->transform_in != NULL &&
3099 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003100 {
3101 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3102 {
3103 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3104 return( ret );
3105 }
3106
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003107 // TODO: what's the purpose of these lines? is in_len used?
3108 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3109 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003110 }
3111#endif /* POLARSSL_ZLIB_SUPPORT */
3112
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003113#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003114 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3115 {
3116 ssl_dtls_replay_update( ssl );
3117 }
3118#endif
3119
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003120 return( 0 );
3121}
3122
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003123static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3124
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003125/*
3126 * Read a record.
3127 *
3128 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3129 * and continue reading until a valid record is found.
3130 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003131int ssl_read_record( ssl_context *ssl )
3132{
3133 int ret;
3134
3135 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3136
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003137 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003138 {
3139 /*
3140 * Get next Handshake message in the current record
3141 */
3142 ssl->in_msglen -= ssl->in_hslen;
3143
3144 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3145 ssl->in_msglen );
3146
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003147 SSL_DEBUG_BUF( 4, "remaining content in record",
3148 ssl->in_msg, ssl->in_msglen );
3149
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003150 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3151 return( ret );
3152
3153 return( 0 );
3154 }
3155
3156 ssl->in_hslen = 0;
3157
3158 /*
3159 * Read the record header and parse it
3160 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003161#if defined(POLARSSL_SSL_PROTO_DTLS)
3162read_record_header:
3163#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003164 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3165 {
3166 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3167 return( ret );
3168 }
3169
3170 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003171 {
3172#if defined(POLARSSL_SSL_PROTO_DTLS)
3173 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3174 {
3175 /* Ignore bad record and get next one; drop the whole datagram
3176 * since current header cannot be trusted to find the next record
3177 * in current datagram */
3178 ssl->next_record_offset = 0;
3179 ssl->in_left = 0;
3180
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003181 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003182 goto read_record_header;
3183 }
3184#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003185 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003186 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003187
3188 /*
3189 * Read and optionally decrypt the message contents
3190 */
3191 if( ( ret = ssl_fetch_input( ssl,
3192 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3193 {
3194 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3195 return( ret );
3196 }
3197
3198 /* Done reading this record, get ready for the next one */
3199#if defined(POLARSSL_SSL_PROTO_DTLS)
3200 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3201 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3202 else
3203#endif
3204 ssl->in_left = 0;
3205
3206 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003207 {
3208#if defined(POLARSSL_SSL_PROTO_DTLS)
3209 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3210 {
3211 /* Silently discard invalid records */
3212 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3213 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3214 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003215 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003216 goto read_record_header;
3217 }
3218
3219 return( ret );
3220 }
3221 else
3222#endif
3223 {
3224 /* Error out (and send alert) on invalid records */
3225#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3226 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3227 {
3228 ssl_send_alert_message( ssl,
3229 SSL_ALERT_LEVEL_FATAL,
3230 SSL_ALERT_MSG_BAD_RECORD_MAC );
3231 }
3232#endif
3233 return( ret );
3234 }
3235 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003236
3237 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003238 * When we sent the last flight of the handshake, we MUST respond to a
3239 * retransmit of the peer's previous flight with a retransmit. (In
3240 * practice, only the Finished message will make it, other messages
3241 * including CCS use the old transform so they're dropped as invalid.)
3242 *
3243 * If the record we received is not a handshake message, however, it
3244 * means the peer received our last flight so we can clean up
3245 * handshake info.
3246 *
3247 * This check needs to be done before prepare_handshake() due to an edge
3248 * case: if the client immediately requests renegotiation, this
3249 * finishes the current handshake first, avoiding the new ClientHello
3250 * being mistaken for an ancient message in the current handshake.
3251 */
3252#if defined(POLARSSL_SSL_PROTO_DTLS)
3253 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3254 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003255 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003256 {
3257 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3258 ssl->in_msg[0] == SSL_HS_FINISHED )
3259 {
3260 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3261
3262 if( ( ret = ssl_resend( ssl ) ) != 0 )
3263 {
3264 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3265 return( ret );
3266 }
3267
3268 return( POLARSSL_ERR_NET_WANT_READ );
3269 }
3270 else
3271 {
3272 ssl_handshake_wrapup_free_hs_transform( ssl );
3273 }
3274 }
3275#endif
3276
3277 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003278 * Handle particular types of records
3279 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3281 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003282 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3283 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 }
3285
3286 if( ssl->in_msgtype == SSL_MSG_ALERT )
3287 {
3288 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3289 ssl->in_msg[0], ssl->in_msg[1] ) );
3290
3291 /*
3292 * Ignore non-fatal alerts, except close_notify
3293 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003294 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003296 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3297 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003298 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003299 }
3300
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003301 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3302 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 {
3304 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003305 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306 }
3307 }
3308
Paul Bakker5121ce52009-01-03 21:22:43 +00003309 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3310
3311 return( 0 );
3312}
3313
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003314int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3315{
3316 int ret;
3317
3318 if( ( ret = ssl_send_alert_message( ssl,
3319 SSL_ALERT_LEVEL_FATAL,
3320 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3321 {
3322 return( ret );
3323 }
3324
3325 return( 0 );
3326}
3327
Paul Bakker0a925182012-04-16 06:46:41 +00003328int ssl_send_alert_message( ssl_context *ssl,
3329 unsigned char level,
3330 unsigned char message )
3331{
3332 int ret;
3333
3334 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3335
3336 ssl->out_msgtype = SSL_MSG_ALERT;
3337 ssl->out_msglen = 2;
3338 ssl->out_msg[0] = level;
3339 ssl->out_msg[1] = message;
3340
3341 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3342 {
3343 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3344 return( ret );
3345 }
3346
3347 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3348
3349 return( 0 );
3350}
3351
Paul Bakker5121ce52009-01-03 21:22:43 +00003352/*
3353 * Handshake functions
3354 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003355#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3356 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3357 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3358 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3359 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3360 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3361 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003362int ssl_write_certificate( ssl_context *ssl )
3363{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003364 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003365
3366 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3367
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003368 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003369 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3370 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003371 {
3372 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3373 ssl->state++;
3374 return( 0 );
3375 }
3376
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003377 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3378 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003379}
3380
3381int ssl_parse_certificate( ssl_context *ssl )
3382{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003383 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3384
3385 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3386
3387 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003388 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3389 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003390 {
3391 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3392 ssl->state++;
3393 return( 0 );
3394 }
3395
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003396 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3397 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398}
3399#else
3400int ssl_write_certificate( ssl_context *ssl )
3401{
3402 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3403 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003404 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003405 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3406
3407 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3408
3409 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003410 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3411 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003412 {
3413 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3414 ssl->state++;
3415 return( 0 );
3416 }
3417
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 if( ssl->endpoint == SSL_IS_CLIENT )
3419 {
3420 if( ssl->client_auth == 0 )
3421 {
3422 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3423 ssl->state++;
3424 return( 0 );
3425 }
3426
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003427#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 /*
3429 * If using SSLv3 and got no cert, send an Alert message
3430 * (otherwise an empty Certificate message will be sent).
3431 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003432 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3434 {
3435 ssl->out_msglen = 2;
3436 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003437 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3438 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
3440 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3441 goto write_msg;
3442 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003443#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 }
3445 else /* SSL_IS_SERVER */
3446 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003447 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 {
3449 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003450 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 }
3452 }
3453
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003454 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003455
3456 /*
3457 * 0 . 0 handshake type
3458 * 1 . 3 handshake length
3459 * 4 . 6 length of all certs
3460 * 7 . 9 length of cert. 1
3461 * 10 . n-1 peer certificate
3462 * n . n+2 length of cert. 2
3463 * n+3 . ... upper level cert, etc.
3464 */
3465 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003466 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003467
Paul Bakker29087132010-03-21 21:03:34 +00003468 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 {
3470 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003471 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 {
3473 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3474 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003475 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 }
3477
3478 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3479 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3480 ssl->out_msg[i + 2] = (unsigned char)( n );
3481
3482 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3483 i += n; crt = crt->next;
3484 }
3485
3486 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3487 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3488 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3489
3490 ssl->out_msglen = i;
3491 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3492 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3493
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003494#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003495write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003496#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
3498 ssl->state++;
3499
3500 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3501 {
3502 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3503 return( ret );
3504 }
3505
3506 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3507
Paul Bakkered27a042013-04-18 22:46:23 +02003508 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509}
3510
3511int ssl_parse_certificate( ssl_context *ssl )
3512{
Paul Bakkered27a042013-04-18 22:46:23 +02003513 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003514 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003515 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
3517 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3518
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003519 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003520 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3521 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003522 {
3523 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3524 ssl->state++;
3525 return( 0 );
3526 }
3527
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003529 ( ssl->authmode == SSL_VERIFY_NONE ||
3530 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003532 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3534 ssl->state++;
3535 return( 0 );
3536 }
3537
3538 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3539 {
3540 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3541 return( ret );
3542 }
3543
3544 ssl->state++;
3545
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003546#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003547 /*
3548 * Check if the client sent an empty certificate
3549 */
3550 if( ssl->endpoint == SSL_IS_SERVER &&
3551 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3552 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003553 if( ssl->in_msglen == 2 &&
3554 ssl->in_msgtype == SSL_MSG_ALERT &&
3555 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3556 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 {
3558 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3559
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003560 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003561 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3562 return( 0 );
3563 else
Paul Bakker40e46942009-01-03 21:51:57 +00003564 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 }
3566 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003567#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003569#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3570 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 if( ssl->endpoint == SSL_IS_SERVER &&
3572 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3573 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003574 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003575 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3576 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003577 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003578 {
3579 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3580
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003581 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003583 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003584 else
3585 return( 0 );
3586 }
3587 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003588#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3589 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
3591 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3592 {
3593 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003594 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 }
3596
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003597 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3598 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 {
3600 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003601 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 }
3603
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003604 i = ssl_hs_hdr_len( ssl );
3605
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 /*
3607 * Same message structure as in ssl_write_certificate()
3608 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003609 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003610
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003611 if( ssl->in_msg[i] != 0 ||
3612 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003613 {
3614 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003615 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003616 }
3617
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003618 /* In case we tried to reuse a session but it failed */
3619 if( ssl->session_negotiate->peer_cert != NULL )
3620 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003621 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003622 polarssl_free( ssl->session_negotiate->peer_cert );
3623 }
3624
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003625 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3626 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003627 {
3628 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003629 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003630 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003631 }
3632
Paul Bakkerb6b09562013-09-18 14:17:41 +02003633 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003634
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003635 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003636
3637 while( i < ssl->in_hslen )
3638 {
3639 if( ssl->in_msg[i] != 0 )
3640 {
3641 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003642 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 }
3644
3645 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3646 | (unsigned int) ssl->in_msg[i + 2];
3647 i += 3;
3648
3649 if( n < 128 || i + n > ssl->in_hslen )
3650 {
3651 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003652 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653 }
3654
Paul Bakkerddf26b42013-09-18 13:46:23 +02003655 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3656 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003657 if( ret != 0 )
3658 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003659 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 return( ret );
3661 }
3662
3663 i += n;
3664 }
3665
Paul Bakker48916f92012-09-16 19:57:18 +00003666 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003668 /*
3669 * On client, make sure the server cert doesn't change during renego to
3670 * avoid "triple handshake" attack: https://secure-resumption.com/
3671 */
3672 if( ssl->endpoint == SSL_IS_CLIENT &&
3673 ssl->renegotiation == SSL_RENEGOTIATION )
3674 {
3675 if( ssl->session->peer_cert == NULL )
3676 {
3677 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3678 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3679 }
3680
3681 if( ssl->session->peer_cert->raw.len !=
3682 ssl->session_negotiate->peer_cert->raw.len ||
3683 memcmp( ssl->session->peer_cert->raw.p,
3684 ssl->session_negotiate->peer_cert->raw.p,
3685 ssl->session->peer_cert->raw.len ) != 0 )
3686 {
3687 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3688 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3689 }
3690 }
3691
Paul Bakker5121ce52009-01-03 21:22:43 +00003692 if( ssl->authmode != SSL_VERIFY_NONE )
3693 {
3694 if( ssl->ca_chain == NULL )
3695 {
3696 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003697 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003698 }
3699
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003700 /*
3701 * Main check: verify certificate
3702 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003703 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3704 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3705 &ssl->session_negotiate->verify_result,
3706 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707
3708 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003709 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003711 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003712
3713 /*
3714 * Secondary checks: always done, but change 'ret' only if it was 0
3715 */
3716
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003717#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003718 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003719 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003720
3721 /* If certificate uses an EC key, make sure the curve is OK */
3722 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3723 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3724 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003725 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003726 if( ret == 0 )
3727 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003728 }
3729 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003730#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003731
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003732 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3733 ciphersuite_info,
3734 ! ssl->endpoint ) != 0 )
3735 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003736 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003737 if( ret == 0 )
3738 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3739 }
3740
Paul Bakker5121ce52009-01-03 21:22:43 +00003741 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3742 ret = 0;
3743 }
3744
3745 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3746
3747 return( ret );
3748}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003749#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3750 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3751 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3752 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3753 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3754 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3755 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
3757int ssl_write_change_cipher_spec( ssl_context *ssl )
3758{
3759 int ret;
3760
3761 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3762
3763 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3764 ssl->out_msglen = 1;
3765 ssl->out_msg[0] = 1;
3766
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 ssl->state++;
3768
3769 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3770 {
3771 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3772 return( ret );
3773 }
3774
3775 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3776
3777 return( 0 );
3778}
3779
3780int ssl_parse_change_cipher_spec( ssl_context *ssl )
3781{
3782 int ret;
3783
3784 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3785
Paul Bakker5121ce52009-01-03 21:22:43 +00003786 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3787 {
3788 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3789 return( ret );
3790 }
3791
3792 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3793 {
3794 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003795 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003796 }
3797
3798 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3799 {
3800 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003801 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003802 }
3803
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003804 /*
3805 * Switch to our negotiated transform and session parameters for inbound
3806 * data.
3807 */
3808 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3809 ssl->transform_in = ssl->transform_negotiate;
3810 ssl->session_in = ssl->session_negotiate;
3811
3812#if defined(POLARSSL_SSL_PROTO_DTLS)
3813 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3814 {
3815#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3816 ssl_dtls_replay_reset( ssl );
3817#endif
3818
3819 /* Increment epoch */
3820 if( ++ssl->in_epoch == 0 )
3821 {
3822 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3823 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3824 }
3825 }
3826 else
3827#endif /* POLARSSL_SSL_PROTO_DTLS */
3828 memset( ssl->in_ctr, 0, 8 );
3829
3830 /*
3831 * Set the in_msg pointer to the correct location based on IV length
3832 */
3833 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3834 {
3835 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3836 ssl->transform_negotiate->fixed_ivlen;
3837 }
3838 else
3839 ssl->in_msg = ssl->in_iv;
3840
3841#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3842 if( ssl_hw_record_activate != NULL )
3843 {
3844 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3845 {
3846 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3847 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3848 }
3849 }
3850#endif
3851
Paul Bakker5121ce52009-01-03 21:22:43 +00003852 ssl->state++;
3853
3854 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3855
3856 return( 0 );
3857}
3858
Paul Bakker41c83d32013-03-20 14:39:14 +01003859void ssl_optimize_checksum( ssl_context *ssl,
3860 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003861{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003862 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003863
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003864#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3865 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003866 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003867 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003868 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003869#endif
3870#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3871#if defined(POLARSSL_SHA512_C)
3872 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3873 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3874 else
3875#endif
3876#if defined(POLARSSL_SHA256_C)
3877 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003878 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003879 else
3880#endif
3881#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003882 {
3883 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003884 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003885 }
Paul Bakker380da532012-04-18 16:10:25 +00003886}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003887
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003888void ssl_reset_checksum( ssl_context *ssl )
3889{
3890#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3891 defined(POLARSSL_SSL_PROTO_TLS1_1)
3892 md5_starts( &ssl->handshake->fin_md5 );
3893 sha1_starts( &ssl->handshake->fin_sha1 );
3894#endif
3895#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3896#if defined(POLARSSL_SHA256_C)
3897 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3898#endif
3899#if defined(POLARSSL_SHA512_C)
3900 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3901#endif
3902#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3903}
3904
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003905static void ssl_update_checksum_start( ssl_context *ssl,
3906 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003907{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003908#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3909 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003910 md5_update( &ssl->handshake->fin_md5 , buf, len );
3911 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003912#endif
3913#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3914#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003915 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003916#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003917#if defined(POLARSSL_SHA512_C)
3918 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003919#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003920#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003921}
3922
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003923#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3924 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003925static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3926 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003927{
Paul Bakker48916f92012-09-16 19:57:18 +00003928 md5_update( &ssl->handshake->fin_md5 , buf, len );
3929 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003930}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003931#endif
Paul Bakker380da532012-04-18 16:10:25 +00003932
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003933#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3934#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003935static void ssl_update_checksum_sha256( 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 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003939}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003940#endif
Paul Bakker380da532012-04-18 16:10:25 +00003941
Paul Bakker9e36f042013-06-30 14:34:05 +02003942#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003943static void ssl_update_checksum_sha384( ssl_context *ssl,
3944 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003945{
Paul Bakker9e36f042013-06-30 14:34:05 +02003946 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003947}
Paul Bakker769075d2012-11-24 11:26:46 +01003948#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003949#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003950
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003951#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003952static void ssl_calc_finished_ssl(
3953 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003954{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003955 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003956 md5_context md5;
3957 sha1_context sha1;
3958
Paul Bakker5121ce52009-01-03 21:22:43 +00003959 unsigned char padbuf[48];
3960 unsigned char md5sum[16];
3961 unsigned char sha1sum[20];
3962
Paul Bakker48916f92012-09-16 19:57:18 +00003963 ssl_session *session = ssl->session_negotiate;
3964 if( !session )
3965 session = ssl->session;
3966
Paul Bakker1ef83d62012-04-11 12:09:53 +00003967 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3968
Paul Bakker48916f92012-09-16 19:57:18 +00003969 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3970 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003971
3972 /*
3973 * SSLv3:
3974 * hash =
3975 * MD5( master + pad2 +
3976 * MD5( handshake + sender + master + pad1 ) )
3977 * + SHA1( master + pad2 +
3978 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979 */
3980
Paul Bakker90995b52013-06-24 19:20:35 +02003981#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003983 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003984#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003985
Paul Bakker90995b52013-06-24 19:20:35 +02003986#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003988 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003990
Paul Bakker3c2122f2013-06-24 19:03:14 +02003991 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3992 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003993
Paul Bakker1ef83d62012-04-11 12:09:53 +00003994 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003995
Paul Bakker3c2122f2013-06-24 19:03:14 +02003996 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003997 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998 md5_update( &md5, padbuf, 48 );
3999 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004000
Paul Bakker3c2122f2013-06-24 19:03:14 +02004001 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004002 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004003 sha1_update( &sha1, padbuf, 40 );
4004 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004005
Paul Bakker1ef83d62012-04-11 12:09:53 +00004006 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004007
Paul Bakker1ef83d62012-04-11 12:09:53 +00004008 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004009 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004010 md5_update( &md5, padbuf, 48 );
4011 md5_update( &md5, md5sum, 16 );
4012 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Paul Bakker1ef83d62012-04-11 12:09:53 +00004014 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004015 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004016 sha1_update( &sha1, padbuf , 40 );
4017 sha1_update( &sha1, sha1sum, 20 );
4018 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004019
Paul Bakker1ef83d62012-04-11 12:09:53 +00004020 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
Paul Bakker5b4af392014-06-26 12:09:34 +02004022 md5_free( &md5 );
4023 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004024
Paul Bakker34617722014-06-13 17:20:13 +02004025 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4026 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4027 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004028
4029 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4030}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004031#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004033#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004034static void ssl_calc_finished_tls(
4035 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004036{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004037 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004038 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004039 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004040 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004041 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004042
Paul Bakker48916f92012-09-16 19:57:18 +00004043 ssl_session *session = ssl->session_negotiate;
4044 if( !session )
4045 session = ssl->session;
4046
Paul Bakker1ef83d62012-04-11 12:09:53 +00004047 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004048
Paul Bakker48916f92012-09-16 19:57:18 +00004049 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4050 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004051
Paul Bakker1ef83d62012-04-11 12:09:53 +00004052 /*
4053 * TLSv1:
4054 * hash = PRF( master, finished_label,
4055 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4056 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004057
Paul Bakker90995b52013-06-24 19:20:35 +02004058#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004059 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4060 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004061#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004062
Paul Bakker90995b52013-06-24 19:20:35 +02004063#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004064 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4065 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004066#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004067
4068 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004069 ? "client finished"
4070 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004071
4072 md5_finish( &md5, padbuf );
4073 sha1_finish( &sha1, padbuf + 16 );
4074
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004075 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004076 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004077
4078 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4079
Paul Bakker5b4af392014-06-26 12:09:34 +02004080 md5_free( &md5 );
4081 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004082
Paul Bakker34617722014-06-13 17:20:13 +02004083 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004084
4085 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4086}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004087#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004088
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004089#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4090#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004091static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004092 ssl_context *ssl, unsigned char *buf, int from )
4093{
4094 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004095 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004096 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004097 unsigned char padbuf[32];
4098
Paul Bakker48916f92012-09-16 19:57:18 +00004099 ssl_session *session = ssl->session_negotiate;
4100 if( !session )
4101 session = ssl->session;
4102
Paul Bakker380da532012-04-18 16:10:25 +00004103 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004104
Paul Bakker9e36f042013-06-30 14:34:05 +02004105 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004106
4107 /*
4108 * TLSv1.2:
4109 * hash = PRF( master, finished_label,
4110 * Hash( handshake ) )[0.11]
4111 */
4112
Paul Bakker9e36f042013-06-30 14:34:05 +02004113#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004115 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004116#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004117
4118 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004119 ? "client finished"
4120 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004121
Paul Bakker9e36f042013-06-30 14:34:05 +02004122 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004123
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004124 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004125 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004126
4127 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4128
Paul Bakker5b4af392014-06-26 12:09:34 +02004129 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004130
Paul Bakker34617722014-06-13 17:20:13 +02004131 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004132
4133 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4134}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004135#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004136
Paul Bakker9e36f042013-06-30 14:34:05 +02004137#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004138static void ssl_calc_finished_tls_sha384(
4139 ssl_context *ssl, unsigned char *buf, int from )
4140{
4141 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004142 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004143 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004144 unsigned char padbuf[48];
4145
Paul Bakker48916f92012-09-16 19:57:18 +00004146 ssl_session *session = ssl->session_negotiate;
4147 if( !session )
4148 session = ssl->session;
4149
Paul Bakker380da532012-04-18 16:10:25 +00004150 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004151
Paul Bakker9e36f042013-06-30 14:34:05 +02004152 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004153
4154 /*
4155 * TLSv1.2:
4156 * hash = PRF( master, finished_label,
4157 * Hash( handshake ) )[0.11]
4158 */
4159
Paul Bakker9e36f042013-06-30 14:34:05 +02004160#if !defined(POLARSSL_SHA512_ALT)
4161 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4162 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004163#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004164
4165 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004166 ? "client finished"
4167 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004168
Paul Bakker9e36f042013-06-30 14:34:05 +02004169 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004170
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004171 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004172 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004173
4174 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4175
Paul Bakker5b4af392014-06-26 12:09:34 +02004176 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004177
Paul Bakker34617722014-06-13 17:20:13 +02004178 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004179
4180 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4181}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004182#endif /* POLARSSL_SHA512_C */
4183#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004184
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004185static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004186{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004187 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004188
4189 /*
4190 * Free our handshake params
4191 */
4192 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004193 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004194 ssl->handshake = NULL;
4195
4196 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004197 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004198 */
4199 if( ssl->transform )
4200 {
4201 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004202 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004203 }
4204 ssl->transform = ssl->transform_negotiate;
4205 ssl->transform_negotiate = NULL;
4206
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004207 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4208}
4209
4210void ssl_handshake_wrapup( ssl_context *ssl )
4211{
4212 int resume = ssl->handshake->resume;
4213
4214 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4215
4216 if( ssl->renegotiation == SSL_RENEGOTIATION )
4217 {
4218 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4219 ssl->renego_records_seen = 0;
4220 }
4221
4222 /*
4223 * Free the previous session and switch in the current one
4224 */
Paul Bakker0a597072012-09-25 21:55:46 +00004225 if( ssl->session )
4226 {
4227 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004228 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004229 }
4230 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004231 ssl->session_negotiate = NULL;
4232
Paul Bakker0a597072012-09-25 21:55:46 +00004233 /*
4234 * Add cache entry
4235 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004236 if( ssl->f_set_cache != NULL &&
4237 ssl->session->length != 0 &&
4238 resume == 0 )
4239 {
Paul Bakker0a597072012-09-25 21:55:46 +00004240 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4241 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004242 }
Paul Bakker0a597072012-09-25 21:55:46 +00004243
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004244#if defined(POLARSSL_SSL_PROTO_DTLS)
4245 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4246 ssl->handshake->flight != NULL )
4247 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004248 /* Cancel handshake timer */
4249 ssl_set_timer( ssl, 0 );
4250
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004251 /* Keep last flight around in case we need to resend it:
4252 * we need the handshake and transform structures for that */
4253 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4254 }
4255 else
4256#endif
4257 ssl_handshake_wrapup_free_hs_transform( ssl );
4258
Paul Bakker48916f92012-09-16 19:57:18 +00004259 ssl->state++;
4260
4261 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4262}
4263
Paul Bakker1ef83d62012-04-11 12:09:53 +00004264int ssl_write_finished( ssl_context *ssl )
4265{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004266 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004267
4268 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4269
Paul Bakker92be97b2013-01-02 17:30:03 +01004270 /*
4271 * Set the out_msg pointer to the correct location based on IV length
4272 */
4273 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4274 {
4275 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4276 ssl->transform_negotiate->fixed_ivlen;
4277 }
4278 else
4279 ssl->out_msg = ssl->out_iv;
4280
Paul Bakker48916f92012-09-16 19:57:18 +00004281 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004282
4283 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004284 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4285
Paul Bakker48916f92012-09-16 19:57:18 +00004286 ssl->verify_data_len = hash_len;
4287 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4288
Paul Bakker5121ce52009-01-03 21:22:43 +00004289 ssl->out_msglen = 4 + hash_len;
4290 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4291 ssl->out_msg[0] = SSL_HS_FINISHED;
4292
4293 /*
4294 * In case of session resuming, invert the client and server
4295 * ChangeCipherSpec messages order.
4296 */
Paul Bakker0a597072012-09-25 21:55:46 +00004297 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004298 {
4299 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004300 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 else
4302 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4303 }
4304 else
4305 ssl->state++;
4306
Paul Bakker48916f92012-09-16 19:57:18 +00004307 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004308 * Switch to our negotiated transform and session parameters for outbound
4309 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004310 */
4311 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004312
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004313#if defined(POLARSSL_SSL_PROTO_DTLS)
4314 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4315 {
4316 unsigned char i;
4317
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004318 /* Remember current epoch settings for resending */
4319 ssl->handshake->alt_transform_out = ssl->transform_out;
4320 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4321
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004322 /* Set sequence_number to zero */
4323 memset( ssl->out_ctr + 2, 0, 6 );
4324
4325 /* Increment epoch */
4326 for( i = 2; i > 0; i-- )
4327 if( ++ssl->out_ctr[i - 1] != 0 )
4328 break;
4329
4330 /* The loop goes to its end iff the counter is wrapping */
4331 if( i == 0 )
4332 {
4333 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4334 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4335 }
4336 }
4337 else
4338#endif /* POLARSSL_SSL_PROTO_DTLS */
4339 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004340
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004341 ssl->transform_out = ssl->transform_negotiate;
4342 ssl->session_out = ssl->session_negotiate;
4343
Paul Bakker07eb38b2012-12-19 14:42:06 +01004344#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004345 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004346 {
4347 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4348 {
4349 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4350 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4351 }
4352 }
4353#endif
4354
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004355#if defined(POLARSSL_SSL_PROTO_DTLS)
4356 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4357 ssl_send_flight_completed( ssl );
4358#endif
4359
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4361 {
4362 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4363 return( ret );
4364 }
4365
4366 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4367
4368 return( 0 );
4369}
4370
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004371#if defined(POLARSSL_SSL_PROTO_SSL3)
4372#define SSL_MAX_HASH_LEN 36
4373#else
4374#define SSL_MAX_HASH_LEN 12
4375#endif
4376
Paul Bakker5121ce52009-01-03 21:22:43 +00004377int ssl_parse_finished( ssl_context *ssl )
4378{
Paul Bakker23986e52011-04-24 08:57:21 +00004379 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004380 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004381 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004382
4383 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4384
Paul Bakker48916f92012-09-16 19:57:18 +00004385 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004386
Paul Bakker5121ce52009-01-03 21:22:43 +00004387 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4388 {
4389 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4390 return( ret );
4391 }
4392
4393 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4394 {
4395 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004396 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004397 }
4398
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004399 /* There is currently no ciphersuite using another length with TLS 1.2 */
4400#if defined(POLARSSL_SSL_PROTO_SSL3)
4401 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4402 hash_len = 36;
4403 else
4404#endif
4405 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004406
4407 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004408 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004409 {
4410 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004411 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004412 }
4413
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004414 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4415 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 {
4417 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004418 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004419 }
4420
Paul Bakker48916f92012-09-16 19:57:18 +00004421 ssl->verify_data_len = hash_len;
4422 memcpy( ssl->peer_verify_data, buf, hash_len );
4423
Paul Bakker0a597072012-09-25 21:55:46 +00004424 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004425 {
4426 if( ssl->endpoint == SSL_IS_CLIENT )
4427 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4428
4429 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004430 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004431 }
4432 else
4433 ssl->state++;
4434
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004435#if defined(POLARSSL_SSL_PROTO_DTLS)
4436 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4437 ssl_recv_flight_completed( ssl );
4438#endif
4439
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4441
4442 return( 0 );
4443}
4444
Paul Bakker968afaa2014-07-09 11:09:24 +02004445static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004446{
4447 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4448
4449#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4450 defined(POLARSSL_SSL_PROTO_TLS1_1)
4451 md5_init( &handshake->fin_md5 );
4452 sha1_init( &handshake->fin_sha1 );
4453 md5_starts( &handshake->fin_md5 );
4454 sha1_starts( &handshake->fin_sha1 );
4455#endif
4456#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4457#if defined(POLARSSL_SHA256_C)
4458 sha256_init( &handshake->fin_sha256 );
4459 sha256_starts( &handshake->fin_sha256, 0 );
4460#endif
4461#if defined(POLARSSL_SHA512_C)
4462 sha512_init( &handshake->fin_sha512 );
4463 sha512_starts( &handshake->fin_sha512, 1 );
4464#endif
4465#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4466
4467 handshake->update_checksum = ssl_update_checksum_start;
4468 handshake->sig_alg = SSL_HASH_SHA1;
4469
4470#if defined(POLARSSL_DHM_C)
4471 dhm_init( &handshake->dhm_ctx );
4472#endif
4473#if defined(POLARSSL_ECDH_C)
4474 ecdh_init( &handshake->ecdh_ctx );
4475#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004476}
4477
4478static void ssl_transform_init( ssl_transform *transform )
4479{
4480 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004481
4482 cipher_init( &transform->cipher_ctx_enc );
4483 cipher_init( &transform->cipher_ctx_dec );
4484
4485 md_init( &transform->md_ctx_enc );
4486 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004487}
4488
4489void ssl_session_init( ssl_session *session )
4490{
4491 memset( session, 0, sizeof(ssl_session) );
4492}
4493
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004494static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004495{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004496 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004497 if( ssl->transform_negotiate )
4498 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004499 if( ssl->session_negotiate )
4500 ssl_session_free( ssl->session_negotiate );
4501 if( ssl->handshake )
4502 ssl_handshake_free( ssl->handshake );
4503
4504 /*
4505 * Either the pointers are now NULL or cleared properly and can be freed.
4506 * Now allocate missing structures.
4507 */
4508 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004509 {
4510 ssl->transform_negotiate =
4511 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4512 }
Paul Bakker48916f92012-09-16 19:57:18 +00004513
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004514 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004515 {
4516 ssl->session_negotiate =
4517 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4518 }
Paul Bakker48916f92012-09-16 19:57:18 +00004519
Paul Bakker82788fb2014-10-20 13:59:19 +02004520 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004521 {
4522 ssl->handshake = (ssl_handshake_params *)
4523 polarssl_malloc( sizeof(ssl_handshake_params) );
4524 }
Paul Bakker48916f92012-09-16 19:57:18 +00004525
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004526 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004527 if( ssl->handshake == NULL ||
4528 ssl->transform_negotiate == NULL ||
4529 ssl->session_negotiate == NULL )
4530 {
4531 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004532
4533 polarssl_free( ssl->handshake );
4534 polarssl_free( ssl->transform_negotiate );
4535 polarssl_free( ssl->session_negotiate );
4536
4537 ssl->handshake = NULL;
4538 ssl->transform_negotiate = NULL;
4539 ssl->session_negotiate = NULL;
4540
Paul Bakker48916f92012-09-16 19:57:18 +00004541 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4542 }
4543
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004544 /* Initialize structures */
4545 ssl_session_init( ssl->session_negotiate );
4546 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004547 ssl_handshake_params_init( ssl->handshake );
4548
4549#if defined(POLARSSL_X509_CRT_PARSE_C)
4550 ssl->handshake->key_cert = ssl->key_cert;
4551#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004552
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004553 /*
4554 * We may not know yet if we're using DTLS,
4555 * so always initiliase DTLS-specific fields.
4556 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004557#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004558 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004559
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004560 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004561 if( ssl->endpoint == SSL_IS_CLIENT )
4562 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4563 else
4564 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004565#endif
4566
Paul Bakker48916f92012-09-16 19:57:18 +00004567 return( 0 );
4568}
4569
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004570#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4571/* Dummy cookie callbacks for defaults */
4572static int ssl_cookie_write_dummy( void *ctx,
4573 unsigned char **p, unsigned char *end,
4574 const unsigned char *cli_id, size_t cli_id_len )
4575{
4576 ((void) ctx);
4577 ((void) p);
4578 ((void) end);
4579 ((void) cli_id);
4580 ((void) cli_id_len);
4581
4582 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4583}
4584
4585static int ssl_cookie_check_dummy( void *ctx,
4586 const unsigned char *cookie, size_t cookie_len,
4587 const unsigned char *cli_id, size_t cli_id_len )
4588{
4589 ((void) ctx);
4590 ((void) cookie);
4591 ((void) cookie_len);
4592 ((void) cli_id);
4593 ((void) cli_id_len);
4594
4595 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4596}
4597#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4598
Paul Bakker5121ce52009-01-03 21:22:43 +00004599/*
4600 * Initialize an SSL context
4601 */
4602int ssl_init( ssl_context *ssl )
4603{
Paul Bakker48916f92012-09-16 19:57:18 +00004604 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004605 int len = SSL_BUFFER_LEN;
4606
4607 memset( ssl, 0, sizeof( ssl_context ) );
4608
Paul Bakker62f2dee2012-09-28 07:31:51 +00004609 /*
4610 * Sane defaults
4611 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004612 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4613 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4614 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4615 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004616
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004617 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004618
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004619 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4620
Paul Bakker62f2dee2012-09-28 07:31:51 +00004621#if defined(POLARSSL_DHM_C)
4622 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4623 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4624 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4625 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4626 {
4627 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4628 return( ret );
4629 }
4630#endif
4631
4632 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004633 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004634 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004635 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004636 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004637
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004638 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 {
4640 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004641 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004642 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004643 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004644 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004645 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004646 }
4647
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004648 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4649 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004650
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004651 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4652 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4653
Paul Bakker606b4ba2013-08-14 16:52:14 +02004654#if defined(POLARSSL_SSL_SESSION_TICKETS)
4655 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4656#endif
4657
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004658#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004659 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004660#endif
4661
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004662#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4663 ssl->f_cookie_write = ssl_cookie_write_dummy;
4664 ssl->f_cookie_check = ssl_cookie_check_dummy;
4665#endif
4666
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004667#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4668 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4669#endif
4670
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004671#if defined(POLARSSL_SSL_PROTO_DTLS)
4672 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4673 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4674#endif
4675
Paul Bakker48916f92012-09-16 19:57:18 +00004676 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4677 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004678
4679 return( 0 );
4680}
4681
4682/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004683 * Reset an initialized and used SSL context for re-use while retaining
4684 * all application-set variables, function pointers and data.
4685 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004686int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004687{
Paul Bakker48916f92012-09-16 19:57:18 +00004688 int ret;
4689
Paul Bakker7eb013f2011-10-06 12:37:39 +00004690 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004691 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4692 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4693
4694 ssl->verify_data_len = 0;
4695 memset( ssl->own_verify_data, 0, 36 );
4696 memset( ssl->peer_verify_data, 0, 36 );
4697
Paul Bakker7eb013f2011-10-06 12:37:39 +00004698 ssl->in_offt = NULL;
4699
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004700 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004701 ssl->in_msgtype = 0;
4702 ssl->in_msglen = 0;
4703 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004704#if defined(POLARSSL_SSL_PROTO_DTLS)
4705 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004706 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004707#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004708#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4709 ssl_dtls_replay_reset( ssl );
4710#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004711
4712 ssl->in_hslen = 0;
4713 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004714 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004715
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004716 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004717 ssl->out_msgtype = 0;
4718 ssl->out_msglen = 0;
4719 ssl->out_left = 0;
4720
Paul Bakker48916f92012-09-16 19:57:18 +00004721 ssl->transform_in = NULL;
4722 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004723
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004724 ssl->renego_records_seen = 0;
4725
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004726 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4727 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004728
4729#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004730 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004731 {
4732 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004733 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004734 {
4735 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4736 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4737 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004738 }
4739#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004740
Paul Bakker48916f92012-09-16 19:57:18 +00004741 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004742 {
Paul Bakker48916f92012-09-16 19:57:18 +00004743 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004744 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004745 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004746 }
Paul Bakker48916f92012-09-16 19:57:18 +00004747
Paul Bakkerc0463502013-02-14 11:19:38 +01004748 if( ssl->session )
4749 {
4750 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004751 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004752 ssl->session = NULL;
4753 }
4754
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004755#if defined(POLARSSL_SSL_ALPN)
4756 ssl->alpn_chosen = NULL;
4757#endif
4758
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004759#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004760 polarssl_free( ssl->cli_id );
4761 ssl->cli_id = NULL;
4762 ssl->cli_id_len = 0;
4763#endif
4764
Paul Bakker48916f92012-09-16 19:57:18 +00004765 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4766 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004767
4768 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004769}
4770
Paul Bakkera503a632013-08-14 13:48:06 +02004771#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004772static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4773{
4774 aes_free( &tkeys->enc );
4775 aes_free( &tkeys->dec );
4776
4777 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4778}
4779
Paul Bakker7eb013f2011-10-06 12:37:39 +00004780/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004781 * Allocate and initialize ticket keys
4782 */
4783static int ssl_ticket_keys_init( ssl_context *ssl )
4784{
4785 int ret;
4786 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004787 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004788
4789 if( ssl->ticket_keys != NULL )
4790 return( 0 );
4791
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004792 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4793 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004794 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4795
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004796 aes_init( &tkeys->enc );
4797 aes_init( &tkeys->dec );
4798
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004799 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004800 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004801 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004802 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004803 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004804 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004805
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004806 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4807 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4808 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4809 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004810 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004811 polarssl_free( tkeys );
4812 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004813 }
4814
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004815 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004816 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004817 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004818 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004819 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004820 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004821
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004822 ssl->ticket_keys = tkeys;
4823
4824 return( 0 );
4825}
Paul Bakkera503a632013-08-14 13:48:06 +02004826#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004827
4828/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004829 * SSL set accessors
4830 */
4831void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4832{
4833 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004834
Paul Bakker606b4ba2013-08-14 16:52:14 +02004835#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004836 if( endpoint == SSL_IS_CLIENT )
4837 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004838#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004839}
4840
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004841int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004842{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004843#if defined(POLARSSL_SSL_PROTO_DTLS)
4844 if( transport == SSL_TRANSPORT_DATAGRAM )
4845 {
4846 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004847
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004848 ssl->out_hdr = ssl->out_buf;
4849 ssl->out_ctr = ssl->out_buf + 3;
4850 ssl->out_len = ssl->out_buf + 11;
4851 ssl->out_iv = ssl->out_buf + 13;
4852 ssl->out_msg = ssl->out_buf + 13;
4853
4854 ssl->in_hdr = ssl->in_buf;
4855 ssl->in_ctr = ssl->in_buf + 3;
4856 ssl->in_len = ssl->in_buf + 11;
4857 ssl->in_iv = ssl->in_buf + 13;
4858 ssl->in_msg = ssl->in_buf + 13;
4859
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004860 /* DTLS starts with TLS1.1 */
4861 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4862 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004863
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004864 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4865 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4866
4867 return( 0 );
4868 }
4869#endif
4870
4871 if( transport == SSL_TRANSPORT_STREAM )
4872 {
4873 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004874
4875 ssl->out_ctr = ssl->out_buf;
4876 ssl->out_hdr = ssl->out_buf + 8;
4877 ssl->out_len = ssl->out_buf + 11;
4878 ssl->out_iv = ssl->out_buf + 13;
4879 ssl->out_msg = ssl->out_buf + 13;
4880
4881 ssl->in_ctr = ssl->in_buf;
4882 ssl->in_hdr = ssl->in_buf + 8;
4883 ssl->in_len = ssl->in_buf + 11;
4884 ssl->in_iv = ssl->in_buf + 13;
4885 ssl->in_msg = ssl->in_buf + 13;
4886
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004887 return( 0 );
4888 }
4889
4890 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004891}
4892
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004893#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4894void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4895{
4896 ssl->anti_replay = mode;
4897}
4898#endif
4899
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004900#if defined(POLARSSL_SSL_PROTO_DTLS)
4901void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4902{
4903 ssl->hs_timeout_min = min;
4904 ssl->hs_timeout_max = max;
4905}
4906#endif
4907
Paul Bakker5121ce52009-01-03 21:22:43 +00004908void ssl_set_authmode( ssl_context *ssl, int authmode )
4909{
4910 ssl->authmode = authmode;
4911}
4912
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004913#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004914void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004915 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004916 void *p_vrfy )
4917{
4918 ssl->f_vrfy = f_vrfy;
4919 ssl->p_vrfy = p_vrfy;
4920}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004921#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004922
Paul Bakker5121ce52009-01-03 21:22:43 +00004923void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004924 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004925 void *p_rng )
4926{
4927 ssl->f_rng = f_rng;
4928 ssl->p_rng = p_rng;
4929}
4930
4931void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004932 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 void *p_dbg )
4934{
4935 ssl->f_dbg = f_dbg;
4936 ssl->p_dbg = p_dbg;
4937}
4938
4939void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004940 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004941 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004942{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004943 if( p_recv != p_send )
4944 {
4945 ssl->f_recv = NULL;
4946 ssl->f_send = NULL;
4947 ssl->p_bio = NULL;
4948 return;
4949 }
4950
Paul Bakker5121ce52009-01-03 21:22:43 +00004951 ssl->f_recv = f_recv;
4952 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004953 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004954}
4955
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004956void ssl_set_bio_timeout( ssl_context *ssl,
4957 void *p_bio,
4958 int (*f_send)(void *, const unsigned char *, size_t),
4959 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004960 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4961 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004962{
4963 ssl->p_bio = p_bio;
4964 ssl->f_send = f_send;
4965 ssl->f_recv = f_recv;
4966 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004967 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004968}
4969
Paul Bakker0a597072012-09-25 21:55:46 +00004970void ssl_set_session_cache( ssl_context *ssl,
4971 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4972 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004973{
Paul Bakker0a597072012-09-25 21:55:46 +00004974 ssl->f_get_cache = f_get_cache;
4975 ssl->p_get_cache = p_get_cache;
4976 ssl->f_set_cache = f_set_cache;
4977 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004978}
4979
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004980int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004981{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004982 int ret;
4983
4984 if( ssl == NULL ||
4985 session == NULL ||
4986 ssl->session_negotiate == NULL ||
4987 ssl->endpoint != SSL_IS_CLIENT )
4988 {
4989 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4990 }
4991
4992 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4993 return( ret );
4994
Paul Bakker0a597072012-09-25 21:55:46 +00004995 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004996
4997 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004998}
4999
Paul Bakkerb68cad62012-08-23 08:34:18 +00005000void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005001{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005002 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5003 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5004 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5005 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5006}
5007
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005008void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5009 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005010 int major, int minor )
5011{
5012 if( major != SSL_MAJOR_VERSION_3 )
5013 return;
5014
5015 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5016 return;
5017
5018 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005019}
5020
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005021#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005022/* Add a new (empty) key_cert entry an return a pointer to it */
5023static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5024{
5025 ssl_key_cert *key_cert, *last;
5026
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005027 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5028 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005029 return( NULL );
5030
5031 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5032
5033 /* Append the new key_cert to the (possibly empty) current list */
5034 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005035 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005036 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005037 if( ssl->handshake != NULL )
5038 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005039 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005040 else
5041 {
5042 last = ssl->key_cert;
5043 while( last->next != NULL )
5044 last = last->next;
5045 last->next = key_cert;
5046 }
5047
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005048 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005049}
5050
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005051void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005052 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005053{
5054 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005055 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005056 ssl->peer_cn = peer_cn;
5057}
5058
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005059int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005060 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005061{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005062 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5063
5064 if( key_cert == NULL )
5065 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5066
5067 key_cert->cert = own_cert;
5068 key_cert->key = pk_key;
5069
5070 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005071}
5072
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005073#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005074int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005075 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005076{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005077 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005078 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005079
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005080 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005081 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5082
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005083 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5084 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005085 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005086
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005087 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005088
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005089 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005090 if( ret != 0 )
5091 return( ret );
5092
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005093 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005094 return( ret );
5095
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005096 key_cert->cert = own_cert;
5097 key_cert->key_own_alloc = 1;
5098
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005099 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005100}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005101#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005102
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005103int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005104 void *rsa_key,
5105 rsa_decrypt_func rsa_decrypt,
5106 rsa_sign_func rsa_sign,
5107 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005108{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005109 int ret;
5110 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005111
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005112 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005113 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5114
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005115 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5116 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005118
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005119 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005120
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005121 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5122 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5123 return( ret );
5124
5125 key_cert->cert = own_cert;
5126 key_cert->key_own_alloc = 1;
5127
5128 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005129}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005130#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005131
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005132#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005133int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5134 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005135{
Paul Bakker6db455e2013-09-18 17:29:31 +02005136 if( psk == NULL || psk_identity == NULL )
5137 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5138
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005139 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005140 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5141
Paul Bakker6db455e2013-09-18 17:29:31 +02005142 if( ssl->psk != NULL )
5143 {
5144 polarssl_free( ssl->psk );
5145 polarssl_free( ssl->psk_identity );
5146 }
5147
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005148 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005149 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005150
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005151 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005152 ssl->psk_identity = (unsigned char *)
5153 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005154
5155 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5156 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5157
5158 memcpy( ssl->psk, psk, ssl->psk_len );
5159 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005160
5161 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005162}
5163
5164void ssl_set_psk_cb( ssl_context *ssl,
5165 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5166 size_t),
5167 void *p_psk )
5168{
5169 ssl->f_psk = f_psk;
5170 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005171}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005172#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005173
Paul Bakker48916f92012-09-16 19:57:18 +00005174#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005175int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005176{
5177 int ret;
5178
Paul Bakker48916f92012-09-16 19:57:18 +00005179 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005180 {
5181 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5182 return( ret );
5183 }
5184
Paul Bakker48916f92012-09-16 19:57:18 +00005185 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005186 {
5187 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5188 return( ret );
5189 }
5190
5191 return( 0 );
5192}
5193
Paul Bakker1b57b062011-01-06 15:48:19 +00005194int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5195{
5196 int ret;
5197
Paul Bakker66d5d072014-06-17 16:39:18 +02005198 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005199 {
5200 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5201 return( ret );
5202 }
5203
Paul Bakker66d5d072014-06-17 16:39:18 +02005204 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005205 {
5206 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5207 return( ret );
5208 }
5209
5210 return( 0 );
5211}
Paul Bakker48916f92012-09-16 19:57:18 +00005212#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005213
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005214#if defined(POLARSSL_SSL_SET_CURVES)
5215/*
5216 * Set the allowed elliptic curves
5217 */
5218void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5219{
5220 ssl->curve_list = curve_list;
5221}
5222#endif
5223
Paul Bakker0be444a2013-08-27 21:55:01 +02005224#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005225int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005226{
5227 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005228 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005229
5230 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005231
5232 if( ssl->hostname_len + 1 == 0 )
5233 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5234
Paul Bakker6e339b52013-07-03 13:37:05 +02005235 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005236
Paul Bakkerb15b8512012-01-13 13:44:06 +00005237 if( ssl->hostname == NULL )
5238 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5239
Paul Bakker3c2122f2013-06-24 19:03:14 +02005240 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005241 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005242
Paul Bakker40ea7de2009-05-03 10:18:48 +00005243 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005244
5245 return( 0 );
5246}
5247
Paul Bakker5701cdc2012-09-27 21:49:42 +00005248void ssl_set_sni( ssl_context *ssl,
5249 int (*f_sni)(void *, ssl_context *,
5250 const unsigned char *, size_t),
5251 void *p_sni )
5252{
5253 ssl->f_sni = f_sni;
5254 ssl->p_sni = p_sni;
5255}
Paul Bakker0be444a2013-08-27 21:55:01 +02005256#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005257
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005258#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005259int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005260{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005261 size_t cur_len, tot_len;
5262 const char **p;
5263
5264 /*
5265 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5266 * truncated". Check lengths now rather than later.
5267 */
5268 tot_len = 0;
5269 for( p = protos; *p != NULL; p++ )
5270 {
5271 cur_len = strlen( *p );
5272 tot_len += cur_len;
5273
5274 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5275 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5276 }
5277
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005278 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005279
5280 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005281}
5282
5283const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5284{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005285 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005286}
5287#endif /* POLARSSL_SSL_ALPN */
5288
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005289static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005290{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005291 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5292 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5293 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5294 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005295 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005296 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005297 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005298
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005299 return( 0 );
5300}
5301
5302int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5303{
5304 if( ssl_check_version( ssl, major, minor ) != 0 )
5305 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5306
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005307 ssl->max_major_ver = major;
5308 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005309
5310 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005311}
5312
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005313int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005314{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005315 if( ssl_check_version( ssl, major, minor ) != 0 )
5316 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005317
5318 ssl->min_major_ver = major;
5319 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005320
5321 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005322}
5323
Paul Bakker05decb22013-08-15 13:33:48 +02005324#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005325int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5326{
Paul Bakker77e257e2013-12-16 15:29:52 +01005327 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005328 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005329 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005330 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005331 }
5332
5333 ssl->mfl_code = mfl_code;
5334
5335 return( 0 );
5336}
Paul Bakker05decb22013-08-15 13:33:48 +02005337#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005338
Paul Bakker1f2bc622013-08-15 13:45:55 +02005339#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005340int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005341{
5342 if( ssl->endpoint != SSL_IS_CLIENT )
5343 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5344
Paul Bakker8c1ede62013-07-19 14:14:37 +02005345 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005346
5347 return( 0 );
5348}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005349#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005350
Paul Bakker48916f92012-09-16 19:57:18 +00005351void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5352{
5353 ssl->disable_renegotiation = renegotiation;
5354}
5355
5356void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5357{
5358 ssl->allow_legacy_renegotiation = allow_legacy;
5359}
5360
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005361void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5362{
5363 ssl->renego_max_records = max_records;
5364}
5365
Paul Bakkera503a632013-08-14 13:48:06 +02005366#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005367int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5368{
5369 ssl->session_tickets = use_tickets;
5370
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005371 if( ssl->endpoint == SSL_IS_CLIENT )
5372 return( 0 );
5373
5374 if( ssl->f_rng == NULL )
5375 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5376
5377 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005378}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005379
5380void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5381{
5382 ssl->ticket_lifetime = lifetime;
5383}
Paul Bakkera503a632013-08-14 13:48:06 +02005384#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005385
Paul Bakker5121ce52009-01-03 21:22:43 +00005386/*
5387 * SSL get accessors
5388 */
Paul Bakker23986e52011-04-24 08:57:21 +00005389size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005390{
5391 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5392}
5393
Paul Bakkerff60ee62010-03-16 21:09:09 +00005394int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005395{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005396 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005397}
5398
Paul Bakkere3166ce2011-01-27 17:40:50 +00005399const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005400{
Paul Bakker926c8e42013-03-06 10:23:34 +01005401 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005402 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005403
Paul Bakkere3166ce2011-01-27 17:40:50 +00005404 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005405}
5406
Paul Bakker43ca69c2011-01-15 17:35:19 +00005407const char *ssl_get_version( const ssl_context *ssl )
5408{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005409#if defined(POLARSSL_SSL_PROTO_DTLS)
5410 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5411 {
5412 switch( ssl->minor_ver )
5413 {
5414 case SSL_MINOR_VERSION_2:
5415 return( "DTLSv1.0" );
5416
5417 case SSL_MINOR_VERSION_3:
5418 return( "DTLSv1.2" );
5419
5420 default:
5421 return( "unknown (DTLS)" );
5422 }
5423 }
5424#endif
5425
Paul Bakker43ca69c2011-01-15 17:35:19 +00005426 switch( ssl->minor_ver )
5427 {
5428 case SSL_MINOR_VERSION_0:
5429 return( "SSLv3.0" );
5430
5431 case SSL_MINOR_VERSION_1:
5432 return( "TLSv1.0" );
5433
5434 case SSL_MINOR_VERSION_2:
5435 return( "TLSv1.1" );
5436
Paul Bakker1ef83d62012-04-11 12:09:53 +00005437 case SSL_MINOR_VERSION_3:
5438 return( "TLSv1.2" );
5439
Paul Bakker43ca69c2011-01-15 17:35:19 +00005440 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005441 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005442 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005443}
5444
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005445#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005446const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005447{
5448 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005449 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005450
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005451 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005452}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005453#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005454
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005455int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5456{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005457 if( ssl == NULL ||
5458 dst == NULL ||
5459 ssl->session == NULL ||
5460 ssl->endpoint != SSL_IS_CLIENT )
5461 {
5462 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5463 }
5464
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005465 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005466}
5467
Paul Bakker5121ce52009-01-03 21:22:43 +00005468/*
Paul Bakker1961b702013-01-25 14:49:24 +01005469 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005470 */
Paul Bakker1961b702013-01-25 14:49:24 +01005471int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005472{
Paul Bakker40e46942009-01-03 21:51:57 +00005473 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005474
Paul Bakker40e46942009-01-03 21:51:57 +00005475#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005477 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005478#endif
5479
Paul Bakker40e46942009-01-03 21:51:57 +00005480#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005481 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005482 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005483#endif
5484
Paul Bakker1961b702013-01-25 14:49:24 +01005485 return( ret );
5486}
5487
5488/*
5489 * Perform the SSL handshake
5490 */
5491int ssl_handshake( ssl_context *ssl )
5492{
5493 int ret = 0;
5494
5495 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5496
5497 while( ssl->state != SSL_HANDSHAKE_OVER )
5498 {
5499 ret = ssl_handshake_step( ssl );
5500
5501 if( ret != 0 )
5502 break;
5503 }
5504
Paul Bakker5121ce52009-01-03 21:22:43 +00005505 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5506
5507 return( ret );
5508}
5509
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005510#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005511/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005512 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005513 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005514static int ssl_write_hello_request( ssl_context *ssl )
5515{
5516 int ret;
5517
5518 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5519
5520 ssl->out_msglen = 4;
5521 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5522 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5523
5524 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5525 {
5526 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5527 return( ret );
5528 }
5529
5530 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5531
5532 return( 0 );
5533}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005534#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005535
5536/*
5537 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005538 * - any side: calling ssl_renegotiate(),
5539 * - client: receiving a HelloRequest during ssl_read(),
5540 * - server: receiving any handshake message on server during ssl_read() after
5541 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005542 * If the handshake doesn't complete due to waiting for I/O, it will continue
5543 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005544 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005545static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005546{
5547 int ret;
5548
5549 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5550
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005551 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5552 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005553
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005554 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5555 * the ServerHello will have message_seq = 1" */
5556#if defined(POLARSSL_SSL_PROTO_DTLS)
5557 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005558 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5559 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005560 if( ssl->endpoint == SSL_IS_SERVER )
5561 ssl->handshake->out_msg_seq = 1;
5562 else
5563 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005564 }
5565#endif
5566
Paul Bakker48916f92012-09-16 19:57:18 +00005567 ssl->state = SSL_HELLO_REQUEST;
5568 ssl->renegotiation = SSL_RENEGOTIATION;
5569
Paul Bakker48916f92012-09-16 19:57:18 +00005570 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5571 {
5572 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5573 return( ret );
5574 }
5575
5576 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5577
5578 return( 0 );
5579}
5580
5581/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005582 * Renegotiate current connection on client,
5583 * or request renegotiation on server
5584 */
5585int ssl_renegotiate( ssl_context *ssl )
5586{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005587 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005588
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005589#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005590 /* On server, just send the request */
5591 if( ssl->endpoint == SSL_IS_SERVER )
5592 {
5593 if( ssl->state != SSL_HANDSHAKE_OVER )
5594 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5595
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005596 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5597
5598 /* Did we already try/start sending HelloRequest? */
5599 if( ssl->out_left != 0 )
5600 return( ssl_flush_output( ssl ) );
5601
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005602 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005603 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005604#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005605
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005606#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005607 /*
5608 * On client, either start the renegotiation process or,
5609 * if already in progress, continue the handshake
5610 */
5611 if( ssl->renegotiation != SSL_RENEGOTIATION )
5612 {
5613 if( ssl->state != SSL_HANDSHAKE_OVER )
5614 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5615
5616 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5617 {
5618 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5619 return( ret );
5620 }
5621 }
5622 else
5623 {
5624 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5625 {
5626 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5627 return( ret );
5628 }
5629 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005630#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005631
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005632 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005633}
5634
5635/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005636 * Receive application data decrypted from the SSL layer
5637 */
Paul Bakker23986e52011-04-24 08:57:21 +00005638int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005639{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005640 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005641 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005642
5643 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5644
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005645#if defined(POLARSSL_SSL_PROTO_DTLS)
5646 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5647 ssl->handshake != NULL &&
5648 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5649 {
5650 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5651 return( ret );
5652
5653 if( ( ret = ssl_resend( ssl ) ) != 0 )
5654 return( ret );
5655 }
5656#endif
5657
Paul Bakker5121ce52009-01-03 21:22:43 +00005658 if( ssl->state != SSL_HANDSHAKE_OVER )
5659 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005660 ret = ssl_handshake( ssl );
5661 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5662 {
5663 record_read = 1;
5664 }
5665 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 {
5667 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5668 return( ret );
5669 }
5670 }
5671
5672 if( ssl->in_offt == NULL )
5673 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005674 /* Start timer if not already running */
5675 if( ssl->time_limit == 0 )
5676 ssl_set_timer( ssl, ssl->read_timeout );
5677
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005678 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005679 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005680 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5681 {
5682 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5683 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005684
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005685 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5686 return( ret );
5687 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005688 }
5689
5690 if( ssl->in_msglen == 0 &&
5691 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5692 {
5693 /*
5694 * OpenSSL sends empty messages to randomize the IV
5695 */
5696 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5697 {
Paul Bakker831a7552011-05-18 13:32:51 +00005698 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5699 return( 0 );
5700
Paul Bakker5121ce52009-01-03 21:22:43 +00005701 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5702 return( ret );
5703 }
5704 }
5705
Paul Bakker48916f92012-09-16 19:57:18 +00005706 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5707 {
5708 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5709
5710 if( ssl->endpoint == SSL_IS_CLIENT &&
5711 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005712 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005713 {
5714 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005715
5716 /* With DTLS, drop the packet (probably from last handshake) */
5717#if defined(POLARSSL_SSL_PROTO_DTLS)
5718 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5719 return( POLARSSL_ERR_NET_WANT_READ );
5720#endif
5721 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5722 }
5723
5724 if( ssl->endpoint == SSL_IS_SERVER &&
5725 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5726 {
5727 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5728
5729 /* With DTLS, drop the packet (probably from last handshake) */
5730#if defined(POLARSSL_SSL_PROTO_DTLS)
5731 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5732 return( POLARSSL_ERR_NET_WANT_READ );
5733#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005734 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5735 }
5736
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005737 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5738 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005739 ssl->allow_legacy_renegotiation ==
5740 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005741 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005742 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005743
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005744#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005745 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005746 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005747 /*
5748 * SSLv3 does not have a "no_renegotiation" alert
5749 */
5750 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5751 return( ret );
5752 }
5753 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005754#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005755#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5756 defined(POLARSSL_SSL_PROTO_TLS1_2)
5757 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005758 {
5759 if( ( ret = ssl_send_alert_message( ssl,
5760 SSL_ALERT_LEVEL_WARNING,
5761 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5762 {
5763 return( ret );
5764 }
Paul Bakker48916f92012-09-16 19:57:18 +00005765 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005766 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005767#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5768 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005769 {
5770 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005771 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005772 }
Paul Bakker48916f92012-09-16 19:57:18 +00005773 }
5774 else
5775 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005776#if defined(POLARSSL_SSL_PROTO_DTLS)
5777 /* DTLS clients need to know renego is server-initiated */
5778 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5779 ssl->endpoint == SSL_IS_CLIENT )
5780 {
5781 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5782 }
5783#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005784 ret = ssl_start_renegotiation( ssl );
5785 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5786 {
5787 record_read = 1;
5788 }
5789 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005790 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005791 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005792 return( ret );
5793 }
Paul Bakker48916f92012-09-16 19:57:18 +00005794 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005795
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005796 /* If a non-handshake record was read during renego, fallthrough,
5797 * else tell the user they should call ssl_read() again */
5798 if( ! record_read )
5799 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005800 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005801 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5802 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005803 ssl->renego_records_seen++;
5804
5805 if( ssl->renego_max_records >= 0 &&
5806 ssl->renego_records_seen > ssl->renego_max_records )
5807 {
5808 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5809 "but not honored by client" ) );
5810 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5811 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005812 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005813
5814 /* Fatal and closure alerts handled by ssl_read_record() */
5815 if( ssl->in_msgtype == SSL_MSG_ALERT )
5816 {
5817 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5818 return( POLARSSL_ERR_NET_WANT_READ );
5819 }
5820
5821 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005822 {
5823 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005824 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
5827 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005828
5829 /* We're going to return something now, cancel timer */
5830 ssl_set_timer( ssl, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005831 }
5832
5833 n = ( len < ssl->in_msglen )
5834 ? len : ssl->in_msglen;
5835
5836 memcpy( buf, ssl->in_offt, n );
5837 ssl->in_msglen -= n;
5838
5839 if( ssl->in_msglen == 0 )
5840 /* all bytes consumed */
5841 ssl->in_offt = NULL;
5842 else
5843 /* more data available */
5844 ssl->in_offt += n;
5845
5846 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5847
Paul Bakker23986e52011-04-24 08:57:21 +00005848 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005849}
5850
5851/*
5852 * Send application data to be encrypted by the SSL layer
5853 */
Paul Bakker23986e52011-04-24 08:57:21 +00005854int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005855{
Paul Bakker23986e52011-04-24 08:57:21 +00005856 int ret;
5857 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005858 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005859
5860 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5861
5862 if( ssl->state != SSL_HANDSHAKE_OVER )
5863 {
5864 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5865 {
5866 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5867 return( ret );
5868 }
5869 }
5870
Paul Bakker05decb22013-08-15 13:33:48 +02005871#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005872 /*
5873 * Assume mfl_code is correct since it was checked when set
5874 */
5875 max_len = mfl_code_to_length[ssl->mfl_code];
5876
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005877 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005878 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005879 */
5880 if( ssl->session_out != NULL &&
5881 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5882 {
5883 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5884 }
Paul Bakker05decb22013-08-15 13:33:48 +02005885#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005886
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005887 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005888
Paul Bakker5121ce52009-01-03 21:22:43 +00005889 if( ssl->out_left != 0 )
5890 {
5891 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5892 {
5893 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5894 return( ret );
5895 }
5896 }
Paul Bakker887bd502011-06-08 13:10:54 +00005897 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005898 {
Paul Bakker887bd502011-06-08 13:10:54 +00005899 ssl->out_msglen = n;
5900 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5901 memcpy( ssl->out_msg, buf, n );
5902
5903 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5904 {
5905 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5906 return( ret );
5907 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005908 }
5909
5910 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5911
Paul Bakker23986e52011-04-24 08:57:21 +00005912 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005913}
5914
5915/*
5916 * Notify the peer that the connection is being closed
5917 */
5918int ssl_close_notify( ssl_context *ssl )
5919{
5920 int ret;
5921
5922 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5923
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005924 if( ssl->out_left != 0 )
5925 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005926
5927 if( ssl->state == SSL_HANDSHAKE_OVER )
5928 {
Paul Bakker48916f92012-09-16 19:57:18 +00005929 if( ( ret = ssl_send_alert_message( ssl,
5930 SSL_ALERT_LEVEL_WARNING,
5931 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005932 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005933 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005934 return( ret );
5935 }
5936 }
5937
5938 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5939
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005940 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005941}
5942
Paul Bakker48916f92012-09-16 19:57:18 +00005943void ssl_transform_free( ssl_transform *transform )
5944{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005945 if( transform == NULL )
5946 return;
5947
Paul Bakker48916f92012-09-16 19:57:18 +00005948#if defined(POLARSSL_ZLIB_SUPPORT)
5949 deflateEnd( &transform->ctx_deflate );
5950 inflateEnd( &transform->ctx_inflate );
5951#endif
5952
Paul Bakker84bbeb52014-07-01 14:53:22 +02005953 cipher_free( &transform->cipher_ctx_enc );
5954 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005955
Paul Bakker84bbeb52014-07-01 14:53:22 +02005956 md_free( &transform->md_ctx_enc );
5957 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005958
Paul Bakker34617722014-06-13 17:20:13 +02005959 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005960}
5961
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005962#if defined(POLARSSL_X509_CRT_PARSE_C)
5963static void ssl_key_cert_free( ssl_key_cert *key_cert )
5964{
5965 ssl_key_cert *cur = key_cert, *next;
5966
5967 while( cur != NULL )
5968 {
5969 next = cur->next;
5970
5971 if( cur->key_own_alloc )
5972 {
5973 pk_free( cur->key );
5974 polarssl_free( cur->key );
5975 }
5976 polarssl_free( cur );
5977
5978 cur = next;
5979 }
5980}
5981#endif /* POLARSSL_X509_CRT_PARSE_C */
5982
Paul Bakker48916f92012-09-16 19:57:18 +00005983void ssl_handshake_free( ssl_handshake_params *handshake )
5984{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005985 if( handshake == NULL )
5986 return;
5987
Paul Bakker48916f92012-09-16 19:57:18 +00005988#if defined(POLARSSL_DHM_C)
5989 dhm_free( &handshake->dhm_ctx );
5990#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005991#if defined(POLARSSL_ECDH_C)
5992 ecdh_free( &handshake->ecdh_ctx );
5993#endif
5994
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005995#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005996 /* explicit void pointer cast for buggy MS compiler */
5997 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005998#endif
5999
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006000#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6001 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6002 /*
6003 * Free only the linked list wrapper, not the keys themselves
6004 * since the belong to the SNI callback
6005 */
6006 if( handshake->sni_key_cert != NULL )
6007 {
6008 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6009
6010 while( cur != NULL )
6011 {
6012 next = cur->next;
6013 polarssl_free( cur );
6014 cur = next;
6015 }
6016 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006017#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006018
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006019#if defined(POLARSSL_SSL_PROTO_DTLS)
6020 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006021 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006022 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006023#endif
6024
Paul Bakker34617722014-06-13 17:20:13 +02006025 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006026}
6027
6028void ssl_session_free( ssl_session *session )
6029{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006030 if( session == NULL )
6031 return;
6032
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006033#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006034 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006035 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006036 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006037 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006038 }
Paul Bakkered27a042013-04-18 22:46:23 +02006039#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006040
Paul Bakkera503a632013-08-14 13:48:06 +02006041#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006042 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006043#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006044
Paul Bakker34617722014-06-13 17:20:13 +02006045 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006046}
6047
Paul Bakker5121ce52009-01-03 21:22:43 +00006048/*
6049 * Free an SSL context
6050 */
6051void ssl_free( ssl_context *ssl )
6052{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006053 if( ssl == NULL )
6054 return;
6055
Paul Bakker5121ce52009-01-03 21:22:43 +00006056 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6057
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006058 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006059 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006060 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6061 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 }
6063
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006064 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006065 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006066 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6067 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 }
6069
Paul Bakker16770332013-10-11 09:59:44 +02006070#if defined(POLARSSL_ZLIB_SUPPORT)
6071 if( ssl->compress_buf != NULL )
6072 {
Paul Bakker34617722014-06-13 17:20:13 +02006073 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006074 polarssl_free( ssl->compress_buf );
6075 }
6076#endif
6077
Paul Bakker40e46942009-01-03 21:51:57 +00006078#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006079 mpi_free( &ssl->dhm_P );
6080 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006081#endif
6082
Paul Bakker48916f92012-09-16 19:57:18 +00006083 if( ssl->transform )
6084 {
6085 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006086 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006087 }
6088
6089 if( ssl->handshake )
6090 {
6091 ssl_handshake_free( ssl->handshake );
6092 ssl_transform_free( ssl->transform_negotiate );
6093 ssl_session_free( ssl->session_negotiate );
6094
Paul Bakker6e339b52013-07-03 13:37:05 +02006095 polarssl_free( ssl->handshake );
6096 polarssl_free( ssl->transform_negotiate );
6097 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006098 }
6099
Paul Bakkerc0463502013-02-14 11:19:38 +01006100 if( ssl->session )
6101 {
6102 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006103 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006104 }
6105
Paul Bakkera503a632013-08-14 13:48:06 +02006106#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006107 if( ssl->ticket_keys )
6108 {
6109 ssl_ticket_keys_free( ssl->ticket_keys );
6110 polarssl_free( ssl->ticket_keys );
6111 }
Paul Bakkera503a632013-08-14 13:48:06 +02006112#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006113
Paul Bakker0be444a2013-08-27 21:55:01 +02006114#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006115 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 {
Paul Bakker34617722014-06-13 17:20:13 +02006117 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006118 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 ssl->hostname_len = 0;
6120 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006121#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006122
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006123#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006124 if( ssl->psk != NULL )
6125 {
Paul Bakker34617722014-06-13 17:20:13 +02006126 polarssl_zeroize( ssl->psk, ssl->psk_len );
6127 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006128 polarssl_free( ssl->psk );
6129 polarssl_free( ssl->psk_identity );
6130 ssl->psk_len = 0;
6131 ssl->psk_identity_len = 0;
6132 }
6133#endif
6134
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006135#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006136 ssl_key_cert_free( ssl->key_cert );
6137#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006138
Paul Bakker05ef8352012-05-08 09:17:57 +00006139#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6140 if( ssl_hw_record_finish != NULL )
6141 {
6142 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6143 ssl_hw_record_finish( ssl );
6144 }
6145#endif
6146
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006147#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006148 polarssl_free( ssl->cli_id );
6149#endif
6150
Paul Bakker5121ce52009-01-03 21:22:43 +00006151 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006152
Paul Bakker86f04f42013-02-14 11:20:09 +01006153 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006154 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006155}
6156
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006157#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006158/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006159 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006160 */
6161unsigned char ssl_sig_from_pk( pk_context *pk )
6162{
6163#if defined(POLARSSL_RSA_C)
6164 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6165 return( SSL_SIG_RSA );
6166#endif
6167#if defined(POLARSSL_ECDSA_C)
6168 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6169 return( SSL_SIG_ECDSA );
6170#endif
6171 return( SSL_SIG_ANON );
6172}
6173
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006174pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6175{
6176 switch( sig )
6177 {
6178#if defined(POLARSSL_RSA_C)
6179 case SSL_SIG_RSA:
6180 return( POLARSSL_PK_RSA );
6181#endif
6182#if defined(POLARSSL_ECDSA_C)
6183 case SSL_SIG_ECDSA:
6184 return( POLARSSL_PK_ECDSA );
6185#endif
6186 default:
6187 return( POLARSSL_PK_NONE );
6188 }
6189}
Paul Bakker9af723c2014-05-01 13:03:14 +02006190#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006191
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006192/*
6193 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6194 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006195md_type_t ssl_md_alg_from_hash( unsigned char hash )
6196{
6197 switch( hash )
6198 {
6199#if defined(POLARSSL_MD5_C)
6200 case SSL_HASH_MD5:
6201 return( POLARSSL_MD_MD5 );
6202#endif
6203#if defined(POLARSSL_SHA1_C)
6204 case SSL_HASH_SHA1:
6205 return( POLARSSL_MD_SHA1 );
6206#endif
6207#if defined(POLARSSL_SHA256_C)
6208 case SSL_HASH_SHA224:
6209 return( POLARSSL_MD_SHA224 );
6210 case SSL_HASH_SHA256:
6211 return( POLARSSL_MD_SHA256 );
6212#endif
6213#if defined(POLARSSL_SHA512_C)
6214 case SSL_HASH_SHA384:
6215 return( POLARSSL_MD_SHA384 );
6216 case SSL_HASH_SHA512:
6217 return( POLARSSL_MD_SHA512 );
6218#endif
6219 default:
6220 return( POLARSSL_MD_NONE );
6221 }
6222}
6223
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006224#if defined(POLARSSL_SSL_SET_CURVES)
6225/*
6226 * Check is a curve proposed by the peer is in our list.
6227 * Return 1 if we're willing to use it, 0 otherwise.
6228 */
6229int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6230{
6231 const ecp_group_id *gid;
6232
6233 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6234 if( *gid == grp_id )
6235 return( 1 );
6236
6237 return( 0 );
6238}
Paul Bakker9af723c2014-05-01 13:03:14 +02006239#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006240
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006241#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006242int ssl_check_cert_usage( const x509_crt *cert,
6243 const ssl_ciphersuite_t *ciphersuite,
6244 int cert_endpoint )
6245{
6246#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6247 int usage = 0;
6248#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006249#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6250 const char *ext_oid;
6251 size_t ext_len;
6252#endif
6253
6254#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6255 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6256 ((void) cert);
6257 ((void) cert_endpoint);
6258#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006259
6260#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6261 if( cert_endpoint == SSL_IS_SERVER )
6262 {
6263 /* Server part of the key exchange */
6264 switch( ciphersuite->key_exchange )
6265 {
6266 case POLARSSL_KEY_EXCHANGE_RSA:
6267 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6268 usage = KU_KEY_ENCIPHERMENT;
6269 break;
6270
6271 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6272 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6273 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6274 usage = KU_DIGITAL_SIGNATURE;
6275 break;
6276
6277 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6278 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6279 usage = KU_KEY_AGREEMENT;
6280 break;
6281
6282 /* Don't use default: we want warnings when adding new values */
6283 case POLARSSL_KEY_EXCHANGE_NONE:
6284 case POLARSSL_KEY_EXCHANGE_PSK:
6285 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6286 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6287 usage = 0;
6288 }
6289 }
6290 else
6291 {
6292 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6293 usage = KU_DIGITAL_SIGNATURE;
6294 }
6295
6296 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6297 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006298#else
6299 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006300#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6301
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006302#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6303 if( cert_endpoint == SSL_IS_SERVER )
6304 {
6305 ext_oid = OID_SERVER_AUTH;
6306 ext_len = OID_SIZE( OID_SERVER_AUTH );
6307 }
6308 else
6309 {
6310 ext_oid = OID_CLIENT_AUTH;
6311 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6312 }
6313
6314 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6315 return( -1 );
6316#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6317
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006318 return( 0 );
6319}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006320#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006321
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006322/*
6323 * Convert version numbers to/from wire format
6324 * and, for DTLS, to/from TLS equivalent.
6325 *
6326 * For TLS this is the identity.
6327 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6328 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6329 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6330 */
6331void ssl_write_version( int major, int minor, int transport,
6332 unsigned char ver[2] )
6333{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006334#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006335 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006336 {
6337 if( minor == SSL_MINOR_VERSION_2 )
6338 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6339
6340 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6341 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6342 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006343 else
6344#else
6345 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006346#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006347 {
6348 ver[0] = (unsigned char) major;
6349 ver[1] = (unsigned char) minor;
6350 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006351}
6352
6353void ssl_read_version( int *major, int *minor, int transport,
6354 const unsigned char ver[2] )
6355{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006356#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006357 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006358 {
6359 *major = 255 - ver[0] + 2;
6360 *minor = 255 - ver[1] + 1;
6361
6362 if( *minor == SSL_MINOR_VERSION_1 )
6363 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6364 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006365 else
6366#else
6367 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006368#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006369 {
6370 *major = ver[0];
6371 *minor = ver[1];
6372 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006373}
6374
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006375#endif /* POLARSSL_SSL_TLS_C */