blob: f6626c995c803dfa93f3fae6a205567db9f65ffb [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
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02001964 SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001965
Paul Bakker831a7552011-05-18 13:32:51 +00001966 if( ret == 0 )
1967 return( POLARSSL_ERR_SSL_CONN_EOF );
1968
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02001969 if( ret == POLARSSL_ERR_NET_TIMEOUT ||
1970 ( ret == POLARSSL_ERR_NET_WANT_READ &&
1971 ssl_check_timer( ssl ) != 0 ) )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02001972 {
1973 SSL_DEBUG_MSG( 2, ( "recv timeout" ) );
1974
1975 if( ( ret = ssl_resend( ssl ) ) != 0 )
1976 {
1977 SSL_DEBUG_RET( 1, "ssl_resend", ret );
1978 return( ret );
1979 }
1980
1981 return( POLARSSL_ERR_NET_WANT_READ );
1982 }
1983
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 if( ret < 0 )
1985 return( ret );
1986
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001987 ssl->in_left = ret;
1988 }
1989 else
1990#endif
1991 {
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001992 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1993 ssl->in_left, nb_want ) );
1994
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001995 while( ssl->in_left < nb_want )
1996 {
1997 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02001998 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr + ssl->in_left, len );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001999
2000 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2001 ssl->in_left, nb_want ) );
2002 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2003
2004 if( ret == 0 )
2005 return( POLARSSL_ERR_SSL_CONN_EOF );
2006
2007 if( ret < 0 )
2008 return( ret );
2009
2010 ssl->in_left += ret;
2011 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 }
2013
2014 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2015
2016 return( 0 );
2017}
2018
2019/*
2020 * Flush any data not yet written
2021 */
2022int ssl_flush_output( ssl_context *ssl )
2023{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002024 int ret;
2025 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00002026
2027 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2028
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002029 if( ssl->f_send == NULL )
2030 {
2031 SSL_DEBUG_MSG( 1, ( "Bad usage of ssl_set_bio() "
2032 "or ssl_set_bio_timeout()" ) );
2033 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2034 }
2035
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002036 /* Avoid incrementing counter if data is flushed */
2037 if( ssl->out_left == 0 )
2038 {
2039 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2040 return( 0 );
2041 }
2042
Paul Bakker5121ce52009-01-03 21:22:43 +00002043 while( ssl->out_left > 0 )
2044 {
2045 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002046 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002047
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002048 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
2049 ssl->out_msglen - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02002050 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002051
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2053
2054 if( ret <= 0 )
2055 return( ret );
2056
2057 ssl->out_left -= ret;
2058 }
2059
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002060 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002061 if( ++ssl->out_ctr[i - 1] != 0 )
2062 break;
2063
2064 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01002065 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002066 {
2067 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
2068 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
2069 }
2070
Paul Bakker5121ce52009-01-03 21:22:43 +00002071 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2072
2073 return( 0 );
2074}
2075
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002076/*
2077 * Functions to handle the DTLS retransmission state machine
2078 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002079#if defined(POLARSSL_SSL_PROTO_DTLS)
2080/*
2081 * Append current handshake message to current outgoing flight
2082 */
2083static int ssl_flight_append( ssl_context *ssl )
2084{
2085 ssl_flight_item *msg;
2086
2087 /* Allocate space for current message */
2088 if( ( msg = polarssl_malloc( sizeof( ssl_flight_item ) ) ) == NULL )
2089 {
2090 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed",
2091 sizeof( ssl_flight_item ) ) );
2092 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2093 }
2094
2095 if( ( msg->p = polarssl_malloc( ssl->out_msglen ) ) == NULL )
2096 {
2097 SSL_DEBUG_MSG( 1, ( "malloc %d bytes failed", ssl->out_msglen ) );
2098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2099 }
2100
2101 /* Copy current handshake message with headers */
2102 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
2103 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002104 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002105 msg->next = NULL;
2106
2107 /* Append to the current flight */
2108 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002109 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002110 else
2111 {
2112 ssl_flight_item *cur = ssl->handshake->flight;
2113 while( cur->next != NULL )
2114 cur = cur->next;
2115 cur->next = msg;
2116 }
2117
2118 return( 0 );
2119}
2120
2121/*
2122 * Free the current flight of handshake messages
2123 */
2124static void ssl_flight_free( ssl_flight_item *flight )
2125{
2126 ssl_flight_item *cur = flight;
2127 ssl_flight_item *next;
2128
2129 while( cur != NULL )
2130 {
2131 next = cur->next;
2132
2133 polarssl_free( cur->p );
2134 polarssl_free( cur );
2135
2136 cur = next;
2137 }
2138}
2139
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002140#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2141static void ssl_dtls_replay_reset( ssl_context *ssl );
2142#endif
2143
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002144/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002145 * Swap transform_out and out_ctr with the alternative ones
2146 */
2147static void ssl_swap_epochs( ssl_context *ssl )
2148{
2149 ssl_transform *tmp_transform;
2150 unsigned char tmp_out_ctr[8];
2151
2152 if( ssl->transform_out == ssl->handshake->alt_transform_out )
2153 {
2154 SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
2155 return;
2156 }
2157
2158 SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
2159
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002160 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002161 tmp_transform = ssl->transform_out;
2162 ssl->transform_out = ssl->handshake->alt_transform_out;
2163 ssl->handshake->alt_transform_out = tmp_transform;
2164
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002165 /* Swap epoch + sequence_number */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002166 memcpy( tmp_out_ctr, ssl->out_ctr, 8 );
2167 memcpy( ssl->out_ctr, ssl->handshake->alt_out_ctr, 8 );
2168 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002169
2170 /* Adjust to the newly activated transform */
2171 if( ssl->transform_out != NULL &&
2172 ssl->minor_ver >= SSL_MINOR_VERSION_2 )
2173 {
2174 ssl->out_msg = ssl->out_iv + ssl->transform_out->ivlen -
2175 ssl->transform_out->fixed_ivlen;
2176 }
2177 else
2178 ssl->out_msg = ssl->out_iv;
2179
2180#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
2181 if( ssl_hw_record_activate != NULL )
2182 {
2183 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
2184 {
2185 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
2186 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
2187 }
2188 }
2189#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002190}
2191
2192/*
2193 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002194 *
2195 * Need to remember the current message in case flush_output returns
2196 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002197 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002198 */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002199int ssl_resend( ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002200{
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002201 SSL_DEBUG_MSG( 2, ( "=> ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002202
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002203 if( ssl->handshake->retransmit_state != SSL_RETRANS_SENDING )
2204 {
2205 SSL_DEBUG_MSG( 2, ( "initialise resending" ) );
2206
2207 ssl->handshake->cur_msg = ssl->handshake->flight;
2208 ssl_swap_epochs( ssl );
2209
2210 ssl->handshake->retransmit_state = SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002211
2212 /* Cancel running timer */
2213 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002214 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002215
2216 while( ssl->handshake->cur_msg != NULL )
2217 {
2218 int ret;
2219 ssl_flight_item *cur = ssl->handshake->cur_msg;
2220
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02002221 /* Swap epochs before sending Finished: we can't do it after
2222 * sending ChangeCipherSpec, in case write returns WANT_READ.
2223 * Must be done before copying, may change out_msg pointer */
2224 if( cur->type == SSL_MSG_HANDSHAKE &&
2225 cur->p[0] == SSL_HS_FINISHED )
2226 {
2227 ssl_swap_epochs( ssl );
2228 }
2229
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002230 memcpy( ssl->out_msg, cur->p, cur->len );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002231 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002232 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002233
2234 ssl->handshake->cur_msg = cur->next;
2235
2236 SSL_DEBUG_BUF( 3, "resent handshake message header", ssl->out_msg, 12 );
2237
2238 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2239 {
2240 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2241 return( ret );
2242 }
2243 }
2244
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02002245 if( ssl->state == SSL_HANDSHAKE_OVER )
2246 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2247 else
2248 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002249
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002250 /* WIP: hardcoded 1 sec will be replaced */
2251 ssl_set_timer( ssl, 1000 );
2252
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002253 SSL_DEBUG_MSG( 2, ( "<= ssl_resend" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002254
2255 return( 0 );
2256}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002257
2258/*
2259 * To be called when the last message of an incoming flight is received.
2260 */
2261void ssl_recv_flight_completed( ssl_context *ssl )
2262{
2263 /* We won't need to resend that one any more */
2264 ssl_flight_free( ssl->handshake->flight );
2265 ssl->handshake->flight = NULL;
2266 ssl->handshake->cur_msg = NULL;
2267
2268 /* The next incoming flight will start with this msg_seq */
2269 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
2270
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002271 /* Cancel timer */
2272 ssl_set_timer( ssl, 0 );
2273
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002274 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2275 ssl->in_msg[0] == SSL_HS_FINISHED )
2276 {
2277 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2278 }
2279 else
2280 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
2281}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02002282
2283/*
2284 * To be called when the last message of an outgoing flight is send.
2285 */
2286void ssl_send_flight_completed( ssl_context *ssl )
2287{
2288 /* WIP: hardcoded 1 sec is temporary */
2289 ssl_set_timer( ssl, 1000 );
2290
2291 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2292 ssl->in_msg[0] == SSL_HS_FINISHED )
2293 {
2294 ssl->handshake->retransmit_state = SSL_RETRANS_FINISHED;
2295 }
2296 else
2297 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
2298}
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002299#endif /* POLARSSL_SSL_PROTO_DTLS */
2300
Paul Bakker5121ce52009-01-03 21:22:43 +00002301/*
2302 * Record layer functions
2303 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002304
2305/*
2306 * Write current record.
2307 * Uses ssl->out_msgtype, ssl->out_msglen and bytes at ssl->out_msg.
2308 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002309int ssl_write_record( ssl_context *ssl )
2310{
Paul Bakker05ef8352012-05-08 09:17:57 +00002311 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002312 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
2314 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2315
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002316#if defined(POLARSSL_SSL_PROTO_DTLS)
2317 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2318 ssl->handshake != NULL &&
2319 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
2320 {
2321 ; /* Skip special handshake treatment when resending */
2322 }
2323 else
2324#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002325 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2326 {
2327 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2328 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2329 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2330
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002331 /*
2332 * DTLS has additional fields in the Handshake layer,
2333 * between the length field and the actual payload:
2334 * uint16 message_seq;
2335 * uint24 fragment_offset;
2336 * uint24 fragment_length;
2337 */
2338#if defined(POLARSSL_SSL_PROTO_DTLS)
2339 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2340 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002341 /* Make room for the additional DTLS fields */
2342 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002343 ssl->out_msglen += 8;
2344 len += 8;
2345
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002346 /* Write message_seq and update it, except for HelloRequest */
2347 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2348 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02002349 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
2350 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
2351 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02002352 }
2353 else
2354 {
2355 ssl->out_msg[4] = 0;
2356 ssl->out_msg[5] = 0;
2357 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002358
2359 /* We don't fragment, so frag_offset = 0 and frag_len = len */
2360 memset( ssl->out_msg + 6, 0x00, 3 );
2361 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002362 }
2363#endif /* POLARSSL_SSL_PROTO_DTLS */
2364
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002365 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2366 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 }
2368
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002369 /* Save handshake and CCS messages for resending */
2370#if defined(POLARSSL_SSL_PROTO_DTLS)
2371 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2372 ssl->handshake != NULL &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02002373 ssl->handshake->retransmit_state != SSL_RETRANS_SENDING &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02002374 ( ssl->out_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC ||
2375 ssl->out_msgtype == SSL_MSG_HANDSHAKE ) )
2376 {
2377 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
2378 {
2379 SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
2380 return( ret );
2381 }
2382 }
2383#endif
2384
Paul Bakker2770fbd2012-07-03 13:30:23 +00002385#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002386 if( ssl->transform_out != NULL &&
2387 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002388 {
2389 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2390 {
2391 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2392 return( ret );
2393 }
2394
2395 len = ssl->out_msglen;
2396 }
2397#endif /*POLARSSL_ZLIB_SUPPORT */
2398
Paul Bakker05ef8352012-05-08 09:17:57 +00002399#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002400 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002402 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Paul Bakker05ef8352012-05-08 09:17:57 +00002404 ret = ssl_hw_record_write( ssl );
2405 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2406 {
2407 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002408 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002409 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002410
2411 if( ret == 0 )
2412 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002413 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002414#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002415 if( !done )
2416 {
2417 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002418 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2419 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002420
2421 ssl->out_len[0] = (unsigned char)( len >> 8 );
2422 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002423
Paul Bakker48916f92012-09-16 19:57:18 +00002424 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002425 {
2426 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2427 {
2428 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2429 return( ret );
2430 }
2431
2432 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002433 ssl->out_len[0] = (unsigned char)( len >> 8 );
2434 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002435 }
2436
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002437 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002438
2439 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2440 "version = [%d:%d], msglen = %d",
2441 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002442 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002443
2444 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002445 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446 }
2447
Paul Bakker5121ce52009-01-03 21:22:43 +00002448 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2449 {
2450 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2451 return( ret );
2452 }
2453
2454 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2455
2456 return( 0 );
2457}
2458
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002459#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002460/*
2461 * Mark bits in bitmask (used for DTLS HS reassembly)
2462 */
2463static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
2464{
2465 unsigned int start_bits, end_bits;
2466
2467 start_bits = 8 - ( offset % 8 );
2468 if( start_bits != 8 )
2469 {
2470 size_t first_byte_idx = offset / 8;
2471
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02002472 /* Special case */
2473 if( len <= start_bits )
2474 {
2475 for( ; len != 0; len-- )
2476 mask[first_byte_idx] |= 1 << ( start_bits - len );
2477
2478 /* Avoid potential issues with offset or len becoming invalid */
2479 return;
2480 }
2481
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002482 offset += start_bits; /* Now offset % 8 == 0 */
2483 len -= start_bits;
2484
2485 for( ; start_bits != 0; start_bits-- )
2486 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
2487 }
2488
2489 end_bits = len % 8;
2490 if( end_bits != 0 )
2491 {
2492 size_t last_byte_idx = ( offset + len ) / 8;
2493
2494 len -= end_bits; /* Now len % 8 == 0 */
2495
2496 for( ; end_bits != 0; end_bits-- )
2497 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
2498 }
2499
2500 memset( mask + offset / 8, 0xFF, len / 8 );
2501}
2502
2503/*
2504 * Check that bitmask is full
2505 */
2506static int ssl_bitmask_check( unsigned char *mask, size_t len )
2507{
2508 size_t i;
2509
2510 for( i = 0; i < len / 8; i++ )
2511 if( mask[i] != 0xFF )
2512 return( -1 );
2513
2514 for( i = 0; i < len % 8; i++ )
2515 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
2516 return( -1 );
2517
2518 return( 0 );
2519}
2520
2521/*
2522 * Reassemble fragmented DTLS handshake messages.
2523 *
2524 * Use a temporary buffer for reassembly, divided in two parts:
2525 * - the first holds the reassembled message (including handshake header),
2526 * - the second holds a bitmask indicating which parts of the message
2527 * (excluding headers) have been received so far.
2528 */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002529static int ssl_reassemble_dtls_handshake( ssl_context *ssl )
2530{
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002531 unsigned char *msg, *bitmask;
2532 size_t frag_len, frag_off;
2533 size_t msg_len = ssl->in_hslen - 12; /* Without headers */
2534
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002535 if( ssl->handshake == NULL )
2536 {
2537 SSL_DEBUG_MSG( 1, ( "not supported outside handshake (for now)" ) );
2538 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2539 }
2540
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002541 /*
2542 * For first fragment, check size and allocate buffer
2543 */
2544 if( ssl->handshake->hs_msg == NULL )
2545 {
2546 size_t alloc_len;
2547
2548 SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
2549 msg_len ) );
2550
2551 if( ssl->in_hslen > SSL_MAX_CONTENT_LEN )
2552 {
2553 SSL_DEBUG_MSG( 1, ( "handshake message too large" ) );
2554 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2555 }
2556
2557 /* The bitmask needs one bit per byte of message excluding header */
2558 alloc_len = 12 + msg_len + msg_len / 8 + ( msg_len % 8 != 0 );
2559
2560 ssl->handshake->hs_msg = polarssl_malloc( alloc_len );
2561 if( ssl->handshake->hs_msg == NULL )
2562 {
2563 SSL_DEBUG_MSG( 1, ( "malloc failed (%d bytes)", alloc_len ) );
2564 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2565 }
2566
2567 memset( ssl->handshake->hs_msg, 0, alloc_len );
2568
2569 /* Prepare final header: copy msg_type, length and message_seq,
2570 * then add standardised fragment_offset and fragment_length */
2571 memcpy( ssl->handshake->hs_msg, ssl->in_msg, 6 );
2572 memset( ssl->handshake->hs_msg + 6, 0, 3 );
2573 memcpy( ssl->handshake->hs_msg + 9,
2574 ssl->handshake->hs_msg + 1, 3 );
2575 }
2576 else
2577 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002578 /* Make sure msg_type and length are consistent */
2579 if( memcmp( ssl->handshake->hs_msg, ssl->in_msg, 4 ) != 0 )
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002580 {
2581 SSL_DEBUG_MSG( 1, ( "fragment header mismatch" ) );
2582 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2583 }
2584 }
2585
2586 msg = ssl->handshake->hs_msg + 12;
2587 bitmask = msg + msg_len;
2588
2589 /*
2590 * Check and copy current fragment
2591 */
2592 frag_off = ( ssl->in_msg[6] << 16 ) |
2593 ( ssl->in_msg[7] << 8 ) |
2594 ssl->in_msg[8];
2595 frag_len = ( ssl->in_msg[9] << 16 ) |
2596 ( ssl->in_msg[10] << 8 ) |
2597 ssl->in_msg[11];
2598
2599 if( frag_off + frag_len > msg_len )
2600 {
2601 SSL_DEBUG_MSG( 1, ( "invalid fragment offset/len: %d + %d > %d",
2602 frag_off, frag_len, msg_len ) );
2603 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2604 }
2605
2606 if( frag_len + 12 > ssl->in_msglen )
2607 {
2608 SSL_DEBUG_MSG( 1, ( "invalid fragment length: %d + 12 > %d",
2609 frag_len, ssl->in_msglen ) );
2610 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2611 }
2612
2613 SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
2614 frag_off, frag_len ) );
2615
2616 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
2617 ssl_bitmask_set( bitmask, frag_off, frag_len );
2618
2619 /*
2620 * Do we have the complete message by now?
2621 * If yes, finalize it, else ask to read the next record.
2622 */
2623 if( ssl_bitmask_check( bitmask, msg_len ) != 0 )
2624 {
2625 SSL_DEBUG_MSG( 2, ( "message is not complete yet" ) );
2626 return( POLARSSL_ERR_NET_WANT_READ );
2627 }
2628
2629 SSL_DEBUG_MSG( 2, ( "handshake message completed" ) );
2630
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002631 if( ssl->in_left > ssl->next_record_offset )
2632 {
2633 /*
2634 * We've got more data in the buffer after the current record,
2635 * that we don't want to overwrite. Move it before writing the
2636 * reassembled message, and adjust in_left and next_record_offset.
2637 */
2638 unsigned char *cur_remain = ssl->in_hdr + ssl->next_record_offset;
2639 unsigned char *new_remain = ssl->in_msg + ssl->in_hslen;
2640 size_t remain_len = ssl->in_left - ssl->next_record_offset;
2641
2642 /* First compute and check new lengths */
2643 ssl->next_record_offset = new_remain - ssl->in_hdr;
2644 ssl->in_left = ssl->next_record_offset + remain_len;
2645
2646 if( ssl->in_left > SSL_BUFFER_LEN -
2647 (size_t)( ssl->in_hdr - ssl->in_buf ) )
2648 {
2649 SSL_DEBUG_MSG( 1, ( "reassembled message too large for buffer" ) );
2650 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
2651 }
2652
2653 memmove( new_remain, cur_remain, remain_len );
2654 }
2655
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002656 memcpy( ssl->in_msg, ssl->handshake->hs_msg, ssl->in_hslen );
2657
2658 polarssl_free( ssl->handshake->hs_msg );
2659 ssl->handshake->hs_msg = NULL;
2660
2661 SSL_DEBUG_BUF( 3, "reassembled handshake message",
2662 ssl->in_msg, ssl->in_hslen );
2663
2664 return( 0 );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002665}
2666#endif /* POLARSSL_SSL_PROTO_DTLS */
2667
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002668static int ssl_prepare_handshake_record( ssl_context *ssl )
2669{
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002670 if( ssl->in_msglen < ssl_hs_hdr_len( ssl ) )
2671 {
2672 SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
2673 ssl->in_msglen ) );
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02002674 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02002675 }
2676
2677 ssl->in_hslen = ssl_hs_hdr_len( ssl ) + (
2678 ( ssl->in_msg[1] << 16 ) |
2679 ( ssl->in_msg[2] << 8 ) |
2680 ssl->in_msg[3] );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002681
2682 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2683 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002684 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002685
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002686#if defined(POLARSSL_SSL_PROTO_DTLS)
2687 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002688 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002689 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002690 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002691
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002692 /* ssl->handshake is NULL when receiving ClientHello for renego */
2693 if( ssl->handshake != NULL &&
2694 recv_msg_seq != ssl->handshake->in_msg_seq )
2695 {
Manuel Pégourié-Gonnard93017de2014-09-19 22:42:40 +02002696 /* No sane server ever retransmits HelloVerifyRequest */
2697 if( recv_msg_seq < ssl->handshake->in_flight_start_seq &&
2698 ssl->in_msg[0] != SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002699 {
2700 SSL_DEBUG_MSG( 2, ( "received message from last flight, "
2701 "message_seq = %d, start_of_flight = %d",
2702 recv_msg_seq,
2703 ssl->handshake->in_flight_start_seq ) );
2704
2705 if( ( ret = ssl_resend( ssl ) ) != 0 )
2706 {
2707 SSL_DEBUG_RET( 1, "ssl_resend", ret );
2708 return( ret );
2709 }
2710 }
2711 else
2712 {
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02002713 SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02002714 "message_seq = %d, expected = %d",
2715 recv_msg_seq,
2716 ssl->handshake->in_msg_seq ) );
2717 }
2718
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002719 return( POLARSSL_ERR_NET_WANT_READ );
2720 }
2721 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002722
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002723 /* Reassemble if current message is fragmented or reassembly is
2724 * already in progress */
2725 if( ssl->in_msglen < ssl->in_hslen ||
2726 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
2727 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ||
2728 ( ssl->handshake != NULL && ssl->handshake->hs_msg != NULL ) )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002729 {
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02002730 SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
2731
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02002732 if( ( ret = ssl_reassemble_dtls_handshake( ssl ) ) != 0 )
2733 {
2734 SSL_DEBUG_RET( 1, "ssl_reassemble_dtls_handshake", ret );
2735 return( ret );
2736 }
2737 }
2738 }
2739 else
2740#endif /* POLARSSL_SSL_PROTO_DTLS */
2741 /* With TLS we don't handle fragmentation (for now) */
2742 if( ssl->in_msglen < ssl->in_hslen )
2743 {
2744 SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
Manuel Pégourié-Gonnard805e2302014-07-11 16:06:15 +02002745 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002746 }
2747
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002748 if( ssl->state != SSL_HANDSHAKE_OVER )
2749 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2750
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02002751 /* Handshake message is complete, increment counter */
2752#if defined(POLARSSL_SSL_PROTO_DTLS)
2753 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2754 ssl->handshake != NULL )
2755 {
2756 ssl->handshake->in_msg_seq++;
2757 }
2758#endif
2759
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002760 return( 0 );
2761}
2762
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002763/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002764 * DTLS anti-replay: RFC 6347 4.1.2.6
2765 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002766 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
2767 * Bit n is set iff record number in_window_top - n has been seen.
2768 *
2769 * Usually, in_window_top is the last record number seen and the lsb of
2770 * in_window is set. The only exception is the initial state (record number 0
2771 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002772 */
2773#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2774static void ssl_dtls_replay_reset( ssl_context *ssl )
2775{
2776 ssl->in_window_top = 0;
2777 ssl->in_window = 0;
2778}
2779
2780static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
2781{
2782 return( ( (uint64_t) buf[0] << 40 ) |
2783 ( (uint64_t) buf[1] << 32 ) |
2784 ( (uint64_t) buf[2] << 24 ) |
2785 ( (uint64_t) buf[3] << 16 ) |
2786 ( (uint64_t) buf[4] << 8 ) |
2787 ( (uint64_t) buf[5] ) );
2788}
2789
2790/*
2791 * Return 0 if sequence number is acceptable, -1 otherwise
2792 */
2793int ssl_dtls_replay_check( ssl_context *ssl )
2794{
2795 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2796 uint64_t bit;
2797
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002798 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2799 return( 0 );
2800
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002801 if( rec_seqnum > ssl->in_window_top )
2802 return( 0 );
2803
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002804 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002805
2806 if( bit >= 64 )
2807 return( -1 );
2808
2809 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
2810 return( -1 );
2811
2812 return( 0 );
2813}
2814
2815/*
2816 * Update replay window on new validated record
2817 */
2818void ssl_dtls_replay_update( ssl_context *ssl )
2819{
2820 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
2821
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02002822 if( ssl->anti_replay == SSL_ANTI_REPLAY_DISABLED )
2823 return;
2824
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002825 if( rec_seqnum > ssl->in_window_top )
2826 {
2827 /* Update window_top and the contents of the window */
2828 uint64_t shift = rec_seqnum - ssl->in_window_top;
2829
2830 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002831 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002832 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002833 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002834 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002835 ssl->in_window |= 1;
2836 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002837
2838 ssl->in_window_top = rec_seqnum;
2839 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002840 else
2841 {
2842 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002843 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02002844
2845 if( bit < 64 ) /* Always true, but be extra sure */
2846 ssl->in_window |= (uint64_t) 1 << bit;
2847 }
2848}
2849#endif /* POLARSSL_SSL_DTLS_ANTI_REPLAY */
2850
2851/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002852 * ContentType type;
2853 * ProtocolVersion version;
2854 * uint16 epoch; // DTLS only
2855 * uint48 sequence_number; // DTLS only
2856 * uint16 length;
2857 */
2858static int ssl_parse_record_header( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002859{
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002860 int ret;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002861 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02002863 SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, ssl_hdr_len( ssl ) );
2864
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002866 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002867 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
2869 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2870 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002871 ssl->in_msgtype,
2872 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002874 /* Check record type */
2875 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2876 ssl->in_msgtype != SSL_MSG_ALERT &&
2877 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2878 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2879 {
2880 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002881
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002882 if( ( ret = ssl_send_alert_message( ssl,
2883 SSL_ALERT_LEVEL_FATAL,
2884 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
2885 {
2886 return( ret );
2887 }
2888
2889 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2890 }
2891
2892 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002893 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002894 {
2895 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002896 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 }
2898
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002899 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 {
2901 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002902 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002903 }
2904
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002905 /* Check epoch (and sequence number) with DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002906#if defined(POLARSSL_SSL_PROTO_DTLS)
2907 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2908 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002909 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002910
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002911 if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002912 {
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002913 SSL_DEBUG_MSG( 1, ( "record from another epoch: "
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002914 "expected %d, received %d",
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02002915 ssl->in_epoch, rec_epoch ) );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002916 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002917 }
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002918
2919#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
2920 if( ssl_dtls_replay_check( ssl ) != 0 )
2921 {
2922 SSL_DEBUG_MSG( 1, ( "replayed record" ) );
2923 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2924 }
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002925#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02002926 }
2927#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard60ca5af2014-09-03 16:02:42 +02002928
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002929 /* Check length against the size of our buffer */
2930 if( ssl->in_msglen > SSL_BUFFER_LEN
2931 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002932 {
2933 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2934 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2935 }
2936
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002937 /* Check length against bounds of the current transform and version */
Paul Bakker48916f92012-09-16 19:57:18 +00002938 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002939 {
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02002940 if( ssl->in_msglen < 1 ||
2941 ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 {
2943 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002944 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 }
2946 }
2947 else
2948 {
Paul Bakker48916f92012-09-16 19:57:18 +00002949 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 {
2951 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002952 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 }
2954
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002955#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002957 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 {
2959 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002960 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002962#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002963#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2964 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 /*
2966 * TLS encrypted messages can have up to 256 bytes of padding
2967 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002969 ssl->in_msglen > ssl->transform_in->minlen +
2970 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002971 {
2972 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002973 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002974 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002975#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002976 }
2977
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002978 return( 0 );
2979}
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02002981/*
2982 * If applicable, decrypt (and decompress) record content
2983 */
2984static int ssl_prepare_record_content( ssl_context *ssl )
2985{
2986 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02002987
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002989 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker05ef8352012-05-08 09:17:57 +00002991#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002992 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002993 {
2994 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2995
2996 ret = ssl_hw_record_read( ssl );
2997 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2998 {
2999 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003000 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00003001 }
Paul Bakkerc7878112012-12-19 14:41:14 +01003002
3003 if( ret == 0 )
3004 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00003005 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003006#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00003007 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 {
3009 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
3010 {
3011 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
3012 return( ret );
3013 }
3014
3015 SSL_DEBUG_BUF( 4, "input payload after decrypt",
3016 ssl->in_msg, ssl->in_msglen );
3017
3018 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
3019 {
3020 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003021 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 }
3023 }
3024
Paul Bakker2770fbd2012-07-03 13:30:23 +00003025#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00003026 if( ssl->transform_in != NULL &&
3027 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003028 {
3029 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
3030 {
3031 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
3032 return( ret );
3033 }
3034
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01003035 // TODO: what's the purpose of these lines? is in_len used?
3036 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
3037 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003038 }
3039#endif /* POLARSSL_ZLIB_SUPPORT */
3040
Manuel Pégourié-Gonnard8464a462014-09-24 14:05:32 +02003041#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003042 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3043 {
3044 ssl_dtls_replay_update( ssl );
3045 }
3046#endif
3047
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003048 return( 0 );
3049}
3050
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003051static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl );
3052
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003053/*
3054 * Read a record.
3055 *
3056 * For DTLS, silently ignore invalid records (RFC 4.1.2.7.)
3057 * and continue reading until a valid record is found.
3058 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003059int ssl_read_record( ssl_context *ssl )
3060{
3061 int ret;
3062
3063 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
3064
Manuel Pégourié-Gonnard624bcb52014-09-10 21:56:38 +02003065 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003066 {
3067 /*
3068 * Get next Handshake message in the current record
3069 */
3070 ssl->in_msglen -= ssl->in_hslen;
3071
3072 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
3073 ssl->in_msglen );
3074
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02003075 SSL_DEBUG_BUF( 4, "remaining content in record",
3076 ssl->in_msg, ssl->in_msglen );
3077
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003078 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3079 return( ret );
3080
3081 return( 0 );
3082 }
3083
3084 ssl->in_hslen = 0;
3085
3086 /*
3087 * Read the record header and parse it
3088 */
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003089#if defined(POLARSSL_SSL_PROTO_DTLS)
3090read_record_header:
3091#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003092 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
3093 {
3094 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3095 return( ret );
3096 }
3097
3098 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003099 {
3100#if defined(POLARSSL_SSL_PROTO_DTLS)
3101 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3102 {
3103 /* Ignore bad record and get next one; drop the whole datagram
3104 * since current header cannot be trusted to find the next record
3105 * in current datagram */
3106 ssl->next_record_offset = 0;
3107 ssl->in_left = 0;
3108
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003109 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003110 goto read_record_header;
3111 }
3112#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003113 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003114 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003115
3116 /*
3117 * Read and optionally decrypt the message contents
3118 */
3119 if( ( ret = ssl_fetch_input( ssl,
3120 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
3121 {
3122 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
3123 return( ret );
3124 }
3125
3126 /* Done reading this record, get ready for the next one */
3127#if defined(POLARSSL_SSL_PROTO_DTLS)
3128 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3129 ssl->next_record_offset = ssl->in_msglen + ssl_hdr_len( ssl );
3130 else
3131#endif
3132 ssl->in_left = 0;
3133
3134 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003135 {
3136#if defined(POLARSSL_SSL_PROTO_DTLS)
3137 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3138 {
3139 /* Silently discard invalid records */
3140 if( ret == POLARSSL_ERR_SSL_INVALID_RECORD ||
3141 ret == POLARSSL_ERR_SSL_INVALID_MAC )
3142 {
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003143 SSL_DEBUG_MSG( 1, ( "discarding invalid record" ) );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02003144 goto read_record_header;
3145 }
3146
3147 return( ret );
3148 }
3149 else
3150#endif
3151 {
3152 /* Error out (and send alert) on invalid records */
3153#if defined(POLARSSL_SSL_ALERT_MESSAGES)
3154 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
3155 {
3156 ssl_send_alert_message( ssl,
3157 SSL_ALERT_LEVEL_FATAL,
3158 SSL_ALERT_MSG_BAD_RECORD_MAC );
3159 }
3160#endif
3161 return( ret );
3162 }
3163 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003164
3165 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003166 * When we sent the last flight of the handshake, we MUST respond to a
3167 * retransmit of the peer's previous flight with a retransmit. (In
3168 * practice, only the Finished message will make it, other messages
3169 * including CCS use the old transform so they're dropped as invalid.)
3170 *
3171 * If the record we received is not a handshake message, however, it
3172 * means the peer received our last flight so we can clean up
3173 * handshake info.
3174 *
3175 * This check needs to be done before prepare_handshake() due to an edge
3176 * case: if the client immediately requests renegotiation, this
3177 * finishes the current handshake first, avoiding the new ClientHello
3178 * being mistaken for an ancient message in the current handshake.
3179 */
3180#if defined(POLARSSL_SSL_PROTO_DTLS)
3181 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3182 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003183 ssl->state == SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02003184 {
3185 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3186 ssl->in_msg[0] == SSL_HS_FINISHED )
3187 {
3188 SSL_DEBUG_MSG( 2, ( "received retransmit of last flight" ) );
3189
3190 if( ( ret = ssl_resend( ssl ) ) != 0 )
3191 {
3192 SSL_DEBUG_RET( 1, "ssl_resend", ret );
3193 return( ret );
3194 }
3195
3196 return( POLARSSL_ERR_NET_WANT_READ );
3197 }
3198 else
3199 {
3200 ssl_handshake_wrapup_free_hs_transform( ssl );
3201 }
3202 }
3203#endif
3204
3205 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02003206 * Handle particular types of records
3207 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003208 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
3209 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01003210 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
3211 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 }
3213
3214 if( ssl->in_msgtype == SSL_MSG_ALERT )
3215 {
3216 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
3217 ssl->in_msg[0], ssl->in_msg[1] ) );
3218
3219 /*
3220 * Ignore non-fatal alerts, except close_notify
3221 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003222 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003223 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
3225 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003226 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227 }
3228
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003229 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3230 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 {
3232 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003233 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003234 }
3235 }
3236
Manuel Pégourié-Gonnard767c6952014-09-20 10:04:00 +02003237#if defined(POLARSSL_SSL_PROTO_DTLS)
3238 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3239 {
3240 /* Drop unexpected ChangeCipherSpec messages */
3241 if( ssl->in_msgtype == SSL_MSG_CHANGE_CIPHER_SPEC &&
3242 ssl->state != SSL_CLIENT_CHANGE_CIPHER_SPEC &&
3243 ssl->state != SSL_SERVER_CHANGE_CIPHER_SPEC )
3244 {
3245 SSL_DEBUG_MSG( 2, ( "dropping unexpected ChangeCipherSpec" ) );
3246 return( POLARSSL_ERR_NET_WANT_READ );
3247 }
3248 }
3249#endif
3250
Paul Bakker5121ce52009-01-03 21:22:43 +00003251 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
3252
3253 return( 0 );
3254}
3255
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00003256int ssl_send_fatal_handshake_failure( ssl_context *ssl )
3257{
3258 int ret;
3259
3260 if( ( ret = ssl_send_alert_message( ssl,
3261 SSL_ALERT_LEVEL_FATAL,
3262 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
3263 {
3264 return( ret );
3265 }
3266
3267 return( 0 );
3268}
3269
Paul Bakker0a925182012-04-16 06:46:41 +00003270int ssl_send_alert_message( ssl_context *ssl,
3271 unsigned char level,
3272 unsigned char message )
3273{
3274 int ret;
3275
3276 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
3277
3278 ssl->out_msgtype = SSL_MSG_ALERT;
3279 ssl->out_msglen = 2;
3280 ssl->out_msg[0] = level;
3281 ssl->out_msg[1] = message;
3282
3283 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3284 {
3285 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3286 return( ret );
3287 }
3288
3289 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
3290
3291 return( 0 );
3292}
3293
Paul Bakker5121ce52009-01-03 21:22:43 +00003294/*
3295 * Handshake functions
3296 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003297#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3298 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
3299 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
3300 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3301 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
3302 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
3303 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003304int ssl_write_certificate( ssl_context *ssl )
3305{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003306 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307
3308 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3309
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003310 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3312 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003313 {
3314 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3315 ssl->state++;
3316 return( 0 );
3317 }
3318
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003319 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3320 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003321}
3322
3323int ssl_parse_certificate( ssl_context *ssl )
3324{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003325 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3326
3327 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3328
3329 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3331 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003332 {
3333 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3334 ssl->state++;
3335 return( 0 );
3336 }
3337
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003340}
3341#else
3342int ssl_write_certificate( ssl_context *ssl )
3343{
3344 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
3345 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003346 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003347 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3348
3349 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
3350
3351 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003352 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3353 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003354 {
3355 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3356 ssl->state++;
3357 return( 0 );
3358 }
3359
Paul Bakker5121ce52009-01-03 21:22:43 +00003360 if( ssl->endpoint == SSL_IS_CLIENT )
3361 {
3362 if( ssl->client_auth == 0 )
3363 {
3364 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
3365 ssl->state++;
3366 return( 0 );
3367 }
3368
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003369#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 /*
3371 * If using SSLv3 and got no cert, send an Alert message
3372 * (otherwise an empty Certificate message will be sent).
3373 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003374 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003375 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3376 {
3377 ssl->out_msglen = 2;
3378 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003379 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
3380 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00003381
3382 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
3383 goto write_msg;
3384 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003385#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 }
3387 else /* SSL_IS_SERVER */
3388 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003389 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 {
3391 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003392 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 }
3394 }
3395
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003396 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003397
3398 /*
3399 * 0 . 0 handshake type
3400 * 1 . 3 handshake length
3401 * 4 . 6 length of all certs
3402 * 7 . 9 length of cert. 1
3403 * 10 . n-1 peer certificate
3404 * n . n+2 length of cert. 2
3405 * n+3 . ... upper level cert, etc.
3406 */
3407 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003408 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003409
Paul Bakker29087132010-03-21 21:03:34 +00003410 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 {
3412 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01003413 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 {
3415 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
3416 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003417 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 }
3419
3420 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
3421 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
3422 ssl->out_msg[i + 2] = (unsigned char)( n );
3423
3424 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
3425 i += n; crt = crt->next;
3426 }
3427
3428 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
3429 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
3430 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
3431
3432 ssl->out_msglen = i;
3433 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3434 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
3435
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003436#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003437write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003438#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003439
3440 ssl->state++;
3441
3442 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3443 {
3444 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3445 return( ret );
3446 }
3447
3448 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
3449
Paul Bakkered27a042013-04-18 22:46:23 +02003450 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451}
3452
3453int ssl_parse_certificate( ssl_context *ssl )
3454{
Paul Bakkered27a042013-04-18 22:46:23 +02003455 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00003456 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003457 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003458
3459 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
3460
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003461 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003462 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003464 {
3465 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3466 ssl->state++;
3467 return( 0 );
3468 }
3469
Paul Bakker5121ce52009-01-03 21:22:43 +00003470 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003471 ( ssl->authmode == SSL_VERIFY_NONE ||
3472 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003474 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00003475 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
3476 ssl->state++;
3477 return( 0 );
3478 }
3479
3480 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3481 {
3482 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3483 return( ret );
3484 }
3485
3486 ssl->state++;
3487
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003488#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00003489 /*
3490 * Check if the client sent an empty certificate
3491 */
3492 if( ssl->endpoint == SSL_IS_SERVER &&
3493 ssl->minor_ver == SSL_MINOR_VERSION_0 )
3494 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003495 if( ssl->in_msglen == 2 &&
3496 ssl->in_msgtype == SSL_MSG_ALERT &&
3497 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
3498 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 {
3500 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
3501
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003502 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003503 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
3504 return( 0 );
3505 else
Paul Bakker40e46942009-01-03 21:51:57 +00003506 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003507 }
3508 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003509#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003511#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3512 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 if( ssl->endpoint == SSL_IS_SERVER &&
3514 ssl->minor_ver != SSL_MINOR_VERSION_0 )
3515 {
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003516 if( ssl->in_hslen == 3 + ssl_hs_hdr_len( ssl ) &&
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
3518 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
Manuel Pégourié-Gonnard08a1d4b2014-09-26 10:35:50 +02003519 memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 {
3521 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
3522
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02003523 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00003524 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00003525 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003526 else
3527 return( 0 );
3528 }
3529 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003530#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3531 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
3533 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3534 {
3535 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003536 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003537 }
3538
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003539 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE ||
3540 ssl->in_hslen < ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003541 {
3542 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003543 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 }
3545
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003546 i = ssl_hs_hdr_len( ssl );
3547
Paul Bakker5121ce52009-01-03 21:22:43 +00003548 /*
3549 * Same message structure as in ssl_write_certificate()
3550 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003551 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00003552
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003553 if( ssl->in_msg[i] != 0 ||
3554 ssl->in_hslen != n + 3 + ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003555 {
3556 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003557 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 }
3559
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003560 /* In case we tried to reuse a session but it failed */
3561 if( ssl->session_negotiate->peer_cert != NULL )
3562 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003563 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02003564 polarssl_free( ssl->session_negotiate->peer_cert );
3565 }
3566
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003567 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
3568 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003569 {
3570 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003571 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003572 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 }
3574
Paul Bakkerb6b09562013-09-18 14:17:41 +02003575 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003576
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00003577 i += 3;
Paul Bakker5121ce52009-01-03 21:22:43 +00003578
3579 while( i < ssl->in_hslen )
3580 {
3581 if( ssl->in_msg[i] != 0 )
3582 {
3583 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003584 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003585 }
3586
3587 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
3588 | (unsigned int) ssl->in_msg[i + 2];
3589 i += 3;
3590
3591 if( n < 128 || i + n > ssl->in_hslen )
3592 {
3593 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003594 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 }
3596
Paul Bakkerddf26b42013-09-18 13:46:23 +02003597 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
3598 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 if( ret != 0 )
3600 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02003601 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 return( ret );
3603 }
3604
3605 i += n;
3606 }
3607
Paul Bakker48916f92012-09-16 19:57:18 +00003608 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01003610 /*
3611 * On client, make sure the server cert doesn't change during renego to
3612 * avoid "triple handshake" attack: https://secure-resumption.com/
3613 */
3614 if( ssl->endpoint == SSL_IS_CLIENT &&
3615 ssl->renegotiation == SSL_RENEGOTIATION )
3616 {
3617 if( ssl->session->peer_cert == NULL )
3618 {
3619 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
3620 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3621 }
3622
3623 if( ssl->session->peer_cert->raw.len !=
3624 ssl->session_negotiate->peer_cert->raw.len ||
3625 memcmp( ssl->session->peer_cert->raw.p,
3626 ssl->session_negotiate->peer_cert->raw.p,
3627 ssl->session->peer_cert->raw.len ) != 0 )
3628 {
3629 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
3630 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
3631 }
3632 }
3633
Paul Bakker5121ce52009-01-03 21:22:43 +00003634 if( ssl->authmode != SSL_VERIFY_NONE )
3635 {
3636 if( ssl->ca_chain == NULL )
3637 {
3638 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003639 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003640 }
3641
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003642 /*
3643 * Main check: verify certificate
3644 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02003645 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
3646 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
3647 &ssl->session_negotiate->verify_result,
3648 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
3650 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003651 {
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003653 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003654
3655 /*
3656 * Secondary checks: always done, but change 'ret' only if it was 0
3657 */
3658
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003659#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003660 {
Paul Bakker93389cc2014-04-17 14:44:38 +02003661 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003662
3663 /* If certificate uses an EC key, make sure the curve is OK */
3664 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
3665 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
3666 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003667 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003668 if( ret == 0 )
3669 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01003670 }
3671 }
Paul Bakker9af723c2014-05-01 13:03:14 +02003672#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00003673
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003674 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
3675 ciphersuite_info,
3676 ! ssl->endpoint ) != 0 )
3677 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02003678 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02003679 if( ret == 0 )
3680 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
3681 }
3682
Paul Bakker5121ce52009-01-03 21:22:43 +00003683 if( ssl->authmode != SSL_VERIFY_REQUIRED )
3684 ret = 0;
3685 }
3686
3687 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
3688
3689 return( ret );
3690}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01003691#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
3692 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
3693 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
3694 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
3695 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
3696 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
3697 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003698
3699int ssl_write_change_cipher_spec( ssl_context *ssl )
3700{
3701 int ret;
3702
3703 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
3704
3705 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
3706 ssl->out_msglen = 1;
3707 ssl->out_msg[0] = 1;
3708
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 ssl->state++;
3710
3711 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3712 {
3713 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3714 return( ret );
3715 }
3716
3717 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
3718
3719 return( 0 );
3720}
3721
3722int ssl_parse_change_cipher_spec( ssl_context *ssl )
3723{
3724 int ret;
3725
3726 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
3727
Paul Bakker5121ce52009-01-03 21:22:43 +00003728 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3729 {
3730 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3731 return( ret );
3732 }
3733
3734 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
3735 {
3736 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003737 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 }
3739
3740 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
3741 {
3742 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003743 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00003744 }
3745
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02003746 /*
3747 * Switch to our negotiated transform and session parameters for inbound
3748 * data.
3749 */
3750 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3751 ssl->transform_in = ssl->transform_negotiate;
3752 ssl->session_in = ssl->session_negotiate;
3753
3754#if defined(POLARSSL_SSL_PROTO_DTLS)
3755 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3756 {
3757#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
3758 ssl_dtls_replay_reset( ssl );
3759#endif
3760
3761 /* Increment epoch */
3762 if( ++ssl->in_epoch == 0 )
3763 {
3764 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
3765 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
3766 }
3767 }
3768 else
3769#endif /* POLARSSL_SSL_PROTO_DTLS */
3770 memset( ssl->in_ctr, 0, 8 );
3771
3772 /*
3773 * Set the in_msg pointer to the correct location based on IV length
3774 */
3775 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3776 {
3777 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3778 ssl->transform_negotiate->fixed_ivlen;
3779 }
3780 else
3781 ssl->in_msg = ssl->in_iv;
3782
3783#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
3784 if( ssl_hw_record_activate != NULL )
3785 {
3786 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3787 {
3788 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3789 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3790 }
3791 }
3792#endif
3793
Paul Bakker5121ce52009-01-03 21:22:43 +00003794 ssl->state++;
3795
3796 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
3797
3798 return( 0 );
3799}
3800
Paul Bakker41c83d32013-03-20 14:39:14 +01003801void ssl_optimize_checksum( ssl_context *ssl,
3802 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00003803{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02003804 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01003805
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003806#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3807 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00003808 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00003809 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00003810 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003811#endif
3812#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3813#if defined(POLARSSL_SHA512_C)
3814 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
3815 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
3816 else
3817#endif
3818#if defined(POLARSSL_SHA256_C)
3819 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00003820 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003821 else
3822#endif
3823#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003824 {
3825 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003826 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003827 }
Paul Bakker380da532012-04-18 16:10:25 +00003828}
Paul Bakkerf7abd422013-04-16 13:15:56 +02003829
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02003830void ssl_reset_checksum( ssl_context *ssl )
3831{
3832#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3833 defined(POLARSSL_SSL_PROTO_TLS1_1)
3834 md5_starts( &ssl->handshake->fin_md5 );
3835 sha1_starts( &ssl->handshake->fin_sha1 );
3836#endif
3837#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3838#if defined(POLARSSL_SHA256_C)
3839 sha256_starts( &ssl->handshake->fin_sha256, 0 );
3840#endif
3841#if defined(POLARSSL_SHA512_C)
3842 sha512_starts( &ssl->handshake->fin_sha512, 1 );
3843#endif
3844#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3845}
3846
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003847static void ssl_update_checksum_start( ssl_context *ssl,
3848 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003849{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003850#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3851 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00003852 md5_update( &ssl->handshake->fin_md5 , buf, len );
3853 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003854#endif
3855#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3856#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02003857 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003858#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02003859#if defined(POLARSSL_SHA512_C)
3860 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01003861#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003862#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003863}
3864
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003865#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3866 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003867static void ssl_update_checksum_md5sha1( ssl_context *ssl,
3868 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003869{
Paul Bakker48916f92012-09-16 19:57:18 +00003870 md5_update( &ssl->handshake->fin_md5 , buf, len );
3871 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003872}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003873#endif
Paul Bakker380da532012-04-18 16:10:25 +00003874
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003875#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3876#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003877static void ssl_update_checksum_sha256( ssl_context *ssl,
3878 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003879{
Paul Bakker9e36f042013-06-30 14:34:05 +02003880 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003881}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003882#endif
Paul Bakker380da532012-04-18 16:10:25 +00003883
Paul Bakker9e36f042013-06-30 14:34:05 +02003884#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003885static void ssl_update_checksum_sha384( ssl_context *ssl,
3886 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003887{
Paul Bakker9e36f042013-06-30 14:34:05 +02003888 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003889}
Paul Bakker769075d2012-11-24 11:26:46 +01003890#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003891#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003892
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003893#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003894static void ssl_calc_finished_ssl(
3895 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003896{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003897 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003898 md5_context md5;
3899 sha1_context sha1;
3900
Paul Bakker5121ce52009-01-03 21:22:43 +00003901 unsigned char padbuf[48];
3902 unsigned char md5sum[16];
3903 unsigned char sha1sum[20];
3904
Paul Bakker48916f92012-09-16 19:57:18 +00003905 ssl_session *session = ssl->session_negotiate;
3906 if( !session )
3907 session = ssl->session;
3908
Paul Bakker1ef83d62012-04-11 12:09:53 +00003909 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3910
Paul Bakker48916f92012-09-16 19:57:18 +00003911 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3912 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913
3914 /*
3915 * SSLv3:
3916 * hash =
3917 * MD5( master + pad2 +
3918 * MD5( handshake + sender + master + pad1 ) )
3919 * + SHA1( master + pad2 +
3920 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003921 */
3922
Paul Bakker90995b52013-06-24 19:20:35 +02003923#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003924 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003925 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003926#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003927
Paul Bakker90995b52013-06-24 19:20:35 +02003928#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003929 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003930 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003931#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003932
Paul Bakker3c2122f2013-06-24 19:03:14 +02003933 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3934 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003935
Paul Bakker1ef83d62012-04-11 12:09:53 +00003936 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Paul Bakker3c2122f2013-06-24 19:03:14 +02003938 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003939 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003940 md5_update( &md5, padbuf, 48 );
3941 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Paul Bakker3c2122f2013-06-24 19:03:14 +02003943 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003944 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003945 sha1_update( &sha1, padbuf, 40 );
3946 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003947
Paul Bakker1ef83d62012-04-11 12:09:53 +00003948 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
Paul Bakker1ef83d62012-04-11 12:09:53 +00003950 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003951 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003952 md5_update( &md5, padbuf, 48 );
3953 md5_update( &md5, md5sum, 16 );
3954 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003955
Paul Bakker1ef83d62012-04-11 12:09:53 +00003956 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003957 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003958 sha1_update( &sha1, padbuf , 40 );
3959 sha1_update( &sha1, sha1sum, 20 );
3960 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Paul Bakker1ef83d62012-04-11 12:09:53 +00003962 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Paul Bakker5b4af392014-06-26 12:09:34 +02003964 md5_free( &md5 );
3965 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003966
Paul Bakker34617722014-06-13 17:20:13 +02003967 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3968 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3969 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003970
3971 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3972}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003973#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003974
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003975#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003976static void ssl_calc_finished_tls(
3977 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003978{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003979 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003980 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003981 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003982 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003983 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003984
Paul Bakker48916f92012-09-16 19:57:18 +00003985 ssl_session *session = ssl->session_negotiate;
3986 if( !session )
3987 session = ssl->session;
3988
Paul Bakker1ef83d62012-04-11 12:09:53 +00003989 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003990
Paul Bakker48916f92012-09-16 19:57:18 +00003991 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3992 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003993
Paul Bakker1ef83d62012-04-11 12:09:53 +00003994 /*
3995 * TLSv1:
3996 * hash = PRF( master, finished_label,
3997 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3998 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003999
Paul Bakker90995b52013-06-24 19:20:35 +02004000#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004001 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
4002 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004003#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004004
Paul Bakker90995b52013-06-24 19:20:35 +02004005#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004006 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
4007 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004008#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004009
4010 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004011 ? "client finished"
4012 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004013
4014 md5_finish( &md5, padbuf );
4015 sha1_finish( &sha1, padbuf + 16 );
4016
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004017 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004018 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004019
4020 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4021
Paul Bakker5b4af392014-06-26 12:09:34 +02004022 md5_free( &md5 );
4023 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004024
Paul Bakker34617722014-06-13 17:20:13 +02004025 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004026
4027 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4028}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004029#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004030
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004031#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4032#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004033static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00004034 ssl_context *ssl, unsigned char *buf, int from )
4035{
4036 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004037 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004038 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004039 unsigned char padbuf[32];
4040
Paul Bakker48916f92012-09-16 19:57:18 +00004041 ssl_session *session = ssl->session_negotiate;
4042 if( !session )
4043 session = ssl->session;
4044
Paul Bakker380da532012-04-18 16:10:25 +00004045 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004046
Paul Bakker9e36f042013-06-30 14:34:05 +02004047 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004048
4049 /*
4050 * TLSv1.2:
4051 * hash = PRF( master, finished_label,
4052 * Hash( handshake ) )[0.11]
4053 */
4054
Paul Bakker9e36f042013-06-30 14:34:05 +02004055#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00004056 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02004057 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004058#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00004059
4060 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004061 ? "client finished"
4062 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00004063
Paul Bakker9e36f042013-06-30 14:34:05 +02004064 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004065
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004066 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004067 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004068
4069 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4070
Paul Bakker5b4af392014-06-26 12:09:34 +02004071 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004072
Paul Bakker34617722014-06-13 17:20:13 +02004073 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004074
4075 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4076}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004077#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00004078
Paul Bakker9e36f042013-06-30 14:34:05 +02004079#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00004080static void ssl_calc_finished_tls_sha384(
4081 ssl_context *ssl, unsigned char *buf, int from )
4082{
4083 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02004084 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02004085 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00004086 unsigned char padbuf[48];
4087
Paul Bakker48916f92012-09-16 19:57:18 +00004088 ssl_session *session = ssl->session_negotiate;
4089 if( !session )
4090 session = ssl->session;
4091
Paul Bakker380da532012-04-18 16:10:25 +00004092 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004093
Paul Bakker9e36f042013-06-30 14:34:05 +02004094 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004095
4096 /*
4097 * TLSv1.2:
4098 * hash = PRF( master, finished_label,
4099 * Hash( handshake ) )[0.11]
4100 */
4101
Paul Bakker9e36f042013-06-30 14:34:05 +02004102#if !defined(POLARSSL_SHA512_ALT)
4103 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
4104 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02004105#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00004106
4107 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02004108 ? "client finished"
4109 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00004110
Paul Bakker9e36f042013-06-30 14:34:05 +02004111 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004112
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004113 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00004114 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004115
4116 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
4117
Paul Bakker5b4af392014-06-26 12:09:34 +02004118 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004119
Paul Bakker34617722014-06-13 17:20:13 +02004120 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00004121
4122 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
4123}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004124#endif /* POLARSSL_SHA512_C */
4125#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00004126
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004127static void ssl_handshake_wrapup_free_hs_transform( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004128{
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004129 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004130
4131 /*
4132 * Free our handshake params
4133 */
4134 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02004135 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00004136 ssl->handshake = NULL;
4137
4138 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004139 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00004140 */
4141 if( ssl->transform )
4142 {
4143 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004144 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004145 }
4146 ssl->transform = ssl->transform_negotiate;
4147 ssl->transform_negotiate = NULL;
4148
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004149 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
4150}
4151
4152void ssl_handshake_wrapup( ssl_context *ssl )
4153{
4154 int resume = ssl->handshake->resume;
4155
4156 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
4157
4158 if( ssl->renegotiation == SSL_RENEGOTIATION )
4159 {
4160 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
4161 ssl->renego_records_seen = 0;
4162 }
4163
4164 /*
4165 * Free the previous session and switch in the current one
4166 */
Paul Bakker0a597072012-09-25 21:55:46 +00004167 if( ssl->session )
4168 {
4169 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004170 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00004171 }
4172 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00004173 ssl->session_negotiate = NULL;
4174
Paul Bakker0a597072012-09-25 21:55:46 +00004175 /*
4176 * Add cache entry
4177 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004178 if( ssl->f_set_cache != NULL &&
4179 ssl->session->length != 0 &&
4180 resume == 0 )
4181 {
Paul Bakker0a597072012-09-25 21:55:46 +00004182 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
4183 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02004184 }
Paul Bakker0a597072012-09-25 21:55:46 +00004185
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02004186#if defined(POLARSSL_SSL_PROTO_DTLS)
4187 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4188 ssl->handshake->flight != NULL )
4189 {
4190 /* Keep last flight around in case we need to resend it:
4191 * we need the handshake and transform structures for that */
4192 SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
4193 }
4194 else
4195#endif
4196 ssl_handshake_wrapup_free_hs_transform( ssl );
4197
Paul Bakker48916f92012-09-16 19:57:18 +00004198 ssl->state++;
4199
4200 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
4201}
4202
Paul Bakker1ef83d62012-04-11 12:09:53 +00004203int ssl_write_finished( ssl_context *ssl )
4204{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004205 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00004206
4207 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
4208
Paul Bakker92be97b2013-01-02 17:30:03 +01004209 /*
4210 * Set the out_msg pointer to the correct location based on IV length
4211 */
4212 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
4213 {
4214 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
4215 ssl->transform_negotiate->fixed_ivlen;
4216 }
4217 else
4218 ssl->out_msg = ssl->out_iv;
4219
Paul Bakker48916f92012-09-16 19:57:18 +00004220 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00004221
4222 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
4224
Paul Bakker48916f92012-09-16 19:57:18 +00004225 ssl->verify_data_len = hash_len;
4226 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
4227
Paul Bakker5121ce52009-01-03 21:22:43 +00004228 ssl->out_msglen = 4 + hash_len;
4229 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4230 ssl->out_msg[0] = SSL_HS_FINISHED;
4231
4232 /*
4233 * In case of session resuming, invert the client and server
4234 * ChangeCipherSpec messages order.
4235 */
Paul Bakker0a597072012-09-25 21:55:46 +00004236 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004237 {
4238 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00004239 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004240 else
4241 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4242 }
4243 else
4244 ssl->state++;
4245
Paul Bakker48916f92012-09-16 19:57:18 +00004246 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004247 * Switch to our negotiated transform and session parameters for outbound
4248 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00004249 */
4250 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01004251
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004252#if defined(POLARSSL_SSL_PROTO_DTLS)
4253 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4254 {
4255 unsigned char i;
4256
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004257 /* Remember current epoch settings for resending */
4258 ssl->handshake->alt_transform_out = ssl->transform_out;
4259 memcpy( ssl->handshake->alt_out_ctr, ssl->out_ctr, 8 );
4260
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004261 /* Set sequence_number to zero */
4262 memset( ssl->out_ctr + 2, 0, 6 );
4263
4264 /* Increment epoch */
4265 for( i = 2; i > 0; i-- )
4266 if( ++ssl->out_ctr[i - 1] != 0 )
4267 break;
4268
4269 /* The loop goes to its end iff the counter is wrapping */
4270 if( i == 0 )
4271 {
4272 SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
4273 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
4274 }
4275 }
4276 else
4277#endif /* POLARSSL_SSL_PROTO_DTLS */
4278 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004279
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004280 ssl->transform_out = ssl->transform_negotiate;
4281 ssl->session_out = ssl->session_negotiate;
4282
Paul Bakker07eb38b2012-12-19 14:42:06 +01004283#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004284 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01004285 {
4286 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
4287 {
4288 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
4289 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4290 }
4291 }
4292#endif
4293
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004294#if defined(POLARSSL_SSL_PROTO_DTLS)
4295 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4296 ssl_send_flight_completed( ssl );
4297#endif
4298
Paul Bakker5121ce52009-01-03 21:22:43 +00004299 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4300 {
4301 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4302 return( ret );
4303 }
4304
4305 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
4306
4307 return( 0 );
4308}
4309
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004310#if defined(POLARSSL_SSL_PROTO_SSL3)
4311#define SSL_MAX_HASH_LEN 36
4312#else
4313#define SSL_MAX_HASH_LEN 12
4314#endif
4315
Paul Bakker5121ce52009-01-03 21:22:43 +00004316int ssl_parse_finished( ssl_context *ssl )
4317{
Paul Bakker23986e52011-04-24 08:57:21 +00004318 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02004319 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004320 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00004321
4322 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
4323
Paul Bakker48916f92012-09-16 19:57:18 +00004324 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325
Paul Bakker5121ce52009-01-03 21:22:43 +00004326 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4327 {
4328 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4329 return( ret );
4330 }
4331
4332 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
4333 {
4334 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004335 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004336 }
4337
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00004338 /* There is currently no ciphersuite using another length with TLS 1.2 */
4339#if defined(POLARSSL_SSL_PROTO_SSL3)
4340 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
4341 hash_len = 36;
4342 else
4343#endif
4344 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00004345
4346 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004347 ssl->in_hslen != ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004348 {
4349 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004350 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004351 }
4352
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00004353 if( safer_memcmp( ssl->in_msg + ssl_hs_hdr_len( ssl ),
4354 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
4356 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004357 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004358 }
4359
Paul Bakker48916f92012-09-16 19:57:18 +00004360 ssl->verify_data_len = hash_len;
4361 memcpy( ssl->peer_verify_data, buf, hash_len );
4362
Paul Bakker0a597072012-09-25 21:55:46 +00004363 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 {
4365 if( ssl->endpoint == SSL_IS_CLIENT )
4366 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
4367
4368 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00004369 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00004370 }
4371 else
4372 ssl->state++;
4373
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004374#if defined(POLARSSL_SSL_PROTO_DTLS)
4375 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4376 ssl_recv_flight_completed( ssl );
4377#endif
4378
Paul Bakker5121ce52009-01-03 21:22:43 +00004379 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
4380
4381 return( 0 );
4382}
4383
Paul Bakker968afaa2014-07-09 11:09:24 +02004384static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004385{
4386 memset( handshake, 0, sizeof( ssl_handshake_params ) );
4387
4388#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
4389 defined(POLARSSL_SSL_PROTO_TLS1_1)
4390 md5_init( &handshake->fin_md5 );
4391 sha1_init( &handshake->fin_sha1 );
4392 md5_starts( &handshake->fin_md5 );
4393 sha1_starts( &handshake->fin_sha1 );
4394#endif
4395#if defined(POLARSSL_SSL_PROTO_TLS1_2)
4396#if defined(POLARSSL_SHA256_C)
4397 sha256_init( &handshake->fin_sha256 );
4398 sha256_starts( &handshake->fin_sha256, 0 );
4399#endif
4400#if defined(POLARSSL_SHA512_C)
4401 sha512_init( &handshake->fin_sha512 );
4402 sha512_starts( &handshake->fin_sha512, 1 );
4403#endif
4404#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
4405
4406 handshake->update_checksum = ssl_update_checksum_start;
4407 handshake->sig_alg = SSL_HASH_SHA1;
4408
4409#if defined(POLARSSL_DHM_C)
4410 dhm_init( &handshake->dhm_ctx );
4411#endif
4412#if defined(POLARSSL_ECDH_C)
4413 ecdh_init( &handshake->ecdh_ctx );
4414#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004415}
4416
4417static void ssl_transform_init( ssl_transform *transform )
4418{
4419 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02004420
4421 cipher_init( &transform->cipher_ctx_enc );
4422 cipher_init( &transform->cipher_ctx_dec );
4423
4424 md_init( &transform->md_ctx_enc );
4425 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004426}
4427
4428void ssl_session_init( ssl_session *session )
4429{
4430 memset( session, 0, sizeof(ssl_session) );
4431}
4432
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02004433static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004434{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004435 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00004436 if( ssl->transform_negotiate )
4437 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004438 if( ssl->session_negotiate )
4439 ssl_session_free( ssl->session_negotiate );
4440 if( ssl->handshake )
4441 ssl_handshake_free( ssl->handshake );
4442
4443 /*
4444 * Either the pointers are now NULL or cleared properly and can be freed.
4445 * Now allocate missing structures.
4446 */
4447 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004448 {
4449 ssl->transform_negotiate =
4450 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
4451 }
Paul Bakker48916f92012-09-16 19:57:18 +00004452
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004453 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004454 {
4455 ssl->session_negotiate =
4456 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
4457 }
Paul Bakker48916f92012-09-16 19:57:18 +00004458
Paul Bakker82788fb2014-10-20 13:59:19 +02004459 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004460 {
4461 ssl->handshake = (ssl_handshake_params *)
4462 polarssl_malloc( sizeof(ssl_handshake_params) );
4463 }
Paul Bakker48916f92012-09-16 19:57:18 +00004464
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004465 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00004466 if( ssl->handshake == NULL ||
4467 ssl->transform_negotiate == NULL ||
4468 ssl->session_negotiate == NULL )
4469 {
4470 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004471
4472 polarssl_free( ssl->handshake );
4473 polarssl_free( ssl->transform_negotiate );
4474 polarssl_free( ssl->session_negotiate );
4475
4476 ssl->handshake = NULL;
4477 ssl->transform_negotiate = NULL;
4478 ssl->session_negotiate = NULL;
4479
Paul Bakker48916f92012-09-16 19:57:18 +00004480 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4481 }
4482
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004483 /* Initialize structures */
4484 ssl_session_init( ssl->session_negotiate );
4485 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02004486 ssl_handshake_params_init( ssl->handshake );
4487
4488#if defined(POLARSSL_X509_CRT_PARSE_C)
4489 ssl->handshake->key_cert = ssl->key_cert;
4490#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01004491
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004492#if defined(POLARSSL_SSL_PROTO_DTLS)
4493 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4494 {
4495 ssl->handshake->alt_transform_out = ssl->transform_out;
4496
4497 if( ssl->endpoint == SSL_IS_CLIENT )
4498 ssl->handshake->retransmit_state = SSL_RETRANS_PREPARING;
4499 else
4500 ssl->handshake->retransmit_state = SSL_RETRANS_WAITING;
4501 }
4502#endif
4503
Paul Bakker48916f92012-09-16 19:57:18 +00004504 return( 0 );
4505}
4506
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004507#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4508/* Dummy cookie callbacks for defaults */
4509static int ssl_cookie_write_dummy( void *ctx,
4510 unsigned char **p, unsigned char *end,
4511 const unsigned char *cli_id, size_t cli_id_len )
4512{
4513 ((void) ctx);
4514 ((void) p);
4515 ((void) end);
4516 ((void) cli_id);
4517 ((void) cli_id_len);
4518
4519 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4520}
4521
4522static int ssl_cookie_check_dummy( void *ctx,
4523 const unsigned char *cookie, size_t cookie_len,
4524 const unsigned char *cli_id, size_t cli_id_len )
4525{
4526 ((void) ctx);
4527 ((void) cookie);
4528 ((void) cookie_len);
4529 ((void) cli_id);
4530 ((void) cli_id_len);
4531
4532 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
4533}
4534#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
4535
Paul Bakker5121ce52009-01-03 21:22:43 +00004536/*
4537 * Initialize an SSL context
4538 */
4539int ssl_init( ssl_context *ssl )
4540{
Paul Bakker48916f92012-09-16 19:57:18 +00004541 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004542 int len = SSL_BUFFER_LEN;
4543
4544 memset( ssl, 0, sizeof( ssl_context ) );
4545
Paul Bakker62f2dee2012-09-28 07:31:51 +00004546 /*
4547 * Sane defaults
4548 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004549 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
4550 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
4551 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
4552 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00004553
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004554 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00004555
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004556 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
4557
Paul Bakker62f2dee2012-09-28 07:31:51 +00004558#if defined(POLARSSL_DHM_C)
4559 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
4560 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
4561 ( ret = mpi_read_string( &ssl->dhm_G, 16,
4562 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
4563 {
4564 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4565 return( ret );
4566 }
4567#endif
4568
4569 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004570 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00004571 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004572 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004573 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004574
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004575 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004576 {
4577 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004578 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004579 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004580 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004581 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00004582 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00004583 }
4584
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004585 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
4586 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00004587
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004588 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
4589 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
4590
Paul Bakker606b4ba2013-08-14 16:52:14 +02004591#if defined(POLARSSL_SSL_SESSION_TICKETS)
4592 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
4593#endif
4594
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004595#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01004596 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01004597#endif
4598
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02004599#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
4600 ssl->f_cookie_write = ssl_cookie_write_dummy;
4601 ssl->f_cookie_check = ssl_cookie_check_dummy;
4602#endif
4603
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004604#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4605 ssl->anti_replay = SSL_ANTI_REPLAY_ENABLED;
4606#endif
4607
Paul Bakker48916f92012-09-16 19:57:18 +00004608 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4609 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004610
4611 return( 0 );
4612}
4613
4614/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00004615 * Reset an initialized and used SSL context for re-use while retaining
4616 * all application-set variables, function pointers and data.
4617 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004618int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00004619{
Paul Bakker48916f92012-09-16 19:57:18 +00004620 int ret;
4621
Paul Bakker7eb013f2011-10-06 12:37:39 +00004622 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00004623 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
4624 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
4625
4626 ssl->verify_data_len = 0;
4627 memset( ssl->own_verify_data, 0, 36 );
4628 memset( ssl->peer_verify_data, 0, 36 );
4629
Paul Bakker7eb013f2011-10-06 12:37:39 +00004630 ssl->in_offt = NULL;
4631
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004632 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004633 ssl->in_msgtype = 0;
4634 ssl->in_msglen = 0;
4635 ssl->in_left = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004636#if defined(POLARSSL_SSL_PROTO_DTLS)
4637 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02004638 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02004639#endif
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02004640#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4641 ssl_dtls_replay_reset( ssl );
4642#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00004643
4644 ssl->in_hslen = 0;
4645 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004646 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004647
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004648 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004649 ssl->out_msgtype = 0;
4650 ssl->out_msglen = 0;
4651 ssl->out_left = 0;
4652
Paul Bakker48916f92012-09-16 19:57:18 +00004653 ssl->transform_in = NULL;
4654 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00004655
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004656 ssl->renego_records_seen = 0;
4657
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004658 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
4659 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00004660
4661#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02004662 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004663 {
4664 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01004665 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004666 {
4667 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
4668 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
4669 }
Paul Bakker05ef8352012-05-08 09:17:57 +00004670 }
4671#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00004672
Paul Bakker48916f92012-09-16 19:57:18 +00004673 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004674 {
Paul Bakker48916f92012-09-16 19:57:18 +00004675 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004676 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004677 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00004678 }
Paul Bakker48916f92012-09-16 19:57:18 +00004679
Paul Bakkerc0463502013-02-14 11:19:38 +01004680 if( ssl->session )
4681 {
4682 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004683 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004684 ssl->session = NULL;
4685 }
4686
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004687#if defined(POLARSSL_SSL_ALPN)
4688 ssl->alpn_chosen = NULL;
4689#endif
4690
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02004691#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004692 polarssl_free( ssl->cli_id );
4693 ssl->cli_id = NULL;
4694 ssl->cli_id_len = 0;
4695#endif
4696
Paul Bakker48916f92012-09-16 19:57:18 +00004697 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4698 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004699
4700 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00004701}
4702
Paul Bakkera503a632013-08-14 13:48:06 +02004703#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004704static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
4705{
4706 aes_free( &tkeys->enc );
4707 aes_free( &tkeys->dec );
4708
4709 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
4710}
4711
Paul Bakker7eb013f2011-10-06 12:37:39 +00004712/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004713 * Allocate and initialize ticket keys
4714 */
4715static int ssl_ticket_keys_init( ssl_context *ssl )
4716{
4717 int ret;
4718 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004719 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004720
4721 if( ssl->ticket_keys != NULL )
4722 return( 0 );
4723
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004724 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
4725 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004726 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4727
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004728 aes_init( &tkeys->enc );
4729 aes_init( &tkeys->dec );
4730
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004731 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004732 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004733 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004734 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004735 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004736 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004737
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004738 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
4739 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
4740 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
4741 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004742 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004743 polarssl_free( tkeys );
4744 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02004745 }
4746
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004747 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01004748 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004749 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004750 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004751 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01004752 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02004753
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004754 ssl->ticket_keys = tkeys;
4755
4756 return( 0 );
4757}
Paul Bakkera503a632013-08-14 13:48:06 +02004758#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004759
4760/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 * SSL set accessors
4762 */
4763void ssl_set_endpoint( ssl_context *ssl, int endpoint )
4764{
4765 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004766
Paul Bakker606b4ba2013-08-14 16:52:14 +02004767#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004768 if( endpoint == SSL_IS_CLIENT )
4769 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02004770#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004771}
4772
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004773int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004774{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004775#if defined(POLARSSL_SSL_PROTO_DTLS)
4776 if( transport == SSL_TRANSPORT_DATAGRAM )
4777 {
4778 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004779
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004780 ssl->out_hdr = ssl->out_buf;
4781 ssl->out_ctr = ssl->out_buf + 3;
4782 ssl->out_len = ssl->out_buf + 11;
4783 ssl->out_iv = ssl->out_buf + 13;
4784 ssl->out_msg = ssl->out_buf + 13;
4785
4786 ssl->in_hdr = ssl->in_buf;
4787 ssl->in_ctr = ssl->in_buf + 3;
4788 ssl->in_len = ssl->in_buf + 11;
4789 ssl->in_iv = ssl->in_buf + 13;
4790 ssl->in_msg = ssl->in_buf + 13;
4791
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004792 /* DTLS starts with TLS1.1 */
4793 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
4794 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004795
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004796 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
4797 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
4798
4799 return( 0 );
4800 }
4801#endif
4802
4803 if( transport == SSL_TRANSPORT_STREAM )
4804 {
4805 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01004806
4807 ssl->out_ctr = ssl->out_buf;
4808 ssl->out_hdr = ssl->out_buf + 8;
4809 ssl->out_len = ssl->out_buf + 11;
4810 ssl->out_iv = ssl->out_buf + 13;
4811 ssl->out_msg = ssl->out_buf + 13;
4812
4813 ssl->in_ctr = ssl->in_buf;
4814 ssl->in_hdr = ssl->in_buf + 8;
4815 ssl->in_len = ssl->in_buf + 11;
4816 ssl->in_iv = ssl->in_buf + 13;
4817 ssl->in_msg = ssl->in_buf + 13;
4818
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004819 return( 0 );
4820 }
4821
4822 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01004823}
4824
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004825#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
4826void ssl_set_dtls_anti_replay( ssl_context *ssl, char mode )
4827{
4828 ssl->anti_replay = mode;
4829}
4830#endif
4831
Paul Bakker5121ce52009-01-03 21:22:43 +00004832void ssl_set_authmode( ssl_context *ssl, int authmode )
4833{
4834 ssl->authmode = authmode;
4835}
4836
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004837#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004838void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004839 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004840 void *p_vrfy )
4841{
4842 ssl->f_vrfy = f_vrfy;
4843 ssl->p_vrfy = p_vrfy;
4844}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004845#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00004846
Paul Bakker5121ce52009-01-03 21:22:43 +00004847void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00004848 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 void *p_rng )
4850{
4851 ssl->f_rng = f_rng;
4852 ssl->p_rng = p_rng;
4853}
4854
4855void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00004856 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00004857 void *p_dbg )
4858{
4859 ssl->f_dbg = f_dbg;
4860 ssl->p_dbg = p_dbg;
4861}
4862
4863void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00004864 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00004865 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00004866{
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004867 if( p_recv != p_send )
4868 {
4869 ssl->f_recv = NULL;
4870 ssl->f_send = NULL;
4871 ssl->p_bio = NULL;
4872 return;
4873 }
4874
Paul Bakker5121ce52009-01-03 21:22:43 +00004875 ssl->f_recv = f_recv;
4876 ssl->f_send = f_send;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02004877 ssl->p_bio = p_send;
Paul Bakker5121ce52009-01-03 21:22:43 +00004878}
4879
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02004880void ssl_set_bio_timeout( ssl_context *ssl,
4881 void *p_bio,
4882 int (*f_send)(void *, const unsigned char *, size_t),
4883 int (*f_recv)(void *, unsigned char *, size_t),
4884 int (*f_recv_timeout)(void *, unsigned char *, size_t, unsigned char),
4885 unsigned char timeout )
4886{
4887 ssl->p_bio = p_bio;
4888 ssl->f_send = f_send;
4889 ssl->f_recv = f_recv;
4890 ssl->f_recv_timeout = f_recv_timeout;
4891 ssl->timeout = timeout;
4892}
4893
Paul Bakker0a597072012-09-25 21:55:46 +00004894void ssl_set_session_cache( ssl_context *ssl,
4895 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
4896 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00004897{
Paul Bakker0a597072012-09-25 21:55:46 +00004898 ssl->f_get_cache = f_get_cache;
4899 ssl->p_get_cache = p_get_cache;
4900 ssl->f_set_cache = f_set_cache;
4901 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00004902}
4903
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004904int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00004905{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004906 int ret;
4907
4908 if( ssl == NULL ||
4909 session == NULL ||
4910 ssl->session_negotiate == NULL ||
4911 ssl->endpoint != SSL_IS_CLIENT )
4912 {
4913 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4914 }
4915
4916 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
4917 return( ret );
4918
Paul Bakker0a597072012-09-25 21:55:46 +00004919 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004920
4921 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004922}
4923
Paul Bakkerb68cad62012-08-23 08:34:18 +00004924void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00004925{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004926 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
4927 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
4928 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
4929 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
4930}
4931
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004932void ssl_set_ciphersuites_for_version( ssl_context *ssl,
4933 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02004934 int major, int minor )
4935{
4936 if( major != SSL_MAJOR_VERSION_3 )
4937 return;
4938
4939 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
4940 return;
4941
4942 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00004943}
4944
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004945#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004946/* Add a new (empty) key_cert entry an return a pointer to it */
4947static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
4948{
4949 ssl_key_cert *key_cert, *last;
4950
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004951 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
4952 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004953 return( NULL );
4954
4955 memset( key_cert, 0, sizeof( ssl_key_cert ) );
4956
4957 /* Append the new key_cert to the (possibly empty) current list */
4958 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01004959 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004960 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01004961 if( ssl->handshake != NULL )
4962 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01004963 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004964 else
4965 {
4966 last = ssl->key_cert;
4967 while( last->next != NULL )
4968 last = last->next;
4969 last->next = key_cert;
4970 }
4971
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004972 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004973}
4974
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004975void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00004976 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00004977{
4978 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00004979 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00004980 ssl->peer_cn = peer_cn;
4981}
4982
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004983int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004984 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00004985{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004986 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
4987
4988 if( key_cert == NULL )
4989 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4990
4991 key_cert->cert = own_cert;
4992 key_cert->key = pk_key;
4993
4994 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004995}
4996
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004997#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004998int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004999 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00005000{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005001 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005002 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005003
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005004 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005005 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5006
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005007 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5008 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005009 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005010
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005011 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005012
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005013 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005014 if( ret != 0 )
5015 return( ret );
5016
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005017 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005018 return( ret );
5019
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005020 key_cert->cert = own_cert;
5021 key_cert->key_own_alloc = 1;
5022
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005023 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02005025#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005026
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005027int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02005028 void *rsa_key,
5029 rsa_decrypt_func rsa_decrypt,
5030 rsa_sign_func rsa_sign,
5031 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00005032{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005033 int ret;
5034 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005035
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005036 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005037 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5038
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005039 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
5040 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005041 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005042
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005043 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005044
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005045 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
5046 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
5047 return( ret );
5048
5049 key_cert->cert = own_cert;
5050 key_cert->key_own_alloc = 1;
5051
5052 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00005053}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005054#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00005055
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005056#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005057int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
5058 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005059{
Paul Bakker6db455e2013-09-18 17:29:31 +02005060 if( psk == NULL || psk_identity == NULL )
5061 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5062
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02005063 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01005064 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5065
Paul Bakker6db455e2013-09-18 17:29:31 +02005066 if( ssl->psk != NULL )
5067 {
5068 polarssl_free( ssl->psk );
5069 polarssl_free( ssl->psk_identity );
5070 }
5071
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005072 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005073 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02005074
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02005075 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005076 ssl->psk_identity = (unsigned char *)
5077 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005078
5079 if( ssl->psk == NULL || ssl->psk_identity == NULL )
5080 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5081
5082 memcpy( ssl->psk, psk, ssl->psk_len );
5083 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02005084
5085 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02005086}
5087
5088void ssl_set_psk_cb( ssl_context *ssl,
5089 int (*f_psk)(void *, ssl_context *, const unsigned char *,
5090 size_t),
5091 void *p_psk )
5092{
5093 ssl->f_psk = f_psk;
5094 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02005095}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005096#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00005097
Paul Bakker48916f92012-09-16 19:57:18 +00005098#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005099int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00005100{
5101 int ret;
5102
Paul Bakker48916f92012-09-16 19:57:18 +00005103 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 {
5105 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5106 return( ret );
5107 }
5108
Paul Bakker48916f92012-09-16 19:57:18 +00005109 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 {
5111 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
5112 return( ret );
5113 }
5114
5115 return( 0 );
5116}
5117
Paul Bakker1b57b062011-01-06 15:48:19 +00005118int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
5119{
5120 int ret;
5121
Paul Bakker66d5d072014-06-17 16:39:18 +02005122 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005123 {
5124 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5125 return( ret );
5126 }
5127
Paul Bakker66d5d072014-06-17 16:39:18 +02005128 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00005129 {
5130 SSL_DEBUG_RET( 1, "mpi_copy", ret );
5131 return( ret );
5132 }
5133
5134 return( 0 );
5135}
Paul Bakker48916f92012-09-16 19:57:18 +00005136#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00005137
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01005138#if defined(POLARSSL_SSL_SET_CURVES)
5139/*
5140 * Set the allowed elliptic curves
5141 */
5142void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
5143{
5144 ssl->curve_list = curve_list;
5145}
5146#endif
5147
Paul Bakker0be444a2013-08-27 21:55:01 +02005148#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00005149int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00005150{
5151 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00005152 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00005153
5154 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02005155
5156 if( ssl->hostname_len + 1 == 0 )
5157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5158
Paul Bakker6e339b52013-07-03 13:37:05 +02005159 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160
Paul Bakkerb15b8512012-01-13 13:44:06 +00005161 if( ssl->hostname == NULL )
5162 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
5163
Paul Bakker3c2122f2013-06-24 19:03:14 +02005164 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00005165 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02005166
Paul Bakker40ea7de2009-05-03 10:18:48 +00005167 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00005168
5169 return( 0 );
5170}
5171
Paul Bakker5701cdc2012-09-27 21:49:42 +00005172void ssl_set_sni( ssl_context *ssl,
5173 int (*f_sni)(void *, ssl_context *,
5174 const unsigned char *, size_t),
5175 void *p_sni )
5176{
5177 ssl->f_sni = f_sni;
5178 ssl->p_sni = p_sni;
5179}
Paul Bakker0be444a2013-08-27 21:55:01 +02005180#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00005181
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005182#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005183int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005184{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005185 size_t cur_len, tot_len;
5186 const char **p;
5187
5188 /*
5189 * "Empty strings MUST NOT be included and byte strings MUST NOT be
5190 * truncated". Check lengths now rather than later.
5191 */
5192 tot_len = 0;
5193 for( p = protos; *p != NULL; p++ )
5194 {
5195 cur_len = strlen( *p );
5196 tot_len += cur_len;
5197
5198 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
5199 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5200 }
5201
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005202 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02005203
5204 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005205}
5206
5207const char *ssl_get_alpn_protocol( const ssl_context *ssl )
5208{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005209 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02005210}
5211#endif /* POLARSSL_SSL_ALPN */
5212
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005213static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00005214{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005215 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
5216 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
5217 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5218 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005219 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005220 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005221 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005222
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005223 return( 0 );
5224}
5225
5226int ssl_set_max_version( ssl_context *ssl, int major, int minor )
5227{
5228 if( ssl_check_version( ssl, major, minor ) != 0 )
5229 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5230
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005231 ssl->max_major_ver = major;
5232 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005233
5234 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00005235}
5236
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005237int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00005238{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005239 if( ssl_check_version( ssl, major, minor ) != 0 )
5240 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005241
5242 ssl->min_major_ver = major;
5243 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01005244
5245 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00005246}
5247
Paul Bakker05decb22013-08-15 13:33:48 +02005248#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005249int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
5250{
Paul Bakker77e257e2013-12-16 15:29:52 +01005251 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005252 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005253 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005254 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005255 }
5256
5257 ssl->mfl_code = mfl_code;
5258
5259 return( 0 );
5260}
Paul Bakker05decb22013-08-15 13:33:48 +02005261#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02005262
Paul Bakker1f2bc622013-08-15 13:45:55 +02005263#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02005264int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005265{
5266 if( ssl->endpoint != SSL_IS_CLIENT )
5267 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5268
Paul Bakker8c1ede62013-07-19 14:14:37 +02005269 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005270
5271 return( 0 );
5272}
Paul Bakker1f2bc622013-08-15 13:45:55 +02005273#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02005274
Paul Bakker48916f92012-09-16 19:57:18 +00005275void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
5276{
5277 ssl->disable_renegotiation = renegotiation;
5278}
5279
5280void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
5281{
5282 ssl->allow_legacy_renegotiation = allow_legacy;
5283}
5284
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005285void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
5286{
5287 ssl->renego_max_records = max_records;
5288}
5289
Paul Bakkera503a632013-08-14 13:48:06 +02005290#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005291int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
5292{
5293 ssl->session_tickets = use_tickets;
5294
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005295 if( ssl->endpoint == SSL_IS_CLIENT )
5296 return( 0 );
5297
5298 if( ssl->f_rng == NULL )
5299 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5300
5301 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005302}
Paul Bakker606b4ba2013-08-14 16:52:14 +02005303
5304void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
5305{
5306 ssl->ticket_lifetime = lifetime;
5307}
Paul Bakkera503a632013-08-14 13:48:06 +02005308#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02005309
Paul Bakker5121ce52009-01-03 21:22:43 +00005310/*
5311 * SSL get accessors
5312 */
Paul Bakker23986e52011-04-24 08:57:21 +00005313size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005314{
5315 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
5316}
5317
Paul Bakkerff60ee62010-03-16 21:09:09 +00005318int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005319{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02005320 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00005321}
5322
Paul Bakkere3166ce2011-01-27 17:40:50 +00005323const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00005324{
Paul Bakker926c8e42013-03-06 10:23:34 +01005325 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005326 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01005327
Paul Bakkere3166ce2011-01-27 17:40:50 +00005328 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00005329}
5330
Paul Bakker43ca69c2011-01-15 17:35:19 +00005331const char *ssl_get_version( const ssl_context *ssl )
5332{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005333#if defined(POLARSSL_SSL_PROTO_DTLS)
5334 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5335 {
5336 switch( ssl->minor_ver )
5337 {
5338 case SSL_MINOR_VERSION_2:
5339 return( "DTLSv1.0" );
5340
5341 case SSL_MINOR_VERSION_3:
5342 return( "DTLSv1.2" );
5343
5344 default:
5345 return( "unknown (DTLS)" );
5346 }
5347 }
5348#endif
5349
Paul Bakker43ca69c2011-01-15 17:35:19 +00005350 switch( ssl->minor_ver )
5351 {
5352 case SSL_MINOR_VERSION_0:
5353 return( "SSLv3.0" );
5354
5355 case SSL_MINOR_VERSION_1:
5356 return( "TLSv1.0" );
5357
5358 case SSL_MINOR_VERSION_2:
5359 return( "TLSv1.1" );
5360
Paul Bakker1ef83d62012-04-11 12:09:53 +00005361 case SSL_MINOR_VERSION_3:
5362 return( "TLSv1.2" );
5363
Paul Bakker43ca69c2011-01-15 17:35:19 +00005364 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01005365 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00005366 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00005367}
5368
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005369#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02005370const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00005371{
5372 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005373 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005374
Paul Bakkerd8bb8262014-06-17 14:06:49 +02005375 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00005376}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005377#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00005378
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005379int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
5380{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005381 if( ssl == NULL ||
5382 dst == NULL ||
5383 ssl->session == NULL ||
5384 ssl->endpoint != SSL_IS_CLIENT )
5385 {
5386 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5387 }
5388
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02005389 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02005390}
5391
Paul Bakker5121ce52009-01-03 21:22:43 +00005392/*
Paul Bakker1961b702013-01-25 14:49:24 +01005393 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00005394 */
Paul Bakker1961b702013-01-25 14:49:24 +01005395int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00005396{
Paul Bakker40e46942009-01-03 21:51:57 +00005397 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00005398
Paul Bakker40e46942009-01-03 21:51:57 +00005399#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005400 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01005401 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005402#endif
5403
Paul Bakker40e46942009-01-03 21:51:57 +00005404#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005405 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01005406 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00005407#endif
5408
Paul Bakker1961b702013-01-25 14:49:24 +01005409 return( ret );
5410}
5411
5412/*
5413 * Perform the SSL handshake
5414 */
5415int ssl_handshake( ssl_context *ssl )
5416{
5417 int ret = 0;
5418
5419 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
5420
5421 while( ssl->state != SSL_HANDSHAKE_OVER )
5422 {
5423 ret = ssl_handshake_step( ssl );
5424
5425 if( ret != 0 )
5426 break;
5427 }
5428
Paul Bakker5121ce52009-01-03 21:22:43 +00005429 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
5430
5431 return( ret );
5432}
5433
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005434#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00005435/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005436 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00005437 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005438static int ssl_write_hello_request( ssl_context *ssl )
5439{
5440 int ret;
5441
5442 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
5443
5444 ssl->out_msglen = 4;
5445 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
5446 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
5447
5448 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5449 {
5450 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5451 return( ret );
5452 }
5453
5454 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
5455
5456 return( 0 );
5457}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005458#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005459
5460/*
5461 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02005462 * - any side: calling ssl_renegotiate(),
5463 * - client: receiving a HelloRequest during ssl_read(),
5464 * - server: receiving any handshake message on server during ssl_read() after
5465 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005466 * If the handshake doesn't complete due to waiting for I/O, it will continue
5467 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005468 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005469static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00005470{
5471 int ret;
5472
5473 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
5474
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005475 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
5476 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005477
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005478 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
5479 * the ServerHello will have message_seq = 1" */
5480#if defined(POLARSSL_SSL_PROTO_DTLS)
5481 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005482 ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5483 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005484 if( ssl->endpoint == SSL_IS_SERVER )
5485 ssl->handshake->out_msg_seq = 1;
5486 else
5487 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02005488 }
5489#endif
5490
Paul Bakker48916f92012-09-16 19:57:18 +00005491 ssl->state = SSL_HELLO_REQUEST;
5492 ssl->renegotiation = SSL_RENEGOTIATION;
5493
Paul Bakker48916f92012-09-16 19:57:18 +00005494 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5495 {
5496 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5497 return( ret );
5498 }
5499
5500 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
5501
5502 return( 0 );
5503}
5504
5505/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005506 * Renegotiate current connection on client,
5507 * or request renegotiation on server
5508 */
5509int ssl_renegotiate( ssl_context *ssl )
5510{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005511 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005512
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005513#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005514 /* On server, just send the request */
5515 if( ssl->endpoint == SSL_IS_SERVER )
5516 {
5517 if( ssl->state != SSL_HANDSHAKE_OVER )
5518 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5519
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02005520 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5521
5522 /* Did we already try/start sending HelloRequest? */
5523 if( ssl->out_left != 0 )
5524 return( ssl_flush_output( ssl ) );
5525
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005526 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005527 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005528#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005529
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005530#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005531 /*
5532 * On client, either start the renegotiation process or,
5533 * if already in progress, continue the handshake
5534 */
5535 if( ssl->renegotiation != SSL_RENEGOTIATION )
5536 {
5537 if( ssl->state != SSL_HANDSHAKE_OVER )
5538 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
5539
5540 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
5541 {
5542 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
5543 return( ret );
5544 }
5545 }
5546 else
5547 {
5548 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5549 {
5550 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5551 return( ret );
5552 }
5553 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005554#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005555
Paul Bakker37ce0ff2013-10-31 14:32:04 +01005556 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01005557}
5558
5559/*
Paul Bakker5121ce52009-01-03 21:22:43 +00005560 * Receive application data decrypted from the SSL layer
5561 */
Paul Bakker23986e52011-04-24 08:57:21 +00005562int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005563{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005564 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00005565 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00005566
5567 SSL_DEBUG_MSG( 2, ( "=> read" ) );
5568
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005569#if defined(POLARSSL_SSL_PROTO_DTLS)
5570 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5571 ssl->handshake != NULL &&
5572 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
5573 {
5574 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5575 return( ret );
5576
5577 if( ( ret = ssl_resend( ssl ) ) != 0 )
5578 return( ret );
5579 }
5580#endif
5581
Paul Bakker5121ce52009-01-03 21:22:43 +00005582 if( ssl->state != SSL_HANDSHAKE_OVER )
5583 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005584 ret = ssl_handshake( ssl );
5585 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5586 {
5587 record_read = 1;
5588 }
5589 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005590 {
5591 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5592 return( ret );
5593 }
5594 }
5595
5596 if( ssl->in_offt == NULL )
5597 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005598 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00005599 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005600 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5601 {
5602 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5603 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00005604
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005605 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5606 return( ret );
5607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005608 }
5609
5610 if( ssl->in_msglen == 0 &&
5611 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
5612 {
5613 /*
5614 * OpenSSL sends empty messages to randomize the IV
5615 */
5616 if( ( ret = ssl_read_record( ssl ) ) != 0 )
5617 {
Paul Bakker831a7552011-05-18 13:32:51 +00005618 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
5619 return( 0 );
5620
Paul Bakker5121ce52009-01-03 21:22:43 +00005621 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
5622 return( ret );
5623 }
5624 }
5625
Paul Bakker48916f92012-09-16 19:57:18 +00005626 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
5627 {
5628 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
5629
5630 if( ssl->endpoint == SSL_IS_CLIENT &&
5631 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00005632 ssl->in_hslen != ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005633 {
5634 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005635
5636 /* With DTLS, drop the packet (probably from last handshake) */
5637#if defined(POLARSSL_SSL_PROTO_DTLS)
5638 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5639 return( POLARSSL_ERR_NET_WANT_READ );
5640#endif
5641 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5642 }
5643
5644 if( ssl->endpoint == SSL_IS_SERVER &&
5645 ssl->in_msg[0] != SSL_HS_CLIENT_HELLO )
5646 {
5647 SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
5648
5649 /* With DTLS, drop the packet (probably from last handshake) */
5650#if defined(POLARSSL_SSL_PROTO_DTLS)
5651 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
5652 return( POLARSSL_ERR_NET_WANT_READ );
5653#endif
Paul Bakker48916f92012-09-16 19:57:18 +00005654 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5655 }
5656
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005657 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
5658 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02005659 ssl->allow_legacy_renegotiation ==
5660 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00005661 {
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02005662 SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005663
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005664#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005665 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005666 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005667 /*
5668 * SSLv3 does not have a "no_renegotiation" alert
5669 */
5670 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
5671 return( ret );
5672 }
5673 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005674#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005675#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
5676 defined(POLARSSL_SSL_PROTO_TLS1_2)
5677 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00005678 {
5679 if( ( ret = ssl_send_alert_message( ssl,
5680 SSL_ALERT_LEVEL_WARNING,
5681 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
5682 {
5683 return( ret );
5684 }
Paul Bakker48916f92012-09-16 19:57:18 +00005685 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02005686 else
Paul Bakker9af723c2014-05-01 13:03:14 +02005687#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
5688 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02005689 {
5690 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02005691 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02005692 }
Paul Bakker48916f92012-09-16 19:57:18 +00005693 }
5694 else
5695 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02005696#if defined(POLARSSL_SSL_PROTO_DTLS)
5697 /* DTLS clients need to know renego is server-initiated */
5698 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
5699 ssl->endpoint == SSL_IS_CLIENT )
5700 {
5701 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
5702 }
5703#endif
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005704 ret = ssl_start_renegotiation( ssl );
5705 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
5706 {
5707 record_read = 1;
5708 }
5709 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00005710 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01005711 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00005712 return( ret );
5713 }
Paul Bakker48916f92012-09-16 19:57:18 +00005714 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005715
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02005716 /* If a non-handshake record was read during renego, fallthrough,
5717 * else tell the user they should call ssl_read() again */
5718 if( ! record_read )
5719 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00005720 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005721 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
5722 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02005723 ssl->renego_records_seen++;
5724
5725 if( ssl->renego_max_records >= 0 &&
5726 ssl->renego_records_seen > ssl->renego_max_records )
5727 {
5728 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
5729 "but not honored by client" ) );
5730 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
5731 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01005732 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02005733
5734 /* Fatal and closure alerts handled by ssl_read_record() */
5735 if( ssl->in_msgtype == SSL_MSG_ALERT )
5736 {
5737 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
5738 return( POLARSSL_ERR_NET_WANT_READ );
5739 }
5740
5741 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00005742 {
5743 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00005744 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00005745 }
5746
5747 ssl->in_offt = ssl->in_msg;
5748 }
5749
5750 n = ( len < ssl->in_msglen )
5751 ? len : ssl->in_msglen;
5752
5753 memcpy( buf, ssl->in_offt, n );
5754 ssl->in_msglen -= n;
5755
5756 if( ssl->in_msglen == 0 )
5757 /* all bytes consumed */
5758 ssl->in_offt = NULL;
5759 else
5760 /* more data available */
5761 ssl->in_offt += n;
5762
5763 SSL_DEBUG_MSG( 2, ( "<= read" ) );
5764
Paul Bakker23986e52011-04-24 08:57:21 +00005765 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005766}
5767
5768/*
5769 * Send application data to be encrypted by the SSL layer
5770 */
Paul Bakker23986e52011-04-24 08:57:21 +00005771int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00005772{
Paul Bakker23986e52011-04-24 08:57:21 +00005773 int ret;
5774 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02005775 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00005776
5777 SSL_DEBUG_MSG( 2, ( "=> write" ) );
5778
5779 if( ssl->state != SSL_HANDSHAKE_OVER )
5780 {
5781 if( ( ret = ssl_handshake( ssl ) ) != 0 )
5782 {
5783 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
5784 return( ret );
5785 }
5786 }
5787
Paul Bakker05decb22013-08-15 13:33:48 +02005788#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005789 /*
5790 * Assume mfl_code is correct since it was checked when set
5791 */
5792 max_len = mfl_code_to_length[ssl->mfl_code];
5793
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005794 /*
Paul Bakker05decb22013-08-15 13:33:48 +02005795 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005796 */
5797 if( ssl->session_out != NULL &&
5798 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
5799 {
5800 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
5801 }
Paul Bakker05decb22013-08-15 13:33:48 +02005802#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02005803
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02005804 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00005805
Paul Bakker5121ce52009-01-03 21:22:43 +00005806 if( ssl->out_left != 0 )
5807 {
5808 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
5809 {
5810 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
5811 return( ret );
5812 }
5813 }
Paul Bakker887bd502011-06-08 13:10:54 +00005814 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00005815 {
Paul Bakker887bd502011-06-08 13:10:54 +00005816 ssl->out_msglen = n;
5817 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
5818 memcpy( ssl->out_msg, buf, n );
5819
5820 if( ( ret = ssl_write_record( ssl ) ) != 0 )
5821 {
5822 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
5823 return( ret );
5824 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005825 }
5826
5827 SSL_DEBUG_MSG( 2, ( "<= write" ) );
5828
Paul Bakker23986e52011-04-24 08:57:21 +00005829 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00005830}
5831
5832/*
5833 * Notify the peer that the connection is being closed
5834 */
5835int ssl_close_notify( ssl_context *ssl )
5836{
5837 int ret;
5838
5839 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
5840
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005841 if( ssl->out_left != 0 )
5842 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005843
5844 if( ssl->state == SSL_HANDSHAKE_OVER )
5845 {
Paul Bakker48916f92012-09-16 19:57:18 +00005846 if( ( ret = ssl_send_alert_message( ssl,
5847 SSL_ALERT_LEVEL_WARNING,
5848 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005849 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005850 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005851 return( ret );
5852 }
5853 }
5854
5855 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
5856
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02005857 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00005858}
5859
Paul Bakker48916f92012-09-16 19:57:18 +00005860void ssl_transform_free( ssl_transform *transform )
5861{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005862 if( transform == NULL )
5863 return;
5864
Paul Bakker48916f92012-09-16 19:57:18 +00005865#if defined(POLARSSL_ZLIB_SUPPORT)
5866 deflateEnd( &transform->ctx_deflate );
5867 inflateEnd( &transform->ctx_inflate );
5868#endif
5869
Paul Bakker84bbeb52014-07-01 14:53:22 +02005870 cipher_free( &transform->cipher_ctx_enc );
5871 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02005872
Paul Bakker84bbeb52014-07-01 14:53:22 +02005873 md_free( &transform->md_ctx_enc );
5874 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02005875
Paul Bakker34617722014-06-13 17:20:13 +02005876 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005877}
5878
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005879#if defined(POLARSSL_X509_CRT_PARSE_C)
5880static void ssl_key_cert_free( ssl_key_cert *key_cert )
5881{
5882 ssl_key_cert *cur = key_cert, *next;
5883
5884 while( cur != NULL )
5885 {
5886 next = cur->next;
5887
5888 if( cur->key_own_alloc )
5889 {
5890 pk_free( cur->key );
5891 polarssl_free( cur->key );
5892 }
5893 polarssl_free( cur );
5894
5895 cur = next;
5896 }
5897}
5898#endif /* POLARSSL_X509_CRT_PARSE_C */
5899
Paul Bakker48916f92012-09-16 19:57:18 +00005900void ssl_handshake_free( ssl_handshake_params *handshake )
5901{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005902 if( handshake == NULL )
5903 return;
5904
Paul Bakker48916f92012-09-16 19:57:18 +00005905#if defined(POLARSSL_DHM_C)
5906 dhm_free( &handshake->dhm_ctx );
5907#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02005908#if defined(POLARSSL_ECDH_C)
5909 ecdh_free( &handshake->ecdh_ctx );
5910#endif
5911
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005912#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02005913 /* explicit void pointer cast for buggy MS compiler */
5914 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02005915#endif
5916
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02005917#if defined(POLARSSL_X509_CRT_PARSE_C) && \
5918 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
5919 /*
5920 * Free only the linked list wrapper, not the keys themselves
5921 * since the belong to the SNI callback
5922 */
5923 if( handshake->sni_key_cert != NULL )
5924 {
5925 ssl_key_cert *cur = handshake->sni_key_cert, *next;
5926
5927 while( cur != NULL )
5928 {
5929 next = cur->next;
5930 polarssl_free( cur );
5931 cur = next;
5932 }
5933 }
Paul Bakker9af723c2014-05-01 13:03:14 +02005934#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005935
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005936#if defined(POLARSSL_SSL_PROTO_DTLS)
5937 polarssl_free( handshake->verify_cookie );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02005938 polarssl_free( handshake->hs_msg );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02005939 ssl_flight_free( handshake->flight );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02005940#endif
5941
Paul Bakker34617722014-06-13 17:20:13 +02005942 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005943}
5944
5945void ssl_session_free( ssl_session *session )
5946{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005947 if( session == NULL )
5948 return;
5949
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005950#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00005951 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00005952 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005953 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005954 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005955 }
Paul Bakkered27a042013-04-18 22:46:23 +02005956#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005957
Paul Bakkera503a632013-08-14 13:48:06 +02005958#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005959 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005960#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005961
Paul Bakker34617722014-06-13 17:20:13 +02005962 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005963}
5964
Paul Bakker5121ce52009-01-03 21:22:43 +00005965/*
5966 * Free an SSL context
5967 */
5968void ssl_free( ssl_context *ssl )
5969{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005970 if( ssl == NULL )
5971 return;
5972
Paul Bakker5121ce52009-01-03 21:22:43 +00005973 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5974
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005975 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005976 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005977 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
5978 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005979 }
5980
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005981 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005982 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01005983 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
5984 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00005985 }
5986
Paul Bakker16770332013-10-11 09:59:44 +02005987#if defined(POLARSSL_ZLIB_SUPPORT)
5988 if( ssl->compress_buf != NULL )
5989 {
Paul Bakker34617722014-06-13 17:20:13 +02005990 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005991 polarssl_free( ssl->compress_buf );
5992 }
5993#endif
5994
Paul Bakker40e46942009-01-03 21:51:57 +00005995#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005996 mpi_free( &ssl->dhm_P );
5997 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005998#endif
5999
Paul Bakker48916f92012-09-16 19:57:18 +00006000 if( ssl->transform )
6001 {
6002 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02006003 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00006004 }
6005
6006 if( ssl->handshake )
6007 {
6008 ssl_handshake_free( ssl->handshake );
6009 ssl_transform_free( ssl->transform_negotiate );
6010 ssl_session_free( ssl->session_negotiate );
6011
Paul Bakker6e339b52013-07-03 13:37:05 +02006012 polarssl_free( ssl->handshake );
6013 polarssl_free( ssl->transform_negotiate );
6014 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00006015 }
6016
Paul Bakkerc0463502013-02-14 11:19:38 +01006017 if( ssl->session )
6018 {
6019 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02006020 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01006021 }
6022
Paul Bakkera503a632013-08-14 13:48:06 +02006023#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02006024 if( ssl->ticket_keys )
6025 {
6026 ssl_ticket_keys_free( ssl->ticket_keys );
6027 polarssl_free( ssl->ticket_keys );
6028 }
Paul Bakkera503a632013-08-14 13:48:06 +02006029#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02006030
Paul Bakker0be444a2013-08-27 21:55:01 +02006031#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02006032 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006033 {
Paul Bakker34617722014-06-13 17:20:13 +02006034 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02006035 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 ssl->hostname_len = 0;
6037 }
Paul Bakker0be444a2013-08-27 21:55:01 +02006038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006039
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02006040#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02006041 if( ssl->psk != NULL )
6042 {
Paul Bakker34617722014-06-13 17:20:13 +02006043 polarssl_zeroize( ssl->psk, ssl->psk_len );
6044 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02006045 polarssl_free( ssl->psk );
6046 polarssl_free( ssl->psk_identity );
6047 ssl->psk_len = 0;
6048 ssl->psk_identity_len = 0;
6049 }
6050#endif
6051
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02006052#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02006053 ssl_key_cert_free( ssl->key_cert );
6054#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02006055
Paul Bakker05ef8352012-05-08 09:17:57 +00006056#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
6057 if( ssl_hw_record_finish != NULL )
6058 {
6059 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
6060 ssl_hw_record_finish( ssl );
6061 }
6062#endif
6063
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02006064#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02006065 polarssl_free( ssl->cli_id );
6066#endif
6067
Paul Bakker5121ce52009-01-03 21:22:43 +00006068 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00006069
Paul Bakker86f04f42013-02-14 11:20:09 +01006070 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02006071 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006072}
6073
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006074#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006075/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006076 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02006077 */
6078unsigned char ssl_sig_from_pk( pk_context *pk )
6079{
6080#if defined(POLARSSL_RSA_C)
6081 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
6082 return( SSL_SIG_RSA );
6083#endif
6084#if defined(POLARSSL_ECDSA_C)
6085 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
6086 return( SSL_SIG_ECDSA );
6087#endif
6088 return( SSL_SIG_ANON );
6089}
6090
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006091pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
6092{
6093 switch( sig )
6094 {
6095#if defined(POLARSSL_RSA_C)
6096 case SSL_SIG_RSA:
6097 return( POLARSSL_PK_RSA );
6098#endif
6099#if defined(POLARSSL_ECDSA_C)
6100 case SSL_SIG_ECDSA:
6101 return( POLARSSL_PK_ECDSA );
6102#endif
6103 default:
6104 return( POLARSSL_PK_NONE );
6105 }
6106}
Paul Bakker9af723c2014-05-01 13:03:14 +02006107#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006108
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02006109/*
6110 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
6111 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02006112md_type_t ssl_md_alg_from_hash( unsigned char hash )
6113{
6114 switch( hash )
6115 {
6116#if defined(POLARSSL_MD5_C)
6117 case SSL_HASH_MD5:
6118 return( POLARSSL_MD_MD5 );
6119#endif
6120#if defined(POLARSSL_SHA1_C)
6121 case SSL_HASH_SHA1:
6122 return( POLARSSL_MD_SHA1 );
6123#endif
6124#if defined(POLARSSL_SHA256_C)
6125 case SSL_HASH_SHA224:
6126 return( POLARSSL_MD_SHA224 );
6127 case SSL_HASH_SHA256:
6128 return( POLARSSL_MD_SHA256 );
6129#endif
6130#if defined(POLARSSL_SHA512_C)
6131 case SSL_HASH_SHA384:
6132 return( POLARSSL_MD_SHA384 );
6133 case SSL_HASH_SHA512:
6134 return( POLARSSL_MD_SHA512 );
6135#endif
6136 default:
6137 return( POLARSSL_MD_NONE );
6138 }
6139}
6140
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01006141#if defined(POLARSSL_SSL_SET_CURVES)
6142/*
6143 * Check is a curve proposed by the peer is in our list.
6144 * Return 1 if we're willing to use it, 0 otherwise.
6145 */
6146int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
6147{
6148 const ecp_group_id *gid;
6149
6150 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
6151 if( *gid == grp_id )
6152 return( 1 );
6153
6154 return( 0 );
6155}
Paul Bakker9af723c2014-05-01 13:03:14 +02006156#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006157
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006158#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006159int ssl_check_cert_usage( const x509_crt *cert,
6160 const ssl_ciphersuite_t *ciphersuite,
6161 int cert_endpoint )
6162{
6163#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6164 int usage = 0;
6165#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006166#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6167 const char *ext_oid;
6168 size_t ext_len;
6169#endif
6170
6171#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
6172 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6173 ((void) cert);
6174 ((void) cert_endpoint);
6175#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006176
6177#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
6178 if( cert_endpoint == SSL_IS_SERVER )
6179 {
6180 /* Server part of the key exchange */
6181 switch( ciphersuite->key_exchange )
6182 {
6183 case POLARSSL_KEY_EXCHANGE_RSA:
6184 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
6185 usage = KU_KEY_ENCIPHERMENT;
6186 break;
6187
6188 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
6189 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
6190 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
6191 usage = KU_DIGITAL_SIGNATURE;
6192 break;
6193
6194 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
6195 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
6196 usage = KU_KEY_AGREEMENT;
6197 break;
6198
6199 /* Don't use default: we want warnings when adding new values */
6200 case POLARSSL_KEY_EXCHANGE_NONE:
6201 case POLARSSL_KEY_EXCHANGE_PSK:
6202 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
6203 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
6204 usage = 0;
6205 }
6206 }
6207 else
6208 {
6209 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
6210 usage = KU_DIGITAL_SIGNATURE;
6211 }
6212
6213 if( x509_crt_check_key_usage( cert, usage ) != 0 )
6214 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006215#else
6216 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006217#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
6218
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02006219#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
6220 if( cert_endpoint == SSL_IS_SERVER )
6221 {
6222 ext_oid = OID_SERVER_AUTH;
6223 ext_len = OID_SIZE( OID_SERVER_AUTH );
6224 }
6225 else
6226 {
6227 ext_oid = OID_CLIENT_AUTH;
6228 ext_len = OID_SIZE( OID_CLIENT_AUTH );
6229 }
6230
6231 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
6232 return( -1 );
6233#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
6234
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02006235 return( 0 );
6236}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02006237#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006238
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006239/*
6240 * Convert version numbers to/from wire format
6241 * and, for DTLS, to/from TLS equivalent.
6242 *
6243 * For TLS this is the identity.
6244 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
6245 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
6246 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
6247 */
6248void ssl_write_version( int major, int minor, int transport,
6249 unsigned char ver[2] )
6250{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006251#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006252 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006253 {
6254 if( minor == SSL_MINOR_VERSION_2 )
6255 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6256
6257 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
6258 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
6259 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006260 else
6261#else
6262 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006263#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006264 {
6265 ver[0] = (unsigned char) major;
6266 ver[1] = (unsigned char) minor;
6267 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006268}
6269
6270void ssl_read_version( int *major, int *minor, int transport,
6271 const unsigned char ver[2] )
6272{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006273#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006274 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006275 {
6276 *major = 255 - ver[0] + 2;
6277 *minor = 255 - ver[1] + 1;
6278
6279 if( *minor == SSL_MINOR_VERSION_1 )
6280 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
6281 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006282 else
6283#else
6284 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006285#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01006286 {
6287 *major = ver[0];
6288 *minor = ver[1];
6289 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01006290}
6291
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02006292#endif /* POLARSSL_SSL_TLS_C */