blob: c6904b0c1273ef0ebb9d3b4e14ec171c0f33fc20 [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
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001990 SSL_DEBUG_MSG( 3, ( "current timer: %u", ssl->time_limit ) );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001991
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02001992 /*
1993 * Don't even try to read if time's out already.
1994 * This avoids by-passing the timer when repeatedly receiving messages
1995 * that will end up being dropped.
1996 */
1997 if( ssl_check_timer( ssl ) != 0 )
1998 ret = POLARSSL_ERR_NET_TIMEOUT;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02001999 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002000 {
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002001 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
2002
2003 if( ssl->state != SSL_HANDSHAKE_OVER )
2004 timeout = ssl->handshake->retransmit_timeout;
2005 else
2006 timeout = ssl->read_timeout;
2007
2008 SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
2009
2010 if( ssl->f_recv_timeout != NULL && timeout != 0 )
2011 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
2012 timeout );
2013 else
2014 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
2015
2016 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
2017
2018 if( ret == 0 )
2019 return( POLARSSL_ERR_SSL_CONN_EOF );
2020 }
2021
2022 if( ret == POLARSSL_ERR_NET_TIMEOUT )
2023 {
2024 SSL_DEBUG_MSG( 2, ( "timeout" ) );
2025 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002026
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002027 if( ssl->state != SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002028 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02002029 if( ssl_double_retransmit_timeout( ssl ) != 0 )
2030 {
2031 SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
2032 return( POLARSSL_ERR_NET_TIMEOUT );
2033 }
2034
2035 if( ( ret = ssl_resend( ssl ) ) != 0 )
2036 {
2037 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2038 return( ret );
2039 }
2040
2041 return( POLARSSL_ERR_NET_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002042 }
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002043 }
2044
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 if( ret < 0 )
2046 return( ret );
2047
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002048 ssl->in_left = ret;
2049 }
2050 else
2051#endif
2052 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002053 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2054 ssl->in_left, nb_want ) );
2055
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002056 while( ssl->in_left < nb_want )
2057 {
2058 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002059 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002060
2061 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2062 ssl->in_left, nb_want ) );
2063 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2064
2065 if( ret == 0 )
2066 return( POLARSSL_ERR_SSL_CONN_EOF );
2067
2068 if( ret < 0 )
2069 return( ret );
2070
2071 ssl->in_left += ret;
2072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
2074
2075 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2076
2077 return( 0 );
2078}
2079
2080/*
2081 * Flush any data not yet written
2082 */
2083int ssl_flush_output( ssl_context *ssl )
2084{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002085 int ret;
2086 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002087
2088 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2089
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002090 if( ssl->f_send == NULL )
2091 {
2092 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2093 "or ssl_set_bio_timeout()" ) );
2094 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2095 }
2096
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002097 /* Avoid incrementing counter if data is flushed */
2098 if( ssl->out_left == 0 )
2099 {
2100 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2101 return( 0 );
2102 }
2103
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 while( ssl->out_left > 0 )
2105 {
2106 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002107 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002108
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002109 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2110 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002111 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002112
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2114
2115 if( ret <= 0 )
2116 return( ret );
2117
2118 ssl->out_left -= ret;
2119 }
2120
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002121 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002122 if( ++ssl->out_ctr[i - 1] != 0 )
2123 break;
2124
2125 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002126 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002127 {
2128 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2129 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2130 }
2131
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2133
2134 return( 0 );
2135}
2136
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002137/*
2138 * Functions to handle the DTLS retransmission state machine
2139 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002140#if defined(POLARSSL_SSL_PROTO_DTLS)
2141/*
2142 * Append current handshake message to current outgoing flight
2143 */
2144static int ssl_flight_append( ssl_context *ssl )
2145{
2146 ssl_flight_item *msg;
2147
2148 /* Allocate space for current message */
2149 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2150 {
2151 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2152 sizeof( ssl_flight_item ) ) );
2153 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2154 }
2155
2156 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2157 {
2158 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2159 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2160 }
2161
2162 /* Copy current handshake message with headers */
2163 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2164 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002165 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002166 msg->next = NULL;
2167
2168 /* Append to the current flight */
2169 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002170 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002171 else
2172 {
2173 ssl_flight_item *cur = ssl->handshake->flight;
2174 while( cur->next != NULL )
2175 cur = cur->next;
2176 cur->next = msg;
2177 }
2178
2179 return( 0 );
2180}
2181
2182/*
2183 * Free the current flight of handshake messages
2184 */
2185static void ssl_flight_free( ssl_flight_item *flight )
2186{
2187 ssl_flight_item *cur = flight;
2188 ssl_flight_item *next;
2189
2190 while( cur != NULL )
2191 {
2192 next = cur->next;
2193
2194 polarssl_free( cur->p );
2195 polarssl_free( cur );
2196
2197 cur = next;
2198 }
2199}
2200
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002201#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2202static void ssl_dtls_replay_reset( ssl_context *ssl );
2203#endif
2204
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002205/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002206 * Swap transform_out and out_ctr with the alternative ones
2207 */
2208static void ssl_swap_epochs( ssl_context *ssl )
2209{
2210 ssl_transform *tmp_transform;
2211 unsigned char tmp_out_ctr[8];
2212
2213 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2214 {
2215 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2216 return;
2217 }
2218
2219 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2220
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002221 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002222 tmp_transform = ssl->transform_out;
2223 ssl->transform_out = ssl->handshake->alt_transform_out;
2224 ssl->handshake->alt_transform_out = tmp_transform;
2225
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002226 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002227 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2228 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2229 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002230
2231 /* Adjust to the newly activated transform */
2232 if( ssl->transform_out != NULL &&
2233 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2234 {
2235 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2236 ssl->transform_out->fixed_ivlen;
2237 }
2238 else
2239 ssl->out_msg = ssl->out_iv;
2240
2241#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2242 if( ssl_hw_record_activate != NULL )
2243 {
2244 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2245 {
2246 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2247 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2248 }
2249 }
2250#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002251}
2252
2253/*
2254 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002255 *
2256 * Need to remember the current message in case flush_output returns
2257 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002258 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002259 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002260int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002261{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002262 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002263
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002264 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2265 {
2266 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2267
2268 ssl->handshake->cur_msg = ssl->handshake->flight;
2269 ssl_swap_epochs( ssl );
2270
2271 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2272 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002273
2274 while( ssl->handshake->cur_msg != NULL )
2275 {
2276 int ret;
2277 ssl_flight_item *cur = ssl->handshake->cur_msg;
2278
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002279 /* Swap epochs before sending Finished: we can't do it after
2280 * sending ChangeCipherSpec, in case write returns WANT_READ.
2281 * Must be done before copying, may change out_msg pointer */
2282 if( cur->type == SSL_MSG_HANDSHAKE &&
2283 cur->p[0] == SSL_HS_FINISHED )
2284 {
2285 ssl_swap_epochs( ssl );
2286 }
2287
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002288 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002289 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002290 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002291
2292 ssl->handshake->cur_msg = cur->next;
2293
2294 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2295
2296 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2297 {
2298 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2299 return( ret );
2300 }
2301 }
2302
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002303 if( ssl->state == SSL_HANDSHAKE_OVER )
2304 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2305 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002306 {
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002307 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02002308 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
2309 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002310
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002311 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002312
2313 return( 0 );
2314}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002315
2316/*
2317 * To be called when the last message of an incoming flight is received.
2318 */
2319void ssl_recv_flight_completed( ssl_context *ssl )
2320{
2321 /* We won't need to resend that one any more */
2322 ssl_flight_free( ssl->handshake->flight );
2323 ssl->handshake->flight = NULL;
2324 ssl->handshake->cur_msg = NULL;
2325
2326 /* The next incoming flight will start with this msg_seq */
2327 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2328
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002329 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002330 ssl_set_timer( ssl, 0 );
2331
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002332 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2333 ssl->in_msg[0] == SSL_HS_FINISHED )
2334 {
2335 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2336 }
2337 else
2338 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2339}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002340
2341/*
2342 * To be called when the last message of an outgoing flight is send.
2343 */
2344void ssl_send_flight_completed( ssl_context *ssl )
2345{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02002346 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02002347 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002348
2349 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2350 ssl->in_msg[0] == SSL_HS_FINISHED )
2351 {
2352 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2353 }
2354 else
2355 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2356}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002357#endif /* POLARSSL_SSL_PROTO_DTLS */
2358
Paul Bakker5121ce52009-01-03 21:22:43 +00002359/*
2360 * Record layer functions
2361 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002362
2363/*
2364 * Write current record.
2365 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2366 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002367int ssl_write_record( ssl_context *ssl )
2368{
Paul Bakker05ef8352012-05-08 09:17:57 +00002369 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002370 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002371
2372 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2373
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002374#if defined(POLARSSL_SSL_PROTO_DTLS)
2375 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2376 ssl->handshake != NULL &&
2377 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2378 {
2379 ; /* Skip special handshake treatment when resending */
2380 }
2381 else
2382#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2384 {
2385 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2386 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2387 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2388
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002389 /*
2390 * DTLS has additional fields in the Handshake layer,
2391 * between the length field and the actual payload:
2392 * uint16 message_seq;
2393 * uint24 fragment_offset;
2394 * uint24 fragment_length;
2395 */
2396#if defined(POLARSSL_SSL_PROTO_DTLS)
2397 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2398 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002399 /* Make room for the additional DTLS fields */
2400 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002401 ssl->out_msglen += 8;
2402 len += 8;
2403
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002404 /* Write message_seq and update it, except for HelloRequest */
2405 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2406 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002407 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2408 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2409 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002410 }
2411 else
2412 {
2413 ssl->out_msg[4] = 0;
2414 ssl->out_msg[5] = 0;
2415 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002416
2417 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2418 memset( ssl->out_msg + 6, 0x00, 3 );
2419 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002420 }
2421#endif /* POLARSSL_SSL_PROTO_DTLS */
2422
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002423 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2424 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 }
2426
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002427 /* Save handshake and CCS messages for resending */
2428#if defined(POLARSSL_SSL_PROTO_DTLS)
2429 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2430 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002431 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002432 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2433 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2434 {
2435 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2436 {
2437 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2438 return( ret );
2439 }
2440 }
2441#endif
2442
Paul Bakker2770fbd2012-07-03 13:30:23 +00002443#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002444 if( ssl->transform_out != NULL &&
2445 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002446 {
2447 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2448 {
2449 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2450 return( ret );
2451 }
2452
2453 len = ssl->out_msglen;
2454 }
2455#endif /*POLARSSL_ZLIB_SUPPORT */
2456
Paul Bakker05ef8352012-05-08 09:17:57 +00002457#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002458 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002460 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Paul Bakker05ef8352012-05-08 09:17:57 +00002462 ret = ssl_hw_record_write( ssl );
2463 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2464 {
2465 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002466 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002467 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002468
2469 if( ret == 0 )
2470 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002471 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002472#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002473 if( !done )
2474 {
2475 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002476 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2477 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002478
2479 ssl->out_len[0] = (unsigned char)( len >> 8 );
2480 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002481
Paul Bakker48916f92012-09-16 19:57:18 +00002482 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002483 {
2484 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2485 {
2486 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2487 return( ret );
2488 }
2489
2490 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002491 ssl->out_len[0] = (unsigned char)( len >> 8 );
2492 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002493 }
2494
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002495 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002496
2497 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2498 "version = [%d:%d], msglen = %d",
2499 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002500 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002501
2502 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002503 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2507 {
2508 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2509 return( ret );
2510 }
2511
2512 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2513
2514 return( 0 );
2515}
2516
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002517#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002518/*
2519 * Mark bits in bitmask (used for DTLS HS reassembly)
2520 */
2521static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2522{
2523 unsigned int start_bits, end_bits;
2524
2525 start_bits = 8 - ( offset % 8 );
2526 if( start_bits != 8 )
2527 {
2528 size_t first_byte_idx = offset / 8;
2529
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002530 /* Special case */
2531 if( len <= start_bits )
2532 {
2533 for( ; len != 0; len-- )
2534 mask[first_byte_idx] |= 1 << ( start_bits - len );
2535
2536 /* Avoid potential issues with offset or len becoming invalid */
2537 return;
2538 }
2539
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002540 offset += start_bits; /* Now offset % 8 == 0 */
2541 len -= start_bits;
2542
2543 for( ; start_bits != 0; start_bits-- )
2544 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2545 }
2546
2547 end_bits = len % 8;
2548 if( end_bits != 0 )
2549 {
2550 size_t last_byte_idx = ( offset + len ) / 8;
2551
2552 len -= end_bits; /* Now len % 8 == 0 */
2553
2554 for( ; end_bits != 0; end_bits-- )
2555 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2556 }
2557
2558 memset( mask + offset / 8, 0xFF, len / 8 );
2559}
2560
2561/*
2562 * Check that bitmask is full
2563 */
2564static int ssl_bitmask_check( unsigned char *mask, size_t len )
2565{
2566 size_t i;
2567
2568 for( i = 0; i < len / 8; i++ )
2569 if( mask[i] != 0xFF )
2570 return( -1 );
2571
2572 for( i = 0; i < len % 8; i++ )
2573 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2574 return( -1 );
2575
2576 return( 0 );
2577}
2578
2579/*
2580 * Reassemble fragmented DTLS handshake messages.
2581 *
2582 * Use a temporary buffer for reassembly, divided in two parts:
2583 * - the first holds the reassembled message (including handshake header),
2584 * - the second holds a bitmask indicating which parts of the message
2585 * (excluding headers) have been received so far.
2586 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002587static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2588{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002589 unsigned char *msg, *bitmask;
2590 size_t frag_len, frag_off;
2591 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2592
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002593 if( ssl->handshake == NULL )
2594 {
2595 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2596 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2597 }
2598
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002599 /*
2600 * For first fragment, check size and allocate buffer
2601 */
2602 if( ssl->handshake->hs_msg == NULL )
2603 {
2604 size_t alloc_len;
2605
2606 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2607 msg_len ) );
2608
2609 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2610 {
2611 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2612 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2613 }
2614
2615 /* The bitmask needs one bit per byte of message excluding header */
2616 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2617
2618 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2619 if( ssl->handshake->hs_msg == NULL )
2620 {
2621 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2622 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2623 }
2624
2625 memset( ssl->handshake->hs_msg, 0, alloc_len );
2626
2627 /* Prepare final header: copy msg_type, length and message_seq,
2628 * then add standardised fragment_offset and fragment_length */
2629 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2630 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2631 memcpy( ssl->handshake->hs_msg + 9,
2632 ssl->handshake->hs_msg + 1, 3 );
2633 }
2634 else
2635 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002636 /* Make sure msg_type and length are consistent */
2637 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002638 {
2639 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2640 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2641 }
2642 }
2643
2644 msg = ssl->handshake->hs_msg + 12;
2645 bitmask = msg + msg_len;
2646
2647 /*
2648 * Check and copy current fragment
2649 */
2650 frag_off = ( ssl->in_msg[6] << 16 ) |
2651 ( ssl->in_msg[7] << 8 ) |
2652 ssl->in_msg[8];
2653 frag_len = ( ssl->in_msg[9] << 16 ) |
2654 ( ssl->in_msg[10] << 8 ) |
2655 ssl->in_msg[11];
2656
2657 if( frag_off + frag_len > msg_len )
2658 {
2659 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2660 frag_off, frag_len, msg_len ) );
2661 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2662 }
2663
2664 if( frag_len + 12 > ssl->in_msglen )
2665 {
2666 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2667 frag_len, ssl->in_msglen ) );
2668 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2669 }
2670
2671 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2672 frag_off, frag_len ) );
2673
2674 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2675 ssl_bitmask_set( bitmask, frag_off, frag_len );
2676
2677 /*
2678 * Do we have the complete message by now?
2679 * If yes, finalize it, else ask to read the next record.
2680 */
2681 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2682 {
2683 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2684 return( POLARSSL_ERR_NET_WANT_READ );
2685 }
2686
2687 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2688
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002689 if( ssl->in_left > ssl->next_record_offset )
2690 {
2691 /*
2692 * We've got more data in the buffer after the current record,
2693 * that we don't want to overwrite. Move it before writing the
2694 * reassembled message, and adjust in_left and next_record_offset.
2695 */
2696 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2697 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2698 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2699
2700 /* First compute and check new lengths */
2701 ssl->next_record_offset = new_remain - ssl->in_hdr;
2702 ssl->in_left = ssl->next_record_offset + remain_len;
2703
2704 if( ssl->in_left > SSL_BUFFER_LEN -
2705 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2706 {
2707 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2708 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2709 }
2710
2711 memmove( new_remain, cur_remain, remain_len );
2712 }
2713
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002714 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2715
2716 polarssl_free( ssl->handshake->hs_msg );
2717 ssl->handshake->hs_msg = NULL;
2718
2719 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2720 ssl->in_msg, ssl->in_hslen );
2721
2722 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002723}
2724#endif /* POLARSSL_SSL_PROTO_DTLS */
2725
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002726static int ssl_prepare_handshake_record( ssl_context *ssl )
2727{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002728 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2729 {
2730 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2731 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002732 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002733 }
2734
2735 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2736 ( ssl->in_msg[1] << 16 ) |
2737 ( ssl->in_msg[2] << 8 ) |
2738 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002739
2740 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2741 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002742 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002743
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002744#if defined(POLARSSL_SSL_PROTO_DTLS)
2745 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002746 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002747 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002748 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002749
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002750 /* ssl->handshake is NULL when receiving ClientHello for renego */
2751 if( ssl->handshake != NULL &&
2752 recv_msg_seq != ssl->handshake->in_msg_seq )
2753 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002754 /* No sane server ever retransmits HelloVerifyRequest */
2755 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2756 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002757 {
2758 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2759 "message_seq = %d, start_of_flight = %d",
2760 recv_msg_seq,
2761 ssl->handshake->in_flight_start_seq ) );
2762
2763 if( ( ret = ssl_resend( ssl ) ) != 0 )
2764 {
2765 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2766 return( ret );
2767 }
2768 }
2769 else
2770 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002771 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002772 "message_seq = %d, expected = %d",
2773 recv_msg_seq,
2774 ssl->handshake->in_msg_seq ) );
2775 }
2776
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002777 return( POLARSSL_ERR_NET_WANT_READ );
2778 }
2779 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002780
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002781 /* Reassemble if current message is fragmented or reassembly is
2782 * already in progress */
2783 if( ssl->in_msglen < ssl->in_hslen ||
2784 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2785 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2786 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002787 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002788 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2789
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002790 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2791 {
2792 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2793 return( ret );
2794 }
2795 }
2796 }
2797 else
2798#endif /* POLARSSL_SSL_PROTO_DTLS */
2799 /* With TLS we don't handle fragmentation (for now) */
2800 if( ssl->in_msglen < ssl->in_hslen )
2801 {
2802 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002803 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002804 }
2805
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002806 if( ssl->state != SSL_HANDSHAKE_OVER )
2807 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2808
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002809 /* Handshake message is complete, increment counter */
2810#if defined(POLARSSL_SSL_PROTO_DTLS)
2811 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2812 ssl->handshake != NULL )
2813 {
2814 ssl->handshake->in_msg_seq++;
2815 }
2816#endif
2817
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002818 return( 0 );
2819}
2820
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002821/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002822 * DTLS anti-replay: RFC 6347 4.1.2.6
2823 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002824 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2825 * Bit n is set iff record number in_window_top - n has been seen.
2826 *
2827 * Usually, in_window_top is the last record number seen and the lsb of
2828 * in_window is set. The only exception is the initial state (record number 0
2829 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002830 */
2831#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2832static void ssl_dtls_replay_reset( ssl_context *ssl )
2833{
2834 ssl->in_window_top = 0;
2835 ssl->in_window = 0;
2836}
2837
2838static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2839{
2840 return( ( (uint64_t) buf[0] << 40 ) |
2841 ( (uint64_t) buf[1] << 32 ) |
2842 ( (uint64_t) buf[2] << 24 ) |
2843 ( (uint64_t) buf[3] << 16 ) |
2844 ( (uint64_t) buf[4] << 8 ) |
2845 ( (uint64_t) buf[5] ) );
2846}
2847
2848/*
2849 * Return 0 if sequence number is acceptable, -1 otherwise
2850 */
2851int ssl_dtls_replay_check( ssl_context *ssl )
2852{
2853 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2854 uint64_t bit;
2855
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002856 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2857 return( 0 );
2858
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002859 if( rec_seqnum > ssl->in_window_top )
2860 return( 0 );
2861
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002862 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002863
2864 if( bit >= 64 )
2865 return( -1 );
2866
2867 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2868 return( -1 );
2869
2870 return( 0 );
2871}
2872
2873/*
2874 * Update replay window on new validated record
2875 */
2876void ssl_dtls_replay_update( ssl_context *ssl )
2877{
2878 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2879
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002880 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2881 return;
2882
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002883 if( rec_seqnum > ssl->in_window_top )
2884 {
2885 /* Update window_top and the contents of the window */
2886 uint64_t shift = rec_seqnum - ssl->in_window_top;
2887
2888 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002889 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002890 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002891 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002892 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002893 ssl->in_window |= 1;
2894 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002895
2896 ssl->in_window_top = rec_seqnum;
2897 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002898 else
2899 {
2900 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002901 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002902
2903 if( bit < 64 ) /* Always true, but be extra sure */
2904 ssl->in_window |= (uint64_t) 1 << bit;
2905 }
2906}
2907#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2908
2909/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002910 * ContentType type;
2911 * ProtocolVersion version;
2912 * uint16 epoch; // DTLS only
2913 * uint48 sequence_number; // DTLS only
2914 * uint16 length;
2915 */
2916static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002917{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002918 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002919 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002921 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2922
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002924 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002925 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
2927 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2928 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002929 ssl->in_msgtype,
2930 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002932 /* Check record type */
2933 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2934 ssl->in_msgtype != SSL_MSG_ALERT &&
2935 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2936 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2937 {
2938 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002939
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002940 if( ( ret = ssl_send_alert_message( ssl,
2941 SSL_ALERT_LEVEL_FATAL,
2942 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2943 {
2944 return( ret );
2945 }
2946
2947 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2948 }
2949
Manuel Pégourié-Gonnarddf9a0a82014-10-02 14:17:18 +02002950#if defined(POLARSSL_SSL_PROTO_DTLS)
2951 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2952 {
2953 /* Drop unexpected ChangeCipherSpec messages */
2954 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
2955 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
2956 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
2957 {
2958 SSL_DEBUG_MSG( 1, ( "dropping unexpected ChangeCipherSpec" ) );
2959 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2960 }
2961
2962 /* Drop unexpected ApplicationData records */
2963 if( ssl->in_msgtype == SSL_MSG_APPLICATION_DATA &&
2964 ssl->state != SSL_HANDSHAKE_OVER )
2965 {
2966 SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
2967 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2968 }
2969 }
2970#endif
2971
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002972 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002973 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002974 {
2975 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002976 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 }
2978
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002979 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 {
2981 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002982 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002983 }
2984
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002985 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002986#if defined(POLARSSL_SSL_PROTO_DTLS)
2987 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2988 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002989 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002990
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002991 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002992 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002993 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002994 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002995 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002996 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002997 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002998
2999#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3000 if( ssl_dtls_replay_check( ssl ) != 0 )
3001 {
3002 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
3003 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3004 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003005#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003006 }
3007#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02003008
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003009 /* Check length against the size of our buffer */
3010 if( ssl->in_msglen > SSL_BUFFER_LEN
3011 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003012 {
3013 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
3014 return( POLARSSL_ERR_SSL_INVALID_RECORD );
3015 }
3016
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003017 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00003018 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003019 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02003020 if( ssl->in_msglen < 1 ||
3021 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
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 }
3027 else
3028 {
Paul Bakker48916f92012-09-16 19:57:18 +00003029 if( ssl->in_msglen < ssl->transform_in->minlen )
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 }
3034
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00003037 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038 {
3039 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003040 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003042#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003043#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3044 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 /*
3046 * TLS encrypted messages can have up to 256 bytes of padding
3047 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003049 ssl->in_msglen > ssl->transform_in->minlen +
3050 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 {
3052 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003053 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003054 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003055#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 }
3057
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003058 return( 0 );
3059}
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003061/*
3062 * If applicable, decrypt (and decompress) record content
3063 */
3064static int ssl_prepare_record_content( ssl_context *ssl )
3065{
3066 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003067
Paul Bakker5121ce52009-01-03 21:22:43 +00003068 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003069 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakker05ef8352012-05-08 09:17:57 +00003071#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003072 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003073 {
3074 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
3075
3076 ret = ssl_hw_record_read( ssl );
3077 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
3078 {
3079 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003080 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003081 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003082
3083 if( ret == 0 )
3084 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003085 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003086#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003087 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003088 {
3089 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3090 {
3091 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3092 return( ret );
3093 }
3094
3095 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3096 ssl->in_msg, ssl->in_msglen );
3097
3098 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3099 {
3100 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003101 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 }
3103 }
3104
Paul Bakker2770fbd2012-07-03 13:30:23 +00003105#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003106 if( ssl->transform_in != NULL &&
3107 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003108 {
3109 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3110 {
3111 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3112 return( ret );
3113 }
3114
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003115 // TODO: what's the purpose of these lines? is in_len used?
3116 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3117 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003118 }
3119#endif /* POLARSSL_ZLIB_SUPPORT */
3120
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003121#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003122 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3123 {
3124 ssl_dtls_replay_update( ssl );
3125 }
3126#endif
3127
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003128 return( 0 );
3129}
3130
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003131static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3132
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003133/*
3134 * Read a record.
3135 *
3136 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3137 * and continue reading until a valid record is found.
3138 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003139int ssl_read_record( ssl_context *ssl )
3140{
3141 int ret;
3142
3143 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3144
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003145 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003146 {
3147 /*
3148 * Get next Handshake message in the current record
3149 */
3150 ssl->in_msglen -= ssl->in_hslen;
3151
3152 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3153 ssl->in_msglen );
3154
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003155 SSL_DEBUG_BUF( 4, "remaining content in record",
3156 ssl->in_msg, ssl->in_msglen );
3157
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003158 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3159 return( ret );
3160
3161 return( 0 );
3162 }
3163
3164 ssl->in_hslen = 0;
3165
3166 /*
3167 * Read the record header and parse it
3168 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003169#if defined(POLARSSL_SSL_PROTO_DTLS)
3170read_record_header:
3171#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003172 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3173 {
3174 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3175 return( ret );
3176 }
3177
3178 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003179 {
3180#if defined(POLARSSL_SSL_PROTO_DTLS)
3181 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3182 {
3183 /* Ignore bad record and get next one; drop the whole datagram
3184 * since current header cannot be trusted to find the next record
3185 * in current datagram */
3186 ssl->next_record_offset = 0;
3187 ssl->in_left = 0;
3188
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003189 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003190 goto read_record_header;
3191 }
3192#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003193 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003194 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003195
3196 /*
3197 * Read and optionally decrypt the message contents
3198 */
3199 if( ( ret = ssl_fetch_input( ssl,
3200 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3201 {
3202 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3203 return( ret );
3204 }
3205
3206 /* Done reading this record, get ready for the next one */
3207#if defined(POLARSSL_SSL_PROTO_DTLS)
3208 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3209 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3210 else
3211#endif
3212 ssl->in_left = 0;
3213
3214 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003215 {
3216#if defined(POLARSSL_SSL_PROTO_DTLS)
3217 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3218 {
3219 /* Silently discard invalid records */
3220 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3221 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3222 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003223 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003224 goto read_record_header;
3225 }
3226
3227 return( ret );
3228 }
3229 else
3230#endif
3231 {
3232 /* Error out (and send alert) on invalid records */
3233#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3234 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3235 {
3236 ssl_send_alert_message( ssl,
3237 SSL_ALERT_LEVEL_FATAL,
3238 SSL_ALERT_MSG_BAD_RECORD_MAC );
3239 }
3240#endif
3241 return( ret );
3242 }
3243 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003244
3245 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003246 * When we sent the last flight of the handshake, we MUST respond to a
3247 * retransmit of the peer's previous flight with a retransmit. (In
3248 * practice, only the Finished message will make it, other messages
3249 * including CCS use the old transform so they're dropped as invalid.)
3250 *
3251 * If the record we received is not a handshake message, however, it
3252 * means the peer received our last flight so we can clean up
3253 * handshake info.
3254 *
3255 * This check needs to be done before prepare_handshake() due to an edge
3256 * case: if the client immediately requests renegotiation, this
3257 * finishes the current handshake first, avoiding the new ClientHello
3258 * being mistaken for an ancient message in the current handshake.
3259 */
3260#if defined(POLARSSL_SSL_PROTO_DTLS)
3261 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3262 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003263 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003264 {
3265 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3266 ssl->in_msg[0] == SSL_HS_FINISHED )
3267 {
3268 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3269
3270 if( ( ret = ssl_resend( ssl ) ) != 0 )
3271 {
3272 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3273 return( ret );
3274 }
3275
3276 return( POLARSSL_ERR_NET_WANT_READ );
3277 }
3278 else
3279 {
3280 ssl_handshake_wrapup_free_hs_transform( ssl );
3281 }
3282 }
3283#endif
3284
3285 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003286 * Handle particular types of records
3287 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3289 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003290 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3291 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 }
3293
3294 if( ssl->in_msgtype == SSL_MSG_ALERT )
3295 {
3296 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3297 ssl->in_msg[0], ssl->in_msg[1] ) );
3298
3299 /*
3300 * Ignore non-fatal alerts, except close_notify
3301 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003302 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003304 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3305 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003306 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 }
3308
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003309 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3310 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003311 {
3312 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003313 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 }
3315 }
3316
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3318
3319 return( 0 );
3320}
3321
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003322int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3323{
3324 int ret;
3325
3326 if( ( ret = ssl_send_alert_message( ssl,
3327 SSL_ALERT_LEVEL_FATAL,
3328 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3329 {
3330 return( ret );
3331 }
3332
3333 return( 0 );
3334}
3335
Paul Bakker0a925182012-04-16 06:46:41 +00003336int ssl_send_alert_message( ssl_context *ssl,
3337 unsigned char level,
3338 unsigned char message )
3339{
3340 int ret;
3341
3342 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3343
3344 ssl->out_msgtype = SSL_MSG_ALERT;
3345 ssl->out_msglen = 2;
3346 ssl->out_msg[0] = level;
3347 ssl->out_msg[1] = message;
3348
3349 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3350 {
3351 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3352 return( ret );
3353 }
3354
3355 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3356
3357 return( 0 );
3358}
3359
Paul Bakker5121ce52009-01-03 21:22:43 +00003360/*
3361 * Handshake functions
3362 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003363#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3364 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3365 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3366 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3367 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3368 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3369 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003370int ssl_write_certificate( ssl_context *ssl )
3371{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003372 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003373
3374 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3375
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003376 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003377 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3378 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003379 {
3380 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3381 ssl->state++;
3382 return( 0 );
3383 }
3384
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003385 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3386 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003387}
3388
3389int ssl_parse_certificate( ssl_context *ssl )
3390{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003391 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3392
3393 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3394
3395 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003396 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3397 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398 {
3399 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3400 ssl->state++;
3401 return( 0 );
3402 }
3403
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003404 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3405 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406}
3407#else
3408int ssl_write_certificate( ssl_context *ssl )
3409{
3410 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3411 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003412 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003413 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3414
3415 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3416
3417 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003418 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3419 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003420 {
3421 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3422 ssl->state++;
3423 return( 0 );
3424 }
3425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 if( ssl->endpoint == SSL_IS_CLIENT )
3427 {
3428 if( ssl->client_auth == 0 )
3429 {
3430 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3431 ssl->state++;
3432 return( 0 );
3433 }
3434
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003435#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 /*
3437 * If using SSLv3 and got no cert, send an Alert message
3438 * (otherwise an empty Certificate message will be sent).
3439 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003440 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3442 {
3443 ssl->out_msglen = 2;
3444 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003445 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3446 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003447
3448 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3449 goto write_msg;
3450 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003451#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003452 }
3453 else /* SSL_IS_SERVER */
3454 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003455 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 {
3457 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003458 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460 }
3461
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003462 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463
3464 /*
3465 * 0 . 0 handshake type
3466 * 1 . 3 handshake length
3467 * 4 . 6 length of all certs
3468 * 7 . 9 length of cert. 1
3469 * 10 . n-1 peer certificate
3470 * n . n+2 length of cert. 2
3471 * n+3 . ... upper level cert, etc.
3472 */
3473 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003474 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
Paul Bakker29087132010-03-21 21:03:34 +00003476 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003477 {
3478 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003479 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003480 {
3481 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3482 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003483 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 }
3485
3486 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3487 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3488 ssl->out_msg[i + 2] = (unsigned char)( n );
3489
3490 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3491 i += n; crt = crt->next;
3492 }
3493
3494 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3495 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3496 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3497
3498 ssl->out_msglen = i;
3499 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3500 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3501
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003502#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003503write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003504#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003505
3506 ssl->state++;
3507
3508 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3509 {
3510 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3511 return( ret );
3512 }
3513
3514 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3515
Paul Bakkered27a042013-04-18 22:46:23 +02003516 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517}
3518
3519int ssl_parse_certificate( ssl_context *ssl )
3520{
Paul Bakkered27a042013-04-18 22:46:23 +02003521 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003522 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003523 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
3525 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3526
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003527 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003528 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3529 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003530 {
3531 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3532 ssl->state++;
3533 return( 0 );
3534 }
3535
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003537 ( ssl->authmode == SSL_VERIFY_NONE ||
3538 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003539 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003540 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3542 ssl->state++;
3543 return( 0 );
3544 }
3545
3546 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3547 {
3548 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3549 return( ret );
3550 }
3551
3552 ssl->state++;
3553
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003554#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 /*
3556 * Check if the client sent an empty certificate
3557 */
3558 if( ssl->endpoint == SSL_IS_SERVER &&
3559 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3560 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003561 if( ssl->in_msglen == 2 &&
3562 ssl->in_msgtype == SSL_MSG_ALERT &&
3563 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3564 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 {
3566 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3567
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003568 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3570 return( 0 );
3571 else
Paul Bakker40e46942009-01-03 21:51:57 +00003572 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 }
3574 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003575#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003576
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003577#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3578 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 if( ssl->endpoint == SSL_IS_SERVER &&
3580 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3581 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003582 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003583 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3584 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003585 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 {
3587 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3588
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003589 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003590 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003591 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 else
3593 return( 0 );
3594 }
3595 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003596#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3597 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003598
3599 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3600 {
3601 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003602 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 }
3604
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003605 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3606 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003607 {
3608 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003609 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003610 }
3611
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003612 i = ssl_hs_hdr_len( ssl );
3613
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 /*
3615 * Same message structure as in ssl_write_certificate()
3616 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003617 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003618
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003619 if( ssl->in_msg[i] != 0 ||
3620 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003621 {
3622 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003623 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 }
3625
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003626 /* In case we tried to reuse a session but it failed */
3627 if( ssl->session_negotiate->peer_cert != NULL )
3628 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003629 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003630 polarssl_free( ssl->session_negotiate->peer_cert );
3631 }
3632
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003633 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3634 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 {
3636 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003637 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003638 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 }
3640
Paul Bakkerb6b09562013-09-18 14:17:41 +02003641 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003642
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003643 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003644
3645 while( i < ssl->in_hslen )
3646 {
3647 if( ssl->in_msg[i] != 0 )
3648 {
3649 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003650 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003651 }
3652
3653 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3654 | (unsigned int) ssl->in_msg[i + 2];
3655 i += 3;
3656
3657 if( n < 128 || i + n > ssl->in_hslen )
3658 {
3659 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003660 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 }
3662
Paul Bakkerddf26b42013-09-18 13:46:23 +02003663 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3664 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003665 if( ret != 0 )
3666 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003667 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003668 return( ret );
3669 }
3670
3671 i += n;
3672 }
3673
Paul Bakker48916f92012-09-16 19:57:18 +00003674 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003675
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003676 /*
3677 * On client, make sure the server cert doesn't change during renego to
3678 * avoid "triple handshake" attack: https://secure-resumption.com/
3679 */
3680 if( ssl->endpoint == SSL_IS_CLIENT &&
3681 ssl->renegotiation == SSL_RENEGOTIATION )
3682 {
3683 if( ssl->session->peer_cert == NULL )
3684 {
3685 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3686 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3687 }
3688
3689 if( ssl->session->peer_cert->raw.len !=
3690 ssl->session_negotiate->peer_cert->raw.len ||
3691 memcmp( ssl->session->peer_cert->raw.p,
3692 ssl->session_negotiate->peer_cert->raw.p,
3693 ssl->session->peer_cert->raw.len ) != 0 )
3694 {
3695 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3696 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3697 }
3698 }
3699
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 if( ssl->authmode != SSL_VERIFY_NONE )
3701 {
3702 if( ssl->ca_chain == NULL )
3703 {
3704 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003705 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003706 }
3707
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003708 /*
3709 * Main check: verify certificate
3710 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003711 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3712 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3713 &ssl->session_negotiate->verify_result,
3714 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003715
3716 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003717 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003719 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003720
3721 /*
3722 * Secondary checks: always done, but change 'ret' only if it was 0
3723 */
3724
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003725#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003726 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003727 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003728
3729 /* If certificate uses an EC key, make sure the curve is OK */
3730 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3731 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3732 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003733 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003734 if( ret == 0 )
3735 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003736 }
3737 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003738#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003739
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003740 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3741 ciphersuite_info,
3742 ! ssl->endpoint ) != 0 )
3743 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003744 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003745 if( ret == 0 )
3746 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3747 }
3748
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3750 ret = 0;
3751 }
3752
3753 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3754
3755 return( ret );
3756}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003757#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3758 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3759 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3760 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3761 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3762 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3763 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003764
3765int ssl_write_change_cipher_spec( ssl_context *ssl )
3766{
3767 int ret;
3768
3769 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3770
3771 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3772 ssl->out_msglen = 1;
3773 ssl->out_msg[0] = 1;
3774
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 ssl->state++;
3776
3777 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3778 {
3779 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3780 return( ret );
3781 }
3782
3783 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3784
3785 return( 0 );
3786}
3787
3788int ssl_parse_change_cipher_spec( ssl_context *ssl )
3789{
3790 int ret;
3791
3792 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3793
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3795 {
3796 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3797 return( ret );
3798 }
3799
3800 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3801 {
3802 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003803 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 }
3805
3806 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3807 {
3808 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003809 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003810 }
3811
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003812 /*
3813 * Switch to our negotiated transform and session parameters for inbound
3814 * data.
3815 */
3816 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3817 ssl->transform_in = ssl->transform_negotiate;
3818 ssl->session_in = ssl->session_negotiate;
3819
3820#if defined(POLARSSL_SSL_PROTO_DTLS)
3821 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3822 {
3823#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3824 ssl_dtls_replay_reset( ssl );
3825#endif
3826
3827 /* Increment epoch */
3828 if( ++ssl->in_epoch == 0 )
3829 {
3830 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3831 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3832 }
3833 }
3834 else
3835#endif /* POLARSSL_SSL_PROTO_DTLS */
3836 memset( ssl->in_ctr, 0, 8 );
3837
3838 /*
3839 * Set the in_msg pointer to the correct location based on IV length
3840 */
3841 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3842 {
3843 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3844 ssl->transform_negotiate->fixed_ivlen;
3845 }
3846 else
3847 ssl->in_msg = ssl->in_iv;
3848
3849#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3850 if( ssl_hw_record_activate != NULL )
3851 {
3852 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3853 {
3854 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3855 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3856 }
3857 }
3858#endif
3859
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 ssl->state++;
3861
3862 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3863
3864 return( 0 );
3865}
3866
Paul Bakker41c83d32013-03-20 14:39:14 +01003867void ssl_optimize_checksum( ssl_context *ssl,
3868 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003869{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003870 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003871
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003872#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3873 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003874 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003875 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003876 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003877#endif
3878#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3879#if defined(POLARSSL_SHA512_C)
3880 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3881 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3882 else
3883#endif
3884#if defined(POLARSSL_SHA256_C)
3885 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003886 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003887 else
3888#endif
3889#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003890 {
3891 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003892 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003893 }
Paul Bakker380da532012-04-18 16:10:25 +00003894}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003895
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003896void ssl_reset_checksum( ssl_context *ssl )
3897{
3898#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3899 defined(POLARSSL_SSL_PROTO_TLS1_1)
3900 md5_starts( &ssl->handshake->fin_md5 );
3901 sha1_starts( &ssl->handshake->fin_sha1 );
3902#endif
3903#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3904#if defined(POLARSSL_SHA256_C)
3905 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3906#endif
3907#if defined(POLARSSL_SHA512_C)
3908 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3909#endif
3910#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3911}
3912
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003913static void ssl_update_checksum_start( ssl_context *ssl,
3914 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003915{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003916#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3917 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003918 md5_update( &ssl->handshake->fin_md5 , buf, len );
3919 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003920#endif
3921#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3922#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003923 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003924#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003925#if defined(POLARSSL_SHA512_C)
3926 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003927#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003928#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003929}
3930
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003931#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3932 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003933static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3934 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003935{
Paul Bakker48916f92012-09-16 19:57:18 +00003936 md5_update( &ssl->handshake->fin_md5 , buf, len );
3937 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003938}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003939#endif
Paul Bakker380da532012-04-18 16:10:25 +00003940
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003941#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3942#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003943static void ssl_update_checksum_sha256( 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 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003947}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003948#endif
Paul Bakker380da532012-04-18 16:10:25 +00003949
Paul Bakker9e36f042013-06-30 14:34:05 +02003950#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003951static void ssl_update_checksum_sha384( ssl_context *ssl,
3952 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003953{
Paul Bakker9e36f042013-06-30 14:34:05 +02003954 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003955}
Paul Bakker769075d2012-11-24 11:26:46 +01003956#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003957#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003958
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003959#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003960static void ssl_calc_finished_ssl(
3961 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003962{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003963 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003964 md5_context md5;
3965 sha1_context sha1;
3966
Paul Bakker5121ce52009-01-03 21:22:43 +00003967 unsigned char padbuf[48];
3968 unsigned char md5sum[16];
3969 unsigned char sha1sum[20];
3970
Paul Bakker48916f92012-09-16 19:57:18 +00003971 ssl_session *session = ssl->session_negotiate;
3972 if( !session )
3973 session = ssl->session;
3974
Paul Bakker1ef83d62012-04-11 12:09:53 +00003975 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3976
Paul Bakker48916f92012-09-16 19:57:18 +00003977 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3978 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
3980 /*
3981 * SSLv3:
3982 * hash =
3983 * MD5( master + pad2 +
3984 * MD5( handshake + sender + master + pad1 ) )
3985 * + SHA1( master + pad2 +
3986 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003987 */
3988
Paul Bakker90995b52013-06-24 19:20:35 +02003989#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003990 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003991 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003992#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003993
Paul Bakker90995b52013-06-24 19:20:35 +02003994#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003995 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003996 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003997#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
Paul Bakker3c2122f2013-06-24 19:03:14 +02003999 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
4000 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00004001
Paul Bakker1ef83d62012-04-11 12:09:53 +00004002 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
Paul Bakker3c2122f2013-06-24 19:03:14 +02004004 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004005 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004006 md5_update( &md5, padbuf, 48 );
4007 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
Paul Bakker3c2122f2013-06-24 19:03:14 +02004009 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00004010 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004011 sha1_update( &sha1, padbuf, 40 );
4012 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Paul Bakker1ef83d62012-04-11 12:09:53 +00004014 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakker1ef83d62012-04-11 12:09:53 +00004016 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00004017 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018 md5_update( &md5, padbuf, 48 );
4019 md5_update( &md5, md5sum, 16 );
4020 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004021
Paul Bakker1ef83d62012-04-11 12:09:53 +00004022 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00004023 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024 sha1_update( &sha1, padbuf , 40 );
4025 sha1_update( &sha1, sha1sum, 20 );
4026 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004027
Paul Bakker1ef83d62012-04-11 12:09:53 +00004028 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004029
Paul Bakker5b4af392014-06-26 12:09:34 +02004030 md5_free( &md5 );
4031 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004032
Paul Bakker34617722014-06-13 17:20:13 +02004033 polarssl_zeroize( padbuf, sizeof( padbuf ) );
4034 polarssl_zeroize( md5sum, sizeof( md5sum ) );
4035 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004036
4037 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4038}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004039#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004040
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004041#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004042static void ssl_calc_finished_tls(
4043 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00004044{
Paul Bakker1ef83d62012-04-11 12:09:53 +00004045 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004046 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004047 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00004048 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004049 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00004050
Paul Bakker48916f92012-09-16 19:57:18 +00004051 ssl_session *session = ssl->session_negotiate;
4052 if( !session )
4053 session = ssl->session;
4054
Paul Bakker1ef83d62012-04-11 12:09:53 +00004055 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056
Paul Bakker48916f92012-09-16 19:57:18 +00004057 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
4058 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
Paul Bakker1ef83d62012-04-11 12:09:53 +00004060 /*
4061 * TLSv1:
4062 * hash = PRF( master, finished_label,
4063 * MD5( handshake ) + SHA1( handshake ) )[0..11]
4064 */
Paul Bakker5121ce52009-01-03 21:22:43 +00004065
Paul Bakker90995b52013-06-24 19:20:35 +02004066#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004067 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4068 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004069#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004070
Paul Bakker90995b52013-06-24 19:20:35 +02004071#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004072 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4073 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004074#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004075
4076 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004077 ? "client finished"
4078 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004079
4080 md5_finish( &md5, padbuf );
4081 sha1_finish( &sha1, padbuf + 16 );
4082
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004083 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004084 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004085
4086 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4087
Paul Bakker5b4af392014-06-26 12:09:34 +02004088 md5_free( &md5 );
4089 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004090
Paul Bakker34617722014-06-13 17:20:13 +02004091 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004092
4093 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4094}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004095#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004096
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004097#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4098#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004099static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004100 ssl_context *ssl, unsigned char *buf, int from )
4101{
4102 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004103 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004104 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004105 unsigned char padbuf[32];
4106
Paul Bakker48916f92012-09-16 19:57:18 +00004107 ssl_session *session = ssl->session_negotiate;
4108 if( !session )
4109 session = ssl->session;
4110
Paul Bakker380da532012-04-18 16:10:25 +00004111 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004112
Paul Bakker9e36f042013-06-30 14:34:05 +02004113 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004114
4115 /*
4116 * TLSv1.2:
4117 * hash = PRF( master, finished_label,
4118 * Hash( handshake ) )[0.11]
4119 */
4120
Paul Bakker9e36f042013-06-30 14:34:05 +02004121#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004122 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004123 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004124#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004125
4126 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004127 ? "client finished"
4128 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004129
Paul Bakker9e36f042013-06-30 14:34:05 +02004130 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004131
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004132 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004133 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004134
4135 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4136
Paul Bakker5b4af392014-06-26 12:09:34 +02004137 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004138
Paul Bakker34617722014-06-13 17:20:13 +02004139 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004140
4141 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4142}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004143#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004144
Paul Bakker9e36f042013-06-30 14:34:05 +02004145#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004146static void ssl_calc_finished_tls_sha384(
4147 ssl_context *ssl, unsigned char *buf, int from )
4148{
4149 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004150 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004151 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004152 unsigned char padbuf[48];
4153
Paul Bakker48916f92012-09-16 19:57:18 +00004154 ssl_session *session = ssl->session_negotiate;
4155 if( !session )
4156 session = ssl->session;
4157
Paul Bakker380da532012-04-18 16:10:25 +00004158 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004159
Paul Bakker9e36f042013-06-30 14:34:05 +02004160 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004161
4162 /*
4163 * TLSv1.2:
4164 * hash = PRF( master, finished_label,
4165 * Hash( handshake ) )[0.11]
4166 */
4167
Paul Bakker9e36f042013-06-30 14:34:05 +02004168#if !defined(POLARSSL_SHA512_ALT)
4169 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4170 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004171#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004172
4173 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004174 ? "client finished"
4175 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004176
Paul Bakker9e36f042013-06-30 14:34:05 +02004177 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004178
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004179 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004180 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004181
4182 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4183
Paul Bakker5b4af392014-06-26 12:09:34 +02004184 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004185
Paul Bakker34617722014-06-13 17:20:13 +02004186 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004187
4188 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4189}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004190#endif /* POLARSSL_SHA512_C */
4191#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004192
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004193static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004194{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004195 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004196
4197 /*
4198 * Free our handshake params
4199 */
4200 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004201 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004202 ssl->handshake = NULL;
4203
4204 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004205 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004206 */
4207 if( ssl->transform )
4208 {
4209 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004210 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004211 }
4212 ssl->transform = ssl->transform_negotiate;
4213 ssl->transform_negotiate = NULL;
4214
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004215 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4216}
4217
4218void ssl_handshake_wrapup( ssl_context *ssl )
4219{
4220 int resume = ssl->handshake->resume;
4221
4222 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4223
4224 if( ssl->renegotiation == SSL_RENEGOTIATION )
4225 {
4226 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4227 ssl->renego_records_seen = 0;
4228 }
4229
4230 /*
4231 * Free the previous session and switch in the current one
4232 */
Paul Bakker0a597072012-09-25 21:55:46 +00004233 if( ssl->session )
4234 {
4235 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004236 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004237 }
4238 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004239 ssl->session_negotiate = NULL;
4240
Paul Bakker0a597072012-09-25 21:55:46 +00004241 /*
4242 * Add cache entry
4243 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004244 if( ssl->f_set_cache != NULL &&
4245 ssl->session->length != 0 &&
4246 resume == 0 )
4247 {
Paul Bakker0a597072012-09-25 21:55:46 +00004248 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4249 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004250 }
Paul Bakker0a597072012-09-25 21:55:46 +00004251
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004252#if defined(POLARSSL_SSL_PROTO_DTLS)
4253 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4254 ssl->handshake->flight != NULL )
4255 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02004256 /* Cancel handshake timer */
4257 ssl_set_timer( ssl, 0 );
4258
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004259 /* Keep last flight around in case we need to resend it:
4260 * we need the handshake and transform structures for that */
4261 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4262 }
4263 else
4264#endif
4265 ssl_handshake_wrapup_free_hs_transform( ssl );
4266
Paul Bakker48916f92012-09-16 19:57:18 +00004267 ssl->state++;
4268
4269 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4270}
4271
Paul Bakker1ef83d62012-04-11 12:09:53 +00004272int ssl_write_finished( ssl_context *ssl )
4273{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004274 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004275
4276 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4277
Paul Bakker92be97b2013-01-02 17:30:03 +01004278 /*
4279 * Set the out_msg pointer to the correct location based on IV length
4280 */
4281 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4282 {
4283 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4284 ssl->transform_negotiate->fixed_ivlen;
4285 }
4286 else
4287 ssl->out_msg = ssl->out_iv;
4288
Paul Bakker48916f92012-09-16 19:57:18 +00004289 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004290
4291 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004292 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4293
Paul Bakker48916f92012-09-16 19:57:18 +00004294 ssl->verify_data_len = hash_len;
4295 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4296
Paul Bakker5121ce52009-01-03 21:22:43 +00004297 ssl->out_msglen = 4 + hash_len;
4298 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4299 ssl->out_msg[0] = SSL_HS_FINISHED;
4300
4301 /*
4302 * In case of session resuming, invert the client and server
4303 * ChangeCipherSpec messages order.
4304 */
Paul Bakker0a597072012-09-25 21:55:46 +00004305 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 {
4307 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004308 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004309 else
4310 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4311 }
4312 else
4313 ssl->state++;
4314
Paul Bakker48916f92012-09-16 19:57:18 +00004315 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004316 * Switch to our negotiated transform and session parameters for outbound
4317 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004318 */
4319 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004320
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004321#if defined(POLARSSL_SSL_PROTO_DTLS)
4322 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4323 {
4324 unsigned char i;
4325
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004326 /* Remember current epoch settings for resending */
4327 ssl->handshake->alt_transform_out = ssl->transform_out;
4328 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4329
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004330 /* Set sequence_number to zero */
4331 memset( ssl->out_ctr + 2, 0, 6 );
4332
4333 /* Increment epoch */
4334 for( i = 2; i > 0; i-- )
4335 if( ++ssl->out_ctr[i - 1] != 0 )
4336 break;
4337
4338 /* The loop goes to its end iff the counter is wrapping */
4339 if( i == 0 )
4340 {
4341 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4342 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4343 }
4344 }
4345 else
4346#endif /* POLARSSL_SSL_PROTO_DTLS */
4347 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004348
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004349 ssl->transform_out = ssl->transform_negotiate;
4350 ssl->session_out = ssl->session_negotiate;
4351
Paul Bakker07eb38b2012-12-19 14:42:06 +01004352#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004353 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004354 {
4355 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4356 {
4357 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4358 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4359 }
4360 }
4361#endif
4362
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004363#if defined(POLARSSL_SSL_PROTO_DTLS)
4364 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4365 ssl_send_flight_completed( ssl );
4366#endif
4367
Paul Bakker5121ce52009-01-03 21:22:43 +00004368 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4369 {
4370 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4371 return( ret );
4372 }
4373
4374 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4375
4376 return( 0 );
4377}
4378
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004379#if defined(POLARSSL_SSL_PROTO_SSL3)
4380#define SSL_MAX_HASH_LEN 36
4381#else
4382#define SSL_MAX_HASH_LEN 12
4383#endif
4384
Paul Bakker5121ce52009-01-03 21:22:43 +00004385int ssl_parse_finished( ssl_context *ssl )
4386{
Paul Bakker23986e52011-04-24 08:57:21 +00004387 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004388 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004389 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004390
4391 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4392
Paul Bakker48916f92012-09-16 19:57:18 +00004393 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004394
Paul Bakker5121ce52009-01-03 21:22:43 +00004395 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4396 {
4397 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4398 return( ret );
4399 }
4400
4401 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4402 {
4403 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004404 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004405 }
4406
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004407 /* There is currently no ciphersuite using another length with TLS 1.2 */
4408#if defined(POLARSSL_SSL_PROTO_SSL3)
4409 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4410 hash_len = 36;
4411 else
4412#endif
4413 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004414
4415 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004416 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004417 {
4418 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004419 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004420 }
4421
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004422 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4423 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 {
4425 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004426 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004427 }
4428
Paul Bakker48916f92012-09-16 19:57:18 +00004429 ssl->verify_data_len = hash_len;
4430 memcpy( ssl->peer_verify_data, buf, hash_len );
4431
Paul Bakker0a597072012-09-25 21:55:46 +00004432 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004433 {
4434 if( ssl->endpoint == SSL_IS_CLIENT )
4435 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4436
4437 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004438 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004439 }
4440 else
4441 ssl->state++;
4442
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004443#if defined(POLARSSL_SSL_PROTO_DTLS)
4444 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4445 ssl_recv_flight_completed( ssl );
4446#endif
4447
Paul Bakker5121ce52009-01-03 21:22:43 +00004448 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4449
4450 return( 0 );
4451}
4452
Paul Bakker968afaa2014-07-09 11:09:24 +02004453static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004454{
4455 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4456
4457#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4458 defined(POLARSSL_SSL_PROTO_TLS1_1)
4459 md5_init( &handshake->fin_md5 );
4460 sha1_init( &handshake->fin_sha1 );
4461 md5_starts( &handshake->fin_md5 );
4462 sha1_starts( &handshake->fin_sha1 );
4463#endif
4464#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4465#if defined(POLARSSL_SHA256_C)
4466 sha256_init( &handshake->fin_sha256 );
4467 sha256_starts( &handshake->fin_sha256, 0 );
4468#endif
4469#if defined(POLARSSL_SHA512_C)
4470 sha512_init( &handshake->fin_sha512 );
4471 sha512_starts( &handshake->fin_sha512, 1 );
4472#endif
4473#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4474
4475 handshake->update_checksum = ssl_update_checksum_start;
4476 handshake->sig_alg = SSL_HASH_SHA1;
4477
4478#if defined(POLARSSL_DHM_C)
4479 dhm_init( &handshake->dhm_ctx );
4480#endif
4481#if defined(POLARSSL_ECDH_C)
4482 ecdh_init( &handshake->ecdh_ctx );
4483#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004484}
4485
4486static void ssl_transform_init( ssl_transform *transform )
4487{
4488 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004489
4490 cipher_init( &transform->cipher_ctx_enc );
4491 cipher_init( &transform->cipher_ctx_dec );
4492
4493 md_init( &transform->md_ctx_enc );
4494 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004495}
4496
4497void ssl_session_init( ssl_session *session )
4498{
4499 memset( session, 0, sizeof(ssl_session) );
4500}
4501
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004502static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004503{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004504 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004505 if( ssl->transform_negotiate )
4506 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004507 if( ssl->session_negotiate )
4508 ssl_session_free( ssl->session_negotiate );
4509 if( ssl->handshake )
4510 ssl_handshake_free( ssl->handshake );
4511
4512 /*
4513 * Either the pointers are now NULL or cleared properly and can be freed.
4514 * Now allocate missing structures.
4515 */
4516 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004517 {
4518 ssl->transform_negotiate =
4519 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4520 }
Paul Bakker48916f92012-09-16 19:57:18 +00004521
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004522 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004523 {
4524 ssl->session_negotiate =
4525 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4526 }
Paul Bakker48916f92012-09-16 19:57:18 +00004527
Paul Bakker82788fb2014-10-20 13:59:19 +02004528 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004529 {
4530 ssl->handshake = (ssl_handshake_params *)
4531 polarssl_malloc( sizeof(ssl_handshake_params) );
4532 }
Paul Bakker48916f92012-09-16 19:57:18 +00004533
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004534 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004535 if( ssl->handshake == NULL ||
4536 ssl->transform_negotiate == NULL ||
4537 ssl->session_negotiate == NULL )
4538 {
4539 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004540
4541 polarssl_free( ssl->handshake );
4542 polarssl_free( ssl->transform_negotiate );
4543 polarssl_free( ssl->session_negotiate );
4544
4545 ssl->handshake = NULL;
4546 ssl->transform_negotiate = NULL;
4547 ssl->session_negotiate = NULL;
4548
Paul Bakker48916f92012-09-16 19:57:18 +00004549 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4550 }
4551
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004552 /* Initialize structures */
4553 ssl_session_init( ssl->session_negotiate );
4554 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004555 ssl_handshake_params_init( ssl->handshake );
4556
4557#if defined(POLARSSL_X509_CRT_PARSE_C)
4558 ssl->handshake->key_cert = ssl->key_cert;
4559#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004560
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004561 /*
4562 * We may not know yet if we're using DTLS,
4563 * so always initiliase DTLS-specific fields.
4564 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004565#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004566 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004567
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004568 // TODO: not the right place, we may not know endpoint yet
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004569 if( ssl->endpoint == SSL_IS_CLIENT )
4570 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4571 else
4572 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004573#endif
4574
Paul Bakker48916f92012-09-16 19:57:18 +00004575 return( 0 );
4576}
4577
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004578#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4579/* Dummy cookie callbacks for defaults */
4580static int ssl_cookie_write_dummy( void *ctx,
4581 unsigned char **p, unsigned char *end,
4582 const unsigned char *cli_id, size_t cli_id_len )
4583{
4584 ((void) ctx);
4585 ((void) p);
4586 ((void) end);
4587 ((void) cli_id);
4588 ((void) cli_id_len);
4589
4590 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4591}
4592
4593static int ssl_cookie_check_dummy( void *ctx,
4594 const unsigned char *cookie, size_t cookie_len,
4595 const unsigned char *cli_id, size_t cli_id_len )
4596{
4597 ((void) ctx);
4598 ((void) cookie);
4599 ((void) cookie_len);
4600 ((void) cli_id);
4601 ((void) cli_id_len);
4602
4603 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4604}
4605#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4606
Paul Bakker5121ce52009-01-03 21:22:43 +00004607/*
4608 * Initialize an SSL context
4609 */
4610int ssl_init( ssl_context *ssl )
4611{
Paul Bakker48916f92012-09-16 19:57:18 +00004612 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004613 int len = SSL_BUFFER_LEN;
4614
4615 memset( ssl, 0, sizeof( ssl_context ) );
4616
Paul Bakker62f2dee2012-09-28 07:31:51 +00004617 /*
4618 * Sane defaults
4619 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004620 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4621 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4622 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4623 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004624
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004625 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004626
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004627 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4628
Paul Bakker62f2dee2012-09-28 07:31:51 +00004629#if defined(POLARSSL_DHM_C)
4630 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4631 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4632 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4633 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4634 {
4635 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4636 return( ret );
4637 }
4638#endif
4639
4640 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004641 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004642 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004643 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004644 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004645
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004646 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004647 {
4648 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004649 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004650 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004651 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004652 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004653 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004654 }
4655
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004656 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4657 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004658
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004659 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4660 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4661
Paul Bakker606b4ba2013-08-14 16:52:14 +02004662#if defined(POLARSSL_SSL_SESSION_TICKETS)
4663 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4664#endif
4665
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004666#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004667 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004668#endif
4669
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004670#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4671 ssl->f_cookie_write = ssl_cookie_write_dummy;
4672 ssl->f_cookie_check = ssl_cookie_check_dummy;
4673#endif
4674
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004675#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4676 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4677#endif
4678
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004679#if defined(POLARSSL_SSL_PROTO_DTLS)
4680 ssl->hs_timeout_min = SSL_DTLS_TIMEOUT_DFL_MIN;
4681 ssl->hs_timeout_max = SSL_DTLS_TIMEOUT_DFL_MAX;
4682#endif
4683
Paul Bakker48916f92012-09-16 19:57:18 +00004684 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4685 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004686
4687 return( 0 );
4688}
4689
4690/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004691 * Reset an initialized and used SSL context for re-use while retaining
4692 * all application-set variables, function pointers and data.
4693 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004694int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004695{
Paul Bakker48916f92012-09-16 19:57:18 +00004696 int ret;
4697
Paul Bakker7eb013f2011-10-06 12:37:39 +00004698 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004699 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4700 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4701
4702 ssl->verify_data_len = 0;
4703 memset( ssl->own_verify_data, 0, 36 );
4704 memset( ssl->peer_verify_data, 0, 36 );
4705
Paul Bakker7eb013f2011-10-06 12:37:39 +00004706 ssl->in_offt = NULL;
4707
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004708 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004709 ssl->in_msgtype = 0;
4710 ssl->in_msglen = 0;
4711 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004712#if defined(POLARSSL_SSL_PROTO_DTLS)
4713 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004714 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004715#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004716#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4717 ssl_dtls_replay_reset( ssl );
4718#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004719
4720 ssl->in_hslen = 0;
4721 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004722 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004723
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004724 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004725 ssl->out_msgtype = 0;
4726 ssl->out_msglen = 0;
4727 ssl->out_left = 0;
4728
Paul Bakker48916f92012-09-16 19:57:18 +00004729 ssl->transform_in = NULL;
4730 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004731
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004732 ssl->renego_records_seen = 0;
4733
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004734 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4735 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004736
4737#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004738 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004739 {
4740 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004741 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004742 {
4743 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4744 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4745 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004746 }
4747#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004748
Paul Bakker48916f92012-09-16 19:57:18 +00004749 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004750 {
Paul Bakker48916f92012-09-16 19:57:18 +00004751 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004752 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004753 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004754 }
Paul Bakker48916f92012-09-16 19:57:18 +00004755
Paul Bakkerc0463502013-02-14 11:19:38 +01004756 if( ssl->session )
4757 {
4758 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004759 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004760 ssl->session = NULL;
4761 }
4762
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004763#if defined(POLARSSL_SSL_ALPN)
4764 ssl->alpn_chosen = NULL;
4765#endif
4766
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004767#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004768 polarssl_free( ssl->cli_id );
4769 ssl->cli_id = NULL;
4770 ssl->cli_id_len = 0;
4771#endif
4772
Paul Bakker48916f92012-09-16 19:57:18 +00004773 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4774 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004775
4776 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004777}
4778
Paul Bakkera503a632013-08-14 13:48:06 +02004779#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004780static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4781{
4782 aes_free( &tkeys->enc );
4783 aes_free( &tkeys->dec );
4784
4785 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4786}
4787
Paul Bakker7eb013f2011-10-06 12:37:39 +00004788/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004789 * Allocate and initialize ticket keys
4790 */
4791static int ssl_ticket_keys_init( ssl_context *ssl )
4792{
4793 int ret;
4794 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004795 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004796
4797 if( ssl->ticket_keys != NULL )
4798 return( 0 );
4799
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004800 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4801 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004802 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4803
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004804 aes_init( &tkeys->enc );
4805 aes_init( &tkeys->dec );
4806
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004807 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004808 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004809 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004810 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004811 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004812 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004813
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004814 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4815 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4816 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4817 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004818 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004819 polarssl_free( tkeys );
4820 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004821 }
4822
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004823 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004824 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004825 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004826 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004827 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004828 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004829
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004830 ssl->ticket_keys = tkeys;
4831
4832 return( 0 );
4833}
Paul Bakkera503a632013-08-14 13:48:06 +02004834#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004835
4836/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004837 * SSL set accessors
4838 */
4839void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4840{
4841 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004842
Paul Bakker606b4ba2013-08-14 16:52:14 +02004843#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004844 if( endpoint == SSL_IS_CLIENT )
4845 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004847}
4848
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004849int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004850{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004851#if defined(POLARSSL_SSL_PROTO_DTLS)
4852 if( transport == SSL_TRANSPORT_DATAGRAM )
4853 {
4854 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004855
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004856 ssl->out_hdr = ssl->out_buf;
4857 ssl->out_ctr = ssl->out_buf + 3;
4858 ssl->out_len = ssl->out_buf + 11;
4859 ssl->out_iv = ssl->out_buf + 13;
4860 ssl->out_msg = ssl->out_buf + 13;
4861
4862 ssl->in_hdr = ssl->in_buf;
4863 ssl->in_ctr = ssl->in_buf + 3;
4864 ssl->in_len = ssl->in_buf + 11;
4865 ssl->in_iv = ssl->in_buf + 13;
4866 ssl->in_msg = ssl->in_buf + 13;
4867
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004868 /* DTLS starts with TLS1.1 */
4869 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4870 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004871
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004872 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4873 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4874
4875 return( 0 );
4876 }
4877#endif
4878
4879 if( transport == SSL_TRANSPORT_STREAM )
4880 {
4881 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004882
4883 ssl->out_ctr = ssl->out_buf;
4884 ssl->out_hdr = ssl->out_buf + 8;
4885 ssl->out_len = ssl->out_buf + 11;
4886 ssl->out_iv = ssl->out_buf + 13;
4887 ssl->out_msg = ssl->out_buf + 13;
4888
4889 ssl->in_ctr = ssl->in_buf;
4890 ssl->in_hdr = ssl->in_buf + 8;
4891 ssl->in_len = ssl->in_buf + 11;
4892 ssl->in_iv = ssl->in_buf + 13;
4893 ssl->in_msg = ssl->in_buf + 13;
4894
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004895 return( 0 );
4896 }
4897
4898 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004899}
4900
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004901#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4902void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4903{
4904 ssl->anti_replay = mode;
4905}
4906#endif
4907
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02004908#if defined(POLARSSL_SSL_PROTO_DTLS)
4909void ssl_set_handshake_timeout( ssl_context *ssl, uint32_t min, uint32_t max )
4910{
4911 ssl->hs_timeout_min = min;
4912 ssl->hs_timeout_max = max;
4913}
4914#endif
4915
Paul Bakker5121ce52009-01-03 21:22:43 +00004916void ssl_set_authmode( ssl_context *ssl, int authmode )
4917{
4918 ssl->authmode = authmode;
4919}
4920
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004921#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004922void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004923 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004924 void *p_vrfy )
4925{
4926 ssl->f_vrfy = f_vrfy;
4927 ssl->p_vrfy = p_vrfy;
4928}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004929#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004930
Paul Bakker5121ce52009-01-03 21:22:43 +00004931void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004932 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004933 void *p_rng )
4934{
4935 ssl->f_rng = f_rng;
4936 ssl->p_rng = p_rng;
4937}
4938
4939void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004940 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004941 void *p_dbg )
4942{
4943 ssl->f_dbg = f_dbg;
4944 ssl->p_dbg = p_dbg;
4945}
4946
4947void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004948 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004949 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004950{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004951 if( p_recv != p_send )
4952 {
4953 ssl->f_recv = NULL;
4954 ssl->f_send = NULL;
4955 ssl->p_bio = NULL;
4956 return;
4957 }
4958
Paul Bakker5121ce52009-01-03 21:22:43 +00004959 ssl->f_recv = f_recv;
4960 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004961 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004962}
4963
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004964void ssl_set_bio_timeout( ssl_context *ssl,
4965 void *p_bio,
4966 int (*f_send)(void *, const unsigned char *, size_t),
4967 int (*f_recv)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnardc8d8e972014-10-01 15:01:39 +02004968 int (*f_recv_timeout)(void *, unsigned char *, size_t, uint32_t),
4969 uint32_t timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004970{
4971 ssl->p_bio = p_bio;
4972 ssl->f_send = f_send;
4973 ssl->f_recv = f_recv;
4974 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard27074302014-10-01 17:35:50 +02004975 ssl->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004976}
4977
Paul Bakker0a597072012-09-25 21:55:46 +00004978void ssl_set_session_cache( ssl_context *ssl,
4979 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4980 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004981{
Paul Bakker0a597072012-09-25 21:55:46 +00004982 ssl->f_get_cache = f_get_cache;
4983 ssl->p_get_cache = p_get_cache;
4984 ssl->f_set_cache = f_set_cache;
4985 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004986}
4987
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004988int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004989{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004990 int ret;
4991
4992 if( ssl == NULL ||
4993 session == NULL ||
4994 ssl->session_negotiate == NULL ||
4995 ssl->endpoint != SSL_IS_CLIENT )
4996 {
4997 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4998 }
4999
5000 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
5001 return( ret );
5002
Paul Bakker0a597072012-09-25 21:55:46 +00005003 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005004
5005 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005006}
5007
Paul Bakkerb68cad62012-08-23 08:34:18 +00005008void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00005009{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005010 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
5011 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
5012 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
5013 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
5014}
5015
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005016void ssl_set_ciphersuites_for_version( ssl_context *ssl,
5017 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02005018 int major, int minor )
5019{
5020 if( major != SSL_MAJOR_VERSION_3 )
5021 return;
5022
5023 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
5024 return;
5025
5026 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00005027}
5028
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005029#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005030/* Add a new (empty) key_cert entry an return a pointer to it */
5031static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
5032{
5033 ssl_key_cert *key_cert, *last;
5034
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005035 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
5036 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005037 return( NULL );
5038
5039 memset( key_cert, 0, sizeof( ssl_key_cert ) );
5040
5041 /* Append the new key_cert to the (possibly empty) current list */
5042 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01005043 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005044 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01005045 if( ssl->handshake != NULL )
5046 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01005047 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005048 else
5049 {
5050 last = ssl->key_cert;
5051 while( last->next != NULL )
5052 last = last->next;
5053 last->next = key_cert;
5054 }
5055
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005056 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005057}
5058
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005059void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00005060 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00005061{
5062 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00005063 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00005064 ssl->peer_cn = peer_cn;
5065}
5066
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005067int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005068 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00005069{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005070 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
5071
5072 if( key_cert == NULL )
5073 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5074
5075 key_cert->cert = own_cert;
5076 key_cert->key = pk_key;
5077
5078 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005079}
5080
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005081#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005082int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005083 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005084{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005085 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005086 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005087
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005088 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005089 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5090
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005091 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5092 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005093 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005094
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005095 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005096
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005097 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005098 if( ret != 0 )
5099 return( ret );
5100
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005101 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005102 return( ret );
5103
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005104 key_cert->cert = own_cert;
5105 key_cert->key_own_alloc = 1;
5106
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005107 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005108}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005109#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005110
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005111int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005112 void *rsa_key,
5113 rsa_decrypt_func rsa_decrypt,
5114 rsa_sign_func rsa_sign,
5115 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005116{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005117 int ret;
5118 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005119
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005120 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005121 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5122
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005123 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5124 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005125 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005126
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005127 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005128
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005129 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5130 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5131 return( ret );
5132
5133 key_cert->cert = own_cert;
5134 key_cert->key_own_alloc = 1;
5135
5136 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005137}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005138#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005139
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005140#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005141int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5142 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005143{
Paul Bakker6db455e2013-09-18 17:29:31 +02005144 if( psk == NULL || psk_identity == NULL )
5145 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5146
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005147 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005148 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5149
Paul Bakker6db455e2013-09-18 17:29:31 +02005150 if( ssl->psk != NULL )
5151 {
5152 polarssl_free( ssl->psk );
5153 polarssl_free( ssl->psk_identity );
5154 }
5155
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005156 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005157 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005158
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005159 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005160 ssl->psk_identity = (unsigned char *)
5161 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005162
5163 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5164 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5165
5166 memcpy( ssl->psk, psk, ssl->psk_len );
5167 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005168
5169 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005170}
5171
5172void ssl_set_psk_cb( ssl_context *ssl,
5173 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5174 size_t),
5175 void *p_psk )
5176{
5177 ssl->f_psk = f_psk;
5178 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005179}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005180#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005181
Paul Bakker48916f92012-09-16 19:57:18 +00005182#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005183int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005184{
5185 int ret;
5186
Paul Bakker48916f92012-09-16 19:57:18 +00005187 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005188 {
5189 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5190 return( ret );
5191 }
5192
Paul Bakker48916f92012-09-16 19:57:18 +00005193 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005194 {
5195 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5196 return( ret );
5197 }
5198
5199 return( 0 );
5200}
5201
Paul Bakker1b57b062011-01-06 15:48:19 +00005202int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5203{
5204 int ret;
5205
Paul Bakker66d5d072014-06-17 16:39:18 +02005206 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005207 {
5208 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5209 return( ret );
5210 }
5211
Paul Bakker66d5d072014-06-17 16:39:18 +02005212 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005213 {
5214 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5215 return( ret );
5216 }
5217
5218 return( 0 );
5219}
Paul Bakker48916f92012-09-16 19:57:18 +00005220#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005221
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005222#if defined(POLARSSL_SSL_SET_CURVES)
5223/*
5224 * Set the allowed elliptic curves
5225 */
5226void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5227{
5228 ssl->curve_list = curve_list;
5229}
5230#endif
5231
Paul Bakker0be444a2013-08-27 21:55:01 +02005232#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005233int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005234{
5235 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005236 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005237
5238 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005239
5240 if( ssl->hostname_len + 1 == 0 )
5241 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5242
Paul Bakker6e339b52013-07-03 13:37:05 +02005243 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005244
Paul Bakkerb15b8512012-01-13 13:44:06 +00005245 if( ssl->hostname == NULL )
5246 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5247
Paul Bakker3c2122f2013-06-24 19:03:14 +02005248 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005249 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005250
Paul Bakker40ea7de2009-05-03 10:18:48 +00005251 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005252
5253 return( 0 );
5254}
5255
Paul Bakker5701cdc2012-09-27 21:49:42 +00005256void ssl_set_sni( ssl_context *ssl,
5257 int (*f_sni)(void *, ssl_context *,
5258 const unsigned char *, size_t),
5259 void *p_sni )
5260{
5261 ssl->f_sni = f_sni;
5262 ssl->p_sni = p_sni;
5263}
Paul Bakker0be444a2013-08-27 21:55:01 +02005264#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005265
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005266#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005267int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005268{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005269 size_t cur_len, tot_len;
5270 const char **p;
5271
5272 /*
5273 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5274 * truncated". Check lengths now rather than later.
5275 */
5276 tot_len = 0;
5277 for( p = protos; *p != NULL; p++ )
5278 {
5279 cur_len = strlen( *p );
5280 tot_len += cur_len;
5281
5282 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5283 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5284 }
5285
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005286 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005287
5288 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005289}
5290
5291const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5292{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005293 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005294}
5295#endif /* POLARSSL_SSL_ALPN */
5296
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005297static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005298{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005299 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5300 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5301 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5302 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005303 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005304 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005305 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005306
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005307 return( 0 );
5308}
5309
5310int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5311{
5312 if( ssl_check_version( ssl, major, minor ) != 0 )
5313 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5314
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005315 ssl->max_major_ver = major;
5316 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005317
5318 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005319}
5320
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005321int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005322{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005323 if( ssl_check_version( ssl, major, minor ) != 0 )
5324 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005325
5326 ssl->min_major_ver = major;
5327 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005328
5329 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005330}
5331
Paul Bakker05decb22013-08-15 13:33:48 +02005332#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005333int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5334{
Paul Bakker77e257e2013-12-16 15:29:52 +01005335 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005336 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005337 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005338 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005339 }
5340
5341 ssl->mfl_code = mfl_code;
5342
5343 return( 0 );
5344}
Paul Bakker05decb22013-08-15 13:33:48 +02005345#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005346
Paul Bakker1f2bc622013-08-15 13:45:55 +02005347#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005348int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005349{
5350 if( ssl->endpoint != SSL_IS_CLIENT )
5351 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5352
Paul Bakker8c1ede62013-07-19 14:14:37 +02005353 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005354
5355 return( 0 );
5356}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005357#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005358
Paul Bakker48916f92012-09-16 19:57:18 +00005359void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5360{
5361 ssl->disable_renegotiation = renegotiation;
5362}
5363
5364void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5365{
5366 ssl->allow_legacy_renegotiation = allow_legacy;
5367}
5368
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005369void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5370{
5371 ssl->renego_max_records = max_records;
5372}
5373
Paul Bakkera503a632013-08-14 13:48:06 +02005374#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005375int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5376{
5377 ssl->session_tickets = use_tickets;
5378
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005379 if( ssl->endpoint == SSL_IS_CLIENT )
5380 return( 0 );
5381
5382 if( ssl->f_rng == NULL )
5383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5384
5385 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005386}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005387
5388void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5389{
5390 ssl->ticket_lifetime = lifetime;
5391}
Paul Bakkera503a632013-08-14 13:48:06 +02005392#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005393
Paul Bakker5121ce52009-01-03 21:22:43 +00005394/*
5395 * SSL get accessors
5396 */
Paul Bakker23986e52011-04-24 08:57:21 +00005397size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005398{
5399 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5400}
5401
Paul Bakkerff60ee62010-03-16 21:09:09 +00005402int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005403{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005404 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005405}
5406
Paul Bakkere3166ce2011-01-27 17:40:50 +00005407const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005408{
Paul Bakker926c8e42013-03-06 10:23:34 +01005409 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005410 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005411
Paul Bakkere3166ce2011-01-27 17:40:50 +00005412 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005413}
5414
Paul Bakker43ca69c2011-01-15 17:35:19 +00005415const char *ssl_get_version( const ssl_context *ssl )
5416{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005417#if defined(POLARSSL_SSL_PROTO_DTLS)
5418 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5419 {
5420 switch( ssl->minor_ver )
5421 {
5422 case SSL_MINOR_VERSION_2:
5423 return( "DTLSv1.0" );
5424
5425 case SSL_MINOR_VERSION_3:
5426 return( "DTLSv1.2" );
5427
5428 default:
5429 return( "unknown (DTLS)" );
5430 }
5431 }
5432#endif
5433
Paul Bakker43ca69c2011-01-15 17:35:19 +00005434 switch( ssl->minor_ver )
5435 {
5436 case SSL_MINOR_VERSION_0:
5437 return( "SSLv3.0" );
5438
5439 case SSL_MINOR_VERSION_1:
5440 return( "TLSv1.0" );
5441
5442 case SSL_MINOR_VERSION_2:
5443 return( "TLSv1.1" );
5444
Paul Bakker1ef83d62012-04-11 12:09:53 +00005445 case SSL_MINOR_VERSION_3:
5446 return( "TLSv1.2" );
5447
Paul Bakker43ca69c2011-01-15 17:35:19 +00005448 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005449 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005450 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005451}
5452
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005453#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005454const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005455{
5456 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005457 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005458
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005459 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005460}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005461#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005462
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005463int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5464{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005465 if( ssl == NULL ||
5466 dst == NULL ||
5467 ssl->session == NULL ||
5468 ssl->endpoint != SSL_IS_CLIENT )
5469 {
5470 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5471 }
5472
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005473 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005474}
5475
Paul Bakker5121ce52009-01-03 21:22:43 +00005476/*
Paul Bakker1961b702013-01-25 14:49:24 +01005477 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005478 */
Paul Bakker1961b702013-01-25 14:49:24 +01005479int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005480{
Paul Bakker40e46942009-01-03 21:51:57 +00005481 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005482
Paul Bakker40e46942009-01-03 21:51:57 +00005483#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005484 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005485 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005486#endif
5487
Paul Bakker40e46942009-01-03 21:51:57 +00005488#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005489 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005490 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005491#endif
5492
Paul Bakker1961b702013-01-25 14:49:24 +01005493 return( ret );
5494}
5495
5496/*
5497 * Perform the SSL handshake
5498 */
5499int ssl_handshake( ssl_context *ssl )
5500{
5501 int ret = 0;
5502
5503 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5504
5505 while( ssl->state != SSL_HANDSHAKE_OVER )
5506 {
5507 ret = ssl_handshake_step( ssl );
5508
5509 if( ret != 0 )
5510 break;
5511 }
5512
Paul Bakker5121ce52009-01-03 21:22:43 +00005513 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5514
5515 return( ret );
5516}
5517
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005518#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005519/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005520 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005521 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005522static int ssl_write_hello_request( ssl_context *ssl )
5523{
5524 int ret;
5525
5526 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5527
5528 ssl->out_msglen = 4;
5529 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5530 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5531
5532 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5533 {
5534 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5535 return( ret );
5536 }
5537
5538 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5539
5540 return( 0 );
5541}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005542#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005543
5544/*
5545 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005546 * - any side: calling ssl_renegotiate(),
5547 * - client: receiving a HelloRequest during ssl_read(),
5548 * - server: receiving any handshake message on server during ssl_read() after
5549 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005550 * If the handshake doesn't complete due to waiting for I/O, it will continue
5551 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005552 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005553static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005554{
5555 int ret;
5556
5557 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5558
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005559 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5560 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005561
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005562 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5563 * the ServerHello will have message_seq = 1" */
5564#if defined(POLARSSL_SSL_PROTO_DTLS)
5565 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005566 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5567 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005568 if( ssl->endpoint == SSL_IS_SERVER )
5569 ssl->handshake->out_msg_seq = 1;
5570 else
5571 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005572 }
5573#endif
5574
Paul Bakker48916f92012-09-16 19:57:18 +00005575 ssl->state = SSL_HELLO_REQUEST;
5576 ssl->renegotiation = SSL_RENEGOTIATION;
5577
Paul Bakker48916f92012-09-16 19:57:18 +00005578 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5579 {
5580 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5581 return( ret );
5582 }
5583
5584 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5585
5586 return( 0 );
5587}
5588
5589/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005590 * Renegotiate current connection on client,
5591 * or request renegotiation on server
5592 */
5593int ssl_renegotiate( ssl_context *ssl )
5594{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005595 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005596
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005597#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005598 /* On server, just send the request */
5599 if( ssl->endpoint == SSL_IS_SERVER )
5600 {
5601 if( ssl->state != SSL_HANDSHAKE_OVER )
5602 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5603
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005604 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5605
5606 /* Did we already try/start sending HelloRequest? */
5607 if( ssl->out_left != 0 )
5608 return( ssl_flush_output( ssl ) );
5609
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005610 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005611 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005612#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005613
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005614#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005615 /*
5616 * On client, either start the renegotiation process or,
5617 * if already in progress, continue the handshake
5618 */
5619 if( ssl->renegotiation != SSL_RENEGOTIATION )
5620 {
5621 if( ssl->state != SSL_HANDSHAKE_OVER )
5622 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5623
5624 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5625 {
5626 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5627 return( ret );
5628 }
5629 }
5630 else
5631 {
5632 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5633 {
5634 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5635 return( ret );
5636 }
5637 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005638#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005639
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005640 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005641}
5642
5643/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005644 * Receive application data decrypted from the SSL layer
5645 */
Paul Bakker23986e52011-04-24 08:57:21 +00005646int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005647{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005648 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005649 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005650
5651 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5652
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005653#if defined(POLARSSL_SSL_PROTO_DTLS)
5654 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5655 ssl->handshake != NULL &&
5656 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5657 {
5658 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5659 return( ret );
5660
5661 if( ( ret = ssl_resend( ssl ) ) != 0 )
5662 return( ret );
5663 }
5664#endif
5665
Paul Bakker5121ce52009-01-03 21:22:43 +00005666 if( ssl->state != SSL_HANDSHAKE_OVER )
5667 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005668 ret = ssl_handshake( ssl );
5669 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5670 {
5671 record_read = 1;
5672 }
5673 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005674 {
5675 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5676 return( ret );
5677 }
5678 }
5679
5680 if( ssl->in_offt == NULL )
5681 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005682 /* Start timer if not already running */
5683 if( ssl->time_limit == 0 )
5684 ssl_set_timer( ssl, ssl->read_timeout );
5685
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005686 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005687 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005688 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5689 {
5690 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5691 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005692
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005693 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5694 return( ret );
5695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005696 }
5697
5698 if( ssl->in_msglen == 0 &&
5699 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5700 {
5701 /*
5702 * OpenSSL sends empty messages to randomize the IV
5703 */
5704 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5705 {
Paul Bakker831a7552011-05-18 13:32:51 +00005706 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5707 return( 0 );
5708
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5710 return( ret );
5711 }
5712 }
5713
Paul Bakker48916f92012-09-16 19:57:18 +00005714 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5715 {
5716 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5717
5718 if( ssl->endpoint == SSL_IS_CLIENT &&
5719 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005720 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005721 {
5722 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005723
5724 /* With DTLS, drop the packet (probably from last handshake) */
5725#if defined(POLARSSL_SSL_PROTO_DTLS)
5726 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5727 return( POLARSSL_ERR_NET_WANT_READ );
5728#endif
5729 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5730 }
5731
5732 if( ssl->endpoint == SSL_IS_SERVER &&
5733 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5734 {
5735 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5736
5737 /* With DTLS, drop the packet (probably from last handshake) */
5738#if defined(POLARSSL_SSL_PROTO_DTLS)
5739 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5740 return( POLARSSL_ERR_NET_WANT_READ );
5741#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005742 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5743 }
5744
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005745 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5746 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005747 ssl->allow_legacy_renegotiation ==
5748 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005749 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005750 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005751
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005752#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005753 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005754 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005755 /*
5756 * SSLv3 does not have a "no_renegotiation" alert
5757 */
5758 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5759 return( ret );
5760 }
5761 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005762#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005763#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5764 defined(POLARSSL_SSL_PROTO_TLS1_2)
5765 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005766 {
5767 if( ( ret = ssl_send_alert_message( ssl,
5768 SSL_ALERT_LEVEL_WARNING,
5769 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5770 {
5771 return( ret );
5772 }
Paul Bakker48916f92012-09-16 19:57:18 +00005773 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005774 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005775#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5776 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005777 {
5778 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005779 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005780 }
Paul Bakker48916f92012-09-16 19:57:18 +00005781 }
5782 else
5783 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005784#if defined(POLARSSL_SSL_PROTO_DTLS)
5785 /* DTLS clients need to know renego is server-initiated */
5786 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5787 ssl->endpoint == SSL_IS_CLIENT )
5788 {
5789 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5790 }
5791#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005792 ret = ssl_start_renegotiation( ssl );
5793 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5794 {
5795 record_read = 1;
5796 }
5797 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005798 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005799 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005800 return( ret );
5801 }
Paul Bakker48916f92012-09-16 19:57:18 +00005802 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005803
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005804 /* If a non-handshake record was read during renego, fallthrough,
5805 * else tell the user they should call ssl_read() again */
5806 if( ! record_read )
5807 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005808 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005809 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5810 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005811 ssl->renego_records_seen++;
5812
5813 if( ssl->renego_max_records >= 0 &&
5814 ssl->renego_records_seen > ssl->renego_max_records )
5815 {
5816 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5817 "but not honored by client" ) );
5818 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5819 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005820 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005821
5822 /* Fatal and closure alerts handled by ssl_read_record() */
5823 if( ssl->in_msgtype == SSL_MSG_ALERT )
5824 {
5825 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5826 return( POLARSSL_ERR_NET_WANT_READ );
5827 }
5828
5829 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005830 {
5831 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005832 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005833 }
5834
5835 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02005836
5837 /* We're going to return something now, cancel timer */
5838 ssl_set_timer( ssl, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005839 }
5840
5841 n = ( len < ssl->in_msglen )
5842 ? len : ssl->in_msglen;
5843
5844 memcpy( buf, ssl->in_offt, n );
5845 ssl->in_msglen -= n;
5846
5847 if( ssl->in_msglen == 0 )
5848 /* all bytes consumed */
5849 ssl->in_offt = NULL;
5850 else
5851 /* more data available */
5852 ssl->in_offt += n;
5853
5854 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5855
Paul Bakker23986e52011-04-24 08:57:21 +00005856 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005857}
5858
5859/*
5860 * Send application data to be encrypted by the SSL layer
5861 */
Paul Bakker23986e52011-04-24 08:57:21 +00005862int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005863{
Paul Bakker23986e52011-04-24 08:57:21 +00005864 int ret;
5865 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005866 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005867
5868 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5869
5870 if( ssl->state != SSL_HANDSHAKE_OVER )
5871 {
5872 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5873 {
5874 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5875 return( ret );
5876 }
5877 }
5878
Paul Bakker05decb22013-08-15 13:33:48 +02005879#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005880 /*
5881 * Assume mfl_code is correct since it was checked when set
5882 */
5883 max_len = mfl_code_to_length[ssl->mfl_code];
5884
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005885 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005886 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005887 */
5888 if( ssl->session_out != NULL &&
5889 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5890 {
5891 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5892 }
Paul Bakker05decb22013-08-15 13:33:48 +02005893#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005894
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005895 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005896
Paul Bakker5121ce52009-01-03 21:22:43 +00005897 if( ssl->out_left != 0 )
5898 {
5899 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5900 {
5901 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5902 return( ret );
5903 }
5904 }
Paul Bakker887bd502011-06-08 13:10:54 +00005905 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005906 {
Paul Bakker887bd502011-06-08 13:10:54 +00005907 ssl->out_msglen = n;
5908 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5909 memcpy( ssl->out_msg, buf, n );
5910
5911 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5912 {
5913 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5914 return( ret );
5915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005916 }
5917
5918 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5919
Paul Bakker23986e52011-04-24 08:57:21 +00005920 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005921}
5922
5923/*
5924 * Notify the peer that the connection is being closed
5925 */
5926int ssl_close_notify( ssl_context *ssl )
5927{
5928 int ret;
5929
5930 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5931
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005932 if( ssl->out_left != 0 )
5933 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005934
5935 if( ssl->state == SSL_HANDSHAKE_OVER )
5936 {
Paul Bakker48916f92012-09-16 19:57:18 +00005937 if( ( ret = ssl_send_alert_message( ssl,
5938 SSL_ALERT_LEVEL_WARNING,
5939 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005941 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005942 return( ret );
5943 }
5944 }
5945
5946 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5947
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005948 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005949}
5950
Paul Bakker48916f92012-09-16 19:57:18 +00005951void ssl_transform_free( ssl_transform *transform )
5952{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005953 if( transform == NULL )
5954 return;
5955
Paul Bakker48916f92012-09-16 19:57:18 +00005956#if defined(POLARSSL_ZLIB_SUPPORT)
5957 deflateEnd( &transform->ctx_deflate );
5958 inflateEnd( &transform->ctx_inflate );
5959#endif
5960
Paul Bakker84bbeb52014-07-01 14:53:22 +02005961 cipher_free( &transform->cipher_ctx_enc );
5962 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005963
Paul Bakker84bbeb52014-07-01 14:53:22 +02005964 md_free( &transform->md_ctx_enc );
5965 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005966
Paul Bakker34617722014-06-13 17:20:13 +02005967 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005968}
5969
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005970#if defined(POLARSSL_X509_CRT_PARSE_C)
5971static void ssl_key_cert_free( ssl_key_cert *key_cert )
5972{
5973 ssl_key_cert *cur = key_cert, *next;
5974
5975 while( cur != NULL )
5976 {
5977 next = cur->next;
5978
5979 if( cur->key_own_alloc )
5980 {
5981 pk_free( cur->key );
5982 polarssl_free( cur->key );
5983 }
5984 polarssl_free( cur );
5985
5986 cur = next;
5987 }
5988}
5989#endif /* POLARSSL_X509_CRT_PARSE_C */
5990
Paul Bakker48916f92012-09-16 19:57:18 +00005991void ssl_handshake_free( ssl_handshake_params *handshake )
5992{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005993 if( handshake == NULL )
5994 return;
5995
Paul Bakker48916f92012-09-16 19:57:18 +00005996#if defined(POLARSSL_DHM_C)
5997 dhm_free( &handshake->dhm_ctx );
5998#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005999#if defined(POLARSSL_ECDH_C)
6000 ecdh_free( &handshake->ecdh_ctx );
6001#endif
6002
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006003#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02006004 /* explicit void pointer cast for buggy MS compiler */
6005 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02006006#endif
6007
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02006008#if defined(POLARSSL_X509_CRT_PARSE_C) && \
6009 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
6010 /*
6011 * Free only the linked list wrapper, not the keys themselves
6012 * since the belong to the SNI callback
6013 */
6014 if( handshake->sni_key_cert != NULL )
6015 {
6016 ssl_key_cert *cur = handshake->sni_key_cert, *next;
6017
6018 while( cur != NULL )
6019 {
6020 next = cur->next;
6021 polarssl_free( cur );
6022 cur = next;
6023 }
6024 }
Paul Bakker9af723c2014-05-01 13:03:14 +02006025#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006026
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006027#if defined(POLARSSL_SSL_PROTO_DTLS)
6028 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02006029 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02006030 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02006031#endif
6032
Paul Bakker34617722014-06-13 17:20:13 +02006033 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006034}
6035
6036void ssl_session_free( ssl_session *session )
6037{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006038 if( session == NULL )
6039 return;
6040
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006041#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00006042 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00006043 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006044 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02006045 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00006046 }
Paul Bakkered27a042013-04-18 22:46:23 +02006047#endif
Paul Bakker0a597072012-09-25 21:55:46 +00006048
Paul Bakkera503a632013-08-14 13:48:06 +02006049#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006050 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02006051#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02006052
Paul Bakker34617722014-06-13 17:20:13 +02006053 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00006054}
6055
Paul Bakker5121ce52009-01-03 21:22:43 +00006056/*
6057 * Free an SSL context
6058 */
6059void ssl_free( ssl_context *ssl )
6060{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02006061 if( ssl == NULL )
6062 return;
6063
Paul Bakker5121ce52009-01-03 21:22:43 +00006064 SSL_DEBUG_MSG( 2, ( "=> free" ) );
6065
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006066 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006067 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006068 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
6069 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 }
6071
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006072 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01006074 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
6075 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 }
6077
Paul Bakker16770332013-10-11 09:59:44 +02006078#if defined(POLARSSL_ZLIB_SUPPORT)
6079 if( ssl->compress_buf != NULL )
6080 {
Paul Bakker34617722014-06-13 17:20:13 +02006081 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02006082 polarssl_free( ssl->compress_buf );
6083 }
6084#endif
6085
Paul Bakker40e46942009-01-03 21:51:57 +00006086#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00006087 mpi_free( &ssl->dhm_P );
6088 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00006089#endif
6090
Paul Bakker48916f92012-09-16 19:57:18 +00006091 if( ssl->transform )
6092 {
6093 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006094 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006095 }
6096
6097 if( ssl->handshake )
6098 {
6099 ssl_handshake_free( ssl->handshake );
6100 ssl_transform_free( ssl->transform_negotiate );
6101 ssl_session_free( ssl->session_negotiate );
6102
Paul Bakker6e339b52013-07-03 13:37:05 +02006103 polarssl_free( ssl->handshake );
6104 polarssl_free( ssl->transform_negotiate );
6105 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006106 }
6107
Paul Bakkerc0463502013-02-14 11:19:38 +01006108 if( ssl->session )
6109 {
6110 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006111 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006112 }
6113
Paul Bakkera503a632013-08-14 13:48:06 +02006114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006115 if( ssl->ticket_keys )
6116 {
6117 ssl_ticket_keys_free( ssl->ticket_keys );
6118 polarssl_free( ssl->ticket_keys );
6119 }
Paul Bakkera503a632013-08-14 13:48:06 +02006120#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006121
Paul Bakker0be444a2013-08-27 21:55:01 +02006122#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006123 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 {
Paul Bakker34617722014-06-13 17:20:13 +02006125 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006126 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 ssl->hostname_len = 0;
6128 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006129#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006130
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006131#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006132 if( ssl->psk != NULL )
6133 {
Paul Bakker34617722014-06-13 17:20:13 +02006134 polarssl_zeroize( ssl->psk, ssl->psk_len );
6135 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006136 polarssl_free( ssl->psk );
6137 polarssl_free( ssl->psk_identity );
6138 ssl->psk_len = 0;
6139 ssl->psk_identity_len = 0;
6140 }
6141#endif
6142
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006143#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006144 ssl_key_cert_free( ssl->key_cert );
6145#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006146
Paul Bakker05ef8352012-05-08 09:17:57 +00006147#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6148 if( ssl_hw_record_finish != NULL )
6149 {
6150 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6151 ssl_hw_record_finish( ssl );
6152 }
6153#endif
6154
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006155#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006156 polarssl_free( ssl->cli_id );
6157#endif
6158
Paul Bakker5121ce52009-01-03 21:22:43 +00006159 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006160
Paul Bakker86f04f42013-02-14 11:20:09 +01006161 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006162 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006163}
6164
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006165#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006166/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006167 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006168 */
6169unsigned char ssl_sig_from_pk( pk_context *pk )
6170{
6171#if defined(POLARSSL_RSA_C)
6172 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6173 return( SSL_SIG_RSA );
6174#endif
6175#if defined(POLARSSL_ECDSA_C)
6176 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6177 return( SSL_SIG_ECDSA );
6178#endif
6179 return( SSL_SIG_ANON );
6180}
6181
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006182pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6183{
6184 switch( sig )
6185 {
6186#if defined(POLARSSL_RSA_C)
6187 case SSL_SIG_RSA:
6188 return( POLARSSL_PK_RSA );
6189#endif
6190#if defined(POLARSSL_ECDSA_C)
6191 case SSL_SIG_ECDSA:
6192 return( POLARSSL_PK_ECDSA );
6193#endif
6194 default:
6195 return( POLARSSL_PK_NONE );
6196 }
6197}
Paul Bakker9af723c2014-05-01 13:03:14 +02006198#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006199
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006200/*
6201 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6202 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006203md_type_t ssl_md_alg_from_hash( unsigned char hash )
6204{
6205 switch( hash )
6206 {
6207#if defined(POLARSSL_MD5_C)
6208 case SSL_HASH_MD5:
6209 return( POLARSSL_MD_MD5 );
6210#endif
6211#if defined(POLARSSL_SHA1_C)
6212 case SSL_HASH_SHA1:
6213 return( POLARSSL_MD_SHA1 );
6214#endif
6215#if defined(POLARSSL_SHA256_C)
6216 case SSL_HASH_SHA224:
6217 return( POLARSSL_MD_SHA224 );
6218 case SSL_HASH_SHA256:
6219 return( POLARSSL_MD_SHA256 );
6220#endif
6221#if defined(POLARSSL_SHA512_C)
6222 case SSL_HASH_SHA384:
6223 return( POLARSSL_MD_SHA384 );
6224 case SSL_HASH_SHA512:
6225 return( POLARSSL_MD_SHA512 );
6226#endif
6227 default:
6228 return( POLARSSL_MD_NONE );
6229 }
6230}
6231
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006232#if defined(POLARSSL_SSL_SET_CURVES)
6233/*
6234 * Check is a curve proposed by the peer is in our list.
6235 * Return 1 if we're willing to use it, 0 otherwise.
6236 */
6237int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6238{
6239 const ecp_group_id *gid;
6240
6241 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6242 if( *gid == grp_id )
6243 return( 1 );
6244
6245 return( 0 );
6246}
Paul Bakker9af723c2014-05-01 13:03:14 +02006247#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006248
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006249#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006250int ssl_check_cert_usage( const x509_crt *cert,
6251 const ssl_ciphersuite_t *ciphersuite,
6252 int cert_endpoint )
6253{
6254#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6255 int usage = 0;
6256#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006257#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6258 const char *ext_oid;
6259 size_t ext_len;
6260#endif
6261
6262#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6263 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6264 ((void) cert);
6265 ((void) cert_endpoint);
6266#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006267
6268#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6269 if( cert_endpoint == SSL_IS_SERVER )
6270 {
6271 /* Server part of the key exchange */
6272 switch( ciphersuite->key_exchange )
6273 {
6274 case POLARSSL_KEY_EXCHANGE_RSA:
6275 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6276 usage = KU_KEY_ENCIPHERMENT;
6277 break;
6278
6279 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6280 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6281 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6282 usage = KU_DIGITAL_SIGNATURE;
6283 break;
6284
6285 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6286 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6287 usage = KU_KEY_AGREEMENT;
6288 break;
6289
6290 /* Don't use default: we want warnings when adding new values */
6291 case POLARSSL_KEY_EXCHANGE_NONE:
6292 case POLARSSL_KEY_EXCHANGE_PSK:
6293 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6294 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6295 usage = 0;
6296 }
6297 }
6298 else
6299 {
6300 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6301 usage = KU_DIGITAL_SIGNATURE;
6302 }
6303
6304 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6305 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006306#else
6307 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006308#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6309
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006310#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6311 if( cert_endpoint == SSL_IS_SERVER )
6312 {
6313 ext_oid = OID_SERVER_AUTH;
6314 ext_len = OID_SIZE( OID_SERVER_AUTH );
6315 }
6316 else
6317 {
6318 ext_oid = OID_CLIENT_AUTH;
6319 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6320 }
6321
6322 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6323 return( -1 );
6324#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6325
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006326 return( 0 );
6327}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006328#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006329
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006330/*
6331 * Convert version numbers to/from wire format
6332 * and, for DTLS, to/from TLS equivalent.
6333 *
6334 * For TLS this is the identity.
6335 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6336 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6337 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6338 */
6339void ssl_write_version( int major, int minor, int transport,
6340 unsigned char ver[2] )
6341{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006342#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006343 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006344 {
6345 if( minor == SSL_MINOR_VERSION_2 )
6346 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6347
6348 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6349 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6350 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006351 else
6352#else
6353 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006354#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006355 {
6356 ver[0] = (unsigned char) major;
6357 ver[1] = (unsigned char) minor;
6358 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006359}
6360
6361void ssl_read_version( int *major, int *minor, int transport,
6362 const unsigned char ver[2] )
6363{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006364#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006365 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006366 {
6367 *major = 255 - ver[0] + 2;
6368 *minor = 255 - ver[1] + 1;
6369
6370 if( *minor == SSL_MINOR_VERSION_1 )
6371 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6372 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006373 else
6374#else
6375 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006376#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006377 {
6378 *major = ver[0];
6379 *minor = ver[1];
6380 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006381}
6382
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006383#endif /* POLARSSL_SSL_TLS_C */