blob: f079adc45faf1cb1a39e629ea1c9d893b96b88f1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The SSL 3.0 specification was drafted by Netscape in 1996,
24 * and became an IETF standard in 1999.
25 *
26 * http://wp.netscape.com/eng/ssl3/
27 * http://www.ietf.org/rfc/rfc2246.txt
28 * http://www.ietf.org/rfc/rfc4346.txt
29 */
30
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
34#include POLARSSL_CONFIG_FILE
35#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000036
Paul Bakker40e46942009-01-03 21:51:57 +000037#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Paul Bakker0be444a2013-08-27 21:55:01 +020039#include "polarssl/debug.h"
40#include "polarssl/ssl.h"
41
Rich Evans00ab4702015-02-06 13:43:58 +000042#include <string.h>
43
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020044#if defined(POLARSSL_X509_CRT_PARSE_C) && \
45 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
46#include "polarssl/oid.h"
47#endif
48
Paul Bakker7dc4c442014-02-01 22:50:26 +010049#if defined(POLARSSL_PLATFORM_C)
50#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdlib.h>
Paul Bakker6e339b52013-07-03 13:37:05 +020053#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker6edcd412013-10-29 15:22:54 +010057#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
58 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000059#define strcasecmp _stricmp
60#endif
61
Paul Bakker34617722014-06-13 17:20:13 +020062/* Implementation that should never be optimized out by the compiler */
63static void polarssl_zeroize( void *v, size_t n ) {
64 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
65}
66
Paul Bakker05decb22013-08-15 13:33:48 +020067#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020068/*
69 * Convert max_fragment_length codes to length.
70 * RFC 6066 says:
71 * enum{
72 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
73 * } MaxFragmentLength;
74 * and we add 0 -> extension unused
75 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020076static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020077{
78 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
79 512, /* SSL_MAX_FRAG_LEN_512 */
80 1024, /* SSL_MAX_FRAG_LEN_1024 */
81 2048, /* SSL_MAX_FRAG_LEN_2048 */
82 4096, /* SSL_MAX_FRAG_LEN_4096 */
83};
Paul Bakker05decb22013-08-15 13:33:48 +020084#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020085
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020086static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
87{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088 ssl_session_free( dst );
89 memcpy( dst, src, sizeof( ssl_session ) );
90
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020092 if( src->peer_cert != NULL )
93 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020094 int ret;
95
Mansour Moufidc531b4a2015-02-15 17:35:38 -050096 dst->peer_cert = polarssl_malloc( sizeof(x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020098 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
99
Paul Bakkerb6b09562013-09-18 14:17:41 +0200100 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200101
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200102 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
103 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200104 {
105 polarssl_free( dst->peer_cert );
106 dst->peer_cert = NULL;
107 return( ret );
108 }
109 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200111
Paul Bakkera503a632013-08-14 13:48:06 +0200112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113 if( src->ticket != NULL )
114 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500115 dst->ticket = polarssl_malloc( src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200116 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200117 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
118
119 memcpy( dst->ticket, src->ticket, src->ticket_len );
120 }
Paul Bakkera503a632013-08-14 13:48:06 +0200121#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200122
123 return( 0 );
124}
125
Paul Bakker05ef8352012-05-08 09:17:57 +0000126#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200127int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200128 const unsigned char *key_enc, const unsigned char *key_dec,
129 size_t keylen,
130 const unsigned char *iv_enc, const unsigned char *iv_dec,
131 size_t ivlen,
132 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200133 size_t maclen ) = NULL;
134int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
135int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
136int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
137int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200139#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/*
142 * Key material generation
143 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200144#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200145static int ssl3_prf( const unsigned char *secret, size_t slen,
146 const char *label,
147 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000148 unsigned char *dstbuf, size_t dlen )
149{
150 size_t i;
151 md5_context md5;
152 sha1_context sha1;
153 unsigned char padding[16];
154 unsigned char sha1sum[20];
155 ((void)label);
156
Paul Bakker5b4af392014-06-26 12:09:34 +0200157 md5_init( &md5 );
158 sha1_init( &sha1 );
159
Paul Bakker5f70b252012-09-13 14:23:06 +0000160 /*
161 * SSLv3:
162 * block =
163 * MD5( secret + SHA1( 'A' + secret + random ) ) +
164 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
165 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
166 * ...
167 */
168 for( i = 0; i < dlen / 16; i++ )
169 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200170 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000171
172 sha1_starts( &sha1 );
173 sha1_update( &sha1, padding, 1 + i );
174 sha1_update( &sha1, secret, slen );
175 sha1_update( &sha1, random, rlen );
176 sha1_finish( &sha1, sha1sum );
177
178 md5_starts( &md5 );
179 md5_update( &md5, secret, slen );
180 md5_update( &md5, sha1sum, 20 );
181 md5_finish( &md5, dstbuf + i * 16 );
182 }
183
Paul Bakker5b4af392014-06-26 12:09:34 +0200184 md5_free( &md5 );
185 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000186
Paul Bakker34617722014-06-13 17:20:13 +0200187 polarssl_zeroize( padding, sizeof( padding ) );
188 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000189
190 return( 0 );
191}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200192#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000193
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200195static int tls1_prf( const unsigned char *secret, size_t slen,
196 const char *label,
197 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000198 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
Paul Bakker23986e52011-04-24 08:57:21 +0000200 size_t nb, hs;
201 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200202 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 unsigned char tmp[128];
204 unsigned char h_i[20];
205
206 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000207 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209 hs = ( slen + 1 ) / 2;
210 S1 = secret;
211 S2 = secret + slen - hs;
212
213 nb = strlen( label );
214 memcpy( tmp + 20, label, nb );
215 memcpy( tmp + 20 + nb, random, rlen );
216 nb += rlen;
217
218 /*
219 * First compute P_md5(secret,label+random)[0..dlen]
220 */
221 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
222
223 for( i = 0; i < dlen; i += 16 )
224 {
225 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
226 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
227
228 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
229
230 for( j = 0; j < k; j++ )
231 dstbuf[i + j] = h_i[j];
232 }
233
234 /*
235 * XOR out with P_sha1(secret,label+random)[0..dlen]
236 */
237 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
238
239 for( i = 0; i < dlen; i += 20 )
240 {
241 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
242 sha1_hmac( S2, hs, tmp, 20, tmp );
243
244 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
245
246 for( j = 0; j < k; j++ )
247 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
248 }
249
Paul Bakker34617722014-06-13 17:20:13 +0200250 polarssl_zeroize( tmp, sizeof( tmp ) );
251 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
253 return( 0 );
254}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200255#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#if defined(POLARSSL_SSL_PROTO_TLS1_2)
258#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200259static int tls_prf_sha256( const unsigned char *secret, size_t slen,
260 const char *label,
261 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000262 unsigned char *dstbuf, size_t dlen )
263{
264 size_t nb;
265 size_t i, j, k;
266 unsigned char tmp[128];
267 unsigned char h_i[32];
268
269 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 nb = strlen( label );
273 memcpy( tmp + 32, label, nb );
274 memcpy( tmp + 32 + nb, random, rlen );
275 nb += rlen;
276
277 /*
278 * Compute P_<hash>(secret, label + random)[0..dlen]
279 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200280 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000281
282 for( i = 0; i < dlen; i += 32 )
283 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200284 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
285 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000286
287 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
288
289 for( j = 0; j < k; j++ )
290 dstbuf[i + j] = h_i[j];
291 }
292
Paul Bakker34617722014-06-13 17:20:13 +0200293 polarssl_zeroize( tmp, sizeof( tmp ) );
294 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000295
296 return( 0 );
297}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200298#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000299
Paul Bakker9e36f042013-06-30 14:34:05 +0200300#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200301static int tls_prf_sha384( const unsigned char *secret, size_t slen,
302 const char *label,
303 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000304 unsigned char *dstbuf, size_t dlen )
305{
306 size_t nb;
307 size_t i, j, k;
308 unsigned char tmp[128];
309 unsigned char h_i[48];
310
311 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
313
314 nb = strlen( label );
315 memcpy( tmp + 48, label, nb );
316 memcpy( tmp + 48 + nb, random, rlen );
317 nb += rlen;
318
319 /*
320 * Compute P_<hash>(secret, label + random)[0..dlen]
321 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200322 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000323
324 for( i = 0; i < dlen; i += 48 )
325 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200326 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
327 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000328
329 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
330
331 for( j = 0; j < k; j++ )
332 dstbuf[i + j] = h_i[j];
333 }
334
Paul Bakker34617722014-06-13 17:20:13 +0200335 polarssl_zeroize( tmp, sizeof( tmp ) );
336 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000337
338 return( 0 );
339}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200340#endif /* POLARSSL_SHA512_C */
341#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000342
Paul Bakker66d5d072014-06-17 16:39:18 +0200343static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200344
345#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
346 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200347static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200348#endif
Paul Bakker380da532012-04-18 16:10:25 +0000349
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200351static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
352static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200353#endif
354
355#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200356static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
357static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200358#endif
359
360#if defined(POLARSSL_SSL_PROTO_TLS1_2)
361#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200362static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
363static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
364static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200365#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100366
Paul Bakker9e36f042013-06-30 14:34:05 +0200367#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200368static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
369static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
370static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100371#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200372#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000373
Paul Bakker5121ce52009-01-03 21:22:43 +0000374int ssl_derive_keys( ssl_context *ssl )
375{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200376 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 unsigned char keyblk[256];
379 unsigned char *key1;
380 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100381 unsigned char *mac_enc;
382 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200383 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100384 const cipher_info_t *cipher_info;
385 const md_info_t *md_info;
386
Paul Bakker48916f92012-09-16 19:57:18 +0000387 ssl_session *session = ssl->session_negotiate;
388 ssl_transform *transform = ssl->transform_negotiate;
389 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
392
Paul Bakker68884e32013-01-07 18:20:04 +0100393 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
394 if( cipher_info == NULL )
395 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200396 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100397 transform->ciphersuite_info->cipher ) );
398 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
399 }
400
401 md_info = md_info_from_type( transform->ciphersuite_info->mac );
402 if( md_info == NULL )
403 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200404 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100405 transform->ciphersuite_info->mac ) );
406 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
407 }
408
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000410 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000411 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200412#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000414 {
Paul Bakker48916f92012-09-16 19:57:18 +0000415 handshake->tls_prf = ssl3_prf;
416 handshake->calc_verify = ssl_calc_verify_ssl;
417 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000418 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200419 else
420#endif
421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
422 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000423 {
Paul Bakker48916f92012-09-16 19:57:18 +0000424 handshake->tls_prf = tls1_prf;
425 handshake->calc_verify = ssl_calc_verify_tls;
426 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000427 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200428 else
429#endif
430#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200431#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200432 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
433 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000434 {
Paul Bakker48916f92012-09-16 19:57:18 +0000435 handshake->tls_prf = tls_prf_sha384;
436 handshake->calc_verify = ssl_calc_verify_tls_sha384;
437 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000438 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000439 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200440#endif
441#if defined(POLARSSL_SHA256_C)
442 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000443 {
Paul Bakker48916f92012-09-16 19:57:18 +0000444 handshake->tls_prf = tls_prf_sha256;
445 handshake->calc_verify = ssl_calc_verify_tls_sha256;
446 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000447 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200448 else
449#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200450#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200451 {
452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200454 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000455
456 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * SSLv3:
458 * master =
459 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
460 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
461 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200462 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200463 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000464 * master = PRF( premaster, "master secret", randbytes )[0..47]
465 */
Paul Bakker0a597072012-09-25 21:55:46 +0000466 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 {
Paul Bakker48916f92012-09-16 19:57:18 +0000468 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
469 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000470
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200471#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
472 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200473 {
474 unsigned char session_hash[48];
475 size_t hash_len;
476
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200477 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200478
479 ssl->handshake->calc_verify( ssl, session_hash );
480
481#if defined(POLARSSL_SSL_PROTO_TLS1_2)
482 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
483 {
484#if defined(POLARSSL_SHA512_C)
485 if( ssl->transform_negotiate->ciphersuite_info->mac ==
486 POLARSSL_MD_SHA384 )
487 {
488 hash_len = 48;
489 }
490 else
491#endif
492 hash_len = 32;
493 }
494 else
495#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
496 hash_len = 36;
497
498 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
499
500 handshake->tls_prf( handshake->premaster, handshake->pmslen,
501 "extended master secret",
502 session_hash, hash_len, session->master, 48 );
503
504 }
505 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200506#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000507 handshake->tls_prf( handshake->premaster, handshake->pmslen,
508 "master secret",
509 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200511
Paul Bakker34617722014-06-13 17:20:13 +0200512 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513 }
514 else
515 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
516
517 /*
518 * Swap the client and server random values.
519 */
Paul Bakker48916f92012-09-16 19:57:18 +0000520 memcpy( tmp, handshake->randbytes, 64 );
521 memcpy( handshake->randbytes, tmp + 32, 32 );
522 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200523 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525 /*
526 * SSLv3:
527 * key block =
528 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
529 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
530 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
532 * ...
533 *
534 * TLSv1:
535 * key block = PRF( master, "key expansion", randbytes )
536 */
Paul Bakker48916f92012-09-16 19:57:18 +0000537 handshake->tls_prf( session->master, 48, "key expansion",
538 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000539
Paul Bakker48916f92012-09-16 19:57:18 +0000540 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
541 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
542 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
543 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
545
Paul Bakker34617722014-06-13 17:20:13 +0200546 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 /*
549 * Determine the appropriate key, IV and MAC length.
550 */
Paul Bakker68884e32013-01-07 18:20:04 +0100551
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200552 transform->keylen = cipher_info->key_length / 8;
553
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200554 if( cipher_info->mode == POLARSSL_MODE_GCM ||
555 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200557 transform->maclen = 0;
558
Paul Bakker68884e32013-01-07 18:20:04 +0100559 transform->ivlen = 12;
560 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200561
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200562 /* Minimum length is expicit IV + tag */
563 transform->minlen = transform->ivlen - transform->fixed_ivlen
564 + ( transform->ciphersuite_info->flags &
565 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100566 }
567 else
568 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200569 int ret;
570
571 /* Initialize HMAC contexts */
572 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
573 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100574 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200575 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
576 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200579 /* Get MAC length */
580 transform->maclen = md_get_size( md_info );
581
582#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
583 /*
584 * If HMAC is to be truncated, we shall keep the leftmost bytes,
585 * (rfc 6066 page 13 or rfc 2104 section 4),
586 * so we only need to adjust the length here.
587 */
588 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
589 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
590#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
591
592 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100593 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000594
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200595 /* Minimum length */
596 if( cipher_info->mode == POLARSSL_MODE_STREAM )
597 transform->minlen = transform->maclen;
598 else
Paul Bakker68884e32013-01-07 18:20:04 +0100599 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200600 /*
601 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100602 * 1. if EtM is in use: one block plus MAC
603 * otherwise: * first multiple of blocklen greater than maclen
604 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200605 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100606#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
607 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
608 {
609 transform->minlen = transform->maclen
610 + cipher_info->block_size;
611 }
612 else
613#endif
614 {
615 transform->minlen = transform->maclen
616 + cipher_info->block_size
617 - transform->maclen % cipher_info->block_size;
618 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200619
620#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
621 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
622 ssl->minor_ver == SSL_MINOR_VERSION_1 )
623 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100624 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200625#endif
626#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
627 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
628 ssl->minor_ver == SSL_MINOR_VERSION_3 )
629 {
630 transform->minlen += transform->ivlen;
631 }
632 else
633#endif
634 {
635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
637 }
Paul Bakker68884e32013-01-07 18:20:04 +0100638 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
640
641 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000642 transform->keylen, transform->minlen, transform->ivlen,
643 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645 /*
646 * Finally setup the cipher contexts, IVs and MAC secrets.
647 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100648#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 if( ssl->endpoint == SSL_IS_CLIENT )
650 {
Paul Bakker48916f92012-09-16 19:57:18 +0000651 key1 = keyblk + transform->maclen * 2;
652 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
Paul Bakker68884e32013-01-07 18:20:04 +0100654 mac_enc = keyblk;
655 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000656
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000657 /*
658 * This is not used in TLS v1.1.
659 */
Paul Bakker48916f92012-09-16 19:57:18 +0000660 iv_copy_len = ( transform->fixed_ivlen ) ?
661 transform->fixed_ivlen : transform->ivlen;
662 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
663 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000664 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 }
666 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100667#endif /* POLARSSL_SSL_CLI_C */
668#if defined(POLARSSL_SSL_SRV_C)
669 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 {
Paul Bakker48916f92012-09-16 19:57:18 +0000671 key1 = keyblk + transform->maclen * 2 + transform->keylen;
672 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000673
Paul Bakker68884e32013-01-07 18:20:04 +0100674 mac_enc = keyblk + transform->maclen;
675 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000676
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000677 /*
678 * This is not used in TLS v1.1.
679 */
Paul Bakker48916f92012-09-16 19:57:18 +0000680 iv_copy_len = ( transform->fixed_ivlen ) ?
681 transform->fixed_ivlen : transform->ivlen;
682 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
683 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000684 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100686 else
687#endif /* POLARSSL_SSL_SRV_C */
688 {
689 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
690 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
691 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000692
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200693#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
695 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100696 if( transform->maclen > sizeof transform->mac_enc )
697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100700 }
701
Paul Bakker68884e32013-01-07 18:20:04 +0100702 memcpy( transform->mac_enc, mac_enc, transform->maclen );
703 memcpy( transform->mac_dec, mac_dec, transform->maclen );
704 }
705 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200706#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200707#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
708 defined(POLARSSL_SSL_PROTO_TLS1_2)
709 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100710 {
711 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
712 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
713 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200714 else
715#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200716 {
717 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200718 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200719 }
Paul Bakker68884e32013-01-07 18:20:04 +0100720
Paul Bakker05ef8352012-05-08 09:17:57 +0000721#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200722 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000723 {
724 int ret = 0;
725
726 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
727
Paul Bakker07eb38b2012-12-19 14:42:06 +0100728 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
729 transform->iv_enc, transform->iv_dec,
730 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100731 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100732 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000733 {
734 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200735 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000736 }
737 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200738#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000739
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200740 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
741 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
744 return( ret );
745 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200746
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200747 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
748 cipher_info ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
751 return( ret );
752 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
755 cipher_info->key_length,
756 POLARSSL_ENCRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200761
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200762 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
763 cipher_info->key_length,
764 POLARSSL_DECRYPT ) ) != 0 )
765 {
766 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
767 return( ret );
768 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200769
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200770#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771 if( cipher_info->mode == POLARSSL_MODE_CBC )
772 {
773 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
774 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200775 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200776 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
777 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779
780 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
781 POLARSSL_PADDING_NONE ) ) != 0 )
782 {
783 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
784 return( ret );
785 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000786 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200787#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker34617722014-06-13 17:20:13 +0200789 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Paul Bakker2770fbd2012-07-03 13:30:23 +0000791#if defined(POLARSSL_ZLIB_SUPPORT)
792 // Initialize compression
793 //
Paul Bakker48916f92012-09-16 19:57:18 +0000794 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000795 {
Paul Bakker16770332013-10-11 09:59:44 +0200796 if( ssl->compress_buf == NULL )
797 {
798 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
799 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
800 if( ssl->compress_buf == NULL )
801 {
802 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
803 SSL_BUFFER_LEN ) );
804 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
805 }
806 }
807
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
809
Paul Bakker48916f92012-09-16 19:57:18 +0000810 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
811 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000812
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200813 if( deflateInit( &transform->ctx_deflate,
814 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000815 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000816 {
817 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
818 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
819 }
820 }
821#endif /* POLARSSL_ZLIB_SUPPORT */
822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
824
825 return( 0 );
826}
827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000830{
831 md5_context md5;
832 sha1_context sha1;
833 unsigned char pad_1[48];
834 unsigned char pad_2[48];
835
Paul Bakker380da532012-04-18 16:10:25 +0000836 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000837
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 Bakker5121ce52009-01-03 21:22:43 +0000840
Paul Bakker380da532012-04-18 16:10:25 +0000841 memset( pad_1, 0x36, 48 );
842 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Paul Bakker48916f92012-09-16 19:57:18 +0000844 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000845 md5_update( &md5, pad_1, 48 );
846 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Paul Bakker380da532012-04-18 16:10:25 +0000848 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000849 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000850 md5_update( &md5, pad_2, 48 );
851 md5_update( &md5, hash, 16 );
852 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000853
Paul Bakker48916f92012-09-16 19:57:18 +0000854 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000855 sha1_update( &sha1, pad_1, 40 );
856 sha1_finish( &sha1, hash + 16 );
857
858 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000859 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000860 sha1_update( &sha1, pad_2, 40 );
861 sha1_update( &sha1, hash + 16, 20 );
862 sha1_finish( &sha1, hash + 16 );
863
864 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
865 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
866
Paul Bakker5b4af392014-06-26 12:09:34 +0200867 md5_free( &md5 );
868 sha1_free( &sha1 );
869
Paul Bakker380da532012-04-18 16:10:25 +0000870 return;
871}
Paul Bakker9af723c2014-05-01 13:03:14 +0200872#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000873
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200874#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000875void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
876{
877 md5_context md5;
878 sha1_context sha1;
879
880 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
881
Paul Bakker48916f92012-09-16 19:57:18 +0000882 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
883 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000884
Paul Bakker48916f92012-09-16 19:57:18 +0000885 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000886 sha1_finish( &sha1, hash + 16 );
887
888 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
889 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
890
Paul Bakker5b4af392014-06-26 12:09:34 +0200891 md5_free( &md5 );
892 sha1_free( &sha1 );
893
Paul Bakker380da532012-04-18 16:10:25 +0000894 return;
895}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200896#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000897
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200898#if defined(POLARSSL_SSL_PROTO_TLS1_2)
899#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
907 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000908
909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha256_free( &sha256 );
913
Paul Bakker380da532012-04-18 16:10:25 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000917
Paul Bakker9e36f042013-06-30 14:34:05 +0200918#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000919void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
920{
Paul Bakker9e36f042013-06-30 14:34:05 +0200921 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000922
923 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
924
Paul Bakker9e36f042013-06-30 14:34:05 +0200925 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
926 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Paul Bakkerca4ab492012-04-18 14:23:57 +0000928 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
930
Paul Bakker5b4af392014-06-26 12:09:34 +0200931 sha512_free( &sha512 );
932
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 return;
934}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200935#endif /* POLARSSL_SHA512_C */
936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000937
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200938#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200939int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
940{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941 unsigned char *p = ssl->handshake->premaster;
942 unsigned char *end = p + sizeof( ssl->handshake->premaster );
943
944 /*
945 * PMS = struct {
946 * opaque other_secret<0..2^16-1>;
947 * opaque psk<0..2^16-1>;
948 * };
949 * with "other_secret" depending on the particular key exchange
950 */
951#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
952 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
953 {
954 if( end - p < 2 + (int) ssl->psk_len )
955 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
956
957 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
958 *(p++) = (unsigned char)( ssl->psk_len );
959 p += ssl->psk_len;
960 }
961 else
962#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200963#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
964 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
965 {
966 /*
967 * other_secret already set by the ClientKeyExchange message,
968 * and is 48 bytes long
969 */
970 *p++ = 0;
971 *p++ = 48;
972 p += 48;
973 }
974 else
975#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200976#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
977 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
978 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200979 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200980 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200981
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200982 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200984 p + 2, &len,
985 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200986 {
987 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
988 return( ret );
989 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200990 *(p++) = (unsigned char)( len >> 8 );
991 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200992 p += len;
993
994 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
995 }
996 else
997#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
998#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
999 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1000 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001001 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001002 size_t zlen;
1003
1004 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001005 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001006 ssl->f_rng, ssl->p_rng ) ) != 0 )
1007 {
1008 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1009 return( ret );
1010 }
1011
1012 *(p++) = (unsigned char)( zlen >> 8 );
1013 *(p++) = (unsigned char)( zlen );
1014 p += zlen;
1015
1016 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1017 }
1018 else
1019#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1020 {
1021 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001022 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001023 }
1024
1025 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001026 if( end - p < 2 + (int) ssl->psk_len )
1027 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1028
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001029 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1030 *(p++) = (unsigned char)( ssl->psk_len );
1031 memcpy( p, ssl->psk, ssl->psk_len );
1032 p += ssl->psk_len;
1033
1034 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1035
1036 return( 0 );
1037}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001038#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001039
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001040#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001041/*
1042 * SSLv3.0 MAC functions
1043 */
Paul Bakker68884e32013-01-07 18:20:04 +01001044static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1045 unsigned char *buf, size_t len,
1046 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001047{
1048 unsigned char header[11];
1049 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001050 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001051 int md_size = md_get_size( md_ctx->md_info );
1052 int md_type = md_get_type( md_ctx->md_info );
1053
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001054 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001055 if( md_type == POLARSSL_MD_MD5 )
1056 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001057 else
Paul Bakker68884e32013-01-07 18:20:04 +01001058 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001059
1060 memcpy( header, ctr, 8 );
1061 header[ 8] = (unsigned char) type;
1062 header[ 9] = (unsigned char)( len >> 8 );
1063 header[10] = (unsigned char)( len );
1064
Paul Bakker68884e32013-01-07 18:20:04 +01001065 memset( padding, 0x36, padlen );
1066 md_starts( md_ctx );
1067 md_update( md_ctx, secret, md_size );
1068 md_update( md_ctx, padding, padlen );
1069 md_update( md_ctx, header, 11 );
1070 md_update( md_ctx, buf, len );
1071 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001072
Paul Bakker68884e32013-01-07 18:20:04 +01001073 memset( padding, 0x5C, padlen );
1074 md_starts( md_ctx );
1075 md_update( md_ctx, secret, md_size );
1076 md_update( md_ctx, padding, padlen );
1077 md_update( md_ctx, buf + len, md_size );
1078 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001079}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001080#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001081
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001082#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1083 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1084 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1085#define POLARSSL_SOME_MODES_USE_MAC
1086#endif
1087
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001088/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001090 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001091static int ssl_encrypt_buf( ssl_context *ssl )
1092{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001093 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001094 cipher_mode_t mode;
1095 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001096
1097 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1098
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001099 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1100 {
1101 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1102 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1103 }
1104
1105 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1106
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001107 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1108 ssl->out_msg, ssl->out_msglen );
1109
Paul Bakker5121ce52009-01-03 21:22:43 +00001110 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001111 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001113#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001114 if( mode == POLARSSL_MODE_STREAM ||
1115 ( mode == POLARSSL_MODE_CBC
1116#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1117 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1118#endif
1119 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001121#if defined(POLARSSL_SSL_PROTO_SSL3)
1122 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1123 {
1124 ssl_mac( &ssl->transform_out->md_ctx_enc,
1125 ssl->transform_out->mac_enc,
1126 ssl->out_msg, ssl->out_msglen,
1127 ssl->out_ctr, ssl->out_msgtype );
1128 }
1129 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001130#endif
1131#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001132 defined(POLARSSL_SSL_PROTO_TLS1_2)
1133 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1134 {
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1136 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg, ssl->out_msglen );
1138 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg + ssl->out_msglen );
1140 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1141 }
1142 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001143#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001144 {
1145 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 }
1148
1149 SSL_DEBUG_BUF( 4, "computed mac",
1150 ssl->out_msg + ssl->out_msglen,
1151 ssl->transform_out->maclen );
1152
1153 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001154 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001155 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001156#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001158 /*
1159 * Encrypt
1160 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001161#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001162 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001164 int ret;
1165 size_t olen = 0;
1166
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1168 "including %d bytes of padding",
1169 ssl->out_msglen, 0 ) );
1170
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001171 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001172 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 ssl->transform_out->ivlen,
1174 ssl->out_msg, ssl->out_msglen,
1175 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001176 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001177 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001178 return( ret );
1179 }
1180
1181 if( ssl->out_msglen != olen )
1182 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001183 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001184 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186 }
Paul Bakker68884e32013-01-07 18:20:04 +01001187 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001188#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001189#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1190 if( mode == POLARSSL_MODE_GCM ||
1191 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001192 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001193 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001194 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001195 unsigned char *enc_msg;
1196 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001197 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1198 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001199
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200 memcpy( add_data, ssl->out_ctr, 8 );
1201 add_data[8] = ssl->out_msgtype;
1202 add_data[9] = ssl->major_ver;
1203 add_data[10] = ssl->minor_ver;
1204 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1205 add_data[12] = ssl->out_msglen & 0xFF;
1206
1207 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1208 add_data, 13 );
1209
Paul Bakker68884e32013-01-07 18:20:04 +01001210 /*
1211 * Generate IV
1212 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001213#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001214 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001215 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1216 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001217 if( ret != 0 )
1218 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001219
Paul Bakker68884e32013-01-07 18:20:04 +01001220 memcpy( ssl->out_iv,
1221 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1222 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001223#else
1224 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1225 {
1226 /* Reminder if we ever add an AEAD mode with a different size */
1227 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1228 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1229 }
1230
1231 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1232 ssl->out_ctr, 8 );
1233 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1234#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001235
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001236 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001237 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001238
Paul Bakker68884e32013-01-07 18:20:04 +01001239 /*
1240 * Fix pointer positions and message length with added IV
1241 */
1242 enc_msg = ssl->out_msg;
1243 enc_msglen = ssl->out_msglen;
1244 ssl->out_msglen += ssl->transform_out->ivlen -
1245 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001246
Paul Bakker68884e32013-01-07 18:20:04 +01001247 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1248 "including %d bytes of padding",
1249 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001250
Paul Bakker68884e32013-01-07 18:20:04 +01001251 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001252 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001253 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1255 ssl->transform_out->iv_enc,
1256 ssl->transform_out->ivlen,
1257 add_data, 13,
1258 enc_msg, enc_msglen,
1259 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001260 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001262 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001263 return( ret );
1264 }
1265
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001266 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001267 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001268 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001269 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001270 }
1271
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001272 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001273 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001274
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001275 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001278#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001279#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1280 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001281 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001282 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001284 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001285 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001286
Paul Bakker48916f92012-09-16 19:57:18 +00001287 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1288 ssl->transform_out->ivlen;
1289 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001290 padlen = 0;
1291
1292 for( i = 0; i <= padlen; i++ )
1293 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1294
1295 ssl->out_msglen += padlen + 1;
1296
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001297 enc_msglen = ssl->out_msglen;
1298 enc_msg = ssl->out_msg;
1299
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001300#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001301 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001302 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1303 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001305 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001306 {
1307 /*
1308 * Generate IV
1309 */
Paul Bakker48916f92012-09-16 19:57:18 +00001310 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1311 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001312 if( ret != 0 )
1313 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001314
Paul Bakker92be97b2013-01-02 17:30:03 +01001315 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001316 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001317
1318 /*
1319 * Fix pointer positions and message length with added IV
1320 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001321 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001322 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001325#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001328 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001329 ssl->out_msglen, ssl->transform_out->ivlen,
1330 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001331
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001333 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001334 ssl->transform_out->ivlen,
1335 enc_msg, enc_msglen,
1336 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001337 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001338 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001339 return( ret );
1340 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001341
Paul Bakkercca5b812013-08-31 17:40:26 +02001342 if( enc_msglen != olen )
1343 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001344 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001345 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001346 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001347
1348#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001349 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1350 {
1351 /*
1352 * Save IV in SSL3 and TLS1
1353 */
1354 memcpy( ssl->transform_out->iv_enc,
1355 ssl->transform_out->cipher_ctx_enc.iv,
1356 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001358#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001359
1360#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001361 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001362 {
1363 /*
1364 * MAC(MAC_write_key, seq_num +
1365 * TLSCipherText.type +
1366 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001367 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001368 * IV + // except for TLS 1.0
1369 * ENC(content + padding + padding_length));
1370 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001371 unsigned char pseudo_hdr[13];
1372
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001373 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1374
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001375 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1376 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001377 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1378 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1379
1380 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001381
1382 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1383 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1384 ssl->out_iv, ssl->out_msglen );
1385 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1386 ssl->out_iv + ssl->out_msglen );
1387 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1388
1389 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001390 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001391 }
1392#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001394 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001395#endif /* POLARSSL_CIPHER_MODE_CBC &&
1396 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001397 {
1398 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001399 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001402 /* Make extra sure authentication was performed, exactly once */
1403 if( auth_done != 1 )
1404 {
1405 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1406 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1407 }
1408
Paul Bakkerca4ab492012-04-18 14:23:57 +00001409 for( i = 8; i > 0; i-- )
1410 if( ++ssl->out_ctr[i - 1] != 0 )
1411 break;
1412
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001413 /* The loops goes to its end iff the counter is wrapping */
1414 if( i == 0 )
1415 {
1416 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1417 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1418 }
1419
Paul Bakker5121ce52009-01-03 21:22:43 +00001420 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1421
1422 return( 0 );
1423}
1424
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001425#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001426
Paul Bakker5121ce52009-01-03 21:22:43 +00001427static int ssl_decrypt_buf( ssl_context *ssl )
1428{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001429 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001430 cipher_mode_t mode;
1431 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001432#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001433 size_t padlen = 0, correct = 1;
1434#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001435
1436 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1437
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001438 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1439 {
1440 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1441 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1442 }
1443
1444 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1445
Paul Bakker48916f92012-09-16 19:57:18 +00001446 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001447 {
1448 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001449 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001450 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001451 }
1452
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001453#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001454 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001455 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001456 int ret;
1457 size_t olen = 0;
1458
Paul Bakker68884e32013-01-07 18:20:04 +01001459 padlen = 0;
1460
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001461 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001462 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001463 ssl->transform_in->ivlen,
1464 ssl->in_msg, ssl->in_msglen,
1465 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001466 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001467 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001468 return( ret );
1469 }
1470
1471 if( ssl->in_msglen != olen )
1472 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001474 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 }
Paul Bakker68884e32013-01-07 18:20:04 +01001477 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001478#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001479#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1480 if( mode == POLARSSL_MODE_GCM ||
1481 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001483 int ret;
1484 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 unsigned char *dec_msg;
1486 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001488 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1489 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001490 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1491 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001492
Manuel Pégourié-Gonnard06d75192015-02-11 14:54:11 +00001493 if( ssl->in_msglen < (size_t) explicit_iv_len + taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001494 {
1495 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1496 "+ taglen (%d)", ssl->in_msglen,
1497 explicit_iv_len, taglen ) );
1498 return( POLARSSL_ERR_SSL_INVALID_MAC );
1499 }
1500 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1501
Paul Bakker68884e32013-01-07 18:20:04 +01001502 dec_msg = ssl->in_msg;
1503 dec_msg_result = ssl->in_msg;
1504 ssl->in_msglen = dec_msglen;
1505
1506 memcpy( add_data, ssl->in_ctr, 8 );
1507 add_data[8] = ssl->in_msgtype;
1508 add_data[9] = ssl->major_ver;
1509 add_data[10] = ssl->minor_ver;
1510 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1511 add_data[12] = ssl->in_msglen & 0xFF;
1512
1513 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1514 add_data, 13 );
1515
1516 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1517 ssl->in_iv,
1518 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1519
1520 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1521 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001522 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001523
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001524 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001525 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001526 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001527 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1528 ssl->transform_in->iv_dec,
1529 ssl->transform_in->ivlen,
1530 add_data, 13,
1531 dec_msg, dec_msglen,
1532 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001533 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001534 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001535 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1536
1537 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1538 return( POLARSSL_ERR_SSL_INVALID_MAC );
1539
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001540 return( ret );
1541 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001542 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001543
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001544 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001545 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001546 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001547 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001548 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001549 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001550 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001551#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001552#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1553 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001554 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 {
Paul Bakker45829992013-01-03 14:52:21 +01001556 /*
1557 * Decrypt and check the padding
1558 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001559 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001560 unsigned char *dec_msg;
1561 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001562 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001563 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001564 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001565
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 /*
Paul Bakker45829992013-01-03 14:52:21 +01001567 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001569#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001570 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1571 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001572#endif
Paul Bakker45829992013-01-03 14:52:21 +01001573
1574 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1575 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1576 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001577 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1578 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1579 ssl->transform_in->ivlen,
1580 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001581 return( POLARSSL_ERR_SSL_INVALID_MAC );
1582 }
1583
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001584 dec_msglen = ssl->in_msglen;
1585 dec_msg = ssl->in_msg;
1586 dec_msg_result = ssl->in_msg;
1587
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001588 /*
1589 * Authenticate before decrypt if enabled
1590 */
1591#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001592 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001593 {
1594 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001595 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001596
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001597 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1598
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001599 dec_msglen -= ssl->transform_in->maclen;
1600 ssl->in_msglen -= ssl->transform_in->maclen;
1601
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001602 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1603 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1604 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1605 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1606
1607 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1608
1609 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001610 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1611 ssl->in_iv, ssl->in_msglen );
1612 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1613 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1614
1615 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1616 ssl->transform_in->maclen );
1617 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1618 ssl->transform_in->maclen );
1619
1620 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1621 ssl->transform_in->maclen ) != 0 )
1622 {
1623 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1624
1625 return( POLARSSL_ERR_SSL_INVALID_MAC );
1626 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001627 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001628 }
1629#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1630
1631 /*
1632 * Check length sanity
1633 */
1634 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1635 {
1636 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1637 ssl->in_msglen, ssl->transform_in->ivlen ) );
1638 return( POLARSSL_ERR_SSL_INVALID_MAC );
1639 }
1640
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001641#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001642 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001643 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001645 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001646 {
Paul Bakker48916f92012-09-16 19:57:18 +00001647 dec_msglen -= ssl->transform_in->ivlen;
1648 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001649
Paul Bakker48916f92012-09-16 19:57:18 +00001650 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001651 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001652 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001653#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001654
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001655 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001656 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001657 ssl->transform_in->ivlen,
1658 dec_msg, dec_msglen,
1659 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001660 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001661 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001662 return( ret );
1663 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001664
Paul Bakkercca5b812013-08-31 17:40:26 +02001665 if( dec_msglen != olen )
1666 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001667 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001668 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001669 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001670
1671#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001672 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1673 {
1674 /*
1675 * Save IV in SSL3 and TLS1
1676 */
1677 memcpy( ssl->transform_in->iv_dec,
1678 ssl->transform_in->cipher_ctx_dec.iv,
1679 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001680 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001681#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
1683 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001684
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001685 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001686 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001687 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001688#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001689 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1690 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001691#endif
Paul Bakker45829992013-01-03 14:52:21 +01001692 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001693 correct = 0;
1694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001696#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1698 {
Paul Bakker48916f92012-09-16 19:57:18 +00001699 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001701#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1703 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001704 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001705#endif
Paul Bakker45829992013-01-03 14:52:21 +01001706 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 }
1708 }
1709 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001710#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001711#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1712 defined(POLARSSL_SSL_PROTO_TLS1_2)
1713 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 {
1715 /*
Paul Bakker45829992013-01-03 14:52:21 +01001716 * TLSv1+: always check the padding up to the first failure
1717 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001719 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720 size_t padding_idx = ssl->in_msglen - padlen - 1;
1721
Paul Bakker956c9e02013-12-19 14:42:28 +01001722 /*
1723 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001724 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001725 *
Paul Bakker61885c72014-04-25 12:59:03 +02001726 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1727 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001728 *
1729 * In both cases we reset padding_idx to a safe value (0) to
1730 * prevent out-of-buffer reads.
1731 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001732 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001733 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1734 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001735
1736 padding_idx *= correct;
1737
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001738 for( i = 1; i <= 256; i++ )
1739 {
1740 real_count &= ( i <= padlen );
1741 pad_count += real_count *
1742 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1743 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001744
1745 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001746
Paul Bakkerd66f0702013-01-31 16:57:45 +01001747#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001748 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001749 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001750#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001753 else
1754#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1755 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001756 {
1757 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001758 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001759 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001760
1761 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001762 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001763 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001764#endif /* POLARSSL_CIPHER_MODE_CBC &&
1765 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001766 {
1767 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001768 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001769 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001770
1771 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1772 ssl->in_msg, ssl->in_msglen );
1773
1774 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001775 * Authenticate if not done yet.
1776 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001777 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001778#if defined(POLARSSL_SOME_MODES_USE_MAC)
1779 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001781 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1782
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001783 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001784
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001785 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1786 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001788 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001790#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001791 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1792 {
1793 ssl_mac( &ssl->transform_in->md_ctx_dec,
1794 ssl->transform_in->mac_dec,
1795 ssl->in_msg, ssl->in_msglen,
1796 ssl->in_ctr, ssl->in_msgtype );
1797 }
1798 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001799#endif /* POLARSSL_SSL_PROTO_SSL3 */
1800#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001801 defined(POLARSSL_SSL_PROTO_TLS1_2)
1802 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1803 {
1804 /*
1805 * Process MAC and always update for padlen afterwards to make
1806 * total time independent of padlen
1807 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001808 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001809 *
1810 * Known timing attacks:
1811 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1812 *
1813 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1814 * correctly. (We round down instead of up, so -56 is the correct
1815 * value for our calculations instead of -55)
1816 */
1817 size_t j, extra_run = 0;
1818 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1819 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001820
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001821 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001822
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001823 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1824 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1825 ssl->in_msglen );
1826 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1827 ssl->in_msg + ssl->in_msglen );
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02001828 /* Call md_process at least once due to cache attacks */
1829 for( j = 0; j < extra_run + 1; j++ )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001830 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001831
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001832 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1833 }
1834 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001835#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001836 POLARSSL_SSL_PROTO_TLS1_2 */
1837 {
1838 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001839 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001840 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001842 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1843 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1844 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001846 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 ssl->transform_in->maclen ) != 0 )
1848 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001849#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001850 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001851#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001852 correct = 0;
1853 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001854 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001856 /*
1857 * Finally check the correct flag
1858 */
1859 if( correct == 0 )
1860 return( POLARSSL_ERR_SSL_INVALID_MAC );
1861 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001862#endif /* POLARSSL_SOME_MODES_USE_MAC */
1863
1864 /* Make extra sure authentication was performed, exactly once */
1865 if( auth_done != 1 )
1866 {
1867 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1868 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 if( ssl->in_msglen == 0 )
1872 {
1873 ssl->nb_zero++;
1874
1875 /*
1876 * Three or more empty messages may be a DoS attack
1877 * (excessive CPU consumption).
1878 */
1879 if( ssl->nb_zero > 3 )
1880 {
1881 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1882 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001883 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 }
1885 }
1886 else
1887 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001888
Paul Bakker23986e52011-04-24 08:57:21 +00001889 for( i = 8; i > 0; i-- )
1890 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 break;
1892
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001893 /* The loops goes to its end iff the counter is wrapping */
1894 if( i == 0 )
1895 {
1896 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1897 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1898 }
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1901
1902 return( 0 );
1903}
1904
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001905#undef MAC_NONE
1906#undef MAC_PLAINTEXT
1907#undef MAC_CIPHERTEXT
1908
Paul Bakker2770fbd2012-07-03 13:30:23 +00001909#if defined(POLARSSL_ZLIB_SUPPORT)
1910/*
1911 * Compression/decompression functions
1912 */
1913static int ssl_compress_buf( ssl_context *ssl )
1914{
1915 int ret;
1916 unsigned char *msg_post = ssl->out_msg;
1917 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001918 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001919
1920 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1921
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001922 if( len_pre == 0 )
1923 return( 0 );
1924
Paul Bakker2770fbd2012-07-03 13:30:23 +00001925 memcpy( msg_pre, ssl->out_msg, len_pre );
1926
1927 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1928 ssl->out_msglen ) );
1929
1930 SSL_DEBUG_BUF( 4, "before compression: output payload",
1931 ssl->out_msg, ssl->out_msglen );
1932
Paul Bakker48916f92012-09-16 19:57:18 +00001933 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1934 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1935 ssl->transform_out->ctx_deflate.next_out = msg_post;
1936 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001937
Paul Bakker48916f92012-09-16 19:57:18 +00001938 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001939 if( ret != Z_OK )
1940 {
1941 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1942 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1943 }
1944
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001945 ssl->out_msglen = SSL_BUFFER_LEN -
1946 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001947
Paul Bakker2770fbd2012-07-03 13:30:23 +00001948 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1949 ssl->out_msglen ) );
1950
1951 SSL_DEBUG_BUF( 4, "after compression: output payload",
1952 ssl->out_msg, ssl->out_msglen );
1953
1954 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1955
1956 return( 0 );
1957}
1958
1959static int ssl_decompress_buf( ssl_context *ssl )
1960{
1961 int ret;
1962 unsigned char *msg_post = ssl->in_msg;
1963 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001964 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001965
1966 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1967
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001968 if( len_pre == 0 )
1969 return( 0 );
1970
Paul Bakker2770fbd2012-07-03 13:30:23 +00001971 memcpy( msg_pre, ssl->in_msg, len_pre );
1972
1973 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1974 ssl->in_msglen ) );
1975
1976 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1977 ssl->in_msg, ssl->in_msglen );
1978
Paul Bakker48916f92012-09-16 19:57:18 +00001979 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1980 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1981 ssl->transform_in->ctx_inflate.next_out = msg_post;
1982 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001983
Paul Bakker48916f92012-09-16 19:57:18 +00001984 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001985 if( ret != Z_OK )
1986 {
1987 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1988 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1989 }
1990
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001991 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1992 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001993
Paul Bakker2770fbd2012-07-03 13:30:23 +00001994 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1995 ssl->in_msglen ) );
1996
1997 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1998 ssl->in_msg, ssl->in_msglen );
1999
2000 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2001
2002 return( 0 );
2003}
2004#endif /* POLARSSL_ZLIB_SUPPORT */
2005
Paul Bakker5121ce52009-01-03 21:22:43 +00002006/*
2007 * Fill the input message buffer
2008 */
Paul Bakker23986e52011-04-24 08:57:21 +00002009int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002010{
Paul Bakker23986e52011-04-24 08:57:21 +00002011 int ret;
2012 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
2014 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2015
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002016 if( nb_want > SSL_BUFFER_LEN - 8 )
2017 {
2018 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2019 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2020 }
2021
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 while( ssl->in_left < nb_want )
2023 {
2024 len = nb_want - ssl->in_left;
2025 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2026
2027 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2028 ssl->in_left, nb_want ) );
2029 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2030
Paul Bakker831a7552011-05-18 13:32:51 +00002031 if( ret == 0 )
2032 return( POLARSSL_ERR_SSL_CONN_EOF );
2033
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 if( ret < 0 )
2035 return( ret );
2036
2037 ssl->in_left += ret;
2038 }
2039
2040 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2041
2042 return( 0 );
2043}
2044
2045/*
2046 * Flush any data not yet written
2047 */
2048int ssl_flush_output( ssl_context *ssl )
2049{
2050 int ret;
2051 unsigned char *buf;
2052
2053 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2054
2055 while( ssl->out_left > 0 )
2056 {
2057 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2058 5 + ssl->out_msglen, ssl->out_left ) );
2059
Paul Bakker5bd42292012-12-19 14:40:42 +01002060 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002062
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2064
2065 if( ret <= 0 )
2066 return( ret );
2067
2068 ssl->out_left -= ret;
2069 }
2070
2071 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2072
2073 return( 0 );
2074}
2075
2076/*
2077 * Record layer functions
2078 */
2079int ssl_write_record( ssl_context *ssl )
2080{
Paul Bakker05ef8352012-05-08 09:17:57 +00002081 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002082 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002083
2084 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2085
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2087 {
2088 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2089 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2090 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2091
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002092 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2093 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
2095
Paul Bakker2770fbd2012-07-03 13:30:23 +00002096#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002097 if( ssl->transform_out != NULL &&
2098 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002099 {
2100 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2101 {
2102 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2103 return( ret );
2104 }
2105
2106 len = ssl->out_msglen;
2107 }
2108#endif /*POLARSSL_ZLIB_SUPPORT */
2109
Paul Bakker05ef8352012-05-08 09:17:57 +00002110#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002111 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002113 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
Paul Bakker05ef8352012-05-08 09:17:57 +00002115 ret = ssl_hw_record_write( ssl );
2116 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2117 {
2118 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002119 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002120 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002121
2122 if( ret == 0 )
2123 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002124 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002125#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002126 if( !done )
2127 {
2128 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2129 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2130 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2132 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002133
Paul Bakker48916f92012-09-16 19:57:18 +00002134 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002135 {
2136 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2137 {
2138 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2139 return( ret );
2140 }
2141
2142 len = ssl->out_msglen;
2143 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2144 ssl->out_hdr[4] = (unsigned char)( len );
2145 }
2146
2147 ssl->out_left = 5 + ssl->out_msglen;
2148
2149 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2150 "version = [%d:%d], msglen = %d",
2151 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2152 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2153
2154 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002155 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 }
2157
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2159 {
2160 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2161 return( ret );
2162 }
2163
2164 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2165
2166 return( 0 );
2167}
2168
2169int ssl_read_record( ssl_context *ssl )
2170{
Paul Bakker05ef8352012-05-08 09:17:57 +00002171 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
2173 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2174
2175 if( ssl->in_hslen != 0 &&
2176 ssl->in_hslen < ssl->in_msglen )
2177 {
2178 /*
2179 * Get next Handshake message in the current record
2180 */
2181 ssl->in_msglen -= ssl->in_hslen;
2182
Paul Bakker8934a982011-08-05 11:11:53 +00002183 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002184 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002185
2186 ssl->in_hslen = 4;
2187 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2188
2189 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2190 " %d, type = %d, hslen = %d",
2191 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2192
2193 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2194 {
2195 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002196 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 }
2198
2199 if( ssl->in_msglen < ssl->in_hslen )
2200 {
2201 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002202 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 }
2204
Paul Bakker4224bc02014-04-08 14:36:50 +02002205 if( ssl->state != SSL_HANDSHAKE_OVER )
2206 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002207
2208 return( 0 );
2209 }
2210
2211 ssl->in_hslen = 0;
2212
2213 /*
2214 * Read the record header and validate it
2215 */
2216 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2217 {
2218 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2219 return( ret );
2220 }
2221
2222 ssl->in_msgtype = ssl->in_hdr[0];
2223 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2224
2225 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2226 "version = [%d:%d], msglen = %d",
2227 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2228 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2229
2230 if( ssl->in_hdr[1] != ssl->major_ver )
2231 {
2232 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002233 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 }
2235
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002236 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 {
2238 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002239 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 }
2241
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002242 /* Sanity check (outer boundaries) */
2243 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2244 {
2245 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2246 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2247 }
2248
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002250 * Make sure the message length is acceptable for the current transform
2251 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 */
Paul Bakker48916f92012-09-16 19:57:18 +00002253 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002255 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 {
2257 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002258 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 }
2260 }
2261 else
2262 {
Paul Bakker48916f92012-09-16 19:57:18 +00002263 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
2265 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002266 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002267 }
2268
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002269#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002271 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
2273 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002274 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002276#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002278#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2279 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 /*
2281 * TLS encrypted messages can have up to 256 bytes of padding
2282 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002283 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002284 ssl->in_msglen > ssl->transform_in->minlen +
2285 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 {
2287 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002291 }
2292
2293 /*
2294 * Read and optionally decrypt the message contents
2295 */
2296 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2297 {
2298 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2299 return( ret );
2300 }
2301
2302 SSL_DEBUG_BUF( 4, "input record from network",
2303 ssl->in_hdr, 5 + ssl->in_msglen );
2304
Paul Bakker05ef8352012-05-08 09:17:57 +00002305#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002306 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002307 {
2308 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2309
2310 ret = ssl_hw_record_read( ssl );
2311 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2312 {
2313 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002314 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002315 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002316
2317 if( ret == 0 )
2318 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002319 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002320#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002321 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002322 {
2323 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2324 {
Paul Bakker40865c82013-01-31 17:13:13 +01002325#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2326 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2327 {
2328 ssl_send_alert_message( ssl,
2329 SSL_ALERT_LEVEL_FATAL,
2330 SSL_ALERT_MSG_BAD_RECORD_MAC );
2331 }
2332#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002333 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2334 return( ret );
2335 }
2336
2337 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2338 ssl->in_msg, ssl->in_msglen );
2339
2340 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2341 {
2342 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002343 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002344 }
2345 }
2346
Paul Bakker2770fbd2012-07-03 13:30:23 +00002347#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002348 if( ssl->transform_in != NULL &&
2349 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002350 {
2351 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2352 {
2353 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2354 return( ret );
2355 }
2356
2357 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2358 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2359 }
2360#endif /* POLARSSL_ZLIB_SUPPORT */
2361
Paul Bakker0a925182012-04-16 06:46:41 +00002362 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2363 ssl->in_msgtype != SSL_MSG_ALERT &&
2364 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2365 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2366 {
2367 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2368
Paul Bakker48916f92012-09-16 19:57:18 +00002369 if( ( ret = ssl_send_alert_message( ssl,
2370 SSL_ALERT_LEVEL_FATAL,
2371 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002372 {
2373 return( ret );
2374 }
2375
2376 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2377 }
2378
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2380 {
2381 ssl->in_hslen = 4;
2382 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2383
2384 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2385 " %d, type = %d, hslen = %d",
2386 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2387
2388 /*
2389 * Additional checks to validate the handshake header
2390 */
2391 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2392 {
2393 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002394 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 }
2396
2397 if( ssl->in_msglen < ssl->in_hslen )
2398 {
2399 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002400 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402
Paul Bakker48916f92012-09-16 19:57:18 +00002403 if( ssl->state != SSL_HANDSHAKE_OVER )
2404 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406
2407 if( ssl->in_msgtype == SSL_MSG_ALERT )
2408 {
2409 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2410 ssl->in_msg[0], ssl->in_msg[1] ) );
2411
2412 /*
2413 * Ignore non-fatal alerts, except close_notify
2414 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002415 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002417 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2418 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002419 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 }
2421
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2423 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
2425 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002426 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 }
2428 }
2429
2430 ssl->in_left = 0;
2431
2432 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2433
2434 return( 0 );
2435}
2436
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002437int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2438{
2439 int ret;
2440
2441 if( ( ret = ssl_send_alert_message( ssl,
2442 SSL_ALERT_LEVEL_FATAL,
2443 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2444 {
2445 return( ret );
2446 }
2447
2448 return( 0 );
2449}
2450
Paul Bakker0a925182012-04-16 06:46:41 +00002451int ssl_send_alert_message( ssl_context *ssl,
2452 unsigned char level,
2453 unsigned char message )
2454{
2455 int ret;
2456
2457 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2458
2459 ssl->out_msgtype = SSL_MSG_ALERT;
2460 ssl->out_msglen = 2;
2461 ssl->out_msg[0] = level;
2462 ssl->out_msg[1] = message;
2463
2464 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2465 {
2466 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2467 return( ret );
2468 }
2469
2470 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2471
2472 return( 0 );
2473}
2474
Paul Bakker5121ce52009-01-03 21:22:43 +00002475/*
2476 * Handshake functions
2477 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002478#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2479 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2481 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2483 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2484 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002485int ssl_write_certificate( ssl_context *ssl )
2486{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002487 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
2489 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2490
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002494 {
2495 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2496 ssl->state++;
2497 return( 0 );
2498 }
2499
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002500 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2501 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002502}
2503
2504int ssl_parse_certificate( ssl_context *ssl )
2505{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002506 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2507
2508 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2509
2510 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 {
2514 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2515 ssl->state++;
2516 return( 0 );
2517 }
2518
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002519 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2520 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002521}
2522#else
2523int ssl_write_certificate( ssl_context *ssl )
2524{
2525 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2526 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002527 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002528 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2529
2530 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2531
2532 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002535 {
2536 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2537 ssl->state++;
2538 return( 0 );
2539 }
2540
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002541#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 if( ssl->endpoint == SSL_IS_CLIENT )
2543 {
2544 if( ssl->client_auth == 0 )
2545 {
2546 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2547 ssl->state++;
2548 return( 0 );
2549 }
2550
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002551#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 /*
2553 * If using SSLv3 and got no cert, send an Alert message
2554 * (otherwise an empty Certificate message will be sent).
2555 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002556 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2558 {
2559 ssl->out_msglen = 2;
2560 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002561 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2562 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002563
2564 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2565 goto write_msg;
2566 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002567#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002568 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002569#endif /* POLARSSL_SSL_CLI_C */
2570#if defined(POLARSSL_SSL_SRV_C)
2571 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002573 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
2575 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002576 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 }
2578 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002579#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002580
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002581 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
2583 /*
2584 * 0 . 0 handshake type
2585 * 1 . 3 handshake length
2586 * 4 . 6 length of all certs
2587 * 7 . 9 length of cert. 1
2588 * 10 . n-1 peer certificate
2589 * n . n+2 length of cert. 2
2590 * n+3 . ... upper level cert, etc.
2591 */
2592 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002593 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
Paul Bakker29087132010-03-21 21:03:34 +00002595 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
2597 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002598 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 {
2600 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2601 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002602 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
2604
2605 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2606 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2607 ssl->out_msg[i + 2] = (unsigned char)( n );
2608
2609 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2610 i += n; crt = crt->next;
2611 }
2612
2613 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2614 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2615 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2616
2617 ssl->out_msglen = i;
2618 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2619 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2620
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002621#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002622write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002623#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002624
2625 ssl->state++;
2626
2627 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2628 {
2629 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2630 return( ret );
2631 }
2632
2633 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2634
Paul Bakkered27a042013-04-18 22:46:23 +02002635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636}
2637
2638int ssl_parse_certificate( ssl_context *ssl )
2639{
Paul Bakkered27a042013-04-18 22:46:23 +02002640 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002641 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002642 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
2644 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2645
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002647 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002649 {
2650 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2651 ssl->state++;
2652 return( 0 );
2653 }
2654
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002655#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002657 ( ssl->authmode == SSL_VERIFY_NONE ||
2658 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002660 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002661 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2662 ssl->state++;
2663 return( 0 );
2664 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002665#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2668 {
2669 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2670 return( ret );
2671 }
2672
2673 ssl->state++;
2674
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002675#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002676#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 /*
2678 * Check if the client sent an empty certificate
2679 */
2680 if( ssl->endpoint == SSL_IS_SERVER &&
2681 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2682 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002683 if( ssl->in_msglen == 2 &&
2684 ssl->in_msgtype == SSL_MSG_ALERT &&
2685 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2686 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 {
2688 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2689
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002690 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2692 return( 0 );
2693 else
Paul Bakker40e46942009-01-03 21:51:57 +00002694 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002695 }
2696 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002697#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002699#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2700 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 if( ssl->endpoint == SSL_IS_SERVER &&
2702 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2703 {
2704 if( ssl->in_hslen == 7 &&
2705 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2706 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2707 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2708 {
2709 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2710
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002711 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002713 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 else
2715 return( 0 );
2716 }
2717 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002718#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2719 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002720#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002721
2722 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002725 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002726 }
2727
2728 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2729 {
2730 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002731 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 }
2733
2734 /*
2735 * Same message structure as in ssl_write_certificate()
2736 */
2737 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2738
2739 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2740 {
2741 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002742 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 }
2744
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002745 /* In case we tried to reuse a session but it failed */
2746 if( ssl->session_negotiate->peer_cert != NULL )
2747 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002748 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002749 polarssl_free( ssl->session_negotiate->peer_cert );
2750 }
2751
Mansour Moufidc531b4a2015-02-15 17:35:38 -05002752 if( ( ssl->session_negotiate->peer_cert = polarssl_malloc(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002753 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 {
2755 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002756 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002757 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 }
2759
Paul Bakkerb6b09562013-09-18 14:17:41 +02002760 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
2762 i = 7;
2763
2764 while( i < ssl->in_hslen )
2765 {
2766 if( ssl->in_msg[i] != 0 )
2767 {
2768 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002769 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 }
2771
2772 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2773 | (unsigned int) ssl->in_msg[i + 2];
2774 i += 3;
2775
2776 if( n < 128 || i + n > ssl->in_hslen )
2777 {
2778 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002779 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 }
2781
Paul Bakkerddf26b42013-09-18 13:46:23 +02002782 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2783 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 if( ret != 0 )
2785 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002786 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002787 return( ret );
2788 }
2789
2790 i += n;
2791 }
2792
Paul Bakker48916f92012-09-16 19:57:18 +00002793 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002794
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002795 /*
2796 * On client, make sure the server cert doesn't change during renego to
2797 * avoid "triple handshake" attack: https://secure-resumption.com/
2798 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002799#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002800 if( ssl->endpoint == SSL_IS_CLIENT &&
2801 ssl->renegotiation == SSL_RENEGOTIATION )
2802 {
2803 if( ssl->session->peer_cert == NULL )
2804 {
2805 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2806 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2807 }
2808
2809 if( ssl->session->peer_cert->raw.len !=
2810 ssl->session_negotiate->peer_cert->raw.len ||
2811 memcmp( ssl->session->peer_cert->raw.p,
2812 ssl->session_negotiate->peer_cert->raw.p,
2813 ssl->session->peer_cert->raw.len ) != 0 )
2814 {
2815 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2816 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2817 }
2818 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002819#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002820
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 if( ssl->authmode != SSL_VERIFY_NONE )
2822 {
2823 if( ssl->ca_chain == NULL )
2824 {
2825 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002826 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 }
2828
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002829 /*
2830 * Main check: verify certificate
2831 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002832 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2833 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2834 &ssl->session_negotiate->verify_result,
2835 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
2837 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002838 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002840 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002841
2842 /*
2843 * Secondary checks: always done, but change 'ret' only if it was 0
2844 */
2845
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002846#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002847 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002848 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002849
2850 /* If certificate uses an EC key, make sure the curve is OK */
2851 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2852 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2853 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002854 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002855 if( ret == 0 )
2856 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002857 }
2858 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002859#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002861 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2862 ciphersuite_info,
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02002863 ! ssl->endpoint,
2864 &ssl->session_negotiate->verify_result ) != 0 )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002865 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002866 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002867 if( ret == 0 )
2868 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2869 }
2870
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2872 ret = 0;
2873 }
2874
2875 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2876
2877 return( ret );
2878}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002879#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2880 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2881 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2882 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2883 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2884 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2885 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887int ssl_write_change_cipher_spec( ssl_context *ssl )
2888{
2889 int ret;
2890
2891 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2892
2893 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2894 ssl->out_msglen = 1;
2895 ssl->out_msg[0] = 1;
2896
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 ssl->state++;
2898
2899 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2900 {
2901 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2902 return( ret );
2903 }
2904
2905 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2906
2907 return( 0 );
2908}
2909
2910int ssl_parse_change_cipher_spec( ssl_context *ssl )
2911{
2912 int ret;
2913
2914 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2917 {
2918 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2919 return( ret );
2920 }
2921
2922 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2923 {
2924 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002925 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
2927
2928 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2929 {
2930 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002931 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 }
2933
2934 ssl->state++;
2935
2936 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2937
2938 return( 0 );
2939}
2940
Paul Bakker41c83d32013-03-20 14:39:14 +01002941void ssl_optimize_checksum( ssl_context *ssl,
2942 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002943{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002944 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002945
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002948 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002949 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002950 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#endif
2952#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2953#if defined(POLARSSL_SHA512_C)
2954 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2955 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2956 else
2957#endif
2958#if defined(POLARSSL_SHA256_C)
2959 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961 else
2962#endif
2963#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002964 {
2965 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002967 }
Paul Bakker380da532012-04-18 16:10:25 +00002968}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002969
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002970static void ssl_update_checksum_start( ssl_context *ssl,
2971 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002972{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2974 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002975 md5_update( &ssl->handshake->fin_md5 , buf, len );
2976 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002977#endif
2978#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2979#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002980 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002981#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002982#if defined(POLARSSL_SHA512_C)
2983 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002984#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002986}
2987
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2989 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002990static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2991 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002992{
Paul Bakker48916f92012-09-16 19:57:18 +00002993 md5_update( &ssl->handshake->fin_md5 , buf, len );
2994 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#endif
Paul Bakker380da532012-04-18 16:10:25 +00002997
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002998#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2999#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003000static void ssl_update_checksum_sha256( ssl_context *ssl,
3001 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003002{
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif
Paul Bakker380da532012-04-18 16:10:25 +00003006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003008static void ssl_update_checksum_sha384( ssl_context *ssl,
3009 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003010{
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003012}
Paul Bakker769075d2012-11-24 11:26:46 +01003013#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003015
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003016#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017static void ssl_calc_finished_ssl(
3018 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003019{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003020 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 md5_context md5;
3022 sha1_context sha1;
3023
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 unsigned char padbuf[48];
3025 unsigned char md5sum[16];
3026 unsigned char sha1sum[20];
3027
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 ssl" ) );
3033
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
3037 /*
3038 * SSLv3:
3039 * hash =
3040 * MD5( master + pad2 +
3041 * MD5( handshake + sender + master + pad1 ) )
3042 * + SHA1( master + pad2 +
3043 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 */
3045
Paul Bakker90995b52013-06-24 19:20:35 +02003046#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakker90995b52013-06-24 19:20:35 +02003051#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker3c2122f2013-06-24 19:03:14 +02003056 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3057 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker1ef83d62012-04-11 12:09:53 +00003059 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Paul Bakker3c2122f2013-06-24 19:03:14 +02003061 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003062 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063 md5_update( &md5, padbuf, 48 );
3064 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakker3c2122f2013-06-24 19:03:14 +02003066 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003067 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068 sha1_update( &sha1, padbuf, 40 );
3069 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003074 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 md5_update( &md5, padbuf, 48 );
3076 md5_update( &md5, md5sum, 16 );
3077 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003080 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 sha1_update( &sha1, padbuf , 40 );
3082 sha1_update( &sha1, sha1sum, 20 );
3083 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker5b4af392014-06-26 12:09:34 +02003087 md5_free( &md5 );
3088 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker34617722014-06-13 17:20:13 +02003090 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3091 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3092 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
3094 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3095}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003096#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003098#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099static void ssl_calc_finished_tls(
3100 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003103 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Paul Bakker48916f92012-09-16 19:57:18 +00003108 ssl_session *session = ssl->session_negotiate;
3109 if( !session )
3110 session = ssl->session;
3111
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker48916f92012-09-16 19:57:18 +00003114 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3115 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117 /*
3118 * TLSv1:
3119 * hash = PRF( master, finished_label,
3120 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3121 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Paul Bakker90995b52013-06-24 19:20:35 +02003123#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3125 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003126#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
Paul Bakker90995b52013-06-24 19:20:35 +02003128#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3130 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003131#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003132
3133 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003134 ? "client finished"
3135 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136
3137 md5_finish( &md5, padbuf );
3138 sha1_finish( &sha1, padbuf + 16 );
3139
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003140 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003141 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142
3143 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3144
Paul Bakker5b4af392014-06-26 12:09:34 +02003145 md5_free( &md5 );
3146 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147
Paul Bakker34617722014-06-13 17:20:13 +02003148 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
3150 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3151}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003152#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003154#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3155#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003156static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157 ssl_context *ssl, unsigned char *buf, int from )
3158{
3159 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003160 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003161 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162 unsigned char padbuf[32];
3163
Paul Bakker48916f92012-09-16 19:57:18 +00003164 ssl_session *session = ssl->session_negotiate;
3165 if( !session )
3166 session = ssl->session;
3167
Paul Bakker380da532012-04-18 16:10:25 +00003168 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
Paul Bakker9e36f042013-06-30 14:34:05 +02003170 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
3172 /*
3173 * TLSv1.2:
3174 * hash = PRF( master, finished_label,
3175 * Hash( handshake ) )[0.11]
3176 */
3177
Paul Bakker9e36f042013-06-30 14:34:05 +02003178#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003180 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003181#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003182
3183 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003184 ? "client finished"
3185 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
Paul Bakker9e36f042013-06-30 14:34:05 +02003187 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003189 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003190 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191
3192 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3193
Paul Bakker5b4af392014-06-26 12:09:34 +02003194 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195
Paul Bakker34617722014-06-13 17:20:13 +02003196 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003197
3198 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3199}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003200#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201
Paul Bakker9e36f042013-06-30 14:34:05 +02003202#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003203static void ssl_calc_finished_tls_sha384(
3204 ssl_context *ssl, unsigned char *buf, int from )
3205{
3206 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003207 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003208 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003209 unsigned char padbuf[48];
3210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ssl_session *session = ssl->session_negotiate;
3212 if( !session )
3213 session = ssl->session;
3214
Paul Bakker380da532012-04-18 16:10:25 +00003215 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216
Paul Bakker9e36f042013-06-30 14:34:05 +02003217 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
3219 /*
3220 * TLSv1.2:
3221 * hash = PRF( master, finished_label,
3222 * Hash( handshake ) )[0.11]
3223 */
3224
Paul Bakker9e36f042013-06-30 14:34:05 +02003225#if !defined(POLARSSL_SHA512_ALT)
3226 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3227 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003228#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003229
3230 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003231 ? "client finished"
3232 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003233
Paul Bakker9e36f042013-06-30 14:34:05 +02003234 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003235
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003236 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003237 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003238
3239 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3240
Paul Bakker5b4af392014-06-26 12:09:34 +02003241 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003242
Paul Bakker34617722014-06-13 17:20:13 +02003243 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003244
3245 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3246}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003247#endif /* POLARSSL_SHA512_C */
3248#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003249
Paul Bakker48916f92012-09-16 19:57:18 +00003250void ssl_handshake_wrapup( ssl_context *ssl )
3251{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003252 int resume = ssl->handshake->resume;
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3255
3256 /*
3257 * Free our handshake params
3258 */
3259 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003260 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003261 ssl->handshake = NULL;
3262
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003263#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003264 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003265 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003266 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003267 ssl->renego_records_seen = 0;
3268 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003269#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003270
Paul Bakker48916f92012-09-16 19:57:18 +00003271 /*
3272 * Switch in our now active transform context
3273 */
3274 if( ssl->transform )
3275 {
3276 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003277 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003278 }
3279 ssl->transform = ssl->transform_negotiate;
3280 ssl->transform_negotiate = NULL;
3281
Paul Bakker0a597072012-09-25 21:55:46 +00003282 if( ssl->session )
3283 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003284#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3285 /* RFC 7366 3.1: keep the EtM state */
3286 ssl->session_negotiate->encrypt_then_mac =
3287 ssl->session->encrypt_then_mac;
3288#endif
3289
Paul Bakker0a597072012-09-25 21:55:46 +00003290 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003291 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003292 }
3293 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003294 ssl->session_negotiate = NULL;
3295
Paul Bakker0a597072012-09-25 21:55:46 +00003296 /*
3297 * Add cache entry
3298 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003299 if( ssl->f_set_cache != NULL &&
3300 ssl->session->length != 0 &&
3301 resume == 0 )
3302 {
Paul Bakker0a597072012-09-25 21:55:46 +00003303 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3304 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003305 }
Paul Bakker0a597072012-09-25 21:55:46 +00003306
Paul Bakker48916f92012-09-16 19:57:18 +00003307 ssl->state++;
3308
3309 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3310}
3311
Paul Bakker1ef83d62012-04-11 12:09:53 +00003312int ssl_write_finished( ssl_context *ssl )
3313{
3314 int ret, hash_len;
3315
3316 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3317
Paul Bakker92be97b2013-01-02 17:30:03 +01003318 /*
3319 * Set the out_msg pointer to the correct location based on IV length
3320 */
3321 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3322 {
3323 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3324 ssl->transform_negotiate->fixed_ivlen;
3325 }
3326 else
3327 ssl->out_msg = ssl->out_iv;
3328
Paul Bakker48916f92012-09-16 19:57:18 +00003329 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003330
3331 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3333
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003334#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003335 ssl->verify_data_len = hash_len;
3336 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003337#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003338
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 ssl->out_msglen = 4 + hash_len;
3340 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3341 ssl->out_msg[0] = SSL_HS_FINISHED;
3342
3343 /*
3344 * In case of session resuming, invert the client and server
3345 * ChangeCipherSpec messages order.
3346 */
Paul Bakker0a597072012-09-25 21:55:46 +00003347 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003349#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003351 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003352#endif
3353#if defined(POLARSSL_SSL_SRV_C)
3354 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 }
3358 else
3359 ssl->state++;
3360
Paul Bakker48916f92012-09-16 19:57:18 +00003361 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003362 * Switch to our negotiated transform and session parameters for outbound
3363 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003364 */
3365 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3366 ssl->transform_out = ssl->transform_negotiate;
3367 ssl->session_out = ssl->session_negotiate;
3368 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
Paul Bakker07eb38b2012-12-19 14:42:06 +01003370#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003371 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003372 {
3373 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3374 {
3375 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3376 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3377 }
3378 }
3379#endif
3380
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3382 {
3383 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3384 return( ret );
3385 }
3386
3387 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3388
3389 return( 0 );
3390}
3391
3392int ssl_parse_finished( ssl_context *ssl )
3393{
Paul Bakker23986e52011-04-24 08:57:21 +00003394 int ret;
3395 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 unsigned char buf[36];
3397
3398 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3399
Paul Bakker48916f92012-09-16 19:57:18 +00003400 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
Paul Bakker48916f92012-09-16 19:57:18 +00003402 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003403 * Switch to our negotiated transform and session parameters for inbound
3404 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003405 */
3406 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3407 ssl->transform_in = ssl->transform_negotiate;
3408 ssl->session_in = ssl->session_negotiate;
3409 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003410
Paul Bakker92be97b2013-01-02 17:30:03 +01003411 /*
3412 * Set the in_msg pointer to the correct location based on IV length
3413 */
3414 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3415 {
3416 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3417 ssl->transform_negotiate->fixed_ivlen;
3418 }
3419 else
3420 ssl->in_msg = ssl->in_iv;
3421
Paul Bakker07eb38b2012-12-19 14:42:06 +01003422#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003423 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003424 {
3425 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3426 {
3427 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3428 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3429 }
3430 }
3431#endif
3432
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3434 {
3435 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3436 return( ret );
3437 }
3438
3439 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3440 {
3441 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003442 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 }
3444
Paul Bakker1ef83d62012-04-11 12:09:53 +00003445 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3447
3448 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3449 ssl->in_hslen != 4 + hash_len )
3450 {
3451 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003452 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 }
3454
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003455 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 {
3457 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003458 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003461#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003462 ssl->verify_data_len = hash_len;
3463 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003464#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003465
Paul Bakker0a597072012-09-25 21:55:46 +00003466 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003468#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 if( ssl->endpoint == SSL_IS_CLIENT )
3470 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003471#endif
3472#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003474 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 }
3477 else
3478 ssl->state++;
3479
3480 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3481
3482 return( 0 );
3483}
3484
Paul Bakker968afaa2014-07-09 11:09:24 +02003485static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003486{
3487 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3488
3489#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3490 defined(POLARSSL_SSL_PROTO_TLS1_1)
3491 md5_init( &handshake->fin_md5 );
3492 sha1_init( &handshake->fin_sha1 );
3493 md5_starts( &handshake->fin_md5 );
3494 sha1_starts( &handshake->fin_sha1 );
3495#endif
3496#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3497#if defined(POLARSSL_SHA256_C)
3498 sha256_init( &handshake->fin_sha256 );
3499 sha256_starts( &handshake->fin_sha256, 0 );
3500#endif
3501#if defined(POLARSSL_SHA512_C)
3502 sha512_init( &handshake->fin_sha512 );
3503 sha512_starts( &handshake->fin_sha512, 1 );
3504#endif
3505#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3506
3507 handshake->update_checksum = ssl_update_checksum_start;
3508 handshake->sig_alg = SSL_HASH_SHA1;
3509
3510#if defined(POLARSSL_DHM_C)
3511 dhm_init( &handshake->dhm_ctx );
3512#endif
3513#if defined(POLARSSL_ECDH_C)
3514 ecdh_init( &handshake->ecdh_ctx );
3515#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003516}
3517
3518static void ssl_transform_init( ssl_transform *transform )
3519{
3520 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003521
3522 cipher_init( &transform->cipher_ctx_enc );
3523 cipher_init( &transform->cipher_ctx_dec );
3524
3525 md_init( &transform->md_ctx_enc );
3526 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003527}
3528
3529void ssl_session_init( ssl_session *session )
3530{
3531 memset( session, 0, sizeof(ssl_session) );
3532}
3533
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003534static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003535{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003536 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003537 if( ssl->transform_negotiate )
3538 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003539 if( ssl->session_negotiate )
3540 ssl_session_free( ssl->session_negotiate );
3541 if( ssl->handshake )
3542 ssl_handshake_free( ssl->handshake );
3543
3544 /*
3545 * Either the pointers are now NULL or cleared properly and can be freed.
3546 * Now allocate missing structures.
3547 */
3548 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003549 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003550 ssl->transform_negotiate = polarssl_malloc( sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003551 }
Paul Bakker48916f92012-09-16 19:57:18 +00003552
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003553 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003554 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003555 ssl->session_negotiate = polarssl_malloc( sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003556 }
Paul Bakker48916f92012-09-16 19:57:18 +00003557
Paul Bakker82788fb2014-10-20 13:59:19 +02003558 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003559 {
Mansour Moufid99b92592015-02-15 17:46:32 -05003560 ssl->handshake = polarssl_malloc( sizeof(ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003561 }
Paul Bakker48916f92012-09-16 19:57:18 +00003562
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003563 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003564 if( ssl->handshake == NULL ||
3565 ssl->transform_negotiate == NULL ||
3566 ssl->session_negotiate == NULL )
3567 {
3568 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003569
3570 polarssl_free( ssl->handshake );
3571 polarssl_free( ssl->transform_negotiate );
3572 polarssl_free( ssl->session_negotiate );
3573
3574 ssl->handshake = NULL;
3575 ssl->transform_negotiate = NULL;
3576 ssl->session_negotiate = NULL;
3577
Paul Bakker48916f92012-09-16 19:57:18 +00003578 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3579 }
3580
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003581 /* Initialize structures */
3582 ssl_session_init( ssl->session_negotiate );
3583 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003584 ssl_handshake_params_init( ssl->handshake );
3585
3586#if defined(POLARSSL_X509_CRT_PARSE_C)
3587 ssl->handshake->key_cert = ssl->key_cert;
3588#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003589
Paul Bakker48916f92012-09-16 19:57:18 +00003590 return( 0 );
3591}
3592
Paul Bakker5121ce52009-01-03 21:22:43 +00003593/*
3594 * Initialize an SSL context
3595 */
3596int ssl_init( ssl_context *ssl )
3597{
Paul Bakker48916f92012-09-16 19:57:18 +00003598 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 int len = SSL_BUFFER_LEN;
3600
3601 memset( ssl, 0, sizeof( ssl_context ) );
3602
Paul Bakker62f2dee2012-09-28 07:31:51 +00003603 /*
3604 * Sane defaults
3605 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003606 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3607 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3608 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3609 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003610
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003611 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003612
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003613#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003614 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003615 memset( ssl->renego_period, 0xFF, 7 );
3616 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003617#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003618
Paul Bakker62f2dee2012-09-28 07:31:51 +00003619#if defined(POLARSSL_DHM_C)
3620 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3621 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3622 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3623 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3624 {
3625 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3626 return( ret );
3627 }
3628#endif
3629
3630 /*
3631 * Prepare base structures
3632 */
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003633 if( ( ssl->in_ctr = polarssl_malloc( len ) ) == NULL ||
3634 ( ssl->out_ctr = polarssl_malloc( len ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 {
3636 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003637 polarssl_free( ssl->in_ctr );
3638 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003639 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003640 }
3641
3642 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3643 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3644
Manuel Pégourié-Gonnardf7db5e02015-02-18 10:32:41 +00003645 ssl->in_hdr = ssl->in_ctr + 8;
3646 ssl->in_iv = ssl->in_ctr + 13;
3647 ssl->in_msg = ssl->in_ctr + 13;
3648
3649 ssl->out_hdr = ssl->out_ctr + 8;
3650 ssl->out_iv = ssl->out_ctr + 13;
3651 ssl->out_msg = ssl->out_ctr + 13;
3652
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003653#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3654 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3655#endif
3656
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003657#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3658 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3659#endif
3660
Paul Bakker606b4ba2013-08-14 16:52:14 +02003661#if defined(POLARSSL_SSL_SESSION_TICKETS)
3662 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3663#endif
3664
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003665#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003666 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003667#endif
3668
Paul Bakker48916f92012-09-16 19:57:18 +00003669 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3670 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
3672 return( 0 );
3673}
3674
3675/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003676 * Reset an initialized and used SSL context for re-use while retaining
3677 * all application-set variables, function pointers and data.
3678 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003679int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003680{
Paul Bakker48916f92012-09-16 19:57:18 +00003681 int ret;
3682
Paul Bakker7eb013f2011-10-06 12:37:39 +00003683 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003684
3685#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003686 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003687 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003688
3689 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003690 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3691 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003692#endif
3693 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003694
Paul Bakker7eb013f2011-10-06 12:37:39 +00003695 ssl->in_offt = NULL;
3696
Paul Bakker92be97b2013-01-02 17:30:03 +01003697 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003698 ssl->in_msgtype = 0;
3699 ssl->in_msglen = 0;
3700 ssl->in_left = 0;
3701
3702 ssl->in_hslen = 0;
3703 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003704 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705
Paul Bakker92be97b2013-01-02 17:30:03 +01003706 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003707 ssl->out_msgtype = 0;
3708 ssl->out_msglen = 0;
3709 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003710#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003711 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3712 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003713#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003714
Paul Bakker48916f92012-09-16 19:57:18 +00003715 ssl->transform_in = NULL;
3716 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003717
3718 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3719 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003720
3721#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003722 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003723 {
3724 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003725 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003726 {
3727 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3728 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3729 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003730 }
3731#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003732
Paul Bakker48916f92012-09-16 19:57:18 +00003733 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003734 {
Paul Bakker48916f92012-09-16 19:57:18 +00003735 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003736 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003737 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003738 }
Paul Bakker48916f92012-09-16 19:57:18 +00003739
Paul Bakkerc0463502013-02-14 11:19:38 +01003740 if( ssl->session )
3741 {
3742 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003743 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003744 ssl->session = NULL;
3745 }
3746
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003747#if defined(POLARSSL_SSL_ALPN)
3748 ssl->alpn_chosen = NULL;
3749#endif
3750
Paul Bakker48916f92012-09-16 19:57:18 +00003751 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3752 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003753
3754 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003755}
3756
Paul Bakkera503a632013-08-14 13:48:06 +02003757#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003758static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3759{
3760 aes_free( &tkeys->enc );
3761 aes_free( &tkeys->dec );
3762
3763 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3764}
3765
Paul Bakker7eb013f2011-10-06 12:37:39 +00003766/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003767 * Allocate and initialize ticket keys
3768 */
3769static int ssl_ticket_keys_init( ssl_context *ssl )
3770{
3771 int ret;
3772 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003773 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003774
3775 if( ssl->ticket_keys != NULL )
3776 return( 0 );
3777
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003778 tkeys = polarssl_malloc( sizeof(ssl_ticket_keys) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003779 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003780 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3781
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003782 aes_init( &tkeys->enc );
3783 aes_init( &tkeys->dec );
3784
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003785 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003786 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003787 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003788 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003789 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003790 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003791
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003792 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3793 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3794 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3795 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003796 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003797 polarssl_free( tkeys );
3798 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003799 }
3800
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003801 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003802 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003803 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003804 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003805 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003806 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003807
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003808 ssl->ticket_keys = tkeys;
3809
3810 return( 0 );
3811}
Paul Bakkera503a632013-08-14 13:48:06 +02003812#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003813
3814/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003815 * SSL set accessors
3816 */
3817void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3818{
3819 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003820
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003821#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3822 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003823 if( endpoint == SSL_IS_CLIENT )
3824 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003825#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003826
3827#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3828 if( endpoint == SSL_IS_SERVER )
3829 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3830#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003831}
3832
3833void ssl_set_authmode( ssl_context *ssl, int authmode )
3834{
3835 ssl->authmode = authmode;
3836}
3837
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003838#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003839void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003840 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003841 void *p_vrfy )
3842{
3843 ssl->f_vrfy = f_vrfy;
3844 ssl->p_vrfy = p_vrfy;
3845}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003846#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003847
Paul Bakker5121ce52009-01-03 21:22:43 +00003848void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003849 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 void *p_rng )
3851{
3852 ssl->f_rng = f_rng;
3853 ssl->p_rng = p_rng;
3854}
3855
3856void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003857 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 void *p_dbg )
3859{
3860 ssl->f_dbg = f_dbg;
3861 ssl->p_dbg = p_dbg;
3862}
3863
3864void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003865 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003866 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003867{
3868 ssl->f_recv = f_recv;
3869 ssl->f_send = f_send;
3870 ssl->p_recv = p_recv;
3871 ssl->p_send = p_send;
3872}
3873
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003874#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003875void ssl_set_session_cache( ssl_context *ssl,
3876 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3877 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003878{
Paul Bakker0a597072012-09-25 21:55:46 +00003879 ssl->f_get_cache = f_get_cache;
3880 ssl->p_get_cache = p_get_cache;
3881 ssl->f_set_cache = f_set_cache;
3882 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003883}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003884#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003885
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003886#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003887int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003888{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003889 int ret;
3890
3891 if( ssl == NULL ||
3892 session == NULL ||
3893 ssl->session_negotiate == NULL ||
3894 ssl->endpoint != SSL_IS_CLIENT )
3895 {
3896 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3897 }
3898
3899 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3900 return( ret );
3901
Paul Bakker0a597072012-09-25 21:55:46 +00003902 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003903
3904 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003905}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003906#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003907
Paul Bakkerb68cad62012-08-23 08:34:18 +00003908void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003909{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003910 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3911 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3912 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3913 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3914}
3915
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003916void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3917 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003918 int major, int minor )
3919{
3920 if( major != SSL_MAJOR_VERSION_3 )
3921 return;
3922
3923 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3924 return;
3925
3926 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003927}
3928
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003929#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003930/* Add a new (empty) key_cert entry an return a pointer to it */
3931static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3932{
3933 ssl_key_cert *key_cert, *last;
3934
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003935 key_cert = polarssl_malloc( sizeof(ssl_key_cert) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003936 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003937 return( NULL );
3938
3939 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3940
3941 /* Append the new key_cert to the (possibly empty) current list */
3942 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003943 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003944 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003945 if( ssl->handshake != NULL )
3946 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003947 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003948 else
3949 {
3950 last = ssl->key_cert;
3951 while( last->next != NULL )
3952 last = last->next;
3953 last->next = key_cert;
3954 }
3955
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003956 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003957}
3958
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003959void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003960 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003961{
3962 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003963 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003964 ssl->peer_cn = peer_cn;
3965}
3966
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003967int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003968 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003969{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003970 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3971
3972 if( key_cert == NULL )
3973 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3974
3975 key_cert->cert = own_cert;
3976 key_cert->key = pk_key;
3977
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00003978 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003979}
3980
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01003981#if ! defined(POLARSSL_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003982#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003983int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003984 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003985{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003986 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003987 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003988
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003989 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003990 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3991
Mansour Moufidc531b4a2015-02-15 17:35:38 -05003992 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003993 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003994 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003995
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003996 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003997
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003998 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003999 if( ret != 0 )
4000 return( ret );
4001
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004002 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004003 return( ret );
4004
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004005 key_cert->cert = own_cert;
4006 key_cert->key_own_alloc = 1;
4007
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004008 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004009}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004010#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004011
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004012int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004013 void *rsa_key,
4014 rsa_decrypt_func rsa_decrypt,
4015 rsa_sign_func rsa_sign,
4016 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004017{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004018 int ret;
4019 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004020
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004021 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004022 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4023
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004024 key_cert->key = polarssl_malloc( sizeof(pk_context) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004025 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004026 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004027
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004028 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004029
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004030 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4031 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4032 return( ret );
4033
4034 key_cert->cert = own_cert;
4035 key_cert->key_own_alloc = 1;
4036
Manuel Pégourié-Gonnardf427f882015-03-10 15:35:29 +00004037 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00004038}
Manuel Pégourié-Gonnardc70581c2015-03-23 13:58:27 +01004039#endif /* POLARSSL_DEPRECATED_REMOVED */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004040#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004041
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004042#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004043int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4044 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004045{
Paul Bakker6db455e2013-09-18 17:29:31 +02004046 if( psk == NULL || psk_identity == NULL )
4047 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4048
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004049 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4051
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004052 if( ssl->psk != NULL || ssl->psk_identity != NULL )
Paul Bakker6db455e2013-09-18 17:29:31 +02004053 {
4054 polarssl_free( ssl->psk );
4055 polarssl_free( ssl->psk_identity );
4056 }
4057
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004058 if( ( ssl->psk = polarssl_malloc( psk_len ) ) == NULL ||
4059 ( ssl->psk_identity = polarssl_malloc( psk_identity_len ) ) == NULL )
Mansour Moufidf81088b2015-02-17 13:10:21 -05004060 {
4061 polarssl_free( ssl->psk );
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004062 ssl->psk = NULL;
Mansour Moufidf81088b2015-02-17 13:10:21 -05004063 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4064 }
Paul Bakker6db455e2013-09-18 17:29:31 +02004065
Manuel Pégourié-Gonnardf45850c2015-02-18 10:23:52 +00004066 ssl->psk_len = psk_len;
4067 ssl->psk_identity_len = psk_identity_len;
4068
Paul Bakker6db455e2013-09-18 17:29:31 +02004069 memcpy( ssl->psk, psk, ssl->psk_len );
4070 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004071
4072 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004073}
4074
4075void ssl_set_psk_cb( ssl_context *ssl,
4076 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4077 size_t),
4078 void *p_psk )
4079{
4080 ssl->f_psk = f_psk;
4081 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004082}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004083#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004084
Paul Bakker48916f92012-09-16 19:57:18 +00004085#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004086int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004087{
4088 int ret;
4089
Paul Bakker48916f92012-09-16 19:57:18 +00004090 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091 {
4092 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4093 return( ret );
4094 }
4095
Paul Bakker48916f92012-09-16 19:57:18 +00004096 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004097 {
4098 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4099 return( ret );
4100 }
4101
4102 return( 0 );
4103}
4104
Paul Bakker1b57b062011-01-06 15:48:19 +00004105int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4106{
4107 int ret;
4108
Paul Bakker66d5d072014-06-17 16:39:18 +02004109 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004110 {
4111 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4112 return( ret );
4113 }
4114
Paul Bakker66d5d072014-06-17 16:39:18 +02004115 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004116 {
4117 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4118 return( ret );
4119 }
4120
4121 return( 0 );
4122}
Paul Bakker48916f92012-09-16 19:57:18 +00004123#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004124
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004125#if defined(POLARSSL_SSL_SET_CURVES)
4126/*
4127 * Set the allowed elliptic curves
4128 */
4129void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4130{
4131 ssl->curve_list = curve_list;
4132}
4133#endif
4134
Paul Bakker0be444a2013-08-27 21:55:01 +02004135#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004136int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004137{
4138 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004139 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004140
4141 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004142
4143 if( ssl->hostname_len + 1 == 0 )
4144 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4145
Mansour Moufidc531b4a2015-02-15 17:35:38 -05004146 ssl->hostname = polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004147
Paul Bakkerb15b8512012-01-13 13:44:06 +00004148 if( ssl->hostname == NULL )
4149 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4150
Paul Bakker3c2122f2013-06-24 19:03:14 +02004151 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004153
Paul Bakker40ea7de2009-05-03 10:18:48 +00004154 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004155
4156 return( 0 );
4157}
4158
Paul Bakker5701cdc2012-09-27 21:49:42 +00004159void ssl_set_sni( ssl_context *ssl,
4160 int (*f_sni)(void *, ssl_context *,
4161 const unsigned char *, size_t),
4162 void *p_sni )
4163{
4164 ssl->f_sni = f_sni;
4165 ssl->p_sni = p_sni;
4166}
Paul Bakker0be444a2013-08-27 21:55:01 +02004167#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004168
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004169#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004170int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004171{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004172 size_t cur_len, tot_len;
4173 const char **p;
4174
4175 /*
4176 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4177 * truncated". Check lengths now rather than later.
4178 */
4179 tot_len = 0;
4180 for( p = protos; *p != NULL; p++ )
4181 {
4182 cur_len = strlen( *p );
4183 tot_len += cur_len;
4184
4185 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4186 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4187 }
4188
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004189 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004190
4191 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004192}
4193
4194const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4195{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004196 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004197}
4198#endif /* POLARSSL_SSL_ALPN */
4199
Paul Bakker490ecc82011-10-06 13:04:09 +00004200void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4201{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004202 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4203 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4204 {
4205 ssl->max_major_ver = major;
4206 ssl->max_minor_ver = minor;
4207 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004208}
4209
Paul Bakker1d29fb52012-09-28 13:28:45 +00004210void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4211{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004212 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4213 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4214 {
4215 ssl->min_major_ver = major;
4216 ssl->min_minor_ver = minor;
4217 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004218}
4219
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004220#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4221void ssl_set_fallback( ssl_context *ssl, char fallback )
4222{
4223 ssl->fallback = fallback;
4224}
4225#endif
4226
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004227#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4228void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4229{
4230 ssl->encrypt_then_mac = etm;
4231}
4232#endif
4233
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004234#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4235void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4236{
4237 ssl->extended_ms = ems;
4238}
4239#endif
4240
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004241void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4242{
4243 ssl->arc4_disabled = arc4;
4244}
4245
Paul Bakker05decb22013-08-15 13:33:48 +02004246#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004247int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4248{
Paul Bakker77e257e2013-12-16 15:29:52 +01004249 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004250 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004251 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004252 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004253 }
4254
4255 ssl->mfl_code = mfl_code;
4256
4257 return( 0 );
4258}
Paul Bakker05decb22013-08-15 13:33:48 +02004259#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004260
Paul Bakker1f2bc622013-08-15 13:45:55 +02004261#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004262int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004263{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004264 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004265
4266 return( 0 );
4267}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004268#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004269
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004270#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4271void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4272{
4273 ssl->split_done = split;
4274}
4275#endif
4276
Paul Bakker48916f92012-09-16 19:57:18 +00004277void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4278{
4279 ssl->allow_legacy_renegotiation = allow_legacy;
4280}
4281
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004282#if defined(POLARSSL_SSL_RENEGOTIATION)
4283void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4284{
4285 ssl->disable_renegotiation = renegotiation;
4286}
4287
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004288void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4289{
4290 ssl->renego_max_records = max_records;
4291}
4292
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004293void ssl_set_renegotiation_period( ssl_context *ssl,
4294 const unsigned char period[8] )
4295{
4296 memcpy( ssl->renego_period, period, 8 );
4297}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004298#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Paul Bakkera503a632013-08-14 13:48:06 +02004300#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004301int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4302{
4303 ssl->session_tickets = use_tickets;
4304
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004305#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004306 if( ssl->endpoint == SSL_IS_CLIENT )
4307 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004308#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004309
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004310 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4311 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004312
4313 if( ssl->f_rng == NULL )
4314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4315
4316 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004317}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004318
4319void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4320{
4321 ssl->ticket_lifetime = lifetime;
4322}
Paul Bakkera503a632013-08-14 13:48:06 +02004323#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004324
Paul Bakker5121ce52009-01-03 21:22:43 +00004325/*
4326 * SSL get accessors
4327 */
4328size_t ssl_get_bytes_avail( const ssl_context *ssl )
4329{
4330 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4331}
4332
Paul Bakkerff60ee62010-03-16 21:09:09 +00004333int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004335 if( ssl->session != NULL )
4336 return( ssl->session->verify_result );
4337
4338 if( ssl->session_negotiate != NULL )
4339 return( ssl->session_negotiate->verify_result );
4340
4341 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004342}
4343
Paul Bakkere3166ce2011-01-27 17:40:50 +00004344const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004345{
Paul Bakker926c8e42013-03-06 10:23:34 +01004346 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004347 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004348
Paul Bakkere3166ce2011-01-27 17:40:50 +00004349 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004350}
4351
Paul Bakker43ca69c2011-01-15 17:35:19 +00004352const char *ssl_get_version( const ssl_context *ssl )
4353{
4354 switch( ssl->minor_ver )
4355 {
4356 case SSL_MINOR_VERSION_0:
4357 return( "SSLv3.0" );
4358
4359 case SSL_MINOR_VERSION_1:
4360 return( "TLSv1.0" );
4361
4362 case SSL_MINOR_VERSION_2:
4363 return( "TLSv1.1" );
4364
Paul Bakker1ef83d62012-04-11 12:09:53 +00004365 case SSL_MINOR_VERSION_3:
4366 return( "TLSv1.2" );
4367
Paul Bakker43ca69c2011-01-15 17:35:19 +00004368 default:
4369 break;
4370 }
4371 return( "unknown" );
4372}
4373
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004374#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004375const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004376{
4377 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004378 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004379
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004380 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004381}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004382#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004383
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004384#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004385int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4386{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004387 if( ssl == NULL ||
4388 dst == NULL ||
4389 ssl->session == NULL ||
4390 ssl->endpoint != SSL_IS_CLIENT )
4391 {
4392 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4393 }
4394
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004395 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004396}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004397#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004398
Paul Bakker5121ce52009-01-03 21:22:43 +00004399/*
Paul Bakker1961b702013-01-25 14:49:24 +01004400 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 */
Paul Bakker1961b702013-01-25 14:49:24 +01004402int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004403{
Paul Bakker40e46942009-01-03 21:51:57 +00004404 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004405
Paul Bakker40e46942009-01-03 21:51:57 +00004406#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004407 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004408 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004409#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004410#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004411 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004412 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004413#endif
4414
Paul Bakker1961b702013-01-25 14:49:24 +01004415 return( ret );
4416}
4417
4418/*
4419 * Perform the SSL handshake
4420 */
4421int ssl_handshake( ssl_context *ssl )
4422{
4423 int ret = 0;
4424
4425 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4426
4427 while( ssl->state != SSL_HANDSHAKE_OVER )
4428 {
4429 ret = ssl_handshake_step( ssl );
4430
4431 if( ret != 0 )
4432 break;
4433 }
4434
Paul Bakker5121ce52009-01-03 21:22:43 +00004435 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4436
4437 return( ret );
4438}
4439
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004440#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004441#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004442/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004443 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004444 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004445static int ssl_write_hello_request( ssl_context *ssl )
4446{
4447 int ret;
4448
4449 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4450
4451 ssl->out_msglen = 4;
4452 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4453 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4454
4455 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4456 {
4457 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4458 return( ret );
4459 }
4460
4461 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4462
4463 return( 0 );
4464}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004465#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004466
4467/*
4468 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004469 * - any side: calling ssl_renegotiate(),
4470 * - client: receiving a HelloRequest during ssl_read(),
4471 * - server: receiving any handshake message on server during ssl_read() after
4472 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004473 * If the handshake doesn't complete due to waiting for I/O, it will continue
4474 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004475 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004476static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004477{
4478 int ret;
4479
4480 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4481
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004482 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4483 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004484
4485 ssl->state = SSL_HELLO_REQUEST;
4486 ssl->renegotiation = SSL_RENEGOTIATION;
4487
Paul Bakker48916f92012-09-16 19:57:18 +00004488 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4489 {
4490 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4491 return( ret );
4492 }
4493
4494 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4495
4496 return( 0 );
4497}
4498
4499/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004500 * Renegotiate current connection on client,
4501 * or request renegotiation on server
4502 */
4503int ssl_renegotiate( ssl_context *ssl )
4504{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004505 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004506
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004507#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004508 /* On server, just send the request */
4509 if( ssl->endpoint == SSL_IS_SERVER )
4510 {
4511 if( ssl->state != SSL_HANDSHAKE_OVER )
4512 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4513
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004514 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4515
4516 /* Did we already try/start sending HelloRequest? */
4517 if( ssl->out_left != 0 )
4518 return( ssl_flush_output( ssl ) );
4519
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004520 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004521 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004522#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004523
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004524#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004525 /*
4526 * On client, either start the renegotiation process or,
4527 * if already in progress, continue the handshake
4528 */
4529 if( ssl->renegotiation != SSL_RENEGOTIATION )
4530 {
4531 if( ssl->state != SSL_HANDSHAKE_OVER )
4532 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4533
4534 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4535 {
4536 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4537 return( ret );
4538 }
4539 }
4540 else
4541 {
4542 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4543 {
4544 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4545 return( ret );
4546 }
4547 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004548#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004549
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004550 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004551}
4552
4553/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004554 * Check record counters and renegotiate if they're above the limit.
4555 */
4556static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4557{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004558 if( ssl->state != SSL_HANDSHAKE_OVER ||
4559 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4560 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4561 {
4562 return( 0 );
4563 }
4564
4565 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004566 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4567 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004568 {
4569 return( 0 );
4570 }
4571
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004572 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004573 return( ssl_renegotiate( ssl ) );
4574}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004575#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004576
4577/*
4578 * Receive application data decrypted from the SSL layer
4579 */
Paul Bakker23986e52011-04-24 08:57:21 +00004580int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004581{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004582 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004583 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004584
4585 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4586
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004587#if defined(POLARSSL_SSL_RENEGOTIATION)
4588 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4589 {
4590 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4591 return( ret );
4592 }
4593#endif
4594
Paul Bakker5121ce52009-01-03 21:22:43 +00004595 if( ssl->state != SSL_HANDSHAKE_OVER )
4596 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004597 ret = ssl_handshake( ssl );
4598 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4599 {
4600 record_read = 1;
4601 }
4602 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004603 {
4604 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4605 return( ret );
4606 }
4607 }
4608
4609 if( ssl->in_offt == NULL )
4610 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004611 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004612 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004613 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4614 {
4615 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4616 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004617
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004618 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4619 return( ret );
4620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004621 }
4622
4623 if( ssl->in_msglen == 0 &&
4624 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4625 {
4626 /*
4627 * OpenSSL sends empty messages to randomize the IV
4628 */
4629 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4630 {
Paul Bakker831a7552011-05-18 13:32:51 +00004631 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4632 return( 0 );
4633
Paul Bakker5121ce52009-01-03 21:22:43 +00004634 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4635 return( ret );
4636 }
4637 }
4638
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004639#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004640 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4641 {
4642 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4643
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004644#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004645 if( ssl->endpoint == SSL_IS_CLIENT &&
4646 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4647 ssl->in_hslen != 4 ) )
4648 {
4649 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4650 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4651 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004652#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004653
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004654 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4655 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004656 ssl->allow_legacy_renegotiation ==
4657 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004658 {
4659 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4660
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004661#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004662 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004663 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004664 /*
4665 * SSLv3 does not have a "no_renegotiation" alert
4666 */
4667 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4668 return( ret );
4669 }
4670 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004671#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004672#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4673 defined(POLARSSL_SSL_PROTO_TLS1_2)
4674 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004675 {
4676 if( ( ret = ssl_send_alert_message( ssl,
4677 SSL_ALERT_LEVEL_WARNING,
4678 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4679 {
4680 return( ret );
4681 }
Paul Bakker48916f92012-09-16 19:57:18 +00004682 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004683 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004684#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4685 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004686 {
4687 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004688 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004689 }
Paul Bakker48916f92012-09-16 19:57:18 +00004690 }
4691 else
4692 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004693 ret = ssl_start_renegotiation( ssl );
4694 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4695 {
4696 record_read = 1;
4697 }
4698 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004699 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004700 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004701 return( ret );
4702 }
Paul Bakker48916f92012-09-16 19:57:18 +00004703 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004704
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004705 /* If a non-handshake record was read during renego, fallthrough,
4706 * else tell the user they should call ssl_read() again */
4707 if( ! record_read )
4708 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004709 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004710 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4711 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004712 ssl->renego_records_seen++;
4713
4714 if( ssl->renego_max_records >= 0 &&
4715 ssl->renego_records_seen > ssl->renego_max_records )
4716 {
4717 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4718 "but not honored by client" ) );
4719 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4720 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004721 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004722#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004723
4724 /* Fatal and closure alerts handled by ssl_read_record() */
4725 if( ssl->in_msgtype == SSL_MSG_ALERT )
4726 {
4727 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4728 return( POLARSSL_ERR_NET_WANT_READ );
4729 }
4730
4731 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004732 {
4733 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004734 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004735 }
4736
4737 ssl->in_offt = ssl->in_msg;
4738 }
4739
4740 n = ( len < ssl->in_msglen )
4741 ? len : ssl->in_msglen;
4742
4743 memcpy( buf, ssl->in_offt, n );
4744 ssl->in_msglen -= n;
4745
4746 if( ssl->in_msglen == 0 )
4747 /* all bytes consumed */
4748 ssl->in_offt = NULL;
4749 else
4750 /* more data available */
4751 ssl->in_offt += n;
4752
4753 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4754
Paul Bakker23986e52011-04-24 08:57:21 +00004755 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004756}
4757
4758/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004759 * Send application data to be encrypted by the SSL layer,
4760 * taking care of max fragment length and buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 */
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004762static int ssl_write_real( ssl_context *ssl,
4763 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004764{
Paul Bakker23986e52011-04-24 08:57:21 +00004765 int ret;
4766 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004767 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004768
Paul Bakker05decb22013-08-15 13:33:48 +02004769#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004770 /*
4771 * Assume mfl_code is correct since it was checked when set
4772 */
4773 max_len = mfl_code_to_length[ssl->mfl_code];
4774
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004775 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004776 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004777 */
4778 if( ssl->session_out != NULL &&
4779 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4780 {
4781 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4782 }
Paul Bakker05decb22013-08-15 13:33:48 +02004783#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004784
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004785 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004786
Paul Bakker5121ce52009-01-03 21:22:43 +00004787 if( ssl->out_left != 0 )
4788 {
4789 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4790 {
4791 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4792 return( ret );
4793 }
4794 }
Paul Bakker887bd502011-06-08 13:10:54 +00004795 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004796 {
Paul Bakker887bd502011-06-08 13:10:54 +00004797 ssl->out_msglen = n;
4798 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4799 memcpy( ssl->out_msg, buf, n );
4800
4801 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4802 {
4803 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4804 return( ret );
4805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004806 }
4807
Paul Bakker23986e52011-04-24 08:57:21 +00004808 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004809}
4810
4811/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004812 * Write application data, doing 1/n-1 splitting if necessary.
4813 *
4814 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004815 * then the caller will call us again with the same arguments, so
4816 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004817 */
4818#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004819static int ssl_write_split( ssl_context *ssl,
4820 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004821{
4822 int ret;
4823
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004824 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4825 len <= 1 ||
4826 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004827 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4828 != POLARSSL_MODE_CBC )
4829 {
4830 return( ssl_write_real( ssl, buf, len ) );
4831 }
4832
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004833 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004834 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004835 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004836 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004837 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004838 }
4839
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004840 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4841 return( ret );
4842 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004843
4844 return( ret + 1 );
4845}
4846#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4847
4848/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02004849 * Write application data (public-facing wrapper)
4850 */
4851int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4852{
4853 int ret;
4854
4855 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4856
4857#if defined(POLARSSL_SSL_RENEGOTIATION)
4858 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4859 {
4860 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4861 return( ret );
4862 }
4863#endif
4864
4865 if( ssl->state != SSL_HANDSHAKE_OVER )
4866 {
4867 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4868 {
4869 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4870 return( ret );
4871 }
4872 }
4873
4874#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4875 ret = ssl_write_split( ssl, buf, len );
4876#else
4877 ret = ssl_write_real( ssl, buf, len );
4878#endif
4879
4880 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4881
4882 return( ret );
4883}
4884
4885/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004886 * Notify the peer that the connection is being closed
4887 */
4888int ssl_close_notify( ssl_context *ssl )
4889{
4890 int ret;
4891
4892 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4893
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004894 if( ssl->out_left != 0 )
4895 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004896
4897 if( ssl->state == SSL_HANDSHAKE_OVER )
4898 {
Paul Bakker48916f92012-09-16 19:57:18 +00004899 if( ( ret = ssl_send_alert_message( ssl,
4900 SSL_ALERT_LEVEL_WARNING,
4901 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004902 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004903 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 return( ret );
4905 }
4906 }
4907
4908 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4909
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004910 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004911}
4912
Paul Bakker48916f92012-09-16 19:57:18 +00004913void ssl_transform_free( ssl_transform *transform )
4914{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004915 if( transform == NULL )
4916 return;
4917
Paul Bakker48916f92012-09-16 19:57:18 +00004918#if defined(POLARSSL_ZLIB_SUPPORT)
4919 deflateEnd( &transform->ctx_deflate );
4920 inflateEnd( &transform->ctx_inflate );
4921#endif
4922
Paul Bakker84bbeb52014-07-01 14:53:22 +02004923 cipher_free( &transform->cipher_ctx_enc );
4924 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004925
Paul Bakker84bbeb52014-07-01 14:53:22 +02004926 md_free( &transform->md_ctx_enc );
4927 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004928
Paul Bakker34617722014-06-13 17:20:13 +02004929 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004930}
4931
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004932#if defined(POLARSSL_X509_CRT_PARSE_C)
4933static void ssl_key_cert_free( ssl_key_cert *key_cert )
4934{
4935 ssl_key_cert *cur = key_cert, *next;
4936
4937 while( cur != NULL )
4938 {
4939 next = cur->next;
4940
4941 if( cur->key_own_alloc )
4942 {
4943 pk_free( cur->key );
4944 polarssl_free( cur->key );
4945 }
4946 polarssl_free( cur );
4947
4948 cur = next;
4949 }
4950}
4951#endif /* POLARSSL_X509_CRT_PARSE_C */
4952
Paul Bakker48916f92012-09-16 19:57:18 +00004953void ssl_handshake_free( ssl_handshake_params *handshake )
4954{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004955 if( handshake == NULL )
4956 return;
4957
Paul Bakker48916f92012-09-16 19:57:18 +00004958#if defined(POLARSSL_DHM_C)
4959 dhm_free( &handshake->dhm_ctx );
4960#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004961#if defined(POLARSSL_ECDH_C)
4962 ecdh_free( &handshake->ecdh_ctx );
4963#endif
4964
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004965#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004966 /* explicit void pointer cast for buggy MS compiler */
4967 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004968#endif
4969
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004970#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4971 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4972 /*
4973 * Free only the linked list wrapper, not the keys themselves
4974 * since the belong to the SNI callback
4975 */
4976 if( handshake->sni_key_cert != NULL )
4977 {
4978 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4979
4980 while( cur != NULL )
4981 {
4982 next = cur->next;
4983 polarssl_free( cur );
4984 cur = next;
4985 }
4986 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004987#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004988
Paul Bakker34617722014-06-13 17:20:13 +02004989 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004990}
4991
4992void ssl_session_free( ssl_session *session )
4993{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004994 if( session == NULL )
4995 return;
4996
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004997#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004998 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004999 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005000 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02005001 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00005002 }
Paul Bakkered27a042013-04-18 22:46:23 +02005003#endif
Paul Bakker0a597072012-09-25 21:55:46 +00005004
Paul Bakkera503a632013-08-14 13:48:06 +02005005#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005006 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02005007#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02005008
Paul Bakker34617722014-06-13 17:20:13 +02005009 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005010}
5011
Paul Bakker5121ce52009-01-03 21:22:43 +00005012/*
5013 * Free an SSL context
5014 */
5015void ssl_free( ssl_context *ssl )
5016{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005017 if( ssl == NULL )
5018 return;
5019
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5021
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 if( ssl->out_ctr != NULL )
5023 {
Paul Bakker34617722014-06-13 17:20:13 +02005024 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005025 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005026 }
5027
5028 if( ssl->in_ctr != NULL )
5029 {
Paul Bakker34617722014-06-13 17:20:13 +02005030 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005031 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005032 }
5033
Paul Bakker16770332013-10-11 09:59:44 +02005034#if defined(POLARSSL_ZLIB_SUPPORT)
5035 if( ssl->compress_buf != NULL )
5036 {
Paul Bakker34617722014-06-13 17:20:13 +02005037 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005038 polarssl_free( ssl->compress_buf );
5039 }
5040#endif
5041
Paul Bakker40e46942009-01-03 21:51:57 +00005042#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005043 mpi_free( &ssl->dhm_P );
5044 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005045#endif
5046
Paul Bakker48916f92012-09-16 19:57:18 +00005047 if( ssl->transform )
5048 {
5049 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005050 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005051 }
5052
5053 if( ssl->handshake )
5054 {
5055 ssl_handshake_free( ssl->handshake );
5056 ssl_transform_free( ssl->transform_negotiate );
5057 ssl_session_free( ssl->session_negotiate );
5058
Paul Bakker6e339b52013-07-03 13:37:05 +02005059 polarssl_free( ssl->handshake );
5060 polarssl_free( ssl->transform_negotiate );
5061 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005062 }
5063
Paul Bakkerc0463502013-02-14 11:19:38 +01005064 if( ssl->session )
5065 {
5066 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005067 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005068 }
5069
Paul Bakkera503a632013-08-14 13:48:06 +02005070#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005071 if( ssl->ticket_keys )
5072 {
5073 ssl_ticket_keys_free( ssl->ticket_keys );
5074 polarssl_free( ssl->ticket_keys );
5075 }
Paul Bakkera503a632013-08-14 13:48:06 +02005076#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005077
Paul Bakker0be444a2013-08-27 21:55:01 +02005078#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005079 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005080 {
Paul Bakker34617722014-06-13 17:20:13 +02005081 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005082 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005083 ssl->hostname_len = 0;
5084 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005085#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005086
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005087#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005088 if( ssl->psk != NULL )
5089 {
Paul Bakker34617722014-06-13 17:20:13 +02005090 polarssl_zeroize( ssl->psk, ssl->psk_len );
5091 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005092 polarssl_free( ssl->psk );
5093 polarssl_free( ssl->psk_identity );
5094 ssl->psk_len = 0;
5095 ssl->psk_identity_len = 0;
5096 }
5097#endif
5098
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005099#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005100 ssl_key_cert_free( ssl->key_cert );
5101#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005102
Paul Bakker05ef8352012-05-08 09:17:57 +00005103#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5104 if( ssl_hw_record_finish != NULL )
5105 {
5106 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5107 ssl_hw_record_finish( ssl );
5108 }
5109#endif
5110
Paul Bakker5121ce52009-01-03 21:22:43 +00005111 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005112
Paul Bakker86f04f42013-02-14 11:20:09 +01005113 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005114 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005115}
5116
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005117#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005118/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005119 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005120 */
5121unsigned char ssl_sig_from_pk( pk_context *pk )
5122{
5123#if defined(POLARSSL_RSA_C)
5124 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5125 return( SSL_SIG_RSA );
5126#endif
5127#if defined(POLARSSL_ECDSA_C)
5128 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5129 return( SSL_SIG_ECDSA );
5130#endif
5131 return( SSL_SIG_ANON );
5132}
5133
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005134pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5135{
5136 switch( sig )
5137 {
5138#if defined(POLARSSL_RSA_C)
5139 case SSL_SIG_RSA:
5140 return( POLARSSL_PK_RSA );
5141#endif
5142#if defined(POLARSSL_ECDSA_C)
5143 case SSL_SIG_ECDSA:
5144 return( POLARSSL_PK_ECDSA );
5145#endif
5146 default:
5147 return( POLARSSL_PK_NONE );
5148 }
5149}
Paul Bakker9af723c2014-05-01 13:03:14 +02005150#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005151
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005152/*
5153 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5154 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005155md_type_t ssl_md_alg_from_hash( unsigned char hash )
5156{
5157 switch( hash )
5158 {
5159#if defined(POLARSSL_MD5_C)
5160 case SSL_HASH_MD5:
5161 return( POLARSSL_MD_MD5 );
5162#endif
5163#if defined(POLARSSL_SHA1_C)
5164 case SSL_HASH_SHA1:
5165 return( POLARSSL_MD_SHA1 );
5166#endif
5167#if defined(POLARSSL_SHA256_C)
5168 case SSL_HASH_SHA224:
5169 return( POLARSSL_MD_SHA224 );
5170 case SSL_HASH_SHA256:
5171 return( POLARSSL_MD_SHA256 );
5172#endif
5173#if defined(POLARSSL_SHA512_C)
5174 case SSL_HASH_SHA384:
5175 return( POLARSSL_MD_SHA384 );
5176 case SSL_HASH_SHA512:
5177 return( POLARSSL_MD_SHA512 );
5178#endif
5179 default:
5180 return( POLARSSL_MD_NONE );
5181 }
5182}
5183
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005184#if defined(POLARSSL_SSL_SET_CURVES)
5185/*
5186 * Check is a curve proposed by the peer is in our list.
5187 * Return 1 if we're willing to use it, 0 otherwise.
5188 */
5189int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5190{
5191 const ecp_group_id *gid;
5192
5193 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5194 if( *gid == grp_id )
5195 return( 1 );
5196
5197 return( 0 );
5198}
Paul Bakker9af723c2014-05-01 13:03:14 +02005199#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005200
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005201#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005202int ssl_check_cert_usage( const x509_crt *cert,
5203 const ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02005204 int cert_endpoint,
5205 int *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005206{
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02005207 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005208#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5209 int usage = 0;
5210#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005211#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5212 const char *ext_oid;
5213 size_t ext_len;
5214#endif
5215
5216#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5217 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5218 ((void) cert);
5219 ((void) cert_endpoint);
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02005220 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005221#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005222
5223#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5224 if( cert_endpoint == SSL_IS_SERVER )
5225 {
5226 /* Server part of the key exchange */
5227 switch( ciphersuite->key_exchange )
5228 {
5229 case POLARSSL_KEY_EXCHANGE_RSA:
5230 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5231 usage = KU_KEY_ENCIPHERMENT;
5232 break;
5233
5234 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5235 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5236 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5237 usage = KU_DIGITAL_SIGNATURE;
5238 break;
5239
5240 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5241 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5242 usage = KU_KEY_AGREEMENT;
5243 break;
5244
5245 /* Don't use default: we want warnings when adding new values */
5246 case POLARSSL_KEY_EXCHANGE_NONE:
5247 case POLARSSL_KEY_EXCHANGE_PSK:
5248 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5249 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5250 usage = 0;
5251 }
5252 }
5253 else
5254 {
5255 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5256 usage = KU_DIGITAL_SIGNATURE;
5257 }
5258
5259 if( x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02005260 {
5261 *flags |= BADCERT_KEY_USAGE;
5262 ret = -1;
5263 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005264#else
5265 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005266#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5267
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005268#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5269 if( cert_endpoint == SSL_IS_SERVER )
5270 {
5271 ext_oid = OID_SERVER_AUTH;
5272 ext_len = OID_SIZE( OID_SERVER_AUTH );
5273 }
5274 else
5275 {
5276 ext_oid = OID_CLIENT_AUTH;
5277 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5278 }
5279
5280 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02005281 {
5282 *flags |= BADCERT_EXT_KEY_USAGE;
5283 ret = -1;
5284 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005285#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5286
Manuel Pégourié-Gonnard9f982512015-04-17 16:55:53 +02005287 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005288}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005289#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005290
5291#endif /* POLARSSL_SSL_TLS_C */