blob: 3f3d4ee57059ddfb52df977bca1cb289a5290582 [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 */
91void ssl_set_timer( ssl_context *ssl, unsigned long millisecs )
92{
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
Paul Bakker05decb22013-08-15 13:33:48 +0200112#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200113/*
114 * Convert max_fragment_length codes to length.
115 * RFC 6066 says:
116 * enum{
117 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
118 * } MaxFragmentLength;
119 * and we add 0 -> extension unused
120 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200121static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200122{
123 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
124 512, /* SSL_MAX_FRAG_LEN_512 */
125 1024, /* SSL_MAX_FRAG_LEN_1024 */
126 2048, /* SSL_MAX_FRAG_LEN_2048 */
127 4096, /* SSL_MAX_FRAG_LEN_4096 */
128};
Paul Bakker05decb22013-08-15 13:33:48 +0200129#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200130
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200131static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
132{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200133 ssl_session_free( dst );
134 memcpy( dst, src, sizeof( ssl_session ) );
135
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200137 if( src->peer_cert != NULL )
138 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200139 int ret;
140
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200141 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
142 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200143 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
144
Paul Bakkerb6b09562013-09-18 14:17:41 +0200145 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200146
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200147 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
148 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200149 {
150 polarssl_free( dst->peer_cert );
151 dst->peer_cert = NULL;
152 return( ret );
153 }
154 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200156
Paul Bakkera503a632013-08-14 13:48:06 +0200157#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200158 if( src->ticket != NULL )
159 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200160 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
161 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200162 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
163
164 memcpy( dst->ticket, src->ticket, src->ticket_len );
165 }
Paul Bakkera503a632013-08-14 13:48:06 +0200166#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200167
168 return( 0 );
169}
170
Paul Bakker05ef8352012-05-08 09:17:57 +0000171#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200172int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200173 const unsigned char *key_enc, const unsigned char *key_dec,
174 size_t keylen,
175 const unsigned char *iv_enc, const unsigned char *iv_dec,
176 size_t ivlen,
177 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200178 size_t maclen ) = NULL;
179int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
180int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
181int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
182int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
183int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200184#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000185
Paul Bakker5121ce52009-01-03 21:22:43 +0000186/*
187 * Key material generation
188 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200189#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200190static int ssl3_prf( const unsigned char *secret, size_t slen,
191 const char *label,
192 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000193 unsigned char *dstbuf, size_t dlen )
194{
195 size_t i;
196 md5_context md5;
197 sha1_context sha1;
198 unsigned char padding[16];
199 unsigned char sha1sum[20];
200 ((void)label);
201
Paul Bakker5b4af392014-06-26 12:09:34 +0200202 md5_init( &md5 );
203 sha1_init( &sha1 );
204
Paul Bakker5f70b252012-09-13 14:23:06 +0000205 /*
206 * SSLv3:
207 * block =
208 * MD5( secret + SHA1( 'A' + secret + random ) ) +
209 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
210 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
211 * ...
212 */
213 for( i = 0; i < dlen / 16; i++ )
214 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200215 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000216
217 sha1_starts( &sha1 );
218 sha1_update( &sha1, padding, 1 + i );
219 sha1_update( &sha1, secret, slen );
220 sha1_update( &sha1, random, rlen );
221 sha1_finish( &sha1, sha1sum );
222
223 md5_starts( &md5 );
224 md5_update( &md5, secret, slen );
225 md5_update( &md5, sha1sum, 20 );
226 md5_finish( &md5, dstbuf + i * 16 );
227 }
228
Paul Bakker5b4af392014-06-26 12:09:34 +0200229 md5_free( &md5 );
230 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000231
Paul Bakker34617722014-06-13 17:20:13 +0200232 polarssl_zeroize( padding, sizeof( padding ) );
233 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000234
235 return( 0 );
236}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200237#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000238
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200239#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200240static int tls1_prf( const unsigned char *secret, size_t slen,
241 const char *label,
242 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000243 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000244{
Paul Bakker23986e52011-04-24 08:57:21 +0000245 size_t nb, hs;
246 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200247 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 unsigned char tmp[128];
249 unsigned char h_i[20];
250
251 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
254 hs = ( slen + 1 ) / 2;
255 S1 = secret;
256 S2 = secret + slen - hs;
257
258 nb = strlen( label );
259 memcpy( tmp + 20, label, nb );
260 memcpy( tmp + 20 + nb, random, rlen );
261 nb += rlen;
262
263 /*
264 * First compute P_md5(secret,label+random)[0..dlen]
265 */
266 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
267
268 for( i = 0; i < dlen; i += 16 )
269 {
270 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
271 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
272
273 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
274
275 for( j = 0; j < k; j++ )
276 dstbuf[i + j] = h_i[j];
277 }
278
279 /*
280 * XOR out with P_sha1(secret,label+random)[0..dlen]
281 */
282 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
283
284 for( i = 0; i < dlen; i += 20 )
285 {
286 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
287 sha1_hmac( S2, hs, tmp, 20, tmp );
288
289 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200302#if defined(POLARSSL_SSL_PROTO_TLS1_2)
303#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200304static int tls_prf_sha256( const unsigned char *secret, size_t slen,
305 const char *label,
306 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000307 unsigned char *dstbuf, size_t dlen )
308{
309 size_t nb;
310 size_t i, j, k;
311 unsigned char tmp[128];
312 unsigned char h_i[32];
313
314 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
315 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
316
317 nb = strlen( label );
318 memcpy( tmp + 32, label, nb );
319 memcpy( tmp + 32 + nb, random, rlen );
320 nb += rlen;
321
322 /*
323 * Compute P_<hash>(secret, label + random)[0..dlen]
324 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200325 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000326
327 for( i = 0; i < dlen; i += 32 )
328 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200329 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
330 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000331
332 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
333
334 for( j = 0; j < k; j++ )
335 dstbuf[i + j] = h_i[j];
336 }
337
Paul Bakker34617722014-06-13 17:20:13 +0200338 polarssl_zeroize( tmp, sizeof( tmp ) );
339 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000340
341 return( 0 );
342}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200343#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000344
Paul Bakker9e36f042013-06-30 14:34:05 +0200345#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200346static int tls_prf_sha384( const unsigned char *secret, size_t slen,
347 const char *label,
348 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349 unsigned char *dstbuf, size_t dlen )
350{
351 size_t nb;
352 size_t i, j, k;
353 unsigned char tmp[128];
354 unsigned char h_i[48];
355
356 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
357 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
358
359 nb = strlen( label );
360 memcpy( tmp + 48, label, nb );
361 memcpy( tmp + 48 + nb, random, rlen );
362 nb += rlen;
363
364 /*
365 * Compute P_<hash>(secret, label + random)[0..dlen]
366 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200367 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000368
369 for( i = 0; i < dlen; i += 48 )
370 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200371 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
372 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000373
374 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
375
376 for( j = 0; j < k; j++ )
377 dstbuf[i + j] = h_i[j];
378 }
379
Paul Bakker34617722014-06-13 17:20:13 +0200380 polarssl_zeroize( tmp, sizeof( tmp ) );
381 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000382
383 return( 0 );
384}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200385#endif /* POLARSSL_SHA512_C */
386#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000387
Paul Bakker66d5d072014-06-17 16:39:18 +0200388static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200389
390#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
391 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200392static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200393#endif
Paul Bakker380da532012-04-18 16:10:25 +0000394
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200395#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200396static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
397static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200398#endif
399
400#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200401static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
402static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200403#endif
404
405#if defined(POLARSSL_SSL_PROTO_TLS1_2)
406#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200407static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
408static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
409static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200410#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100411
Paul Bakker9e36f042013-06-30 14:34:05 +0200412#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200413static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
414static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
415static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100416#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200417#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000418
Paul Bakker5121ce52009-01-03 21:22:43 +0000419int ssl_derive_keys( ssl_context *ssl )
420{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200421 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 unsigned char keyblk[256];
424 unsigned char *key1;
425 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100426 unsigned char *mac_enc;
427 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200428 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100429 const cipher_info_t *cipher_info;
430 const md_info_t *md_info;
431
Paul Bakker48916f92012-09-16 19:57:18 +0000432 ssl_session *session = ssl->session_negotiate;
433 ssl_transform *transform = ssl->transform_negotiate;
434 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
437
Paul Bakker68884e32013-01-07 18:20:04 +0100438 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
439 if( cipher_info == NULL )
440 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200441 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100442 transform->ciphersuite_info->cipher ) );
443 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
444 }
445
446 md_info = md_info_from_type( transform->ciphersuite_info->mac );
447 if( md_info == NULL )
448 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200449 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100450 transform->ciphersuite_info->mac ) );
451 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
452 }
453
Paul Bakker5121ce52009-01-03 21:22:43 +0000454 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000455 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000456 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200457#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000458 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000459 {
Paul Bakker48916f92012-09-16 19:57:18 +0000460 handshake->tls_prf = ssl3_prf;
461 handshake->calc_verify = ssl_calc_verify_ssl;
462 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000463 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200464 else
465#endif
466#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
467 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000468 {
Paul Bakker48916f92012-09-16 19:57:18 +0000469 handshake->tls_prf = tls1_prf;
470 handshake->calc_verify = ssl_calc_verify_tls;
471 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000472 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200473 else
474#endif
475#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200476#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200477 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
478 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000479 {
Paul Bakker48916f92012-09-16 19:57:18 +0000480 handshake->tls_prf = tls_prf_sha384;
481 handshake->calc_verify = ssl_calc_verify_tls_sha384;
482 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000483 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000484 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200485#endif
486#if defined(POLARSSL_SHA256_C)
487 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000488 {
Paul Bakker48916f92012-09-16 19:57:18 +0000489 handshake->tls_prf = tls_prf_sha256;
490 handshake->calc_verify = ssl_calc_verify_tls_sha256;
491 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000492 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200493 else
494#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200496 {
497 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200498 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200499 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000500
501 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000502 * SSLv3:
503 * master =
504 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
505 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
506 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200507 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200508 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 * master = PRF( premaster, "master secret", randbytes )[0..47]
510 */
Paul Bakker0a597072012-09-25 21:55:46 +0000511 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 {
Paul Bakker48916f92012-09-16 19:57:18 +0000513 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
514 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515
Paul Bakker48916f92012-09-16 19:57:18 +0000516 handshake->tls_prf( handshake->premaster, handshake->pmslen,
517 "master secret",
518 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519
Paul Bakker34617722014-06-13 17:20:13 +0200520 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 }
522 else
523 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
524
525 /*
526 * Swap the client and server random values.
527 */
Paul Bakker48916f92012-09-16 19:57:18 +0000528 memcpy( tmp, handshake->randbytes, 64 );
529 memcpy( handshake->randbytes, tmp + 32, 32 );
530 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200531 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533 /*
534 * SSLv3:
535 * key block =
536 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
537 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
538 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
539 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
540 * ...
541 *
542 * TLSv1:
543 * key block = PRF( master, "key expansion", randbytes )
544 */
Paul Bakker48916f92012-09-16 19:57:18 +0000545 handshake->tls_prf( session->master, 48, "key expansion",
546 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
Paul Bakker48916f92012-09-16 19:57:18 +0000548 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
549 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
550 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
551 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
553
Paul Bakker34617722014-06-13 17:20:13 +0200554 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 /*
557 * Determine the appropriate key, IV and MAC length.
558 */
Paul Bakker68884e32013-01-07 18:20:04 +0100559
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200560 transform->keylen = cipher_info->key_length / 8;
561
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200562 if( cipher_info->mode == POLARSSL_MODE_GCM ||
563 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200565 transform->maclen = 0;
566
Paul Bakker68884e32013-01-07 18:20:04 +0100567 transform->ivlen = 12;
568 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200569
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200570 /* Minimum length is expicit IV + tag */
571 transform->minlen = transform->ivlen - transform->fixed_ivlen
572 + ( transform->ciphersuite_info->flags &
573 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100574 }
575 else
576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 int ret;
578
579 /* Initialize HMAC contexts */
580 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
581 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100582 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200583 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
584 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100585 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200587 /* Get MAC length */
588 transform->maclen = md_get_size( md_info );
589
590#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
591 /*
592 * If HMAC is to be truncated, we shall keep the leftmost bytes,
593 * (rfc 6066 page 13 or rfc 2104 section 4),
594 * so we only need to adjust the length here.
595 */
596 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
597 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
598#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
599
600 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100601 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000602
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200603 /* Minimum length */
604 if( cipher_info->mode == POLARSSL_MODE_STREAM )
605 transform->minlen = transform->maclen;
606 else
Paul Bakker68884e32013-01-07 18:20:04 +0100607 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200608 /*
609 * GenericBlockCipher:
610 * first multiple of blocklen greater than maclen
611 * + IV except for SSL3 and TLS 1.0
612 */
613 transform->minlen = transform->maclen
614 + cipher_info->block_size
615 - transform->maclen % cipher_info->block_size;
616
617#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
618 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
619 ssl->minor_ver == SSL_MINOR_VERSION_1 )
620 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100621 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200622#endif
623#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
624 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
625 ssl->minor_ver == SSL_MINOR_VERSION_3 )
626 {
627 transform->minlen += transform->ivlen;
628 }
629 else
630#endif
631 {
632 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
633 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
634 }
Paul Bakker68884e32013-01-07 18:20:04 +0100635 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 }
637
638 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000639 transform->keylen, transform->minlen, transform->ivlen,
640 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642 /*
643 * Finally setup the cipher contexts, IVs and MAC secrets.
644 */
645 if( ssl->endpoint == SSL_IS_CLIENT )
646 {
Paul Bakker48916f92012-09-16 19:57:18 +0000647 key1 = keyblk + transform->maclen * 2;
648 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000649
Paul Bakker68884e32013-01-07 18:20:04 +0100650 mac_enc = keyblk;
651 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000653 /*
654 * This is not used in TLS v1.1.
655 */
Paul Bakker48916f92012-09-16 19:57:18 +0000656 iv_copy_len = ( transform->fixed_ivlen ) ?
657 transform->fixed_ivlen : transform->ivlen;
658 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
659 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000660 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 }
662 else
663 {
Paul Bakker48916f92012-09-16 19:57:18 +0000664 key1 = keyblk + transform->maclen * 2 + transform->keylen;
665 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Paul Bakker68884e32013-01-07 18:20:04 +0100667 mac_enc = keyblk + transform->maclen;
668 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000670 /*
671 * This is not used in TLS v1.1.
672 */
Paul Bakker48916f92012-09-16 19:57:18 +0000673 iv_copy_len = ( transform->fixed_ivlen ) ?
674 transform->fixed_ivlen : transform->ivlen;
675 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
676 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000677 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 }
679
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200680#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100681 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
682 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100683 if( transform->maclen > sizeof transform->mac_enc )
684 {
685 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200686 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100687 }
688
Paul Bakker68884e32013-01-07 18:20:04 +0100689 memcpy( transform->mac_enc, mac_enc, transform->maclen );
690 memcpy( transform->mac_dec, mac_dec, transform->maclen );
691 }
692 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200693#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200694#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
695 defined(POLARSSL_SSL_PROTO_TLS1_2)
696 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100697 {
698 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
699 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
700 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200701 else
702#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200703 {
704 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200705 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200706 }
Paul Bakker68884e32013-01-07 18:20:04 +0100707
Paul Bakker05ef8352012-05-08 09:17:57 +0000708#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200709 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000710 {
711 int ret = 0;
712
713 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
714
Paul Bakker07eb38b2012-12-19 14:42:06 +0100715 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
716 transform->iv_enc, transform->iv_dec,
717 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100718 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100719 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000720 {
721 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200722 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000723 }
724 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200725#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000726
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200727 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
728 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000729 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200730 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
731 return( ret );
732 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200733
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200734 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
735 cipher_info ) ) != 0 )
736 {
737 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
738 return( ret );
739 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200740
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
742 cipher_info->key_length,
743 POLARSSL_ENCRYPT ) ) != 0 )
744 {
745 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
746 return( ret );
747 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200748
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200749 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
750 cipher_info->key_length,
751 POLARSSL_DECRYPT ) ) != 0 )
752 {
753 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
754 return( ret );
755 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200756
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200757#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200758 if( cipher_info->mode == POLARSSL_MODE_CBC )
759 {
760 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
761 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200762 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200763 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
764 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200765 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200766
767 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
768 POLARSSL_PADDING_NONE ) ) != 0 )
769 {
770 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
771 return( ret );
772 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200774#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000775
Paul Bakker34617722014-06-13 17:20:13 +0200776 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000777
Paul Bakker2770fbd2012-07-03 13:30:23 +0000778#if defined(POLARSSL_ZLIB_SUPPORT)
779 // Initialize compression
780 //
Paul Bakker48916f92012-09-16 19:57:18 +0000781 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000782 {
Paul Bakker16770332013-10-11 09:59:44 +0200783 if( ssl->compress_buf == NULL )
784 {
785 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
786 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
787 if( ssl->compress_buf == NULL )
788 {
789 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
790 SSL_BUFFER_LEN ) );
791 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
792 }
793 }
794
Paul Bakker2770fbd2012-07-03 13:30:23 +0000795 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
796
Paul Bakker48916f92012-09-16 19:57:18 +0000797 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
798 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000799
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200800 if( deflateInit( &transform->ctx_deflate,
801 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000802 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000803 {
804 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
805 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
806 }
807 }
808#endif /* POLARSSL_ZLIB_SUPPORT */
809
Paul Bakker5121ce52009-01-03 21:22:43 +0000810 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
811
812 return( 0 );
813}
814
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000816void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000817{
818 md5_context md5;
819 sha1_context sha1;
820 unsigned char pad_1[48];
821 unsigned char pad_2[48];
822
Paul Bakker380da532012-04-18 16:10:25 +0000823 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakker48916f92012-09-16 19:57:18 +0000825 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
826 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Paul Bakker380da532012-04-18 16:10:25 +0000828 memset( pad_1, 0x36, 48 );
829 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000830
Paul Bakker48916f92012-09-16 19:57:18 +0000831 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000832 md5_update( &md5, pad_1, 48 );
833 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker380da532012-04-18 16:10:25 +0000835 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000836 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000837 md5_update( &md5, pad_2, 48 );
838 md5_update( &md5, hash, 16 );
839 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker48916f92012-09-16 19:57:18 +0000841 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000842 sha1_update( &sha1, pad_1, 40 );
843 sha1_finish( &sha1, hash + 16 );
844
845 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000846 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000847 sha1_update( &sha1, pad_2, 40 );
848 sha1_update( &sha1, hash + 16, 20 );
849 sha1_finish( &sha1, hash + 16 );
850
851 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
852 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
853
Paul Bakker5b4af392014-06-26 12:09:34 +0200854 md5_free( &md5 );
855 sha1_free( &sha1 );
856
Paul Bakker380da532012-04-18 16:10:25 +0000857 return;
858}
Paul Bakker9af723c2014-05-01 13:03:14 +0200859#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000860
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200861#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000862void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
863{
864 md5_context md5;
865 sha1_context sha1;
866
867 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
868
Paul Bakker48916f92012-09-16 19:57:18 +0000869 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
870 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakker48916f92012-09-16 19:57:18 +0000872 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000873 sha1_finish( &sha1, hash + 16 );
874
875 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
876 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
877
Paul Bakker5b4af392014-06-26 12:09:34 +0200878 md5_free( &md5 );
879 sha1_free( &sha1 );
880
Paul Bakker380da532012-04-18 16:10:25 +0000881 return;
882}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200883#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000884
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200885#if defined(POLARSSL_SSL_PROTO_TLS1_2)
886#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000887void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
888{
Paul Bakker9e36f042013-06-30 14:34:05 +0200889 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000890
891 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
892
Paul Bakker9e36f042013-06-30 14:34:05 +0200893 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
894 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000895
896 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
897 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
898
Paul Bakker5b4af392014-06-26 12:09:34 +0200899 sha256_free( &sha256 );
900
Paul Bakker380da532012-04-18 16:10:25 +0000901 return;
902}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200903#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000904
Paul Bakker9e36f042013-06-30 14:34:05 +0200905#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000906void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
907{
Paul Bakker9e36f042013-06-30 14:34:05 +0200908 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000909
910 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
911
Paul Bakker9e36f042013-06-30 14:34:05 +0200912 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
913 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000914
Paul Bakkerca4ab492012-04-18 14:23:57 +0000915 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
917
Paul Bakker5b4af392014-06-26 12:09:34 +0200918 sha512_free( &sha512 );
919
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 return;
921}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200922#endif /* POLARSSL_SHA512_C */
923#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000924
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200925#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200926int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
927{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200928 unsigned char *p = ssl->handshake->premaster;
929 unsigned char *end = p + sizeof( ssl->handshake->premaster );
930
931 /*
932 * PMS = struct {
933 * opaque other_secret<0..2^16-1>;
934 * opaque psk<0..2^16-1>;
935 * };
936 * with "other_secret" depending on the particular key exchange
937 */
938#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
939 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
940 {
941 if( end - p < 2 + (int) ssl->psk_len )
942 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
943
944 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
945 *(p++) = (unsigned char)( ssl->psk_len );
946 p += ssl->psk_len;
947 }
948 else
949#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200950#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
951 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
952 {
953 /*
954 * other_secret already set by the ClientKeyExchange message,
955 * and is 48 bytes long
956 */
957 *p++ = 0;
958 *p++ = 48;
959 p += 48;
960 }
961 else
962#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200963#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
964 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
965 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200966 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200967 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200968
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200969 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200970 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200971 p + 2, &len,
972 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 {
974 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
975 return( ret );
976 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200977 *(p++) = (unsigned char)( len >> 8 );
978 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979 p += len;
980
981 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
982 }
983 else
984#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
985#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
986 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
987 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200988 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200989 size_t zlen;
990
991 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200992 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200993 ssl->f_rng, ssl->p_rng ) ) != 0 )
994 {
995 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
996 return( ret );
997 }
998
999 *(p++) = (unsigned char)( zlen >> 8 );
1000 *(p++) = (unsigned char)( zlen );
1001 p += zlen;
1002
1003 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1004 }
1005 else
1006#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1007 {
1008 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001009 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 }
1011
1012 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001013 if( end - p < 2 + (int) ssl->psk_len )
1014 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1015
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001016 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1017 *(p++) = (unsigned char)( ssl->psk_len );
1018 memcpy( p, ssl->psk, ssl->psk_len );
1019 p += ssl->psk_len;
1020
1021 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1022
1023 return( 0 );
1024}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001025#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001026
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001027#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001028/*
1029 * SSLv3.0 MAC functions
1030 */
Paul Bakker68884e32013-01-07 18:20:04 +01001031static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1032 unsigned char *buf, size_t len,
1033 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001034{
1035 unsigned char header[11];
1036 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001037 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001038 int md_size = md_get_size( md_ctx->md_info );
1039 int md_type = md_get_type( md_ctx->md_info );
1040
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001041 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001042 if( md_type == POLARSSL_MD_MD5 )
1043 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001044 else
Paul Bakker68884e32013-01-07 18:20:04 +01001045 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
1047 memcpy( header, ctr, 8 );
1048 header[ 8] = (unsigned char) type;
1049 header[ 9] = (unsigned char)( len >> 8 );
1050 header[10] = (unsigned char)( len );
1051
Paul Bakker68884e32013-01-07 18:20:04 +01001052 memset( padding, 0x36, padlen );
1053 md_starts( md_ctx );
1054 md_update( md_ctx, secret, md_size );
1055 md_update( md_ctx, padding, padlen );
1056 md_update( md_ctx, header, 11 );
1057 md_update( md_ctx, buf, len );
1058 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
Paul Bakker68884e32013-01-07 18:20:04 +01001060 memset( padding, 0x5C, padlen );
1061 md_starts( md_ctx );
1062 md_update( md_ctx, secret, md_size );
1063 md_update( md_ctx, padding, padlen );
1064 md_update( md_ctx, buf + len, md_size );
1065 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001066}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001067#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001068
Paul Bakker5121ce52009-01-03 21:22:43 +00001069/*
1070 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001071 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001072static int ssl_encrypt_buf( ssl_context *ssl )
1073{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001074 const cipher_mode_t mode = cipher_get_cipher_mode(
1075 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
1077 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1078
1079 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001080 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001081 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1083 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1084 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001085 if( mode != POLARSSL_MODE_GCM &&
1086 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001088#if defined(POLARSSL_SSL_PROTO_SSL3)
1089 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1090 {
1091 ssl_mac( &ssl->transform_out->md_ctx_enc,
1092 ssl->transform_out->mac_enc,
1093 ssl->out_msg, ssl->out_msglen,
1094 ssl->out_ctr, ssl->out_msgtype );
1095 }
1096 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001097#endif
1098#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001099 defined(POLARSSL_SSL_PROTO_TLS1_2)
1100 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1101 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001102 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1103 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1104 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001105 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1106 ssl->out_msg, ssl->out_msglen );
1107 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1108 ssl->out_msg + ssl->out_msglen );
1109 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1110 }
1111 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001112#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001113 {
1114 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001115 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001116 }
1117
1118 SSL_DEBUG_BUF( 4, "computed mac",
1119 ssl->out_msg + ssl->out_msglen,
1120 ssl->transform_out->maclen );
1121
1122 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001123 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001124#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001126 /*
1127 * Encrypt
1128 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001129#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001130 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001132 int ret;
1133 size_t olen = 0;
1134
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1136 "including %d bytes of padding",
1137 ssl->out_msglen, 0 ) );
1138
1139 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1140 ssl->out_msg, ssl->out_msglen );
1141
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001142 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001143 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001144 ssl->transform_out->ivlen,
1145 ssl->out_msg, ssl->out_msglen,
1146 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001147 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001148 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001149 return( ret );
1150 }
1151
1152 if( ssl->out_msglen != olen )
1153 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001154 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001155 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001156 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001157 }
Paul Bakker68884e32013-01-07 18:20:04 +01001158 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001159#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001160#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1161 if( mode == POLARSSL_MODE_GCM ||
1162 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001163 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001164 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001165 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001166 unsigned char *enc_msg;
1167 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001168 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1169 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001170
Paul Bakkerca4ab492012-04-18 14:23:57 +00001171 memcpy( add_data, ssl->out_ctr, 8 );
1172 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001173 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1174 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001175 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1176 add_data[12] = ssl->out_msglen & 0xFF;
1177
1178 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1179 add_data, 13 );
1180
Paul Bakker68884e32013-01-07 18:20:04 +01001181 /*
1182 * Generate IV
1183 */
1184 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001185 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1186 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001187 if( ret != 0 )
1188 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001189
Paul Bakker68884e32013-01-07 18:20:04 +01001190 memcpy( ssl->out_iv,
1191 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1192 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001194 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001195 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001196
Paul Bakker68884e32013-01-07 18:20:04 +01001197 /*
1198 * Fix pointer positions and message length with added IV
1199 */
1200 enc_msg = ssl->out_msg;
1201 enc_msglen = ssl->out_msglen;
1202 ssl->out_msglen += ssl->transform_out->ivlen -
1203 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204
Paul Bakker68884e32013-01-07 18:20:04 +01001205 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1206 "including %d bytes of padding",
1207 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001208
Paul Bakker68884e32013-01-07 18:20:04 +01001209 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1210 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001213 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001214 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001215 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1216 ssl->transform_out->iv_enc,
1217 ssl->transform_out->ivlen,
1218 add_data, 13,
1219 enc_msg, enc_msglen,
1220 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001221 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001222 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001223 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001224 return( ret );
1225 }
1226
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001227 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001228 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001229 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001230 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001231 }
1232
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001233 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001235 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001238#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001239#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1240 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001241 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001243 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001245 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246
Paul Bakker48916f92012-09-16 19:57:18 +00001247 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1248 ssl->transform_out->ivlen;
1249 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001250 padlen = 0;
1251
1252 for( i = 0; i <= padlen; i++ )
1253 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1254
1255 ssl->out_msglen += padlen + 1;
1256
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 enc_msglen = ssl->out_msglen;
1258 enc_msg = ssl->out_msg;
1259
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001260#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001261 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001262 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1263 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001264 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001265 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001266 {
1267 /*
1268 * Generate IV
1269 */
Paul Bakker48916f92012-09-16 19:57:18 +00001270 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1271 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001272 if( ret != 0 )
1273 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001274
Paul Bakker92be97b2013-01-02 17:30:03 +01001275 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001276 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001277
1278 /*
1279 * Fix pointer positions and message length with added IV
1280 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001281 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001282 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001283 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001284 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001285#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001286
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001289 ssl->out_msglen, ssl->transform_out->ivlen,
1290 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291
1292 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001293 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001295 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001296 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001297 ssl->transform_out->ivlen,
1298 enc_msg, enc_msglen,
1299 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001300 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001301 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001302 return( ret );
1303 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001304
Paul Bakkercca5b812013-08-31 17:40:26 +02001305 if( enc_msglen != olen )
1306 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001307 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001308 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001309 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001310
1311#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001312 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1313 {
1314 /*
1315 * Save IV in SSL3 and TLS1
1316 */
1317 memcpy( ssl->transform_out->iv_enc,
1318 ssl->transform_out->cipher_ctx_enc.iv,
1319 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001321#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001323 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001324#endif /* POLARSSL_CIPHER_MODE_CBC &&
1325 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001326 {
1327 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001328 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
1331 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1332
1333 return( 0 );
1334}
1335
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001336#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001337
Paul Bakker5121ce52009-01-03 21:22:43 +00001338static int ssl_decrypt_buf( ssl_context *ssl )
1339{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001340 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001341 const cipher_mode_t mode = cipher_get_cipher_mode(
1342 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001343#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1344 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1345 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1346 size_t padlen = 0, correct = 1;
1347#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
1349 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1350
Paul Bakker48916f92012-09-16 19:57:18 +00001351 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 {
1353 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001354 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001355 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 }
1357
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001358#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001359 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001360 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001361 int ret;
1362 size_t olen = 0;
1363
Paul Bakker68884e32013-01-07 18:20:04 +01001364 padlen = 0;
1365
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001366 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001367 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001368 ssl->transform_in->ivlen,
1369 ssl->in_msg, ssl->in_msglen,
1370 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001371 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001372 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001373 return( ret );
1374 }
1375
1376 if( ssl->in_msglen != olen )
1377 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001378 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001379 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001380 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 }
Paul Bakker68884e32013-01-07 18:20:04 +01001382 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001383#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001384#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1385 if( mode == POLARSSL_MODE_GCM ||
1386 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001387 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001388 int ret;
1389 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001390 unsigned char *dec_msg;
1391 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001393 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1394 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001395 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1396 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001397
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001398 if( ssl->in_msglen < explicit_iv_len + taglen )
1399 {
1400 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1401 "+ taglen (%d)", ssl->in_msglen,
1402 explicit_iv_len, taglen ) );
1403 return( POLARSSL_ERR_SSL_INVALID_MAC );
1404 }
1405 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1406
Paul Bakker68884e32013-01-07 18:20:04 +01001407 dec_msg = ssl->in_msg;
1408 dec_msg_result = ssl->in_msg;
1409 ssl->in_msglen = dec_msglen;
1410
1411 memcpy( add_data, ssl->in_ctr, 8 );
1412 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001413 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1414 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001415 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1416 add_data[12] = ssl->in_msglen & 0xFF;
1417
1418 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1419 add_data, 13 );
1420
1421 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1422 ssl->in_iv,
1423 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1424
1425 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1426 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001427 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001428
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001430 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001431 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001432 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1433 ssl->transform_in->iv_dec,
1434 ssl->transform_in->ivlen,
1435 add_data, 13,
1436 dec_msg, dec_msglen,
1437 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001438 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001439 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001440 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1441
1442 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1443 return( POLARSSL_ERR_SSL_INVALID_MAC );
1444
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001445 return( ret );
1446 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001447
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001448 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001449 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001450 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001451 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001452 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001453 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001455#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001456#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1457 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001458 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 {
Paul Bakker45829992013-01-03 14:52:21 +01001460 /*
1461 * Decrypt and check the padding
1462 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001463 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 unsigned char *dec_msg;
1465 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001466 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001467 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001468 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469
Paul Bakker5121ce52009-01-03 21:22:43 +00001470 /*
Paul Bakker45829992013-01-03 14:52:21 +01001471 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 */
Paul Bakker48916f92012-09-16 19:57:18 +00001473 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 {
1475 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001476 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001477 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 }
1479
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001480#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001481 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1482 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483#endif
Paul Bakker45829992013-01-03 14:52:21 +01001484
1485 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1486 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1487 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001488 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1489 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1490 ssl->transform_in->ivlen,
1491 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001492 return( POLARSSL_ERR_SSL_INVALID_MAC );
1493 }
1494
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001495 dec_msglen = ssl->in_msglen;
1496 dec_msg = ssl->in_msg;
1497 dec_msg_result = ssl->in_msg;
1498
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001499#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001500 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001501 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001502 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001503 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001504 {
Paul Bakker48916f92012-09-16 19:57:18 +00001505 dec_msglen -= ssl->transform_in->ivlen;
1506 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001507
Paul Bakker48916f92012-09-16 19:57:18 +00001508 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001509 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001510 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001512
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001513 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001514 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001515 ssl->transform_in->ivlen,
1516 dec_msg, dec_msglen,
1517 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001518 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001519 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001520 return( ret );
1521 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001522
Paul Bakkercca5b812013-08-31 17:40:26 +02001523 if( dec_msglen != olen )
1524 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001525 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001526 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001527 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001528
1529#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001530 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1531 {
1532 /*
1533 * Save IV in SSL3 and TLS1
1534 */
1535 memcpy( ssl->transform_in->iv_dec,
1536 ssl->transform_in->cipher_ctx_dec.iv,
1537 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001539#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001540
1541 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001542
1543 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1544 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001545#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001546 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1547 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001548#endif
Paul Bakker45829992013-01-03 14:52:21 +01001549 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001550 correct = 0;
1551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001553#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1555 {
Paul Bakker48916f92012-09-16 19:57:18 +00001556 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001558#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1560 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001561 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001562#endif
Paul Bakker45829992013-01-03 14:52:21 +01001563 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 }
1565 }
1566 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001567#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001568#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1569 defined(POLARSSL_SSL_PROTO_TLS1_2)
1570 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 {
1572 /*
Paul Bakker45829992013-01-03 14:52:21 +01001573 * TLSv1+: always check the padding up to the first failure
1574 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001576 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001577 size_t padding_idx = ssl->in_msglen - padlen - 1;
1578
Paul Bakker956c9e02013-12-19 14:42:28 +01001579 /*
1580 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001581 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001582 *
Paul Bakker61885c72014-04-25 12:59:03 +02001583 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1584 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001585 *
1586 * In both cases we reset padding_idx to a safe value (0) to
1587 * prevent out-of-buffer reads.
1588 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001589 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001590 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1591 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001592
1593 padding_idx *= correct;
1594
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001595 for( i = 1; i <= 256; i++ )
1596 {
1597 real_count &= ( i <= padlen );
1598 pad_count += real_count *
1599 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1600 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001601
1602 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001603
Paul Bakkerd66f0702013-01-31 16:57:45 +01001604#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001605 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001606 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001607#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001608 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001609 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001610 else
1611#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1612 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001613 {
1614 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001615 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001618 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001619#endif /* POLARSSL_CIPHER_MODE_CBC &&
1620 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001621 {
1622 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001623 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001624 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
1626 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1627 ssl->in_msg, ssl->in_msglen );
1628
1629 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001630 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001632#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1633 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1634 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001635 if( mode != POLARSSL_MODE_GCM &&
1636 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001637 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001638 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1639
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001640 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001642 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1643 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001645 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001647#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001648 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1649 {
1650 ssl_mac( &ssl->transform_in->md_ctx_dec,
1651 ssl->transform_in->mac_dec,
1652 ssl->in_msg, ssl->in_msglen,
1653 ssl->in_ctr, ssl->in_msgtype );
1654 }
1655 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001656#endif /* POLARSSL_SSL_PROTO_SSL3 */
1657#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 defined(POLARSSL_SSL_PROTO_TLS1_2)
1659 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1660 {
1661 /*
1662 * Process MAC and always update for padlen afterwards to make
1663 * total time independent of padlen
1664 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001665 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001666 *
1667 * Known timing attacks:
1668 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1669 *
1670 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1671 * correctly. (We round down instead of up, so -56 is the correct
1672 * value for our calculations instead of -55)
1673 */
1674 size_t j, extra_run = 0;
1675 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1676 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001677
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001679
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001680 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1681 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1682 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1684 ssl->in_msglen );
1685 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1686 ssl->in_msg + ssl->in_msglen );
1687 for( j = 0; j < extra_run; j++ )
1688 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001689
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001690 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1691 }
1692 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 POLARSSL_SSL_PROTO_TLS1_2 */
1695 {
1696 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001697 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001700 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1701 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1702 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001704 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001705 ssl->transform_in->maclen ) != 0 )
1706 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001707#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001708 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001709#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 correct = 0;
1711 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001712
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001713 /*
1714 * Finally check the correct flag
1715 */
1716 if( correct == 0 )
1717 return( POLARSSL_ERR_SSL_INVALID_MAC );
1718 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001719#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
1721 if( ssl->in_msglen == 0 )
1722 {
1723 ssl->nb_zero++;
1724
1725 /*
1726 * Three or more empty messages may be a DoS attack
1727 * (excessive CPU consumption).
1728 */
1729 if( ssl->nb_zero > 3 )
1730 {
1731 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1732 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001733 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734 }
1735 }
1736 else
1737 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001738
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001739#if defined(POLARSSL_SSL_PROTO_DTLS)
1740 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001741 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02001742 ; /* in_ctr read from peer, not maintained internally */
Manuel Pégourié-Gonnardea22ce52014-09-24 09:46:10 +02001743 }
1744 else
1745#endif
1746 {
1747 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1748 if( ++ssl->in_ctr[i - 1] != 0 )
1749 break;
1750
1751 /* The loop goes to its end iff the counter is wrapping */
1752 if( i == ssl_ep_len( ssl ) )
1753 {
1754 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1755 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1756 }
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001757 }
1758
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1760
1761 return( 0 );
1762}
1763
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764#if defined(POLARSSL_ZLIB_SUPPORT)
1765/*
1766 * Compression/decompression functions
1767 */
1768static int ssl_compress_buf( ssl_context *ssl )
1769{
1770 int ret;
1771 unsigned char *msg_post = ssl->out_msg;
1772 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001773 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001774
1775 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1776
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001777 if( len_pre == 0 )
1778 return( 0 );
1779
Paul Bakker2770fbd2012-07-03 13:30:23 +00001780 memcpy( msg_pre, ssl->out_msg, len_pre );
1781
1782 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1783 ssl->out_msglen ) );
1784
1785 SSL_DEBUG_BUF( 4, "before compression: output payload",
1786 ssl->out_msg, ssl->out_msglen );
1787
Paul Bakker48916f92012-09-16 19:57:18 +00001788 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1789 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1790 ssl->transform_out->ctx_deflate.next_out = msg_post;
1791 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001792
Paul Bakker48916f92012-09-16 19:57:18 +00001793 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794 if( ret != Z_OK )
1795 {
1796 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1797 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1798 }
1799
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001800 ssl->out_msglen = SSL_BUFFER_LEN -
1801 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802
Paul Bakker2770fbd2012-07-03 13:30:23 +00001803 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1804 ssl->out_msglen ) );
1805
1806 SSL_DEBUG_BUF( 4, "after compression: output payload",
1807 ssl->out_msg, ssl->out_msglen );
1808
1809 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1810
1811 return( 0 );
1812}
1813
1814static int ssl_decompress_buf( ssl_context *ssl )
1815{
1816 int ret;
1817 unsigned char *msg_post = ssl->in_msg;
1818 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001819 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001820
1821 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1822
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001823 if( len_pre == 0 )
1824 return( 0 );
1825
Paul Bakker2770fbd2012-07-03 13:30:23 +00001826 memcpy( msg_pre, ssl->in_msg, len_pre );
1827
1828 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1829 ssl->in_msglen ) );
1830
1831 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1832 ssl->in_msg, ssl->in_msglen );
1833
Paul Bakker48916f92012-09-16 19:57:18 +00001834 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1835 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1836 ssl->transform_in->ctx_inflate.next_out = msg_post;
1837 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001838
Paul Bakker48916f92012-09-16 19:57:18 +00001839 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001840 if( ret != Z_OK )
1841 {
1842 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1843 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1844 }
1845
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001846 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1847 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001848
Paul Bakker2770fbd2012-07-03 13:30:23 +00001849 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1850 ssl->in_msglen ) );
1851
1852 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1853 ssl->in_msg, ssl->in_msglen );
1854
1855 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1856
1857 return( 0 );
1858}
1859#endif /* POLARSSL_ZLIB_SUPPORT */
1860
Paul Bakker5121ce52009-01-03 21:22:43 +00001861/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001862 * Fill the input message buffer by appending data to it.
1863 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001864 *
1865 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1866 * available (from this read and/or a previous one). Otherwise, an error code
1867 * is returned (possibly EOF or WANT_READ).
1868 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001869 * With stream transport (TLS) on success ssl->in_left == nb_want, but
1870 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
1871 * since we always read a whole datagram at once.
1872 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02001873 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001874 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 */
Paul Bakker23986e52011-04-24 08:57:21 +00001876int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001877{
Paul Bakker23986e52011-04-24 08:57:21 +00001878 int ret;
1879 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
1881 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1882
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001883 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
1884 {
1885 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
1886 "or ssl_set_bio_timeout()" ) );
1887 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1888 }
1889
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001890 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001891 {
1892 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1893 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1894 }
1895
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001896#if defined(POLARSSL_SSL_PROTO_DTLS)
1897 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001898 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001899 /*
1900 * The point is, we need to always read a full datagram at once, so we
1901 * sometimes read more then requested, and handle the additional data.
1902 * It could be the rest of the current record (while fetching the
1903 * header) and/or some other records in the same datagram.
1904 */
1905
1906 /*
1907 * Move to the next record in the already read datagram if applicable
1908 */
1909 if( ssl->next_record_offset != 0 )
1910 {
1911 if( ssl->in_left < ssl->next_record_offset )
1912 {
1913 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1914 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1915 }
1916
1917 ssl->in_left -= ssl->next_record_offset;
1918
1919 if( ssl->in_left != 0 )
1920 {
1921 SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
1922 ssl->next_record_offset ) );
1923 memmove( ssl->in_hdr,
1924 ssl->in_hdr + ssl->next_record_offset,
1925 ssl->in_left );
1926 }
1927
1928 ssl->next_record_offset = 0;
1929 }
1930
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1932 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001933
1934 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001935 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001936 */
1937 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001938 {
1939 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001940 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02001941 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001942
1943 /*
1944 * A record can't be split accross datagrams. If we need to read but
1945 * are not at the beginning of a new record, the caller did something
1946 * wrong.
1947 */
1948 if( ssl->in_left != 0 )
1949 {
1950 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1951 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1952 }
1953
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001954 // TODO-DTLS: for now, use constant timeout = 1 sec/datagram
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001955 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001956 if( ssl->f_recv_timeout != NULL &&
1957 ssl->handshake != NULL ) /* No resend outside handshake */
1958 {
1959 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, 1 );
1960 }
1961 else
1962 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001963
Paul Bakker5121ce52009-01-03 21:22:43 +00001964 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1965
Paul Bakker831a7552011-05-18 13:32:51 +00001966 if( ret == 0 )
1967 return( POLARSSL_ERR_SSL_CONN_EOF );
1968
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001969 if( ret == POLARSSL_ERR_NET_TIMEOUT )
1970 {
1971 SSL_DEBUG_MSG( 2, ( "recv timeout" ) );
1972
1973 if( ( ret = ssl_resend( ssl ) ) != 0 )
1974 {
1975 SSL_DEBUG_RET( 1, "ssl_resend", ret );
1976 return( ret );
1977 }
1978
1979 return( POLARSSL_ERR_NET_WANT_READ );
1980 }
1981
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 if( ret < 0 )
1983 return( ret );
1984
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001985 ssl->in_left = ret;
1986 }
1987 else
1988#endif
1989 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001990 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1991 ssl->in_left, nb_want ) );
1992
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001993 while( ssl->in_left < nb_want )
1994 {
1995 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001996 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001997
1998 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1999 ssl->in_left, nb_want ) );
2000 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2001
2002 if( ret == 0 )
2003 return( POLARSSL_ERR_SSL_CONN_EOF );
2004
2005 if( ret < 0 )
2006 return( ret );
2007
2008 ssl->in_left += ret;
2009 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 }
2011
2012 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2013
2014 return( 0 );
2015}
2016
2017/*
2018 * Flush any data not yet written
2019 */
2020int ssl_flush_output( ssl_context *ssl )
2021{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002022 int ret;
2023 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
2025 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2026
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002027 if( ssl->f_send == NULL )
2028 {
2029 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2030 "or ssl_set_bio_timeout()" ) );
2031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2032 }
2033
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002034 /* Avoid incrementing counter if data is flushed */
2035 if( ssl->out_left == 0 )
2036 {
2037 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2038 return( 0 );
2039 }
2040
Paul Bakker5121ce52009-01-03 21:22:43 +00002041 while( ssl->out_left > 0 )
2042 {
2043 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002044 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002046 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2047 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002048 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002049
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2051
2052 if( ret <= 0 )
2053 return( ret );
2054
2055 ssl->out_left -= ret;
2056 }
2057
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002058 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002059 if( ++ssl->out_ctr[i - 1] != 0 )
2060 break;
2061
2062 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002063 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002064 {
2065 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2066 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2067 }
2068
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2070
2071 return( 0 );
2072}
2073
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002074/*
2075 * Functions to handle the DTLS retransmission state machine
2076 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002077#if defined(POLARSSL_SSL_PROTO_DTLS)
2078/*
2079 * Append current handshake message to current outgoing flight
2080 */
2081static int ssl_flight_append( ssl_context *ssl )
2082{
2083 ssl_flight_item *msg;
2084
2085 /* Allocate space for current message */
2086 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2087 {
2088 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2089 sizeof( ssl_flight_item ) ) );
2090 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2091 }
2092
2093 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2094 {
2095 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2096 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2097 }
2098
2099 /* Copy current handshake message with headers */
2100 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2101 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002102 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002103 msg->next = NULL;
2104
2105 /* Append to the current flight */
2106 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002107 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002108 else
2109 {
2110 ssl_flight_item *cur = ssl->handshake->flight;
2111 while( cur->next != NULL )
2112 cur = cur->next;
2113 cur->next = msg;
2114 }
2115
2116 return( 0 );
2117}
2118
2119/*
2120 * Free the current flight of handshake messages
2121 */
2122static void ssl_flight_free( ssl_flight_item *flight )
2123{
2124 ssl_flight_item *cur = flight;
2125 ssl_flight_item *next;
2126
2127 while( cur != NULL )
2128 {
2129 next = cur->next;
2130
2131 polarssl_free( cur->p );
2132 polarssl_free( cur );
2133
2134 cur = next;
2135 }
2136}
2137
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002138#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2139static void ssl_dtls_replay_reset( ssl_context *ssl );
2140#endif
2141
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002142/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002143 * Swap transform_out and out_ctr with the alternative ones
2144 */
2145static void ssl_swap_epochs( ssl_context *ssl )
2146{
2147 ssl_transform *tmp_transform;
2148 unsigned char tmp_out_ctr[8];
2149
2150 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2151 {
2152 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2153 return;
2154 }
2155
2156 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2157
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002158 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002159 tmp_transform = ssl->transform_out;
2160 ssl->transform_out = ssl->handshake->alt_transform_out;
2161 ssl->handshake->alt_transform_out = tmp_transform;
2162
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002163 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002164 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2165 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2166 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002167
2168 /* Adjust to the newly activated transform */
2169 if( ssl->transform_out != NULL &&
2170 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2171 {
2172 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2173 ssl->transform_out->fixed_ivlen;
2174 }
2175 else
2176 ssl->out_msg = ssl->out_iv;
2177
2178#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2179 if( ssl_hw_record_activate != NULL )
2180 {
2181 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2182 {
2183 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2184 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2185 }
2186 }
2187#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002188}
2189
2190/*
2191 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002192 *
2193 * Need to remember the current message in case flush_output returns
2194 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002195 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002196 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002197int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002198{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002199 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002200
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002201 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2202 {
2203 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2204
2205 ssl->handshake->cur_msg = ssl->handshake->flight;
2206 ssl_swap_epochs( ssl );
2207
2208 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
2209 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002210
2211 while( ssl->handshake->cur_msg != NULL )
2212 {
2213 int ret;
2214 ssl_flight_item *cur = ssl->handshake->cur_msg;
2215
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002216 /* Swap epochs before sending Finished: we can't do it after
2217 * sending ChangeCipherSpec, in case write returns WANT_READ.
2218 * Must be done before copying, may change out_msg pointer */
2219 if( cur->type == SSL_MSG_HANDSHAKE &&
2220 cur->p[0] == SSL_HS_FINISHED )
2221 {
2222 ssl_swap_epochs( ssl );
2223 }
2224
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002225 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002226 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002227 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002228
2229 ssl->handshake->cur_msg = cur->next;
2230
2231 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2232
2233 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2234 {
2235 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2236 return( ret );
2237 }
2238 }
2239
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002240 if( ssl->state == SSL_HANDSHAKE_OVER )
2241 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2242 else
2243 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002244
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002245 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002246
2247 return( 0 );
2248}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002249
2250/*
2251 * To be called when the last message of an incoming flight is received.
2252 */
2253void ssl_recv_flight_completed( ssl_context *ssl )
2254{
2255 /* We won't need to resend that one any more */
2256 ssl_flight_free( ssl->handshake->flight );
2257 ssl->handshake->flight = NULL;
2258 ssl->handshake->cur_msg = NULL;
2259
2260 /* The next incoming flight will start with this msg_seq */
2261 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2262
2263 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2264 ssl->in_msg[0] == SSL_HS_FINISHED )
2265 {
2266 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2267 }
2268 else
2269 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2270}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002271#endif /* POLARSSL_SSL_PROTO_DTLS */
2272
Paul Bakker5121ce52009-01-03 21:22:43 +00002273/*
2274 * Record layer functions
2275 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002276
2277/*
2278 * Write current record.
2279 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2280 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002281int ssl_write_record( ssl_context *ssl )
2282{
Paul Bakker05ef8352012-05-08 09:17:57 +00002283 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002284 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
2286 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2287
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002288#if defined(POLARSSL_SSL_PROTO_DTLS)
2289 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2290 ssl->handshake != NULL &&
2291 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2292 {
2293 ; /* Skip special handshake treatment when resending */
2294 }
2295 else
2296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002297 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2298 {
2299 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2300 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2301 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2302
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002303 /*
2304 * DTLS has additional fields in the Handshake layer,
2305 * between the length field and the actual payload:
2306 * uint16 message_seq;
2307 * uint24 fragment_offset;
2308 * uint24 fragment_length;
2309 */
2310#if defined(POLARSSL_SSL_PROTO_DTLS)
2311 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2312 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002313 /* Make room for the additional DTLS fields */
2314 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002315 ssl->out_msglen += 8;
2316 len += 8;
2317
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002318 /* Write message_seq and update it, except for HelloRequest */
2319 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2320 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002321 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2322 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2323 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002324 }
2325 else
2326 {
2327 ssl->out_msg[4] = 0;
2328 ssl->out_msg[5] = 0;
2329 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002330
2331 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2332 memset( ssl->out_msg + 6, 0x00, 3 );
2333 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002334 }
2335#endif /* POLARSSL_SSL_PROTO_DTLS */
2336
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002337 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2338 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 }
2340
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002341 /* Save handshake and CCS messages for resending */
2342#if defined(POLARSSL_SSL_PROTO_DTLS)
2343 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2344 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002345 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002346 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2347 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2348 {
2349 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2350 {
2351 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2352 return( ret );
2353 }
2354 }
2355#endif
2356
Paul Bakker2770fbd2012-07-03 13:30:23 +00002357#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002358 if( ssl->transform_out != NULL &&
2359 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002360 {
2361 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2362 {
2363 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2364 return( ret );
2365 }
2366
2367 len = ssl->out_msglen;
2368 }
2369#endif /*POLARSSL_ZLIB_SUPPORT */
2370
Paul Bakker05ef8352012-05-08 09:17:57 +00002371#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002372 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002374 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002375
Paul Bakker05ef8352012-05-08 09:17:57 +00002376 ret = ssl_hw_record_write( ssl );
2377 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2378 {
2379 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002380 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002381 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002382
2383 if( ret == 0 )
2384 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002385 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002386#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002387 if( !done )
2388 {
2389 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002390 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2391 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002392
2393 ssl->out_len[0] = (unsigned char)( len >> 8 );
2394 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002395
Paul Bakker48916f92012-09-16 19:57:18 +00002396 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002397 {
2398 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2399 {
2400 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2401 return( ret );
2402 }
2403
2404 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002405 ssl->out_len[0] = (unsigned char)( len >> 8 );
2406 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002407 }
2408
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002409 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002410
2411 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2412 "version = [%d:%d], msglen = %d",
2413 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002414 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002415
2416 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002417 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002418 }
2419
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2421 {
2422 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2423 return( ret );
2424 }
2425
2426 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2427
2428 return( 0 );
2429}
2430
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002431#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002432/*
2433 * Mark bits in bitmask (used for DTLS HS reassembly)
2434 */
2435static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2436{
2437 unsigned int start_bits, end_bits;
2438
2439 start_bits = 8 - ( offset % 8 );
2440 if( start_bits != 8 )
2441 {
2442 size_t first_byte_idx = offset / 8;
2443
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002444 /* Special case */
2445 if( len <= start_bits )
2446 {
2447 for( ; len != 0; len-- )
2448 mask[first_byte_idx] |= 1 << ( start_bits - len );
2449
2450 /* Avoid potential issues with offset or len becoming invalid */
2451 return;
2452 }
2453
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002454 offset += start_bits; /* Now offset % 8 == 0 */
2455 len -= start_bits;
2456
2457 for( ; start_bits != 0; start_bits-- )
2458 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2459 }
2460
2461 end_bits = len % 8;
2462 if( end_bits != 0 )
2463 {
2464 size_t last_byte_idx = ( offset + len ) / 8;
2465
2466 len -= end_bits; /* Now len % 8 == 0 */
2467
2468 for( ; end_bits != 0; end_bits-- )
2469 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2470 }
2471
2472 memset( mask + offset / 8, 0xFF, len / 8 );
2473}
2474
2475/*
2476 * Check that bitmask is full
2477 */
2478static int ssl_bitmask_check( unsigned char *mask, size_t len )
2479{
2480 size_t i;
2481
2482 for( i = 0; i < len / 8; i++ )
2483 if( mask[i] != 0xFF )
2484 return( -1 );
2485
2486 for( i = 0; i < len % 8; i++ )
2487 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2488 return( -1 );
2489
2490 return( 0 );
2491}
2492
2493/*
2494 * Reassemble fragmented DTLS handshake messages.
2495 *
2496 * Use a temporary buffer for reassembly, divided in two parts:
2497 * - the first holds the reassembled message (including handshake header),
2498 * - the second holds a bitmask indicating which parts of the message
2499 * (excluding headers) have been received so far.
2500 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002501static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2502{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002503 unsigned char *msg, *bitmask;
2504 size_t frag_len, frag_off;
2505 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2506
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002507 if( ssl->handshake == NULL )
2508 {
2509 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2510 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2511 }
2512
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002513 /*
2514 * For first fragment, check size and allocate buffer
2515 */
2516 if( ssl->handshake->hs_msg == NULL )
2517 {
2518 size_t alloc_len;
2519
2520 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2521 msg_len ) );
2522
2523 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2524 {
2525 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2526 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2527 }
2528
2529 /* The bitmask needs one bit per byte of message excluding header */
2530 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2531
2532 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2533 if( ssl->handshake->hs_msg == NULL )
2534 {
2535 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2536 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2537 }
2538
2539 memset( ssl->handshake->hs_msg, 0, alloc_len );
2540
2541 /* Prepare final header: copy msg_type, length and message_seq,
2542 * then add standardised fragment_offset and fragment_length */
2543 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2544 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2545 memcpy( ssl->handshake->hs_msg + 9,
2546 ssl->handshake->hs_msg + 1, 3 );
2547 }
2548 else
2549 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002550 /* Make sure msg_type and length are consistent */
2551 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002552 {
2553 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2554 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2555 }
2556 }
2557
2558 msg = ssl->handshake->hs_msg + 12;
2559 bitmask = msg + msg_len;
2560
2561 /*
2562 * Check and copy current fragment
2563 */
2564 frag_off = ( ssl->in_msg[6] << 16 ) |
2565 ( ssl->in_msg[7] << 8 ) |
2566 ssl->in_msg[8];
2567 frag_len = ( ssl->in_msg[9] << 16 ) |
2568 ( ssl->in_msg[10] << 8 ) |
2569 ssl->in_msg[11];
2570
2571 if( frag_off + frag_len > msg_len )
2572 {
2573 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2574 frag_off, frag_len, msg_len ) );
2575 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2576 }
2577
2578 if( frag_len + 12 > ssl->in_msglen )
2579 {
2580 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2581 frag_len, ssl->in_msglen ) );
2582 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2583 }
2584
2585 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2586 frag_off, frag_len ) );
2587
2588 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2589 ssl_bitmask_set( bitmask, frag_off, frag_len );
2590
2591 /*
2592 * Do we have the complete message by now?
2593 * If yes, finalize it, else ask to read the next record.
2594 */
2595 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2596 {
2597 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2598 return( POLARSSL_ERR_NET_WANT_READ );
2599 }
2600
2601 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2602
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002603 if( ssl->in_left > ssl->next_record_offset )
2604 {
2605 /*
2606 * We've got more data in the buffer after the current record,
2607 * that we don't want to overwrite. Move it before writing the
2608 * reassembled message, and adjust in_left and next_record_offset.
2609 */
2610 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2611 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2612 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2613
2614 /* First compute and check new lengths */
2615 ssl->next_record_offset = new_remain - ssl->in_hdr;
2616 ssl->in_left = ssl->next_record_offset + remain_len;
2617
2618 if( ssl->in_left > SSL_BUFFER_LEN -
2619 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2620 {
2621 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2622 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2623 }
2624
2625 memmove( new_remain, cur_remain, remain_len );
2626 }
2627
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002628 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2629
2630 polarssl_free( ssl->handshake->hs_msg );
2631 ssl->handshake->hs_msg = NULL;
2632
2633 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2634 ssl->in_msg, ssl->in_hslen );
2635
2636 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002637}
2638#endif /* POLARSSL_SSL_PROTO_DTLS */
2639
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002640static int ssl_prepare_handshake_record( ssl_context *ssl )
2641{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002642 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2643 {
2644 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2645 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002646 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002647 }
2648
2649 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2650 ( ssl->in_msg[1] << 16 ) |
2651 ( ssl->in_msg[2] << 8 ) |
2652 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002653
2654 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2655 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002656 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002657
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002658#if defined(POLARSSL_SSL_PROTO_DTLS)
2659 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002660 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002661 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002662 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002663
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002664 /* ssl->handshake is NULL when receiving ClientHello for renego */
2665 if( ssl->handshake != NULL &&
2666 recv_msg_seq != ssl->handshake->in_msg_seq )
2667 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002668 /* No sane server ever retransmits HelloVerifyRequest */
2669 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2670 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002671 {
2672 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2673 "message_seq = %d, start_of_flight = %d",
2674 recv_msg_seq,
2675 ssl->handshake->in_flight_start_seq ) );
2676
2677 if( ( ret = ssl_resend( ssl ) ) != 0 )
2678 {
2679 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2680 return( ret );
2681 }
2682 }
2683 else
2684 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002685 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002686 "message_seq = %d, expected = %d",
2687 recv_msg_seq,
2688 ssl->handshake->in_msg_seq ) );
2689 }
2690
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002691 return( POLARSSL_ERR_NET_WANT_READ );
2692 }
2693 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002694
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002695 /* Reassemble if current message is fragmented or reassembly is
2696 * already in progress */
2697 if( ssl->in_msglen < ssl->in_hslen ||
2698 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2699 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2700 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002701 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002702 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2703
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002704 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2705 {
2706 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2707 return( ret );
2708 }
2709 }
2710 }
2711 else
2712#endif /* POLARSSL_SSL_PROTO_DTLS */
2713 /* With TLS we don't handle fragmentation (for now) */
2714 if( ssl->in_msglen < ssl->in_hslen )
2715 {
2716 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002717 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002718 }
2719
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002720 if( ssl->state != SSL_HANDSHAKE_OVER )
2721 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2722
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002723 /* Handshake message is complete, increment counter */
2724#if defined(POLARSSL_SSL_PROTO_DTLS)
2725 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2726 ssl->handshake != NULL )
2727 {
2728 ssl->handshake->in_msg_seq++;
2729 }
2730#endif
2731
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002732 return( 0 );
2733}
2734
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002735/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002736 * DTLS anti-replay: RFC 6347 4.1.2.6
2737 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002738 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2739 * Bit n is set iff record number in_window_top - n has been seen.
2740 *
2741 * Usually, in_window_top is the last record number seen and the lsb of
2742 * in_window is set. The only exception is the initial state (record number 0
2743 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002744 */
2745#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2746static void ssl_dtls_replay_reset( ssl_context *ssl )
2747{
2748 ssl->in_window_top = 0;
2749 ssl->in_window = 0;
2750}
2751
2752static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2753{
2754 return( ( (uint64_t) buf[0] << 40 ) |
2755 ( (uint64_t) buf[1] << 32 ) |
2756 ( (uint64_t) buf[2] << 24 ) |
2757 ( (uint64_t) buf[3] << 16 ) |
2758 ( (uint64_t) buf[4] << 8 ) |
2759 ( (uint64_t) buf[5] ) );
2760}
2761
2762/*
2763 * Return 0 if sequence number is acceptable, -1 otherwise
2764 */
2765int ssl_dtls_replay_check( ssl_context *ssl )
2766{
2767 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2768 uint64_t bit;
2769
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002770 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2771 return( 0 );
2772
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002773 if( rec_seqnum > ssl->in_window_top )
2774 return( 0 );
2775
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002776 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002777
2778 if( bit >= 64 )
2779 return( -1 );
2780
2781 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2782 return( -1 );
2783
2784 return( 0 );
2785}
2786
2787/*
2788 * Update replay window on new validated record
2789 */
2790void ssl_dtls_replay_update( ssl_context *ssl )
2791{
2792 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2793
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002794 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2795 return;
2796
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002797 if( rec_seqnum > ssl->in_window_top )
2798 {
2799 /* Update window_top and the contents of the window */
2800 uint64_t shift = rec_seqnum - ssl->in_window_top;
2801
2802 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002803 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002804 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002805 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002806 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002807 ssl->in_window |= 1;
2808 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002809
2810 ssl->in_window_top = rec_seqnum;
2811 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002812 else
2813 {
2814 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002815 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002816
2817 if( bit < 64 ) /* Always true, but be extra sure */
2818 ssl->in_window |= (uint64_t) 1 << bit;
2819 }
2820}
2821#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2822
2823/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002824 * ContentType type;
2825 * ProtocolVersion version;
2826 * uint16 epoch; // DTLS only
2827 * uint48 sequence_number; // DTLS only
2828 * uint16 length;
2829 */
2830static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002831{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002832 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002833 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002835 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2836
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002838 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002839 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840
2841 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2842 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002843 ssl->in_msgtype,
2844 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002846 /* Check record type */
2847 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2848 ssl->in_msgtype != SSL_MSG_ALERT &&
2849 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2850 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2851 {
2852 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002853
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002854 if( ( ret = ssl_send_alert_message( ssl,
2855 SSL_ALERT_LEVEL_FATAL,
2856 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2857 {
2858 return( ret );
2859 }
2860
2861 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2862 }
2863
2864 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002865 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002866 {
2867 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002868 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 }
2870
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002871 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 {
2873 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002874 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 }
2876
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002877 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002878#if defined(POLARSSL_SSL_PROTO_DTLS)
2879 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2880 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002881 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002882
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002883 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002884 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002885 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002886 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002887 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002888 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002889 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002890
2891#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2892 if( ssl_dtls_replay_check( ssl ) != 0 )
2893 {
2894 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
2895 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2896 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002897#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002898 }
2899#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002900
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002901 /* Check length against the size of our buffer */
2902 if( ssl->in_msglen > SSL_BUFFER_LEN
2903 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002904 {
2905 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2906 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2907 }
2908
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002909 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002910 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002912 if( ssl->in_msglen < 1 ||
2913 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 {
2915 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002916 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 }
2918 }
2919 else
2920 {
Paul Bakker48916f92012-09-16 19:57:18 +00002921 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 {
2923 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002924 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002925 }
2926
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002927#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002929 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 {
2931 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002932 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2936 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 /*
2938 * TLS encrypted messages can have up to 256 bytes of padding
2939 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002941 ssl->in_msglen > ssl->transform_in->minlen +
2942 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002943 {
2944 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002945 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002946 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 }
2949
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002950 return( 0 );
2951}
Paul Bakker5121ce52009-01-03 21:22:43 +00002952
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002953/*
2954 * If applicable, decrypt (and decompress) record content
2955 */
2956static int ssl_prepare_record_content( ssl_context *ssl )
2957{
2958 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002959
Paul Bakker5121ce52009-01-03 21:22:43 +00002960 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002961 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Paul Bakker05ef8352012-05-08 09:17:57 +00002963#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002964 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002965 {
2966 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2967
2968 ret = ssl_hw_record_read( ssl );
2969 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2970 {
2971 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002972 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002973 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002974
2975 if( ret == 0 )
2976 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002977 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002978#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002979 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002980 {
2981 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2982 {
2983 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2984 return( ret );
2985 }
2986
2987 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2988 ssl->in_msg, ssl->in_msglen );
2989
2990 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2991 {
2992 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002993 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 }
2995 }
2996
Paul Bakker2770fbd2012-07-03 13:30:23 +00002997#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002998 if( ssl->transform_in != NULL &&
2999 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003000 {
3001 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3002 {
3003 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3004 return( ret );
3005 }
3006
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003007 // TODO: what's the purpose of these lines? is in_len used?
3008 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3009 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003010 }
3011#endif /* POLARSSL_ZLIB_SUPPORT */
3012
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003013#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003014 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3015 {
3016 ssl_dtls_replay_update( ssl );
3017 }
3018#endif
3019
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003020 return( 0 );
3021}
3022
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003023static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3024
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003025/*
3026 * Read a record.
3027 *
3028 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3029 * and continue reading until a valid record is found.
3030 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003031int ssl_read_record( ssl_context *ssl )
3032{
3033 int ret;
3034
3035 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3036
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003037 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003038 {
3039 /*
3040 * Get next Handshake message in the current record
3041 */
3042 ssl->in_msglen -= ssl->in_hslen;
3043
3044 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3045 ssl->in_msglen );
3046
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003047 SSL_DEBUG_BUF( 4, "remaining content in record",
3048 ssl->in_msg, ssl->in_msglen );
3049
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003050 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3051 return( ret );
3052
3053 return( 0 );
3054 }
3055
3056 ssl->in_hslen = 0;
3057
3058 /*
3059 * Read the record header and parse it
3060 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003061#if defined(POLARSSL_SSL_PROTO_DTLS)
3062read_record_header:
3063#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003064 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3065 {
3066 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3067 return( ret );
3068 }
3069
3070 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003071 {
3072#if defined(POLARSSL_SSL_PROTO_DTLS)
3073 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3074 {
3075 /* Ignore bad record and get next one; drop the whole datagram
3076 * since current header cannot be trusted to find the next record
3077 * in current datagram */
3078 ssl->next_record_offset = 0;
3079 ssl->in_left = 0;
3080
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003081 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003082 goto read_record_header;
3083 }
3084#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003085 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003086 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003087
3088 /*
3089 * Read and optionally decrypt the message contents
3090 */
3091 if( ( ret = ssl_fetch_input( ssl,
3092 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3093 {
3094 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3095 return( ret );
3096 }
3097
3098 /* Done reading this record, get ready for the next one */
3099#if defined(POLARSSL_SSL_PROTO_DTLS)
3100 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3101 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3102 else
3103#endif
3104 ssl->in_left = 0;
3105
3106 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003107 {
3108#if defined(POLARSSL_SSL_PROTO_DTLS)
3109 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3110 {
3111 /* Silently discard invalid records */
3112 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3113 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3114 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003115 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003116 goto read_record_header;
3117 }
3118
3119 return( ret );
3120 }
3121 else
3122#endif
3123 {
3124 /* Error out (and send alert) on invalid records */
3125#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3126 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3127 {
3128 ssl_send_alert_message( ssl,
3129 SSL_ALERT_LEVEL_FATAL,
3130 SSL_ALERT_MSG_BAD_RECORD_MAC );
3131 }
3132#endif
3133 return( ret );
3134 }
3135 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003136
3137 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003138 * When we sent the last flight of the handshake, we MUST respond to a
3139 * retransmit of the peer's previous flight with a retransmit. (In
3140 * practice, only the Finished message will make it, other messages
3141 * including CCS use the old transform so they're dropped as invalid.)
3142 *
3143 * If the record we received is not a handshake message, however, it
3144 * means the peer received our last flight so we can clean up
3145 * handshake info.
3146 *
3147 * This check needs to be done before prepare_handshake() due to an edge
3148 * case: if the client immediately requests renegotiation, this
3149 * finishes the current handshake first, avoiding the new ClientHello
3150 * being mistaken for an ancient message in the current handshake.
3151 */
3152#if defined(POLARSSL_SSL_PROTO_DTLS)
3153 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3154 ssl->handshake != NULL &&
3155 ssl->handshake->retransmit_state == SSL_RETRANS_FINISHED )
3156 {
3157 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3158 ssl->in_msg[0] == SSL_HS_FINISHED )
3159 {
3160 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3161
3162 if( ( ret = ssl_resend( ssl ) ) != 0 )
3163 {
3164 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3165 return( ret );
3166 }
3167
3168 return( POLARSSL_ERR_NET_WANT_READ );
3169 }
3170 else
3171 {
3172 ssl_handshake_wrapup_free_hs_transform( ssl );
3173 }
3174 }
3175#endif
3176
3177 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003178 * Handle particular types of records
3179 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3181 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003182 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3183 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 }
3185
3186 if( ssl->in_msgtype == SSL_MSG_ALERT )
3187 {
3188 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3189 ssl->in_msg[0], ssl->in_msg[1] ) );
3190
3191 /*
3192 * Ignore non-fatal alerts, except close_notify
3193 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003194 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3197 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199 }
3200
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003201 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3202 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003203 {
3204 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003205 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003206 }
3207 }
3208
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003209#if defined(POLARSSL_SSL_PROTO_DTLS)
3210 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3211 {
3212 /* Drop unexpected ChangeCipherSpec messages */
3213 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3214 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3215 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3216 {
3217 SSL_DEBUG_MSG( 2, ( "dropping unexpected ChangeCipherSpec" ) );
3218 return( POLARSSL_ERR_NET_WANT_READ );
3219 }
3220 }
3221#endif
3222
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3224
3225 return( 0 );
3226}
3227
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003228int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3229{
3230 int ret;
3231
3232 if( ( ret = ssl_send_alert_message( ssl,
3233 SSL_ALERT_LEVEL_FATAL,
3234 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3235 {
3236 return( ret );
3237 }
3238
3239 return( 0 );
3240}
3241
Paul Bakker0a925182012-04-16 06:46:41 +00003242int ssl_send_alert_message( ssl_context *ssl,
3243 unsigned char level,
3244 unsigned char message )
3245{
3246 int ret;
3247
3248 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3249
3250 ssl->out_msgtype = SSL_MSG_ALERT;
3251 ssl->out_msglen = 2;
3252 ssl->out_msg[0] = level;
3253 ssl->out_msg[1] = message;
3254
3255 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3256 {
3257 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3258 return( ret );
3259 }
3260
3261 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3262
3263 return( 0 );
3264}
3265
Paul Bakker5121ce52009-01-03 21:22:43 +00003266/*
3267 * Handshake functions
3268 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003269#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3270 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3271 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3272 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3273 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3274 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3275 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003276int ssl_write_certificate( ssl_context *ssl )
3277{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003278 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003279
3280 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3281
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003282 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003283 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3284 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003285 {
3286 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3287 ssl->state++;
3288 return( 0 );
3289 }
3290
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003291 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3292 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003293}
3294
3295int ssl_parse_certificate( ssl_context *ssl )
3296{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003297 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3298
3299 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3300
3301 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003302 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3303 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003304 {
3305 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3306 ssl->state++;
3307 return( 0 );
3308 }
3309
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003310 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3311 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003312}
3313#else
3314int ssl_write_certificate( ssl_context *ssl )
3315{
3316 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3317 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003318 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003319 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3320
3321 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3322
3323 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003324 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3325 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003326 {
3327 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3328 ssl->state++;
3329 return( 0 );
3330 }
3331
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 if( ssl->endpoint == SSL_IS_CLIENT )
3333 {
3334 if( ssl->client_auth == 0 )
3335 {
3336 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3337 ssl->state++;
3338 return( 0 );
3339 }
3340
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003341#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 /*
3343 * If using SSLv3 and got no cert, send an Alert message
3344 * (otherwise an empty Certificate message will be sent).
3345 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003346 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003347 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3348 {
3349 ssl->out_msglen = 2;
3350 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003351 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3352 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
3354 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3355 goto write_msg;
3356 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003357#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 }
3359 else /* SSL_IS_SERVER */
3360 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003361 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 {
3363 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003364 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003365 }
3366 }
3367
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003368 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
3370 /*
3371 * 0 . 0 handshake type
3372 * 1 . 3 handshake length
3373 * 4 . 6 length of all certs
3374 * 7 . 9 length of cert. 1
3375 * 10 . n-1 peer certificate
3376 * n . n+2 length of cert. 2
3377 * n+3 . ... upper level cert, etc.
3378 */
3379 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003380 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
Paul Bakker29087132010-03-21 21:03:34 +00003382 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 {
3384 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003385 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 {
3387 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3388 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003389 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 }
3391
3392 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3393 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3394 ssl->out_msg[i + 2] = (unsigned char)( n );
3395
3396 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3397 i += n; crt = crt->next;
3398 }
3399
3400 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3401 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3402 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3403
3404 ssl->out_msglen = i;
3405 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3406 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3407
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003408#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003409write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003411
3412 ssl->state++;
3413
3414 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3415 {
3416 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3417 return( ret );
3418 }
3419
3420 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3421
Paul Bakkered27a042013-04-18 22:46:23 +02003422 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003423}
3424
3425int ssl_parse_certificate( ssl_context *ssl )
3426{
Paul Bakkered27a042013-04-18 22:46:23 +02003427 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003428 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003429 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003430
3431 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3432
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003433 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003434 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3435 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003436 {
3437 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3438 ssl->state++;
3439 return( 0 );
3440 }
3441
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003443 ( ssl->authmode == SSL_VERIFY_NONE ||
3444 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003446 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3448 ssl->state++;
3449 return( 0 );
3450 }
3451
3452 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3453 {
3454 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3455 return( ret );
3456 }
3457
3458 ssl->state++;
3459
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003460#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 /*
3462 * Check if the client sent an empty certificate
3463 */
3464 if( ssl->endpoint == SSL_IS_SERVER &&
3465 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3466 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003467 if( ssl->in_msglen == 2 &&
3468 ssl->in_msgtype == SSL_MSG_ALERT &&
3469 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3470 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 {
3472 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3473
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003474 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3476 return( 0 );
3477 else
Paul Bakker40e46942009-01-03 21:51:57 +00003478 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003479 }
3480 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003481#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003482
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003483#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3484 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 if( ssl->endpoint == SSL_IS_SERVER &&
3486 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3487 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003488 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3490 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003491 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003492 {
3493 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3494
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003495 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003496 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003497 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003498 else
3499 return( 0 );
3500 }
3501 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003502#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3503 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
3505 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3506 {
3507 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003508 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509 }
3510
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003511 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3512 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 {
3514 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003515 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516 }
3517
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003518 i = ssl_hs_hdr_len( ssl );
3519
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 /*
3521 * Same message structure as in ssl_write_certificate()
3522 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003523 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003524
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003525 if( ssl->in_msg[i] != 0 ||
3526 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003527 {
3528 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003529 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 }
3531
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003532 /* In case we tried to reuse a session but it failed */
3533 if( ssl->session_negotiate->peer_cert != NULL )
3534 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003535 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003536 polarssl_free( ssl->session_negotiate->peer_cert );
3537 }
3538
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003539 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3540 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 {
3542 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003543 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003544 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003545 }
3546
Paul Bakkerb6b09562013-09-18 14:17:41 +02003547 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003549 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003550
3551 while( i < ssl->in_hslen )
3552 {
3553 if( ssl->in_msg[i] != 0 )
3554 {
3555 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003556 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 }
3558
3559 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3560 | (unsigned int) ssl->in_msg[i + 2];
3561 i += 3;
3562
3563 if( n < 128 || i + n > ssl->in_hslen )
3564 {
3565 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003566 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 }
3568
Paul Bakkerddf26b42013-09-18 13:46:23 +02003569 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3570 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 if( ret != 0 )
3572 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003573 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 return( ret );
3575 }
3576
3577 i += n;
3578 }
3579
Paul Bakker48916f92012-09-16 19:57:18 +00003580 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003582 /*
3583 * On client, make sure the server cert doesn't change during renego to
3584 * avoid "triple handshake" attack: https://secure-resumption.com/
3585 */
3586 if( ssl->endpoint == SSL_IS_CLIENT &&
3587 ssl->renegotiation == SSL_RENEGOTIATION )
3588 {
3589 if( ssl->session->peer_cert == NULL )
3590 {
3591 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3592 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3593 }
3594
3595 if( ssl->session->peer_cert->raw.len !=
3596 ssl->session_negotiate->peer_cert->raw.len ||
3597 memcmp( ssl->session->peer_cert->raw.p,
3598 ssl->session_negotiate->peer_cert->raw.p,
3599 ssl->session->peer_cert->raw.len ) != 0 )
3600 {
3601 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3603 }
3604 }
3605
Paul Bakker5121ce52009-01-03 21:22:43 +00003606 if( ssl->authmode != SSL_VERIFY_NONE )
3607 {
3608 if( ssl->ca_chain == NULL )
3609 {
3610 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003611 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003612 }
3613
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003614 /*
3615 * Main check: verify certificate
3616 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003617 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3618 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3619 &ssl->session_negotiate->verify_result,
3620 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003621
3622 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003623 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003624 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003625 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003626
3627 /*
3628 * Secondary checks: always done, but change 'ret' only if it was 0
3629 */
3630
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003631#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003632 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003633 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003634
3635 /* If certificate uses an EC key, make sure the curve is OK */
3636 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3637 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3638 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003639 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003640 if( ret == 0 )
3641 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003642 }
3643 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003644#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003645
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003646 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3647 ciphersuite_info,
3648 ! ssl->endpoint ) != 0 )
3649 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003650 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003651 if( ret == 0 )
3652 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3653 }
3654
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3656 ret = 0;
3657 }
3658
3659 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3660
3661 return( ret );
3662}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003663#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3664 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3665 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3666 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3667 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3668 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3669 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003670
3671int ssl_write_change_cipher_spec( ssl_context *ssl )
3672{
3673 int ret;
3674
3675 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3676
3677 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3678 ssl->out_msglen = 1;
3679 ssl->out_msg[0] = 1;
3680
Paul Bakker5121ce52009-01-03 21:22:43 +00003681 ssl->state++;
3682
3683 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3684 {
3685 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3686 return( ret );
3687 }
3688
3689 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3690
3691 return( 0 );
3692}
3693
3694int ssl_parse_change_cipher_spec( ssl_context *ssl )
3695{
3696 int ret;
3697
3698 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3699
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3701 {
3702 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3703 return( ret );
3704 }
3705
3706 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3707 {
3708 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003709 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 }
3711
3712 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3713 {
3714 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003715 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 }
3717
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003718 /*
3719 * Switch to our negotiated transform and session parameters for inbound
3720 * data.
3721 */
3722 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3723 ssl->transform_in = ssl->transform_negotiate;
3724 ssl->session_in = ssl->session_negotiate;
3725
3726#if defined(POLARSSL_SSL_PROTO_DTLS)
3727 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3728 {
3729#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3730 ssl_dtls_replay_reset( ssl );
3731#endif
3732
3733 /* Increment epoch */
3734 if( ++ssl->in_epoch == 0 )
3735 {
3736 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3737 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3738 }
3739 }
3740 else
3741#endif /* POLARSSL_SSL_PROTO_DTLS */
3742 memset( ssl->in_ctr, 0, 8 );
3743
3744 /*
3745 * Set the in_msg pointer to the correct location based on IV length
3746 */
3747 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3748 {
3749 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3750 ssl->transform_negotiate->fixed_ivlen;
3751 }
3752 else
3753 ssl->in_msg = ssl->in_iv;
3754
3755#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3756 if( ssl_hw_record_activate != NULL )
3757 {
3758 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3759 {
3760 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3761 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3762 }
3763 }
3764#endif
3765
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 ssl->state++;
3767
3768 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3769
3770 return( 0 );
3771}
3772
Paul Bakker41c83d32013-03-20 14:39:14 +01003773void ssl_optimize_checksum( ssl_context *ssl,
3774 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003775{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003776 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003777
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003778#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3779 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003780 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003781 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003782 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003783#endif
3784#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3785#if defined(POLARSSL_SHA512_C)
3786 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3787 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3788 else
3789#endif
3790#if defined(POLARSSL_SHA256_C)
3791 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003792 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003793 else
3794#endif
3795#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003796 {
3797 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003798 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003799 }
Paul Bakker380da532012-04-18 16:10:25 +00003800}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003801
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003802void ssl_reset_checksum( ssl_context *ssl )
3803{
3804#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3805 defined(POLARSSL_SSL_PROTO_TLS1_1)
3806 md5_starts( &ssl->handshake->fin_md5 );
3807 sha1_starts( &ssl->handshake->fin_sha1 );
3808#endif
3809#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3810#if defined(POLARSSL_SHA256_C)
3811 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3812#endif
3813#if defined(POLARSSL_SHA512_C)
3814 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3815#endif
3816#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3817}
3818
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003819static void ssl_update_checksum_start( ssl_context *ssl,
3820 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003821{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003822#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3823 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003824 md5_update( &ssl->handshake->fin_md5 , buf, len );
3825 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003826#endif
3827#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3828#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003829 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003830#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003831#if defined(POLARSSL_SHA512_C)
3832 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003833#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003834#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003835}
3836
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003837#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3838 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003839static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3840 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003841{
Paul Bakker48916f92012-09-16 19:57:18 +00003842 md5_update( &ssl->handshake->fin_md5 , buf, len );
3843 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003844}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003845#endif
Paul Bakker380da532012-04-18 16:10:25 +00003846
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003847#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3848#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003849static void ssl_update_checksum_sha256( ssl_context *ssl,
3850 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003851{
Paul Bakker9e36f042013-06-30 14:34:05 +02003852 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003853}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003854#endif
Paul Bakker380da532012-04-18 16:10:25 +00003855
Paul Bakker9e36f042013-06-30 14:34:05 +02003856#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003857static void ssl_update_checksum_sha384( ssl_context *ssl,
3858 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003859{
Paul Bakker9e36f042013-06-30 14:34:05 +02003860 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003861}
Paul Bakker769075d2012-11-24 11:26:46 +01003862#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003863#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003864
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003865#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003866static void ssl_calc_finished_ssl(
3867 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003868{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003869 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003870 md5_context md5;
3871 sha1_context sha1;
3872
Paul Bakker5121ce52009-01-03 21:22:43 +00003873 unsigned char padbuf[48];
3874 unsigned char md5sum[16];
3875 unsigned char sha1sum[20];
3876
Paul Bakker48916f92012-09-16 19:57:18 +00003877 ssl_session *session = ssl->session_negotiate;
3878 if( !session )
3879 session = ssl->session;
3880
Paul Bakker1ef83d62012-04-11 12:09:53 +00003881 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3882
Paul Bakker48916f92012-09-16 19:57:18 +00003883 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3884 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003885
3886 /*
3887 * SSLv3:
3888 * hash =
3889 * MD5( master + pad2 +
3890 * MD5( handshake + sender + master + pad1 ) )
3891 * + SHA1( master + pad2 +
3892 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003893 */
3894
Paul Bakker90995b52013-06-24 19:20:35 +02003895#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003897 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003899
Paul Bakker90995b52013-06-24 19:20:35 +02003900#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003902 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003903#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003904
Paul Bakker3c2122f2013-06-24 19:03:14 +02003905 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3906 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
Paul Bakker1ef83d62012-04-11 12:09:53 +00003908 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003909
Paul Bakker3c2122f2013-06-24 19:03:14 +02003910 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003911 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003912 md5_update( &md5, padbuf, 48 );
3913 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003914
Paul Bakker3c2122f2013-06-24 19:03:14 +02003915 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003916 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003917 sha1_update( &sha1, padbuf, 40 );
3918 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003919
Paul Bakker1ef83d62012-04-11 12:09:53 +00003920 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003921
Paul Bakker1ef83d62012-04-11 12:09:53 +00003922 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003923 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003924 md5_update( &md5, padbuf, 48 );
3925 md5_update( &md5, md5sum, 16 );
3926 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
Paul Bakker1ef83d62012-04-11 12:09:53 +00003928 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003929 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003930 sha1_update( &sha1, padbuf , 40 );
3931 sha1_update( &sha1, sha1sum, 20 );
3932 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003933
Paul Bakker1ef83d62012-04-11 12:09:53 +00003934 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003935
Paul Bakker5b4af392014-06-26 12:09:34 +02003936 md5_free( &md5 );
3937 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003938
Paul Bakker34617722014-06-13 17:20:13 +02003939 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3940 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3941 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
3943 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3944}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003945#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003946
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003947#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003948static void ssl_calc_finished_tls(
3949 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003950{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003951 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003952 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003953 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003954 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003955 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Paul Bakker48916f92012-09-16 19:57:18 +00003957 ssl_session *session = ssl->session_negotiate;
3958 if( !session )
3959 session = ssl->session;
3960
Paul Bakker1ef83d62012-04-11 12:09:53 +00003961 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003962
Paul Bakker48916f92012-09-16 19:57:18 +00003963 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3964 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003965
Paul Bakker1ef83d62012-04-11 12:09:53 +00003966 /*
3967 * TLSv1:
3968 * hash = PRF( master, finished_label,
3969 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3970 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003971
Paul Bakker90995b52013-06-24 19:20:35 +02003972#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003973 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3974 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003975#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003976
Paul Bakker90995b52013-06-24 19:20:35 +02003977#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003978 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3979 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003980#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003981
3982 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003983 ? "client finished"
3984 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003985
3986 md5_finish( &md5, padbuf );
3987 sha1_finish( &sha1, padbuf + 16 );
3988
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003989 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003990 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003991
3992 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3993
Paul Bakker5b4af392014-06-26 12:09:34 +02003994 md5_free( &md5 );
3995 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003996
Paul Bakker34617722014-06-13 17:20:13 +02003997 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003998
3999 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4000}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004001#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004002
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004003#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4004#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004005static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004006 ssl_context *ssl, unsigned char *buf, int from )
4007{
4008 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004009 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004010 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004011 unsigned char padbuf[32];
4012
Paul Bakker48916f92012-09-16 19:57:18 +00004013 ssl_session *session = ssl->session_negotiate;
4014 if( !session )
4015 session = ssl->session;
4016
Paul Bakker380da532012-04-18 16:10:25 +00004017 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004018
Paul Bakker9e36f042013-06-30 14:34:05 +02004019 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004020
4021 /*
4022 * TLSv1.2:
4023 * hash = PRF( master, finished_label,
4024 * Hash( handshake ) )[0.11]
4025 */
4026
Paul Bakker9e36f042013-06-30 14:34:05 +02004027#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004028 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004029 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004030#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004031
4032 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004033 ? "client finished"
4034 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004035
Paul Bakker9e36f042013-06-30 14:34:05 +02004036 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004037
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004038 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004039 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004040
4041 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4042
Paul Bakker5b4af392014-06-26 12:09:34 +02004043 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004044
Paul Bakker34617722014-06-13 17:20:13 +02004045 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004046
4047 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4048}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004049#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004050
Paul Bakker9e36f042013-06-30 14:34:05 +02004051#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004052static void ssl_calc_finished_tls_sha384(
4053 ssl_context *ssl, unsigned char *buf, int from )
4054{
4055 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004056 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004057 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004058 unsigned char padbuf[48];
4059
Paul Bakker48916f92012-09-16 19:57:18 +00004060 ssl_session *session = ssl->session_negotiate;
4061 if( !session )
4062 session = ssl->session;
4063
Paul Bakker380da532012-04-18 16:10:25 +00004064 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004065
Paul Bakker9e36f042013-06-30 14:34:05 +02004066 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004067
4068 /*
4069 * TLSv1.2:
4070 * hash = PRF( master, finished_label,
4071 * Hash( handshake ) )[0.11]
4072 */
4073
Paul Bakker9e36f042013-06-30 14:34:05 +02004074#if !defined(POLARSSL_SHA512_ALT)
4075 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4076 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004077#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004078
4079 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004080 ? "client finished"
4081 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004082
Paul Bakker9e36f042013-06-30 14:34:05 +02004083 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004084
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004085 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004086 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004087
4088 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4089
Paul Bakker5b4af392014-06-26 12:09:34 +02004090 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004091
Paul Bakker34617722014-06-13 17:20:13 +02004092 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004093
4094 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4095}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004096#endif /* POLARSSL_SHA512_C */
4097#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004098
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004099static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004100{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004101 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004102
4103 /*
4104 * Free our handshake params
4105 */
4106 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004107 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004108 ssl->handshake = NULL;
4109
4110 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004111 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004112 */
4113 if( ssl->transform )
4114 {
4115 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004116 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004117 }
4118 ssl->transform = ssl->transform_negotiate;
4119 ssl->transform_negotiate = NULL;
4120
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004121 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4122}
4123
4124void ssl_handshake_wrapup( ssl_context *ssl )
4125{
4126 int resume = ssl->handshake->resume;
4127
4128 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4129
4130 if( ssl->renegotiation == SSL_RENEGOTIATION )
4131 {
4132 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4133 ssl->renego_records_seen = 0;
4134 }
4135
4136 /*
4137 * Free the previous session and switch in the current one
4138 */
Paul Bakker0a597072012-09-25 21:55:46 +00004139 if( ssl->session )
4140 {
4141 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004142 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004143 }
4144 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004145 ssl->session_negotiate = NULL;
4146
Paul Bakker0a597072012-09-25 21:55:46 +00004147 /*
4148 * Add cache entry
4149 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004150 if( ssl->f_set_cache != NULL &&
4151 ssl->session->length != 0 &&
4152 resume == 0 )
4153 {
Paul Bakker0a597072012-09-25 21:55:46 +00004154 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4155 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004156 }
Paul Bakker0a597072012-09-25 21:55:46 +00004157
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004158#if defined(POLARSSL_SSL_PROTO_DTLS)
4159 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4160 ssl->handshake->flight != NULL )
4161 {
4162 /* Keep last flight around in case we need to resend it:
4163 * we need the handshake and transform structures for that */
4164 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4165 }
4166 else
4167#endif
4168 ssl_handshake_wrapup_free_hs_transform( ssl );
4169
Paul Bakker48916f92012-09-16 19:57:18 +00004170 ssl->state++;
4171
4172 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4173}
4174
Paul Bakker1ef83d62012-04-11 12:09:53 +00004175int ssl_write_finished( ssl_context *ssl )
4176{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004177 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004178
4179 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4180
Paul Bakker92be97b2013-01-02 17:30:03 +01004181 /*
4182 * Set the out_msg pointer to the correct location based on IV length
4183 */
4184 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4185 {
4186 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4187 ssl->transform_negotiate->fixed_ivlen;
4188 }
4189 else
4190 ssl->out_msg = ssl->out_iv;
4191
Paul Bakker48916f92012-09-16 19:57:18 +00004192 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004193
4194 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004195 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4196
Paul Bakker48916f92012-09-16 19:57:18 +00004197 ssl->verify_data_len = hash_len;
4198 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4199
Paul Bakker5121ce52009-01-03 21:22:43 +00004200 ssl->out_msglen = 4 + hash_len;
4201 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4202 ssl->out_msg[0] = SSL_HS_FINISHED;
4203
4204 /*
4205 * In case of session resuming, invert the client and server
4206 * ChangeCipherSpec messages order.
4207 */
Paul Bakker0a597072012-09-25 21:55:46 +00004208 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004209 {
4210 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004211 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004212 else
4213 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4214 }
4215 else
4216 ssl->state++;
4217
Paul Bakker48916f92012-09-16 19:57:18 +00004218 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004219 * Switch to our negotiated transform and session parameters for outbound
4220 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004221 */
4222 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004223
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004224#if defined(POLARSSL_SSL_PROTO_DTLS)
4225 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4226 {
4227 unsigned char i;
4228
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004229 /* Remember current epoch settings for resending */
4230 ssl->handshake->alt_transform_out = ssl->transform_out;
4231 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4232
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004233 /* Set sequence_number to zero */
4234 memset( ssl->out_ctr + 2, 0, 6 );
4235
4236 /* Increment epoch */
4237 for( i = 2; i > 0; i-- )
4238 if( ++ssl->out_ctr[i - 1] != 0 )
4239 break;
4240
4241 /* The loop goes to its end iff the counter is wrapping */
4242 if( i == 0 )
4243 {
4244 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4245 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4246 }
4247 }
4248 else
4249#endif /* POLARSSL_SSL_PROTO_DTLS */
4250 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004251
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004252 ssl->transform_out = ssl->transform_negotiate;
4253 ssl->session_out = ssl->session_negotiate;
4254
Paul Bakker07eb38b2012-12-19 14:42:06 +01004255#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004256 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004257 {
4258 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4259 {
4260 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4261 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4262 }
4263 }
4264#endif
4265
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4267 {
4268 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4269 return( ret );
4270 }
4271
4272 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4273
4274 return( 0 );
4275}
4276
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004277#if defined(POLARSSL_SSL_PROTO_SSL3)
4278#define SSL_MAX_HASH_LEN 36
4279#else
4280#define SSL_MAX_HASH_LEN 12
4281#endif
4282
Paul Bakker5121ce52009-01-03 21:22:43 +00004283int ssl_parse_finished( ssl_context *ssl )
4284{
Paul Bakker23986e52011-04-24 08:57:21 +00004285 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004286 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004287 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004288
4289 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4290
Paul Bakker48916f92012-09-16 19:57:18 +00004291 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004292
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4294 {
4295 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4296 return( ret );
4297 }
4298
4299 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4300 {
4301 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004302 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004303 }
4304
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004305 /* There is currently no ciphersuite using another length with TLS 1.2 */
4306#if defined(POLARSSL_SSL_PROTO_SSL3)
4307 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4308 hash_len = 36;
4309 else
4310#endif
4311 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004312
4313 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004314 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004315 {
4316 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004317 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 }
4319
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004320 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4321 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004322 {
4323 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004324 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 }
4326
Paul Bakker48916f92012-09-16 19:57:18 +00004327 ssl->verify_data_len = hash_len;
4328 memcpy( ssl->peer_verify_data, buf, hash_len );
4329
Paul Bakker0a597072012-09-25 21:55:46 +00004330 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 {
4332 if( ssl->endpoint == SSL_IS_CLIENT )
4333 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4334
4335 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004336 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004337 }
4338 else
4339 ssl->state++;
4340
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004341#if defined(POLARSSL_SSL_PROTO_DTLS)
4342 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4343 ssl_recv_flight_completed( ssl );
4344#endif
4345
Paul Bakker5121ce52009-01-03 21:22:43 +00004346 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4347
4348 return( 0 );
4349}
4350
Paul Bakker968afaa2014-07-09 11:09:24 +02004351static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004352{
4353 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4354
4355#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4356 defined(POLARSSL_SSL_PROTO_TLS1_1)
4357 md5_init( &handshake->fin_md5 );
4358 sha1_init( &handshake->fin_sha1 );
4359 md5_starts( &handshake->fin_md5 );
4360 sha1_starts( &handshake->fin_sha1 );
4361#endif
4362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4363#if defined(POLARSSL_SHA256_C)
4364 sha256_init( &handshake->fin_sha256 );
4365 sha256_starts( &handshake->fin_sha256, 0 );
4366#endif
4367#if defined(POLARSSL_SHA512_C)
4368 sha512_init( &handshake->fin_sha512 );
4369 sha512_starts( &handshake->fin_sha512, 1 );
4370#endif
4371#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4372
4373 handshake->update_checksum = ssl_update_checksum_start;
4374 handshake->sig_alg = SSL_HASH_SHA1;
4375
4376#if defined(POLARSSL_DHM_C)
4377 dhm_init( &handshake->dhm_ctx );
4378#endif
4379#if defined(POLARSSL_ECDH_C)
4380 ecdh_init( &handshake->ecdh_ctx );
4381#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004382}
4383
4384static void ssl_transform_init( ssl_transform *transform )
4385{
4386 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004387
4388 cipher_init( &transform->cipher_ctx_enc );
4389 cipher_init( &transform->cipher_ctx_dec );
4390
4391 md_init( &transform->md_ctx_enc );
4392 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004393}
4394
4395void ssl_session_init( ssl_session *session )
4396{
4397 memset( session, 0, sizeof(ssl_session) );
4398}
4399
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004400static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004401{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004402 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004403 if( ssl->transform_negotiate )
4404 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004405 if( ssl->session_negotiate )
4406 ssl_session_free( ssl->session_negotiate );
4407 if( ssl->handshake )
4408 ssl_handshake_free( ssl->handshake );
4409
4410 /*
4411 * Either the pointers are now NULL or cleared properly and can be freed.
4412 * Now allocate missing structures.
4413 */
4414 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004415 {
4416 ssl->transform_negotiate =
4417 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4418 }
Paul Bakker48916f92012-09-16 19:57:18 +00004419
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004420 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004421 {
4422 ssl->session_negotiate =
4423 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4424 }
Paul Bakker48916f92012-09-16 19:57:18 +00004425
Paul Bakker82788fb2014-10-20 13:59:19 +02004426 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004427 {
4428 ssl->handshake = (ssl_handshake_params *)
4429 polarssl_malloc( sizeof(ssl_handshake_params) );
4430 }
Paul Bakker48916f92012-09-16 19:57:18 +00004431
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004432 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004433 if( ssl->handshake == NULL ||
4434 ssl->transform_negotiate == NULL ||
4435 ssl->session_negotiate == NULL )
4436 {
4437 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004438
4439 polarssl_free( ssl->handshake );
4440 polarssl_free( ssl->transform_negotiate );
4441 polarssl_free( ssl->session_negotiate );
4442
4443 ssl->handshake = NULL;
4444 ssl->transform_negotiate = NULL;
4445 ssl->session_negotiate = NULL;
4446
Paul Bakker48916f92012-09-16 19:57:18 +00004447 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4448 }
4449
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004450 /* Initialize structures */
4451 ssl_session_init( ssl->session_negotiate );
4452 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004453 ssl_handshake_params_init( ssl->handshake );
4454
4455#if defined(POLARSSL_X509_CRT_PARSE_C)
4456 ssl->handshake->key_cert = ssl->key_cert;
4457#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004458
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004459#if defined(POLARSSL_SSL_PROTO_DTLS)
4460 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4461 {
4462 ssl->handshake->alt_transform_out = ssl->transform_out;
4463
4464 if( ssl->endpoint == SSL_IS_CLIENT )
4465 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4466 else
4467 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
4468 }
4469#endif
4470
Paul Bakker48916f92012-09-16 19:57:18 +00004471 return( 0 );
4472}
4473
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004474#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4475/* Dummy cookie callbacks for defaults */
4476static int ssl_cookie_write_dummy( void *ctx,
4477 unsigned char **p, unsigned char *end,
4478 const unsigned char *cli_id, size_t cli_id_len )
4479{
4480 ((void) ctx);
4481 ((void) p);
4482 ((void) end);
4483 ((void) cli_id);
4484 ((void) cli_id_len);
4485
4486 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4487}
4488
4489static int ssl_cookie_check_dummy( void *ctx,
4490 const unsigned char *cookie, size_t cookie_len,
4491 const unsigned char *cli_id, size_t cli_id_len )
4492{
4493 ((void) ctx);
4494 ((void) cookie);
4495 ((void) cookie_len);
4496 ((void) cli_id);
4497 ((void) cli_id_len);
4498
4499 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4500}
4501#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4502
Paul Bakker5121ce52009-01-03 21:22:43 +00004503/*
4504 * Initialize an SSL context
4505 */
4506int ssl_init( ssl_context *ssl )
4507{
Paul Bakker48916f92012-09-16 19:57:18 +00004508 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004509 int len = SSL_BUFFER_LEN;
4510
4511 memset( ssl, 0, sizeof( ssl_context ) );
4512
Paul Bakker62f2dee2012-09-28 07:31:51 +00004513 /*
4514 * Sane defaults
4515 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004516 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4517 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4518 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4519 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004520
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004521 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004522
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004523 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4524
Paul Bakker62f2dee2012-09-28 07:31:51 +00004525#if defined(POLARSSL_DHM_C)
4526 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4527 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4528 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4529 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4530 {
4531 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4532 return( ret );
4533 }
4534#endif
4535
4536 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004537 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004538 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004539 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004540 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004541
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004542 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 {
4544 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004545 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004546 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004547 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004548 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004549 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004550 }
4551
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004552 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4553 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004554
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004555 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4556 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4557
Paul Bakker606b4ba2013-08-14 16:52:14 +02004558#if defined(POLARSSL_SSL_SESSION_TICKETS)
4559 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4560#endif
4561
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004562#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004563 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004564#endif
4565
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004566#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4567 ssl->f_cookie_write = ssl_cookie_write_dummy;
4568 ssl->f_cookie_check = ssl_cookie_check_dummy;
4569#endif
4570
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004571#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4572 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4573#endif
4574
Paul Bakker48916f92012-09-16 19:57:18 +00004575 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004577
4578 return( 0 );
4579}
4580
4581/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004582 * Reset an initialized and used SSL context for re-use while retaining
4583 * all application-set variables, function pointers and data.
4584 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004585int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004586{
Paul Bakker48916f92012-09-16 19:57:18 +00004587 int ret;
4588
Paul Bakker7eb013f2011-10-06 12:37:39 +00004589 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004590 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4591 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4592
4593 ssl->verify_data_len = 0;
4594 memset( ssl->own_verify_data, 0, 36 );
4595 memset( ssl->peer_verify_data, 0, 36 );
4596
Paul Bakker7eb013f2011-10-06 12:37:39 +00004597 ssl->in_offt = NULL;
4598
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004599 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004600 ssl->in_msgtype = 0;
4601 ssl->in_msglen = 0;
4602 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004603#if defined(POLARSSL_SSL_PROTO_DTLS)
4604 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004605 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004606#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004607#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4608 ssl_dtls_replay_reset( ssl );
4609#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004610
4611 ssl->in_hslen = 0;
4612 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004613 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004614
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004615 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004616 ssl->out_msgtype = 0;
4617 ssl->out_msglen = 0;
4618 ssl->out_left = 0;
4619
Paul Bakker48916f92012-09-16 19:57:18 +00004620 ssl->transform_in = NULL;
4621 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004622
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004623 ssl->renego_records_seen = 0;
4624
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004625 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4626 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004627
4628#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004629 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004630 {
4631 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004632 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004633 {
4634 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4635 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4636 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004637 }
4638#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004639
Paul Bakker48916f92012-09-16 19:57:18 +00004640 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004641 {
Paul Bakker48916f92012-09-16 19:57:18 +00004642 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004643 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004644 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004645 }
Paul Bakker48916f92012-09-16 19:57:18 +00004646
Paul Bakkerc0463502013-02-14 11:19:38 +01004647 if( ssl->session )
4648 {
4649 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004650 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004651 ssl->session = NULL;
4652 }
4653
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004654#if defined(POLARSSL_SSL_ALPN)
4655 ssl->alpn_chosen = NULL;
4656#endif
4657
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004658#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004659 polarssl_free( ssl->cli_id );
4660 ssl->cli_id = NULL;
4661 ssl->cli_id_len = 0;
4662#endif
4663
Paul Bakker48916f92012-09-16 19:57:18 +00004664 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4665 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004666
4667 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004668}
4669
Paul Bakkera503a632013-08-14 13:48:06 +02004670#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004671static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4672{
4673 aes_free( &tkeys->enc );
4674 aes_free( &tkeys->dec );
4675
4676 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4677}
4678
Paul Bakker7eb013f2011-10-06 12:37:39 +00004679/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004680 * Allocate and initialize ticket keys
4681 */
4682static int ssl_ticket_keys_init( ssl_context *ssl )
4683{
4684 int ret;
4685 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004686 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004687
4688 if( ssl->ticket_keys != NULL )
4689 return( 0 );
4690
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004691 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4692 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004693 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4694
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004695 aes_init( &tkeys->enc );
4696 aes_init( &tkeys->dec );
4697
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004698 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004699 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004700 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004701 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004702 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004703 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004704
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004705 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4706 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4707 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4708 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004709 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004710 polarssl_free( tkeys );
4711 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004712 }
4713
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004714 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004715 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004716 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004717 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004718 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004719 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004720
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004721 ssl->ticket_keys = tkeys;
4722
4723 return( 0 );
4724}
Paul Bakkera503a632013-08-14 13:48:06 +02004725#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004726
4727/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004728 * SSL set accessors
4729 */
4730void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4731{
4732 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004733
Paul Bakker606b4ba2013-08-14 16:52:14 +02004734#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004735 if( endpoint == SSL_IS_CLIENT )
4736 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004737#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004738}
4739
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004740int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004741{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004742#if defined(POLARSSL_SSL_PROTO_DTLS)
4743 if( transport == SSL_TRANSPORT_DATAGRAM )
4744 {
4745 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004746
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004747 ssl->out_hdr = ssl->out_buf;
4748 ssl->out_ctr = ssl->out_buf + 3;
4749 ssl->out_len = ssl->out_buf + 11;
4750 ssl->out_iv = ssl->out_buf + 13;
4751 ssl->out_msg = ssl->out_buf + 13;
4752
4753 ssl->in_hdr = ssl->in_buf;
4754 ssl->in_ctr = ssl->in_buf + 3;
4755 ssl->in_len = ssl->in_buf + 11;
4756 ssl->in_iv = ssl->in_buf + 13;
4757 ssl->in_msg = ssl->in_buf + 13;
4758
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004759 /* DTLS starts with TLS1.1 */
4760 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4761 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004762
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004763 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4764 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4765
4766 return( 0 );
4767 }
4768#endif
4769
4770 if( transport == SSL_TRANSPORT_STREAM )
4771 {
4772 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004773
4774 ssl->out_ctr = ssl->out_buf;
4775 ssl->out_hdr = ssl->out_buf + 8;
4776 ssl->out_len = ssl->out_buf + 11;
4777 ssl->out_iv = ssl->out_buf + 13;
4778 ssl->out_msg = ssl->out_buf + 13;
4779
4780 ssl->in_ctr = ssl->in_buf;
4781 ssl->in_hdr = ssl->in_buf + 8;
4782 ssl->in_len = ssl->in_buf + 11;
4783 ssl->in_iv = ssl->in_buf + 13;
4784 ssl->in_msg = ssl->in_buf + 13;
4785
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004786 return( 0 );
4787 }
4788
4789 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004790}
4791
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004792#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4793void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4794{
4795 ssl->anti_replay = mode;
4796}
4797#endif
4798
Paul Bakker5121ce52009-01-03 21:22:43 +00004799void ssl_set_authmode( ssl_context *ssl, int authmode )
4800{
4801 ssl->authmode = authmode;
4802}
4803
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004804#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004805void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004806 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004807 void *p_vrfy )
4808{
4809 ssl->f_vrfy = f_vrfy;
4810 ssl->p_vrfy = p_vrfy;
4811}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004812#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004813
Paul Bakker5121ce52009-01-03 21:22:43 +00004814void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004815 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 void *p_rng )
4817{
4818 ssl->f_rng = f_rng;
4819 ssl->p_rng = p_rng;
4820}
4821
4822void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004823 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004824 void *p_dbg )
4825{
4826 ssl->f_dbg = f_dbg;
4827 ssl->p_dbg = p_dbg;
4828}
4829
4830void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004831 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004832 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004833{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004834 if( p_recv != p_send )
4835 {
4836 ssl->f_recv = NULL;
4837 ssl->f_send = NULL;
4838 ssl->p_bio = NULL;
4839 return;
4840 }
4841
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 ssl->f_recv = f_recv;
4843 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004844 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845}
4846
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004847void ssl_set_bio_timeout( ssl_context *ssl,
4848 void *p_bio,
4849 int (*f_send)(void *, const unsigned char *, size_t),
4850 int (*f_recv)(void *, unsigned char *, size_t),
4851 int (*f_recv_timeout)(void *, unsigned char *, size_t, unsigned char),
4852 unsigned char timeout )
4853{
4854 ssl->p_bio = p_bio;
4855 ssl->f_send = f_send;
4856 ssl->f_recv = f_recv;
4857 ssl->f_recv_timeout = f_recv_timeout;
4858 ssl->timeout = timeout;
4859}
4860
Paul Bakker0a597072012-09-25 21:55:46 +00004861void ssl_set_session_cache( ssl_context *ssl,
4862 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4863 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004864{
Paul Bakker0a597072012-09-25 21:55:46 +00004865 ssl->f_get_cache = f_get_cache;
4866 ssl->p_get_cache = p_get_cache;
4867 ssl->f_set_cache = f_set_cache;
4868 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004869}
4870
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004871int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004872{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004873 int ret;
4874
4875 if( ssl == NULL ||
4876 session == NULL ||
4877 ssl->session_negotiate == NULL ||
4878 ssl->endpoint != SSL_IS_CLIENT )
4879 {
4880 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4881 }
4882
4883 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4884 return( ret );
4885
Paul Bakker0a597072012-09-25 21:55:46 +00004886 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004887
4888 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004889}
4890
Paul Bakkerb68cad62012-08-23 08:34:18 +00004891void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004893 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4894 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4895 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4896 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4897}
4898
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004899void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4900 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004901 int major, int minor )
4902{
4903 if( major != SSL_MAJOR_VERSION_3 )
4904 return;
4905
4906 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4907 return;
4908
4909 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004910}
4911
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004912#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004913/* Add a new (empty) key_cert entry an return a pointer to it */
4914static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4915{
4916 ssl_key_cert *key_cert, *last;
4917
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004918 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
4919 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004920 return( NULL );
4921
4922 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4923
4924 /* Append the new key_cert to the (possibly empty) current list */
4925 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004926 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004927 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004928 if( ssl->handshake != NULL )
4929 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004930 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004931 else
4932 {
4933 last = ssl->key_cert;
4934 while( last->next != NULL )
4935 last = last->next;
4936 last->next = key_cert;
4937 }
4938
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004939 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004940}
4941
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004942void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004943 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004944{
4945 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004946 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004947 ssl->peer_cn = peer_cn;
4948}
4949
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004950int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004951 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004952{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004953 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4954
4955 if( key_cert == NULL )
4956 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4957
4958 key_cert->cert = own_cert;
4959 key_cert->key = pk_key;
4960
4961 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004962}
4963
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004964#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004965int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004966 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00004967{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004968 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004969 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004970
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004971 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004972 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4973
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004974 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4975 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004976 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004977
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004978 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004979
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004980 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004981 if( ret != 0 )
4982 return( ret );
4983
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004984 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004985 return( ret );
4986
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004987 key_cert->cert = own_cert;
4988 key_cert->key_own_alloc = 1;
4989
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004990 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004991}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004992#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004993
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004994int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004995 void *rsa_key,
4996 rsa_decrypt_func rsa_decrypt,
4997 rsa_sign_func rsa_sign,
4998 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004999{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005000 int ret;
5001 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005002
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005003 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005004 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5005
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005006 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5007 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005008 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005009
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005010 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005011
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005012 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5013 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5014 return( ret );
5015
5016 key_cert->cert = own_cert;
5017 key_cert->key_own_alloc = 1;
5018
5019 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005020}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005021#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005022
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005023#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005024int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5025 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005026{
Paul Bakker6db455e2013-09-18 17:29:31 +02005027 if( psk == NULL || psk_identity == NULL )
5028 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5029
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005030 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5032
Paul Bakker6db455e2013-09-18 17:29:31 +02005033 if( ssl->psk != NULL )
5034 {
5035 polarssl_free( ssl->psk );
5036 polarssl_free( ssl->psk_identity );
5037 }
5038
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005039 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005040 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005041
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005042 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005043 ssl->psk_identity = (unsigned char *)
5044 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005045
5046 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5047 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5048
5049 memcpy( ssl->psk, psk, ssl->psk_len );
5050 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005051
5052 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005053}
5054
5055void ssl_set_psk_cb( ssl_context *ssl,
5056 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5057 size_t),
5058 void *p_psk )
5059{
5060 ssl->f_psk = f_psk;
5061 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005062}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005063#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005064
Paul Bakker48916f92012-09-16 19:57:18 +00005065#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005066int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005067{
5068 int ret;
5069
Paul Bakker48916f92012-09-16 19:57:18 +00005070 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 {
5072 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5073 return( ret );
5074 }
5075
Paul Bakker48916f92012-09-16 19:57:18 +00005076 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005077 {
5078 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5079 return( ret );
5080 }
5081
5082 return( 0 );
5083}
5084
Paul Bakker1b57b062011-01-06 15:48:19 +00005085int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5086{
5087 int ret;
5088
Paul Bakker66d5d072014-06-17 16:39:18 +02005089 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005090 {
5091 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5092 return( ret );
5093 }
5094
Paul Bakker66d5d072014-06-17 16:39:18 +02005095 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005096 {
5097 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5098 return( ret );
5099 }
5100
5101 return( 0 );
5102}
Paul Bakker48916f92012-09-16 19:57:18 +00005103#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005104
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005105#if defined(POLARSSL_SSL_SET_CURVES)
5106/*
5107 * Set the allowed elliptic curves
5108 */
5109void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5110{
5111 ssl->curve_list = curve_list;
5112}
5113#endif
5114
Paul Bakker0be444a2013-08-27 21:55:01 +02005115#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005116int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005117{
5118 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005119 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005120
5121 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005122
5123 if( ssl->hostname_len + 1 == 0 )
5124 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5125
Paul Bakker6e339b52013-07-03 13:37:05 +02005126 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005127
Paul Bakkerb15b8512012-01-13 13:44:06 +00005128 if( ssl->hostname == NULL )
5129 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5130
Paul Bakker3c2122f2013-06-24 19:03:14 +02005131 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005132 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005133
Paul Bakker40ea7de2009-05-03 10:18:48 +00005134 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005135
5136 return( 0 );
5137}
5138
Paul Bakker5701cdc2012-09-27 21:49:42 +00005139void ssl_set_sni( ssl_context *ssl,
5140 int (*f_sni)(void *, ssl_context *,
5141 const unsigned char *, size_t),
5142 void *p_sni )
5143{
5144 ssl->f_sni = f_sni;
5145 ssl->p_sni = p_sni;
5146}
Paul Bakker0be444a2013-08-27 21:55:01 +02005147#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005148
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005149#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005150int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005151{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005152 size_t cur_len, tot_len;
5153 const char **p;
5154
5155 /*
5156 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5157 * truncated". Check lengths now rather than later.
5158 */
5159 tot_len = 0;
5160 for( p = protos; *p != NULL; p++ )
5161 {
5162 cur_len = strlen( *p );
5163 tot_len += cur_len;
5164
5165 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5167 }
5168
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005169 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005170
5171 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005172}
5173
5174const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5175{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005176 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005177}
5178#endif /* POLARSSL_SSL_ALPN */
5179
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005180static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005181{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005182 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5183 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5184 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5185 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005186 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005187 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005188 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005189
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005190 return( 0 );
5191}
5192
5193int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5194{
5195 if( ssl_check_version( ssl, major, minor ) != 0 )
5196 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5197
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005198 ssl->max_major_ver = major;
5199 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005200
5201 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005202}
5203
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005204int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005205{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005206 if( ssl_check_version( ssl, major, minor ) != 0 )
5207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005208
5209 ssl->min_major_ver = major;
5210 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005211
5212 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005213}
5214
Paul Bakker05decb22013-08-15 13:33:48 +02005215#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005216int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5217{
Paul Bakker77e257e2013-12-16 15:29:52 +01005218 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005219 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005220 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005222 }
5223
5224 ssl->mfl_code = mfl_code;
5225
5226 return( 0 );
5227}
Paul Bakker05decb22013-08-15 13:33:48 +02005228#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005229
Paul Bakker1f2bc622013-08-15 13:45:55 +02005230#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005231int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005232{
5233 if( ssl->endpoint != SSL_IS_CLIENT )
5234 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5235
Paul Bakker8c1ede62013-07-19 14:14:37 +02005236 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005237
5238 return( 0 );
5239}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005240#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005241
Paul Bakker48916f92012-09-16 19:57:18 +00005242void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5243{
5244 ssl->disable_renegotiation = renegotiation;
5245}
5246
5247void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5248{
5249 ssl->allow_legacy_renegotiation = allow_legacy;
5250}
5251
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005252void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5253{
5254 ssl->renego_max_records = max_records;
5255}
5256
Paul Bakkera503a632013-08-14 13:48:06 +02005257#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005258int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5259{
5260 ssl->session_tickets = use_tickets;
5261
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005262 if( ssl->endpoint == SSL_IS_CLIENT )
5263 return( 0 );
5264
5265 if( ssl->f_rng == NULL )
5266 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5267
5268 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005269}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005270
5271void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5272{
5273 ssl->ticket_lifetime = lifetime;
5274}
Paul Bakkera503a632013-08-14 13:48:06 +02005275#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005276
Paul Bakker5121ce52009-01-03 21:22:43 +00005277/*
5278 * SSL get accessors
5279 */
Paul Bakker23986e52011-04-24 08:57:21 +00005280size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005281{
5282 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5283}
5284
Paul Bakkerff60ee62010-03-16 21:09:09 +00005285int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005286{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005287 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005288}
5289
Paul Bakkere3166ce2011-01-27 17:40:50 +00005290const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005291{
Paul Bakker926c8e42013-03-06 10:23:34 +01005292 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005293 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005294
Paul Bakkere3166ce2011-01-27 17:40:50 +00005295 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005296}
5297
Paul Bakker43ca69c2011-01-15 17:35:19 +00005298const char *ssl_get_version( const ssl_context *ssl )
5299{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005300#if defined(POLARSSL_SSL_PROTO_DTLS)
5301 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5302 {
5303 switch( ssl->minor_ver )
5304 {
5305 case SSL_MINOR_VERSION_2:
5306 return( "DTLSv1.0" );
5307
5308 case SSL_MINOR_VERSION_3:
5309 return( "DTLSv1.2" );
5310
5311 default:
5312 return( "unknown (DTLS)" );
5313 }
5314 }
5315#endif
5316
Paul Bakker43ca69c2011-01-15 17:35:19 +00005317 switch( ssl->minor_ver )
5318 {
5319 case SSL_MINOR_VERSION_0:
5320 return( "SSLv3.0" );
5321
5322 case SSL_MINOR_VERSION_1:
5323 return( "TLSv1.0" );
5324
5325 case SSL_MINOR_VERSION_2:
5326 return( "TLSv1.1" );
5327
Paul Bakker1ef83d62012-04-11 12:09:53 +00005328 case SSL_MINOR_VERSION_3:
5329 return( "TLSv1.2" );
5330
Paul Bakker43ca69c2011-01-15 17:35:19 +00005331 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005332 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005333 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005334}
5335
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005336#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005337const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005338{
5339 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005340 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005341
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005342 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005343}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005344#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005345
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005346int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5347{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005348 if( ssl == NULL ||
5349 dst == NULL ||
5350 ssl->session == NULL ||
5351 ssl->endpoint != SSL_IS_CLIENT )
5352 {
5353 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5354 }
5355
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005356 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005357}
5358
Paul Bakker5121ce52009-01-03 21:22:43 +00005359/*
Paul Bakker1961b702013-01-25 14:49:24 +01005360 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005361 */
Paul Bakker1961b702013-01-25 14:49:24 +01005362int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005363{
Paul Bakker40e46942009-01-03 21:51:57 +00005364 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005365
Paul Bakker40e46942009-01-03 21:51:57 +00005366#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005367 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005368 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005369#endif
5370
Paul Bakker40e46942009-01-03 21:51:57 +00005371#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005372 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005373 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005374#endif
5375
Paul Bakker1961b702013-01-25 14:49:24 +01005376 return( ret );
5377}
5378
5379/*
5380 * Perform the SSL handshake
5381 */
5382int ssl_handshake( ssl_context *ssl )
5383{
5384 int ret = 0;
5385
5386 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5387
5388 while( ssl->state != SSL_HANDSHAKE_OVER )
5389 {
5390 ret = ssl_handshake_step( ssl );
5391
5392 if( ret != 0 )
5393 break;
5394 }
5395
Paul Bakker5121ce52009-01-03 21:22:43 +00005396 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5397
5398 return( ret );
5399}
5400
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005401#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005402/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005403 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005404 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005405static int ssl_write_hello_request( ssl_context *ssl )
5406{
5407 int ret;
5408
5409 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5410
5411 ssl->out_msglen = 4;
5412 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5413 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5414
5415 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5416 {
5417 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5418 return( ret );
5419 }
5420
5421 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5422
5423 return( 0 );
5424}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005425#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005426
5427/*
5428 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005429 * - any side: calling ssl_renegotiate(),
5430 * - client: receiving a HelloRequest during ssl_read(),
5431 * - server: receiving any handshake message on server during ssl_read() after
5432 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005433 * If the handshake doesn't complete due to waiting for I/O, it will continue
5434 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005435 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005436static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005437{
5438 int ret;
5439
5440 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5441
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005442 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5443 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005444
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005445 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5446 * the ServerHello will have message_seq = 1" */
5447#if defined(POLARSSL_SSL_PROTO_DTLS)
5448 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005449 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5450 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005451 if( ssl->endpoint == SSL_IS_SERVER )
5452 ssl->handshake->out_msg_seq = 1;
5453 else
5454 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005455 }
5456#endif
5457
Paul Bakker48916f92012-09-16 19:57:18 +00005458 ssl->state = SSL_HELLO_REQUEST;
5459 ssl->renegotiation = SSL_RENEGOTIATION;
5460
Paul Bakker48916f92012-09-16 19:57:18 +00005461 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5462 {
5463 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5464 return( ret );
5465 }
5466
5467 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5468
5469 return( 0 );
5470}
5471
5472/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005473 * Renegotiate current connection on client,
5474 * or request renegotiation on server
5475 */
5476int ssl_renegotiate( ssl_context *ssl )
5477{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005478 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005479
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005480#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005481 /* On server, just send the request */
5482 if( ssl->endpoint == SSL_IS_SERVER )
5483 {
5484 if( ssl->state != SSL_HANDSHAKE_OVER )
5485 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5486
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005487 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5488
5489 /* Did we already try/start sending HelloRequest? */
5490 if( ssl->out_left != 0 )
5491 return( ssl_flush_output( ssl ) );
5492
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005493 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005494 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005495#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005496
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005497#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005498 /*
5499 * On client, either start the renegotiation process or,
5500 * if already in progress, continue the handshake
5501 */
5502 if( ssl->renegotiation != SSL_RENEGOTIATION )
5503 {
5504 if( ssl->state != SSL_HANDSHAKE_OVER )
5505 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5506
5507 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5508 {
5509 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5510 return( ret );
5511 }
5512 }
5513 else
5514 {
5515 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5516 {
5517 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5518 return( ret );
5519 }
5520 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005521#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005522
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005523 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005524}
5525
5526/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005527 * Receive application data decrypted from the SSL layer
5528 */
Paul Bakker23986e52011-04-24 08:57:21 +00005529int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005530{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005531 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005532 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005533
5534 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5535
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005536#if defined(POLARSSL_SSL_PROTO_DTLS)
5537 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5538 ssl->handshake != NULL &&
5539 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5540 {
5541 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5542 return( ret );
5543
5544 if( ( ret = ssl_resend( ssl ) ) != 0 )
5545 return( ret );
5546 }
5547#endif
5548
Paul Bakker5121ce52009-01-03 21:22:43 +00005549 if( ssl->state != SSL_HANDSHAKE_OVER )
5550 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005551 ret = ssl_handshake( ssl );
5552 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5553 {
5554 record_read = 1;
5555 }
5556 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005557 {
5558 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5559 return( ret );
5560 }
5561 }
5562
5563 if( ssl->in_offt == NULL )
5564 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005565 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005566 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005567 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5568 {
5569 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5570 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005571
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005572 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5573 return( ret );
5574 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005575 }
5576
5577 if( ssl->in_msglen == 0 &&
5578 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5579 {
5580 /*
5581 * OpenSSL sends empty messages to randomize the IV
5582 */
5583 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5584 {
Paul Bakker831a7552011-05-18 13:32:51 +00005585 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5586 return( 0 );
5587
Paul Bakker5121ce52009-01-03 21:22:43 +00005588 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5589 return( ret );
5590 }
5591 }
5592
Paul Bakker48916f92012-09-16 19:57:18 +00005593 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5594 {
5595 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5596
5597 if( ssl->endpoint == SSL_IS_CLIENT &&
5598 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005599 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005600 {
5601 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005602
5603 /* With DTLS, drop the packet (probably from last handshake) */
5604#if defined(POLARSSL_SSL_PROTO_DTLS)
5605 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5606 return( POLARSSL_ERR_NET_WANT_READ );
5607#endif
5608 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5609 }
5610
5611 if( ssl->endpoint == SSL_IS_SERVER &&
5612 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5613 {
5614 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5615
5616 /* With DTLS, drop the packet (probably from last handshake) */
5617#if defined(POLARSSL_SSL_PROTO_DTLS)
5618 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5619 return( POLARSSL_ERR_NET_WANT_READ );
5620#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005621 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5622 }
5623
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005624 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5625 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005626 ssl->allow_legacy_renegotiation ==
5627 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005628 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005629 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005630
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005631#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005632 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005633 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005634 /*
5635 * SSLv3 does not have a "no_renegotiation" alert
5636 */
5637 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5638 return( ret );
5639 }
5640 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005641#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005642#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5643 defined(POLARSSL_SSL_PROTO_TLS1_2)
5644 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005645 {
5646 if( ( ret = ssl_send_alert_message( ssl,
5647 SSL_ALERT_LEVEL_WARNING,
5648 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5649 {
5650 return( ret );
5651 }
Paul Bakker48916f92012-09-16 19:57:18 +00005652 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005653 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005654#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5655 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005656 {
5657 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005658 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005659 }
Paul Bakker48916f92012-09-16 19:57:18 +00005660 }
5661 else
5662 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005663#if defined(POLARSSL_SSL_PROTO_DTLS)
5664 /* DTLS clients need to know renego is server-initiated */
5665 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5666 ssl->endpoint == SSL_IS_CLIENT )
5667 {
5668 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5669 }
5670#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005671 ret = ssl_start_renegotiation( ssl );
5672 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5673 {
5674 record_read = 1;
5675 }
5676 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005677 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005678 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005679 return( ret );
5680 }
Paul Bakker48916f92012-09-16 19:57:18 +00005681 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005682
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005683 /* If a non-handshake record was read during renego, fallthrough,
5684 * else tell the user they should call ssl_read() again */
5685 if( ! record_read )
5686 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005687 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005688 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5689 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005690 ssl->renego_records_seen++;
5691
5692 if( ssl->renego_max_records >= 0 &&
5693 ssl->renego_records_seen > ssl->renego_max_records )
5694 {
5695 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5696 "but not honored by client" ) );
5697 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5698 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005699 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005700
5701 /* Fatal and closure alerts handled by ssl_read_record() */
5702 if( ssl->in_msgtype == SSL_MSG_ALERT )
5703 {
5704 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5705 return( POLARSSL_ERR_NET_WANT_READ );
5706 }
5707
5708 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005709 {
5710 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005711 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005712 }
5713
5714 ssl->in_offt = ssl->in_msg;
5715 }
5716
5717 n = ( len < ssl->in_msglen )
5718 ? len : ssl->in_msglen;
5719
5720 memcpy( buf, ssl->in_offt, n );
5721 ssl->in_msglen -= n;
5722
5723 if( ssl->in_msglen == 0 )
5724 /* all bytes consumed */
5725 ssl->in_offt = NULL;
5726 else
5727 /* more data available */
5728 ssl->in_offt += n;
5729
5730 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5731
Paul Bakker23986e52011-04-24 08:57:21 +00005732 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005733}
5734
5735/*
5736 * Send application data to be encrypted by the SSL layer
5737 */
Paul Bakker23986e52011-04-24 08:57:21 +00005738int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005739{
Paul Bakker23986e52011-04-24 08:57:21 +00005740 int ret;
5741 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005742 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005743
5744 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5745
5746 if( ssl->state != SSL_HANDSHAKE_OVER )
5747 {
5748 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5749 {
5750 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5751 return( ret );
5752 }
5753 }
5754
Paul Bakker05decb22013-08-15 13:33:48 +02005755#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005756 /*
5757 * Assume mfl_code is correct since it was checked when set
5758 */
5759 max_len = mfl_code_to_length[ssl->mfl_code];
5760
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005761 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005762 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005763 */
5764 if( ssl->session_out != NULL &&
5765 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5766 {
5767 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5768 }
Paul Bakker05decb22013-08-15 13:33:48 +02005769#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005770
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005771 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005772
Paul Bakker5121ce52009-01-03 21:22:43 +00005773 if( ssl->out_left != 0 )
5774 {
5775 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5776 {
5777 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5778 return( ret );
5779 }
5780 }
Paul Bakker887bd502011-06-08 13:10:54 +00005781 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005782 {
Paul Bakker887bd502011-06-08 13:10:54 +00005783 ssl->out_msglen = n;
5784 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5785 memcpy( ssl->out_msg, buf, n );
5786
5787 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5788 {
5789 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5790 return( ret );
5791 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005792 }
5793
5794 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5795
Paul Bakker23986e52011-04-24 08:57:21 +00005796 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005797}
5798
5799/*
5800 * Notify the peer that the connection is being closed
5801 */
5802int ssl_close_notify( ssl_context *ssl )
5803{
5804 int ret;
5805
5806 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5807
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005808 if( ssl->out_left != 0 )
5809 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005810
5811 if( ssl->state == SSL_HANDSHAKE_OVER )
5812 {
Paul Bakker48916f92012-09-16 19:57:18 +00005813 if( ( ret = ssl_send_alert_message( ssl,
5814 SSL_ALERT_LEVEL_WARNING,
5815 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005816 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005817 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005818 return( ret );
5819 }
5820 }
5821
5822 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5823
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005824 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005825}
5826
Paul Bakker48916f92012-09-16 19:57:18 +00005827void ssl_transform_free( ssl_transform *transform )
5828{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005829 if( transform == NULL )
5830 return;
5831
Paul Bakker48916f92012-09-16 19:57:18 +00005832#if defined(POLARSSL_ZLIB_SUPPORT)
5833 deflateEnd( &transform->ctx_deflate );
5834 inflateEnd( &transform->ctx_inflate );
5835#endif
5836
Paul Bakker84bbeb52014-07-01 14:53:22 +02005837 cipher_free( &transform->cipher_ctx_enc );
5838 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005839
Paul Bakker84bbeb52014-07-01 14:53:22 +02005840 md_free( &transform->md_ctx_enc );
5841 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005842
Paul Bakker34617722014-06-13 17:20:13 +02005843 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005844}
5845
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005846#if defined(POLARSSL_X509_CRT_PARSE_C)
5847static void ssl_key_cert_free( ssl_key_cert *key_cert )
5848{
5849 ssl_key_cert *cur = key_cert, *next;
5850
5851 while( cur != NULL )
5852 {
5853 next = cur->next;
5854
5855 if( cur->key_own_alloc )
5856 {
5857 pk_free( cur->key );
5858 polarssl_free( cur->key );
5859 }
5860 polarssl_free( cur );
5861
5862 cur = next;
5863 }
5864}
5865#endif /* POLARSSL_X509_CRT_PARSE_C */
5866
Paul Bakker48916f92012-09-16 19:57:18 +00005867void ssl_handshake_free( ssl_handshake_params *handshake )
5868{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005869 if( handshake == NULL )
5870 return;
5871
Paul Bakker48916f92012-09-16 19:57:18 +00005872#if defined(POLARSSL_DHM_C)
5873 dhm_free( &handshake->dhm_ctx );
5874#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005875#if defined(POLARSSL_ECDH_C)
5876 ecdh_free( &handshake->ecdh_ctx );
5877#endif
5878
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005879#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005880 /* explicit void pointer cast for buggy MS compiler */
5881 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005882#endif
5883
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005884#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5885 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5886 /*
5887 * Free only the linked list wrapper, not the keys themselves
5888 * since the belong to the SNI callback
5889 */
5890 if( handshake->sni_key_cert != NULL )
5891 {
5892 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5893
5894 while( cur != NULL )
5895 {
5896 next = cur->next;
5897 polarssl_free( cur );
5898 cur = next;
5899 }
5900 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005901#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005902
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005903#if defined(POLARSSL_SSL_PROTO_DTLS)
5904 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02005905 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02005906 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005907#endif
5908
Paul Bakker34617722014-06-13 17:20:13 +02005909 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005910}
5911
5912void ssl_session_free( ssl_session *session )
5913{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005914 if( session == NULL )
5915 return;
5916
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005917#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005918 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005919 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005920 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005921 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005922 }
Paul Bakkered27a042013-04-18 22:46:23 +02005923#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005924
Paul Bakkera503a632013-08-14 13:48:06 +02005925#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005926 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005927#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005928
Paul Bakker34617722014-06-13 17:20:13 +02005929 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005930}
5931
Paul Bakker5121ce52009-01-03 21:22:43 +00005932/*
5933 * Free an SSL context
5934 */
5935void ssl_free( ssl_context *ssl )
5936{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005937 if( ssl == NULL )
5938 return;
5939
Paul Bakker5121ce52009-01-03 21:22:43 +00005940 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5941
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005942 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005943 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005944 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
5945 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005946 }
5947
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005948 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005949 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005950 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
5951 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005952 }
5953
Paul Bakker16770332013-10-11 09:59:44 +02005954#if defined(POLARSSL_ZLIB_SUPPORT)
5955 if( ssl->compress_buf != NULL )
5956 {
Paul Bakker34617722014-06-13 17:20:13 +02005957 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005958 polarssl_free( ssl->compress_buf );
5959 }
5960#endif
5961
Paul Bakker40e46942009-01-03 21:51:57 +00005962#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005963 mpi_free( &ssl->dhm_P );
5964 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005965#endif
5966
Paul Bakker48916f92012-09-16 19:57:18 +00005967 if( ssl->transform )
5968 {
5969 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005970 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005971 }
5972
5973 if( ssl->handshake )
5974 {
5975 ssl_handshake_free( ssl->handshake );
5976 ssl_transform_free( ssl->transform_negotiate );
5977 ssl_session_free( ssl->session_negotiate );
5978
Paul Bakker6e339b52013-07-03 13:37:05 +02005979 polarssl_free( ssl->handshake );
5980 polarssl_free( ssl->transform_negotiate );
5981 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005982 }
5983
Paul Bakkerc0463502013-02-14 11:19:38 +01005984 if( ssl->session )
5985 {
5986 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005987 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005988 }
5989
Paul Bakkera503a632013-08-14 13:48:06 +02005990#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005991 if( ssl->ticket_keys )
5992 {
5993 ssl_ticket_keys_free( ssl->ticket_keys );
5994 polarssl_free( ssl->ticket_keys );
5995 }
Paul Bakkera503a632013-08-14 13:48:06 +02005996#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005997
Paul Bakker0be444a2013-08-27 21:55:01 +02005998#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005999 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006000 {
Paul Bakker34617722014-06-13 17:20:13 +02006001 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006002 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006003 ssl->hostname_len = 0;
6004 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006005#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006006
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006007#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006008 if( ssl->psk != NULL )
6009 {
Paul Bakker34617722014-06-13 17:20:13 +02006010 polarssl_zeroize( ssl->psk, ssl->psk_len );
6011 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006012 polarssl_free( ssl->psk );
6013 polarssl_free( ssl->psk_identity );
6014 ssl->psk_len = 0;
6015 ssl->psk_identity_len = 0;
6016 }
6017#endif
6018
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006019#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006020 ssl_key_cert_free( ssl->key_cert );
6021#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006022
Paul Bakker05ef8352012-05-08 09:17:57 +00006023#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6024 if( ssl_hw_record_finish != NULL )
6025 {
6026 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6027 ssl_hw_record_finish( ssl );
6028 }
6029#endif
6030
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006031#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006032 polarssl_free( ssl->cli_id );
6033#endif
6034
Paul Bakker5121ce52009-01-03 21:22:43 +00006035 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006036
Paul Bakker86f04f42013-02-14 11:20:09 +01006037 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006038 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006039}
6040
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006041#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006042/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006043 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006044 */
6045unsigned char ssl_sig_from_pk( pk_context *pk )
6046{
6047#if defined(POLARSSL_RSA_C)
6048 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6049 return( SSL_SIG_RSA );
6050#endif
6051#if defined(POLARSSL_ECDSA_C)
6052 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6053 return( SSL_SIG_ECDSA );
6054#endif
6055 return( SSL_SIG_ANON );
6056}
6057
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006058pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6059{
6060 switch( sig )
6061 {
6062#if defined(POLARSSL_RSA_C)
6063 case SSL_SIG_RSA:
6064 return( POLARSSL_PK_RSA );
6065#endif
6066#if defined(POLARSSL_ECDSA_C)
6067 case SSL_SIG_ECDSA:
6068 return( POLARSSL_PK_ECDSA );
6069#endif
6070 default:
6071 return( POLARSSL_PK_NONE );
6072 }
6073}
Paul Bakker9af723c2014-05-01 13:03:14 +02006074#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006075
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006076/*
6077 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6078 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006079md_type_t ssl_md_alg_from_hash( unsigned char hash )
6080{
6081 switch( hash )
6082 {
6083#if defined(POLARSSL_MD5_C)
6084 case SSL_HASH_MD5:
6085 return( POLARSSL_MD_MD5 );
6086#endif
6087#if defined(POLARSSL_SHA1_C)
6088 case SSL_HASH_SHA1:
6089 return( POLARSSL_MD_SHA1 );
6090#endif
6091#if defined(POLARSSL_SHA256_C)
6092 case SSL_HASH_SHA224:
6093 return( POLARSSL_MD_SHA224 );
6094 case SSL_HASH_SHA256:
6095 return( POLARSSL_MD_SHA256 );
6096#endif
6097#if defined(POLARSSL_SHA512_C)
6098 case SSL_HASH_SHA384:
6099 return( POLARSSL_MD_SHA384 );
6100 case SSL_HASH_SHA512:
6101 return( POLARSSL_MD_SHA512 );
6102#endif
6103 default:
6104 return( POLARSSL_MD_NONE );
6105 }
6106}
6107
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006108#if defined(POLARSSL_SSL_SET_CURVES)
6109/*
6110 * Check is a curve proposed by the peer is in our list.
6111 * Return 1 if we're willing to use it, 0 otherwise.
6112 */
6113int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6114{
6115 const ecp_group_id *gid;
6116
6117 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6118 if( *gid == grp_id )
6119 return( 1 );
6120
6121 return( 0 );
6122}
Paul Bakker9af723c2014-05-01 13:03:14 +02006123#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006124
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006125#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006126int ssl_check_cert_usage( const x509_crt *cert,
6127 const ssl_ciphersuite_t *ciphersuite,
6128 int cert_endpoint )
6129{
6130#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6131 int usage = 0;
6132#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006133#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6134 const char *ext_oid;
6135 size_t ext_len;
6136#endif
6137
6138#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6139 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6140 ((void) cert);
6141 ((void) cert_endpoint);
6142#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006143
6144#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6145 if( cert_endpoint == SSL_IS_SERVER )
6146 {
6147 /* Server part of the key exchange */
6148 switch( ciphersuite->key_exchange )
6149 {
6150 case POLARSSL_KEY_EXCHANGE_RSA:
6151 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6152 usage = KU_KEY_ENCIPHERMENT;
6153 break;
6154
6155 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6156 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6157 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6158 usage = KU_DIGITAL_SIGNATURE;
6159 break;
6160
6161 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6162 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6163 usage = KU_KEY_AGREEMENT;
6164 break;
6165
6166 /* Don't use default: we want warnings when adding new values */
6167 case POLARSSL_KEY_EXCHANGE_NONE:
6168 case POLARSSL_KEY_EXCHANGE_PSK:
6169 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6170 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6171 usage = 0;
6172 }
6173 }
6174 else
6175 {
6176 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6177 usage = KU_DIGITAL_SIGNATURE;
6178 }
6179
6180 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6181 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006182#else
6183 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006184#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6185
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006186#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6187 if( cert_endpoint == SSL_IS_SERVER )
6188 {
6189 ext_oid = OID_SERVER_AUTH;
6190 ext_len = OID_SIZE( OID_SERVER_AUTH );
6191 }
6192 else
6193 {
6194 ext_oid = OID_CLIENT_AUTH;
6195 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6196 }
6197
6198 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6199 return( -1 );
6200#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6201
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006202 return( 0 );
6203}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006204#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006205
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006206/*
6207 * Convert version numbers to/from wire format
6208 * and, for DTLS, to/from TLS equivalent.
6209 *
6210 * For TLS this is the identity.
6211 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6212 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6213 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6214 */
6215void ssl_write_version( int major, int minor, int transport,
6216 unsigned char ver[2] )
6217{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006218#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006219 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006220 {
6221 if( minor == SSL_MINOR_VERSION_2 )
6222 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6223
6224 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6225 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6226 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006227 else
6228#else
6229 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006230#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006231 {
6232 ver[0] = (unsigned char) major;
6233 ver[1] = (unsigned char) minor;
6234 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006235}
6236
6237void ssl_read_version( int *major, int *minor, int transport,
6238 const unsigned char ver[2] )
6239{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006240#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006241 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006242 {
6243 *major = 255 - ver[0] + 2;
6244 *minor = 255 - ver[1] + 1;
6245
6246 if( *minor == SSL_MINOR_VERSION_1 )
6247 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6248 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006249 else
6250#else
6251 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006252#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006253 {
6254 *major = ver[0];
6255 *minor = ver[1];
6256 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006257}
6258
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006259#endif /* POLARSSL_SSL_TLS_C */