blob: 0b325616ff5bce6472d8a518cdebb1c139b1c9c5 [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 );
75#endif
76 return( 0 );
77}
78
Paul Bakker05decb22013-08-15 13:33:48 +020079#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020080/*
81 * Convert max_fragment_length codes to length.
82 * RFC 6066 says:
83 * enum{
84 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
85 * } MaxFragmentLength;
86 * and we add 0 -> extension unused
87 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020088static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020089{
90 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
91 512, /* SSL_MAX_FRAG_LEN_512 */
92 1024, /* SSL_MAX_FRAG_LEN_1024 */
93 2048, /* SSL_MAX_FRAG_LEN_2048 */
94 4096, /* SSL_MAX_FRAG_LEN_4096 */
95};
Paul Bakker05decb22013-08-15 13:33:48 +020096#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020097
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
99{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 ssl_session_free( dst );
101 memcpy( dst, src, sizeof( ssl_session ) );
102
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200103#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 if( src->peer_cert != NULL )
105 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200106 int ret;
107
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200108 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
109 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200110 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
111
Paul Bakkerb6b09562013-09-18 14:17:41 +0200112 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200114 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
115 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200116 {
117 polarssl_free( dst->peer_cert );
118 dst->peer_cert = NULL;
119 return( ret );
120 }
121 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200123
Paul Bakkera503a632013-08-14 13:48:06 +0200124#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200125 if( src->ticket != NULL )
126 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200127 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
128 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200129 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
130
131 memcpy( dst->ticket, src->ticket, src->ticket_len );
132 }
Paul Bakkera503a632013-08-14 13:48:06 +0200133#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200134
135 return( 0 );
136}
137
Paul Bakker05ef8352012-05-08 09:17:57 +0000138#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200139int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200140 const unsigned char *key_enc, const unsigned char *key_dec,
141 size_t keylen,
142 const unsigned char *iv_enc, const unsigned char *iv_dec,
143 size_t ivlen,
144 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200145 size_t maclen ) = NULL;
146int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
147int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
148int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
149int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
150int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200151#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153/*
154 * Key material generation
155 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200156#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200157static int ssl3_prf( const unsigned char *secret, size_t slen,
158 const char *label,
159 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 unsigned char *dstbuf, size_t dlen )
161{
162 size_t i;
163 md5_context md5;
164 sha1_context sha1;
165 unsigned char padding[16];
166 unsigned char sha1sum[20];
167 ((void)label);
168
Paul Bakker5b4af392014-06-26 12:09:34 +0200169 md5_init( &md5 );
170 sha1_init( &sha1 );
171
Paul Bakker5f70b252012-09-13 14:23:06 +0000172 /*
173 * SSLv3:
174 * block =
175 * MD5( secret + SHA1( 'A' + secret + random ) ) +
176 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
177 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
178 * ...
179 */
180 for( i = 0; i < dlen / 16; i++ )
181 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200182 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000183
184 sha1_starts( &sha1 );
185 sha1_update( &sha1, padding, 1 + i );
186 sha1_update( &sha1, secret, slen );
187 sha1_update( &sha1, random, rlen );
188 sha1_finish( &sha1, sha1sum );
189
190 md5_starts( &md5 );
191 md5_update( &md5, secret, slen );
192 md5_update( &md5, sha1sum, 20 );
193 md5_finish( &md5, dstbuf + i * 16 );
194 }
195
Paul Bakker5b4af392014-06-26 12:09:34 +0200196 md5_free( &md5 );
197 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000198
Paul Bakker34617722014-06-13 17:20:13 +0200199 polarssl_zeroize( padding, sizeof( padding ) );
200 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000201
202 return( 0 );
203}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200204#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000205
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200206#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200207static int tls1_prf( const unsigned char *secret, size_t slen,
208 const char *label,
209 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000210 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Paul Bakker23986e52011-04-24 08:57:21 +0000212 size_t nb, hs;
213 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200214 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 unsigned char tmp[128];
216 unsigned char h_i[20];
217
218 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000219 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 hs = ( slen + 1 ) / 2;
222 S1 = secret;
223 S2 = secret + slen - hs;
224
225 nb = strlen( label );
226 memcpy( tmp + 20, label, nb );
227 memcpy( tmp + 20 + nb, random, rlen );
228 nb += rlen;
229
230 /*
231 * First compute P_md5(secret,label+random)[0..dlen]
232 */
233 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
234
235 for( i = 0; i < dlen; i += 16 )
236 {
237 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
238 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
239
240 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
241
242 for( j = 0; j < k; j++ )
243 dstbuf[i + j] = h_i[j];
244 }
245
246 /*
247 * XOR out with P_sha1(secret,label+random)[0..dlen]
248 */
249 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
250
251 for( i = 0; i < dlen; i += 20 )
252 {
253 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
254 sha1_hmac( S2, hs, tmp, 20, tmp );
255
256 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
257
258 for( j = 0; j < k; j++ )
259 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
260 }
261
Paul Bakker34617722014-06-13 17:20:13 +0200262 polarssl_zeroize( tmp, sizeof( tmp ) );
263 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
265 return( 0 );
266}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200267#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200269#if defined(POLARSSL_SSL_PROTO_TLS1_2)
270#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200271static int tls_prf_sha256( const unsigned char *secret, size_t slen,
272 const char *label,
273 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000274 unsigned char *dstbuf, size_t dlen )
275{
276 size_t nb;
277 size_t i, j, k;
278 unsigned char tmp[128];
279 unsigned char h_i[32];
280
281 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
282 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
283
284 nb = strlen( label );
285 memcpy( tmp + 32, label, nb );
286 memcpy( tmp + 32 + nb, random, rlen );
287 nb += rlen;
288
289 /*
290 * Compute P_<hash>(secret, label + random)[0..dlen]
291 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200292 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000293
294 for( i = 0; i < dlen; i += 32 )
295 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200296 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
297 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000298
299 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
300
301 for( j = 0; j < k; j++ )
302 dstbuf[i + j] = h_i[j];
303 }
304
Paul Bakker34617722014-06-13 17:20:13 +0200305 polarssl_zeroize( tmp, sizeof( tmp ) );
306 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000307
308 return( 0 );
309}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200310#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000311
Paul Bakker9e36f042013-06-30 14:34:05 +0200312#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200313static int tls_prf_sha384( const unsigned char *secret, size_t slen,
314 const char *label,
315 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000316 unsigned char *dstbuf, size_t dlen )
317{
318 size_t nb;
319 size_t i, j, k;
320 unsigned char tmp[128];
321 unsigned char h_i[48];
322
323 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
324 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
325
326 nb = strlen( label );
327 memcpy( tmp + 48, label, nb );
328 memcpy( tmp + 48 + nb, random, rlen );
329 nb += rlen;
330
331 /*
332 * Compute P_<hash>(secret, label + random)[0..dlen]
333 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200334 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000335
336 for( i = 0; i < dlen; i += 48 )
337 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200338 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
339 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000340
341 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
342
343 for( j = 0; j < k; j++ )
344 dstbuf[i + j] = h_i[j];
345 }
346
Paul Bakker34617722014-06-13 17:20:13 +0200347 polarssl_zeroize( tmp, sizeof( tmp ) );
348 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000349
350 return( 0 );
351}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#endif /* POLARSSL_SHA512_C */
353#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000354
Paul Bakker66d5d072014-06-17 16:39:18 +0200355static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200356
357#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
358 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200359static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
Paul Bakker380da532012-04-18 16:10:25 +0000361
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200363static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
364static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
366
367#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
369static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200370#endif
371
372#if defined(POLARSSL_SSL_PROTO_TLS1_2)
373#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200374static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
375static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
376static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200377#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100378
Paul Bakker9e36f042013-06-30 14:34:05 +0200379#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200380static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
381static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
382static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100383#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200384#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000385
Paul Bakker5121ce52009-01-03 21:22:43 +0000386int ssl_derive_keys( ssl_context *ssl )
387{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200388 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 unsigned char keyblk[256];
391 unsigned char *key1;
392 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100393 unsigned char *mac_enc;
394 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200395 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100396 const cipher_info_t *cipher_info;
397 const md_info_t *md_info;
398
Paul Bakker48916f92012-09-16 19:57:18 +0000399 ssl_session *session = ssl->session_negotiate;
400 ssl_transform *transform = ssl->transform_negotiate;
401 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
403 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
404
Paul Bakker68884e32013-01-07 18:20:04 +0100405 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
406 if( cipher_info == NULL )
407 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200408 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100409 transform->ciphersuite_info->cipher ) );
410 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
411 }
412
413 md_info = md_info_from_type( transform->ciphersuite_info->mac );
414 if( md_info == NULL )
415 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200416 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100417 transform->ciphersuite_info->mac ) );
418 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
419 }
420
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000422 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000423 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200424#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000425 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000426 {
Paul Bakker48916f92012-09-16 19:57:18 +0000427 handshake->tls_prf = ssl3_prf;
428 handshake->calc_verify = ssl_calc_verify_ssl;
429 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200431 else
432#endif
433#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
434 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000435 {
Paul Bakker48916f92012-09-16 19:57:18 +0000436 handshake->tls_prf = tls1_prf;
437 handshake->calc_verify = ssl_calc_verify_tls;
438 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000439 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440 else
441#endif
442#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200443#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
445 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000446 {
Paul Bakker48916f92012-09-16 19:57:18 +0000447 handshake->tls_prf = tls_prf_sha384;
448 handshake->calc_verify = ssl_calc_verify_tls_sha384;
449 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000450 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000451 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200452#endif
453#if defined(POLARSSL_SHA256_C)
454 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000455 {
Paul Bakker48916f92012-09-16 19:57:18 +0000456 handshake->tls_prf = tls_prf_sha256;
457 handshake->calc_verify = ssl_calc_verify_tls_sha256;
458 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200460 else
461#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200462#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200463 {
464 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200465 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200466 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000467
468 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 * SSLv3:
470 * master =
471 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
472 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
473 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200474 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200475 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 * master = PRF( premaster, "master secret", randbytes )[0..47]
477 */
Paul Bakker0a597072012-09-25 21:55:46 +0000478 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000479 {
Paul Bakker48916f92012-09-16 19:57:18 +0000480 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
481 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000482
Paul Bakker48916f92012-09-16 19:57:18 +0000483 handshake->tls_prf( handshake->premaster, handshake->pmslen,
484 "master secret",
485 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
Paul Bakker34617722014-06-13 17:20:13 +0200487 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488 }
489 else
490 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
491
492 /*
493 * Swap the client and server random values.
494 */
Paul Bakker48916f92012-09-16 19:57:18 +0000495 memcpy( tmp, handshake->randbytes, 64 );
496 memcpy( handshake->randbytes, tmp + 32, 32 );
497 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200498 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500 /*
501 * SSLv3:
502 * key block =
503 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
504 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
505 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
506 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
507 * ...
508 *
509 * TLSv1:
510 * key block = PRF( master, "key expansion", randbytes )
511 */
Paul Bakker48916f92012-09-16 19:57:18 +0000512 handshake->tls_prf( session->master, 48, "key expansion",
513 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000514
Paul Bakker48916f92012-09-16 19:57:18 +0000515 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
516 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
517 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
518 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
520
Paul Bakker34617722014-06-13 17:20:13 +0200521 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523 /*
524 * Determine the appropriate key, IV and MAC length.
525 */
Paul Bakker68884e32013-01-07 18:20:04 +0100526
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200527 transform->keylen = cipher_info->key_length / 8;
528
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200529 if( cipher_info->mode == POLARSSL_MODE_GCM ||
530 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200532 transform->maclen = 0;
533
Paul Bakker68884e32013-01-07 18:20:04 +0100534 transform->ivlen = 12;
535 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200536
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200537 /* Minimum length is expicit IV + tag */
538 transform->minlen = transform->ivlen - transform->fixed_ivlen
539 + ( transform->ciphersuite_info->flags &
540 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100541 }
542 else
543 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200544 int ret;
545
546 /* Initialize HMAC contexts */
547 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
548 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100549 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200550 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
551 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100552 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 /* Get MAC length */
555 transform->maclen = md_get_size( md_info );
556
557#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
558 /*
559 * If HMAC is to be truncated, we shall keep the leftmost bytes,
560 * (rfc 6066 page 13 or rfc 2104 section 4),
561 * so we only need to adjust the length here.
562 */
563 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
564 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
565#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
566
567 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100568 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200570 /* Minimum length */
571 if( cipher_info->mode == POLARSSL_MODE_STREAM )
572 transform->minlen = transform->maclen;
573 else
Paul Bakker68884e32013-01-07 18:20:04 +0100574 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200575 /*
576 * GenericBlockCipher:
577 * first multiple of blocklen greater than maclen
578 * + IV except for SSL3 and TLS 1.0
579 */
580 transform->minlen = transform->maclen
581 + cipher_info->block_size
582 - transform->maclen % cipher_info->block_size;
583
584#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
585 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
586 ssl->minor_ver == SSL_MINOR_VERSION_1 )
587 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100588 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200589#endif
590#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
591 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
592 ssl->minor_ver == SSL_MINOR_VERSION_3 )
593 {
594 transform->minlen += transform->ivlen;
595 }
596 else
597#endif
598 {
599 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
600 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
601 }
Paul Bakker68884e32013-01-07 18:20:04 +0100602 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 }
604
605 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000606 transform->keylen, transform->minlen, transform->ivlen,
607 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000608
609 /*
610 * Finally setup the cipher contexts, IVs and MAC secrets.
611 */
612 if( ssl->endpoint == SSL_IS_CLIENT )
613 {
Paul Bakker48916f92012-09-16 19:57:18 +0000614 key1 = keyblk + transform->maclen * 2;
615 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
Paul Bakker68884e32013-01-07 18:20:04 +0100617 mac_enc = keyblk;
618 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000620 /*
621 * This is not used in TLS v1.1.
622 */
Paul Bakker48916f92012-09-16 19:57:18 +0000623 iv_copy_len = ( transform->fixed_ivlen ) ?
624 transform->fixed_ivlen : transform->ivlen;
625 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
626 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000627 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 }
629 else
630 {
Paul Bakker48916f92012-09-16 19:57:18 +0000631 key1 = keyblk + transform->maclen * 2 + transform->keylen;
632 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Paul Bakker68884e32013-01-07 18:20:04 +0100634 mac_enc = keyblk + transform->maclen;
635 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000637 /*
638 * This is not used in TLS v1.1.
639 */
Paul Bakker48916f92012-09-16 19:57:18 +0000640 iv_copy_len = ( transform->fixed_ivlen ) ?
641 transform->fixed_ivlen : transform->ivlen;
642 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
643 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000644 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 }
646
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100648 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
649 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100650 if( transform->maclen > sizeof transform->mac_enc )
651 {
652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100654 }
655
Paul Bakker68884e32013-01-07 18:20:04 +0100656 memcpy( transform->mac_enc, mac_enc, transform->maclen );
657 memcpy( transform->mac_dec, mac_dec, transform->maclen );
658 }
659 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200660#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200661#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
662 defined(POLARSSL_SSL_PROTO_TLS1_2)
663 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100664 {
665 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
666 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
667 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668 else
669#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200670 {
671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200672 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200673 }
Paul Bakker68884e32013-01-07 18:20:04 +0100674
Paul Bakker05ef8352012-05-08 09:17:57 +0000675#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200676 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 {
678 int ret = 0;
679
680 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
681
Paul Bakker07eb38b2012-12-19 14:42:06 +0100682 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
683 transform->iv_enc, transform->iv_dec,
684 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100685 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100686 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000687 {
688 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200689 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000690 }
691 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200692#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000693
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200694 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
695 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200697 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
698 return( ret );
699 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200700
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200701 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
702 cipher_info ) ) != 0 )
703 {
704 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
705 return( ret );
706 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200707
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200708 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
709 cipher_info->key_length,
710 POLARSSL_ENCRYPT ) ) != 0 )
711 {
712 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
713 return( ret );
714 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200715
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200716 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
717 cipher_info->key_length,
718 POLARSSL_DECRYPT ) ) != 0 )
719 {
720 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
721 return( ret );
722 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200723
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200724#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200725 if( cipher_info->mode == POLARSSL_MODE_CBC )
726 {
727 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
728 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200729 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200730 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
731 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200732 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200733
734 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
735 POLARSSL_PADDING_NONE ) ) != 0 )
736 {
737 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
738 return( ret );
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker34617722014-06-13 17:20:13 +0200743 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker2770fbd2012-07-03 13:30:23 +0000745#if defined(POLARSSL_ZLIB_SUPPORT)
746 // Initialize compression
747 //
Paul Bakker48916f92012-09-16 19:57:18 +0000748 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 {
Paul Bakker16770332013-10-11 09:59:44 +0200750 if( ssl->compress_buf == NULL )
751 {
752 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
753 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
754 if( ssl->compress_buf == NULL )
755 {
756 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
757 SSL_BUFFER_LEN ) );
758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
759 }
760 }
761
Paul Bakker2770fbd2012-07-03 13:30:23 +0000762 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
763
Paul Bakker48916f92012-09-16 19:57:18 +0000764 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
765 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000766
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200767 if( deflateInit( &transform->ctx_deflate,
768 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000769 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000770 {
771 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
772 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
773 }
774 }
775#endif /* POLARSSL_ZLIB_SUPPORT */
776
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
778
779 return( 0 );
780}
781
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000783void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000784{
785 md5_context md5;
786 sha1_context sha1;
787 unsigned char pad_1[48];
788 unsigned char pad_2[48];
789
Paul Bakker380da532012-04-18 16:10:25 +0000790 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakker48916f92012-09-16 19:57:18 +0000792 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
793 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker380da532012-04-18 16:10:25 +0000795 memset( pad_1, 0x36, 48 );
796 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Paul Bakker48916f92012-09-16 19:57:18 +0000798 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 md5_update( &md5, pad_1, 48 );
800 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Paul Bakker380da532012-04-18 16:10:25 +0000802 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000803 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_update( &md5, pad_2, 48 );
805 md5_update( &md5, hash, 16 );
806 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000809 sha1_update( &sha1, pad_1, 40 );
810 sha1_finish( &sha1, hash + 16 );
811
812 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000813 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000814 sha1_update( &sha1, pad_2, 40 );
815 sha1_update( &sha1, hash + 16, 20 );
816 sha1_finish( &sha1, hash + 16 );
817
818 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
819 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
820
Paul Bakker5b4af392014-06-26 12:09:34 +0200821 md5_free( &md5 );
822 sha1_free( &sha1 );
823
Paul Bakker380da532012-04-18 16:10:25 +0000824 return;
825}
Paul Bakker9af723c2014-05-01 13:03:14 +0200826#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
830{
831 md5_context md5;
832 sha1_context sha1;
833
834 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000838
Paul Bakker48916f92012-09-16 19:57:18 +0000839 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000840 sha1_finish( &sha1, hash + 16 );
841
842 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
843 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
844
Paul Bakker5b4af392014-06-26 12:09:34 +0200845 md5_free( &md5 );
846 sha1_free( &sha1 );
847
Paul Bakker380da532012-04-18 16:10:25 +0000848 return;
849}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200850#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000851
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#if defined(POLARSSL_SSL_PROTO_TLS1_2)
853#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000854void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
855{
Paul Bakker9e36f042013-06-30 14:34:05 +0200856 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000857
858 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
859
Paul Bakker9e36f042013-06-30 14:34:05 +0200860 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
861 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000862
863 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
864 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
865
Paul Bakker5b4af392014-06-26 12:09:34 +0200866 sha256_free( &sha256 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200870#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakker9e36f042013-06-30 14:34:05 +0200872#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
874{
Paul Bakker9e36f042013-06-30 14:34:05 +0200875 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000876
877 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
878
Paul Bakker9e36f042013-06-30 14:34:05 +0200879 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
880 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Paul Bakkerca4ab492012-04-18 14:23:57 +0000882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 sha512_free( &sha512 );
886
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 return;
888}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200889#endif /* POLARSSL_SHA512_C */
890#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200892#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200893int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
894{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 unsigned char *p = ssl->handshake->premaster;
896 unsigned char *end = p + sizeof( ssl->handshake->premaster );
897
898 /*
899 * PMS = struct {
900 * opaque other_secret<0..2^16-1>;
901 * opaque psk<0..2^16-1>;
902 * };
903 * with "other_secret" depending on the particular key exchange
904 */
905#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
906 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
907 {
908 if( end - p < 2 + (int) ssl->psk_len )
909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
910
911 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
912 *(p++) = (unsigned char)( ssl->psk_len );
913 p += ssl->psk_len;
914 }
915 else
916#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200917#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
918 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
919 {
920 /*
921 * other_secret already set by the ClientKeyExchange message,
922 * and is 48 bytes long
923 */
924 *p++ = 0;
925 *p++ = 48;
926 p += 48;
927 }
928 else
929#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
931 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
932 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200933 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200934 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200935
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200936 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 p + 2, &len,
939 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200940 {
941 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
942 return( ret );
943 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200944 *(p++) = (unsigned char)( len >> 8 );
945 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200946 p += len;
947
948 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
949 }
950 else
951#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
952#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
953 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
954 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200955 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200956 size_t zlen;
957
958 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200959 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960 ssl->f_rng, ssl->p_rng ) ) != 0 )
961 {
962 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
963 return( ret );
964 }
965
966 *(p++) = (unsigned char)( zlen >> 8 );
967 *(p++) = (unsigned char)( zlen );
968 p += zlen;
969
970 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
971 }
972 else
973#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
974 {
975 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200976 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200977 }
978
979 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100980 if( end - p < 2 + (int) ssl->psk_len )
981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
982
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
984 *(p++) = (unsigned char)( ssl->psk_len );
985 memcpy( p, ssl->psk, ssl->psk_len );
986 p += ssl->psk_len;
987
988 ssl->handshake->pmslen = p - ssl->handshake->premaster;
989
990 return( 0 );
991}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200992#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200993
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000995/*
996 * SSLv3.0 MAC functions
997 */
Paul Bakker68884e32013-01-07 18:20:04 +0100998static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
999 unsigned char *buf, size_t len,
1000 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001001{
1002 unsigned char header[11];
1003 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001004 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001005 int md_size = md_get_size( md_ctx->md_info );
1006 int md_type = md_get_type( md_ctx->md_info );
1007
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001008 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 if( md_type == POLARSSL_MD_MD5 )
1010 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001011 else
Paul Bakker68884e32013-01-07 18:20:04 +01001012 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014 memcpy( header, ctr, 8 );
1015 header[ 8] = (unsigned char) type;
1016 header[ 9] = (unsigned char)( len >> 8 );
1017 header[10] = (unsigned char)( len );
1018
Paul Bakker68884e32013-01-07 18:20:04 +01001019 memset( padding, 0x36, padlen );
1020 md_starts( md_ctx );
1021 md_update( md_ctx, secret, md_size );
1022 md_update( md_ctx, padding, padlen );
1023 md_update( md_ctx, header, 11 );
1024 md_update( md_ctx, buf, len );
1025 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker68884e32013-01-07 18:20:04 +01001027 memset( padding, 0x5C, padlen );
1028 md_starts( md_ctx );
1029 md_update( md_ctx, secret, md_size );
1030 md_update( md_ctx, padding, padlen );
1031 md_update( md_ctx, buf + len, md_size );
1032 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001033}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001034#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001035
Paul Bakker5121ce52009-01-03 21:22:43 +00001036/*
1037 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001038 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001039static int ssl_encrypt_buf( ssl_context *ssl )
1040{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001041 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001042 const cipher_mode_t mode = cipher_get_cipher_mode(
1043 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
1045 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1046
1047 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001048 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001049 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001050#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1051 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1052 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001053 if( mode != POLARSSL_MODE_GCM &&
1054 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001056#if defined(POLARSSL_SSL_PROTO_SSL3)
1057 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1058 {
1059 ssl_mac( &ssl->transform_out->md_ctx_enc,
1060 ssl->transform_out->mac_enc,
1061 ssl->out_msg, ssl->out_msglen,
1062 ssl->out_ctr, ssl->out_msgtype );
1063 }
1064 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065#endif
1066#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001067 defined(POLARSSL_SSL_PROTO_TLS1_2)
1068 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1069 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01001070 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_buf, 13 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001071 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1072 ssl->out_msg, ssl->out_msglen );
1073 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1074 ssl->out_msg + ssl->out_msglen );
1075 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1076 }
1077 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001078#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001079 {
1080 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001081 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 }
1083
1084 SSL_DEBUG_BUF( 4, "computed mac",
1085 ssl->out_msg + ssl->out_msglen,
1086 ssl->transform_out->maclen );
1087
1088 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001089 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001090#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001092 /*
1093 * Encrypt
1094 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001095#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001096 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001097 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001098 int ret;
1099 size_t olen = 0;
1100
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1102 "including %d bytes of padding",
1103 ssl->out_msglen, 0 ) );
1104
1105 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1106 ssl->out_msg, ssl->out_msglen );
1107
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001108 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001109 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001110 ssl->transform_out->ivlen,
1111 ssl->out_msg, ssl->out_msglen,
1112 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001113 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001114 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001115 return( ret );
1116 }
1117
1118 if( ssl->out_msglen != olen )
1119 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001120 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001121 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 }
Paul Bakker68884e32013-01-07 18:20:04 +01001124 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001126#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1127 if( mode == POLARSSL_MODE_GCM ||
1128 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001130 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001131 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 unsigned char *enc_msg;
1133 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001134 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1135 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137 memcpy( add_data, ssl->out_ctr, 8 );
1138 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001139 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1140 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001141 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1142 add_data[12] = ssl->out_msglen & 0xFF;
1143
1144 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1145 add_data, 13 );
1146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 /*
1148 * Generate IV
1149 */
1150 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001151 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1152 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001153 if( ret != 0 )
1154 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001155
Paul Bakker68884e32013-01-07 18:20:04 +01001156 memcpy( ssl->out_iv,
1157 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1158 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001159
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001160 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001161 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001162
Paul Bakker68884e32013-01-07 18:20:04 +01001163 /*
1164 * Fix pointer positions and message length with added IV
1165 */
1166 enc_msg = ssl->out_msg;
1167 enc_msglen = ssl->out_msglen;
1168 ssl->out_msglen += ssl->transform_out->ivlen -
1169 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001170
Paul Bakker68884e32013-01-07 18:20:04 +01001171 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1172 "including %d bytes of padding",
1173 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001174
Paul Bakker68884e32013-01-07 18:20:04 +01001175 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1176 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Paul Bakker68884e32013-01-07 18:20:04 +01001178 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001179 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001180 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001181 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1182 ssl->transform_out->iv_enc,
1183 ssl->transform_out->ivlen,
1184 add_data, 13,
1185 enc_msg, enc_msglen,
1186 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001187 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001188 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001189 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 return( ret );
1191 }
1192
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001193 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001194 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001195 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001196 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001197 }
1198
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001201 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001205#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1206 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001207 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001209 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001210 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001211 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212
Paul Bakker48916f92012-09-16 19:57:18 +00001213 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1214 ssl->transform_out->ivlen;
1215 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 padlen = 0;
1217
1218 for( i = 0; i <= padlen; i++ )
1219 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1220
1221 ssl->out_msglen += padlen + 1;
1222
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001223 enc_msglen = ssl->out_msglen;
1224 enc_msg = ssl->out_msg;
1225
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001226#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001227 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001228 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1229 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001231 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001232 {
1233 /*
1234 * Generate IV
1235 */
Paul Bakker48916f92012-09-16 19:57:18 +00001236 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1237 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001238 if( ret != 0 )
1239 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240
Paul Bakker92be97b2013-01-02 17:30:03 +01001241 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001242 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243
1244 /*
1245 * Fix pointer positions and message length with added IV
1246 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001247 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001248 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001249 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001251#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001252
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001254 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001255 ssl->out_msglen, ssl->transform_out->ivlen,
1256 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
1258 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001259 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001261 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001262 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001263 ssl->transform_out->ivlen,
1264 enc_msg, enc_msglen,
1265 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001266 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001267 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 return( ret );
1269 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001270
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 if( enc_msglen != olen )
1272 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001273 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001274 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001276
1277#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1279 {
1280 /*
1281 * Save IV in SSL3 and TLS1
1282 */
1283 memcpy( ssl->transform_out->iv_enc,
1284 ssl->transform_out->cipher_ctx_enc.iv,
1285 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001286 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001287#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001288 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001289 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001290#endif /* POLARSSL_CIPHER_MODE_CBC &&
1291 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 {
1293 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001294 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
1297 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1298
1299 return( 0 );
1300}
1301
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001302#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001303
Paul Bakker5121ce52009-01-03 21:22:43 +00001304static int ssl_decrypt_buf( ssl_context *ssl )
1305{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001306 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001307 const cipher_mode_t mode = cipher_get_cipher_mode(
1308 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001309#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1310 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1311 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1312 size_t padlen = 0, correct = 1;
1313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
1315 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1316
Paul Bakker48916f92012-09-16 19:57:18 +00001317 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 {
1319 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001320 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001321 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
1323
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001324#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001325 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001326 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001327 int ret;
1328 size_t olen = 0;
1329
Paul Bakker68884e32013-01-07 18:20:04 +01001330 padlen = 0;
1331
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001333 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001334 ssl->transform_in->ivlen,
1335 ssl->in_msg, ssl->in_msglen,
1336 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001337 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001338 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001339 return( ret );
1340 }
1341
1342 if( ssl->in_msglen != olen )
1343 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001344 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001345 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001347 }
Paul Bakker68884e32013-01-07 18:20:04 +01001348 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001349#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001350#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1351 if( mode == POLARSSL_MODE_GCM ||
1352 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001354 int ret;
1355 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 unsigned char *dec_msg;
1357 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001358 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001359 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1360 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001361 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1362 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001363
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001364 if( ssl->in_msglen < explicit_iv_len + taglen )
1365 {
1366 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1367 "+ taglen (%d)", ssl->in_msglen,
1368 explicit_iv_len, taglen ) );
1369 return( POLARSSL_ERR_SSL_INVALID_MAC );
1370 }
1371 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1372
Paul Bakker68884e32013-01-07 18:20:04 +01001373 dec_msg = ssl->in_msg;
1374 dec_msg_result = ssl->in_msg;
1375 ssl->in_msglen = dec_msglen;
1376
1377 memcpy( add_data, ssl->in_ctr, 8 );
1378 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001379 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1380 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001381 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1382 add_data[12] = ssl->in_msglen & 0xFF;
1383
1384 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1385 add_data, 13 );
1386
1387 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1388 ssl->in_iv,
1389 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1390
1391 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1392 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001393 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001394
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001395 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001396 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001397 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001398 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1399 ssl->transform_in->iv_dec,
1400 ssl->transform_in->ivlen,
1401 add_data, 13,
1402 dec_msg, dec_msglen,
1403 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001404 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001405 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001406 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1407
1408 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1409 return( POLARSSL_ERR_SSL_INVALID_MAC );
1410
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001411 return( ret );
1412 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001413
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001414 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001415 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001416 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001417 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001418 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001421#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001422#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1423 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001424 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001425 {
Paul Bakker45829992013-01-03 14:52:21 +01001426 /*
1427 * Decrypt and check the padding
1428 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001429 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001430 unsigned char *dec_msg;
1431 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001432 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001433 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001434 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001435
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 /*
Paul Bakker45829992013-01-03 14:52:21 +01001437 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 */
Paul Bakker48916f92012-09-16 19:57:18 +00001439 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 {
1441 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001442 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001443 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444 }
1445
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001446#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001447 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1448 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#endif
Paul Bakker45829992013-01-03 14:52:21 +01001450
1451 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1452 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1453 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001454 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1455 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1456 ssl->transform_in->ivlen,
1457 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001458 return( POLARSSL_ERR_SSL_INVALID_MAC );
1459 }
1460
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001461 dec_msglen = ssl->in_msglen;
1462 dec_msg = ssl->in_msg;
1463 dec_msg_result = ssl->in_msg;
1464
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001465#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001466 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001467 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001469 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001470 {
Paul Bakker48916f92012-09-16 19:57:18 +00001471 dec_msglen -= ssl->transform_in->ivlen;
1472 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473
Paul Bakker48916f92012-09-16 19:57:18 +00001474 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001475 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001477#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001478
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001479 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001480 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001481 ssl->transform_in->ivlen,
1482 dec_msg, dec_msglen,
1483 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001484 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001485 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001486 return( ret );
1487 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001488
Paul Bakkercca5b812013-08-31 17:40:26 +02001489 if( dec_msglen != olen )
1490 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001491 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001492 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001493 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001494
1495#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001496 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1497 {
1498 /*
1499 * Save IV in SSL3 and TLS1
1500 */
1501 memcpy( ssl->transform_in->iv_dec,
1502 ssl->transform_in->cipher_ctx_dec.iv,
1503 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001504 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001505#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001506
1507 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001508
1509 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1510 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001511#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001512 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1513 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001514#endif
Paul Bakker45829992013-01-03 14:52:21 +01001515 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001516 correct = 0;
1517 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001519#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1521 {
Paul Bakker48916f92012-09-16 19:57:18 +00001522 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001524#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001525 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1526 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001527 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001528#endif
Paul Bakker45829992013-01-03 14:52:21 +01001529 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 }
1531 }
1532 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001533#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001534#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1535 defined(POLARSSL_SSL_PROTO_TLS1_2)
1536 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 {
1538 /*
Paul Bakker45829992013-01-03 14:52:21 +01001539 * TLSv1+: always check the padding up to the first failure
1540 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001542 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001543 size_t padding_idx = ssl->in_msglen - padlen - 1;
1544
Paul Bakker956c9e02013-12-19 14:42:28 +01001545 /*
1546 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001547 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001548 *
Paul Bakker61885c72014-04-25 12:59:03 +02001549 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1550 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001551 *
1552 * In both cases we reset padding_idx to a safe value (0) to
1553 * prevent out-of-buffer reads.
1554 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001555 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001556 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1557 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001558
1559 padding_idx *= correct;
1560
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001561 for( i = 1; i <= 256; i++ )
1562 {
1563 real_count &= ( i <= padlen );
1564 pad_count += real_count *
1565 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1566 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001567
1568 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001569
Paul Bakkerd66f0702013-01-31 16:57:45 +01001570#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001571 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001572 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001573#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001574 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001576 else
1577#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1578 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001579 {
1580 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001581 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001584 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001585#endif /* POLARSSL_CIPHER_MODE_CBC &&
1586 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001587 {
1588 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001589 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
1592 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1593 ssl->in_msg, ssl->in_msglen );
1594
1595 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001596 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001598#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1599 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1600 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001601 if( mode != POLARSSL_MODE_GCM &&
1602 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001603 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001604 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1605
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001607
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001608 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1609 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001611 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001613#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1615 {
1616 ssl_mac( &ssl->transform_in->md_ctx_dec,
1617 ssl->transform_in->mac_dec,
1618 ssl->in_msg, ssl->in_msglen,
1619 ssl->in_ctr, ssl->in_msgtype );
1620 }
1621 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001622#endif /* POLARSSL_SSL_PROTO_SSL3 */
1623#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 defined(POLARSSL_SSL_PROTO_TLS1_2)
1625 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1626 {
1627 /*
1628 * Process MAC and always update for padlen afterwards to make
1629 * total time independent of padlen
1630 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001631 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001632 *
1633 * Known timing attacks:
1634 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1635 *
1636 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1637 * correctly. (We round down instead of up, so -56 is the correct
1638 * value for our calculations instead of -55)
1639 */
1640 size_t j, extra_run = 0;
1641 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1642 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001643
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001644 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001645
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01001646 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_buf, 13 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1648 ssl->in_msglen );
1649 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1650 ssl->in_msg + ssl->in_msglen );
1651 for( j = 0; j < extra_run; j++ )
1652 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001653
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001654 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1655 }
1656 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001657#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 POLARSSL_SSL_PROTO_TLS1_2 */
1659 {
1660 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001661 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1665 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1666 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001668 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 ssl->transform_in->maclen ) != 0 )
1670 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001671#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001673#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001674 correct = 0;
1675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 /*
1678 * Finally check the correct flag
1679 */
1680 if( correct == 0 )
1681 return( POLARSSL_ERR_SSL_INVALID_MAC );
1682 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001683#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 if( ssl->in_msglen == 0 )
1686 {
1687 ssl->nb_zero++;
1688
1689 /*
1690 * Three or more empty messages may be a DoS attack
1691 * (excessive CPU consumption).
1692 */
1693 if( ssl->nb_zero > 3 )
1694 {
1695 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1696 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001697 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 }
1699 }
1700 else
1701 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001702
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001703 /* Input counter not used with DTLS right now,
1704 * but it doesn't hurt to have this part ready */
1705 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1706 if( ++ssl->in_ctr[i - 1] != 0 )
1707 break;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001708
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001709 /* The loop goes to its end iff the counter is wrapping */
1710 if( i == ssl_ep_len( ssl ) )
1711 {
1712 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1713 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001714 }
1715
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1717
1718 return( 0 );
1719}
1720
Paul Bakker2770fbd2012-07-03 13:30:23 +00001721#if defined(POLARSSL_ZLIB_SUPPORT)
1722/*
1723 * Compression/decompression functions
1724 */
1725static int ssl_compress_buf( ssl_context *ssl )
1726{
1727 int ret;
1728 unsigned char *msg_post = ssl->out_msg;
1729 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001730 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001731
1732 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1733
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001734 if( len_pre == 0 )
1735 return( 0 );
1736
Paul Bakker2770fbd2012-07-03 13:30:23 +00001737 memcpy( msg_pre, ssl->out_msg, len_pre );
1738
1739 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1740 ssl->out_msglen ) );
1741
1742 SSL_DEBUG_BUF( 4, "before compression: output payload",
1743 ssl->out_msg, ssl->out_msglen );
1744
Paul Bakker48916f92012-09-16 19:57:18 +00001745 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1746 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1747 ssl->transform_out->ctx_deflate.next_out = msg_post;
1748 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001751 if( ret != Z_OK )
1752 {
1753 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1754 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1755 }
1756
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001757 ssl->out_msglen = SSL_BUFFER_LEN -
1758 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759
Paul Bakker2770fbd2012-07-03 13:30:23 +00001760 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1761 ssl->out_msglen ) );
1762
1763 SSL_DEBUG_BUF( 4, "after compression: output payload",
1764 ssl->out_msg, ssl->out_msglen );
1765
1766 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1767
1768 return( 0 );
1769}
1770
1771static int ssl_decompress_buf( ssl_context *ssl )
1772{
1773 int ret;
1774 unsigned char *msg_post = ssl->in_msg;
1775 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001776 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001777
1778 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1779
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001780 if( len_pre == 0 )
1781 return( 0 );
1782
Paul Bakker2770fbd2012-07-03 13:30:23 +00001783 memcpy( msg_pre, ssl->in_msg, len_pre );
1784
1785 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1786 ssl->in_msglen ) );
1787
1788 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1789 ssl->in_msg, ssl->in_msglen );
1790
Paul Bakker48916f92012-09-16 19:57:18 +00001791 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1792 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1793 ssl->transform_in->ctx_inflate.next_out = msg_post;
1794 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001797 if( ret != Z_OK )
1798 {
1799 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1800 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1801 }
1802
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001803 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1804 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805
Paul Bakker2770fbd2012-07-03 13:30:23 +00001806 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1807 ssl->in_msglen ) );
1808
1809 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1810 ssl->in_msg, ssl->in_msglen );
1811
1812 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1813
1814 return( 0 );
1815}
1816#endif /* POLARSSL_ZLIB_SUPPORT */
1817
Paul Bakker5121ce52009-01-03 21:22:43 +00001818/*
1819 * Fill the input message buffer
1820 */
Paul Bakker23986e52011-04-24 08:57:21 +00001821int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001822{
Paul Bakker23986e52011-04-24 08:57:21 +00001823 int ret;
1824 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
1826 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1827
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001828 if( nb_want > SSL_BUFFER_LEN - 8 )
1829 {
1830 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1831 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1832 }
1833
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 while( ssl->in_left < nb_want )
1835 {
1836 len = nb_want - ssl->in_left;
1837 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1838
1839 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1840 ssl->in_left, nb_want ) );
1841 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1842
Paul Bakker831a7552011-05-18 13:32:51 +00001843 if( ret == 0 )
1844 return( POLARSSL_ERR_SSL_CONN_EOF );
1845
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 if( ret < 0 )
1847 return( ret );
1848
1849 ssl->in_left += ret;
1850 }
1851
1852 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1853
1854 return( 0 );
1855}
1856
1857/*
1858 * Flush any data not yet written
1859 */
1860int ssl_flush_output( ssl_context *ssl )
1861{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001862 int ret;
1863 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
1865 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1866
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001867 /* Avoid incrementing counter if data is flushed */
1868 if( ssl->out_left == 0 )
1869 {
1870 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1871 return( 0 );
1872 }
1873
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 while( ssl->out_left > 0 )
1875 {
1876 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001877 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001879 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
1880 ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001882
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1884
1885 if( ret <= 0 )
1886 return( ret );
1887
1888 ssl->out_left -= ret;
1889 }
1890
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001891 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001892 if( ++ssl->out_ctr[i - 1] != 0 )
1893 break;
1894
1895 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001896 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001897 {
1898 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1899 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1900 }
1901
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1903
1904 return( 0 );
1905}
1906
1907/*
1908 * Record layer functions
1909 */
1910int ssl_write_record( ssl_context *ssl )
1911{
Paul Bakker05ef8352012-05-08 09:17:57 +00001912 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001913 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
1915 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1916
Paul Bakker5121ce52009-01-03 21:22:43 +00001917 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1918 {
1919 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1920 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1921 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1922
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001923 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1924 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 }
1926
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001928 if( ssl->transform_out != NULL &&
1929 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001930 {
1931 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1932 {
1933 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1934 return( ret );
1935 }
1936
1937 len = ssl->out_msglen;
1938 }
1939#endif /*POLARSSL_ZLIB_SUPPORT */
1940
Paul Bakker05ef8352012-05-08 09:17:57 +00001941#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001942 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001944 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Paul Bakker05ef8352012-05-08 09:17:57 +00001946 ret = ssl_hw_record_write( ssl );
1947 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1948 {
1949 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001950 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001951 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001952
1953 if( ret == 0 )
1954 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001955 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001956#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001957 if( !done )
1958 {
1959 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001960 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1961 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001962
1963 ssl->out_len[0] = (unsigned char)( len >> 8 );
1964 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001965
Paul Bakker48916f92012-09-16 19:57:18 +00001966 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001967 {
1968 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1969 {
1970 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1971 return( ret );
1972 }
1973
1974 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001975 ssl->out_len[0] = (unsigned char)( len >> 8 );
1976 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001977 }
1978
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001979 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00001980
1981 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1982 "version = [%d:%d], msglen = %d",
1983 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001984 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001985
1986 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001987 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 }
1989
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1991 {
1992 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1993 return( ret );
1994 }
1995
1996 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1997
1998 return( 0 );
1999}
2000
2001int ssl_read_record( ssl_context *ssl )
2002{
Paul Bakker05ef8352012-05-08 09:17:57 +00002003 int ret, done = 0;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002004 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002005
2006 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2007
2008 if( ssl->in_hslen != 0 &&
2009 ssl->in_hslen < ssl->in_msglen )
2010 {
2011 /*
2012 * Get next Handshake message in the current record
2013 */
2014 ssl->in_msglen -= ssl->in_hslen;
2015
Paul Bakker8934a982011-08-05 11:11:53 +00002016 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002017 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002018
2019 ssl->in_hslen = 4;
2020 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2021
2022 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2023 " %d, type = %d, hslen = %d",
2024 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2025
2026 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2027 {
2028 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002029 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 }
2031
2032 if( ssl->in_msglen < ssl->in_hslen )
2033 {
2034 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002035 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002036 }
2037
Paul Bakker4224bc02014-04-08 14:36:50 +02002038 if( ssl->state != SSL_HANDSHAKE_OVER )
2039 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002040
2041 return( 0 );
2042 }
2043
2044 ssl->in_hslen = 0;
2045
2046 /*
2047 * Read the record header and validate it
2048 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002049 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 {
2051 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2052 return( ret );
2053 }
2054
2055 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002056 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
2058 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2059 "version = [%d:%d], msglen = %d",
2060 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002061 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002063 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
2064
2065 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 {
2067 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002068 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002069 }
2070
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002071 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002072 {
2073 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002074 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 }
2076
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002077 /* Sanity check (outer boundaries) */
2078 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2079 {
2080 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2081 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2082 }
2083
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002085 * Make sure the message length is acceptable for the current transform
2086 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 */
Paul Bakker48916f92012-09-16 19:57:18 +00002088 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002090 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 {
2092 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002093 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
2095 }
2096 else
2097 {
Paul Bakker48916f92012-09-16 19:57:18 +00002098 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 {
2100 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002101 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 }
2103
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002104#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002106 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 {
2108 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002109 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002113#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2114 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 /*
2116 * TLS encrypted messages can have up to 256 bytes of padding
2117 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002118 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002119 ssl->in_msglen > ssl->transform_in->minlen +
2120 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002125#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 }
2127
2128 /*
2129 * Read and optionally decrypt the message contents
2130 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002131 if( ( ret = ssl_fetch_input( ssl,
2132 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133 {
2134 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2135 return( ret );
2136 }
2137
2138 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002139 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140
Paul Bakker05ef8352012-05-08 09:17:57 +00002141#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002142 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002143 {
2144 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2145
2146 ret = ssl_hw_record_read( ssl );
2147 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2148 {
2149 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002150 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002151 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002152
2153 if( ret == 0 )
2154 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002155 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002156#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002157 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 {
2159 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2160 {
Paul Bakker40865c82013-01-31 17:13:13 +01002161#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2162 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2163 {
2164 ssl_send_alert_message( ssl,
2165 SSL_ALERT_LEVEL_FATAL,
2166 SSL_ALERT_MSG_BAD_RECORD_MAC );
2167 }
2168#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2170 return( ret );
2171 }
2172
2173 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2174 ssl->in_msg, ssl->in_msglen );
2175
2176 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2177 {
2178 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002179 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 }
2181 }
2182
Paul Bakker2770fbd2012-07-03 13:30:23 +00002183#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002184 if( ssl->transform_in != NULL &&
2185 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002186 {
2187 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2188 {
2189 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2190 return( ret );
2191 }
2192
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002193 // TODO: what's the purpose of these lines? is in_len used?
2194 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2195 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002196 }
2197#endif /* POLARSSL_ZLIB_SUPPORT */
2198
Paul Bakker0a925182012-04-16 06:46:41 +00002199 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2200 ssl->in_msgtype != SSL_MSG_ALERT &&
2201 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2202 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2203 {
2204 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2205
Paul Bakker48916f92012-09-16 19:57:18 +00002206 if( ( ret = ssl_send_alert_message( ssl,
2207 SSL_ALERT_LEVEL_FATAL,
2208 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002209 {
2210 return( ret );
2211 }
2212
2213 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2214 }
2215
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2217 {
2218 ssl->in_hslen = 4;
2219 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2220
2221 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2222 " %d, type = %d, hslen = %d",
2223 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2224
2225 /*
2226 * Additional checks to validate the handshake header
2227 */
2228 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2229 {
2230 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002231 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 }
2233
2234 if( ssl->in_msglen < ssl->in_hslen )
2235 {
2236 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239
Paul Bakker48916f92012-09-16 19:57:18 +00002240 if( ssl->state != SSL_HANDSHAKE_OVER )
2241 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
2244 if( ssl->in_msgtype == SSL_MSG_ALERT )
2245 {
2246 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2247 ssl->in_msg[0], ssl->in_msg[1] ) );
2248
2249 /*
2250 * Ignore non-fatal alerts, except close_notify
2251 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002252 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002254 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2255 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002256 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 }
2258
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002259 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2260 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002261 {
2262 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002263 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 }
2265 }
2266
2267 ssl->in_left = 0;
2268
2269 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2270
2271 return( 0 );
2272}
2273
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002274int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2275{
2276 int ret;
2277
2278 if( ( ret = ssl_send_alert_message( ssl,
2279 SSL_ALERT_LEVEL_FATAL,
2280 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2281 {
2282 return( ret );
2283 }
2284
2285 return( 0 );
2286}
2287
Paul Bakker0a925182012-04-16 06:46:41 +00002288int ssl_send_alert_message( ssl_context *ssl,
2289 unsigned char level,
2290 unsigned char message )
2291{
2292 int ret;
2293
2294 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2295
2296 ssl->out_msgtype = SSL_MSG_ALERT;
2297 ssl->out_msglen = 2;
2298 ssl->out_msg[0] = level;
2299 ssl->out_msg[1] = message;
2300
2301 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2302 {
2303 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2304 return( ret );
2305 }
2306
2307 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2308
2309 return( 0 );
2310}
2311
Paul Bakker5121ce52009-01-03 21:22:43 +00002312/*
2313 * Handshake functions
2314 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002315#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2316 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2317 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2318 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2319 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2320 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2321 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002322int ssl_write_certificate( ssl_context *ssl )
2323{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002324 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002325
2326 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2327
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002328 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002329 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002331 {
2332 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2333 ssl->state++;
2334 return( 0 );
2335 }
2336
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002337 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2338 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002339}
2340
2341int ssl_parse_certificate( ssl_context *ssl )
2342{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002343 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2344
2345 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2346
2347 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002348 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2349 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 {
2351 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2352 ssl->state++;
2353 return( 0 );
2354 }
2355
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002356 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2357 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002358}
2359#else
2360int ssl_write_certificate( ssl_context *ssl )
2361{
2362 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2363 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002364 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002365 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2366
2367 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2368
2369 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002370 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2371 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002372 {
2373 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2374 ssl->state++;
2375 return( 0 );
2376 }
2377
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 if( ssl->endpoint == SSL_IS_CLIENT )
2379 {
2380 if( ssl->client_auth == 0 )
2381 {
2382 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2383 ssl->state++;
2384 return( 0 );
2385 }
2386
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002387#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 /*
2389 * If using SSLv3 and got no cert, send an Alert message
2390 * (otherwise an empty Certificate message will be sent).
2391 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002392 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002393 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2394 {
2395 ssl->out_msglen = 2;
2396 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002397 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2398 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
2400 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2401 goto write_msg;
2402 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002403#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405 else /* SSL_IS_SERVER */
2406 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002407 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002410 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
2412 }
2413
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002414 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 /*
2417 * 0 . 0 handshake type
2418 * 1 . 3 handshake length
2419 * 4 . 6 length of all certs
2420 * 7 . 9 length of cert. 1
2421 * 10 . n-1 peer certificate
2422 * n . n+2 length of cert. 2
2423 * n+3 . ... upper level cert, etc.
2424 */
2425 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002426 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
Paul Bakker29087132010-03-21 21:03:34 +00002428 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 {
2430 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002431 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 {
2433 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2434 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002435 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 }
2437
2438 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2439 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2440 ssl->out_msg[i + 2] = (unsigned char)( n );
2441
2442 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2443 i += n; crt = crt->next;
2444 }
2445
2446 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2447 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2448 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2449
2450 ssl->out_msglen = i;
2451 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2452 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2453
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002454#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002455write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002456#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 ssl->state++;
2459
2460 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2461 {
2462 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2463 return( ret );
2464 }
2465
2466 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2467
Paul Bakkered27a042013-04-18 22:46:23 +02002468 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469}
2470
2471int ssl_parse_certificate( ssl_context *ssl )
2472{
Paul Bakkered27a042013-04-18 22:46:23 +02002473 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002474 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002475 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
2477 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2478
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002479 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002480 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2481 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002482 {
2483 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2484 ssl->state++;
2485 return( 0 );
2486 }
2487
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002489 ( ssl->authmode == SSL_VERIFY_NONE ||
2490 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002492 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2494 ssl->state++;
2495 return( 0 );
2496 }
2497
2498 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2499 {
2500 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2501 return( ret );
2502 }
2503
2504 ssl->state++;
2505
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002506#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 /*
2508 * Check if the client sent an empty certificate
2509 */
2510 if( ssl->endpoint == SSL_IS_SERVER &&
2511 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2512 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002513 if( ssl->in_msglen == 2 &&
2514 ssl->in_msgtype == SSL_MSG_ALERT &&
2515 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2516 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 {
2518 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2519
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002520 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2522 return( 0 );
2523 else
Paul Bakker40e46942009-01-03 21:51:57 +00002524 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 }
2526 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002528
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002529#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2530 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 if( ssl->endpoint == SSL_IS_SERVER &&
2532 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2533 {
2534 if( ssl->in_hslen == 7 &&
2535 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2536 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2537 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2538 {
2539 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2540
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002541 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002543 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002544 else
2545 return( 0 );
2546 }
2547 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002548#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2549 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002550
2551 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2552 {
2553 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002554 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 }
2556
2557 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2558 {
2559 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002560 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
2562
2563 /*
2564 * Same message structure as in ssl_write_certificate()
2565 */
2566 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2567
2568 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2569 {
2570 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002571 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 }
2573
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002574 /* In case we tried to reuse a session but it failed */
2575 if( ssl->session_negotiate->peer_cert != NULL )
2576 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002577 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002578 polarssl_free( ssl->session_negotiate->peer_cert );
2579 }
2580
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002581 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2582 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 {
2584 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002585 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002586 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 }
2588
Paul Bakkerb6b09562013-09-18 14:17:41 +02002589 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590
2591 i = 7;
2592
2593 while( i < ssl->in_hslen )
2594 {
2595 if( ssl->in_msg[i] != 0 )
2596 {
2597 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002598 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 }
2600
2601 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2602 | (unsigned int) ssl->in_msg[i + 2];
2603 i += 3;
2604
2605 if( n < 128 || i + n > ssl->in_hslen )
2606 {
2607 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002608 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
2610
Paul Bakkerddf26b42013-09-18 13:46:23 +02002611 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2612 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 if( ret != 0 )
2614 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002615 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 return( ret );
2617 }
2618
2619 i += n;
2620 }
2621
Paul Bakker48916f92012-09-16 19:57:18 +00002622 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002624 /*
2625 * On client, make sure the server cert doesn't change during renego to
2626 * avoid "triple handshake" attack: https://secure-resumption.com/
2627 */
2628 if( ssl->endpoint == SSL_IS_CLIENT &&
2629 ssl->renegotiation == SSL_RENEGOTIATION )
2630 {
2631 if( ssl->session->peer_cert == NULL )
2632 {
2633 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2634 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2635 }
2636
2637 if( ssl->session->peer_cert->raw.len !=
2638 ssl->session_negotiate->peer_cert->raw.len ||
2639 memcmp( ssl->session->peer_cert->raw.p,
2640 ssl->session_negotiate->peer_cert->raw.p,
2641 ssl->session->peer_cert->raw.len ) != 0 )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2645 }
2646 }
2647
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 if( ssl->authmode != SSL_VERIFY_NONE )
2649 {
2650 if( ssl->ca_chain == NULL )
2651 {
2652 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002653 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 }
2655
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002656 /*
2657 * Main check: verify certificate
2658 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002659 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2660 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2661 &ssl->session_negotiate->verify_result,
2662 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002663
2664 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002665 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002667 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002668
2669 /*
2670 * Secondary checks: always done, but change 'ret' only if it was 0
2671 */
2672
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002673#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002674 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002675 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002676
2677 /* If certificate uses an EC key, make sure the curve is OK */
2678 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2679 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2680 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002681 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002682 if( ret == 0 )
2683 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002684 }
2685 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002686#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002687
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002688 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2689 ciphersuite_info,
2690 ! ssl->endpoint ) != 0 )
2691 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002692 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002693 if( ret == 0 )
2694 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2695 }
2696
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2698 ret = 0;
2699 }
2700
2701 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2702
2703 return( ret );
2704}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002705#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2706 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2707 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2708 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2709 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2710 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2711 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
2713int ssl_write_change_cipher_spec( ssl_context *ssl )
2714{
2715 int ret;
2716
2717 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2718
2719 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2720 ssl->out_msglen = 1;
2721 ssl->out_msg[0] = 1;
2722
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 ssl->state++;
2724
2725 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2726 {
2727 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2728 return( ret );
2729 }
2730
2731 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2732
2733 return( 0 );
2734}
2735
2736int ssl_parse_change_cipher_spec( ssl_context *ssl )
2737{
2738 int ret;
2739
2740 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2741
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2743 {
2744 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2745 return( ret );
2746 }
2747
2748 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2749 {
2750 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002751 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 }
2753
2754 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2755 {
2756 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002757 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 }
2759
2760 ssl->state++;
2761
2762 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2763
2764 return( 0 );
2765}
2766
Paul Bakker41c83d32013-03-20 14:39:14 +01002767void ssl_optimize_checksum( ssl_context *ssl,
2768 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002769{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002770 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002771
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002772#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2773 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002774 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002775 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002776 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002777#endif
2778#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2779#if defined(POLARSSL_SHA512_C)
2780 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2781 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2782 else
2783#endif
2784#if defined(POLARSSL_SHA256_C)
2785 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002786 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002787 else
2788#endif
2789#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002790 {
2791 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002793 }
Paul Bakker380da532012-04-18 16:10:25 +00002794}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002795
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002796static void ssl_update_checksum_start( ssl_context *ssl,
2797 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002798{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2800 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002801 md5_update( &ssl->handshake->fin_md5 , buf, len );
2802 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803#endif
2804#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2805#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002806 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002807#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002808#if defined(POLARSSL_SHA512_C)
2809 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002810#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002811#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002812}
2813
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2815 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002816static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2817 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002818{
Paul Bakker48916f92012-09-16 19:57:18 +00002819 md5_update( &ssl->handshake->fin_md5 , buf, len );
2820 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002821}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002822#endif
Paul Bakker380da532012-04-18 16:10:25 +00002823
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002824#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2825#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002826static void ssl_update_checksum_sha256( ssl_context *ssl,
2827 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002828{
Paul Bakker9e36f042013-06-30 14:34:05 +02002829 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002830}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002831#endif
Paul Bakker380da532012-04-18 16:10:25 +00002832
Paul Bakker9e36f042013-06-30 14:34:05 +02002833#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002834static void ssl_update_checksum_sha384( ssl_context *ssl,
2835 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002836{
Paul Bakker9e36f042013-06-30 14:34:05 +02002837 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002838}
Paul Bakker769075d2012-11-24 11:26:46 +01002839#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002840#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002841
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002842#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002843static void ssl_calc_finished_ssl(
2844 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002845{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002846 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002847 md5_context md5;
2848 sha1_context sha1;
2849
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 unsigned char padbuf[48];
2851 unsigned char md5sum[16];
2852 unsigned char sha1sum[20];
2853
Paul Bakker48916f92012-09-16 19:57:18 +00002854 ssl_session *session = ssl->session_negotiate;
2855 if( !session )
2856 session = ssl->session;
2857
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2859
Paul Bakker48916f92012-09-16 19:57:18 +00002860 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2861 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
2863 /*
2864 * SSLv3:
2865 * hash =
2866 * MD5( master + pad2 +
2867 * MD5( handshake + sender + master + pad1 ) )
2868 * + SHA1( master + pad2 +
2869 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 */
2871
Paul Bakker90995b52013-06-24 19:20:35 +02002872#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Paul Bakker90995b52013-06-24 19:20:35 +02002877#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002879 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002880#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker3c2122f2013-06-24 19:03:14 +02002882 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2883 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
Paul Bakker3c2122f2013-06-24 19:03:14 +02002887 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002888 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002889 md5_update( &md5, padbuf, 48 );
2890 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker3c2122f2013-06-24 19:03:14 +02002892 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002893 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 sha1_update( &sha1, padbuf, 40 );
2895 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Paul Bakker1ef83d62012-04-11 12:09:53 +00002897 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002898
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002900 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002901 md5_update( &md5, padbuf, 48 );
2902 md5_update( &md5, md5sum, 16 );
2903 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002906 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 sha1_update( &sha1, padbuf , 40 );
2908 sha1_update( &sha1, sha1sum, 20 );
2909 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker1ef83d62012-04-11 12:09:53 +00002911 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912
Paul Bakker5b4af392014-06-26 12:09:34 +02002913 md5_free( &md5 );
2914 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakker34617722014-06-13 17:20:13 +02002916 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2917 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2918 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
2920 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2921}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002922#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925static void ssl_calc_finished_tls(
2926 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002928 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002929 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakker48916f92012-09-16 19:57:18 +00002934 ssl_session *session = ssl->session_negotiate;
2935 if( !session )
2936 session = ssl->session;
2937
Paul Bakker1ef83d62012-04-11 12:09:53 +00002938 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Paul Bakker48916f92012-09-16 19:57:18 +00002940 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2941 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002942
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943 /*
2944 * TLSv1:
2945 * hash = PRF( master, finished_label,
2946 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2947 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakker90995b52013-06-24 19:20:35 +02002949#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2951 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002952#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002953
Paul Bakker90995b52013-06-24 19:20:35 +02002954#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2956 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002957#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958
2959 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002960 ? "client finished"
2961 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
2963 md5_finish( &md5, padbuf );
2964 sha1_finish( &sha1, padbuf + 16 );
2965
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002966 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002967 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968
2969 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2970
Paul Bakker5b4af392014-06-26 12:09:34 +02002971 md5_free( &md5 );
2972 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973
Paul Bakker34617722014-06-13 17:20:13 +02002974 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975
2976 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2977}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002978#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002980#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2981#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002982static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983 ssl_context *ssl, unsigned char *buf, int from )
2984{
2985 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002986 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002987 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988 unsigned char padbuf[32];
2989
Paul Bakker48916f92012-09-16 19:57:18 +00002990 ssl_session *session = ssl->session_negotiate;
2991 if( !session )
2992 session = ssl->session;
2993
Paul Bakker380da532012-04-18 16:10:25 +00002994 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
Paul Bakker9e36f042013-06-30 14:34:05 +02002996 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002997
2998 /*
2999 * TLSv1.2:
3000 * hash = PRF( master, finished_label,
3001 * Hash( handshake ) )[0.11]
3002 */
3003
Paul Bakker9e36f042013-06-30 14:34:05 +02003004#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003006 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003007#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003008
3009 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003010 ? "client finished"
3011 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003012
Paul Bakker9e36f042013-06-30 14:34:05 +02003013 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003015 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003016 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017
3018 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3019
Paul Bakker5b4af392014-06-26 12:09:34 +02003020 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021
Paul Bakker34617722014-06-13 17:20:13 +02003022 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003023
3024 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3025}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003026#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003027
Paul Bakker9e36f042013-06-30 14:34:05 +02003028#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003029static void ssl_calc_finished_tls_sha384(
3030 ssl_context *ssl, unsigned char *buf, int from )
3031{
3032 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003033 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003034 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003035 unsigned char padbuf[48];
3036
Paul Bakker48916f92012-09-16 19:57:18 +00003037 ssl_session *session = ssl->session_negotiate;
3038 if( !session )
3039 session = ssl->session;
3040
Paul Bakker380da532012-04-18 16:10:25 +00003041 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003042
Paul Bakker9e36f042013-06-30 14:34:05 +02003043 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003044
3045 /*
3046 * TLSv1.2:
3047 * hash = PRF( master, finished_label,
3048 * Hash( handshake ) )[0.11]
3049 */
3050
Paul Bakker9e36f042013-06-30 14:34:05 +02003051#if !defined(POLARSSL_SHA512_ALT)
3052 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3053 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003054#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003055
3056 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003057 ? "client finished"
3058 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003059
Paul Bakker9e36f042013-06-30 14:34:05 +02003060 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003061
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003062 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003063 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003064
3065 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3066
Paul Bakker5b4af392014-06-26 12:09:34 +02003067 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003068
Paul Bakker34617722014-06-13 17:20:13 +02003069 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003070
3071 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3072}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003073#endif /* POLARSSL_SHA512_C */
3074#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003075
Paul Bakker48916f92012-09-16 19:57:18 +00003076void ssl_handshake_wrapup( ssl_context *ssl )
3077{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003078 int resume = ssl->handshake->resume;
3079
Paul Bakker48916f92012-09-16 19:57:18 +00003080 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3081
3082 /*
3083 * Free our handshake params
3084 */
3085 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003086 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003087 ssl->handshake = NULL;
3088
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003089 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003090 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003091 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003092 ssl->renego_records_seen = 0;
3093 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003094
Paul Bakker48916f92012-09-16 19:57:18 +00003095 /*
3096 * Switch in our now active transform context
3097 */
3098 if( ssl->transform )
3099 {
3100 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003101 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003102 }
3103 ssl->transform = ssl->transform_negotiate;
3104 ssl->transform_negotiate = NULL;
3105
Paul Bakker0a597072012-09-25 21:55:46 +00003106 if( ssl->session )
3107 {
3108 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003109 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003110 }
3111 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003112 ssl->session_negotiate = NULL;
3113
Paul Bakker0a597072012-09-25 21:55:46 +00003114 /*
3115 * Add cache entry
3116 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003117 if( ssl->f_set_cache != NULL &&
3118 ssl->session->length != 0 &&
3119 resume == 0 )
3120 {
Paul Bakker0a597072012-09-25 21:55:46 +00003121 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3122 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003123 }
Paul Bakker0a597072012-09-25 21:55:46 +00003124
Paul Bakker48916f92012-09-16 19:57:18 +00003125 ssl->state++;
3126
3127 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3128}
3129
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130int ssl_write_finished( ssl_context *ssl )
3131{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003132 int ret, hash_len, i;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133
3134 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3135
Paul Bakker92be97b2013-01-02 17:30:03 +01003136 /*
3137 * Set the out_msg pointer to the correct location based on IV length
3138 */
3139 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3140 {
3141 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3142 ssl->transform_negotiate->fixed_ivlen;
3143 }
3144 else
3145 ssl->out_msg = ssl->out_iv;
3146
Paul Bakker48916f92012-09-16 19:57:18 +00003147 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148
3149 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003150 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3151
Paul Bakker48916f92012-09-16 19:57:18 +00003152 ssl->verify_data_len = hash_len;
3153 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3154
Paul Bakker5121ce52009-01-03 21:22:43 +00003155 ssl->out_msglen = 4 + hash_len;
3156 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3157 ssl->out_msg[0] = SSL_HS_FINISHED;
3158
3159 /*
3160 * In case of session resuming, invert the client and server
3161 * ChangeCipherSpec messages order.
3162 */
Paul Bakker0a597072012-09-25 21:55:46 +00003163 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 {
3165 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003166 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 else
3168 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3169 }
3170 else
3171 ssl->state++;
3172
Paul Bakker48916f92012-09-16 19:57:18 +00003173 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003174 * Switch to our negotiated transform and session parameters for outbound
3175 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003176 */
3177 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3178 ssl->transform_out = ssl->transform_negotiate;
3179 ssl->session_out = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003180
3181 memset( ssl->out_ctr + ssl_ep_len( ssl ), 0, 8 - ssl_ep_len( ssl ) );
3182 for( i = ssl_ep_len( ssl ); i > 0; i-- )
3183 if( ++ssl->out_ctr[i - 1] != 0 )
3184 break;
3185 // TODO: abort on epoch wrap!
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Paul Bakker07eb38b2012-12-19 14:42:06 +01003187#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003188 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003189 {
3190 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3191 {
3192 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3193 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3194 }
3195 }
3196#endif
3197
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3199 {
3200 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3201 return( ret );
3202 }
3203
3204 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3205
3206 return( 0 );
3207}
3208
3209int ssl_parse_finished( ssl_context *ssl )
3210{
Paul Bakker23986e52011-04-24 08:57:21 +00003211 int ret;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003212 unsigned int hash_len, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 unsigned char buf[36];
3214
3215 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3216
Paul Bakker48916f92012-09-16 19:57:18 +00003217 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003220 * Switch to our negotiated transform and session parameters for inbound
3221 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003222 */
3223 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3224 ssl->transform_in = ssl->transform_negotiate;
3225 ssl->session_in = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003226
3227 /* Input counter/epoch not used with DTLS right now,
3228 * but it doesn't hurt to have this part ready */
3229 memset( ssl->in_ctr + ssl_ep_len( ssl ), 0, 8 - ssl_ep_len( ssl ) );
3230 for( i = ssl_ep_len( ssl ); i > 0; i-- )
3231 if( ++ssl->in_ctr[i - 1] != 0 )
3232 break;
3233 // TODO: abort on epoch wrap!
Paul Bakker5121ce52009-01-03 21:22:43 +00003234
Paul Bakker92be97b2013-01-02 17:30:03 +01003235 /*
3236 * Set the in_msg pointer to the correct location based on IV length
3237 */
3238 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3239 {
3240 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3241 ssl->transform_negotiate->fixed_ivlen;
3242 }
3243 else
3244 ssl->in_msg = ssl->in_iv;
3245
Paul Bakker07eb38b2012-12-19 14:42:06 +01003246#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003247 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003248 {
3249 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3250 {
3251 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3252 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3253 }
3254 }
3255#endif
3256
Paul Bakker5121ce52009-01-03 21:22:43 +00003257 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3258 {
3259 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3260 return( ret );
3261 }
3262
3263 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3264 {
3265 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003266 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003267 }
3268
Paul Bakker1ef83d62012-04-11 12:09:53 +00003269 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3271
3272 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3273 ssl->in_hslen != 4 + hash_len )
3274 {
3275 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003276 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 }
3278
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003279 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 {
3281 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003282 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003283 }
3284
Paul Bakker48916f92012-09-16 19:57:18 +00003285 ssl->verify_data_len = hash_len;
3286 memcpy( ssl->peer_verify_data, buf, hash_len );
3287
Paul Bakker0a597072012-09-25 21:55:46 +00003288 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 {
3290 if( ssl->endpoint == SSL_IS_CLIENT )
3291 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3292
3293 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003294 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 }
3296 else
3297 ssl->state++;
3298
3299 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3300
3301 return( 0 );
3302}
3303
Paul Bakker968afaa2014-07-09 11:09:24 +02003304static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003305{
3306 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3307
3308#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3309 defined(POLARSSL_SSL_PROTO_TLS1_1)
3310 md5_init( &handshake->fin_md5 );
3311 sha1_init( &handshake->fin_sha1 );
3312 md5_starts( &handshake->fin_md5 );
3313 sha1_starts( &handshake->fin_sha1 );
3314#endif
3315#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3316#if defined(POLARSSL_SHA256_C)
3317 sha256_init( &handshake->fin_sha256 );
3318 sha256_starts( &handshake->fin_sha256, 0 );
3319#endif
3320#if defined(POLARSSL_SHA512_C)
3321 sha512_init( &handshake->fin_sha512 );
3322 sha512_starts( &handshake->fin_sha512, 1 );
3323#endif
3324#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3325
3326 handshake->update_checksum = ssl_update_checksum_start;
3327 handshake->sig_alg = SSL_HASH_SHA1;
3328
3329#if defined(POLARSSL_DHM_C)
3330 dhm_init( &handshake->dhm_ctx );
3331#endif
3332#if defined(POLARSSL_ECDH_C)
3333 ecdh_init( &handshake->ecdh_ctx );
3334#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003335}
3336
3337static void ssl_transform_init( ssl_transform *transform )
3338{
3339 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003340
3341 cipher_init( &transform->cipher_ctx_enc );
3342 cipher_init( &transform->cipher_ctx_dec );
3343
3344 md_init( &transform->md_ctx_enc );
3345 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003346}
3347
3348void ssl_session_init( ssl_session *session )
3349{
3350 memset( session, 0, sizeof(ssl_session) );
3351}
3352
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003353static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003354{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003355 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003356 if( ssl->transform_negotiate )
3357 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003358 if( ssl->session_negotiate )
3359 ssl_session_free( ssl->session_negotiate );
3360 if( ssl->handshake )
3361 ssl_handshake_free( ssl->handshake );
3362
3363 /*
3364 * Either the pointers are now NULL or cleared properly and can be freed.
3365 * Now allocate missing structures.
3366 */
3367 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003368 {
3369 ssl->transform_negotiate =
3370 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3371 }
Paul Bakker48916f92012-09-16 19:57:18 +00003372
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003373 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003374 {
3375 ssl->session_negotiate =
3376 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3377 }
Paul Bakker48916f92012-09-16 19:57:18 +00003378
Paul Bakker82788fb2014-10-20 13:59:19 +02003379 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003380 {
3381 ssl->handshake = (ssl_handshake_params *)
3382 polarssl_malloc( sizeof(ssl_handshake_params) );
3383 }
Paul Bakker48916f92012-09-16 19:57:18 +00003384
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003385 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003386 if( ssl->handshake == NULL ||
3387 ssl->transform_negotiate == NULL ||
3388 ssl->session_negotiate == NULL )
3389 {
3390 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003391
3392 polarssl_free( ssl->handshake );
3393 polarssl_free( ssl->transform_negotiate );
3394 polarssl_free( ssl->session_negotiate );
3395
3396 ssl->handshake = NULL;
3397 ssl->transform_negotiate = NULL;
3398 ssl->session_negotiate = NULL;
3399
Paul Bakker48916f92012-09-16 19:57:18 +00003400 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3401 }
3402
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003403 /* Initialize structures */
3404 ssl_session_init( ssl->session_negotiate );
3405 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003406 ssl_handshake_params_init( ssl->handshake );
3407
3408#if defined(POLARSSL_X509_CRT_PARSE_C)
3409 ssl->handshake->key_cert = ssl->key_cert;
3410#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003411
Paul Bakker48916f92012-09-16 19:57:18 +00003412 return( 0 );
3413}
3414
Paul Bakker5121ce52009-01-03 21:22:43 +00003415/*
3416 * Initialize an SSL context
3417 */
3418int ssl_init( ssl_context *ssl )
3419{
Paul Bakker48916f92012-09-16 19:57:18 +00003420 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 int len = SSL_BUFFER_LEN;
3422
3423 memset( ssl, 0, sizeof( ssl_context ) );
3424
Paul Bakker62f2dee2012-09-28 07:31:51 +00003425 /*
3426 * Sane defaults
3427 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003428 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3429 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3430 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3431 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003432
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003433 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003434
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003435 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3436
Paul Bakker62f2dee2012-09-28 07:31:51 +00003437#if defined(POLARSSL_DHM_C)
3438 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3439 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3440 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3441 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3442 {
3443 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3444 return( ret );
3445 }
3446#endif
3447
3448 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003449 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003450 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003451 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003452 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003454 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 {
3456 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003457 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003458 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003459 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003460 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003461 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 }
3463
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003464 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
3465 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00003466
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003467 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
3468 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
3469
Paul Bakker606b4ba2013-08-14 16:52:14 +02003470#if defined(POLARSSL_SSL_SESSION_TICKETS)
3471 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3472#endif
3473
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003474#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003475 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003476#endif
3477
Paul Bakker48916f92012-09-16 19:57:18 +00003478 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3479 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003480
3481 return( 0 );
3482}
3483
3484/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485 * Reset an initialized and used SSL context for re-use while retaining
3486 * all application-set variables, function pointers and data.
3487 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003488int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003489{
Paul Bakker48916f92012-09-16 19:57:18 +00003490 int ret;
3491
Paul Bakker7eb013f2011-10-06 12:37:39 +00003492 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003493 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3494 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3495
3496 ssl->verify_data_len = 0;
3497 memset( ssl->own_verify_data, 0, 36 );
3498 memset( ssl->peer_verify_data, 0, 36 );
3499
Paul Bakker7eb013f2011-10-06 12:37:39 +00003500 ssl->in_offt = NULL;
3501
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003502 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003503 ssl->in_msgtype = 0;
3504 ssl->in_msglen = 0;
3505 ssl->in_left = 0;
3506
3507 ssl->in_hslen = 0;
3508 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003509 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003510
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003511 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003512 ssl->out_msgtype = 0;
3513 ssl->out_msglen = 0;
3514 ssl->out_left = 0;
3515
Paul Bakker48916f92012-09-16 19:57:18 +00003516 ssl->transform_in = NULL;
3517 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003518
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003519 ssl->renego_records_seen = 0;
3520
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003521 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
3522 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003523
3524#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003525 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003526 {
3527 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003528 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003529 {
3530 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3531 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3532 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003533 }
3534#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003535
Paul Bakker48916f92012-09-16 19:57:18 +00003536 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003537 {
Paul Bakker48916f92012-09-16 19:57:18 +00003538 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003539 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003540 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003541 }
Paul Bakker48916f92012-09-16 19:57:18 +00003542
Paul Bakkerc0463502013-02-14 11:19:38 +01003543 if( ssl->session )
3544 {
3545 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003546 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003547 ssl->session = NULL;
3548 }
3549
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003550#if defined(POLARSSL_SSL_ALPN)
3551 ssl->alpn_chosen = NULL;
3552#endif
3553
Paul Bakker48916f92012-09-16 19:57:18 +00003554 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3555 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003556
3557 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003558}
3559
Paul Bakkera503a632013-08-14 13:48:06 +02003560#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003561static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3562{
3563 aes_free( &tkeys->enc );
3564 aes_free( &tkeys->dec );
3565
3566 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3567}
3568
Paul Bakker7eb013f2011-10-06 12:37:39 +00003569/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003570 * Allocate and initialize ticket keys
3571 */
3572static int ssl_ticket_keys_init( ssl_context *ssl )
3573{
3574 int ret;
3575 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003576 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003577
3578 if( ssl->ticket_keys != NULL )
3579 return( 0 );
3580
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003581 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3582 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003583 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3584
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003585 aes_init( &tkeys->enc );
3586 aes_init( &tkeys->dec );
3587
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003588 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003589 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003590 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003591 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003592 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003593 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003594
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003595 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3596 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3597 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3598 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003599 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003600 polarssl_free( tkeys );
3601 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003602 }
3603
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003604 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003605 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003606 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003607 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003608 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003609 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003610
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003611 ssl->ticket_keys = tkeys;
3612
3613 return( 0 );
3614}
Paul Bakkera503a632013-08-14 13:48:06 +02003615#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003616
3617/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003618 * SSL set accessors
3619 */
3620void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3621{
3622 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003623
Paul Bakker606b4ba2013-08-14 16:52:14 +02003624#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003625 if( endpoint == SSL_IS_CLIENT )
3626 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003627#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003628}
3629
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003630int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003631{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003632#if defined(POLARSSL_SSL_PROTO_DTLS)
3633 if( transport == SSL_TRANSPORT_DATAGRAM )
3634 {
3635 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003636
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003637 ssl->out_hdr = ssl->out_buf;
3638 ssl->out_ctr = ssl->out_buf + 3;
3639 ssl->out_len = ssl->out_buf + 11;
3640 ssl->out_iv = ssl->out_buf + 13;
3641 ssl->out_msg = ssl->out_buf + 13;
3642
3643 ssl->in_hdr = ssl->in_buf;
3644 ssl->in_ctr = ssl->in_buf + 3;
3645 ssl->in_len = ssl->in_buf + 11;
3646 ssl->in_iv = ssl->in_buf + 13;
3647 ssl->in_msg = ssl->in_buf + 13;
3648
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003649 /* DTLS starts with TLS1.1 */
3650 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
3651 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003652
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003653 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
3654 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
3655
3656 return( 0 );
3657 }
3658#endif
3659
3660 if( transport == SSL_TRANSPORT_STREAM )
3661 {
3662 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003663
3664 ssl->out_ctr = ssl->out_buf;
3665 ssl->out_hdr = ssl->out_buf + 8;
3666 ssl->out_len = ssl->out_buf + 11;
3667 ssl->out_iv = ssl->out_buf + 13;
3668 ssl->out_msg = ssl->out_buf + 13;
3669
3670 ssl->in_ctr = ssl->in_buf;
3671 ssl->in_hdr = ssl->in_buf + 8;
3672 ssl->in_len = ssl->in_buf + 11;
3673 ssl->in_iv = ssl->in_buf + 13;
3674 ssl->in_msg = ssl->in_buf + 13;
3675
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003676 return( 0 );
3677 }
3678
3679 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003680}
3681
Paul Bakker5121ce52009-01-03 21:22:43 +00003682void ssl_set_authmode( ssl_context *ssl, int authmode )
3683{
3684 ssl->authmode = authmode;
3685}
3686
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003687#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003688void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003689 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003690 void *p_vrfy )
3691{
3692 ssl->f_vrfy = f_vrfy;
3693 ssl->p_vrfy = p_vrfy;
3694}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003695#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003696
Paul Bakker5121ce52009-01-03 21:22:43 +00003697void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003698 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 void *p_rng )
3700{
3701 ssl->f_rng = f_rng;
3702 ssl->p_rng = p_rng;
3703}
3704
3705void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003706 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 void *p_dbg )
3708{
3709 ssl->f_dbg = f_dbg;
3710 ssl->p_dbg = p_dbg;
3711}
3712
3713void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003714 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003715 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003716{
3717 ssl->f_recv = f_recv;
3718 ssl->f_send = f_send;
3719 ssl->p_recv = p_recv;
3720 ssl->p_send = p_send;
3721}
3722
Paul Bakker0a597072012-09-25 21:55:46 +00003723void ssl_set_session_cache( ssl_context *ssl,
3724 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3725 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003726{
Paul Bakker0a597072012-09-25 21:55:46 +00003727 ssl->f_get_cache = f_get_cache;
3728 ssl->p_get_cache = p_get_cache;
3729 ssl->f_set_cache = f_set_cache;
3730 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731}
3732
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003733int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003734{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003735 int ret;
3736
3737 if( ssl == NULL ||
3738 session == NULL ||
3739 ssl->session_negotiate == NULL ||
3740 ssl->endpoint != SSL_IS_CLIENT )
3741 {
3742 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3743 }
3744
3745 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3746 return( ret );
3747
Paul Bakker0a597072012-09-25 21:55:46 +00003748 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003749
3750 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003751}
3752
Paul Bakkerb68cad62012-08-23 08:34:18 +00003753void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003754{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003755 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3756 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3757 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3758 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3759}
3760
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003761void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3762 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003763 int major, int minor )
3764{
3765 if( major != SSL_MAJOR_VERSION_3 )
3766 return;
3767
3768 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3769 return;
3770
3771 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003772}
3773
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003774#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003775/* Add a new (empty) key_cert entry an return a pointer to it */
3776static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3777{
3778 ssl_key_cert *key_cert, *last;
3779
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003780 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3781 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003782 return( NULL );
3783
3784 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3785
3786 /* Append the new key_cert to the (possibly empty) current list */
3787 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003788 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003789 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003790 if( ssl->handshake != NULL )
3791 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003792 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003793 else
3794 {
3795 last = ssl->key_cert;
3796 while( last->next != NULL )
3797 last = last->next;
3798 last->next = key_cert;
3799 }
3800
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003801 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003802}
3803
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003804void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003805 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003806{
3807 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003808 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 ssl->peer_cn = peer_cn;
3810}
3811
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003812int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003813 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003814{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003815 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3816
3817 if( key_cert == NULL )
3818 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3819
3820 key_cert->cert = own_cert;
3821 key_cert->key = pk_key;
3822
3823 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003824}
3825
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003826#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003827int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003828 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003829{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003830 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003831 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003832
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003833 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003834 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3835
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003836 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3837 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003838 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003839
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003840 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003841
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003842 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003843 if( ret != 0 )
3844 return( ret );
3845
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003846 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003847 return( ret );
3848
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003849 key_cert->cert = own_cert;
3850 key_cert->key_own_alloc = 1;
3851
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003852 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003853}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003854#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003855
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003856int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003857 void *rsa_key,
3858 rsa_decrypt_func rsa_decrypt,
3859 rsa_sign_func rsa_sign,
3860 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003861{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003862 int ret;
3863 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003864
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003865 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003866 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3867
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003868 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3869 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003870 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003871
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003872 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003873
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003874 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3875 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3876 return( ret );
3877
3878 key_cert->cert = own_cert;
3879 key_cert->key_own_alloc = 1;
3880
3881 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003882}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003883#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003884
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003885#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003886int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3887 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003888{
Paul Bakker6db455e2013-09-18 17:29:31 +02003889 if( psk == NULL || psk_identity == NULL )
3890 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3891
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003892 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003893 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3894
Paul Bakker6db455e2013-09-18 17:29:31 +02003895 if( ssl->psk != NULL )
3896 {
3897 polarssl_free( ssl->psk );
3898 polarssl_free( ssl->psk_identity );
3899 }
3900
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003901 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003902 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003903
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003904 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003905 ssl->psk_identity = (unsigned char *)
3906 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003907
3908 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3909 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3910
3911 memcpy( ssl->psk, psk, ssl->psk_len );
3912 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003913
3914 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003915}
3916
3917void ssl_set_psk_cb( ssl_context *ssl,
3918 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3919 size_t),
3920 void *p_psk )
3921{
3922 ssl->f_psk = f_psk;
3923 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003924}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003925#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003926
Paul Bakker48916f92012-09-16 19:57:18 +00003927#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003928int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003929{
3930 int ret;
3931
Paul Bakker48916f92012-09-16 19:57:18 +00003932 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003933 {
3934 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3935 return( ret );
3936 }
3937
Paul Bakker48916f92012-09-16 19:57:18 +00003938 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939 {
3940 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3941 return( ret );
3942 }
3943
3944 return( 0 );
3945}
3946
Paul Bakker1b57b062011-01-06 15:48:19 +00003947int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3948{
3949 int ret;
3950
Paul Bakker66d5d072014-06-17 16:39:18 +02003951 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003952 {
3953 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3954 return( ret );
3955 }
3956
Paul Bakker66d5d072014-06-17 16:39:18 +02003957 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003958 {
3959 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3960 return( ret );
3961 }
3962
3963 return( 0 );
3964}
Paul Bakker48916f92012-09-16 19:57:18 +00003965#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003966
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003967#if defined(POLARSSL_SSL_SET_CURVES)
3968/*
3969 * Set the allowed elliptic curves
3970 */
3971void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3972{
3973 ssl->curve_list = curve_list;
3974}
3975#endif
3976
Paul Bakker0be444a2013-08-27 21:55:01 +02003977#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003978int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979{
3980 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003982
3983 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003984
3985 if( ssl->hostname_len + 1 == 0 )
3986 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3987
Paul Bakker6e339b52013-07-03 13:37:05 +02003988 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003989
Paul Bakkerb15b8512012-01-13 13:44:06 +00003990 if( ssl->hostname == NULL )
3991 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3992
Paul Bakker3c2122f2013-06-24 19:03:14 +02003993 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003994 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003995
Paul Bakker40ea7de2009-05-03 10:18:48 +00003996 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003997
3998 return( 0 );
3999}
4000
Paul Bakker5701cdc2012-09-27 21:49:42 +00004001void ssl_set_sni( ssl_context *ssl,
4002 int (*f_sni)(void *, ssl_context *,
4003 const unsigned char *, size_t),
4004 void *p_sni )
4005{
4006 ssl->f_sni = f_sni;
4007 ssl->p_sni = p_sni;
4008}
Paul Bakker0be444a2013-08-27 21:55:01 +02004009#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004010
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004011#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004012int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004013{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004014 size_t cur_len, tot_len;
4015 const char **p;
4016
4017 /*
4018 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4019 * truncated". Check lengths now rather than later.
4020 */
4021 tot_len = 0;
4022 for( p = protos; *p != NULL; p++ )
4023 {
4024 cur_len = strlen( *p );
4025 tot_len += cur_len;
4026
4027 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4028 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4029 }
4030
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004031 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004032
4033 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004034}
4035
4036const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4037{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004038 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004039}
4040#endif /* POLARSSL_SSL_ALPN */
4041
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004042static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00004043{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004044 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
4045 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
4046 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4047 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004048 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004049 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004050 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004051
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004052 return( 0 );
4053}
4054
4055int ssl_set_max_version( ssl_context *ssl, int major, int minor )
4056{
4057 if( ssl_check_version( ssl, major, minor ) != 0 )
4058 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4059
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004060 ssl->max_major_ver = major;
4061 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004062
4063 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00004064}
4065
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004066int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00004067{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004068 if( ssl_check_version( ssl, major, minor ) != 0 )
4069 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004070
4071 ssl->min_major_ver = major;
4072 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004073
4074 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00004075}
4076
Paul Bakker05decb22013-08-15 13:33:48 +02004077#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004078int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4079{
Paul Bakker77e257e2013-12-16 15:29:52 +01004080 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004081 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004082 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004083 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004084 }
4085
4086 ssl->mfl_code = mfl_code;
4087
4088 return( 0 );
4089}
Paul Bakker05decb22013-08-15 13:33:48 +02004090#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004091
Paul Bakker1f2bc622013-08-15 13:45:55 +02004092#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004093int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004094{
4095 if( ssl->endpoint != SSL_IS_CLIENT )
4096 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4097
Paul Bakker8c1ede62013-07-19 14:14:37 +02004098 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004099
4100 return( 0 );
4101}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004102#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004103
Paul Bakker48916f92012-09-16 19:57:18 +00004104void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4105{
4106 ssl->disable_renegotiation = renegotiation;
4107}
4108
4109void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4110{
4111 ssl->allow_legacy_renegotiation = allow_legacy;
4112}
4113
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004114void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4115{
4116 ssl->renego_max_records = max_records;
4117}
4118
Paul Bakkera503a632013-08-14 13:48:06 +02004119#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004120int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4121{
4122 ssl->session_tickets = use_tickets;
4123
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004124 if( ssl->endpoint == SSL_IS_CLIENT )
4125 return( 0 );
4126
4127 if( ssl->f_rng == NULL )
4128 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4129
4130 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004131}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004132
4133void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4134{
4135 ssl->ticket_lifetime = lifetime;
4136}
Paul Bakkera503a632013-08-14 13:48:06 +02004137#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004138
Paul Bakker5121ce52009-01-03 21:22:43 +00004139/*
4140 * SSL get accessors
4141 */
Paul Bakker23986e52011-04-24 08:57:21 +00004142size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004143{
4144 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4145}
4146
Paul Bakkerff60ee62010-03-16 21:09:09 +00004147int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004148{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004149 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004150}
4151
Paul Bakkere3166ce2011-01-27 17:40:50 +00004152const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004153{
Paul Bakker926c8e42013-03-06 10:23:34 +01004154 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004155 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004156
Paul Bakkere3166ce2011-01-27 17:40:50 +00004157 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004158}
4159
Paul Bakker43ca69c2011-01-15 17:35:19 +00004160const char *ssl_get_version( const ssl_context *ssl )
4161{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004162#if defined(POLARSSL_SSL_PROTO_DTLS)
4163 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4164 {
4165 switch( ssl->minor_ver )
4166 {
4167 case SSL_MINOR_VERSION_2:
4168 return( "DTLSv1.0" );
4169
4170 case SSL_MINOR_VERSION_3:
4171 return( "DTLSv1.2" );
4172
4173 default:
4174 return( "unknown (DTLS)" );
4175 }
4176 }
4177#endif
4178
Paul Bakker43ca69c2011-01-15 17:35:19 +00004179 switch( ssl->minor_ver )
4180 {
4181 case SSL_MINOR_VERSION_0:
4182 return( "SSLv3.0" );
4183
4184 case SSL_MINOR_VERSION_1:
4185 return( "TLSv1.0" );
4186
4187 case SSL_MINOR_VERSION_2:
4188 return( "TLSv1.1" );
4189
Paul Bakker1ef83d62012-04-11 12:09:53 +00004190 case SSL_MINOR_VERSION_3:
4191 return( "TLSv1.2" );
4192
Paul Bakker43ca69c2011-01-15 17:35:19 +00004193 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004194 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00004195 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00004196}
4197
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004198#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004199const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004200{
4201 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004202 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004203
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004204 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004205}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004206#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004207
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004208int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4209{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004210 if( ssl == NULL ||
4211 dst == NULL ||
4212 ssl->session == NULL ||
4213 ssl->endpoint != SSL_IS_CLIENT )
4214 {
4215 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4216 }
4217
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004218 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004219}
4220
Paul Bakker5121ce52009-01-03 21:22:43 +00004221/*
Paul Bakker1961b702013-01-25 14:49:24 +01004222 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004223 */
Paul Bakker1961b702013-01-25 14:49:24 +01004224int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004225{
Paul Bakker40e46942009-01-03 21:51:57 +00004226 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004227
Paul Bakker40e46942009-01-03 21:51:57 +00004228#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004229 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004230 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004231#endif
4232
Paul Bakker40e46942009-01-03 21:51:57 +00004233#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004235 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004236#endif
4237
Paul Bakker1961b702013-01-25 14:49:24 +01004238 return( ret );
4239}
4240
4241/*
4242 * Perform the SSL handshake
4243 */
4244int ssl_handshake( ssl_context *ssl )
4245{
4246 int ret = 0;
4247
4248 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4249
4250 while( ssl->state != SSL_HANDSHAKE_OVER )
4251 {
4252 ret = ssl_handshake_step( ssl );
4253
4254 if( ret != 0 )
4255 break;
4256 }
4257
Paul Bakker5121ce52009-01-03 21:22:43 +00004258 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4259
4260 return( ret );
4261}
4262
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004263#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004264/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004265 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004266 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004267static int ssl_write_hello_request( ssl_context *ssl )
4268{
4269 int ret;
4270
4271 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4272
4273 ssl->out_msglen = 4;
4274 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4275 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4276
4277 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4278 {
4279 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4280 return( ret );
4281 }
4282
4283 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4284
4285 return( 0 );
4286}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004287#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004288
4289/*
4290 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004291 * - any side: calling ssl_renegotiate(),
4292 * - client: receiving a HelloRequest during ssl_read(),
4293 * - server: receiving any handshake message on server during ssl_read() after
4294 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004295 * If the handshake doesn't complete due to waiting for I/O, it will continue
4296 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004297 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004298static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004299{
4300 int ret;
4301
4302 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4303
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004304 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4305 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004306
4307 ssl->state = SSL_HELLO_REQUEST;
4308 ssl->renegotiation = SSL_RENEGOTIATION;
4309
Paul Bakker48916f92012-09-16 19:57:18 +00004310 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4311 {
4312 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4313 return( ret );
4314 }
4315
4316 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4317
4318 return( 0 );
4319}
4320
4321/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004322 * Renegotiate current connection on client,
4323 * or request renegotiation on server
4324 */
4325int ssl_renegotiate( ssl_context *ssl )
4326{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004327 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004328
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004329#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004330 /* On server, just send the request */
4331 if( ssl->endpoint == SSL_IS_SERVER )
4332 {
4333 if( ssl->state != SSL_HANDSHAKE_OVER )
4334 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4335
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004336 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4337
4338 /* Did we already try/start sending HelloRequest? */
4339 if( ssl->out_left != 0 )
4340 return( ssl_flush_output( ssl ) );
4341
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004342 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004343 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004344#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004345
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004346#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004347 /*
4348 * On client, either start the renegotiation process or,
4349 * if already in progress, continue the handshake
4350 */
4351 if( ssl->renegotiation != SSL_RENEGOTIATION )
4352 {
4353 if( ssl->state != SSL_HANDSHAKE_OVER )
4354 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4355
4356 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4357 {
4358 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4359 return( ret );
4360 }
4361 }
4362 else
4363 {
4364 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4365 {
4366 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4367 return( ret );
4368 }
4369 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004370#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004371
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004372 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004373}
4374
4375/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004376 * Receive application data decrypted from the SSL layer
4377 */
Paul Bakker23986e52011-04-24 08:57:21 +00004378int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004379{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004380 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004381 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004382
4383 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4384
4385 if( ssl->state != SSL_HANDSHAKE_OVER )
4386 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004387 ret = ssl_handshake( ssl );
4388 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4389 {
4390 record_read = 1;
4391 }
4392 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004393 {
4394 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4395 return( ret );
4396 }
4397 }
4398
4399 if( ssl->in_offt == NULL )
4400 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004401 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004402 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004403 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4404 {
4405 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4406 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004407
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004408 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4409 return( ret );
4410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 }
4412
4413 if( ssl->in_msglen == 0 &&
4414 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4415 {
4416 /*
4417 * OpenSSL sends empty messages to randomize the IV
4418 */
4419 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4420 {
Paul Bakker831a7552011-05-18 13:32:51 +00004421 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4422 return( 0 );
4423
Paul Bakker5121ce52009-01-03 21:22:43 +00004424 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4425 return( ret );
4426 }
4427 }
4428
Paul Bakker48916f92012-09-16 19:57:18 +00004429 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4430 {
4431 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4432
4433 if( ssl->endpoint == SSL_IS_CLIENT &&
4434 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4435 ssl->in_hslen != 4 ) )
4436 {
4437 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4438 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4439 }
4440
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004441 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4442 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004443 ssl->allow_legacy_renegotiation ==
4444 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004445 {
4446 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4447
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004448#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004449 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004450 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004451 /*
4452 * SSLv3 does not have a "no_renegotiation" alert
4453 */
4454 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4455 return( ret );
4456 }
4457 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004458#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004459#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4460 defined(POLARSSL_SSL_PROTO_TLS1_2)
4461 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004462 {
4463 if( ( ret = ssl_send_alert_message( ssl,
4464 SSL_ALERT_LEVEL_WARNING,
4465 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4466 {
4467 return( ret );
4468 }
Paul Bakker48916f92012-09-16 19:57:18 +00004469 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004470 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004471#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4472 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004473 {
4474 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004475 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004476 }
Paul Bakker48916f92012-09-16 19:57:18 +00004477 }
4478 else
4479 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004480 ret = ssl_start_renegotiation( ssl );
4481 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4482 {
4483 record_read = 1;
4484 }
4485 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004486 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004487 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004488 return( ret );
4489 }
Paul Bakker48916f92012-09-16 19:57:18 +00004490 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004491
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004492 /* If a non-handshake record was read during renego, fallthrough,
4493 * else tell the user they should call ssl_read() again */
4494 if( ! record_read )
4495 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004496 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004497 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4498 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004499 ssl->renego_records_seen++;
4500
4501 if( ssl->renego_max_records >= 0 &&
4502 ssl->renego_records_seen > ssl->renego_max_records )
4503 {
4504 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4505 "but not honored by client" ) );
4506 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4507 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004508 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004509
4510 /* Fatal and closure alerts handled by ssl_read_record() */
4511 if( ssl->in_msgtype == SSL_MSG_ALERT )
4512 {
4513 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4514 return( POLARSSL_ERR_NET_WANT_READ );
4515 }
4516
4517 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004518 {
4519 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004520 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 }
4522
4523 ssl->in_offt = ssl->in_msg;
4524 }
4525
4526 n = ( len < ssl->in_msglen )
4527 ? len : ssl->in_msglen;
4528
4529 memcpy( buf, ssl->in_offt, n );
4530 ssl->in_msglen -= n;
4531
4532 if( ssl->in_msglen == 0 )
4533 /* all bytes consumed */
4534 ssl->in_offt = NULL;
4535 else
4536 /* more data available */
4537 ssl->in_offt += n;
4538
4539 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4540
Paul Bakker23986e52011-04-24 08:57:21 +00004541 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004542}
4543
4544/*
4545 * Send application data to be encrypted by the SSL layer
4546 */
Paul Bakker23986e52011-04-24 08:57:21 +00004547int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004548{
Paul Bakker23986e52011-04-24 08:57:21 +00004549 int ret;
4550 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004551 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004552
4553 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4554
4555 if( ssl->state != SSL_HANDSHAKE_OVER )
4556 {
4557 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4558 {
4559 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4560 return( ret );
4561 }
4562 }
4563
Paul Bakker05decb22013-08-15 13:33:48 +02004564#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004565 /*
4566 * Assume mfl_code is correct since it was checked when set
4567 */
4568 max_len = mfl_code_to_length[ssl->mfl_code];
4569
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004570 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004571 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004572 */
4573 if( ssl->session_out != NULL &&
4574 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4575 {
4576 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4577 }
Paul Bakker05decb22013-08-15 13:33:48 +02004578#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004579
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004580 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004581
Paul Bakker5121ce52009-01-03 21:22:43 +00004582 if( ssl->out_left != 0 )
4583 {
4584 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4585 {
4586 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4587 return( ret );
4588 }
4589 }
Paul Bakker887bd502011-06-08 13:10:54 +00004590 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004591 {
Paul Bakker887bd502011-06-08 13:10:54 +00004592 ssl->out_msglen = n;
4593 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4594 memcpy( ssl->out_msg, buf, n );
4595
4596 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4597 {
4598 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4599 return( ret );
4600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004601 }
4602
4603 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4604
Paul Bakker23986e52011-04-24 08:57:21 +00004605 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004606}
4607
4608/*
4609 * Notify the peer that the connection is being closed
4610 */
4611int ssl_close_notify( ssl_context *ssl )
4612{
4613 int ret;
4614
4615 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4616
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004617 if( ssl->out_left != 0 )
4618 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004619
4620 if( ssl->state == SSL_HANDSHAKE_OVER )
4621 {
Paul Bakker48916f92012-09-16 19:57:18 +00004622 if( ( ret = ssl_send_alert_message( ssl,
4623 SSL_ALERT_LEVEL_WARNING,
4624 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004625 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004626 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004627 return( ret );
4628 }
4629 }
4630
4631 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4632
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004633 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004634}
4635
Paul Bakker48916f92012-09-16 19:57:18 +00004636void ssl_transform_free( ssl_transform *transform )
4637{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004638 if( transform == NULL )
4639 return;
4640
Paul Bakker48916f92012-09-16 19:57:18 +00004641#if defined(POLARSSL_ZLIB_SUPPORT)
4642 deflateEnd( &transform->ctx_deflate );
4643 inflateEnd( &transform->ctx_inflate );
4644#endif
4645
Paul Bakker84bbeb52014-07-01 14:53:22 +02004646 cipher_free( &transform->cipher_ctx_enc );
4647 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004648
Paul Bakker84bbeb52014-07-01 14:53:22 +02004649 md_free( &transform->md_ctx_enc );
4650 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004651
Paul Bakker34617722014-06-13 17:20:13 +02004652 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004653}
4654
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004655#if defined(POLARSSL_X509_CRT_PARSE_C)
4656static void ssl_key_cert_free( ssl_key_cert *key_cert )
4657{
4658 ssl_key_cert *cur = key_cert, *next;
4659
4660 while( cur != NULL )
4661 {
4662 next = cur->next;
4663
4664 if( cur->key_own_alloc )
4665 {
4666 pk_free( cur->key );
4667 polarssl_free( cur->key );
4668 }
4669 polarssl_free( cur );
4670
4671 cur = next;
4672 }
4673}
4674#endif /* POLARSSL_X509_CRT_PARSE_C */
4675
Paul Bakker48916f92012-09-16 19:57:18 +00004676void ssl_handshake_free( ssl_handshake_params *handshake )
4677{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004678 if( handshake == NULL )
4679 return;
4680
Paul Bakker48916f92012-09-16 19:57:18 +00004681#if defined(POLARSSL_DHM_C)
4682 dhm_free( &handshake->dhm_ctx );
4683#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004684#if defined(POLARSSL_ECDH_C)
4685 ecdh_free( &handshake->ecdh_ctx );
4686#endif
4687
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004688#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004689 /* explicit void pointer cast for buggy MS compiler */
4690 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004691#endif
4692
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004693#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4694 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4695 /*
4696 * Free only the linked list wrapper, not the keys themselves
4697 * since the belong to the SNI callback
4698 */
4699 if( handshake->sni_key_cert != NULL )
4700 {
4701 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4702
4703 while( cur != NULL )
4704 {
4705 next = cur->next;
4706 polarssl_free( cur );
4707 cur = next;
4708 }
4709 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004710#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004711
Paul Bakker34617722014-06-13 17:20:13 +02004712 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004713}
4714
4715void ssl_session_free( ssl_session *session )
4716{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004717 if( session == NULL )
4718 return;
4719
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004720#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004721 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004722 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004723 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004724 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004725 }
Paul Bakkered27a042013-04-18 22:46:23 +02004726#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004727
Paul Bakkera503a632013-08-14 13:48:06 +02004728#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004729 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004730#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004731
Paul Bakker34617722014-06-13 17:20:13 +02004732 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004733}
4734
Paul Bakker5121ce52009-01-03 21:22:43 +00004735/*
4736 * Free an SSL context
4737 */
4738void ssl_free( ssl_context *ssl )
4739{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004740 if( ssl == NULL )
4741 return;
4742
Paul Bakker5121ce52009-01-03 21:22:43 +00004743 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4744
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004745 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004746 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004747 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
4748 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004749 }
4750
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004751 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004752 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004753 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
4754 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004755 }
4756
Paul Bakker16770332013-10-11 09:59:44 +02004757#if defined(POLARSSL_ZLIB_SUPPORT)
4758 if( ssl->compress_buf != NULL )
4759 {
Paul Bakker34617722014-06-13 17:20:13 +02004760 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004761 polarssl_free( ssl->compress_buf );
4762 }
4763#endif
4764
Paul Bakker40e46942009-01-03 21:51:57 +00004765#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004766 mpi_free( &ssl->dhm_P );
4767 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004768#endif
4769
Paul Bakker48916f92012-09-16 19:57:18 +00004770 if( ssl->transform )
4771 {
4772 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004773 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004774 }
4775
4776 if( ssl->handshake )
4777 {
4778 ssl_handshake_free( ssl->handshake );
4779 ssl_transform_free( ssl->transform_negotiate );
4780 ssl_session_free( ssl->session_negotiate );
4781
Paul Bakker6e339b52013-07-03 13:37:05 +02004782 polarssl_free( ssl->handshake );
4783 polarssl_free( ssl->transform_negotiate );
4784 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004785 }
4786
Paul Bakkerc0463502013-02-14 11:19:38 +01004787 if( ssl->session )
4788 {
4789 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004790 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004791 }
4792
Paul Bakkera503a632013-08-14 13:48:06 +02004793#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004794 if( ssl->ticket_keys )
4795 {
4796 ssl_ticket_keys_free( ssl->ticket_keys );
4797 polarssl_free( ssl->ticket_keys );
4798 }
Paul Bakkera503a632013-08-14 13:48:06 +02004799#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004800
Paul Bakker0be444a2013-08-27 21:55:01 +02004801#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004802 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004803 {
Paul Bakker34617722014-06-13 17:20:13 +02004804 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004805 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004806 ssl->hostname_len = 0;
4807 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004808#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004809
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004810#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004811 if( ssl->psk != NULL )
4812 {
Paul Bakker34617722014-06-13 17:20:13 +02004813 polarssl_zeroize( ssl->psk, ssl->psk_len );
4814 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004815 polarssl_free( ssl->psk );
4816 polarssl_free( ssl->psk_identity );
4817 ssl->psk_len = 0;
4818 ssl->psk_identity_len = 0;
4819 }
4820#endif
4821
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004822#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004823 ssl_key_cert_free( ssl->key_cert );
4824#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004825
Paul Bakker05ef8352012-05-08 09:17:57 +00004826#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4827 if( ssl_hw_record_finish != NULL )
4828 {
4829 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4830 ssl_hw_record_finish( ssl );
4831 }
4832#endif
4833
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004835
Paul Bakker86f04f42013-02-14 11:20:09 +01004836 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004837 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004838}
4839
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004840#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004841/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004842 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004843 */
4844unsigned char ssl_sig_from_pk( pk_context *pk )
4845{
4846#if defined(POLARSSL_RSA_C)
4847 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4848 return( SSL_SIG_RSA );
4849#endif
4850#if defined(POLARSSL_ECDSA_C)
4851 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4852 return( SSL_SIG_ECDSA );
4853#endif
4854 return( SSL_SIG_ANON );
4855}
4856
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004857pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4858{
4859 switch( sig )
4860 {
4861#if defined(POLARSSL_RSA_C)
4862 case SSL_SIG_RSA:
4863 return( POLARSSL_PK_RSA );
4864#endif
4865#if defined(POLARSSL_ECDSA_C)
4866 case SSL_SIG_ECDSA:
4867 return( POLARSSL_PK_ECDSA );
4868#endif
4869 default:
4870 return( POLARSSL_PK_NONE );
4871 }
4872}
Paul Bakker9af723c2014-05-01 13:03:14 +02004873#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004874
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004875/*
4876 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4877 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004878md_type_t ssl_md_alg_from_hash( unsigned char hash )
4879{
4880 switch( hash )
4881 {
4882#if defined(POLARSSL_MD5_C)
4883 case SSL_HASH_MD5:
4884 return( POLARSSL_MD_MD5 );
4885#endif
4886#if defined(POLARSSL_SHA1_C)
4887 case SSL_HASH_SHA1:
4888 return( POLARSSL_MD_SHA1 );
4889#endif
4890#if defined(POLARSSL_SHA256_C)
4891 case SSL_HASH_SHA224:
4892 return( POLARSSL_MD_SHA224 );
4893 case SSL_HASH_SHA256:
4894 return( POLARSSL_MD_SHA256 );
4895#endif
4896#if defined(POLARSSL_SHA512_C)
4897 case SSL_HASH_SHA384:
4898 return( POLARSSL_MD_SHA384 );
4899 case SSL_HASH_SHA512:
4900 return( POLARSSL_MD_SHA512 );
4901#endif
4902 default:
4903 return( POLARSSL_MD_NONE );
4904 }
4905}
4906
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004907#if defined(POLARSSL_SSL_SET_CURVES)
4908/*
4909 * Check is a curve proposed by the peer is in our list.
4910 * Return 1 if we're willing to use it, 0 otherwise.
4911 */
4912int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4913{
4914 const ecp_group_id *gid;
4915
4916 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4917 if( *gid == grp_id )
4918 return( 1 );
4919
4920 return( 0 );
4921}
Paul Bakker9af723c2014-05-01 13:03:14 +02004922#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004923
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004924#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004925int ssl_check_cert_usage( const x509_crt *cert,
4926 const ssl_ciphersuite_t *ciphersuite,
4927 int cert_endpoint )
4928{
4929#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4930 int usage = 0;
4931#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004932#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4933 const char *ext_oid;
4934 size_t ext_len;
4935#endif
4936
4937#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4938 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4939 ((void) cert);
4940 ((void) cert_endpoint);
4941#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004942
4943#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4944 if( cert_endpoint == SSL_IS_SERVER )
4945 {
4946 /* Server part of the key exchange */
4947 switch( ciphersuite->key_exchange )
4948 {
4949 case POLARSSL_KEY_EXCHANGE_RSA:
4950 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4951 usage = KU_KEY_ENCIPHERMENT;
4952 break;
4953
4954 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4955 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4956 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4957 usage = KU_DIGITAL_SIGNATURE;
4958 break;
4959
4960 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4961 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4962 usage = KU_KEY_AGREEMENT;
4963 break;
4964
4965 /* Don't use default: we want warnings when adding new values */
4966 case POLARSSL_KEY_EXCHANGE_NONE:
4967 case POLARSSL_KEY_EXCHANGE_PSK:
4968 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4969 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4970 usage = 0;
4971 }
4972 }
4973 else
4974 {
4975 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4976 usage = KU_DIGITAL_SIGNATURE;
4977 }
4978
4979 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4980 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004981#else
4982 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004983#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4984
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004985#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4986 if( cert_endpoint == SSL_IS_SERVER )
4987 {
4988 ext_oid = OID_SERVER_AUTH;
4989 ext_len = OID_SIZE( OID_SERVER_AUTH );
4990 }
4991 else
4992 {
4993 ext_oid = OID_CLIENT_AUTH;
4994 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4995 }
4996
4997 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4998 return( -1 );
4999#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5000
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005001 return( 0 );
5002}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005003#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005004
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005005/*
5006 * Convert version numbers to/from wire format
5007 * and, for DTLS, to/from TLS equivalent.
5008 *
5009 * For TLS this is the identity.
5010 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
5011 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5012 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5013 */
5014void ssl_write_version( int major, int minor, int transport,
5015 unsigned char ver[2] )
5016{
5017 if( transport == SSL_TRANSPORT_STREAM )
5018 {
5019 ver[0] = (unsigned char) major;
5020 ver[1] = (unsigned char) minor;
5021 }
5022#if defined(POLARSSL_SSL_PROTO_DTLS)
5023 else
5024 {
5025 if( minor == SSL_MINOR_VERSION_2 )
5026 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5027
5028 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5029 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5030 }
5031#endif
5032}
5033
5034void ssl_read_version( int *major, int *minor, int transport,
5035 const unsigned char ver[2] )
5036{
5037 if( transport == SSL_TRANSPORT_STREAM )
5038 {
5039 *major = ver[0];
5040 *minor = ver[1];
5041 }
5042#if defined(POLARSSL_SSL_PROTO_DTLS)
5043 else
5044 {
5045 *major = 255 - ver[0] + 2;
5046 *minor = 255 - ver[1] + 1;
5047
5048 if( *minor == SSL_MINOR_VERSION_1 )
5049 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5050 }
5051#endif
5052}
5053
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005054#endif /* POLARSSL_SSL_TLS_C */