blob: 961f4dcf09ad7d78bc653262edd25807ea40f763 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020096 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
97 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 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200115 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
116 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é-Gonnard0bcc4e12014-06-17 10:54:17 +02001493 if( ssl->in_msglen < explicit_iv_len + taglen )
1494 {
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 );
1828 for( j = 0; j < extra_run; j++ )
1829 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001830
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001831 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1832 }
1833 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001834#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001835 POLARSSL_SSL_PROTO_TLS1_2 */
1836 {
1837 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001838 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1842 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1843 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001845 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001846 ssl->transform_in->maclen ) != 0 )
1847 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001848#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001849 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001850#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001851 correct = 0;
1852 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001853 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001855 /*
1856 * Finally check the correct flag
1857 */
1858 if( correct == 0 )
1859 return( POLARSSL_ERR_SSL_INVALID_MAC );
1860 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001861#endif /* POLARSSL_SOME_MODES_USE_MAC */
1862
1863 /* Make extra sure authentication was performed, exactly once */
1864 if( auth_done != 1 )
1865 {
1866 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1867 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
1870 if( ssl->in_msglen == 0 )
1871 {
1872 ssl->nb_zero++;
1873
1874 /*
1875 * Three or more empty messages may be a DoS attack
1876 * (excessive CPU consumption).
1877 */
1878 if( ssl->nb_zero > 3 )
1879 {
1880 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1881 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001882 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 }
1884 }
1885 else
1886 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001887
Paul Bakker23986e52011-04-24 08:57:21 +00001888 for( i = 8; i > 0; i-- )
1889 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 break;
1891
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001892 /* The loops goes to its end iff the counter is wrapping */
1893 if( i == 0 )
1894 {
1895 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1896 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1897 }
1898
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1900
1901 return( 0 );
1902}
1903
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001904#undef MAC_NONE
1905#undef MAC_PLAINTEXT
1906#undef MAC_CIPHERTEXT
1907
Paul Bakker2770fbd2012-07-03 13:30:23 +00001908#if defined(POLARSSL_ZLIB_SUPPORT)
1909/*
1910 * Compression/decompression functions
1911 */
1912static int ssl_compress_buf( ssl_context *ssl )
1913{
1914 int ret;
1915 unsigned char *msg_post = ssl->out_msg;
1916 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001917 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001918
1919 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1920
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001921 if( len_pre == 0 )
1922 return( 0 );
1923
Paul Bakker2770fbd2012-07-03 13:30:23 +00001924 memcpy( msg_pre, ssl->out_msg, len_pre );
1925
1926 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1927 ssl->out_msglen ) );
1928
1929 SSL_DEBUG_BUF( 4, "before compression: output payload",
1930 ssl->out_msg, ssl->out_msglen );
1931
Paul Bakker48916f92012-09-16 19:57:18 +00001932 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1933 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1934 ssl->transform_out->ctx_deflate.next_out = msg_post;
1935 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001936
Paul Bakker48916f92012-09-16 19:57:18 +00001937 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001938 if( ret != Z_OK )
1939 {
1940 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1941 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1942 }
1943
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001944 ssl->out_msglen = SSL_BUFFER_LEN -
1945 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001946
Paul Bakker2770fbd2012-07-03 13:30:23 +00001947 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1948 ssl->out_msglen ) );
1949
1950 SSL_DEBUG_BUF( 4, "after compression: output payload",
1951 ssl->out_msg, ssl->out_msglen );
1952
1953 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1954
1955 return( 0 );
1956}
1957
1958static int ssl_decompress_buf( ssl_context *ssl )
1959{
1960 int ret;
1961 unsigned char *msg_post = ssl->in_msg;
1962 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001963 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001964
1965 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1966
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001967 if( len_pre == 0 )
1968 return( 0 );
1969
Paul Bakker2770fbd2012-07-03 13:30:23 +00001970 memcpy( msg_pre, ssl->in_msg, len_pre );
1971
1972 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1973 ssl->in_msglen ) );
1974
1975 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1976 ssl->in_msg, ssl->in_msglen );
1977
Paul Bakker48916f92012-09-16 19:57:18 +00001978 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1979 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1980 ssl->transform_in->ctx_inflate.next_out = msg_post;
1981 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001982
Paul Bakker48916f92012-09-16 19:57:18 +00001983 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001984 if( ret != Z_OK )
1985 {
1986 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1987 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1988 }
1989
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001990 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1991 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001992
Paul Bakker2770fbd2012-07-03 13:30:23 +00001993 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1994 ssl->in_msglen ) );
1995
1996 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1997 ssl->in_msg, ssl->in_msglen );
1998
1999 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2000
2001 return( 0 );
2002}
2003#endif /* POLARSSL_ZLIB_SUPPORT */
2004
Paul Bakker5121ce52009-01-03 21:22:43 +00002005/*
2006 * Fill the input message buffer
2007 */
Paul Bakker23986e52011-04-24 08:57:21 +00002008int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002009{
Paul Bakker23986e52011-04-24 08:57:21 +00002010 int ret;
2011 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002012
2013 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2014
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002015 if( nb_want > SSL_BUFFER_LEN - 8 )
2016 {
2017 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2018 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2019 }
2020
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 while( ssl->in_left < nb_want )
2022 {
2023 len = nb_want - ssl->in_left;
2024 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2025
2026 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2027 ssl->in_left, nb_want ) );
2028 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2029
Paul Bakker831a7552011-05-18 13:32:51 +00002030 if( ret == 0 )
2031 return( POLARSSL_ERR_SSL_CONN_EOF );
2032
Paul Bakker5121ce52009-01-03 21:22:43 +00002033 if( ret < 0 )
2034 return( ret );
2035
2036 ssl->in_left += ret;
2037 }
2038
2039 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2040
2041 return( 0 );
2042}
2043
2044/*
2045 * Flush any data not yet written
2046 */
2047int ssl_flush_output( ssl_context *ssl )
2048{
2049 int ret;
2050 unsigned char *buf;
2051
2052 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2053
2054 while( ssl->out_left > 0 )
2055 {
2056 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2057 5 + ssl->out_msglen, ssl->out_left ) );
2058
Paul Bakker5bd42292012-12-19 14:40:42 +01002059 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002061
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2063
2064 if( ret <= 0 )
2065 return( ret );
2066
2067 ssl->out_left -= ret;
2068 }
2069
2070 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2071
2072 return( 0 );
2073}
2074
2075/*
2076 * Record layer functions
2077 */
2078int ssl_write_record( ssl_context *ssl )
2079{
Paul Bakker05ef8352012-05-08 09:17:57 +00002080 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002081 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
2083 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2084
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2086 {
2087 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2088 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2089 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2090
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002091 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2092 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094
Paul Bakker2770fbd2012-07-03 13:30:23 +00002095#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002096 if( ssl->transform_out != NULL &&
2097 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002098 {
2099 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2100 {
2101 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2102 return( ret );
2103 }
2104
2105 len = ssl->out_msglen;
2106 }
2107#endif /*POLARSSL_ZLIB_SUPPORT */
2108
Paul Bakker05ef8352012-05-08 09:17:57 +00002109#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002110 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002112 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113
Paul Bakker05ef8352012-05-08 09:17:57 +00002114 ret = ssl_hw_record_write( ssl );
2115 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2116 {
2117 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002118 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002119 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002120
2121 if( ret == 0 )
2122 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002123 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002124#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002125 if( !done )
2126 {
2127 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2128 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2129 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002130 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2131 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002132
Paul Bakker48916f92012-09-16 19:57:18 +00002133 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002134 {
2135 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2136 {
2137 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2138 return( ret );
2139 }
2140
2141 len = ssl->out_msglen;
2142 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2143 ssl->out_hdr[4] = (unsigned char)( len );
2144 }
2145
2146 ssl->out_left = 5 + ssl->out_msglen;
2147
2148 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2149 "version = [%d:%d], msglen = %d",
2150 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2151 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2152
2153 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002154 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002155 }
2156
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2158 {
2159 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2160 return( ret );
2161 }
2162
2163 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2164
2165 return( 0 );
2166}
2167
2168int ssl_read_record( ssl_context *ssl )
2169{
Paul Bakker05ef8352012-05-08 09:17:57 +00002170 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
2172 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2173
2174 if( ssl->in_hslen != 0 &&
2175 ssl->in_hslen < ssl->in_msglen )
2176 {
2177 /*
2178 * Get next Handshake message in the current record
2179 */
2180 ssl->in_msglen -= ssl->in_hslen;
2181
Paul Bakker8934a982011-08-05 11:11:53 +00002182 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002183 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
2185 ssl->in_hslen = 4;
2186 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2187
2188 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2189 " %d, type = %d, hslen = %d",
2190 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2191
2192 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002195 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
2197
2198 if( ssl->in_msglen < ssl->in_hslen )
2199 {
2200 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002201 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 }
2203
Paul Bakker4224bc02014-04-08 14:36:50 +02002204 if( ssl->state != SSL_HANDSHAKE_OVER )
2205 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002206
2207 return( 0 );
2208 }
2209
2210 ssl->in_hslen = 0;
2211
2212 /*
2213 * Read the record header and validate it
2214 */
2215 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2216 {
2217 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2218 return( ret );
2219 }
2220
2221 ssl->in_msgtype = ssl->in_hdr[0];
2222 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2223
2224 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2225 "version = [%d:%d], msglen = %d",
2226 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2227 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2228
2229 if( ssl->in_hdr[1] != ssl->major_ver )
2230 {
2231 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002232 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 }
2234
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002235 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 {
2237 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002238 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002239 }
2240
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002241 /* Sanity check (outer boundaries) */
2242 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2243 {
2244 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2245 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2246 }
2247
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002249 * Make sure the message length is acceptable for the current transform
2250 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 */
Paul Bakker48916f92012-09-16 19:57:18 +00002252 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002254 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
2256 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002257 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
2259 }
2260 else
2261 {
Paul Bakker48916f92012-09-16 19:57:18 +00002262 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002263 {
2264 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002265 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266 }
2267
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002268#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002269 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002270 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 {
2272 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002273 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002275#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002276
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002277#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2278 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002279 /*
2280 * TLS encrypted messages can have up to 256 bytes of padding
2281 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002282 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002283 ssl->in_msglen > ssl->transform_in->minlen +
2284 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002285 {
2286 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002287 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002289#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
2291
2292 /*
2293 * Read and optionally decrypt the message contents
2294 */
2295 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2296 {
2297 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2298 return( ret );
2299 }
2300
2301 SSL_DEBUG_BUF( 4, "input record from network",
2302 ssl->in_hdr, 5 + ssl->in_msglen );
2303
Paul Bakker05ef8352012-05-08 09:17:57 +00002304#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002305 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002306 {
2307 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2308
2309 ret = ssl_hw_record_read( ssl );
2310 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2311 {
2312 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002313 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002314 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002315
2316 if( ret == 0 )
2317 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002318 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002319#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002320 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 {
2322 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2323 {
Paul Bakker40865c82013-01-31 17:13:13 +01002324#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2325 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2326 {
2327 ssl_send_alert_message( ssl,
2328 SSL_ALERT_LEVEL_FATAL,
2329 SSL_ALERT_MSG_BAD_RECORD_MAC );
2330 }
2331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2333 return( ret );
2334 }
2335
2336 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2337 ssl->in_msg, ssl->in_msglen );
2338
2339 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2340 {
2341 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002342 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002343 }
2344 }
2345
Paul Bakker2770fbd2012-07-03 13:30:23 +00002346#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002347 if( ssl->transform_in != NULL &&
2348 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002349 {
2350 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2351 {
2352 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2353 return( ret );
2354 }
2355
2356 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2357 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2358 }
2359#endif /* POLARSSL_ZLIB_SUPPORT */
2360
Paul Bakker0a925182012-04-16 06:46:41 +00002361 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2362 ssl->in_msgtype != SSL_MSG_ALERT &&
2363 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2364 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2365 {
2366 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2367
Paul Bakker48916f92012-09-16 19:57:18 +00002368 if( ( ret = ssl_send_alert_message( ssl,
2369 SSL_ALERT_LEVEL_FATAL,
2370 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002371 {
2372 return( ret );
2373 }
2374
2375 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2376 }
2377
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2379 {
2380 ssl->in_hslen = 4;
2381 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2382
2383 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2384 " %d, type = %d, hslen = %d",
2385 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2386
2387 /*
2388 * Additional checks to validate the handshake header
2389 */
2390 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2391 {
2392 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002393 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 }
2395
2396 if( ssl->in_msglen < ssl->in_hslen )
2397 {
2398 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002399 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 }
2401
Paul Bakker48916f92012-09-16 19:57:18 +00002402 if( ssl->state != SSL_HANDSHAKE_OVER )
2403 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
2406 if( ssl->in_msgtype == SSL_MSG_ALERT )
2407 {
2408 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2409 ssl->in_msg[0], ssl->in_msg[1] ) );
2410
2411 /*
2412 * Ignore non-fatal alerts, except close_notify
2413 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002414 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002416 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2417 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 }
2420
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002421 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2422 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 {
2424 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002425 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 }
2427 }
2428
2429 ssl->in_left = 0;
2430
2431 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2432
2433 return( 0 );
2434}
2435
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002436int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2437{
2438 int ret;
2439
2440 if( ( ret = ssl_send_alert_message( ssl,
2441 SSL_ALERT_LEVEL_FATAL,
2442 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2443 {
2444 return( ret );
2445 }
2446
2447 return( 0 );
2448}
2449
Paul Bakker0a925182012-04-16 06:46:41 +00002450int ssl_send_alert_message( ssl_context *ssl,
2451 unsigned char level,
2452 unsigned char message )
2453{
2454 int ret;
2455
2456 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2457
2458 ssl->out_msgtype = SSL_MSG_ALERT;
2459 ssl->out_msglen = 2;
2460 ssl->out_msg[0] = level;
2461 ssl->out_msg[1] = message;
2462
2463 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2464 {
2465 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2466 return( ret );
2467 }
2468
2469 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2470
2471 return( 0 );
2472}
2473
Paul Bakker5121ce52009-01-03 21:22:43 +00002474/*
2475 * Handshake functions
2476 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002477#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2478 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2479 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2481 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2483 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484int ssl_write_certificate( ssl_context *ssl )
2485{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002486 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2489
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002493 {
2494 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2495 ssl->state++;
2496 return( 0 );
2497 }
2498
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002499 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2500 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002501}
2502
2503int ssl_parse_certificate( ssl_context *ssl )
2504{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2506
2507 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2508
2509 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2511 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 {
2513 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2514 ssl->state++;
2515 return( 0 );
2516 }
2517
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002518 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2519 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520}
2521#else
2522int ssl_write_certificate( ssl_context *ssl )
2523{
2524 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2525 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002526 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002527 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2528
2529 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2530
2531 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002534 {
2535 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2536 ssl->state++;
2537 return( 0 );
2538 }
2539
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002540#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 if( ssl->endpoint == SSL_IS_CLIENT )
2542 {
2543 if( ssl->client_auth == 0 )
2544 {
2545 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2546 ssl->state++;
2547 return( 0 );
2548 }
2549
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002550#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 /*
2552 * If using SSLv3 and got no cert, send an Alert message
2553 * (otherwise an empty Certificate message will be sent).
2554 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002555 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002556 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2557 {
2558 ssl->out_msglen = 2;
2559 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002560 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2561 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2564 goto write_msg;
2565 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002566#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002568#endif /* POLARSSL_SSL_CLI_C */
2569#if defined(POLARSSL_SSL_SRV_C)
2570 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002572 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
2574 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002578#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002580 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
2582 /*
2583 * 0 . 0 handshake type
2584 * 1 . 3 handshake length
2585 * 4 . 6 length of all certs
2586 * 7 . 9 length of cert. 1
2587 * 10 . n-1 peer certificate
2588 * n . n+2 length of cert. 2
2589 * n+3 . ... upper level cert, etc.
2590 */
2591 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002592 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593
Paul Bakker29087132010-03-21 21:03:34 +00002594 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 {
2596 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002597 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 {
2599 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2600 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002601 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
2603
2604 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2605 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2606 ssl->out_msg[i + 2] = (unsigned char)( n );
2607
2608 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2609 i += n; crt = crt->next;
2610 }
2611
2612 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2613 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2614 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2615
2616 ssl->out_msglen = i;
2617 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2618 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2619
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002620#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002621write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002622#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 ssl->state++;
2625
2626 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2627 {
2628 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2629 return( ret );
2630 }
2631
2632 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2633
Paul Bakkered27a042013-04-18 22:46:23 +02002634 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002635}
2636
2637int ssl_parse_certificate( ssl_context *ssl )
2638{
Paul Bakkered27a042013-04-18 22:46:23 +02002639 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002640 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002641 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
2643 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2644
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002645 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002646 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2647 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002648 {
2649 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2650 ssl->state++;
2651 return( 0 );
2652 }
2653
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002654#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002656 ( ssl->authmode == SSL_VERIFY_NONE ||
2657 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002658 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002659 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2661 ssl->state++;
2662 return( 0 );
2663 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
2666 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2667 {
2668 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2669 return( ret );
2670 }
2671
2672 ssl->state++;
2673
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002674#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002675#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 /*
2677 * Check if the client sent an empty certificate
2678 */
2679 if( ssl->endpoint == SSL_IS_SERVER &&
2680 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2681 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002682 if( ssl->in_msglen == 2 &&
2683 ssl->in_msgtype == SSL_MSG_ALERT &&
2684 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2685 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 {
2687 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2688
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002689 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002690 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2691 return( 0 );
2692 else
Paul Bakker40e46942009-01-03 21:51:57 +00002693 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002694 }
2695 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002696#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002698#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2699 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 if( ssl->endpoint == SSL_IS_SERVER &&
2701 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2702 {
2703 if( ssl->in_hslen == 7 &&
2704 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2705 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2706 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2707 {
2708 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2709
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002710 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002712 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 else
2714 return( 0 );
2715 }
2716 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002717#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2718 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002719#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
2721 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2722 {
2723 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002724 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
2726
2727 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2728 {
2729 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002730 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
2733 /*
2734 * Same message structure as in ssl_write_certificate()
2735 */
2736 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2737
2738 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2739 {
2740 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002741 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002742 }
2743
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002744 /* In case we tried to reuse a session but it failed */
2745 if( ssl->session_negotiate->peer_cert != NULL )
2746 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002747 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002748 polarssl_free( ssl->session_negotiate->peer_cert );
2749 }
2750
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002751 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2752 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 {
2754 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002755 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002756 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758
Paul Bakkerb6b09562013-09-18 14:17:41 +02002759 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760
2761 i = 7;
2762
2763 while( i < ssl->in_hslen )
2764 {
2765 if( ssl->in_msg[i] != 0 )
2766 {
2767 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002768 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 }
2770
2771 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2772 | (unsigned int) ssl->in_msg[i + 2];
2773 i += 3;
2774
2775 if( n < 128 || i + n > ssl->in_hslen )
2776 {
2777 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002778 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
2780
Paul Bakkerddf26b42013-09-18 13:46:23 +02002781 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2782 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002783 if( ret != 0 )
2784 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002785 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786 return( ret );
2787 }
2788
2789 i += n;
2790 }
2791
Paul Bakker48916f92012-09-16 19:57:18 +00002792 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002794 /*
2795 * On client, make sure the server cert doesn't change during renego to
2796 * avoid "triple handshake" attack: https://secure-resumption.com/
2797 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002798#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002799 if( ssl->endpoint == SSL_IS_CLIENT &&
2800 ssl->renegotiation == SSL_RENEGOTIATION )
2801 {
2802 if( ssl->session->peer_cert == NULL )
2803 {
2804 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2805 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2806 }
2807
2808 if( ssl->session->peer_cert->raw.len !=
2809 ssl->session_negotiate->peer_cert->raw.len ||
2810 memcmp( ssl->session->peer_cert->raw.p,
2811 ssl->session_negotiate->peer_cert->raw.p,
2812 ssl->session->peer_cert->raw.len ) != 0 )
2813 {
2814 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2815 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2816 }
2817 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002818#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 if( ssl->authmode != SSL_VERIFY_NONE )
2821 {
2822 if( ssl->ca_chain == NULL )
2823 {
2824 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002825 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 }
2827
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002828 /*
2829 * Main check: verify certificate
2830 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002831 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2832 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2833 &ssl->session_negotiate->verify_result,
2834 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002835
2836 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002837 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002838 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002839 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002840
2841 /*
2842 * Secondary checks: always done, but change 'ret' only if it was 0
2843 */
2844
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002845#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002846 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002847 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002848
2849 /* If certificate uses an EC key, make sure the curve is OK */
2850 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2851 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2852 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002853 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002854 if( ret == 0 )
2855 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002856 }
2857 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002858#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002860 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2861 ciphersuite_info,
2862 ! ssl->endpoint ) != 0 )
2863 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002864 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002865 if( ret == 0 )
2866 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2867 }
2868
Paul Bakker5121ce52009-01-03 21:22:43 +00002869 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2870 ret = 0;
2871 }
2872
2873 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2874
2875 return( ret );
2876}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002877#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2878 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2879 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2880 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2881 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2882 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2883 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
2885int ssl_write_change_cipher_spec( ssl_context *ssl )
2886{
2887 int ret;
2888
2889 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2890
2891 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2892 ssl->out_msglen = 1;
2893 ssl->out_msg[0] = 1;
2894
Paul Bakker5121ce52009-01-03 21:22:43 +00002895 ssl->state++;
2896
2897 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2898 {
2899 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2900 return( ret );
2901 }
2902
2903 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2904
2905 return( 0 );
2906}
2907
2908int ssl_parse_change_cipher_spec( ssl_context *ssl )
2909{
2910 int ret;
2911
2912 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2913
Paul Bakker5121ce52009-01-03 21:22:43 +00002914 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2915 {
2916 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2917 return( ret );
2918 }
2919
2920 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2921 {
2922 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002923 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 }
2925
2926 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2927 {
2928 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002929 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 }
2931
2932 ssl->state++;
2933
2934 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2935
2936 return( 0 );
2937}
2938
Paul Bakker41c83d32013-03-20 14:39:14 +01002939void ssl_optimize_checksum( ssl_context *ssl,
2940 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002941{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002942 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002943
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2945 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002946 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002947 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002948 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002949#endif
2950#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2951#if defined(POLARSSL_SHA512_C)
2952 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2953 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2954 else
2955#endif
2956#if defined(POLARSSL_SHA256_C)
2957 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002958 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002959 else
2960#endif
2961#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002962 {
2963 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002964 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002965 }
Paul Bakker380da532012-04-18 16:10:25 +00002966}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002967
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002968static void ssl_update_checksum_start( ssl_context *ssl,
2969 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002970{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2972 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002973 md5_update( &ssl->handshake->fin_md5 , buf, len );
2974 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002975#endif
2976#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2977#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002978 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002979#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002980#if defined(POLARSSL_SHA512_C)
2981 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002982#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002983#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002984}
2985
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002986#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2987 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002988static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2989 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002990{
Paul Bakker48916f92012-09-16 19:57:18 +00002991 md5_update( &ssl->handshake->fin_md5 , buf, len );
2992 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002993}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002994#endif
Paul Bakker380da532012-04-18 16:10:25 +00002995
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2997#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002998static void ssl_update_checksum_sha256( ssl_context *ssl,
2999 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003000{
Paul Bakker9e36f042013-06-30 14:34:05 +02003001 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003002}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003003#endif
Paul Bakker380da532012-04-18 16:10:25 +00003004
Paul Bakker9e36f042013-06-30 14:34:05 +02003005#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003006static void ssl_update_checksum_sha384( ssl_context *ssl,
3007 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003008{
Paul Bakker9e36f042013-06-30 14:34:05 +02003009 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003010}
Paul Bakker769075d2012-11-24 11:26:46 +01003011#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003013
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015static void ssl_calc_finished_ssl(
3016 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003018 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019 md5_context md5;
3020 sha1_context sha1;
3021
Paul Bakker5121ce52009-01-03 21:22:43 +00003022 unsigned char padbuf[48];
3023 unsigned char md5sum[16];
3024 unsigned char sha1sum[20];
3025
Paul Bakker48916f92012-09-16 19:57:18 +00003026 ssl_session *session = ssl->session_negotiate;
3027 if( !session )
3028 session = ssl->session;
3029
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3031
Paul Bakker48916f92012-09-16 19:57:18 +00003032 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3033 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
3035 /*
3036 * SSLv3:
3037 * hash =
3038 * MD5( master + pad2 +
3039 * MD5( handshake + sender + master + pad1 ) )
3040 * + SHA1( master + pad2 +
3041 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 */
3043
Paul Bakker90995b52013-06-24 19:20:35 +02003044#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Paul Bakker90995b52013-06-24 19:20:35 +02003049#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003051 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003053
Paul Bakker3c2122f2013-06-24 19:03:14 +02003054 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3055 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakker1ef83d62012-04-11 12:09:53 +00003057 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker3c2122f2013-06-24 19:03:14 +02003059 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003060 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003061 md5_update( &md5, padbuf, 48 );
3062 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003063
Paul Bakker3c2122f2013-06-24 19:03:14 +02003064 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003065 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003066 sha1_update( &sha1, padbuf, 40 );
3067 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003068
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003072 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_update( &md5, padbuf, 48 );
3074 md5_update( &md5, md5sum, 16 );
3075 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003076
Paul Bakker1ef83d62012-04-11 12:09:53 +00003077 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003078 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 sha1_update( &sha1, padbuf , 40 );
3080 sha1_update( &sha1, sha1sum, 20 );
3081 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003082
Paul Bakker1ef83d62012-04-11 12:09:53 +00003083 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Paul Bakker5b4af392014-06-26 12:09:34 +02003085 md5_free( &md5 );
3086 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003087
Paul Bakker34617722014-06-13 17:20:13 +02003088 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3089 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3090 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
3092 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3093}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003094#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003096#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097static void ssl_calc_finished_tls(
3098 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003099{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003101 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003103 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 ssl_session *session = ssl->session_negotiate;
3107 if( !session )
3108 session = ssl->session;
3109
Paul Bakker1ef83d62012-04-11 12:09:53 +00003110 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003111
Paul Bakker48916f92012-09-16 19:57:18 +00003112 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3113 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003114
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115 /*
3116 * TLSv1:
3117 * hash = PRF( master, finished_label,
3118 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3119 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003120
Paul Bakker90995b52013-06-24 19:20:35 +02003121#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3123 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003124#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125
Paul Bakker90995b52013-06-24 19:20:35 +02003126#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3128 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003129#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130
3131 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003132 ? "client finished"
3133 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134
3135 md5_finish( &md5, padbuf );
3136 sha1_finish( &sha1, padbuf + 16 );
3137
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003138 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003139 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003140
3141 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3142
Paul Bakker5b4af392014-06-26 12:09:34 +02003143 md5_free( &md5 );
3144 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003145
Paul Bakker34617722014-06-13 17:20:13 +02003146 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147
3148 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3149}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003150#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003151
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003152#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3153#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003154static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155 ssl_context *ssl, unsigned char *buf, int from )
3156{
3157 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003158 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003159 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003160 unsigned char padbuf[32];
3161
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ssl_session *session = ssl->session_negotiate;
3163 if( !session )
3164 session = ssl->session;
3165
Paul Bakker380da532012-04-18 16:10:25 +00003166 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003167
Paul Bakker9e36f042013-06-30 14:34:05 +02003168 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
3170 /*
3171 * TLSv1.2:
3172 * hash = PRF( master, finished_label,
3173 * Hash( handshake ) )[0.11]
3174 */
3175
Paul Bakker9e36f042013-06-30 14:34:05 +02003176#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003177 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003178 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003179#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003180
3181 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003182 ? "client finished"
3183 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003184
Paul Bakker9e36f042013-06-30 14:34:05 +02003185 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003187 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003188 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003189
3190 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3191
Paul Bakker5b4af392014-06-26 12:09:34 +02003192 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003193
Paul Bakker34617722014-06-13 17:20:13 +02003194 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195
3196 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3197}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003198#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003199
Paul Bakker9e36f042013-06-30 14:34:05 +02003200#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003201static void ssl_calc_finished_tls_sha384(
3202 ssl_context *ssl, unsigned char *buf, int from )
3203{
3204 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003205 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003206 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003207 unsigned char padbuf[48];
3208
Paul Bakker48916f92012-09-16 19:57:18 +00003209 ssl_session *session = ssl->session_negotiate;
3210 if( !session )
3211 session = ssl->session;
3212
Paul Bakker380da532012-04-18 16:10:25 +00003213 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003214
Paul Bakker9e36f042013-06-30 14:34:05 +02003215 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216
3217 /*
3218 * TLSv1.2:
3219 * hash = PRF( master, finished_label,
3220 * Hash( handshake ) )[0.11]
3221 */
3222
Paul Bakker9e36f042013-06-30 14:34:05 +02003223#if !defined(POLARSSL_SHA512_ALT)
3224 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3225 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003226#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003227
3228 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003229 ? "client finished"
3230 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003231
Paul Bakker9e36f042013-06-30 14:34:05 +02003232 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003233
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003234 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003235 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003236
3237 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3238
Paul Bakker5b4af392014-06-26 12:09:34 +02003239 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003240
Paul Bakker34617722014-06-13 17:20:13 +02003241 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003242
3243 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3244}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003245#endif /* POLARSSL_SHA512_C */
3246#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003247
Paul Bakker48916f92012-09-16 19:57:18 +00003248void ssl_handshake_wrapup( ssl_context *ssl )
3249{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003250 int resume = ssl->handshake->resume;
3251
Paul Bakker48916f92012-09-16 19:57:18 +00003252 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3253
3254 /*
3255 * Free our handshake params
3256 */
3257 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003258 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003259 ssl->handshake = NULL;
3260
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003261#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003262 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003263 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003264 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003265 ssl->renego_records_seen = 0;
3266 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003267#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003268
Paul Bakker48916f92012-09-16 19:57:18 +00003269 /*
3270 * Switch in our now active transform context
3271 */
3272 if( ssl->transform )
3273 {
3274 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003275 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003276 }
3277 ssl->transform = ssl->transform_negotiate;
3278 ssl->transform_negotiate = NULL;
3279
Paul Bakker0a597072012-09-25 21:55:46 +00003280 if( ssl->session )
3281 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003282#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3283 /* RFC 7366 3.1: keep the EtM state */
3284 ssl->session_negotiate->encrypt_then_mac =
3285 ssl->session->encrypt_then_mac;
3286#endif
3287
Paul Bakker0a597072012-09-25 21:55:46 +00003288 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003289 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003290 }
3291 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003292 ssl->session_negotiate = NULL;
3293
Paul Bakker0a597072012-09-25 21:55:46 +00003294 /*
3295 * Add cache entry
3296 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003297 if( ssl->f_set_cache != NULL &&
3298 ssl->session->length != 0 &&
3299 resume == 0 )
3300 {
Paul Bakker0a597072012-09-25 21:55:46 +00003301 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3302 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003303 }
Paul Bakker0a597072012-09-25 21:55:46 +00003304
Paul Bakker48916f92012-09-16 19:57:18 +00003305 ssl->state++;
3306
3307 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3308}
3309
Paul Bakker1ef83d62012-04-11 12:09:53 +00003310int ssl_write_finished( ssl_context *ssl )
3311{
3312 int ret, hash_len;
3313
3314 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3315
Paul Bakker92be97b2013-01-02 17:30:03 +01003316 /*
3317 * Set the out_msg pointer to the correct location based on IV length
3318 */
3319 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3320 {
3321 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3322 ssl->transform_negotiate->fixed_ivlen;
3323 }
3324 else
3325 ssl->out_msg = ssl->out_iv;
3326
Paul Bakker48916f92012-09-16 19:57:18 +00003327 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003328
3329 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3331
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003332#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003333 ssl->verify_data_len = hash_len;
3334 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003335#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003336
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 ssl->out_msglen = 4 + hash_len;
3338 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3339 ssl->out_msg[0] = SSL_HS_FINISHED;
3340
3341 /*
3342 * In case of session resuming, invert the client and server
3343 * ChangeCipherSpec messages order.
3344 */
Paul Bakker0a597072012-09-25 21:55:46 +00003345 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003347#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003349 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003350#endif
3351#if defined(POLARSSL_SSL_SRV_C)
3352 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003354#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 }
3356 else
3357 ssl->state++;
3358
Paul Bakker48916f92012-09-16 19:57:18 +00003359 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003360 * Switch to our negotiated transform and session parameters for outbound
3361 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003362 */
3363 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3364 ssl->transform_out = ssl->transform_negotiate;
3365 ssl->session_out = ssl->session_negotiate;
3366 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003367
Paul Bakker07eb38b2012-12-19 14:42:06 +01003368#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003369 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003370 {
3371 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3372 {
3373 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3374 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3375 }
3376 }
3377#endif
3378
Paul Bakker5121ce52009-01-03 21:22:43 +00003379 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3380 {
3381 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3382 return( ret );
3383 }
3384
3385 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3386
3387 return( 0 );
3388}
3389
3390int ssl_parse_finished( ssl_context *ssl )
3391{
Paul Bakker23986e52011-04-24 08:57:21 +00003392 int ret;
3393 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 unsigned char buf[36];
3395
3396 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3397
Paul Bakker48916f92012-09-16 19:57:18 +00003398 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003399
Paul Bakker48916f92012-09-16 19:57:18 +00003400 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003401 * Switch to our negotiated transform and session parameters for inbound
3402 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003403 */
3404 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3405 ssl->transform_in = ssl->transform_negotiate;
3406 ssl->session_in = ssl->session_negotiate;
3407 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408
Paul Bakker92be97b2013-01-02 17:30:03 +01003409 /*
3410 * Set the in_msg pointer to the correct location based on IV length
3411 */
3412 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3413 {
3414 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3415 ssl->transform_negotiate->fixed_ivlen;
3416 }
3417 else
3418 ssl->in_msg = ssl->in_iv;
3419
Paul Bakker07eb38b2012-12-19 14:42:06 +01003420#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003421 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003422 {
3423 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3424 {
3425 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3426 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3427 }
3428 }
3429#endif
3430
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3432 {
3433 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3434 return( ret );
3435 }
3436
3437 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3438 {
3439 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003440 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003441 }
3442
Paul Bakker1ef83d62012-04-11 12:09:53 +00003443 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003444 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3445
3446 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3447 ssl->in_hslen != 4 + hash_len )
3448 {
3449 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003450 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 }
3452
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003453 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 {
3455 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003456 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 }
3458
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003459#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003460 ssl->verify_data_len = hash_len;
3461 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003462#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003463
Paul Bakker0a597072012-09-25 21:55:46 +00003464 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003466#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 if( ssl->endpoint == SSL_IS_CLIENT )
3468 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003469#endif
3470#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003472 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003473#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003474 }
3475 else
3476 ssl->state++;
3477
3478 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3479
3480 return( 0 );
3481}
3482
Paul Bakker968afaa2014-07-09 11:09:24 +02003483static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003484{
3485 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3486
3487#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3488 defined(POLARSSL_SSL_PROTO_TLS1_1)
3489 md5_init( &handshake->fin_md5 );
3490 sha1_init( &handshake->fin_sha1 );
3491 md5_starts( &handshake->fin_md5 );
3492 sha1_starts( &handshake->fin_sha1 );
3493#endif
3494#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3495#if defined(POLARSSL_SHA256_C)
3496 sha256_init( &handshake->fin_sha256 );
3497 sha256_starts( &handshake->fin_sha256, 0 );
3498#endif
3499#if defined(POLARSSL_SHA512_C)
3500 sha512_init( &handshake->fin_sha512 );
3501 sha512_starts( &handshake->fin_sha512, 1 );
3502#endif
3503#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3504
3505 handshake->update_checksum = ssl_update_checksum_start;
3506 handshake->sig_alg = SSL_HASH_SHA1;
3507
3508#if defined(POLARSSL_DHM_C)
3509 dhm_init( &handshake->dhm_ctx );
3510#endif
3511#if defined(POLARSSL_ECDH_C)
3512 ecdh_init( &handshake->ecdh_ctx );
3513#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003514}
3515
3516static void ssl_transform_init( ssl_transform *transform )
3517{
3518 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003519
3520 cipher_init( &transform->cipher_ctx_enc );
3521 cipher_init( &transform->cipher_ctx_dec );
3522
3523 md_init( &transform->md_ctx_enc );
3524 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003525}
3526
3527void ssl_session_init( ssl_session *session )
3528{
3529 memset( session, 0, sizeof(ssl_session) );
3530}
3531
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003532static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003533{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003534 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003535 if( ssl->transform_negotiate )
3536 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003537 if( ssl->session_negotiate )
3538 ssl_session_free( ssl->session_negotiate );
3539 if( ssl->handshake )
3540 ssl_handshake_free( ssl->handshake );
3541
3542 /*
3543 * Either the pointers are now NULL or cleared properly and can be freed.
3544 * Now allocate missing structures.
3545 */
3546 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003547 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003548 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
3549 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003550 }
Paul Bakker48916f92012-09-16 19:57:18 +00003551
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003552 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003553 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003554 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
3555 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 {
3560 ssl->handshake = (ssl_handshake_params *)
3561 polarssl_malloc( sizeof(ssl_handshake_params) );
3562 }
Paul Bakker48916f92012-09-16 19:57:18 +00003563
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003564 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003565 if( ssl->handshake == NULL ||
3566 ssl->transform_negotiate == NULL ||
3567 ssl->session_negotiate == NULL )
3568 {
3569 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003570
3571 polarssl_free( ssl->handshake );
3572 polarssl_free( ssl->transform_negotiate );
3573 polarssl_free( ssl->session_negotiate );
3574
3575 ssl->handshake = NULL;
3576 ssl->transform_negotiate = NULL;
3577 ssl->session_negotiate = NULL;
3578
Paul Bakker48916f92012-09-16 19:57:18 +00003579 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3580 }
3581
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003582 /* Initialize structures */
3583 ssl_session_init( ssl->session_negotiate );
3584 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003585 ssl_handshake_params_init( ssl->handshake );
3586
3587#if defined(POLARSSL_X509_CRT_PARSE_C)
3588 ssl->handshake->key_cert = ssl->key_cert;
3589#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003590
Paul Bakker48916f92012-09-16 19:57:18 +00003591 return( 0 );
3592}
3593
Paul Bakker5121ce52009-01-03 21:22:43 +00003594/*
3595 * Initialize an SSL context
3596 */
3597int ssl_init( ssl_context *ssl )
3598{
Paul Bakker48916f92012-09-16 19:57:18 +00003599 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 int len = SSL_BUFFER_LEN;
3601
3602 memset( ssl, 0, sizeof( ssl_context ) );
3603
Paul Bakker62f2dee2012-09-28 07:31:51 +00003604 /*
3605 * Sane defaults
3606 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003607 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3608 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3609 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3610 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003611
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003612 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003613
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003614#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003615 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003616 memset( ssl->renego_period, 0xFF, 7 );
3617 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003618#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003619
Paul Bakker62f2dee2012-09-28 07:31:51 +00003620#if defined(POLARSSL_DHM_C)
3621 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3622 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3623 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3624 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3625 {
3626 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3627 return( ret );
3628 }
3629#endif
3630
3631 /*
3632 * Prepare base structures
3633 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003634 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003635 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003636 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 ssl->in_msg = ssl->in_ctr + 13;
3638
3639 if( ssl->in_ctr == NULL )
3640 {
3641 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003642 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 }
3644
Paul Bakker6e339b52013-07-03 13:37:05 +02003645 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003647 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003648 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003649
3650 if( ssl->out_ctr == NULL )
3651 {
3652 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003653 polarssl_free( ssl->in_ctr );
3654 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003655 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003656 }
3657
3658 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3659 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3660
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003661#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3662 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3663#endif
3664
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003665#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3666 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3667#endif
3668
Paul Bakker606b4ba2013-08-14 16:52:14 +02003669#if defined(POLARSSL_SSL_SESSION_TICKETS)
3670 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3671#endif
3672
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003673#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003674 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003675#endif
3676
Paul Bakker48916f92012-09-16 19:57:18 +00003677 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3678 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
3680 return( 0 );
3681}
3682
3683/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003684 * Reset an initialized and used SSL context for re-use while retaining
3685 * all application-set variables, function pointers and data.
3686 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003687int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003688{
Paul Bakker48916f92012-09-16 19:57:18 +00003689 int ret;
3690
Paul Bakker7eb013f2011-10-06 12:37:39 +00003691 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003692
3693#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003694 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003695 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003696
3697 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003698 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3699 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003700#endif
3701 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003702
Paul Bakker7eb013f2011-10-06 12:37:39 +00003703 ssl->in_offt = NULL;
3704
Paul Bakker92be97b2013-01-02 17:30:03 +01003705 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003706 ssl->in_msgtype = 0;
3707 ssl->in_msglen = 0;
3708 ssl->in_left = 0;
3709
3710 ssl->in_hslen = 0;
3711 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003712 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003713
Paul Bakker92be97b2013-01-02 17:30:03 +01003714 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003715 ssl->out_msgtype = 0;
3716 ssl->out_msglen = 0;
3717 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003718#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003719 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3720 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003721#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003722
Paul Bakker48916f92012-09-16 19:57:18 +00003723 ssl->transform_in = NULL;
3724 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003725
3726 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3727 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003728
3729#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003730 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003731 {
3732 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003733 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003734 {
3735 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3736 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3737 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003738 }
3739#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003740
Paul Bakker48916f92012-09-16 19:57:18 +00003741 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003742 {
Paul Bakker48916f92012-09-16 19:57:18 +00003743 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003744 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003745 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003746 }
Paul Bakker48916f92012-09-16 19:57:18 +00003747
Paul Bakkerc0463502013-02-14 11:19:38 +01003748 if( ssl->session )
3749 {
3750 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003751 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003752 ssl->session = NULL;
3753 }
3754
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003755#if defined(POLARSSL_SSL_ALPN)
3756 ssl->alpn_chosen = NULL;
3757#endif
3758
Paul Bakker48916f92012-09-16 19:57:18 +00003759 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3760 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003761
3762 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003763}
3764
Paul Bakkera503a632013-08-14 13:48:06 +02003765#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003766static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3767{
3768 aes_free( &tkeys->enc );
3769 aes_free( &tkeys->dec );
3770
3771 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3772}
3773
Paul Bakker7eb013f2011-10-06 12:37:39 +00003774/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003775 * Allocate and initialize ticket keys
3776 */
3777static int ssl_ticket_keys_init( ssl_context *ssl )
3778{
3779 int ret;
3780 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003781 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003782
3783 if( ssl->ticket_keys != NULL )
3784 return( 0 );
3785
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003786 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3787 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003788 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3789
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003790 aes_init( &tkeys->enc );
3791 aes_init( &tkeys->dec );
3792
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003793 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003794 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003795 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003796 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003797 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003798 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003799
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003800 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3801 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3802 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3803 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003804 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003805 polarssl_free( tkeys );
3806 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003807 }
3808
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003809 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003810 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003811 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003812 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003813 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003814 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003815
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003816 ssl->ticket_keys = tkeys;
3817
3818 return( 0 );
3819}
Paul Bakkera503a632013-08-14 13:48:06 +02003820#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003821
3822/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003823 * SSL set accessors
3824 */
3825void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3826{
3827 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003828
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003829#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3830 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003831 if( endpoint == SSL_IS_CLIENT )
3832 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003833#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003834
3835#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3836 if( endpoint == SSL_IS_SERVER )
3837 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3838#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003839}
3840
3841void ssl_set_authmode( ssl_context *ssl, int authmode )
3842{
3843 ssl->authmode = authmode;
3844}
3845
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003846#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003847void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003848 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003849 void *p_vrfy )
3850{
3851 ssl->f_vrfy = f_vrfy;
3852 ssl->p_vrfy = p_vrfy;
3853}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003854#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003855
Paul Bakker5121ce52009-01-03 21:22:43 +00003856void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003857 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 void *p_rng )
3859{
3860 ssl->f_rng = f_rng;
3861 ssl->p_rng = p_rng;
3862}
3863
3864void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003865 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003866 void *p_dbg )
3867{
3868 ssl->f_dbg = f_dbg;
3869 ssl->p_dbg = p_dbg;
3870}
3871
3872void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003873 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003874 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003875{
3876 ssl->f_recv = f_recv;
3877 ssl->f_send = f_send;
3878 ssl->p_recv = p_recv;
3879 ssl->p_send = p_send;
3880}
3881
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003882#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003883void ssl_set_session_cache( ssl_context *ssl,
3884 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3885 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003886{
Paul Bakker0a597072012-09-25 21:55:46 +00003887 ssl->f_get_cache = f_get_cache;
3888 ssl->p_get_cache = p_get_cache;
3889 ssl->f_set_cache = f_set_cache;
3890 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003891}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003892#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003893
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003894#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003895int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003896{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003897 int ret;
3898
3899 if( ssl == NULL ||
3900 session == NULL ||
3901 ssl->session_negotiate == NULL ||
3902 ssl->endpoint != SSL_IS_CLIENT )
3903 {
3904 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3905 }
3906
3907 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3908 return( ret );
3909
Paul Bakker0a597072012-09-25 21:55:46 +00003910 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003911
3912 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003914#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003915
Paul Bakkerb68cad62012-08-23 08:34:18 +00003916void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003917{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003918 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3919 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3920 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3921 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3922}
3923
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003924void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3925 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003926 int major, int minor )
3927{
3928 if( major != SSL_MAJOR_VERSION_3 )
3929 return;
3930
3931 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3932 return;
3933
3934 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003935}
3936
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003937#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003938/* Add a new (empty) key_cert entry an return a pointer to it */
3939static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3940{
3941 ssl_key_cert *key_cert, *last;
3942
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003943 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3944 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003945 return( NULL );
3946
3947 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3948
3949 /* Append the new key_cert to the (possibly empty) current list */
3950 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003951 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003952 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003953 if( ssl->handshake != NULL )
3954 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003955 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003956 else
3957 {
3958 last = ssl->key_cert;
3959 while( last->next != NULL )
3960 last = last->next;
3961 last->next = key_cert;
3962 }
3963
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003964 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003965}
3966
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003967void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003968 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003969{
3970 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003971 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003972 ssl->peer_cn = peer_cn;
3973}
3974
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003976 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003977{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003978 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3979
3980 if( key_cert == NULL )
3981 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3982
3983 key_cert->cert = own_cert;
3984 key_cert->key = pk_key;
3985
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003986 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003987}
3988
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003989#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003990int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003991 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003992{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003993 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003994 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003995
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003996 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003997 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3998
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003999 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4000 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004001 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004002
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004003 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004004
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004005 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004006 if( ret != 0 )
4007 return( ret );
4008
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004009 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004010 return( ret );
4011
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004012 key_cert->cert = own_cert;
4013 key_cert->key_own_alloc = 1;
4014
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004015 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004016}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004017#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004018
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004019int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004020 void *rsa_key,
4021 rsa_decrypt_func rsa_decrypt,
4022 rsa_sign_func rsa_sign,
4023 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004024{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004025 int ret;
4026 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004027
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004028 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004029 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4030
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004031 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4032 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004033 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004034
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004035 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004036
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004037 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4038 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4039 return( ret );
4040
4041 key_cert->cert = own_cert;
4042 key_cert->key_own_alloc = 1;
4043
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004044 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00004045}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004046#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004047
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004048#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004049int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4050 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004051{
Paul Bakker6db455e2013-09-18 17:29:31 +02004052 if( psk == NULL || psk_identity == NULL )
4053 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4054
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004055 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004056 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4057
Paul Bakker6db455e2013-09-18 17:29:31 +02004058 if( ssl->psk != NULL )
4059 {
4060 polarssl_free( ssl->psk );
4061 polarssl_free( ssl->psk_identity );
4062 }
4063
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004064 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004065 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004066
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004067 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004068 ssl->psk_identity = (unsigned char *)
4069 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004070
4071 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4072 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4073
4074 memcpy( ssl->psk, psk, ssl->psk_len );
4075 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004076
4077 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004078}
4079
4080void ssl_set_psk_cb( ssl_context *ssl,
4081 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4082 size_t),
4083 void *p_psk )
4084{
4085 ssl->f_psk = f_psk;
4086 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004087}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004088#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004089
Paul Bakker48916f92012-09-16 19:57:18 +00004090#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004091int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004092{
4093 int ret;
4094
Paul Bakker48916f92012-09-16 19:57:18 +00004095 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004096 {
4097 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4098 return( ret );
4099 }
4100
Paul Bakker48916f92012-09-16 19:57:18 +00004101 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004102 {
4103 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4104 return( ret );
4105 }
4106
4107 return( 0 );
4108}
4109
Paul Bakker1b57b062011-01-06 15:48:19 +00004110int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4111{
4112 int ret;
4113
Paul Bakker66d5d072014-06-17 16:39:18 +02004114 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004115 {
4116 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4117 return( ret );
4118 }
4119
Paul Bakker66d5d072014-06-17 16:39:18 +02004120 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004121 {
4122 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4123 return( ret );
4124 }
4125
4126 return( 0 );
4127}
Paul Bakker48916f92012-09-16 19:57:18 +00004128#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004129
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004130#if defined(POLARSSL_SSL_SET_CURVES)
4131/*
4132 * Set the allowed elliptic curves
4133 */
4134void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4135{
4136 ssl->curve_list = curve_list;
4137}
4138#endif
4139
Paul Bakker0be444a2013-08-27 21:55:01 +02004140#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004141int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004142{
4143 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004144 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004145
4146 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004147
4148 if( ssl->hostname_len + 1 == 0 )
4149 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4150
Paul Bakker6e339b52013-07-03 13:37:05 +02004151 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004152
Paul Bakkerb15b8512012-01-13 13:44:06 +00004153 if( ssl->hostname == NULL )
4154 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4155
Paul Bakker3c2122f2013-06-24 19:03:14 +02004156 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004157 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004158
Paul Bakker40ea7de2009-05-03 10:18:48 +00004159 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004160
4161 return( 0 );
4162}
4163
Paul Bakker5701cdc2012-09-27 21:49:42 +00004164void ssl_set_sni( ssl_context *ssl,
4165 int (*f_sni)(void *, ssl_context *,
4166 const unsigned char *, size_t),
4167 void *p_sni )
4168{
4169 ssl->f_sni = f_sni;
4170 ssl->p_sni = p_sni;
4171}
Paul Bakker0be444a2013-08-27 21:55:01 +02004172#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004173
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004174#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004175int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004176{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004177 size_t cur_len, tot_len;
4178 const char **p;
4179
4180 /*
4181 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4182 * truncated". Check lengths now rather than later.
4183 */
4184 tot_len = 0;
4185 for( p = protos; *p != NULL; p++ )
4186 {
4187 cur_len = strlen( *p );
4188 tot_len += cur_len;
4189
4190 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4191 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4192 }
4193
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004194 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004195
4196 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004197}
4198
4199const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4200{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004201 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004202}
4203#endif /* POLARSSL_SSL_ALPN */
4204
Paul Bakker490ecc82011-10-06 13:04:09 +00004205void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4206{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004207 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4208 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4209 {
4210 ssl->max_major_ver = major;
4211 ssl->max_minor_ver = minor;
4212 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004213}
4214
Paul Bakker1d29fb52012-09-28 13:28:45 +00004215void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4216{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004217 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4218 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4219 {
4220 ssl->min_major_ver = major;
4221 ssl->min_minor_ver = minor;
4222 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004223}
4224
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004225#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4226void ssl_set_fallback( ssl_context *ssl, char fallback )
4227{
4228 ssl->fallback = fallback;
4229}
4230#endif
4231
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004232#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4233void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4234{
4235 ssl->encrypt_then_mac = etm;
4236}
4237#endif
4238
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004239#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4240void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4241{
4242 ssl->extended_ms = ems;
4243}
4244#endif
4245
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01004246void ssl_set_arc4_support( ssl_context *ssl, char arc4 )
4247{
4248 ssl->arc4_disabled = arc4;
4249}
4250
Paul Bakker05decb22013-08-15 13:33:48 +02004251#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004252int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4253{
Paul Bakker77e257e2013-12-16 15:29:52 +01004254 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004255 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004256 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004257 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004258 }
4259
4260 ssl->mfl_code = mfl_code;
4261
4262 return( 0 );
4263}
Paul Bakker05decb22013-08-15 13:33:48 +02004264#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004265
Paul Bakker1f2bc622013-08-15 13:45:55 +02004266#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004267int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004268{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004269 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004270
4271 return( 0 );
4272}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004273#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004274
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004275#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4276void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4277{
4278 ssl->split_done = split;
4279}
4280#endif
4281
Paul Bakker48916f92012-09-16 19:57:18 +00004282void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4283{
4284 ssl->allow_legacy_renegotiation = allow_legacy;
4285}
4286
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004287#if defined(POLARSSL_SSL_RENEGOTIATION)
4288void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4289{
4290 ssl->disable_renegotiation = renegotiation;
4291}
4292
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004293void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4294{
4295 ssl->renego_max_records = max_records;
4296}
4297
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004298void ssl_set_renegotiation_period( ssl_context *ssl,
4299 const unsigned char period[8] )
4300{
4301 memcpy( ssl->renego_period, period, 8 );
4302}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004303#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004304
Paul Bakkera503a632013-08-14 13:48:06 +02004305#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004306int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4307{
4308 ssl->session_tickets = use_tickets;
4309
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004310#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004311 if( ssl->endpoint == SSL_IS_CLIENT )
4312 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004313#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004314
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004315 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4316 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004317
4318 if( ssl->f_rng == NULL )
4319 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4320
4321 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004322}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004323
4324void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4325{
4326 ssl->ticket_lifetime = lifetime;
4327}
Paul Bakkera503a632013-08-14 13:48:06 +02004328#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004329
Paul Bakker5121ce52009-01-03 21:22:43 +00004330/*
4331 * SSL get accessors
4332 */
4333size_t ssl_get_bytes_avail( const ssl_context *ssl )
4334{
4335 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4336}
4337
Paul Bakkerff60ee62010-03-16 21:09:09 +00004338int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004339{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00004340 if( ssl->session != NULL )
4341 return( ssl->session->verify_result );
4342
4343 if( ssl->session_negotiate != NULL )
4344 return( ssl->session_negotiate->verify_result );
4345
4346 return( -1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004347}
4348
Paul Bakkere3166ce2011-01-27 17:40:50 +00004349const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004350{
Paul Bakker926c8e42013-03-06 10:23:34 +01004351 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004352 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004353
Paul Bakkere3166ce2011-01-27 17:40:50 +00004354 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004355}
4356
Paul Bakker43ca69c2011-01-15 17:35:19 +00004357const char *ssl_get_version( const ssl_context *ssl )
4358{
4359 switch( ssl->minor_ver )
4360 {
4361 case SSL_MINOR_VERSION_0:
4362 return( "SSLv3.0" );
4363
4364 case SSL_MINOR_VERSION_1:
4365 return( "TLSv1.0" );
4366
4367 case SSL_MINOR_VERSION_2:
4368 return( "TLSv1.1" );
4369
Paul Bakker1ef83d62012-04-11 12:09:53 +00004370 case SSL_MINOR_VERSION_3:
4371 return( "TLSv1.2" );
4372
Paul Bakker43ca69c2011-01-15 17:35:19 +00004373 default:
4374 break;
4375 }
4376 return( "unknown" );
4377}
4378
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004379#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004380const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004381{
4382 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004383 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004384
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004385 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004386}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004387#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004388
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004389#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004390int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4391{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004392 if( ssl == NULL ||
4393 dst == NULL ||
4394 ssl->session == NULL ||
4395 ssl->endpoint != SSL_IS_CLIENT )
4396 {
4397 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4398 }
4399
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004400 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004401}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004402#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004403
Paul Bakker5121ce52009-01-03 21:22:43 +00004404/*
Paul Bakker1961b702013-01-25 14:49:24 +01004405 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004406 */
Paul Bakker1961b702013-01-25 14:49:24 +01004407int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004408{
Paul Bakker40e46942009-01-03 21:51:57 +00004409 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004410
Paul Bakker40e46942009-01-03 21:51:57 +00004411#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004412 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004413 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004414#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004415#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004416 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004417 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004418#endif
4419
Paul Bakker1961b702013-01-25 14:49:24 +01004420 return( ret );
4421}
4422
4423/*
4424 * Perform the SSL handshake
4425 */
4426int ssl_handshake( ssl_context *ssl )
4427{
4428 int ret = 0;
4429
4430 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4431
4432 while( ssl->state != SSL_HANDSHAKE_OVER )
4433 {
4434 ret = ssl_handshake_step( ssl );
4435
4436 if( ret != 0 )
4437 break;
4438 }
4439
Paul Bakker5121ce52009-01-03 21:22:43 +00004440 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4441
4442 return( ret );
4443}
4444
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004445#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004446#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004447/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004448 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004449 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004450static int ssl_write_hello_request( ssl_context *ssl )
4451{
4452 int ret;
4453
4454 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4455
4456 ssl->out_msglen = 4;
4457 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4458 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4459
4460 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4461 {
4462 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4463 return( ret );
4464 }
4465
4466 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4467
4468 return( 0 );
4469}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004470#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004471
4472/*
4473 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004474 * - any side: calling ssl_renegotiate(),
4475 * - client: receiving a HelloRequest during ssl_read(),
4476 * - server: receiving any handshake message on server during ssl_read() after
4477 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004478 * If the handshake doesn't complete due to waiting for I/O, it will continue
4479 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004480 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004481static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004482{
4483 int ret;
4484
4485 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4486
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004487 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4488 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004489
4490 ssl->state = SSL_HELLO_REQUEST;
4491 ssl->renegotiation = SSL_RENEGOTIATION;
4492
Paul Bakker48916f92012-09-16 19:57:18 +00004493 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4494 {
4495 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4496 return( ret );
4497 }
4498
4499 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4500
4501 return( 0 );
4502}
4503
4504/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004505 * Renegotiate current connection on client,
4506 * or request renegotiation on server
4507 */
4508int ssl_renegotiate( ssl_context *ssl )
4509{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004510 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004511
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004512#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004513 /* On server, just send the request */
4514 if( ssl->endpoint == SSL_IS_SERVER )
4515 {
4516 if( ssl->state != SSL_HANDSHAKE_OVER )
4517 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4518
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004519 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4520
4521 /* Did we already try/start sending HelloRequest? */
4522 if( ssl->out_left != 0 )
4523 return( ssl_flush_output( ssl ) );
4524
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004525 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004526 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004527#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004528
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004529#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004530 /*
4531 * On client, either start the renegotiation process or,
4532 * if already in progress, continue the handshake
4533 */
4534 if( ssl->renegotiation != SSL_RENEGOTIATION )
4535 {
4536 if( ssl->state != SSL_HANDSHAKE_OVER )
4537 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4538
4539 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4540 {
4541 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4542 return( ret );
4543 }
4544 }
4545 else
4546 {
4547 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4548 {
4549 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4550 return( ret );
4551 }
4552 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004553#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004554
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004555 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004556}
4557
4558/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004559 * Check record counters and renegotiate if they're above the limit.
4560 */
4561static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4562{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004563 if( ssl->state != SSL_HANDSHAKE_OVER ||
4564 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4565 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4566 {
4567 return( 0 );
4568 }
4569
4570 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004571 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4572 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004573 {
4574 return( 0 );
4575 }
4576
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004577 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004578 return( ssl_renegotiate( ssl ) );
4579}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004580#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
4582/*
4583 * Receive application data decrypted from the SSL layer
4584 */
Paul Bakker23986e52011-04-24 08:57:21 +00004585int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004586{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004587 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004588 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004589
4590 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4591
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004592#if defined(POLARSSL_SSL_RENEGOTIATION)
4593 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4594 {
4595 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4596 return( ret );
4597 }
4598#endif
4599
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 if( ssl->state != SSL_HANDSHAKE_OVER )
4601 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004602 ret = ssl_handshake( ssl );
4603 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4604 {
4605 record_read = 1;
4606 }
4607 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004608 {
4609 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4610 return( ret );
4611 }
4612 }
4613
4614 if( ssl->in_offt == NULL )
4615 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004616 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004617 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004618 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4619 {
4620 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4621 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004622
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004623 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4624 return( ret );
4625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 }
4627
4628 if( ssl->in_msglen == 0 &&
4629 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4630 {
4631 /*
4632 * OpenSSL sends empty messages to randomize the IV
4633 */
4634 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4635 {
Paul Bakker831a7552011-05-18 13:32:51 +00004636 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4637 return( 0 );
4638
Paul Bakker5121ce52009-01-03 21:22:43 +00004639 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4640 return( ret );
4641 }
4642 }
4643
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004644#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004645 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4646 {
4647 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4648
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004649#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004650 if( ssl->endpoint == SSL_IS_CLIENT &&
4651 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4652 ssl->in_hslen != 4 ) )
4653 {
4654 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4655 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4656 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004657#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004658
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004659 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4660 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004661 ssl->allow_legacy_renegotiation ==
4662 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004663 {
4664 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4665
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004666#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004667 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004668 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004669 /*
4670 * SSLv3 does not have a "no_renegotiation" alert
4671 */
4672 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4673 return( ret );
4674 }
4675 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004676#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004677#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4678 defined(POLARSSL_SSL_PROTO_TLS1_2)
4679 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004680 {
4681 if( ( ret = ssl_send_alert_message( ssl,
4682 SSL_ALERT_LEVEL_WARNING,
4683 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4684 {
4685 return( ret );
4686 }
Paul Bakker48916f92012-09-16 19:57:18 +00004687 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004688 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004689#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4690 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004691 {
4692 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004693 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004694 }
Paul Bakker48916f92012-09-16 19:57:18 +00004695 }
4696 else
4697 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004698 ret = ssl_start_renegotiation( ssl );
4699 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4700 {
4701 record_read = 1;
4702 }
4703 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004704 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004705 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004706 return( ret );
4707 }
Paul Bakker48916f92012-09-16 19:57:18 +00004708 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004709
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004710 /* If a non-handshake record was read during renego, fallthrough,
4711 * else tell the user they should call ssl_read() again */
4712 if( ! record_read )
4713 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004714 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004715 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4716 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004717 ssl->renego_records_seen++;
4718
4719 if( ssl->renego_max_records >= 0 &&
4720 ssl->renego_records_seen > ssl->renego_max_records )
4721 {
4722 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4723 "but not honored by client" ) );
4724 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4725 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004726 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004727#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004728
4729 /* Fatal and closure alerts handled by ssl_read_record() */
4730 if( ssl->in_msgtype == SSL_MSG_ALERT )
4731 {
4732 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4733 return( POLARSSL_ERR_NET_WANT_READ );
4734 }
4735
4736 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 {
4738 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004739 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 }
4741
4742 ssl->in_offt = ssl->in_msg;
4743 }
4744
4745 n = ( len < ssl->in_msglen )
4746 ? len : ssl->in_msglen;
4747
4748 memcpy( buf, ssl->in_offt, n );
4749 ssl->in_msglen -= n;
4750
4751 if( ssl->in_msglen == 0 )
4752 /* all bytes consumed */
4753 ssl->in_offt = NULL;
4754 else
4755 /* more data available */
4756 ssl->in_offt += n;
4757
4758 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4759
Paul Bakker23986e52011-04-24 08:57:21 +00004760 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004761}
4762
4763/*
4764 * Send application data to be encrypted by the SSL layer
4765 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004766#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4767static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
4768#else
Paul Bakker23986e52011-04-24 08:57:21 +00004769int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004770#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004771{
Paul Bakker23986e52011-04-24 08:57:21 +00004772 int ret;
4773 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004774 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004775
4776 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4777
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004778#if defined(POLARSSL_SSL_RENEGOTIATION)
4779 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4780 {
4781 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4782 return( ret );
4783 }
4784#endif
4785
Paul Bakker5121ce52009-01-03 21:22:43 +00004786 if( ssl->state != SSL_HANDSHAKE_OVER )
4787 {
4788 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4789 {
4790 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4791 return( ret );
4792 }
4793 }
4794
Paul Bakker05decb22013-08-15 13:33:48 +02004795#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004796 /*
4797 * Assume mfl_code is correct since it was checked when set
4798 */
4799 max_len = mfl_code_to_length[ssl->mfl_code];
4800
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004801 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004802 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004803 */
4804 if( ssl->session_out != NULL &&
4805 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4806 {
4807 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4808 }
Paul Bakker05decb22013-08-15 13:33:48 +02004809#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004810
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004811 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004812
Paul Bakker5121ce52009-01-03 21:22:43 +00004813 if( ssl->out_left != 0 )
4814 {
4815 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4816 {
4817 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4818 return( ret );
4819 }
4820 }
Paul Bakker887bd502011-06-08 13:10:54 +00004821 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004822 {
Paul Bakker887bd502011-06-08 13:10:54 +00004823 ssl->out_msglen = n;
4824 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4825 memcpy( ssl->out_msg, buf, n );
4826
4827 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4828 {
4829 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4830 return( ret );
4831 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004832 }
4833
4834 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4835
Paul Bakker23986e52011-04-24 08:57:21 +00004836 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004837}
4838
4839/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004840 * Write application data, doing 1/n-1 splitting if necessary.
4841 *
4842 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004843 * then the caller will call us again with the same arguments, so
4844 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004845 */
4846#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4847int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4848{
4849 int ret;
4850
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004851 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4852 len <= 1 ||
4853 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004854 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4855 != POLARSSL_MODE_CBC )
4856 {
4857 return( ssl_write_real( ssl, buf, len ) );
4858 }
4859
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004860 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004861 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004862 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004863 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004864 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004865 }
4866
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004867 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4868 return( ret );
4869 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004870
4871 return( ret + 1 );
4872}
4873#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4874
4875/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004876 * Notify the peer that the connection is being closed
4877 */
4878int ssl_close_notify( ssl_context *ssl )
4879{
4880 int ret;
4881
4882 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4883
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004884 if( ssl->out_left != 0 )
4885 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004886
4887 if( ssl->state == SSL_HANDSHAKE_OVER )
4888 {
Paul Bakker48916f92012-09-16 19:57:18 +00004889 if( ( ret = ssl_send_alert_message( ssl,
4890 SSL_ALERT_LEVEL_WARNING,
4891 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004893 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004894 return( ret );
4895 }
4896 }
4897
4898 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4899
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004900 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004901}
4902
Paul Bakker48916f92012-09-16 19:57:18 +00004903void ssl_transform_free( ssl_transform *transform )
4904{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004905 if( transform == NULL )
4906 return;
4907
Paul Bakker48916f92012-09-16 19:57:18 +00004908#if defined(POLARSSL_ZLIB_SUPPORT)
4909 deflateEnd( &transform->ctx_deflate );
4910 inflateEnd( &transform->ctx_inflate );
4911#endif
4912
Paul Bakker84bbeb52014-07-01 14:53:22 +02004913 cipher_free( &transform->cipher_ctx_enc );
4914 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004915
Paul Bakker84bbeb52014-07-01 14:53:22 +02004916 md_free( &transform->md_ctx_enc );
4917 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004918
Paul Bakker34617722014-06-13 17:20:13 +02004919 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004920}
4921
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004922#if defined(POLARSSL_X509_CRT_PARSE_C)
4923static void ssl_key_cert_free( ssl_key_cert *key_cert )
4924{
4925 ssl_key_cert *cur = key_cert, *next;
4926
4927 while( cur != NULL )
4928 {
4929 next = cur->next;
4930
4931 if( cur->key_own_alloc )
4932 {
4933 pk_free( cur->key );
4934 polarssl_free( cur->key );
4935 }
4936 polarssl_free( cur );
4937
4938 cur = next;
4939 }
4940}
4941#endif /* POLARSSL_X509_CRT_PARSE_C */
4942
Paul Bakker48916f92012-09-16 19:57:18 +00004943void ssl_handshake_free( ssl_handshake_params *handshake )
4944{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004945 if( handshake == NULL )
4946 return;
4947
Paul Bakker48916f92012-09-16 19:57:18 +00004948#if defined(POLARSSL_DHM_C)
4949 dhm_free( &handshake->dhm_ctx );
4950#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004951#if defined(POLARSSL_ECDH_C)
4952 ecdh_free( &handshake->ecdh_ctx );
4953#endif
4954
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004955#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004956 /* explicit void pointer cast for buggy MS compiler */
4957 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004958#endif
4959
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004960#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4961 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4962 /*
4963 * Free only the linked list wrapper, not the keys themselves
4964 * since the belong to the SNI callback
4965 */
4966 if( handshake->sni_key_cert != NULL )
4967 {
4968 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4969
4970 while( cur != NULL )
4971 {
4972 next = cur->next;
4973 polarssl_free( cur );
4974 cur = next;
4975 }
4976 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004977#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004978
Paul Bakker34617722014-06-13 17:20:13 +02004979 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004980}
4981
4982void ssl_session_free( ssl_session *session )
4983{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004984 if( session == NULL )
4985 return;
4986
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004987#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004988 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004989 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004990 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004991 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004992 }
Paul Bakkered27a042013-04-18 22:46:23 +02004993#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004994
Paul Bakkera503a632013-08-14 13:48:06 +02004995#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004996 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004997#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004998
Paul Bakker34617722014-06-13 17:20:13 +02004999 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00005000}
5001
Paul Bakker5121ce52009-01-03 21:22:43 +00005002/*
5003 * Free an SSL context
5004 */
5005void ssl_free( ssl_context *ssl )
5006{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02005007 if( ssl == NULL )
5008 return;
5009
Paul Bakker5121ce52009-01-03 21:22:43 +00005010 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5011
Paul Bakker5121ce52009-01-03 21:22:43 +00005012 if( ssl->out_ctr != NULL )
5013 {
Paul Bakker34617722014-06-13 17:20:13 +02005014 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005015 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005016 }
5017
5018 if( ssl->in_ctr != NULL )
5019 {
Paul Bakker34617722014-06-13 17:20:13 +02005020 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005021 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005022 }
5023
Paul Bakker16770332013-10-11 09:59:44 +02005024#if defined(POLARSSL_ZLIB_SUPPORT)
5025 if( ssl->compress_buf != NULL )
5026 {
Paul Bakker34617722014-06-13 17:20:13 +02005027 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005028 polarssl_free( ssl->compress_buf );
5029 }
5030#endif
5031
Paul Bakker40e46942009-01-03 21:51:57 +00005032#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005033 mpi_free( &ssl->dhm_P );
5034 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005035#endif
5036
Paul Bakker48916f92012-09-16 19:57:18 +00005037 if( ssl->transform )
5038 {
5039 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005040 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005041 }
5042
5043 if( ssl->handshake )
5044 {
5045 ssl_handshake_free( ssl->handshake );
5046 ssl_transform_free( ssl->transform_negotiate );
5047 ssl_session_free( ssl->session_negotiate );
5048
Paul Bakker6e339b52013-07-03 13:37:05 +02005049 polarssl_free( ssl->handshake );
5050 polarssl_free( ssl->transform_negotiate );
5051 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005052 }
5053
Paul Bakkerc0463502013-02-14 11:19:38 +01005054 if( ssl->session )
5055 {
5056 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005057 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005058 }
5059
Paul Bakkera503a632013-08-14 13:48:06 +02005060#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005061 if( ssl->ticket_keys )
5062 {
5063 ssl_ticket_keys_free( ssl->ticket_keys );
5064 polarssl_free( ssl->ticket_keys );
5065 }
Paul Bakkera503a632013-08-14 13:48:06 +02005066#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005067
Paul Bakker0be444a2013-08-27 21:55:01 +02005068#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005069 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005070 {
Paul Bakker34617722014-06-13 17:20:13 +02005071 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005072 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005073 ssl->hostname_len = 0;
5074 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005075#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005076
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005077#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005078 if( ssl->psk != NULL )
5079 {
Paul Bakker34617722014-06-13 17:20:13 +02005080 polarssl_zeroize( ssl->psk, ssl->psk_len );
5081 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005082 polarssl_free( ssl->psk );
5083 polarssl_free( ssl->psk_identity );
5084 ssl->psk_len = 0;
5085 ssl->psk_identity_len = 0;
5086 }
5087#endif
5088
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005089#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005090 ssl_key_cert_free( ssl->key_cert );
5091#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005092
Paul Bakker05ef8352012-05-08 09:17:57 +00005093#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5094 if( ssl_hw_record_finish != NULL )
5095 {
5096 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5097 ssl_hw_record_finish( ssl );
5098 }
5099#endif
5100
Paul Bakker5121ce52009-01-03 21:22:43 +00005101 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005102
Paul Bakker86f04f42013-02-14 11:20:09 +01005103 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005104 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005105}
5106
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005107#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005108/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005109 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005110 */
5111unsigned char ssl_sig_from_pk( pk_context *pk )
5112{
5113#if defined(POLARSSL_RSA_C)
5114 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5115 return( SSL_SIG_RSA );
5116#endif
5117#if defined(POLARSSL_ECDSA_C)
5118 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5119 return( SSL_SIG_ECDSA );
5120#endif
5121 return( SSL_SIG_ANON );
5122}
5123
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005124pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5125{
5126 switch( sig )
5127 {
5128#if defined(POLARSSL_RSA_C)
5129 case SSL_SIG_RSA:
5130 return( POLARSSL_PK_RSA );
5131#endif
5132#if defined(POLARSSL_ECDSA_C)
5133 case SSL_SIG_ECDSA:
5134 return( POLARSSL_PK_ECDSA );
5135#endif
5136 default:
5137 return( POLARSSL_PK_NONE );
5138 }
5139}
Paul Bakker9af723c2014-05-01 13:03:14 +02005140#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005141
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005142/*
5143 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5144 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005145md_type_t ssl_md_alg_from_hash( unsigned char hash )
5146{
5147 switch( hash )
5148 {
5149#if defined(POLARSSL_MD5_C)
5150 case SSL_HASH_MD5:
5151 return( POLARSSL_MD_MD5 );
5152#endif
5153#if defined(POLARSSL_SHA1_C)
5154 case SSL_HASH_SHA1:
5155 return( POLARSSL_MD_SHA1 );
5156#endif
5157#if defined(POLARSSL_SHA256_C)
5158 case SSL_HASH_SHA224:
5159 return( POLARSSL_MD_SHA224 );
5160 case SSL_HASH_SHA256:
5161 return( POLARSSL_MD_SHA256 );
5162#endif
5163#if defined(POLARSSL_SHA512_C)
5164 case SSL_HASH_SHA384:
5165 return( POLARSSL_MD_SHA384 );
5166 case SSL_HASH_SHA512:
5167 return( POLARSSL_MD_SHA512 );
5168#endif
5169 default:
5170 return( POLARSSL_MD_NONE );
5171 }
5172}
5173
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005174#if defined(POLARSSL_SSL_SET_CURVES)
5175/*
5176 * Check is a curve proposed by the peer is in our list.
5177 * Return 1 if we're willing to use it, 0 otherwise.
5178 */
5179int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5180{
5181 const ecp_group_id *gid;
5182
5183 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5184 if( *gid == grp_id )
5185 return( 1 );
5186
5187 return( 0 );
5188}
Paul Bakker9af723c2014-05-01 13:03:14 +02005189#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005190
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005191#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005192int ssl_check_cert_usage( const x509_crt *cert,
5193 const ssl_ciphersuite_t *ciphersuite,
5194 int cert_endpoint )
5195{
5196#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5197 int usage = 0;
5198#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005199#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5200 const char *ext_oid;
5201 size_t ext_len;
5202#endif
5203
5204#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5205 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5206 ((void) cert);
5207 ((void) cert_endpoint);
5208#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005209
5210#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5211 if( cert_endpoint == SSL_IS_SERVER )
5212 {
5213 /* Server part of the key exchange */
5214 switch( ciphersuite->key_exchange )
5215 {
5216 case POLARSSL_KEY_EXCHANGE_RSA:
5217 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5218 usage = KU_KEY_ENCIPHERMENT;
5219 break;
5220
5221 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5222 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5223 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5224 usage = KU_DIGITAL_SIGNATURE;
5225 break;
5226
5227 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5228 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5229 usage = KU_KEY_AGREEMENT;
5230 break;
5231
5232 /* Don't use default: we want warnings when adding new values */
5233 case POLARSSL_KEY_EXCHANGE_NONE:
5234 case POLARSSL_KEY_EXCHANGE_PSK:
5235 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5236 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5237 usage = 0;
5238 }
5239 }
5240 else
5241 {
5242 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5243 usage = KU_DIGITAL_SIGNATURE;
5244 }
5245
5246 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5247 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005248#else
5249 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005250#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5251
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005252#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5253 if( cert_endpoint == SSL_IS_SERVER )
5254 {
5255 ext_oid = OID_SERVER_AUTH;
5256 ext_len = OID_SIZE( OID_SERVER_AUTH );
5257 }
5258 else
5259 {
5260 ext_oid = OID_CLIENT_AUTH;
5261 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5262 }
5263
5264 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5265 return( -1 );
5266#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5267
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005268 return( 0 );
5269}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005270#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005271
5272#endif /* POLARSSL_SSL_TLS_C */