blob: 7d6b9abc57afb00bcd9b8ef50643fef6226e902a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
70static inline size_t ssl_ep_len( const ssl_context *ssl )
71{
72#if defined(POLARSSL_SSL_PROTO_DTLS)
73 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
74 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Paul Bakker05decb22013-08-15 13:33:48 +020081#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020082/*
83 * Convert max_fragment_length codes to length.
84 * RFC 6066 says:
85 * enum{
86 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
87 * } MaxFragmentLength;
88 * and we add 0 -> extension unused
89 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020090static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020091{
92 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
93 512, /* SSL_MAX_FRAG_LEN_512 */
94 1024, /* SSL_MAX_FRAG_LEN_1024 */
95 2048, /* SSL_MAX_FRAG_LEN_2048 */
96 4096, /* SSL_MAX_FRAG_LEN_4096 */
97};
Paul Bakker05decb22013-08-15 13:33:48 +020098#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020099
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
101{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200102 ssl_session_free( dst );
103 memcpy( dst, src, sizeof( ssl_session ) );
104
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 if( src->peer_cert != NULL )
107 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200108 int ret;
109
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200110 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
111 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200112 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
113
Paul Bakkerb6b09562013-09-18 14:17:41 +0200114 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200116 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
117 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200118 {
119 polarssl_free( dst->peer_cert );
120 dst->peer_cert = NULL;
121 return( ret );
122 }
123 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200125
Paul Bakkera503a632013-08-14 13:48:06 +0200126#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200127 if( src->ticket != NULL )
128 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
130 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200131 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
132
133 memcpy( dst->ticket, src->ticket, src->ticket_len );
134 }
Paul Bakkera503a632013-08-14 13:48:06 +0200135#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200136
137 return( 0 );
138}
139
Paul Bakker05ef8352012-05-08 09:17:57 +0000140#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200141int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200142 const unsigned char *key_enc, const unsigned char *key_dec,
143 size_t keylen,
144 const unsigned char *iv_enc, const unsigned char *iv_dec,
145 size_t ivlen,
146 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200147 size_t maclen ) = NULL;
148int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
149int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
150int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
151int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
152int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200153#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000154
Paul Bakker5121ce52009-01-03 21:22:43 +0000155/*
156 * Key material generation
157 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200158#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200159static int ssl3_prf( const unsigned char *secret, size_t slen,
160 const char *label,
161 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 unsigned char *dstbuf, size_t dlen )
163{
164 size_t i;
165 md5_context md5;
166 sha1_context sha1;
167 unsigned char padding[16];
168 unsigned char sha1sum[20];
169 ((void)label);
170
Paul Bakker5b4af392014-06-26 12:09:34 +0200171 md5_init( &md5 );
172 sha1_init( &sha1 );
173
Paul Bakker5f70b252012-09-13 14:23:06 +0000174 /*
175 * SSLv3:
176 * block =
177 * MD5( secret + SHA1( 'A' + secret + random ) ) +
178 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
179 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
180 * ...
181 */
182 for( i = 0; i < dlen / 16; i++ )
183 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200184 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000185
186 sha1_starts( &sha1 );
187 sha1_update( &sha1, padding, 1 + i );
188 sha1_update( &sha1, secret, slen );
189 sha1_update( &sha1, random, rlen );
190 sha1_finish( &sha1, sha1sum );
191
192 md5_starts( &md5 );
193 md5_update( &md5, secret, slen );
194 md5_update( &md5, sha1sum, 20 );
195 md5_finish( &md5, dstbuf + i * 16 );
196 }
197
Paul Bakker5b4af392014-06-26 12:09:34 +0200198 md5_free( &md5 );
199 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000200
Paul Bakker34617722014-06-13 17:20:13 +0200201 polarssl_zeroize( padding, sizeof( padding ) );
202 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000203
204 return( 0 );
205}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200206#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000207
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200208#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200209static int tls1_prf( const unsigned char *secret, size_t slen,
210 const char *label,
211 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000212 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213{
Paul Bakker23986e52011-04-24 08:57:21 +0000214 size_t nb, hs;
215 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200216 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 unsigned char tmp[128];
218 unsigned char h_i[20];
219
220 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000221 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223 hs = ( slen + 1 ) / 2;
224 S1 = secret;
225 S2 = secret + slen - hs;
226
227 nb = strlen( label );
228 memcpy( tmp + 20, label, nb );
229 memcpy( tmp + 20 + nb, random, rlen );
230 nb += rlen;
231
232 /*
233 * First compute P_md5(secret,label+random)[0..dlen]
234 */
235 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
236
237 for( i = 0; i < dlen; i += 16 )
238 {
239 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
240 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
241
242 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
243
244 for( j = 0; j < k; j++ )
245 dstbuf[i + j] = h_i[j];
246 }
247
248 /*
249 * XOR out with P_sha1(secret,label+random)[0..dlen]
250 */
251 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
252
253 for( i = 0; i < dlen; i += 20 )
254 {
255 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
256 sha1_hmac( S2, hs, tmp, 20, tmp );
257
258 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
259
260 for( j = 0; j < k; j++ )
261 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
262 }
263
Paul Bakker34617722014-06-13 17:20:13 +0200264 polarssl_zeroize( tmp, sizeof( tmp ) );
265 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267 return( 0 );
268}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200269#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200271#if defined(POLARSSL_SSL_PROTO_TLS1_2)
272#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200273static int tls_prf_sha256( const unsigned char *secret, size_t slen,
274 const char *label,
275 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000276 unsigned char *dstbuf, size_t dlen )
277{
278 size_t nb;
279 size_t i, j, k;
280 unsigned char tmp[128];
281 unsigned char h_i[32];
282
283 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
284 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
285
286 nb = strlen( label );
287 memcpy( tmp + 32, label, nb );
288 memcpy( tmp + 32 + nb, random, rlen );
289 nb += rlen;
290
291 /*
292 * Compute P_<hash>(secret, label + random)[0..dlen]
293 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200294 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 for( i = 0; i < dlen; i += 32 )
297 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200298 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
299 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000300
301 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
302
303 for( j = 0; j < k; j++ )
304 dstbuf[i + j] = h_i[j];
305 }
306
Paul Bakker34617722014-06-13 17:20:13 +0200307 polarssl_zeroize( tmp, sizeof( tmp ) );
308 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000309
310 return( 0 );
311}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200312#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000313
Paul Bakker9e36f042013-06-30 14:34:05 +0200314#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200315static int tls_prf_sha384( const unsigned char *secret, size_t slen,
316 const char *label,
317 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000318 unsigned char *dstbuf, size_t dlen )
319{
320 size_t nb;
321 size_t i, j, k;
322 unsigned char tmp[128];
323 unsigned char h_i[48];
324
325 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
326 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
327
328 nb = strlen( label );
329 memcpy( tmp + 48, label, nb );
330 memcpy( tmp + 48 + nb, random, rlen );
331 nb += rlen;
332
333 /*
334 * Compute P_<hash>(secret, label + random)[0..dlen]
335 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200336 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 for( i = 0; i < dlen; i += 48 )
339 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200340 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
341 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
343 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
344
345 for( j = 0; j < k; j++ )
346 dstbuf[i + j] = h_i[j];
347 }
348
Paul Bakker34617722014-06-13 17:20:13 +0200349 polarssl_zeroize( tmp, sizeof( tmp ) );
350 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000351
352 return( 0 );
353}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200354#endif /* POLARSSL_SHA512_C */
355#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000356
Paul Bakker66d5d072014-06-17 16:39:18 +0200357static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358
359#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
360 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200361static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200362#endif
Paul Bakker380da532012-04-18 16:10:25 +0000363
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200364#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200365static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
366static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
368
369#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
371static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200372#endif
373
374#if defined(POLARSSL_SSL_PROTO_TLS1_2)
375#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200376static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
377static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
378static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200379#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100380
Paul Bakker9e36f042013-06-30 14:34:05 +0200381#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200382static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
383static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
384static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100385#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200386#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388int ssl_derive_keys( ssl_context *ssl )
389{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200390 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 unsigned char keyblk[256];
393 unsigned char *key1;
394 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100395 unsigned char *mac_enc;
396 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100398 const cipher_info_t *cipher_info;
399 const md_info_t *md_info;
400
Paul Bakker48916f92012-09-16 19:57:18 +0000401 ssl_session *session = ssl->session_negotiate;
402 ssl_transform *transform = ssl->transform_negotiate;
403 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
406
Paul Bakker68884e32013-01-07 18:20:04 +0100407 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
408 if( cipher_info == NULL )
409 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200410 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100411 transform->ciphersuite_info->cipher ) );
412 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
413 }
414
415 md_info = md_info_from_type( transform->ciphersuite_info->mac );
416 if( md_info == NULL )
417 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200418 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100419 transform->ciphersuite_info->mac ) );
420 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
421 }
422
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000424 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000425 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200426#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000427 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000428 {
Paul Bakker48916f92012-09-16 19:57:18 +0000429 handshake->tls_prf = ssl3_prf;
430 handshake->calc_verify = ssl_calc_verify_ssl;
431 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000432 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200433 else
434#endif
435#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
436 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000437 {
Paul Bakker48916f92012-09-16 19:57:18 +0000438 handshake->tls_prf = tls1_prf;
439 handshake->calc_verify = ssl_calc_verify_tls;
440 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000441 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442 else
443#endif
444#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200445#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200446 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
447 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000448 {
Paul Bakker48916f92012-09-16 19:57:18 +0000449 handshake->tls_prf = tls_prf_sha384;
450 handshake->calc_verify = ssl_calc_verify_tls_sha384;
451 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000452 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000453 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200454#endif
455#if defined(POLARSSL_SHA256_C)
456 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000457 {
Paul Bakker48916f92012-09-16 19:57:18 +0000458 handshake->tls_prf = tls_prf_sha256;
459 handshake->calc_verify = ssl_calc_verify_tls_sha256;
460 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000461 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200462 else
463#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200464#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200465 {
466 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200467 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200468 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000469
470 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * SSLv3:
472 * master =
473 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
474 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
475 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200476 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200477 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 * master = PRF( premaster, "master secret", randbytes )[0..47]
479 */
Paul Bakker0a597072012-09-25 21:55:46 +0000480 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 {
Paul Bakker48916f92012-09-16 19:57:18 +0000482 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
483 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000484
Paul Bakker48916f92012-09-16 19:57:18 +0000485 handshake->tls_prf( handshake->premaster, handshake->pmslen,
486 "master secret",
487 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
Paul Bakker34617722014-06-13 17:20:13 +0200489 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 }
491 else
492 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
493
494 /*
495 * Swap the client and server random values.
496 */
Paul Bakker48916f92012-09-16 19:57:18 +0000497 memcpy( tmp, handshake->randbytes, 64 );
498 memcpy( handshake->randbytes, tmp + 32, 32 );
499 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200500 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 /*
503 * SSLv3:
504 * key block =
505 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
506 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
507 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
508 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
509 * ...
510 *
511 * TLSv1:
512 * key block = PRF( master, "key expansion", randbytes )
513 */
Paul Bakker48916f92012-09-16 19:57:18 +0000514 handshake->tls_prf( session->master, 48, "key expansion",
515 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
Paul Bakker48916f92012-09-16 19:57:18 +0000517 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
518 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
519 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
520 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
522
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * Determine the appropriate key, IV and MAC length.
527 */
Paul Bakker68884e32013-01-07 18:20:04 +0100528
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200529 transform->keylen = cipher_info->key_length / 8;
530
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200531 if( cipher_info->mode == POLARSSL_MODE_GCM ||
532 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 transform->maclen = 0;
535
Paul Bakker68884e32013-01-07 18:20:04 +0100536 transform->ivlen = 12;
537 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200538
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200539 /* Minimum length is expicit IV + tag */
540 transform->minlen = transform->ivlen - transform->fixed_ivlen
541 + ( transform->ciphersuite_info->flags &
542 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100543 }
544 else
545 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200546 int ret;
547
548 /* Initialize HMAC contexts */
549 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
550 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100551 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
553 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100554 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200556 /* Get MAC length */
557 transform->maclen = md_get_size( md_info );
558
559#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
560 /*
561 * If HMAC is to be truncated, we shall keep the leftmost bytes,
562 * (rfc 6066 page 13 or rfc 2104 section 4),
563 * so we only need to adjust the length here.
564 */
565 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
566 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
567#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
568
569 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100570 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200572 /* Minimum length */
573 if( cipher_info->mode == POLARSSL_MODE_STREAM )
574 transform->minlen = transform->maclen;
575 else
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200577 /*
578 * GenericBlockCipher:
579 * first multiple of blocklen greater than maclen
580 * + IV except for SSL3 and TLS 1.0
581 */
582 transform->minlen = transform->maclen
583 + cipher_info->block_size
584 - transform->maclen % cipher_info->block_size;
585
586#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
587 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
588 ssl->minor_ver == SSL_MINOR_VERSION_1 )
589 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100590 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200591#endif
592#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
593 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
594 ssl->minor_ver == SSL_MINOR_VERSION_3 )
595 {
596 transform->minlen += transform->ivlen;
597 }
598 else
599#endif
600 {
601 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
602 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
603 }
Paul Bakker68884e32013-01-07 18:20:04 +0100604 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 }
606
607 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000608 transform->keylen, transform->minlen, transform->ivlen,
609 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
611 /*
612 * Finally setup the cipher contexts, IVs and MAC secrets.
613 */
614 if( ssl->endpoint == SSL_IS_CLIENT )
615 {
Paul Bakker48916f92012-09-16 19:57:18 +0000616 key1 = keyblk + transform->maclen * 2;
617 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
Paul Bakker68884e32013-01-07 18:20:04 +0100619 mac_enc = keyblk;
620 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000622 /*
623 * This is not used in TLS v1.1.
624 */
Paul Bakker48916f92012-09-16 19:57:18 +0000625 iv_copy_len = ( transform->fixed_ivlen ) ?
626 transform->fixed_ivlen : transform->ivlen;
627 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
628 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000629 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631 else
632 {
Paul Bakker48916f92012-09-16 19:57:18 +0000633 key1 = keyblk + transform->maclen * 2 + transform->keylen;
634 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
Paul Bakker68884e32013-01-07 18:20:04 +0100636 mac_enc = keyblk + transform->maclen;
637 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000639 /*
640 * This is not used in TLS v1.1.
641 */
Paul Bakker48916f92012-09-16 19:57:18 +0000642 iv_copy_len = ( transform->fixed_ivlen ) ?
643 transform->fixed_ivlen : transform->ivlen;
644 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
645 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000646 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 }
648
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200649#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100650 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
651 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100652 if( transform->maclen > sizeof transform->mac_enc )
653 {
654 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200655 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100656 }
657
Paul Bakker68884e32013-01-07 18:20:04 +0100658 memcpy( transform->mac_enc, mac_enc, transform->maclen );
659 memcpy( transform->mac_dec, mac_dec, transform->maclen );
660 }
661 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200662#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200663#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
664 defined(POLARSSL_SSL_PROTO_TLS1_2)
665 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100666 {
667 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
668 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
669 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200670 else
671#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200672 {
673 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200674 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200675 }
Paul Bakker68884e32013-01-07 18:20:04 +0100676
Paul Bakker05ef8352012-05-08 09:17:57 +0000677#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200678 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000679 {
680 int ret = 0;
681
682 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
683
Paul Bakker07eb38b2012-12-19 14:42:06 +0100684 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
685 transform->iv_enc, transform->iv_dec,
686 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100687 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100688 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000689 {
690 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200691 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000692 }
693 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200694#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000695
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200696 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
697 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200699 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
700 return( ret );
701 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200702
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200703 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
704 cipher_info ) ) != 0 )
705 {
706 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
707 return( ret );
708 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200709
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200710 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
711 cipher_info->key_length,
712 POLARSSL_ENCRYPT ) ) != 0 )
713 {
714 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
715 return( ret );
716 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200717
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200718 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
719 cipher_info->key_length,
720 POLARSSL_DECRYPT ) ) != 0 )
721 {
722 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
723 return( ret );
724 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200725
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200726#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200727 if( cipher_info->mode == POLARSSL_MODE_CBC )
728 {
729 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
730 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200731 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200732 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
733 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200734 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735
736 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
737 POLARSSL_PADDING_NONE ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
740 return( ret );
741 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker34617722014-06-13 17:20:13 +0200745 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Paul Bakker2770fbd2012-07-03 13:30:23 +0000747#if defined(POLARSSL_ZLIB_SUPPORT)
748 // Initialize compression
749 //
Paul Bakker48916f92012-09-16 19:57:18 +0000750 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000751 {
Paul Bakker16770332013-10-11 09:59:44 +0200752 if( ssl->compress_buf == NULL )
753 {
754 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
755 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
756 if( ssl->compress_buf == NULL )
757 {
758 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
759 SSL_BUFFER_LEN ) );
760 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
761 }
762 }
763
Paul Bakker2770fbd2012-07-03 13:30:23 +0000764 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
765
Paul Bakker48916f92012-09-16 19:57:18 +0000766 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
767 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000768
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200769 if( deflateInit( &transform->ctx_deflate,
770 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000771 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772 {
773 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
774 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
775 }
776 }
777#endif /* POLARSSL_ZLIB_SUPPORT */
778
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
780
781 return( 0 );
782}
783
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200784#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000785void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000786{
787 md5_context md5;
788 sha1_context sha1;
789 unsigned char pad_1[48];
790 unsigned char pad_2[48];
791
Paul Bakker380da532012-04-18 16:10:25 +0000792 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000793
Paul Bakker48916f92012-09-16 19:57:18 +0000794 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
795 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000796
Paul Bakker380da532012-04-18 16:10:25 +0000797 memset( pad_1, 0x36, 48 );
798 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
Paul Bakker48916f92012-09-16 19:57:18 +0000800 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000801 md5_update( &md5, pad_1, 48 );
802 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000803
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000805 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000806 md5_update( &md5, pad_2, 48 );
807 md5_update( &md5, hash, 16 );
808 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000811 sha1_update( &sha1, pad_1, 40 );
812 sha1_finish( &sha1, hash + 16 );
813
814 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000815 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000816 sha1_update( &sha1, pad_2, 40 );
817 sha1_update( &sha1, hash + 16, 20 );
818 sha1_finish( &sha1, hash + 16 );
819
820 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
821 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
822
Paul Bakker5b4af392014-06-26 12:09:34 +0200823 md5_free( &md5 );
824 sha1_free( &sha1 );
825
Paul Bakker380da532012-04-18 16:10:25 +0000826 return;
827}
Paul Bakker9af723c2014-05-01 13:03:14 +0200828#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000829
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200830#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000831void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
832{
833 md5_context md5;
834 sha1_context sha1;
835
836 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
837
Paul Bakker48916f92012-09-16 19:57:18 +0000838 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
839 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000840
Paul Bakker48916f92012-09-16 19:57:18 +0000841 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000842 sha1_finish( &sha1, hash + 16 );
843
844 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
845 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
846
Paul Bakker5b4af392014-06-26 12:09:34 +0200847 md5_free( &md5 );
848 sha1_free( &sha1 );
849
Paul Bakker380da532012-04-18 16:10:25 +0000850 return;
851}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000853
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200854#if defined(POLARSSL_SSL_PROTO_TLS1_2)
855#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
857{
Paul Bakker9e36f042013-06-30 14:34:05 +0200858 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000859
860 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
861
Paul Bakker9e36f042013-06-30 14:34:05 +0200862 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
863 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000864
865 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
866 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
867
Paul Bakker5b4af392014-06-26 12:09:34 +0200868 sha256_free( &sha256 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200872#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakker9e36f042013-06-30 14:34:05 +0200874#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
876{
Paul Bakker9e36f042013-06-30 14:34:05 +0200877 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000878
879 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
880
Paul Bakker9e36f042013-06-30 14:34:05 +0200881 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
882 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
Paul Bakkerca4ab492012-04-18 14:23:57 +0000884 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000885 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
886
Paul Bakker5b4af392014-06-26 12:09:34 +0200887 sha512_free( &sha512 );
888
Paul Bakker5121ce52009-01-03 21:22:43 +0000889 return;
890}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200891#endif /* POLARSSL_SHA512_C */
892#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000893
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200894#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
896{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200897 unsigned char *p = ssl->handshake->premaster;
898 unsigned char *end = p + sizeof( ssl->handshake->premaster );
899
900 /*
901 * PMS = struct {
902 * opaque other_secret<0..2^16-1>;
903 * opaque psk<0..2^16-1>;
904 * };
905 * with "other_secret" depending on the particular key exchange
906 */
907#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
909 {
910 if( end - p < 2 + (int) ssl->psk_len )
911 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
912
913 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
914 *(p++) = (unsigned char)( ssl->psk_len );
915 p += ssl->psk_len;
916 }
917 else
918#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200919#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
920 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
921 {
922 /*
923 * other_secret already set by the ClientKeyExchange message,
924 * and is 48 bytes long
925 */
926 *p++ = 0;
927 *p++ = 48;
928 p += 48;
929 }
930 else
931#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200932#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
934 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200935 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200936 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200940 p + 2, &len,
941 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200942 {
943 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
944 return( ret );
945 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200946 *(p++) = (unsigned char)( len >> 8 );
947 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200948 p += len;
949
950 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
951 }
952 else
953#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
954#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
955 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
956 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200957 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200958 size_t zlen;
959
960 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200961 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962 ssl->f_rng, ssl->p_rng ) ) != 0 )
963 {
964 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
965 return( ret );
966 }
967
968 *(p++) = (unsigned char)( zlen >> 8 );
969 *(p++) = (unsigned char)( zlen );
970 p += zlen;
971
972 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
973 }
974 else
975#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
976 {
977 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200978 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200979 }
980
981 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100982 if( end - p < 2 + (int) ssl->psk_len )
983 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
984
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200985 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
986 *(p++) = (unsigned char)( ssl->psk_len );
987 memcpy( p, ssl->psk, ssl->psk_len );
988 p += ssl->psk_len;
989
990 ssl->handshake->pmslen = p - ssl->handshake->premaster;
991
992 return( 0 );
993}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200994#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200995
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200996#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000997/*
998 * SSLv3.0 MAC functions
999 */
Paul Bakker68884e32013-01-07 18:20:04 +01001000static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1001 unsigned char *buf, size_t len,
1002 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001003{
1004 unsigned char header[11];
1005 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001006 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001007 int md_size = md_get_size( md_ctx->md_info );
1008 int md_type = md_get_type( md_ctx->md_info );
1009
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001010 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001011 if( md_type == POLARSSL_MD_MD5 )
1012 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001013 else
Paul Bakker68884e32013-01-07 18:20:04 +01001014 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001015
1016 memcpy( header, ctr, 8 );
1017 header[ 8] = (unsigned char) type;
1018 header[ 9] = (unsigned char)( len >> 8 );
1019 header[10] = (unsigned char)( len );
1020
Paul Bakker68884e32013-01-07 18:20:04 +01001021 memset( padding, 0x36, padlen );
1022 md_starts( md_ctx );
1023 md_update( md_ctx, secret, md_size );
1024 md_update( md_ctx, padding, padlen );
1025 md_update( md_ctx, header, 11 );
1026 md_update( md_ctx, buf, len );
1027 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
Paul Bakker68884e32013-01-07 18:20:04 +01001029 memset( padding, 0x5C, padlen );
1030 md_starts( md_ctx );
1031 md_update( md_ctx, secret, md_size );
1032 md_update( md_ctx, padding, padlen );
1033 md_update( md_ctx, buf + len, md_size );
1034 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001035}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001036#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001037
Paul Bakker5121ce52009-01-03 21:22:43 +00001038/*
1039 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001040 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001041static int ssl_encrypt_buf( ssl_context *ssl )
1042{
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001043 const cipher_mode_t mode = cipher_get_cipher_mode(
1044 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
1046 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1047
1048 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001049 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001051#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1052 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1053 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001054 if( mode != POLARSSL_MODE_GCM &&
1055 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001057#if defined(POLARSSL_SSL_PROTO_SSL3)
1058 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1059 {
1060 ssl_mac( &ssl->transform_out->md_ctx_enc,
1061 ssl->transform_out->mac_enc,
1062 ssl->out_msg, ssl->out_msglen,
1063 ssl->out_ctr, ssl->out_msgtype );
1064 }
1065 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001066#endif
1067#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001068 defined(POLARSSL_SSL_PROTO_TLS1_2)
1069 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1070 {
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001071 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 8 );
1072 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_hdr, 3 );
1073 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001074 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1075 ssl->out_msg, ssl->out_msglen );
1076 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1077 ssl->out_msg + ssl->out_msglen );
1078 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1079 }
1080 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001081#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 {
1083 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001084 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001085 }
1086
1087 SSL_DEBUG_BUF( 4, "computed mac",
1088 ssl->out_msg + ssl->out_msglen,
1089 ssl->transform_out->maclen );
1090
1091 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001092 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001093#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001095 /*
1096 * Encrypt
1097 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001099 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001101 int ret;
1102 size_t olen = 0;
1103
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1105 "including %d bytes of padding",
1106 ssl->out_msglen, 0 ) );
1107
1108 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1109 ssl->out_msg, ssl->out_msglen );
1110
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001111 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001112 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001113 ssl->transform_out->ivlen,
1114 ssl->out_msg, ssl->out_msglen,
1115 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001116 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001117 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001118 return( ret );
1119 }
1120
1121 if( ssl->out_msglen != olen )
1122 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001123 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001124 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 }
Paul Bakker68884e32013-01-07 18:20:04 +01001127 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001128#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001129#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1130 if( mode == POLARSSL_MODE_GCM ||
1131 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001133 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001134 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001135 unsigned char *enc_msg;
1136 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001137 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1138 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001139
Paul Bakkerca4ab492012-04-18 14:23:57 +00001140 memcpy( add_data, ssl->out_ctr, 8 );
1141 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001142 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1143 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001144 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1145 add_data[12] = ssl->out_msglen & 0xFF;
1146
1147 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1148 add_data, 13 );
1149
Paul Bakker68884e32013-01-07 18:20:04 +01001150 /*
1151 * Generate IV
1152 */
1153 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001154 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1155 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001156 if( ret != 0 )
1157 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001158
Paul Bakker68884e32013-01-07 18:20:04 +01001159 memcpy( ssl->out_iv,
1160 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1161 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001162
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001163 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001165
Paul Bakker68884e32013-01-07 18:20:04 +01001166 /*
1167 * Fix pointer positions and message length with added IV
1168 */
1169 enc_msg = ssl->out_msg;
1170 enc_msglen = ssl->out_msglen;
1171 ssl->out_msglen += ssl->transform_out->ivlen -
1172 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001173
Paul Bakker68884e32013-01-07 18:20:04 +01001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Paul Bakker68884e32013-01-07 18:20:04 +01001178 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1179 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001180
Paul Bakker68884e32013-01-07 18:20:04 +01001181 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001182 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001184 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1185 ssl->transform_out->iv_enc,
1186 ssl->transform_out->ivlen,
1187 add_data, 13,
1188 enc_msg, enc_msglen,
1189 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001190 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001191 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 return( ret );
1194 }
1195
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001197 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001198 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001199 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001200 }
1201
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001203
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001207#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001208#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1209 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001210 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001212 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 unsigned char *enc_msg;
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01001214 size_t enc_msglen, padlen, olen = 0, i;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215
Paul Bakker48916f92012-09-16 19:57:18 +00001216 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1217 ssl->transform_out->ivlen;
1218 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 padlen = 0;
1220
1221 for( i = 0; i <= padlen; i++ )
1222 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1223
1224 ssl->out_msglen += padlen + 1;
1225
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 enc_msglen = ssl->out_msglen;
1227 enc_msg = ssl->out_msg;
1228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001229#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001231 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1232 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001234 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 {
1236 /*
1237 * Generate IV
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1240 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001241 if( ret != 0 )
1242 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246
1247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001250 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001251 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001252 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001254#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001258 ssl->out_msglen, ssl->transform_out->ivlen,
1259 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001262 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001264 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001265 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001266 ssl->transform_out->ivlen,
1267 enc_msg, enc_msglen,
1268 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001270 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 return( ret );
1272 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( enc_msglen != olen )
1275 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001276 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001277 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279
1280#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1282 {
1283 /*
1284 * Save IV in SSL3 and TLS1
1285 */
1286 memcpy( ssl->transform_out->iv_enc,
1287 ssl->transform_out->cipher_ctx_enc.iv,
1288 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001293#endif /* POLARSSL_CIPHER_MODE_CBC &&
1294 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001295 {
1296 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
1300 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1301
1302 return( 0 );
1303}
1304
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001305#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001306
Paul Bakker5121ce52009-01-03 21:22:43 +00001307static int ssl_decrypt_buf( ssl_context *ssl )
1308{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001309 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001310 const cipher_mode_t mode = cipher_get_cipher_mode(
1311 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001312#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1313 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1314 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1315 size_t padlen = 0, correct = 1;
1316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
1318 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1319
Paul Bakker48916f92012-09-16 19:57:18 +00001320 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 {
1322 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001324 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
1326
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001327#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001328 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001329 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001330 int ret;
1331 size_t olen = 0;
1332
Paul Bakker68884e32013-01-07 18:20:04 +01001333 padlen = 0;
1334
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001336 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001337 ssl->transform_in->ivlen,
1338 ssl->in_msg, ssl->in_msglen,
1339 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001340 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001341 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001342 return( ret );
1343 }
1344
1345 if( ssl->in_msglen != olen )
1346 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001347 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001348 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
Paul Bakker68884e32013-01-07 18:20:04 +01001351 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001352#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001353#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1354 if( mode == POLARSSL_MODE_GCM ||
1355 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001357 int ret;
1358 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 unsigned char *dec_msg;
1360 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001361 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001362 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1363 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001364 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1365 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001366
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001367 if( ssl->in_msglen < explicit_iv_len + taglen )
1368 {
1369 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1370 "+ taglen (%d)", ssl->in_msglen,
1371 explicit_iv_len, taglen ) );
1372 return( POLARSSL_ERR_SSL_INVALID_MAC );
1373 }
1374 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1375
Paul Bakker68884e32013-01-07 18:20:04 +01001376 dec_msg = ssl->in_msg;
1377 dec_msg_result = ssl->in_msg;
1378 ssl->in_msglen = dec_msglen;
1379
1380 memcpy( add_data, ssl->in_ctr, 8 );
1381 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001382 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1383 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001384 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1385 add_data[12] = ssl->in_msglen & 0xFF;
1386
1387 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1388 add_data, 13 );
1389
1390 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1391 ssl->in_iv,
1392 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1393
1394 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1395 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001396 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001397
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001398 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001399 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001400 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001401 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1402 ssl->transform_in->iv_dec,
1403 ssl->transform_in->ivlen,
1404 add_data, 13,
1405 dec_msg, dec_msglen,
1406 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001407 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001408 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001409 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1410
1411 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1412 return( POLARSSL_ERR_SSL_INVALID_MAC );
1413
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001414 return( ret );
1415 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001416
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001417 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001418 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001419 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001420 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001421 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001422 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001424#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001425#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1426 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001427 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 {
Paul Bakker45829992013-01-03 14:52:21 +01001429 /*
1430 * Decrypt and check the padding
1431 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001432 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001433 unsigned char *dec_msg;
1434 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001435 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001436 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001437 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001438
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 /*
Paul Bakker45829992013-01-03 14:52:21 +01001440 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 */
Paul Bakker48916f92012-09-16 19:57:18 +00001442 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
1444 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001445 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001446 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 }
1448
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001449#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001450 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1451 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#endif
Paul Bakker45829992013-01-03 14:52:21 +01001453
1454 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1455 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1456 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001457 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1458 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1459 ssl->transform_in->ivlen,
1460 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001461 return( POLARSSL_ERR_SSL_INVALID_MAC );
1462 }
1463
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001464 dec_msglen = ssl->in_msglen;
1465 dec_msg = ssl->in_msg;
1466 dec_msg_result = ssl->in_msg;
1467
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001468#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001470 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001471 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001472 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473 {
Paul Bakker48916f92012-09-16 19:57:18 +00001474 dec_msglen -= ssl->transform_in->ivlen;
1475 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001476
Paul Bakker48916f92012-09-16 19:57:18 +00001477 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001478 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001480#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001481
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001482 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001483 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001484 ssl->transform_in->ivlen,
1485 dec_msg, dec_msglen,
1486 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001487 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001488 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001489 return( ret );
1490 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001491
Paul Bakkercca5b812013-08-31 17:40:26 +02001492 if( dec_msglen != olen )
1493 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001494 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001495 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001496 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001497
1498#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001499 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1500 {
1501 /*
1502 * Save IV in SSL3 and TLS1
1503 */
1504 memcpy( ssl->transform_in->iv_dec,
1505 ssl->transform_in->cipher_ctx_dec.iv,
1506 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001507 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001508#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001511
1512 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1513 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001514#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001515 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1516 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001517#endif
Paul Bakker45829992013-01-03 14:52:21 +01001518 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001519 correct = 0;
1520 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001521
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001522#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1524 {
Paul Bakker48916f92012-09-16 19:57:18 +00001525 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001527#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1529 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001530 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001531#endif
Paul Bakker45829992013-01-03 14:52:21 +01001532 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 }
1534 }
1535 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001536#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001537#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1538 defined(POLARSSL_SSL_PROTO_TLS1_2)
1539 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001540 {
1541 /*
Paul Bakker45829992013-01-03 14:52:21 +01001542 * TLSv1+: always check the padding up to the first failure
1543 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001545 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001546 size_t padding_idx = ssl->in_msglen - padlen - 1;
1547
Paul Bakker956c9e02013-12-19 14:42:28 +01001548 /*
1549 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001550 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001551 *
Paul Bakker61885c72014-04-25 12:59:03 +02001552 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1553 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001554 *
1555 * In both cases we reset padding_idx to a safe value (0) to
1556 * prevent out-of-buffer reads.
1557 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001558 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001559 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1560 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001561
1562 padding_idx *= correct;
1563
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001564 for( i = 1; i <= 256; i++ )
1565 {
1566 real_count &= ( i <= padlen );
1567 pad_count += real_count *
1568 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1569 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570
1571 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001572
Paul Bakkerd66f0702013-01-31 16:57:45 +01001573#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001574 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001575 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001576#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001577 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001578 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001579 else
1580#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1581 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001582 {
1583 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001584 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001587 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001588#endif /* POLARSSL_CIPHER_MODE_CBC &&
1589 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001590 {
1591 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001592 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001594
1595 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1596 ssl->in_msg, ssl->in_msglen );
1597
1598 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001599 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001601#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1602 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1603 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001604 if( mode != POLARSSL_MODE_GCM &&
1605 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001606 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001607 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01001611 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
1612 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001614 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001616#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001617 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1618 {
1619 ssl_mac( &ssl->transform_in->md_ctx_dec,
1620 ssl->transform_in->mac_dec,
1621 ssl->in_msg, ssl->in_msglen,
1622 ssl->in_ctr, ssl->in_msgtype );
1623 }
1624 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001625#endif /* POLARSSL_SSL_PROTO_SSL3 */
1626#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001627 defined(POLARSSL_SSL_PROTO_TLS1_2)
1628 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1629 {
1630 /*
1631 * Process MAC and always update for padlen afterwards to make
1632 * total time independent of padlen
1633 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001634 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001635 *
1636 * Known timing attacks:
1637 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1638 *
1639 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1640 * correctly. (We round down instead of up, so -56 is the correct
1641 * value for our calculations instead of -55)
1642 */
1643 size_t j, extra_run = 0;
1644 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1645 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001646
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001648
Manuel Pégourié-Gonnardf302fb52014-02-18 09:43:50 +01001649 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 8 );
1650 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_hdr, 3 );
1651 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_len, 2 );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1653 ssl->in_msglen );
1654 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1655 ssl->in_msg + ssl->in_msglen );
1656 for( j = 0; j < extra_run; j++ )
1657 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001658
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1660 }
1661 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001662#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 POLARSSL_SSL_PROTO_TLS1_2 */
1664 {
1665 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001666 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001669 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1670 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1671 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001673 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001674 ssl->transform_in->maclen ) != 0 )
1675 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001676#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001677 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001678#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 correct = 0;
1680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 /*
1683 * Finally check the correct flag
1684 */
1685 if( correct == 0 )
1686 return( POLARSSL_ERR_SSL_INVALID_MAC );
1687 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001688#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 if( ssl->in_msglen == 0 )
1691 {
1692 ssl->nb_zero++;
1693
1694 /*
1695 * Three or more empty messages may be a DoS attack
1696 * (excessive CPU consumption).
1697 */
1698 if( ssl->nb_zero > 3 )
1699 {
1700 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1701 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001702 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703 }
1704 }
1705 else
1706 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001707
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001708 /* Input counter not used with DTLS right now,
1709 * but it doesn't hurt to have this part ready */
1710 for( i = 8; i > ssl_ep_len( ssl ); i-- )
1711 if( ++ssl->in_ctr[i - 1] != 0 )
1712 break;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001713
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001714 /* The loop goes to its end iff the counter is wrapping */
1715 if( i == ssl_ep_len( ssl ) )
1716 {
1717 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1718 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001719 }
1720
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1722
1723 return( 0 );
1724}
1725
Paul Bakker2770fbd2012-07-03 13:30:23 +00001726#if defined(POLARSSL_ZLIB_SUPPORT)
1727/*
1728 * Compression/decompression functions
1729 */
1730static int ssl_compress_buf( ssl_context *ssl )
1731{
1732 int ret;
1733 unsigned char *msg_post = ssl->out_msg;
1734 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001735 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001736
1737 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1738
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001739 if( len_pre == 0 )
1740 return( 0 );
1741
Paul Bakker2770fbd2012-07-03 13:30:23 +00001742 memcpy( msg_pre, ssl->out_msg, len_pre );
1743
1744 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1745 ssl->out_msglen ) );
1746
1747 SSL_DEBUG_BUF( 4, "before compression: output payload",
1748 ssl->out_msg, ssl->out_msglen );
1749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1751 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1752 ssl->transform_out->ctx_deflate.next_out = msg_post;
1753 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001754
Paul Bakker48916f92012-09-16 19:57:18 +00001755 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001756 if( ret != Z_OK )
1757 {
1758 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1759 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1760 }
1761
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001762 ssl->out_msglen = SSL_BUFFER_LEN -
1763 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001764
Paul Bakker2770fbd2012-07-03 13:30:23 +00001765 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1766 ssl->out_msglen ) );
1767
1768 SSL_DEBUG_BUF( 4, "after compression: output payload",
1769 ssl->out_msg, ssl->out_msglen );
1770
1771 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1772
1773 return( 0 );
1774}
1775
1776static int ssl_decompress_buf( ssl_context *ssl )
1777{
1778 int ret;
1779 unsigned char *msg_post = ssl->in_msg;
1780 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001781 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782
1783 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1784
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001785 if( len_pre == 0 )
1786 return( 0 );
1787
Paul Bakker2770fbd2012-07-03 13:30:23 +00001788 memcpy( msg_pre, ssl->in_msg, len_pre );
1789
1790 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1791 ssl->in_msglen ) );
1792
1793 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1794 ssl->in_msg, ssl->in_msglen );
1795
Paul Bakker48916f92012-09-16 19:57:18 +00001796 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1797 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1798 ssl->transform_in->ctx_inflate.next_out = msg_post;
1799 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001800
Paul Bakker48916f92012-09-16 19:57:18 +00001801 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001802 if( ret != Z_OK )
1803 {
1804 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1805 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1806 }
1807
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001808 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1809 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001810
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1812 ssl->in_msglen ) );
1813
1814 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1815 ssl->in_msg, ssl->in_msglen );
1816
1817 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1818
1819 return( 0 );
1820}
1821#endif /* POLARSSL_ZLIB_SUPPORT */
1822
Paul Bakker5121ce52009-01-03 21:22:43 +00001823/*
1824 * Fill the input message buffer
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001825 *
1826 * If we return 0, is it guaranteed that (at least) nb_want bytes are
1827 * available (from this read and/or a previous one). Otherwise, an error code
1828 * is returned (possibly EOF or WANT_READ).
1829 *
1830 * Set ssl->in_left to 0 before calling to start a new record. Apart from
1831 * this, ssl->in_left is an internal variable and should never be read.
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 */
Paul Bakker23986e52011-04-24 08:57:21 +00001833int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001834{
Paul Bakker23986e52011-04-24 08:57:21 +00001835 int ret;
1836 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
1838 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1839
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001840 if( nb_want > SSL_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001841 {
1842 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1843 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1844 }
1845
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001846#if defined(POLARSSL_SSL_PROTO_DTLS)
1847 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001848 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1850 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001851
1852 /*
1853 * With UDP, we must always read a full datagram.
1854 * Just remember how much we read and avoid reading again if we
1855 * already have enough data.
1856 */
1857 if( nb_want <= ssl->in_left)
1858 return( 0 );
1859
1860 /*
1861 * A record can't be split accross datagrams. If we need to read but
1862 * are not at the beginning of a new record, the caller did something
1863 * wrong.
1864 */
1865 if( ssl->in_left != 0 )
1866 {
1867 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1868 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1869 }
1870
1871 len = SSL_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
1872 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr, len );
1873
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1875
Paul Bakker831a7552011-05-18 13:32:51 +00001876 if( ret == 0 )
1877 return( POLARSSL_ERR_SSL_CONN_EOF );
1878
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 if( ret < 0 )
1880 return( ret );
1881
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01001882 ssl->in_left = ret;
1883 }
1884 else
1885#endif
1886 {
1887 while( ssl->in_left < nb_want )
1888 {
1889 len = nb_want - ssl->in_left;
1890 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1891
1892 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1893 ssl->in_left, nb_want ) );
1894 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1895
1896 if( ret == 0 )
1897 return( POLARSSL_ERR_SSL_CONN_EOF );
1898
1899 if( ret < 0 )
1900 return( ret );
1901
1902 ssl->in_left += ret;
1903 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 }
1905
1906 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1907
1908 return( 0 );
1909}
1910
1911/*
1912 * Flush any data not yet written
1913 */
1914int ssl_flush_output( ssl_context *ssl )
1915{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001916 int ret;
1917 unsigned char *buf, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
1919 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1920
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001921 /* Avoid incrementing counter if data is flushed */
1922 if( ssl->out_left == 0 )
1923 {
1924 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1925 return( 0 );
1926 }
1927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 while( ssl->out_left > 0 )
1929 {
1930 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001931 ssl_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001933 buf = ssl->out_hdr + ssl_hdr_len( ssl ) +
1934 ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001936
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1938
1939 if( ret <= 0 )
1940 return( ret );
1941
1942 ssl->out_left -= ret;
1943 }
1944
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001945 for( i = 8; i > ssl_ep_len( ssl ); i-- )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001946 if( ++ssl->out_ctr[i - 1] != 0 )
1947 break;
1948
1949 /* The loop goes to its end iff the counter is wrapping */
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01001950 if( i == ssl_ep_len( ssl ) )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001951 {
1952 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1953 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1954 }
1955
Paul Bakker5121ce52009-01-03 21:22:43 +00001956 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1957
1958 return( 0 );
1959}
1960
1961/*
1962 * Record layer functions
1963 */
1964int ssl_write_record( ssl_context *ssl )
1965{
Paul Bakker05ef8352012-05-08 09:17:57 +00001966 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001967 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001968
1969 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1970
Paul Bakker5121ce52009-01-03 21:22:43 +00001971 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1972 {
1973 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1974 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1975 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1976
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001977 /*
1978 * DTLS has additional fields in the Handshake layer,
1979 * between the length field and the actual payload:
1980 * uint16 message_seq;
1981 * uint24 fragment_offset;
1982 * uint24 fragment_length;
1983 */
1984#if defined(POLARSSL_SSL_PROTO_DTLS)
1985 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1986 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001987 /* Make room for the additional DTLS fields */
1988 memmove( ssl->out_msg + 12, ssl->out_msg + 4, len - 4 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001989 ssl->out_msglen += 8;
1990 len += 8;
1991
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001992 /* Write message_seq and update it */
1993 ssl->out_msg[4] = ( ssl->handshake->msg_seq >> 8 ) & 0xFF;
1994 ssl->out_msg[5] = ( ssl->handshake->msg_seq ) & 0xFF;
1995 ++( ssl->handshake->msg_seq );
1996
1997 /* We don't fragment, so frag_offset = 0 and frag_len = len */
1998 memset( ssl->out_msg + 6, 0x00, 3 );
1999 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002000 }
2001#endif /* POLARSSL_SSL_PROTO_DTLS */
2002
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002003 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2004 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 }
2006
Paul Bakker2770fbd2012-07-03 13:30:23 +00002007#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002008 if( ssl->transform_out != NULL &&
2009 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002010 {
2011 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2012 {
2013 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2014 return( ret );
2015 }
2016
2017 len = ssl->out_msglen;
2018 }
2019#endif /*POLARSSL_ZLIB_SUPPORT */
2020
Paul Bakker05ef8352012-05-08 09:17:57 +00002021#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002022 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002024 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Paul Bakker05ef8352012-05-08 09:17:57 +00002026 ret = ssl_hw_record_write( ssl );
2027 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2028 {
2029 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002030 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002031 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002032
2033 if( ret == 0 )
2034 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002035 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002036#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002037 if( !done )
2038 {
2039 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002040 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2041 ssl->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002042
2043 ssl->out_len[0] = (unsigned char)( len >> 8 );
2044 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002045
Paul Bakker48916f92012-09-16 19:57:18 +00002046 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002047 {
2048 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2049 {
2050 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2051 return( ret );
2052 }
2053
2054 len = ssl->out_msglen;
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002055 ssl->out_len[0] = (unsigned char)( len >> 8 );
2056 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002057 }
2058
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002059 ssl->out_left = ssl_hdr_len( ssl ) + ssl->out_msglen;
Paul Bakker05ef8352012-05-08 09:17:57 +00002060
2061 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2062 "version = [%d:%d], msglen = %d",
2063 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002064 ( ssl->out_len[0] << 8 ) | ssl->out_len[1] ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00002065
2066 SSL_DEBUG_BUF( 4, "output record sent to network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002067 ssl->out_hdr, ssl_hdr_len( ssl ) + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2071 {
2072 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2073 return( ret );
2074 }
2075
2076 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2077
2078 return( 0 );
2079}
2080
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002081static int ssl_prepare_handshake_record( ssl_context *ssl )
2082{
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002083 ssl->in_hslen = ssl->transport == SSL_TRANSPORT_DATAGRAM ? 12 : 4;
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002084 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2085
2086 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2087 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002088 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002089
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002090 /* We don't handle handshake messages larger than one record (for now) */
2091 if( ssl->in_msg[1] != 0 ||
2092 ssl->in_msglen < ssl->in_hslen )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002093 {
2094 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
2095 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2096 }
2097
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002098 if( ssl->state != SSL_HANDSHAKE_OVER )
2099 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
2100
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002101 /*
2102 * For DTLS, we move data so that is looks like
2103 * TLS handshake format to other functions.
2104 */
2105#if defined(POLARSSL_SSL_PROTO_DTLS)
2106 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2107 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01002108 // TODO: DTLS: check message_seq
2109
2110 /* For now we don't support fragmentation, so make sure
2111 * fragment_offset == 0 and fragment_length == length */
2112 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
2113 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
2114 {
2115 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
2116 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2117 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002118
2119 memmove( ssl->in_msg + 4, ssl->in_msg + 12, ssl->in_hslen - 12 );
2120 ssl->in_hslen -= 8;
2121 }
2122#endif /* POLARSSL_SSL_PROTO_DTLS */
2123
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002124 return( 0 );
2125}
2126
Paul Bakker5121ce52009-01-03 21:22:43 +00002127int ssl_read_record( ssl_context *ssl )
2128{
Paul Bakker05ef8352012-05-08 09:17:57 +00002129 int ret, done = 0;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002130 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002131
2132 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2133
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01002134 /*
2135 * With DTLS, we cheated on in_hslen to make the handshake message look
2136 * like TLS format, restore the truth now
2137 */
2138#if defined(POLARSSL_SSL_PROTO_DTLS)
2139 if( ssl->in_hslen != 0 && ssl->transport == SSL_TRANSPORT_DATAGRAM )
2140 ssl->in_hslen += 8;
2141#endif
2142
2143 if( ssl->in_hslen != 0 && ssl->in_hslen < ssl->in_msglen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 {
2145 /*
2146 * Get next Handshake message in the current record
2147 */
2148 ssl->in_msglen -= ssl->in_hslen;
2149
Paul Bakker8934a982011-08-05 11:11:53 +00002150 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002151 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002152
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002153 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2154 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
2156 return( 0 );
2157 }
2158
2159 ssl->in_hslen = 0;
2160
2161 /*
2162 * Read the record header and validate it
2163 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002164 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 {
2166 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2167 return( ret );
2168 }
2169
2170 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002171 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
2173 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2174 "version = [%d:%d], msglen = %d",
2175 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002176 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002177
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002178 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
2179
2180 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 {
2182 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002183 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184 }
2185
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002186 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 {
2188 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002189 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 }
2191
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002192 /* Sanity check (outer boundaries) */
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01002193 if( ssl->in_msglen < 1 ||
2194 ssl->in_msglen > SSL_BUFFER_LEN - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002195 {
2196 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2197 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2198 }
2199
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002201 * Make sure the message length is acceptable for the current transform
2202 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 */
Paul Bakker48916f92012-09-16 19:57:18 +00002204 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002206 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 {
2208 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002209 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 }
2211 }
2212 else
2213 {
Paul Bakker48916f92012-09-16 19:57:18 +00002214 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 {
2216 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
2219
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002220#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002222 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
2224 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002225 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002227#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002229#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2230 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002231 /*
2232 * TLS encrypted messages can have up to 256 bytes of padding
2233 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002234 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002235 ssl->in_msglen > ssl->transform_in->minlen +
2236 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 {
2238 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002239 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002241#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 }
2243
2244 /*
2245 * Read and optionally decrypt the message contents
2246 */
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002247 if( ( ret = ssl_fetch_input( ssl,
2248 ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 {
2250 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2251 return( ret );
2252 }
2253
2254 SSL_DEBUG_BUF( 4, "input record from network",
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01002255 ssl->in_hdr, ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002256
Paul Bakker05ef8352012-05-08 09:17:57 +00002257#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002258 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002259 {
2260 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2261
2262 ret = ssl_hw_record_read( ssl );
2263 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2264 {
2265 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002266 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002267 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002268
2269 if( ret == 0 )
2270 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002271 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002272#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002273 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 {
2275 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2276 {
Paul Bakker40865c82013-01-31 17:13:13 +01002277#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2278 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2279 {
2280 ssl_send_alert_message( ssl,
2281 SSL_ALERT_LEVEL_FATAL,
2282 SSL_ALERT_MSG_BAD_RECORD_MAC );
2283 }
2284#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2286 return( ret );
2287 }
2288
2289 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2290 ssl->in_msg, ssl->in_msglen );
2291
2292 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2293 {
2294 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002295 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 }
2297 }
2298
Paul Bakker2770fbd2012-07-03 13:30:23 +00002299#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002300 if( ssl->transform_in != NULL &&
2301 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002302 {
2303 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2304 {
2305 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2306 return( ret );
2307 }
2308
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01002309 // TODO: what's the purpose of these lines? is in_len used?
2310 ssl->in_len[0] = (unsigned char)( ssl->in_msglen >> 8 );
2311 ssl->in_len[1] = (unsigned char)( ssl->in_msglen );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002312 }
2313#endif /* POLARSSL_ZLIB_SUPPORT */
2314
Paul Bakker0a925182012-04-16 06:46:41 +00002315 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2316 ssl->in_msgtype != SSL_MSG_ALERT &&
2317 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2318 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2319 {
2320 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2321
Paul Bakker48916f92012-09-16 19:57:18 +00002322 if( ( ret = ssl_send_alert_message( ssl,
2323 SSL_ALERT_LEVEL_FATAL,
2324 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002325 {
2326 return( ret );
2327 }
2328
2329 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2330 }
2331
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2333 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01002334 if( ( ret = ssl_prepare_handshake_record( ssl ) ) != 0 )
2335 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 }
2337
2338 if( ssl->in_msgtype == SSL_MSG_ALERT )
2339 {
2340 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2341 ssl->in_msg[0], ssl->in_msg[1] ) );
2342
2343 /*
2344 * Ignore non-fatal alerts, except close_notify
2345 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002346 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2349 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002350 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 }
2352
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002353 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2354 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 {
2356 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002357 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002358 }
2359 }
2360
2361 ssl->in_left = 0;
2362
2363 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2364
2365 return( 0 );
2366}
2367
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002368int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2369{
2370 int ret;
2371
2372 if( ( ret = ssl_send_alert_message( ssl,
2373 SSL_ALERT_LEVEL_FATAL,
2374 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2375 {
2376 return( ret );
2377 }
2378
2379 return( 0 );
2380}
2381
Paul Bakker0a925182012-04-16 06:46:41 +00002382int ssl_send_alert_message( ssl_context *ssl,
2383 unsigned char level,
2384 unsigned char message )
2385{
2386 int ret;
2387
2388 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2389
2390 ssl->out_msgtype = SSL_MSG_ALERT;
2391 ssl->out_msglen = 2;
2392 ssl->out_msg[0] = level;
2393 ssl->out_msg[1] = message;
2394
2395 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2396 {
2397 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2398 return( ret );
2399 }
2400
2401 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2402
2403 return( 0 );
2404}
2405
Paul Bakker5121ce52009-01-03 21:22:43 +00002406/*
2407 * Handshake functions
2408 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002409#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2410 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2411 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2412 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2413 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2414 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2415 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002416int ssl_write_certificate( ssl_context *ssl )
2417{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002418 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2421
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002423 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2424 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002425 {
2426 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2427 ssl->state++;
2428 return( 0 );
2429 }
2430
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002431 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2432 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433}
2434
2435int ssl_parse_certificate( ssl_context *ssl )
2436{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002437 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2438
2439 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2440
2441 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002442 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2443 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002444 {
2445 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2446 ssl->state++;
2447 return( 0 );
2448 }
2449
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002450 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2451 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002452}
2453#else
2454int ssl_write_certificate( ssl_context *ssl )
2455{
2456 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2457 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002458 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002459 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2460
2461 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2462
2463 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2465 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002466 {
2467 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2468 ssl->state++;
2469 return( 0 );
2470 }
2471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 if( ssl->endpoint == SSL_IS_CLIENT )
2473 {
2474 if( ssl->client_auth == 0 )
2475 {
2476 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2477 ssl->state++;
2478 return( 0 );
2479 }
2480
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002481#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 /*
2483 * If using SSLv3 and got no cert, send an Alert message
2484 * (otherwise an empty Certificate message will be sent).
2485 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002486 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2488 {
2489 ssl->out_msglen = 2;
2490 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002491 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2492 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002493
2494 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2495 goto write_msg;
2496 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002497#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 }
2499 else /* SSL_IS_SERVER */
2500 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002501 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
2503 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002504 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 }
2506 }
2507
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002508 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509
2510 /*
2511 * 0 . 0 handshake type
2512 * 1 . 3 handshake length
2513 * 4 . 6 length of all certs
2514 * 7 . 9 length of cert. 1
2515 * 10 . n-1 peer certificate
2516 * n . n+2 length of cert. 2
2517 * n+3 . ... upper level cert, etc.
2518 */
2519 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002520 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521
Paul Bakker29087132010-03-21 21:03:34 +00002522 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 {
2524 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002525 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 {
2527 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2528 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002529 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002530 }
2531
2532 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2533 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2534 ssl->out_msg[i + 2] = (unsigned char)( n );
2535
2536 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2537 i += n; crt = crt->next;
2538 }
2539
2540 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2541 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2542 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2543
2544 ssl->out_msglen = i;
2545 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2546 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2547
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002548#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002549write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002550#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002551
2552 ssl->state++;
2553
2554 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2555 {
2556 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2557 return( ret );
2558 }
2559
2560 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2561
Paul Bakkered27a042013-04-18 22:46:23 +02002562 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002563}
2564
2565int ssl_parse_certificate( ssl_context *ssl )
2566{
Paul Bakkered27a042013-04-18 22:46:23 +02002567 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002568 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002569 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
2571 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2572
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002574 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2575 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002576 {
2577 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2578 ssl->state++;
2579 return( 0 );
2580 }
2581
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002583 ( ssl->authmode == SSL_VERIFY_NONE ||
2584 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002586 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002587 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2588 ssl->state++;
2589 return( 0 );
2590 }
2591
2592 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2593 {
2594 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2595 return( ret );
2596 }
2597
2598 ssl->state++;
2599
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002600#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 /*
2602 * Check if the client sent an empty certificate
2603 */
2604 if( ssl->endpoint == SSL_IS_SERVER &&
2605 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2606 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002607 if( ssl->in_msglen == 2 &&
2608 ssl->in_msgtype == SSL_MSG_ALERT &&
2609 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2610 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002611 {
2612 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2613
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002614 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2616 return( 0 );
2617 else
Paul Bakker40e46942009-01-03 21:51:57 +00002618 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002621#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002623#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2624 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 if( ssl->endpoint == SSL_IS_SERVER &&
2626 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2627 {
2628 if( ssl->in_hslen == 7 &&
2629 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2630 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2631 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2632 {
2633 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2634
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002635 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002637 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 else
2639 return( 0 );
2640 }
2641 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002642#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2643 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2646 {
2647 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002648 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 }
2650
2651 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002654 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
2657 /*
2658 * Same message structure as in ssl_write_certificate()
2659 */
2660 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2661
2662 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2663 {
2664 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002665 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 }
2667
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002668 /* In case we tried to reuse a session but it failed */
2669 if( ssl->session_negotiate->peer_cert != NULL )
2670 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002671 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002672 polarssl_free( ssl->session_negotiate->peer_cert );
2673 }
2674
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002675 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2676 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 {
2678 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002679 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002680 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002681 }
2682
Paul Bakkerb6b09562013-09-18 14:17:41 +02002683 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684
2685 i = 7;
2686
2687 while( i < ssl->in_hslen )
2688 {
2689 if( ssl->in_msg[i] != 0 )
2690 {
2691 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002692 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 }
2694
2695 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2696 | (unsigned int) ssl->in_msg[i + 2];
2697 i += 3;
2698
2699 if( n < 128 || i + n > ssl->in_hslen )
2700 {
2701 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002702 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 }
2704
Paul Bakkerddf26b42013-09-18 13:46:23 +02002705 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2706 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 if( ret != 0 )
2708 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002709 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 return( ret );
2711 }
2712
2713 i += n;
2714 }
2715
Paul Bakker48916f92012-09-16 19:57:18 +00002716 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002718 /*
2719 * On client, make sure the server cert doesn't change during renego to
2720 * avoid "triple handshake" attack: https://secure-resumption.com/
2721 */
2722 if( ssl->endpoint == SSL_IS_CLIENT &&
2723 ssl->renegotiation == SSL_RENEGOTIATION )
2724 {
2725 if( ssl->session->peer_cert == NULL )
2726 {
2727 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2728 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2729 }
2730
2731 if( ssl->session->peer_cert->raw.len !=
2732 ssl->session_negotiate->peer_cert->raw.len ||
2733 memcmp( ssl->session->peer_cert->raw.p,
2734 ssl->session_negotiate->peer_cert->raw.p,
2735 ssl->session->peer_cert->raw.len ) != 0 )
2736 {
2737 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2738 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2739 }
2740 }
2741
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 if( ssl->authmode != SSL_VERIFY_NONE )
2743 {
2744 if( ssl->ca_chain == NULL )
2745 {
2746 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002747 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748 }
2749
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002750 /*
2751 * Main check: verify certificate
2752 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002753 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2754 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2755 &ssl->session_negotiate->verify_result,
2756 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757
2758 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002759 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002761 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002762
2763 /*
2764 * Secondary checks: always done, but change 'ret' only if it was 0
2765 */
2766
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002767#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002768 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002769 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002770
2771 /* If certificate uses an EC key, make sure the curve is OK */
2772 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2773 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2774 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002775 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002776 if( ret == 0 )
2777 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002778 }
2779 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002780#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002782 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2783 ciphersuite_info,
2784 ! ssl->endpoint ) != 0 )
2785 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002786 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002787 if( ret == 0 )
2788 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2789 }
2790
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2792 ret = 0;
2793 }
2794
2795 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2796
2797 return( ret );
2798}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002799#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2800 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2801 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2802 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2803 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2804 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2805 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002806
2807int ssl_write_change_cipher_spec( ssl_context *ssl )
2808{
2809 int ret;
2810
2811 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2812
2813 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2814 ssl->out_msglen = 1;
2815 ssl->out_msg[0] = 1;
2816
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 ssl->state++;
2818
2819 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2820 {
2821 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2822 return( ret );
2823 }
2824
2825 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2826
2827 return( 0 );
2828}
2829
2830int ssl_parse_change_cipher_spec( ssl_context *ssl )
2831{
2832 int ret;
2833
2834 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2835
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2837 {
2838 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2839 return( ret );
2840 }
2841
2842 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2843 {
2844 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002845 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 }
2847
2848 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2849 {
2850 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002851 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002852 }
2853
2854 ssl->state++;
2855
2856 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2857
2858 return( 0 );
2859}
2860
Paul Bakker41c83d32013-03-20 14:39:14 +01002861void ssl_optimize_checksum( ssl_context *ssl,
2862 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002863{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002864 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002865
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002866#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2867 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002868 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002869 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002870 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#endif
2872#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2873#if defined(POLARSSL_SHA512_C)
2874 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2875 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2876 else
2877#endif
2878#if defined(POLARSSL_SHA256_C)
2879 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002880 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002881 else
2882#endif
2883#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002884 {
2885 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002886 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002887 }
Paul Bakker380da532012-04-18 16:10:25 +00002888}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002889
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002890static void ssl_update_checksum_start( ssl_context *ssl,
2891 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002892{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002893#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2894 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002895 md5_update( &ssl->handshake->fin_md5 , buf, len );
2896 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002897#endif
2898#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2899#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002900 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002901#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002902#if defined(POLARSSL_SHA512_C)
2903 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002904#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002905#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002906}
2907
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002908#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2909 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002910static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2911 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002912{
Paul Bakker48916f92012-09-16 19:57:18 +00002913 md5_update( &ssl->handshake->fin_md5 , buf, len );
2914 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002916#endif
Paul Bakker380da532012-04-18 16:10:25 +00002917
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002918#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2919#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002920static void ssl_update_checksum_sha256( ssl_context *ssl,
2921 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002922{
Paul Bakker9e36f042013-06-30 14:34:05 +02002923 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002924}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#endif
Paul Bakker380da532012-04-18 16:10:25 +00002926
Paul Bakker9e36f042013-06-30 14:34:05 +02002927#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002928static void ssl_update_checksum_sha384( ssl_context *ssl,
2929 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002930{
Paul Bakker9e36f042013-06-30 14:34:05 +02002931 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002932}
Paul Bakker769075d2012-11-24 11:26:46 +01002933#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002935
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002936#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937static void ssl_calc_finished_ssl(
2938 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002939{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002940 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941 md5_context md5;
2942 sha1_context sha1;
2943
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 unsigned char padbuf[48];
2945 unsigned char md5sum[16];
2946 unsigned char sha1sum[20];
2947
Paul Bakker48916f92012-09-16 19:57:18 +00002948 ssl_session *session = ssl->session_negotiate;
2949 if( !session )
2950 session = ssl->session;
2951
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2953
Paul Bakker48916f92012-09-16 19:57:18 +00002954 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2955 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002956
2957 /*
2958 * SSLv3:
2959 * hash =
2960 * MD5( master + pad2 +
2961 * MD5( handshake + sender + master + pad1 ) )
2962 * + SHA1( master + pad2 +
2963 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002964 */
2965
Paul Bakker90995b52013-06-24 19:20:35 +02002966#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002967 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002969#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Paul Bakker90995b52013-06-24 19:20:35 +02002971#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002972 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002974#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Paul Bakker3c2122f2013-06-24 19:03:14 +02002976 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2977 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Paul Bakker3c2122f2013-06-24 19:03:14 +02002981 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002982 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983 md5_update( &md5, padbuf, 48 );
2984 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Paul Bakker3c2122f2013-06-24 19:03:14 +02002986 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002987 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988 sha1_update( &sha1, padbuf, 40 );
2989 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002992
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002994 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995 md5_update( &md5, padbuf, 48 );
2996 md5_update( &md5, md5sum, 16 );
2997 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003000 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003001 sha1_update( &sha1, padbuf , 40 );
3002 sha1_update( &sha1, sha1sum, 20 );
3003 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003004
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Paul Bakker5b4af392014-06-26 12:09:34 +02003007 md5_free( &md5 );
3008 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
Paul Bakker34617722014-06-13 17:20:13 +02003010 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3011 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3012 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003013
3014 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3015}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003016#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003018#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019static void ssl_calc_finished_tls(
3020 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003023 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003027
Paul Bakker48916f92012-09-16 19:57:18 +00003028 ssl_session *session = ssl->session_negotiate;
3029 if( !session )
3030 session = ssl->session;
3031
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
Paul Bakker48916f92012-09-16 19:57:18 +00003034 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3035 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 /*
3038 * TLSv1:
3039 * hash = PRF( master, finished_label,
3040 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3041 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003042
Paul Bakker90995b52013-06-24 19:20:35 +02003043#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3045 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003046#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003047
Paul Bakker90995b52013-06-24 19:20:35 +02003048#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003049 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3050 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003051#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052
3053 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003054 ? "client finished"
3055 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003056
3057 md5_finish( &md5, padbuf );
3058 sha1_finish( &sha1, padbuf + 16 );
3059
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003060 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003061 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003062
3063 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3064
Paul Bakker5b4af392014-06-26 12:09:34 +02003065 md5_free( &md5 );
3066 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003067
Paul Bakker34617722014-06-13 17:20:13 +02003068 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069
3070 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3071}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003072#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003074#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3075#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003076static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003077 ssl_context *ssl, unsigned char *buf, int from )
3078{
3079 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003080 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003081 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003082 unsigned char padbuf[32];
3083
Paul Bakker48916f92012-09-16 19:57:18 +00003084 ssl_session *session = ssl->session_negotiate;
3085 if( !session )
3086 session = ssl->session;
3087
Paul Bakker380da532012-04-18 16:10:25 +00003088 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003089
Paul Bakker9e36f042013-06-30 14:34:05 +02003090 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003091
3092 /*
3093 * TLSv1.2:
3094 * hash = PRF( master, finished_label,
3095 * Hash( handshake ) )[0.11]
3096 */
3097
Paul Bakker9e36f042013-06-30 14:34:05 +02003098#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003100 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003101#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102
3103 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003104 ? "client finished"
3105 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106
Paul Bakker9e36f042013-06-30 14:34:05 +02003107 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003108
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003109 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003110 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003111
3112 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3113
Paul Bakker5b4af392014-06-26 12:09:34 +02003114 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115
Paul Bakker34617722014-06-13 17:20:13 +02003116 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117
3118 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3119}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003120#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003121
Paul Bakker9e36f042013-06-30 14:34:05 +02003122#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003123static void ssl_calc_finished_tls_sha384(
3124 ssl_context *ssl, unsigned char *buf, int from )
3125{
3126 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003127 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003128 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003129 unsigned char padbuf[48];
3130
Paul Bakker48916f92012-09-16 19:57:18 +00003131 ssl_session *session = ssl->session_negotiate;
3132 if( !session )
3133 session = ssl->session;
3134
Paul Bakker380da532012-04-18 16:10:25 +00003135 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003136
Paul Bakker9e36f042013-06-30 14:34:05 +02003137 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003138
3139 /*
3140 * TLSv1.2:
3141 * hash = PRF( master, finished_label,
3142 * Hash( handshake ) )[0.11]
3143 */
3144
Paul Bakker9e36f042013-06-30 14:34:05 +02003145#if !defined(POLARSSL_SHA512_ALT)
3146 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3147 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003148#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003149
3150 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003151 ? "client finished"
3152 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003153
Paul Bakker9e36f042013-06-30 14:34:05 +02003154 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003155
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003156 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003157 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003158
3159 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3160
Paul Bakker5b4af392014-06-26 12:09:34 +02003161 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003162
Paul Bakker34617722014-06-13 17:20:13 +02003163 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003164
3165 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3166}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003167#endif /* POLARSSL_SHA512_C */
3168#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003169
Paul Bakker48916f92012-09-16 19:57:18 +00003170void ssl_handshake_wrapup( ssl_context *ssl )
3171{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003172 int resume = ssl->handshake->resume;
3173
Paul Bakker48916f92012-09-16 19:57:18 +00003174 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3175
3176 /*
3177 * Free our handshake params
3178 */
3179 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003180 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003181 ssl->handshake = NULL;
3182
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003183 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003184 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003185 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003186 ssl->renego_records_seen = 0;
3187 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003188
Paul Bakker48916f92012-09-16 19:57:18 +00003189 /*
3190 * Switch in our now active transform context
3191 */
3192 if( ssl->transform )
3193 {
3194 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003195 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003196 }
3197 ssl->transform = ssl->transform_negotiate;
3198 ssl->transform_negotiate = NULL;
3199
Paul Bakker0a597072012-09-25 21:55:46 +00003200 if( ssl->session )
3201 {
3202 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003203 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003204 }
3205 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003206 ssl->session_negotiate = NULL;
3207
Paul Bakker0a597072012-09-25 21:55:46 +00003208 /*
3209 * Add cache entry
3210 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003211 if( ssl->f_set_cache != NULL &&
3212 ssl->session->length != 0 &&
3213 resume == 0 )
3214 {
Paul Bakker0a597072012-09-25 21:55:46 +00003215 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3216 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003217 }
Paul Bakker0a597072012-09-25 21:55:46 +00003218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 ssl->state++;
3220
3221 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3222}
3223
Paul Bakker1ef83d62012-04-11 12:09:53 +00003224int ssl_write_finished( ssl_context *ssl )
3225{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003226 int ret, hash_len, i;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003227
3228 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3229
Paul Bakker92be97b2013-01-02 17:30:03 +01003230 /*
3231 * Set the out_msg pointer to the correct location based on IV length
3232 */
3233 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3234 {
3235 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3236 ssl->transform_negotiate->fixed_ivlen;
3237 }
3238 else
3239 ssl->out_msg = ssl->out_iv;
3240
Paul Bakker48916f92012-09-16 19:57:18 +00003241 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003242
3243 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3245
Paul Bakker48916f92012-09-16 19:57:18 +00003246 ssl->verify_data_len = hash_len;
3247 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3248
Paul Bakker5121ce52009-01-03 21:22:43 +00003249 ssl->out_msglen = 4 + hash_len;
3250 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3251 ssl->out_msg[0] = SSL_HS_FINISHED;
3252
3253 /*
3254 * In case of session resuming, invert the client and server
3255 * ChangeCipherSpec messages order.
3256 */
Paul Bakker0a597072012-09-25 21:55:46 +00003257 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 {
3259 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003260 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003261 else
3262 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3263 }
3264 else
3265 ssl->state++;
3266
Paul Bakker48916f92012-09-16 19:57:18 +00003267 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003268 * Switch to our negotiated transform and session parameters for outbound
3269 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003270 */
3271 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3272 ssl->transform_out = ssl->transform_negotiate;
3273 ssl->session_out = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003274
3275 memset( ssl->out_ctr + ssl_ep_len( ssl ), 0, 8 - ssl_ep_len( ssl ) );
3276 for( i = ssl_ep_len( ssl ); i > 0; i-- )
3277 if( ++ssl->out_ctr[i - 1] != 0 )
3278 break;
3279 // TODO: abort on epoch wrap!
Paul Bakker5121ce52009-01-03 21:22:43 +00003280
Paul Bakker07eb38b2012-12-19 14:42:06 +01003281#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003282 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003283 {
3284 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3285 {
3286 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3287 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3288 }
3289 }
3290#endif
3291
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3293 {
3294 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3295 return( ret );
3296 }
3297
3298 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3299
3300 return( 0 );
3301}
3302
3303int ssl_parse_finished( ssl_context *ssl )
3304{
Paul Bakker23986e52011-04-24 08:57:21 +00003305 int ret;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003306 unsigned int hash_len, i;
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 unsigned char buf[36];
3308
3309 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3310
Paul Bakker48916f92012-09-16 19:57:18 +00003311 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312
Paul Bakker48916f92012-09-16 19:57:18 +00003313 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003314 * Switch to our negotiated transform and session parameters for inbound
3315 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003316 */
3317 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3318 ssl->transform_in = ssl->transform_negotiate;
3319 ssl->session_in = ssl->session_negotiate;
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003320
3321 /* Input counter/epoch not used with DTLS right now,
3322 * but it doesn't hurt to have this part ready */
3323 memset( ssl->in_ctr + ssl_ep_len( ssl ), 0, 8 - ssl_ep_len( ssl ) );
3324 for( i = ssl_ep_len( ssl ); i > 0; i-- )
3325 if( ++ssl->in_ctr[i - 1] != 0 )
3326 break;
3327 // TODO: abort on epoch wrap!
Paul Bakker5121ce52009-01-03 21:22:43 +00003328
Paul Bakker92be97b2013-01-02 17:30:03 +01003329 /*
3330 * Set the in_msg pointer to the correct location based on IV length
3331 */
3332 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3333 {
3334 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3335 ssl->transform_negotiate->fixed_ivlen;
3336 }
3337 else
3338 ssl->in_msg = ssl->in_iv;
3339
Paul Bakker07eb38b2012-12-19 14:42:06 +01003340#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003341 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003342 {
3343 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3344 {
3345 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3346 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3347 }
3348 }
3349#endif
3350
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3352 {
3353 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3354 return( ret );
3355 }
3356
3357 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3358 {
3359 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003360 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 }
3362
Paul Bakker1ef83d62012-04-11 12:09:53 +00003363 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003364 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3365
3366 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3367 ssl->in_hslen != 4 + hash_len )
3368 {
3369 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003370 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003371 }
3372
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003373 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 {
3375 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003376 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003377 }
3378
Paul Bakker48916f92012-09-16 19:57:18 +00003379 ssl->verify_data_len = hash_len;
3380 memcpy( ssl->peer_verify_data, buf, hash_len );
3381
Paul Bakker0a597072012-09-25 21:55:46 +00003382 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 {
3384 if( ssl->endpoint == SSL_IS_CLIENT )
3385 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3386
3387 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003388 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003389 }
3390 else
3391 ssl->state++;
3392
3393 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3394
3395 return( 0 );
3396}
3397
Paul Bakker968afaa2014-07-09 11:09:24 +02003398static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003399{
3400 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3401
3402#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3403 defined(POLARSSL_SSL_PROTO_TLS1_1)
3404 md5_init( &handshake->fin_md5 );
3405 sha1_init( &handshake->fin_sha1 );
3406 md5_starts( &handshake->fin_md5 );
3407 sha1_starts( &handshake->fin_sha1 );
3408#endif
3409#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3410#if defined(POLARSSL_SHA256_C)
3411 sha256_init( &handshake->fin_sha256 );
3412 sha256_starts( &handshake->fin_sha256, 0 );
3413#endif
3414#if defined(POLARSSL_SHA512_C)
3415 sha512_init( &handshake->fin_sha512 );
3416 sha512_starts( &handshake->fin_sha512, 1 );
3417#endif
3418#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3419
3420 handshake->update_checksum = ssl_update_checksum_start;
3421 handshake->sig_alg = SSL_HASH_SHA1;
3422
3423#if defined(POLARSSL_DHM_C)
3424 dhm_init( &handshake->dhm_ctx );
3425#endif
3426#if defined(POLARSSL_ECDH_C)
3427 ecdh_init( &handshake->ecdh_ctx );
3428#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003429}
3430
3431static void ssl_transform_init( ssl_transform *transform )
3432{
3433 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003434
3435 cipher_init( &transform->cipher_ctx_enc );
3436 cipher_init( &transform->cipher_ctx_dec );
3437
3438 md_init( &transform->md_ctx_enc );
3439 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003440}
3441
3442void ssl_session_init( ssl_session *session )
3443{
3444 memset( session, 0, sizeof(ssl_session) );
3445}
3446
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003447static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003448{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003449 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003450 if( ssl->transform_negotiate )
3451 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003452 if( ssl->session_negotiate )
3453 ssl_session_free( ssl->session_negotiate );
3454 if( ssl->handshake )
3455 ssl_handshake_free( ssl->handshake );
3456
3457 /*
3458 * Either the pointers are now NULL or cleared properly and can be freed.
3459 * Now allocate missing structures.
3460 */
3461 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003462 {
3463 ssl->transform_negotiate =
3464 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3465 }
Paul Bakker48916f92012-09-16 19:57:18 +00003466
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003467 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003468 {
3469 ssl->session_negotiate =
3470 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3471 }
Paul Bakker48916f92012-09-16 19:57:18 +00003472
Paul Bakker82788fb2014-10-20 13:59:19 +02003473 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003474 {
3475 ssl->handshake = (ssl_handshake_params *)
3476 polarssl_malloc( sizeof(ssl_handshake_params) );
3477 }
Paul Bakker48916f92012-09-16 19:57:18 +00003478
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003479 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003480 if( ssl->handshake == NULL ||
3481 ssl->transform_negotiate == NULL ||
3482 ssl->session_negotiate == NULL )
3483 {
3484 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003485
3486 polarssl_free( ssl->handshake );
3487 polarssl_free( ssl->transform_negotiate );
3488 polarssl_free( ssl->session_negotiate );
3489
3490 ssl->handshake = NULL;
3491 ssl->transform_negotiate = NULL;
3492 ssl->session_negotiate = NULL;
3493
Paul Bakker48916f92012-09-16 19:57:18 +00003494 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3495 }
3496
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003497 /* Initialize structures */
3498 ssl_session_init( ssl->session_negotiate );
3499 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003500 ssl_handshake_params_init( ssl->handshake );
3501
3502#if defined(POLARSSL_X509_CRT_PARSE_C)
3503 ssl->handshake->key_cert = ssl->key_cert;
3504#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003505
Paul Bakker48916f92012-09-16 19:57:18 +00003506 return( 0 );
3507}
3508
Paul Bakker5121ce52009-01-03 21:22:43 +00003509/*
3510 * Initialize an SSL context
3511 */
3512int ssl_init( ssl_context *ssl )
3513{
Paul Bakker48916f92012-09-16 19:57:18 +00003514 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 int len = SSL_BUFFER_LEN;
3516
3517 memset( ssl, 0, sizeof( ssl_context ) );
3518
Paul Bakker62f2dee2012-09-28 07:31:51 +00003519 /*
3520 * Sane defaults
3521 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003522 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3523 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3524 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3525 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003526
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003527 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003528
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003529 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3530
Paul Bakker62f2dee2012-09-28 07:31:51 +00003531#if defined(POLARSSL_DHM_C)
3532 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3533 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3534 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3535 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3536 {
3537 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3538 return( ret );
3539 }
3540#endif
3541
3542 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003543 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00003544 */
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003545 ssl->in_buf = (unsigned char *) polarssl_malloc( len );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003546 ssl->out_buf = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003547
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003548 if( ssl->in_buf == NULL || ssl->out_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 {
3550 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003551 polarssl_free( ssl->in_buf );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003552 polarssl_free( ssl->out_buf );
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003553 ssl->in_buf = NULL;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003554 ssl->out_buf = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003555 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556 }
3557
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003558 memset( ssl-> in_buf, 0, SSL_BUFFER_LEN );
3559 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003561 /* No error is possible, SSL_TRANSPORT_STREAM always valid */
3562 (void) ssl_set_transport( ssl, SSL_TRANSPORT_STREAM );
3563
Paul Bakker606b4ba2013-08-14 16:52:14 +02003564#if defined(POLARSSL_SSL_SESSION_TICKETS)
3565 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3566#endif
3567
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003568#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003569 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003570#endif
3571
Paul Bakker48916f92012-09-16 19:57:18 +00003572 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3573 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
3575 return( 0 );
3576}
3577
3578/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003579 * Reset an initialized and used SSL context for re-use while retaining
3580 * all application-set variables, function pointers and data.
3581 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003582int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003583{
Paul Bakker48916f92012-09-16 19:57:18 +00003584 int ret;
3585
Paul Bakker7eb013f2011-10-06 12:37:39 +00003586 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003587 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3588 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3589
3590 ssl->verify_data_len = 0;
3591 memset( ssl->own_verify_data, 0, 36 );
3592 memset( ssl->peer_verify_data, 0, 36 );
3593
Paul Bakker7eb013f2011-10-06 12:37:39 +00003594 ssl->in_offt = NULL;
3595
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003596 ssl->in_msg = ssl->in_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003597 ssl->in_msgtype = 0;
3598 ssl->in_msglen = 0;
3599 ssl->in_left = 0;
3600
3601 ssl->in_hslen = 0;
3602 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003603 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003604
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003605 ssl->out_msg = ssl->out_buf + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003606 ssl->out_msgtype = 0;
3607 ssl->out_msglen = 0;
3608 ssl->out_left = 0;
3609
Paul Bakker48916f92012-09-16 19:57:18 +00003610 ssl->transform_in = NULL;
3611 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003612
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003613 ssl->renego_records_seen = 0;
3614
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01003615 memset( ssl->out_buf, 0, SSL_BUFFER_LEN );
3616 memset( ssl->in_buf, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003617
3618#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003619 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003620 {
3621 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003622 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003623 {
3624 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3625 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3626 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003627 }
3628#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003629
Paul Bakker48916f92012-09-16 19:57:18 +00003630 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003631 {
Paul Bakker48916f92012-09-16 19:57:18 +00003632 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003633 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003634 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003635 }
Paul Bakker48916f92012-09-16 19:57:18 +00003636
Paul Bakkerc0463502013-02-14 11:19:38 +01003637 if( ssl->session )
3638 {
3639 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003640 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003641 ssl->session = NULL;
3642 }
3643
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003644#if defined(POLARSSL_SSL_ALPN)
3645 ssl->alpn_chosen = NULL;
3646#endif
3647
Paul Bakker48916f92012-09-16 19:57:18 +00003648 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3649 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003650
3651 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003652}
3653
Paul Bakkera503a632013-08-14 13:48:06 +02003654#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003655static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3656{
3657 aes_free( &tkeys->enc );
3658 aes_free( &tkeys->dec );
3659
3660 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3661}
3662
Paul Bakker7eb013f2011-10-06 12:37:39 +00003663/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003664 * Allocate and initialize ticket keys
3665 */
3666static int ssl_ticket_keys_init( ssl_context *ssl )
3667{
3668 int ret;
3669 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003670 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003671
3672 if( ssl->ticket_keys != NULL )
3673 return( 0 );
3674
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003675 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3676 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003677 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3678
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003679 aes_init( &tkeys->enc );
3680 aes_init( &tkeys->dec );
3681
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003682 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003683 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003684 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003685 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003686 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003687 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003688
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003689 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3690 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3691 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3692 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003693 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003694 polarssl_free( tkeys );
3695 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003696 }
3697
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003698 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003699 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003700 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003701 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003702 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003703 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003704
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003705 ssl->ticket_keys = tkeys;
3706
3707 return( 0 );
3708}
Paul Bakkera503a632013-08-14 13:48:06 +02003709#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003710
3711/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 * SSL set accessors
3713 */
3714void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3715{
3716 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003717
Paul Bakker606b4ba2013-08-14 16:52:14 +02003718#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003719 if( endpoint == SSL_IS_CLIENT )
3720 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003721#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003722}
3723
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003724int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003725{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003726#if defined(POLARSSL_SSL_PROTO_DTLS)
3727 if( transport == SSL_TRANSPORT_DATAGRAM )
3728 {
3729 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003730
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003731 ssl->out_hdr = ssl->out_buf;
3732 ssl->out_ctr = ssl->out_buf + 3;
3733 ssl->out_len = ssl->out_buf + 11;
3734 ssl->out_iv = ssl->out_buf + 13;
3735 ssl->out_msg = ssl->out_buf + 13;
3736
3737 ssl->in_hdr = ssl->in_buf;
3738 ssl->in_ctr = ssl->in_buf + 3;
3739 ssl->in_len = ssl->in_buf + 11;
3740 ssl->in_iv = ssl->in_buf + 13;
3741 ssl->in_msg = ssl->in_buf + 13;
3742
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003743 /* DTLS starts with TLS1.1 */
3744 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
3745 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003746
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003747 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
3748 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
3749
3750 return( 0 );
3751 }
3752#endif
3753
3754 if( transport == SSL_TRANSPORT_STREAM )
3755 {
3756 ssl->transport = transport;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003757
3758 ssl->out_ctr = ssl->out_buf;
3759 ssl->out_hdr = ssl->out_buf + 8;
3760 ssl->out_len = ssl->out_buf + 11;
3761 ssl->out_iv = ssl->out_buf + 13;
3762 ssl->out_msg = ssl->out_buf + 13;
3763
3764 ssl->in_ctr = ssl->in_buf;
3765 ssl->in_hdr = ssl->in_buf + 8;
3766 ssl->in_len = ssl->in_buf + 11;
3767 ssl->in_iv = ssl->in_buf + 13;
3768 ssl->in_msg = ssl->in_buf + 13;
3769
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003770 return( 0 );
3771 }
3772
3773 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003774}
3775
Paul Bakker5121ce52009-01-03 21:22:43 +00003776void ssl_set_authmode( ssl_context *ssl, int authmode )
3777{
3778 ssl->authmode = authmode;
3779}
3780
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003781#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003782void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003783 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003784 void *p_vrfy )
3785{
3786 ssl->f_vrfy = f_vrfy;
3787 ssl->p_vrfy = p_vrfy;
3788}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003789#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003790
Paul Bakker5121ce52009-01-03 21:22:43 +00003791void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003792 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003793 void *p_rng )
3794{
3795 ssl->f_rng = f_rng;
3796 ssl->p_rng = p_rng;
3797}
3798
3799void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003800 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 void *p_dbg )
3802{
3803 ssl->f_dbg = f_dbg;
3804 ssl->p_dbg = p_dbg;
3805}
3806
3807void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003808 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003809 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003810{
3811 ssl->f_recv = f_recv;
3812 ssl->f_send = f_send;
3813 ssl->p_recv = p_recv;
3814 ssl->p_send = p_send;
3815}
3816
Paul Bakker0a597072012-09-25 21:55:46 +00003817void ssl_set_session_cache( ssl_context *ssl,
3818 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3819 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003820{
Paul Bakker0a597072012-09-25 21:55:46 +00003821 ssl->f_get_cache = f_get_cache;
3822 ssl->p_get_cache = p_get_cache;
3823 ssl->f_set_cache = f_set_cache;
3824 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003825}
3826
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003827int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003828{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003829 int ret;
3830
3831 if( ssl == NULL ||
3832 session == NULL ||
3833 ssl->session_negotiate == NULL ||
3834 ssl->endpoint != SSL_IS_CLIENT )
3835 {
3836 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3837 }
3838
3839 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3840 return( ret );
3841
Paul Bakker0a597072012-09-25 21:55:46 +00003842 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003843
3844 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003845}
3846
Paul Bakkerb68cad62012-08-23 08:34:18 +00003847void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003848{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003849 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3850 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3851 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3852 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3853}
3854
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003855void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3856 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003857 int major, int minor )
3858{
3859 if( major != SSL_MAJOR_VERSION_3 )
3860 return;
3861
3862 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3863 return;
3864
3865 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003866}
3867
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003868#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003869/* Add a new (empty) key_cert entry an return a pointer to it */
3870static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3871{
3872 ssl_key_cert *key_cert, *last;
3873
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003874 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3875 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003876 return( NULL );
3877
3878 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3879
3880 /* Append the new key_cert to the (possibly empty) current list */
3881 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003882 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003883 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003884 if( ssl->handshake != NULL )
3885 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003886 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003887 else
3888 {
3889 last = ssl->key_cert;
3890 while( last->next != NULL )
3891 last = last->next;
3892 last->next = key_cert;
3893 }
3894
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003895 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003896}
3897
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003898void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003899 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003900{
3901 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003902 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003903 ssl->peer_cn = peer_cn;
3904}
3905
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003906int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003907 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003908{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003909 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3910
3911 if( key_cert == NULL )
3912 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3913
3914 key_cert->cert = own_cert;
3915 key_cert->key = pk_key;
3916
3917 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003918}
3919
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003920#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003921int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003922 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003923{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003924 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003925 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003926
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003927 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003928 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3929
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003930 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3931 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003932 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003933
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003934 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003935
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003936 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003937 if( ret != 0 )
3938 return( ret );
3939
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003940 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003941 return( ret );
3942
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003943 key_cert->cert = own_cert;
3944 key_cert->key_own_alloc = 1;
3945
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003946 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003947}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003948#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003950int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003951 void *rsa_key,
3952 rsa_decrypt_func rsa_decrypt,
3953 rsa_sign_func rsa_sign,
3954 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003955{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003956 int ret;
3957 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003958
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003959 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003960 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3961
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003962 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3963 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003964 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003965
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003966 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003967
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003968 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3969 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3970 return( ret );
3971
3972 key_cert->cert = own_cert;
3973 key_cert->key_own_alloc = 1;
3974
3975 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003976}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003977#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003978
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003979#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003980int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3981 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003982{
Paul Bakker6db455e2013-09-18 17:29:31 +02003983 if( psk == NULL || psk_identity == NULL )
3984 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3985
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003986 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003987 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3988
Paul Bakker6db455e2013-09-18 17:29:31 +02003989 if( ssl->psk != NULL )
3990 {
3991 polarssl_free( ssl->psk );
3992 polarssl_free( ssl->psk_identity );
3993 }
3994
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003995 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003996 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003997
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003998 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003999 ssl->psk_identity = (unsigned char *)
4000 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004001
4002 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4003 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4004
4005 memcpy( ssl->psk, psk, ssl->psk_len );
4006 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004007
4008 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004009}
4010
4011void ssl_set_psk_cb( ssl_context *ssl,
4012 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4013 size_t),
4014 void *p_psk )
4015{
4016 ssl->f_psk = f_psk;
4017 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004018}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004019#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004020
Paul Bakker48916f92012-09-16 19:57:18 +00004021#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004022int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004023{
4024 int ret;
4025
Paul Bakker48916f92012-09-16 19:57:18 +00004026 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004027 {
4028 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4029 return( ret );
4030 }
4031
Paul Bakker48916f92012-09-16 19:57:18 +00004032 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004033 {
4034 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4035 return( ret );
4036 }
4037
4038 return( 0 );
4039}
4040
Paul Bakker1b57b062011-01-06 15:48:19 +00004041int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4042{
4043 int ret;
4044
Paul Bakker66d5d072014-06-17 16:39:18 +02004045 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004046 {
4047 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4048 return( ret );
4049 }
4050
Paul Bakker66d5d072014-06-17 16:39:18 +02004051 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004052 {
4053 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4054 return( ret );
4055 }
4056
4057 return( 0 );
4058}
Paul Bakker48916f92012-09-16 19:57:18 +00004059#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004060
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004061#if defined(POLARSSL_SSL_SET_CURVES)
4062/*
4063 * Set the allowed elliptic curves
4064 */
4065void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4066{
4067 ssl->curve_list = curve_list;
4068}
4069#endif
4070
Paul Bakker0be444a2013-08-27 21:55:01 +02004071#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004072int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004073{
4074 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004075 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004076
4077 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004078
4079 if( ssl->hostname_len + 1 == 0 )
4080 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4081
Paul Bakker6e339b52013-07-03 13:37:05 +02004082 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004083
Paul Bakkerb15b8512012-01-13 13:44:06 +00004084 if( ssl->hostname == NULL )
4085 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4086
Paul Bakker3c2122f2013-06-24 19:03:14 +02004087 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004088 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004089
Paul Bakker40ea7de2009-05-03 10:18:48 +00004090 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004091
4092 return( 0 );
4093}
4094
Paul Bakker5701cdc2012-09-27 21:49:42 +00004095void ssl_set_sni( ssl_context *ssl,
4096 int (*f_sni)(void *, ssl_context *,
4097 const unsigned char *, size_t),
4098 void *p_sni )
4099{
4100 ssl->f_sni = f_sni;
4101 ssl->p_sni = p_sni;
4102}
Paul Bakker0be444a2013-08-27 21:55:01 +02004103#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004104
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004105#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004106int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004107{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004108 size_t cur_len, tot_len;
4109 const char **p;
4110
4111 /*
4112 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4113 * truncated". Check lengths now rather than later.
4114 */
4115 tot_len = 0;
4116 for( p = protos; *p != NULL; p++ )
4117 {
4118 cur_len = strlen( *p );
4119 tot_len += cur_len;
4120
4121 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4123 }
4124
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004125 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004126
4127 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004128}
4129
4130const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4131{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004132 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004133}
4134#endif /* POLARSSL_SSL_ALPN */
4135
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004136static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00004137{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004138 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
4139 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
4140 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
4141 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004142 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004143 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004144 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004145
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004146 return( 0 );
4147}
4148
4149int ssl_set_max_version( ssl_context *ssl, int major, int minor )
4150{
4151 if( ssl_check_version( ssl, major, minor ) != 0 )
4152 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4153
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004154 ssl->max_major_ver = major;
4155 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004156
4157 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00004158}
4159
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004160int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00004161{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004162 if( ssl_check_version( ssl, major, minor ) != 0 )
4163 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004164
4165 ssl->min_major_ver = major;
4166 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004167
4168 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00004169}
4170
Paul Bakker05decb22013-08-15 13:33:48 +02004171#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004172int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4173{
Paul Bakker77e257e2013-12-16 15:29:52 +01004174 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004175 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004176 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004177 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004178 }
4179
4180 ssl->mfl_code = mfl_code;
4181
4182 return( 0 );
4183}
Paul Bakker05decb22013-08-15 13:33:48 +02004184#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004185
Paul Bakker1f2bc622013-08-15 13:45:55 +02004186#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004187int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004188{
4189 if( ssl->endpoint != SSL_IS_CLIENT )
4190 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4191
Paul Bakker8c1ede62013-07-19 14:14:37 +02004192 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004193
4194 return( 0 );
4195}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004196#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004197
Paul Bakker48916f92012-09-16 19:57:18 +00004198void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4199{
4200 ssl->disable_renegotiation = renegotiation;
4201}
4202
4203void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4204{
4205 ssl->allow_legacy_renegotiation = allow_legacy;
4206}
4207
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004208void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4209{
4210 ssl->renego_max_records = max_records;
4211}
4212
Paul Bakkera503a632013-08-14 13:48:06 +02004213#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004214int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4215{
4216 ssl->session_tickets = use_tickets;
4217
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004218 if( ssl->endpoint == SSL_IS_CLIENT )
4219 return( 0 );
4220
4221 if( ssl->f_rng == NULL )
4222 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4223
4224 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004225}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004226
4227void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4228{
4229 ssl->ticket_lifetime = lifetime;
4230}
Paul Bakkera503a632013-08-14 13:48:06 +02004231#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004232
Paul Bakker5121ce52009-01-03 21:22:43 +00004233/*
4234 * SSL get accessors
4235 */
Paul Bakker23986e52011-04-24 08:57:21 +00004236size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004237{
4238 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4239}
4240
Paul Bakkerff60ee62010-03-16 21:09:09 +00004241int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004242{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004243 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004244}
4245
Paul Bakkere3166ce2011-01-27 17:40:50 +00004246const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004247{
Paul Bakker926c8e42013-03-06 10:23:34 +01004248 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004249 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004250
Paul Bakkere3166ce2011-01-27 17:40:50 +00004251 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004252}
4253
Paul Bakker43ca69c2011-01-15 17:35:19 +00004254const char *ssl_get_version( const ssl_context *ssl )
4255{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004256#if defined(POLARSSL_SSL_PROTO_DTLS)
4257 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4258 {
4259 switch( ssl->minor_ver )
4260 {
4261 case SSL_MINOR_VERSION_2:
4262 return( "DTLSv1.0" );
4263
4264 case SSL_MINOR_VERSION_3:
4265 return( "DTLSv1.2" );
4266
4267 default:
4268 return( "unknown (DTLS)" );
4269 }
4270 }
4271#endif
4272
Paul Bakker43ca69c2011-01-15 17:35:19 +00004273 switch( ssl->minor_ver )
4274 {
4275 case SSL_MINOR_VERSION_0:
4276 return( "SSLv3.0" );
4277
4278 case SSL_MINOR_VERSION_1:
4279 return( "TLSv1.0" );
4280
4281 case SSL_MINOR_VERSION_2:
4282 return( "TLSv1.1" );
4283
Paul Bakker1ef83d62012-04-11 12:09:53 +00004284 case SSL_MINOR_VERSION_3:
4285 return( "TLSv1.2" );
4286
Paul Bakker43ca69c2011-01-15 17:35:19 +00004287 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004288 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00004289 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00004290}
4291
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004292#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004293const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004294{
4295 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004296 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004297
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004298 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004299}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004300#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004301
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004302int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4303{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004304 if( ssl == NULL ||
4305 dst == NULL ||
4306 ssl->session == NULL ||
4307 ssl->endpoint != SSL_IS_CLIENT )
4308 {
4309 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4310 }
4311
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004312 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004313}
4314
Paul Bakker5121ce52009-01-03 21:22:43 +00004315/*
Paul Bakker1961b702013-01-25 14:49:24 +01004316 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004317 */
Paul Bakker1961b702013-01-25 14:49:24 +01004318int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004319{
Paul Bakker40e46942009-01-03 21:51:57 +00004320 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004321
Paul Bakker40e46942009-01-03 21:51:57 +00004322#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004323 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004324 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004325#endif
4326
Paul Bakker40e46942009-01-03 21:51:57 +00004327#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004328 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004329 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004330#endif
4331
Paul Bakker1961b702013-01-25 14:49:24 +01004332 return( ret );
4333}
4334
4335/*
4336 * Perform the SSL handshake
4337 */
4338int ssl_handshake( ssl_context *ssl )
4339{
4340 int ret = 0;
4341
4342 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4343
4344 while( ssl->state != SSL_HANDSHAKE_OVER )
4345 {
4346 ret = ssl_handshake_step( ssl );
4347
4348 if( ret != 0 )
4349 break;
4350 }
4351
Paul Bakker5121ce52009-01-03 21:22:43 +00004352 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4353
4354 return( ret );
4355}
4356
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004357#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004358/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004359 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004360 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004361static int ssl_write_hello_request( ssl_context *ssl )
4362{
4363 int ret;
4364
4365 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4366
4367 ssl->out_msglen = 4;
4368 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4369 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4370
4371 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4372 {
4373 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4374 return( ret );
4375 }
4376
4377 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4378
4379 return( 0 );
4380}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004381#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004382
4383/*
4384 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004385 * - any side: calling ssl_renegotiate(),
4386 * - client: receiving a HelloRequest during ssl_read(),
4387 * - server: receiving any handshake message on server during ssl_read() after
4388 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004389 * If the handshake doesn't complete due to waiting for I/O, it will continue
4390 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004391 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004392static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004393{
4394 int ret;
4395
4396 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4397
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004398 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4399 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004400
4401 ssl->state = SSL_HELLO_REQUEST;
4402 ssl->renegotiation = SSL_RENEGOTIATION;
4403
Paul Bakker48916f92012-09-16 19:57:18 +00004404 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4405 {
4406 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4407 return( ret );
4408 }
4409
4410 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4411
4412 return( 0 );
4413}
4414
4415/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004416 * Renegotiate current connection on client,
4417 * or request renegotiation on server
4418 */
4419int ssl_renegotiate( ssl_context *ssl )
4420{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004421 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004422
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004423#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004424 /* On server, just send the request */
4425 if( ssl->endpoint == SSL_IS_SERVER )
4426 {
4427 if( ssl->state != SSL_HANDSHAKE_OVER )
4428 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4429
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004430 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4431
4432 /* Did we already try/start sending HelloRequest? */
4433 if( ssl->out_left != 0 )
4434 return( ssl_flush_output( ssl ) );
4435
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004436 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004437 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004438#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004439
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004440#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004441 /*
4442 * On client, either start the renegotiation process or,
4443 * if already in progress, continue the handshake
4444 */
4445 if( ssl->renegotiation != SSL_RENEGOTIATION )
4446 {
4447 if( ssl->state != SSL_HANDSHAKE_OVER )
4448 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4449
4450 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4451 {
4452 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4453 return( ret );
4454 }
4455 }
4456 else
4457 {
4458 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4459 {
4460 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4461 return( ret );
4462 }
4463 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004464#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004465
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004466 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004467}
4468
4469/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004470 * Receive application data decrypted from the SSL layer
4471 */
Paul Bakker23986e52011-04-24 08:57:21 +00004472int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004473{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004474 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004475 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004476
4477 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4478
4479 if( ssl->state != SSL_HANDSHAKE_OVER )
4480 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004481 ret = ssl_handshake( ssl );
4482 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4483 {
4484 record_read = 1;
4485 }
4486 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004487 {
4488 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4489 return( ret );
4490 }
4491 }
4492
4493 if( ssl->in_offt == NULL )
4494 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004495 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004496 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004497 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4498 {
4499 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4500 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004501
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004502 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4503 return( ret );
4504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 }
4506
4507 if( ssl->in_msglen == 0 &&
4508 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4509 {
4510 /*
4511 * OpenSSL sends empty messages to randomize the IV
4512 */
4513 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4514 {
Paul Bakker831a7552011-05-18 13:32:51 +00004515 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4516 return( 0 );
4517
Paul Bakker5121ce52009-01-03 21:22:43 +00004518 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4519 return( ret );
4520 }
4521 }
4522
Paul Bakker48916f92012-09-16 19:57:18 +00004523 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4524 {
4525 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4526
4527 if( ssl->endpoint == SSL_IS_CLIENT &&
4528 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4529 ssl->in_hslen != 4 ) )
4530 {
4531 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4532 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4533 }
4534
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004535 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4536 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004537 ssl->allow_legacy_renegotiation ==
4538 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004539 {
4540 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4541
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004542#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004543 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004544 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004545 /*
4546 * SSLv3 does not have a "no_renegotiation" alert
4547 */
4548 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4549 return( ret );
4550 }
4551 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004552#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004553#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4554 defined(POLARSSL_SSL_PROTO_TLS1_2)
4555 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004556 {
4557 if( ( ret = ssl_send_alert_message( ssl,
4558 SSL_ALERT_LEVEL_WARNING,
4559 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4560 {
4561 return( ret );
4562 }
Paul Bakker48916f92012-09-16 19:57:18 +00004563 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004564 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004565#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4566 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004567 {
4568 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004569 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004570 }
Paul Bakker48916f92012-09-16 19:57:18 +00004571 }
4572 else
4573 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004574 ret = ssl_start_renegotiation( ssl );
4575 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4576 {
4577 record_read = 1;
4578 }
4579 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004580 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004581 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004582 return( ret );
4583 }
Paul Bakker48916f92012-09-16 19:57:18 +00004584 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004585
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004586 /* If a non-handshake record was read during renego, fallthrough,
4587 * else tell the user they should call ssl_read() again */
4588 if( ! record_read )
4589 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004590 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004591 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4592 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004593 ssl->renego_records_seen++;
4594
4595 if( ssl->renego_max_records >= 0 &&
4596 ssl->renego_records_seen > ssl->renego_max_records )
4597 {
4598 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4599 "but not honored by client" ) );
4600 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4601 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004602 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004603
4604 /* Fatal and closure alerts handled by ssl_read_record() */
4605 if( ssl->in_msgtype == SSL_MSG_ALERT )
4606 {
4607 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4608 return( POLARSSL_ERR_NET_WANT_READ );
4609 }
4610
4611 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 {
4613 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004614 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 }
4616
4617 ssl->in_offt = ssl->in_msg;
4618 }
4619
4620 n = ( len < ssl->in_msglen )
4621 ? len : ssl->in_msglen;
4622
4623 memcpy( buf, ssl->in_offt, n );
4624 ssl->in_msglen -= n;
4625
4626 if( ssl->in_msglen == 0 )
4627 /* all bytes consumed */
4628 ssl->in_offt = NULL;
4629 else
4630 /* more data available */
4631 ssl->in_offt += n;
4632
4633 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4634
Paul Bakker23986e52011-04-24 08:57:21 +00004635 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004636}
4637
4638/*
4639 * Send application data to be encrypted by the SSL layer
4640 */
Paul Bakker23986e52011-04-24 08:57:21 +00004641int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004642{
Paul Bakker23986e52011-04-24 08:57:21 +00004643 int ret;
4644 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004645 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004646
4647 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4648
4649 if( ssl->state != SSL_HANDSHAKE_OVER )
4650 {
4651 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4652 {
4653 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4654 return( ret );
4655 }
4656 }
4657
Paul Bakker05decb22013-08-15 13:33:48 +02004658#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004659 /*
4660 * Assume mfl_code is correct since it was checked when set
4661 */
4662 max_len = mfl_code_to_length[ssl->mfl_code];
4663
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004664 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004665 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004666 */
4667 if( ssl->session_out != NULL &&
4668 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4669 {
4670 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4671 }
Paul Bakker05decb22013-08-15 13:33:48 +02004672#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004673
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004674 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004675
Paul Bakker5121ce52009-01-03 21:22:43 +00004676 if( ssl->out_left != 0 )
4677 {
4678 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4679 {
4680 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4681 return( ret );
4682 }
4683 }
Paul Bakker887bd502011-06-08 13:10:54 +00004684 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004685 {
Paul Bakker887bd502011-06-08 13:10:54 +00004686 ssl->out_msglen = n;
4687 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4688 memcpy( ssl->out_msg, buf, n );
4689
4690 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4691 {
4692 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4693 return( ret );
4694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004695 }
4696
4697 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4698
Paul Bakker23986e52011-04-24 08:57:21 +00004699 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004700}
4701
4702/*
4703 * Notify the peer that the connection is being closed
4704 */
4705int ssl_close_notify( ssl_context *ssl )
4706{
4707 int ret;
4708
4709 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4710
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004711 if( ssl->out_left != 0 )
4712 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004713
4714 if( ssl->state == SSL_HANDSHAKE_OVER )
4715 {
Paul Bakker48916f92012-09-16 19:57:18 +00004716 if( ( ret = ssl_send_alert_message( ssl,
4717 SSL_ALERT_LEVEL_WARNING,
4718 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004719 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004720 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004721 return( ret );
4722 }
4723 }
4724
4725 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4726
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004727 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004728}
4729
Paul Bakker48916f92012-09-16 19:57:18 +00004730void ssl_transform_free( ssl_transform *transform )
4731{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004732 if( transform == NULL )
4733 return;
4734
Paul Bakker48916f92012-09-16 19:57:18 +00004735#if defined(POLARSSL_ZLIB_SUPPORT)
4736 deflateEnd( &transform->ctx_deflate );
4737 inflateEnd( &transform->ctx_inflate );
4738#endif
4739
Paul Bakker84bbeb52014-07-01 14:53:22 +02004740 cipher_free( &transform->cipher_ctx_enc );
4741 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004742
Paul Bakker84bbeb52014-07-01 14:53:22 +02004743 md_free( &transform->md_ctx_enc );
4744 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004745
Paul Bakker34617722014-06-13 17:20:13 +02004746 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004747}
4748
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004749#if defined(POLARSSL_X509_CRT_PARSE_C)
4750static void ssl_key_cert_free( ssl_key_cert *key_cert )
4751{
4752 ssl_key_cert *cur = key_cert, *next;
4753
4754 while( cur != NULL )
4755 {
4756 next = cur->next;
4757
4758 if( cur->key_own_alloc )
4759 {
4760 pk_free( cur->key );
4761 polarssl_free( cur->key );
4762 }
4763 polarssl_free( cur );
4764
4765 cur = next;
4766 }
4767}
4768#endif /* POLARSSL_X509_CRT_PARSE_C */
4769
Paul Bakker48916f92012-09-16 19:57:18 +00004770void ssl_handshake_free( ssl_handshake_params *handshake )
4771{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004772 if( handshake == NULL )
4773 return;
4774
Paul Bakker48916f92012-09-16 19:57:18 +00004775#if defined(POLARSSL_DHM_C)
4776 dhm_free( &handshake->dhm_ctx );
4777#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004778#if defined(POLARSSL_ECDH_C)
4779 ecdh_free( &handshake->ecdh_ctx );
4780#endif
4781
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004782#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004783 /* explicit void pointer cast for buggy MS compiler */
4784 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004785#endif
4786
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004787#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4788 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4789 /*
4790 * Free only the linked list wrapper, not the keys themselves
4791 * since the belong to the SNI callback
4792 */
4793 if( handshake->sni_key_cert != NULL )
4794 {
4795 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4796
4797 while( cur != NULL )
4798 {
4799 next = cur->next;
4800 polarssl_free( cur );
4801 cur = next;
4802 }
4803 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004804#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004805
Paul Bakker34617722014-06-13 17:20:13 +02004806 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004807}
4808
4809void ssl_session_free( ssl_session *session )
4810{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004811 if( session == NULL )
4812 return;
4813
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004814#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004815 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004816 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004817 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004818 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004819 }
Paul Bakkered27a042013-04-18 22:46:23 +02004820#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004821
Paul Bakkera503a632013-08-14 13:48:06 +02004822#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004823 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004824#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004825
Paul Bakker34617722014-06-13 17:20:13 +02004826 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004827}
4828
Paul Bakker5121ce52009-01-03 21:22:43 +00004829/*
4830 * Free an SSL context
4831 */
4832void ssl_free( ssl_context *ssl )
4833{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004834 if( ssl == NULL )
4835 return;
4836
Paul Bakker5121ce52009-01-03 21:22:43 +00004837 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4838
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004839 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004841 polarssl_zeroize( ssl->out_buf, SSL_BUFFER_LEN );
4842 polarssl_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 }
4844
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004845 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 {
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +01004847 polarssl_zeroize( ssl->in_buf, SSL_BUFFER_LEN );
4848 polarssl_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00004849 }
4850
Paul Bakker16770332013-10-11 09:59:44 +02004851#if defined(POLARSSL_ZLIB_SUPPORT)
4852 if( ssl->compress_buf != NULL )
4853 {
Paul Bakker34617722014-06-13 17:20:13 +02004854 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004855 polarssl_free( ssl->compress_buf );
4856 }
4857#endif
4858
Paul Bakker40e46942009-01-03 21:51:57 +00004859#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004860 mpi_free( &ssl->dhm_P );
4861 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004862#endif
4863
Paul Bakker48916f92012-09-16 19:57:18 +00004864 if( ssl->transform )
4865 {
4866 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004867 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004868 }
4869
4870 if( ssl->handshake )
4871 {
4872 ssl_handshake_free( ssl->handshake );
4873 ssl_transform_free( ssl->transform_negotiate );
4874 ssl_session_free( ssl->session_negotiate );
4875
Paul Bakker6e339b52013-07-03 13:37:05 +02004876 polarssl_free( ssl->handshake );
4877 polarssl_free( ssl->transform_negotiate );
4878 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004879 }
4880
Paul Bakkerc0463502013-02-14 11:19:38 +01004881 if( ssl->session )
4882 {
4883 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004884 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004885 }
4886
Paul Bakkera503a632013-08-14 13:48:06 +02004887#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004888 if( ssl->ticket_keys )
4889 {
4890 ssl_ticket_keys_free( ssl->ticket_keys );
4891 polarssl_free( ssl->ticket_keys );
4892 }
Paul Bakkera503a632013-08-14 13:48:06 +02004893#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004894
Paul Bakker0be444a2013-08-27 21:55:01 +02004895#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004896 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004897 {
Paul Bakker34617722014-06-13 17:20:13 +02004898 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004899 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 ssl->hostname_len = 0;
4901 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004902#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004903
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004904#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004905 if( ssl->psk != NULL )
4906 {
Paul Bakker34617722014-06-13 17:20:13 +02004907 polarssl_zeroize( ssl->psk, ssl->psk_len );
4908 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004909 polarssl_free( ssl->psk );
4910 polarssl_free( ssl->psk_identity );
4911 ssl->psk_len = 0;
4912 ssl->psk_identity_len = 0;
4913 }
4914#endif
4915
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004916#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004917 ssl_key_cert_free( ssl->key_cert );
4918#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004919
Paul Bakker05ef8352012-05-08 09:17:57 +00004920#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4921 if( ssl_hw_record_finish != NULL )
4922 {
4923 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4924 ssl_hw_record_finish( ssl );
4925 }
4926#endif
4927
Paul Bakker5121ce52009-01-03 21:22:43 +00004928 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004929
Paul Bakker86f04f42013-02-14 11:20:09 +01004930 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004931 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004932}
4933
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004934#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004935/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004936 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004937 */
4938unsigned char ssl_sig_from_pk( pk_context *pk )
4939{
4940#if defined(POLARSSL_RSA_C)
4941 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4942 return( SSL_SIG_RSA );
4943#endif
4944#if defined(POLARSSL_ECDSA_C)
4945 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4946 return( SSL_SIG_ECDSA );
4947#endif
4948 return( SSL_SIG_ANON );
4949}
4950
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004951pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4952{
4953 switch( sig )
4954 {
4955#if defined(POLARSSL_RSA_C)
4956 case SSL_SIG_RSA:
4957 return( POLARSSL_PK_RSA );
4958#endif
4959#if defined(POLARSSL_ECDSA_C)
4960 case SSL_SIG_ECDSA:
4961 return( POLARSSL_PK_ECDSA );
4962#endif
4963 default:
4964 return( POLARSSL_PK_NONE );
4965 }
4966}
Paul Bakker9af723c2014-05-01 13:03:14 +02004967#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004968
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004969/*
4970 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4971 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004972md_type_t ssl_md_alg_from_hash( unsigned char hash )
4973{
4974 switch( hash )
4975 {
4976#if defined(POLARSSL_MD5_C)
4977 case SSL_HASH_MD5:
4978 return( POLARSSL_MD_MD5 );
4979#endif
4980#if defined(POLARSSL_SHA1_C)
4981 case SSL_HASH_SHA1:
4982 return( POLARSSL_MD_SHA1 );
4983#endif
4984#if defined(POLARSSL_SHA256_C)
4985 case SSL_HASH_SHA224:
4986 return( POLARSSL_MD_SHA224 );
4987 case SSL_HASH_SHA256:
4988 return( POLARSSL_MD_SHA256 );
4989#endif
4990#if defined(POLARSSL_SHA512_C)
4991 case SSL_HASH_SHA384:
4992 return( POLARSSL_MD_SHA384 );
4993 case SSL_HASH_SHA512:
4994 return( POLARSSL_MD_SHA512 );
4995#endif
4996 default:
4997 return( POLARSSL_MD_NONE );
4998 }
4999}
5000
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005001#if defined(POLARSSL_SSL_SET_CURVES)
5002/*
5003 * Check is a curve proposed by the peer is in our list.
5004 * Return 1 if we're willing to use it, 0 otherwise.
5005 */
5006int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5007{
5008 const ecp_group_id *gid;
5009
5010 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5011 if( *gid == grp_id )
5012 return( 1 );
5013
5014 return( 0 );
5015}
Paul Bakker9af723c2014-05-01 13:03:14 +02005016#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005017
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005018#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005019int ssl_check_cert_usage( const x509_crt *cert,
5020 const ssl_ciphersuite_t *ciphersuite,
5021 int cert_endpoint )
5022{
5023#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5024 int usage = 0;
5025#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005026#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5027 const char *ext_oid;
5028 size_t ext_len;
5029#endif
5030
5031#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5032 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5033 ((void) cert);
5034 ((void) cert_endpoint);
5035#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005036
5037#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5038 if( cert_endpoint == SSL_IS_SERVER )
5039 {
5040 /* Server part of the key exchange */
5041 switch( ciphersuite->key_exchange )
5042 {
5043 case POLARSSL_KEY_EXCHANGE_RSA:
5044 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5045 usage = KU_KEY_ENCIPHERMENT;
5046 break;
5047
5048 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5049 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5050 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5051 usage = KU_DIGITAL_SIGNATURE;
5052 break;
5053
5054 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5055 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5056 usage = KU_KEY_AGREEMENT;
5057 break;
5058
5059 /* Don't use default: we want warnings when adding new values */
5060 case POLARSSL_KEY_EXCHANGE_NONE:
5061 case POLARSSL_KEY_EXCHANGE_PSK:
5062 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5063 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5064 usage = 0;
5065 }
5066 }
5067 else
5068 {
5069 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5070 usage = KU_DIGITAL_SIGNATURE;
5071 }
5072
5073 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5074 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005075#else
5076 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005077#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5078
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005079#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5080 if( cert_endpoint == SSL_IS_SERVER )
5081 {
5082 ext_oid = OID_SERVER_AUTH;
5083 ext_len = OID_SIZE( OID_SERVER_AUTH );
5084 }
5085 else
5086 {
5087 ext_oid = OID_CLIENT_AUTH;
5088 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5089 }
5090
5091 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5092 return( -1 );
5093#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5094
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005095 return( 0 );
5096}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005097#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005098
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005099/*
5100 * Convert version numbers to/from wire format
5101 * and, for DTLS, to/from TLS equivalent.
5102 *
5103 * For TLS this is the identity.
5104 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
5105 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
5106 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
5107 */
5108void ssl_write_version( int major, int minor, int transport,
5109 unsigned char ver[2] )
5110{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005111#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005112 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005113 {
5114 if( minor == SSL_MINOR_VERSION_2 )
5115 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5116
5117 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
5118 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
5119 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005120 else
5121#else
5122 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005123#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005124 {
5125 ver[0] = (unsigned char) major;
5126 ver[1] = (unsigned char) minor;
5127 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005128}
5129
5130void ssl_read_version( int *major, int *minor, int transport,
5131 const unsigned char ver[2] )
5132{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005133#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005134 if( transport == SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005135 {
5136 *major = 255 - ver[0] + 2;
5137 *minor = 255 - ver[1] + 1;
5138
5139 if( *minor == SSL_MINOR_VERSION_1 )
5140 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
5141 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005142 else
5143#else
5144 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005145#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +01005146 {
5147 *major = ver[0];
5148 *minor = ver[1];
5149 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005150}
5151
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005152#endif /* POLARSSL_SSL_TLS_C */