blob: eda3d6da7d08777c8cfbe4cc0a8dac5ddab62d33 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604 * 1. if EtM is in use: one block plus MAC
605 * otherwise: * first multiple of blocklen greater than maclen
606 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200607 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100608#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
609 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
610 {
611 transform->minlen = transform->maclen
612 + cipher_info->block_size;
613 }
614 else
615#endif
616 {
617 transform->minlen = transform->maclen
618 + cipher_info->block_size
619 - transform->maclen % cipher_info->block_size;
620 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200621
622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
623 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
624 ssl->minor_ver == SSL_MINOR_VERSION_1 )
625 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100626 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200627#endif
628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
630 ssl->minor_ver == SSL_MINOR_VERSION_3 )
631 {
632 transform->minlen += transform->ivlen;
633 }
634 else
635#endif
636 {
637 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
638 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
639 }
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
643 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000644 transform->keylen, transform->minlen, transform->ivlen,
645 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 /*
648 * Finally setup the cipher contexts, IVs and MAC secrets.
649 */
650 if( ssl->endpoint == SSL_IS_CLIENT )
651 {
Paul Bakker48916f92012-09-16 19:57:18 +0000652 key1 = keyblk + transform->maclen * 2;
653 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Paul Bakker68884e32013-01-07 18:20:04 +0100655 mac_enc = keyblk;
656 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000658 /*
659 * This is not used in TLS v1.1.
660 */
Paul Bakker48916f92012-09-16 19:57:18 +0000661 iv_copy_len = ( transform->fixed_ivlen ) ?
662 transform->fixed_ivlen : transform->ivlen;
663 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
664 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000665 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 }
667 else
668 {
Paul Bakker48916f92012-09-16 19:57:18 +0000669 key1 = keyblk + transform->maclen * 2 + transform->keylen;
670 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
Paul Bakker68884e32013-01-07 18:20:04 +0100672 mac_enc = keyblk + transform->maclen;
673 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000675 /*
676 * This is not used in TLS v1.1.
677 */
Paul Bakker48916f92012-09-16 19:57:18 +0000678 iv_copy_len = ( transform->fixed_ivlen ) ?
679 transform->fixed_ivlen : transform->ivlen;
680 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
681 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000682 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 }
684
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200685#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100686 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
687 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100688 if( transform->maclen > sizeof transform->mac_enc )
689 {
690 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200691 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100692 }
693
Paul Bakker68884e32013-01-07 18:20:04 +0100694 memcpy( transform->mac_enc, mac_enc, transform->maclen );
695 memcpy( transform->mac_dec, mac_dec, transform->maclen );
696 }
697 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200698#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200699#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
700 defined(POLARSSL_SSL_PROTO_TLS1_2)
701 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100702 {
703 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
704 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
705 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200706 else
707#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200708 {
709 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200710 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200711 }
Paul Bakker68884e32013-01-07 18:20:04 +0100712
Paul Bakker05ef8352012-05-08 09:17:57 +0000713#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200714 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000715 {
716 int ret = 0;
717
718 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
719
Paul Bakker07eb38b2012-12-19 14:42:06 +0100720 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
721 transform->iv_enc, transform->iv_dec,
722 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100723 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100724 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000725 {
726 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200727 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000728 }
729 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200730#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000731
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200732 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
733 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000734 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
736 return( ret );
737 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200738
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200739 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
740 cipher_info ) ) != 0 )
741 {
742 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
743 return( ret );
744 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200745
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200746 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
747 cipher_info->key_length,
748 POLARSSL_ENCRYPT ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
751 return( ret );
752 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200753
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200754 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
755 cipher_info->key_length,
756 POLARSSL_DECRYPT ) ) != 0 )
757 {
758 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
759 return( ret );
760 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200761
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200762#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200763 if( cipher_info->mode == POLARSSL_MODE_CBC )
764 {
765 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
766 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200767 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
769 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200770 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200771
772 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
773 POLARSSL_PADDING_NONE ) ) != 0 )
774 {
775 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
776 return( ret );
777 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200779#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000780
Paul Bakker34617722014-06-13 17:20:13 +0200781 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Paul Bakker2770fbd2012-07-03 13:30:23 +0000783#if defined(POLARSSL_ZLIB_SUPPORT)
784 // Initialize compression
785 //
Paul Bakker48916f92012-09-16 19:57:18 +0000786 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000787 {
Paul Bakker16770332013-10-11 09:59:44 +0200788 if( ssl->compress_buf == NULL )
789 {
790 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
791 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
792 if( ssl->compress_buf == NULL )
793 {
794 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
795 SSL_BUFFER_LEN ) );
796 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
797 }
798 }
799
Paul Bakker2770fbd2012-07-03 13:30:23 +0000800 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
801
Paul Bakker48916f92012-09-16 19:57:18 +0000802 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
803 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000804
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200805 if( deflateInit( &transform->ctx_deflate,
806 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000807 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000808 {
809 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
810 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
811 }
812 }
813#endif /* POLARSSL_ZLIB_SUPPORT */
814
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
816
817 return( 0 );
818}
819
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000821void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000822{
823 md5_context md5;
824 sha1_context sha1;
825 unsigned char pad_1[48];
826 unsigned char pad_2[48];
827
Paul Bakker380da532012-04-18 16:10:25 +0000828 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Paul Bakker48916f92012-09-16 19:57:18 +0000830 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
831 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
Paul Bakker380da532012-04-18 16:10:25 +0000833 memset( pad_1, 0x36, 48 );
834 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000837 md5_update( &md5, pad_1, 48 );
838 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker380da532012-04-18 16:10:25 +0000840 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000841 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000842 md5_update( &md5, pad_2, 48 );
843 md5_update( &md5, hash, 16 );
844 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker48916f92012-09-16 19:57:18 +0000846 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000847 sha1_update( &sha1, pad_1, 40 );
848 sha1_finish( &sha1, hash + 16 );
849
850 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000851 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000852 sha1_update( &sha1, pad_2, 40 );
853 sha1_update( &sha1, hash + 16, 20 );
854 sha1_finish( &sha1, hash + 16 );
855
856 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
857 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
858
Paul Bakker5b4af392014-06-26 12:09:34 +0200859 md5_free( &md5 );
860 sha1_free( &sha1 );
861
Paul Bakker380da532012-04-18 16:10:25 +0000862 return;
863}
Paul Bakker9af723c2014-05-01 13:03:14 +0200864#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000867void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
868{
869 md5_context md5;
870 sha1_context sha1;
871
872 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
873
Paul Bakker48916f92012-09-16 19:57:18 +0000874 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
875 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000876
Paul Bakker48916f92012-09-16 19:57:18 +0000877 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000878 sha1_finish( &sha1, hash + 16 );
879
880 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
881 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
882
Paul Bakker5b4af392014-06-26 12:09:34 +0200883 md5_free( &md5 );
884 sha1_free( &sha1 );
885
Paul Bakker380da532012-04-18 16:10:25 +0000886 return;
887}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200888#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000889
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200890#if defined(POLARSSL_SSL_PROTO_TLS1_2)
891#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000892void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
893{
Paul Bakker9e36f042013-06-30 14:34:05 +0200894 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000895
896 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
897
Paul Bakker9e36f042013-06-30 14:34:05 +0200898 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
899 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000900
901 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
902 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
903
Paul Bakker5b4af392014-06-26 12:09:34 +0200904 sha256_free( &sha256 );
905
Paul Bakker380da532012-04-18 16:10:25 +0000906 return;
907}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200908#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000909
Paul Bakker9e36f042013-06-30 14:34:05 +0200910#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000911void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
912{
Paul Bakker9e36f042013-06-30 14:34:05 +0200913 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000914
915 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
916
Paul Bakker9e36f042013-06-30 14:34:05 +0200917 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
918 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Paul Bakkerca4ab492012-04-18 14:23:57 +0000920 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
922
Paul Bakker5b4af392014-06-26 12:09:34 +0200923 sha512_free( &sha512 );
924
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 return;
926}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200927#endif /* POLARSSL_SHA512_C */
928#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200930#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200931int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
932{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200933 unsigned char *p = ssl->handshake->premaster;
934 unsigned char *end = p + sizeof( ssl->handshake->premaster );
935
936 /*
937 * PMS = struct {
938 * opaque other_secret<0..2^16-1>;
939 * opaque psk<0..2^16-1>;
940 * };
941 * with "other_secret" depending on the particular key exchange
942 */
943#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
944 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
945 {
946 if( end - p < 2 + (int) ssl->psk_len )
947 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
948
949 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
950 *(p++) = (unsigned char)( ssl->psk_len );
951 p += ssl->psk_len;
952 }
953 else
954#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200955#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
956 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
957 {
958 /*
959 * other_secret already set by the ClientKeyExchange message,
960 * and is 48 bytes long
961 */
962 *p++ = 0;
963 *p++ = 48;
964 p += 48;
965 }
966 else
967#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200968#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
969 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
970 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200971 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200972 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200974 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200975 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200976 p + 2, &len,
977 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200978 {
979 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
980 return( ret );
981 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200982 *(p++) = (unsigned char)( len >> 8 );
983 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200984 p += len;
985
986 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
987 }
988 else
989#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
990#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
991 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
992 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200993 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994 size_t zlen;
995
996 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200997 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200998 ssl->f_rng, ssl->p_rng ) ) != 0 )
999 {
1000 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1001 return( ret );
1002 }
1003
1004 *(p++) = (unsigned char)( zlen >> 8 );
1005 *(p++) = (unsigned char)( zlen );
1006 p += zlen;
1007
1008 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1009 }
1010 else
1011#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1012 {
1013 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001014 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001015 }
1016
1017 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001018 if( end - p < 2 + (int) ssl->psk_len )
1019 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1020
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001021 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1022 *(p++) = (unsigned char)( ssl->psk_len );
1023 memcpy( p, ssl->psk, ssl->psk_len );
1024 p += ssl->psk_len;
1025
1026 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1027
1028 return( 0 );
1029}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001030#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001031
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001032#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001033/*
1034 * SSLv3.0 MAC functions
1035 */
Paul Bakker68884e32013-01-07 18:20:04 +01001036static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1037 unsigned char *buf, size_t len,
1038 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001039{
1040 unsigned char header[11];
1041 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001042 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001043 int md_size = md_get_size( md_ctx->md_info );
1044 int md_type = md_get_type( md_ctx->md_info );
1045
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001046 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001047 if( md_type == POLARSSL_MD_MD5 )
1048 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001049 else
Paul Bakker68884e32013-01-07 18:20:04 +01001050 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001051
1052 memcpy( header, ctr, 8 );
1053 header[ 8] = (unsigned char) type;
1054 header[ 9] = (unsigned char)( len >> 8 );
1055 header[10] = (unsigned char)( len );
1056
Paul Bakker68884e32013-01-07 18:20:04 +01001057 memset( padding, 0x36, padlen );
1058 md_starts( md_ctx );
1059 md_update( md_ctx, secret, md_size );
1060 md_update( md_ctx, padding, padlen );
1061 md_update( md_ctx, header, 11 );
1062 md_update( md_ctx, buf, len );
1063 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001064
Paul Bakker68884e32013-01-07 18:20:04 +01001065 memset( padding, 0x5C, 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, buf + len, md_size );
1070 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001071}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001072#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001073
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001074#define MAC_NONE 0
1075#define MAC_PLAINTEXT 1
1076#define MAC_CIPHERTEXT 2
1077
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001078#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1079 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1080 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1081#define POLARSSL_SOME_MODES_USE_MAC
1082#endif
1083
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001084/*
1085 * Is MAC applied on ciphertext, cleartext or not at all?
1086 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001087#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001088static char ssl_get_mac_order( ssl_context *ssl,
1089 const ssl_session *session,
1090 cipher_mode_t mode )
1091{
1092#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1093 if( mode == POLARSSL_MODE_STREAM )
1094 return( MAC_PLAINTEXT );
1095#endif
1096
1097#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1098 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1099 if( mode == POLARSSL_MODE_CBC )
1100 {
1101#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1102 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1103 {
1104 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1105 return( MAC_CIPHERTEXT );
1106 }
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001107#else
1108 ((void) ssl);
1109 ((void) session);
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001110#endif
1111
1112 return( MAC_PLAINTEXT );
1113 }
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001114#else
Manuel Pégourié-Gonnard9d7821d2014-11-06 01:19:52 +01001115 ((void) ssl);
1116 ((void) session);
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001117#endif
Manuel Pégourié-Gonnard9d7821d2014-11-06 01:19:52 +01001118
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001119 return( MAC_NONE );
1120}
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001121#endif /* POLARSSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001122
Paul Bakker5121ce52009-01-03 21:22:43 +00001123/*
1124 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001125 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001126static int ssl_encrypt_buf( ssl_context *ssl )
1127{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001128 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001129 const cipher_mode_t mode = cipher_get_cipher_mode(
1130 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001131
1132 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1133
1134 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001135 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 */
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001137#if defined(POLARSSL_SOME_MODES_USE_MAC)
1138 if( ssl_get_mac_order( ssl, ssl->session_out, mode ) == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001139 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001140#if defined(POLARSSL_SSL_PROTO_SSL3)
1141 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1142 {
1143 ssl_mac( &ssl->transform_out->md_ctx_enc,
1144 ssl->transform_out->mac_enc,
1145 ssl->out_msg, ssl->out_msglen,
1146 ssl->out_ctr, ssl->out_msgtype );
1147 }
1148 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001149#endif
1150#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001151 defined(POLARSSL_SSL_PROTO_TLS1_2)
1152 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1153 {
1154 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1155 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1156 ssl->out_msg, ssl->out_msglen );
1157 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1158 ssl->out_msg + ssl->out_msglen );
1159 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1160 }
1161 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001162#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001163 {
1164 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001165 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001166 }
1167
1168 SSL_DEBUG_BUF( 4, "computed mac",
1169 ssl->out_msg + ssl->out_msglen,
1170 ssl->transform_out->maclen );
1171
1172 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001173 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001174#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001175
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001176 /*
1177 * Encrypt
1178 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001179#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001180 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001182 int ret;
1183 size_t olen = 0;
1184
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1186 "including %d bytes of padding",
1187 ssl->out_msglen, 0 ) );
1188
1189 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1190 ssl->out_msg, ssl->out_msglen );
1191
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001192 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001193 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001194 ssl->transform_out->ivlen,
1195 ssl->out_msg, ssl->out_msglen,
1196 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001197 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001198 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001199 return( ret );
1200 }
1201
1202 if( ssl->out_msglen != olen )
1203 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001204 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001205 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001206 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 }
Paul Bakker68884e32013-01-07 18:20:04 +01001208 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001209#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001210#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1211 if( mode == POLARSSL_MODE_GCM ||
1212 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001213 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001214 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001215 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001216 unsigned char *enc_msg;
1217 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001218 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1219 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Paul Bakkerca4ab492012-04-18 14:23:57 +00001221 memcpy( add_data, ssl->out_ctr, 8 );
1222 add_data[8] = ssl->out_msgtype;
1223 add_data[9] = ssl->major_ver;
1224 add_data[10] = ssl->minor_ver;
1225 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1226 add_data[12] = ssl->out_msglen & 0xFF;
1227
1228 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1229 add_data, 13 );
1230
Paul Bakker68884e32013-01-07 18:20:04 +01001231 /*
1232 * Generate IV
1233 */
1234 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001235 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1236 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001237 if( ret != 0 )
1238 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 memcpy( ssl->out_iv,
1241 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1242 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001243
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001244 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001245 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001246
Paul Bakker68884e32013-01-07 18:20:04 +01001247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
1250 enc_msg = ssl->out_msg;
1251 enc_msglen = ssl->out_msglen;
1252 ssl->out_msglen += ssl->transform_out->ivlen -
1253 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001254
Paul Bakker68884e32013-01-07 18:20:04 +01001255 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1256 "including %d bytes of padding",
1257 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001258
Paul Bakker68884e32013-01-07 18:20:04 +01001259 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1260 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001261
Paul Bakker68884e32013-01-07 18:20:04 +01001262 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001263 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001264 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001265 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1266 ssl->transform_out->iv_enc,
1267 ssl->transform_out->ivlen,
1268 add_data, 13,
1269 enc_msg, enc_msglen,
1270 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001271 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001273 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001274 return( ret );
1275 }
1276
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001277 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001278 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001279 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001280 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001281 }
1282
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001283 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001284
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001285 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001286 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001287 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001288#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001289#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1290 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001291 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001293 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001295 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296
Paul Bakker48916f92012-09-16 19:57:18 +00001297 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1298 ssl->transform_out->ivlen;
1299 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001300 padlen = 0;
1301
1302 for( i = 0; i <= padlen; i++ )
1303 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1304
1305 ssl->out_msglen += padlen + 1;
1306
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307 enc_msglen = ssl->out_msglen;
1308 enc_msg = ssl->out_msg;
1309
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001310#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001311 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001312 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1313 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001314 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001315 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316 {
1317 /*
1318 * Generate IV
1319 */
Paul Bakker48916f92012-09-16 19:57:18 +00001320 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1321 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001322 if( ret != 0 )
1323 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324
Paul Bakker92be97b2013-01-02 17:30:03 +01001325 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001326 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001327
1328 /*
1329 * Fix pointer positions and message length with added IV
1330 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001331 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001332 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001333 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001334 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001335#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001336
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001338 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001339 ssl->out_msglen, ssl->transform_out->ivlen,
1340 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001343 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001345 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001346 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001347 ssl->transform_out->ivlen,
1348 enc_msg, enc_msglen,
1349 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001350 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001351 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001352 return( ret );
1353 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001354
Paul Bakkercca5b812013-08-31 17:40:26 +02001355 if( enc_msglen != olen )
1356 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001357 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001358 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001359 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001360
1361#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001362 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1363 {
1364 /*
1365 * Save IV in SSL3 and TLS1
1366 */
1367 memcpy( ssl->transform_out->iv_enc,
1368 ssl->transform_out->cipher_ctx_enc.iv,
1369 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001371#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001372
1373#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001374 if( ssl_get_mac_order( ssl, ssl->session_out, mode ) == MAC_CIPHERTEXT )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001375 {
1376 /*
1377 * MAC(MAC_write_key, seq_num +
1378 * TLSCipherText.type +
1379 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001380 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001381 * IV + // except for TLS 1.0
1382 * ENC(content + padding + padding_length));
1383 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001384 unsigned char pseudo_hdr[13];
1385
1386 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1387 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001388 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1389 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1390
1391 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001392
1393 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1394 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1395 ssl->out_iv, ssl->out_msglen );
1396 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1397 ssl->out_iv + ssl->out_msglen );
1398 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1399
1400 ssl->out_msglen += ssl->transform_out->maclen;
1401 }
1402#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001404 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001405#endif /* POLARSSL_CIPHER_MODE_CBC &&
1406 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001407 {
1408 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001409 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001410 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001411
Paul Bakkerca4ab492012-04-18 14:23:57 +00001412 for( i = 8; i > 0; i-- )
1413 if( ++ssl->out_ctr[i - 1] != 0 )
1414 break;
1415
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001416 /* The loops goes to its end iff the counter is wrapping */
1417 if( i == 0 )
1418 {
1419 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1420 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1421 }
1422
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1424
1425 return( 0 );
1426}
1427
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001428#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001429
Paul Bakker5121ce52009-01-03 21:22:43 +00001430static int ssl_decrypt_buf( ssl_context *ssl )
1431{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001432 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001433 const cipher_mode_t mode = cipher_get_cipher_mode(
1434 &ssl->transform_in->cipher_ctx_dec );
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001435#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001436 size_t padlen = 0, correct = 1;
1437#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001438
1439 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1440
Paul Bakker48916f92012-09-16 19:57:18 +00001441 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
1443 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001444 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001445 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001446 }
1447
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001448#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001449 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001450 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001451 int ret;
1452 size_t olen = 0;
1453
Paul Bakker68884e32013-01-07 18:20:04 +01001454 padlen = 0;
1455
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001456 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001457 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001458 ssl->transform_in->ivlen,
1459 ssl->in_msg, ssl->in_msglen,
1460 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001461 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001462 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463 return( ret );
1464 }
1465
1466 if( ssl->in_msglen != olen )
1467 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001468 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001469 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001470 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001471 }
Paul Bakker68884e32013-01-07 18:20:04 +01001472 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001473#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001474#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1475 if( mode == POLARSSL_MODE_GCM ||
1476 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001477 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001478 int ret;
1479 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001480 unsigned char *dec_msg;
1481 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001482 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001483 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1484 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001485 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1486 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001488 if( ssl->in_msglen < explicit_iv_len + taglen )
1489 {
1490 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1491 "+ taglen (%d)", ssl->in_msglen,
1492 explicit_iv_len, taglen ) );
1493 return( POLARSSL_ERR_SSL_INVALID_MAC );
1494 }
1495 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1496
Paul Bakker68884e32013-01-07 18:20:04 +01001497 dec_msg = ssl->in_msg;
1498 dec_msg_result = ssl->in_msg;
1499 ssl->in_msglen = dec_msglen;
1500
1501 memcpy( add_data, ssl->in_ctr, 8 );
1502 add_data[8] = ssl->in_msgtype;
1503 add_data[9] = ssl->major_ver;
1504 add_data[10] = ssl->minor_ver;
1505 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1506 add_data[12] = ssl->in_msglen & 0xFF;
1507
1508 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1509 add_data, 13 );
1510
1511 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1512 ssl->in_iv,
1513 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1514
1515 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1516 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001517 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001518
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001519 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001520 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001521 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001522 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1523 ssl->transform_in->iv_dec,
1524 ssl->transform_in->ivlen,
1525 add_data, 13,
1526 dec_msg, dec_msglen,
1527 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001528 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001529 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001530 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1531
1532 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1533 return( POLARSSL_ERR_SSL_INVALID_MAC );
1534
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001535 return( ret );
1536 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001537
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001538 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001539 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001540 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001541 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001542 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001545#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001546#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1547 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001548 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001549 {
Paul Bakker45829992013-01-03 14:52:21 +01001550 /*
1551 * Decrypt and check the padding
1552 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001553 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001554 unsigned char *dec_msg;
1555 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001556 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001557 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001558 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001559
Paul Bakker5121ce52009-01-03 21:22:43 +00001560 /*
Paul Bakker45829992013-01-03 14:52:21 +01001561 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001562 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001563#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001564 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1565 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001566#endif
Paul Bakker45829992013-01-03 14:52:21 +01001567
1568 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1569 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1570 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001571 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1572 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1573 ssl->transform_in->ivlen,
1574 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001575 return( POLARSSL_ERR_SSL_INVALID_MAC );
1576 }
1577
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001578 dec_msglen = ssl->in_msglen;
1579 dec_msg = ssl->in_msg;
1580 dec_msg_result = ssl->in_msg;
1581
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001582 /*
1583 * Authenticate before decrypt if enabled
1584 */
1585#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001586 if( ssl_get_mac_order( ssl, ssl->session_in, mode ) == MAC_CIPHERTEXT )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001587 {
1588 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001589 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001590
1591 dec_msglen -= ssl->transform_in->maclen;
1592 ssl->in_msglen -= ssl->transform_in->maclen;
1593
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001594 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1595 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1596 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1597 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1598
1599 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1600
1601 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001602 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1603 ssl->in_iv, ssl->in_msglen );
1604 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1605 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1606
1607 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1608 ssl->transform_in->maclen );
1609 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1610 ssl->transform_in->maclen );
1611
1612 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1613 ssl->transform_in->maclen ) != 0 )
1614 {
1615 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1616
1617 return( POLARSSL_ERR_SSL_INVALID_MAC );
1618 }
1619 }
1620#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1621
1622 /*
1623 * Check length sanity
1624 */
1625 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1626 {
1627 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1628 ssl->in_msglen, ssl->transform_in->ivlen ) );
1629 return( POLARSSL_ERR_SSL_INVALID_MAC );
1630 }
1631
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001632#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001633 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001634 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001635 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001636 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001637 {
Paul Bakker48916f92012-09-16 19:57:18 +00001638 dec_msglen -= ssl->transform_in->ivlen;
1639 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001640
Paul Bakker48916f92012-09-16 19:57:18 +00001641 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001642 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001643 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001644#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001645
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001646 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001647 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001648 ssl->transform_in->ivlen,
1649 dec_msg, dec_msglen,
1650 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001651 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001652 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001653 return( ret );
1654 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001655
Paul Bakkercca5b812013-08-31 17:40:26 +02001656 if( dec_msglen != olen )
1657 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001658 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001659 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001660 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001661
1662#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001663 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1664 {
1665 /*
1666 * Save IV in SSL3 and TLS1
1667 */
1668 memcpy( ssl->transform_in->iv_dec,
1669 ssl->transform_in->cipher_ctx_dec.iv,
1670 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001672#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
1674 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001675
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001676 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001677 ssl_get_mac_order( ssl, ssl->session_in, mode ) == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001678 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001679#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001680 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1681 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001682#endif
Paul Bakker45829992013-01-03 14:52:21 +01001683 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001684 correct = 0;
1685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001686
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001687#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001688 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1689 {
Paul Bakker48916f92012-09-16 19:57:18 +00001690 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001692#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001693 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1694 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001695 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001696#endif
Paul Bakker45829992013-01-03 14:52:21 +01001697 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001698 }
1699 }
1700 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001701#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1703 defined(POLARSSL_SSL_PROTO_TLS1_2)
1704 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 {
1706 /*
Paul Bakker45829992013-01-03 14:52:21 +01001707 * TLSv1+: always check the padding up to the first failure
1708 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001710 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001711 size_t padding_idx = ssl->in_msglen - padlen - 1;
1712
Paul Bakker956c9e02013-12-19 14:42:28 +01001713 /*
1714 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001715 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001716 *
Paul Bakker61885c72014-04-25 12:59:03 +02001717 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1718 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001719 *
1720 * In both cases we reset padding_idx to a safe value (0) to
1721 * prevent out-of-buffer reads.
1722 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001723 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001724 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1725 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001726
1727 padding_idx *= correct;
1728
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001729 for( i = 1; i <= 256; i++ )
1730 {
1731 real_count &= ( i <= padlen );
1732 pad_count += real_count *
1733 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1734 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001735
1736 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001737
Paul Bakkerd66f0702013-01-31 16:57:45 +01001738#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001739 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001740 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001741#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001742 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001744 else
1745#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1746 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001747 {
1748 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001749 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001750 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001751
1752 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001753 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001754 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001755#endif /* POLARSSL_CIPHER_MODE_CBC &&
1756 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001757 {
1758 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001759 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001760 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001761
1762 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1763 ssl->in_msg, ssl->in_msglen );
1764
1765 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001766 * Authenticate if not done yet.
1767 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001769#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1770 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1771 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001772 if( ssl_get_mac_order( ssl, ssl->session_in, mode ) == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001773 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001774 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1775
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001776 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001778 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1779 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001781 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001783#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001784 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1785 {
1786 ssl_mac( &ssl->transform_in->md_ctx_dec,
1787 ssl->transform_in->mac_dec,
1788 ssl->in_msg, ssl->in_msglen,
1789 ssl->in_ctr, ssl->in_msgtype );
1790 }
1791 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001792#endif /* POLARSSL_SSL_PROTO_SSL3 */
1793#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001794 defined(POLARSSL_SSL_PROTO_TLS1_2)
1795 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1796 {
1797 /*
1798 * Process MAC and always update for padlen afterwards to make
1799 * total time independent of padlen
1800 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001801 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001802 *
1803 * Known timing attacks:
1804 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1805 *
1806 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1807 * correctly. (We round down instead of up, so -56 is the correct
1808 * value for our calculations instead of -55)
1809 */
1810 size_t j, extra_run = 0;
1811 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1812 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001813
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001814 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001815
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001816 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1817 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1818 ssl->in_msglen );
1819 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1820 ssl->in_msg + ssl->in_msglen );
1821 for( j = 0; j < extra_run; j++ )
1822 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001823
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001824 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1825 }
1826 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001827#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001828 POLARSSL_SSL_PROTO_TLS1_2 */
1829 {
1830 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001831 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001832 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001834 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1835 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1836 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001838 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001839 ssl->transform_in->maclen ) != 0 )
1840 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001841#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001842 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001843#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001844 correct = 0;
1845 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001847 /*
1848 * Finally check the correct flag
1849 */
1850 if( correct == 0 )
1851 return( POLARSSL_ERR_SSL_INVALID_MAC );
1852 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001853#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 if( ssl->in_msglen == 0 )
1856 {
1857 ssl->nb_zero++;
1858
1859 /*
1860 * Three or more empty messages may be a DoS attack
1861 * (excessive CPU consumption).
1862 */
1863 if( ssl->nb_zero > 3 )
1864 {
1865 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1866 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001867 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868 }
1869 }
1870 else
1871 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001872
Paul Bakker23986e52011-04-24 08:57:21 +00001873 for( i = 8; i > 0; i-- )
1874 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 break;
1876
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001877 /* The loops goes to its end iff the counter is wrapping */
1878 if( i == 0 )
1879 {
1880 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1881 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1882 }
1883
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1885
1886 return( 0 );
1887}
1888
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001889#undef MAC_NONE
1890#undef MAC_PLAINTEXT
1891#undef MAC_CIPHERTEXT
1892
Paul Bakker2770fbd2012-07-03 13:30:23 +00001893#if defined(POLARSSL_ZLIB_SUPPORT)
1894/*
1895 * Compression/decompression functions
1896 */
1897static int ssl_compress_buf( ssl_context *ssl )
1898{
1899 int ret;
1900 unsigned char *msg_post = ssl->out_msg;
1901 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001902 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001903
1904 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1905
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001906 if( len_pre == 0 )
1907 return( 0 );
1908
Paul Bakker2770fbd2012-07-03 13:30:23 +00001909 memcpy( msg_pre, ssl->out_msg, len_pre );
1910
1911 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1912 ssl->out_msglen ) );
1913
1914 SSL_DEBUG_BUF( 4, "before compression: output payload",
1915 ssl->out_msg, ssl->out_msglen );
1916
Paul Bakker48916f92012-09-16 19:57:18 +00001917 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1918 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1919 ssl->transform_out->ctx_deflate.next_out = msg_post;
1920 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001921
Paul Bakker48916f92012-09-16 19:57:18 +00001922 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001923 if( ret != Z_OK )
1924 {
1925 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1926 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1927 }
1928
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001929 ssl->out_msglen = SSL_BUFFER_LEN -
1930 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001931
Paul Bakker2770fbd2012-07-03 13:30:23 +00001932 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1933 ssl->out_msglen ) );
1934
1935 SSL_DEBUG_BUF( 4, "after compression: output payload",
1936 ssl->out_msg, ssl->out_msglen );
1937
1938 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1939
1940 return( 0 );
1941}
1942
1943static int ssl_decompress_buf( ssl_context *ssl )
1944{
1945 int ret;
1946 unsigned char *msg_post = ssl->in_msg;
1947 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001948 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001949
1950 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1951
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001952 if( len_pre == 0 )
1953 return( 0 );
1954
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955 memcpy( msg_pre, ssl->in_msg, len_pre );
1956
1957 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1958 ssl->in_msglen ) );
1959
1960 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1961 ssl->in_msg, ssl->in_msglen );
1962
Paul Bakker48916f92012-09-16 19:57:18 +00001963 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1964 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1965 ssl->transform_in->ctx_inflate.next_out = msg_post;
1966 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001967
Paul Bakker48916f92012-09-16 19:57:18 +00001968 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001969 if( ret != Z_OK )
1970 {
1971 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1972 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1973 }
1974
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001975 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1976 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001977
Paul Bakker2770fbd2012-07-03 13:30:23 +00001978 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1979 ssl->in_msglen ) );
1980
1981 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1982 ssl->in_msg, ssl->in_msglen );
1983
1984 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1985
1986 return( 0 );
1987}
1988#endif /* POLARSSL_ZLIB_SUPPORT */
1989
Paul Bakker5121ce52009-01-03 21:22:43 +00001990/*
1991 * Fill the input message buffer
1992 */
Paul Bakker23986e52011-04-24 08:57:21 +00001993int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001994{
Paul Bakker23986e52011-04-24 08:57:21 +00001995 int ret;
1996 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
1998 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1999
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002000 if( nb_want > SSL_BUFFER_LEN - 8 )
2001 {
2002 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2003 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2004 }
2005
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 while( ssl->in_left < nb_want )
2007 {
2008 len = nb_want - ssl->in_left;
2009 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2010
2011 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2012 ssl->in_left, nb_want ) );
2013 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2014
Paul Bakker831a7552011-05-18 13:32:51 +00002015 if( ret == 0 )
2016 return( POLARSSL_ERR_SSL_CONN_EOF );
2017
Paul Bakker5121ce52009-01-03 21:22:43 +00002018 if( ret < 0 )
2019 return( ret );
2020
2021 ssl->in_left += ret;
2022 }
2023
2024 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2025
2026 return( 0 );
2027}
2028
2029/*
2030 * Flush any data not yet written
2031 */
2032int ssl_flush_output( ssl_context *ssl )
2033{
2034 int ret;
2035 unsigned char *buf;
2036
2037 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2038
2039 while( ssl->out_left > 0 )
2040 {
2041 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2042 5 + ssl->out_msglen, ssl->out_left ) );
2043
Paul Bakker5bd42292012-12-19 14:40:42 +01002044 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002046
Paul Bakker5121ce52009-01-03 21:22:43 +00002047 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2048
2049 if( ret <= 0 )
2050 return( ret );
2051
2052 ssl->out_left -= ret;
2053 }
2054
2055 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2056
2057 return( 0 );
2058}
2059
2060/*
2061 * Record layer functions
2062 */
2063int ssl_write_record( ssl_context *ssl )
2064{
Paul Bakker05ef8352012-05-08 09:17:57 +00002065 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002066 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002067
2068 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2069
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2071 {
2072 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2073 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2074 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2075
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002076 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2077 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 }
2079
Paul Bakker2770fbd2012-07-03 13:30:23 +00002080#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002081 if( ssl->transform_out != NULL &&
2082 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002083 {
2084 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2085 {
2086 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2087 return( ret );
2088 }
2089
2090 len = ssl->out_msglen;
2091 }
2092#endif /*POLARSSL_ZLIB_SUPPORT */
2093
Paul Bakker05ef8352012-05-08 09:17:57 +00002094#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002095 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002097 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002098
Paul Bakker05ef8352012-05-08 09:17:57 +00002099 ret = ssl_hw_record_write( ssl );
2100 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2101 {
2102 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002103 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002104 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002105
2106 if( ret == 0 )
2107 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002108 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002109#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002110 if( !done )
2111 {
2112 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2113 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2114 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2116 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002117
Paul Bakker48916f92012-09-16 19:57:18 +00002118 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002119 {
2120 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2121 {
2122 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2123 return( ret );
2124 }
2125
2126 len = ssl->out_msglen;
2127 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2128 ssl->out_hdr[4] = (unsigned char)( len );
2129 }
2130
2131 ssl->out_left = 5 + ssl->out_msglen;
2132
2133 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2134 "version = [%d:%d], msglen = %d",
2135 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2136 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2137
2138 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002139 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002140 }
2141
Paul Bakker5121ce52009-01-03 21:22:43 +00002142 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2143 {
2144 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2145 return( ret );
2146 }
2147
2148 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2149
2150 return( 0 );
2151}
2152
2153int ssl_read_record( ssl_context *ssl )
2154{
Paul Bakker05ef8352012-05-08 09:17:57 +00002155 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
2157 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2158
2159 if( ssl->in_hslen != 0 &&
2160 ssl->in_hslen < ssl->in_msglen )
2161 {
2162 /*
2163 * Get next Handshake message in the current record
2164 */
2165 ssl->in_msglen -= ssl->in_hslen;
2166
Paul Bakker8934a982011-08-05 11:11:53 +00002167 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002168 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 ssl->in_hslen = 4;
2171 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2172
2173 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2174 " %d, type = %d, hslen = %d",
2175 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2176
2177 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2178 {
2179 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002180 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 }
2182
2183 if( ssl->in_msglen < ssl->in_hslen )
2184 {
2185 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002186 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
2188
Paul Bakker4224bc02014-04-08 14:36:50 +02002189 if( ssl->state != SSL_HANDSHAKE_OVER )
2190 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
2192 return( 0 );
2193 }
2194
2195 ssl->in_hslen = 0;
2196
2197 /*
2198 * Read the record header and validate it
2199 */
2200 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2201 {
2202 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2203 return( ret );
2204 }
2205
2206 ssl->in_msgtype = ssl->in_hdr[0];
2207 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2208
2209 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2210 "version = [%d:%d], msglen = %d",
2211 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2212 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2213
2214 if( ssl->in_hdr[1] != ssl->major_ver )
2215 {
2216 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002217 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 }
2219
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002220 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 {
2222 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002223 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 }
2225
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002226 /* Sanity check (outer boundaries) */
2227 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2228 {
2229 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2230 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2231 }
2232
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002234 * Make sure the message length is acceptable for the current transform
2235 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 */
Paul Bakker48916f92012-09-16 19:57:18 +00002237 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002239 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 {
2241 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002242 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244 }
2245 else
2246 {
Paul Bakker48916f92012-09-16 19:57:18 +00002247 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
2249 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002250 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002253#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002254 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002255 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 {
2257 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002258 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002259 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002260#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002261
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002262#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2263 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 /*
2265 * TLS encrypted messages can have up to 256 bytes of padding
2266 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002267 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002268 ssl->in_msglen > ssl->transform_in->minlen +
2269 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002270 {
2271 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002272 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002274#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276
2277 /*
2278 * Read and optionally decrypt the message contents
2279 */
2280 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2281 {
2282 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2283 return( ret );
2284 }
2285
2286 SSL_DEBUG_BUF( 4, "input record from network",
2287 ssl->in_hdr, 5 + ssl->in_msglen );
2288
Paul Bakker05ef8352012-05-08 09:17:57 +00002289#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002290 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002291 {
2292 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2293
2294 ret = ssl_hw_record_read( ssl );
2295 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2296 {
2297 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002298 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002299 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002300
2301 if( ret == 0 )
2302 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002303 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002304#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002305 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 {
2307 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2308 {
Paul Bakker40865c82013-01-31 17:13:13 +01002309#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2310 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2311 {
2312 ssl_send_alert_message( ssl,
2313 SSL_ALERT_LEVEL_FATAL,
2314 SSL_ALERT_MSG_BAD_RECORD_MAC );
2315 }
2316#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002317 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2318 return( ret );
2319 }
2320
2321 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2322 ssl->in_msg, ssl->in_msglen );
2323
2324 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2325 {
2326 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002327 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002328 }
2329 }
2330
Paul Bakker2770fbd2012-07-03 13:30:23 +00002331#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002332 if( ssl->transform_in != NULL &&
2333 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002334 {
2335 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2336 {
2337 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2338 return( ret );
2339 }
2340
2341 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2342 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2343 }
2344#endif /* POLARSSL_ZLIB_SUPPORT */
2345
Paul Bakker0a925182012-04-16 06:46:41 +00002346 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2347 ssl->in_msgtype != SSL_MSG_ALERT &&
2348 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2349 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2350 {
2351 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2352
Paul Bakker48916f92012-09-16 19:57:18 +00002353 if( ( ret = ssl_send_alert_message( ssl,
2354 SSL_ALERT_LEVEL_FATAL,
2355 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002356 {
2357 return( ret );
2358 }
2359
2360 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2361 }
2362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2364 {
2365 ssl->in_hslen = 4;
2366 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2367
2368 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2369 " %d, type = %d, hslen = %d",
2370 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2371
2372 /*
2373 * Additional checks to validate the handshake header
2374 */
2375 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2376 {
2377 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002378 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379 }
2380
2381 if( ssl->in_msglen < ssl->in_hslen )
2382 {
2383 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002384 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 }
2386
Paul Bakker48916f92012-09-16 19:57:18 +00002387 if( ssl->state != SSL_HANDSHAKE_OVER )
2388 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 }
2390
2391 if( ssl->in_msgtype == SSL_MSG_ALERT )
2392 {
2393 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2394 ssl->in_msg[0], ssl->in_msg[1] ) );
2395
2396 /*
2397 * Ignore non-fatal alerts, except close_notify
2398 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002399 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002401 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2402 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002403 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 }
2405
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2407 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
2409 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002410 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 }
2412 }
2413
2414 ssl->in_left = 0;
2415
2416 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2417
2418 return( 0 );
2419}
2420
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002421int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2422{
2423 int ret;
2424
2425 if( ( ret = ssl_send_alert_message( ssl,
2426 SSL_ALERT_LEVEL_FATAL,
2427 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2428 {
2429 return( ret );
2430 }
2431
2432 return( 0 );
2433}
2434
Paul Bakker0a925182012-04-16 06:46:41 +00002435int ssl_send_alert_message( ssl_context *ssl,
2436 unsigned char level,
2437 unsigned char message )
2438{
2439 int ret;
2440
2441 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2442
2443 ssl->out_msgtype = SSL_MSG_ALERT;
2444 ssl->out_msglen = 2;
2445 ssl->out_msg[0] = level;
2446 ssl->out_msg[1] = message;
2447
2448 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2449 {
2450 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2451 return( ret );
2452 }
2453
2454 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2455
2456 return( 0 );
2457}
2458
Paul Bakker5121ce52009-01-03 21:22:43 +00002459/*
2460 * Handshake functions
2461 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002462#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2463 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2464 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2465 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2466 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2467 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2468 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002469int ssl_write_certificate( ssl_context *ssl )
2470{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002471 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
2473 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2474
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002476 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2477 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002478 {
2479 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2480 ssl->state++;
2481 return( 0 );
2482 }
2483
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002484 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2485 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486}
2487
2488int ssl_parse_certificate( ssl_context *ssl )
2489{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2491
2492 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2493
2494 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002495 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497 {
2498 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2499 ssl->state++;
2500 return( 0 );
2501 }
2502
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002503 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2504 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505}
2506#else
2507int ssl_write_certificate( ssl_context *ssl )
2508{
2509 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2510 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002511 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2513
2514 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2515
2516 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002517 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2518 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 {
2520 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2521 ssl->state++;
2522 return( 0 );
2523 }
2524
Paul Bakker5121ce52009-01-03 21:22:43 +00002525 if( ssl->endpoint == SSL_IS_CLIENT )
2526 {
2527 if( ssl->client_auth == 0 )
2528 {
2529 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2530 ssl->state++;
2531 return( 0 );
2532 }
2533
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002534#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 /*
2536 * If using SSLv3 and got no cert, send an Alert message
2537 * (otherwise an empty Certificate message will be sent).
2538 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002539 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2541 {
2542 ssl->out_msglen = 2;
2543 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002544 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2545 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
2547 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2548 goto write_msg;
2549 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002550#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 }
2552 else /* SSL_IS_SERVER */
2553 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002554 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 {
2556 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002557 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 }
2559 }
2560
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002561 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 /*
2564 * 0 . 0 handshake type
2565 * 1 . 3 handshake length
2566 * 4 . 6 length of all certs
2567 * 7 . 9 length of cert. 1
2568 * 10 . n-1 peer certificate
2569 * n . n+2 length of cert. 2
2570 * n+3 . ... upper level cert, etc.
2571 */
2572 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002573 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Paul Bakker29087132010-03-21 21:03:34 +00002575 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 {
2577 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002578 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002579 {
2580 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2581 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002582 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
2585 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2586 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2587 ssl->out_msg[i + 2] = (unsigned char)( n );
2588
2589 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2590 i += n; crt = crt->next;
2591 }
2592
2593 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2594 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2595 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2596
2597 ssl->out_msglen = i;
2598 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2599 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2600
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002601#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002602write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002603#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002604
2605 ssl->state++;
2606
2607 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2608 {
2609 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2610 return( ret );
2611 }
2612
2613 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2614
Paul Bakkered27a042013-04-18 22:46:23 +02002615 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002616}
2617
2618int ssl_parse_certificate( ssl_context *ssl )
2619{
Paul Bakkered27a042013-04-18 22:46:23 +02002620 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002621 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002622 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002623
2624 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2625
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002626 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002627 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2628 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002629 {
2630 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2631 ssl->state++;
2632 return( 0 );
2633 }
2634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002636 ( ssl->authmode == SSL_VERIFY_NONE ||
2637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002638 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002639 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2641 ssl->state++;
2642 return( 0 );
2643 }
2644
2645 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2646 {
2647 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2648 return( ret );
2649 }
2650
2651 ssl->state++;
2652
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002653#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 /*
2655 * Check if the client sent an empty certificate
2656 */
2657 if( ssl->endpoint == SSL_IS_SERVER &&
2658 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2659 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002660 if( ssl->in_msglen == 2 &&
2661 ssl->in_msgtype == SSL_MSG_ALERT &&
2662 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2663 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002664 {
2665 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2666
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002667 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2669 return( 0 );
2670 else
Paul Bakker40e46942009-01-03 21:51:57 +00002671 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002672 }
2673 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002674#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002676#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2677 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 if( ssl->endpoint == SSL_IS_SERVER &&
2679 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2680 {
2681 if( ssl->in_hslen == 7 &&
2682 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2683 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2684 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2687
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002688 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002689 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002690 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 else
2692 return( 0 );
2693 }
2694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002695#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2696 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002697
2698 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002701 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 }
2703
2704 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2705 {
2706 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002707 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 }
2709
2710 /*
2711 * Same message structure as in ssl_write_certificate()
2712 */
2713 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2714
2715 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2716 {
2717 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002718 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 }
2720
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002721 /* In case we tried to reuse a session but it failed */
2722 if( ssl->session_negotiate->peer_cert != NULL )
2723 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002724 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002725 polarssl_free( ssl->session_negotiate->peer_cert );
2726 }
2727
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002728 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2729 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 {
2731 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002732 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002733 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
Paul Bakkerb6b09562013-09-18 14:17:41 +02002736 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002737
2738 i = 7;
2739
2740 while( i < ssl->in_hslen )
2741 {
2742 if( ssl->in_msg[i] != 0 )
2743 {
2744 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002745 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002746 }
2747
2748 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2749 | (unsigned int) ssl->in_msg[i + 2];
2750 i += 3;
2751
2752 if( n < 128 || i + n > ssl->in_hslen )
2753 {
2754 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002755 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756 }
2757
Paul Bakkerddf26b42013-09-18 13:46:23 +02002758 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2759 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 if( ret != 0 )
2761 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002762 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 return( ret );
2764 }
2765
2766 i += n;
2767 }
2768
Paul Bakker48916f92012-09-16 19:57:18 +00002769 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002771 /*
2772 * On client, make sure the server cert doesn't change during renego to
2773 * avoid "triple handshake" attack: https://secure-resumption.com/
2774 */
2775 if( ssl->endpoint == SSL_IS_CLIENT &&
2776 ssl->renegotiation == SSL_RENEGOTIATION )
2777 {
2778 if( ssl->session->peer_cert == NULL )
2779 {
2780 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2781 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2782 }
2783
2784 if( ssl->session->peer_cert->raw.len !=
2785 ssl->session_negotiate->peer_cert->raw.len ||
2786 memcmp( ssl->session->peer_cert->raw.p,
2787 ssl->session_negotiate->peer_cert->raw.p,
2788 ssl->session->peer_cert->raw.len ) != 0 )
2789 {
2790 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2791 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2792 }
2793 }
2794
Paul Bakker5121ce52009-01-03 21:22:43 +00002795 if( ssl->authmode != SSL_VERIFY_NONE )
2796 {
2797 if( ssl->ca_chain == NULL )
2798 {
2799 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002800 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002801 }
2802
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002803 /*
2804 * Main check: verify certificate
2805 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002806 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2807 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2808 &ssl->session_negotiate->verify_result,
2809 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002810
2811 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002812 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002813 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002814 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002815
2816 /*
2817 * Secondary checks: always done, but change 'ret' only if it was 0
2818 */
2819
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002820#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002821 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002822 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002823
2824 /* If certificate uses an EC key, make sure the curve is OK */
2825 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2826 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2827 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002828 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002829 if( ret == 0 )
2830 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002831 }
2832 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002833#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002834
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002835 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2836 ciphersuite_info,
2837 ! ssl->endpoint ) != 0 )
2838 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002839 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002840 if( ret == 0 )
2841 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2842 }
2843
Paul Bakker5121ce52009-01-03 21:22:43 +00002844 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2845 ret = 0;
2846 }
2847
2848 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2849
2850 return( ret );
2851}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002852#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2853 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2854 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2855 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2856 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2857 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2858 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002859
2860int ssl_write_change_cipher_spec( ssl_context *ssl )
2861{
2862 int ret;
2863
2864 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2865
2866 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2867 ssl->out_msglen = 1;
2868 ssl->out_msg[0] = 1;
2869
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 ssl->state++;
2871
2872 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2873 {
2874 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2875 return( ret );
2876 }
2877
2878 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2879
2880 return( 0 );
2881}
2882
2883int ssl_parse_change_cipher_spec( ssl_context *ssl )
2884{
2885 int ret;
2886
2887 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2888
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2890 {
2891 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2892 return( ret );
2893 }
2894
2895 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2896 {
2897 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002898 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 }
2900
2901 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2902 {
2903 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002904 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 }
2906
2907 ssl->state++;
2908
2909 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2910
2911 return( 0 );
2912}
2913
Paul Bakker41c83d32013-03-20 14:39:14 +01002914void ssl_optimize_checksum( ssl_context *ssl,
2915 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002916{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002917 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002918
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002919#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2920 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002921 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002922 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002923 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924#endif
2925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2926#if defined(POLARSSL_SHA512_C)
2927 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2928 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2929 else
2930#endif
2931#if defined(POLARSSL_SHA256_C)
2932 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002933 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002934 else
2935#endif
2936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002937 {
2938 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002939 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002940 }
Paul Bakker380da532012-04-18 16:10:25 +00002941}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002942
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002943static void ssl_update_checksum_start( ssl_context *ssl,
2944 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002945{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002948 md5_update( &ssl->handshake->fin_md5 , buf, len );
2949 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002950#endif
2951#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2952#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002953 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002954#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002955#if defined(POLARSSL_SHA512_C)
2956 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002957#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002958#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002959}
2960
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2962 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002963static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2964 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002965{
Paul Bakker48916f92012-09-16 19:57:18 +00002966 md5_update( &ssl->handshake->fin_md5 , buf, len );
2967 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002968}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002969#endif
Paul Bakker380da532012-04-18 16:10:25 +00002970
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002971#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2972#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002973static void ssl_update_checksum_sha256( ssl_context *ssl,
2974 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002975{
Paul Bakker9e36f042013-06-30 14:34:05 +02002976 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002977}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002978#endif
Paul Bakker380da532012-04-18 16:10:25 +00002979
Paul Bakker9e36f042013-06-30 14:34:05 +02002980#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002981static void ssl_update_checksum_sha384( ssl_context *ssl,
2982 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002983{
Paul Bakker9e36f042013-06-30 14:34:05 +02002984 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002985}
Paul Bakker769075d2012-11-24 11:26:46 +01002986#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002987#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002988
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002989#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990static void ssl_calc_finished_ssl(
2991 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002992{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002993 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 md5_context md5;
2995 sha1_context sha1;
2996
Paul Bakker5121ce52009-01-03 21:22:43 +00002997 unsigned char padbuf[48];
2998 unsigned char md5sum[16];
2999 unsigned char sha1sum[20];
3000
Paul Bakker48916f92012-09-16 19:57:18 +00003001 ssl_session *session = ssl->session_negotiate;
3002 if( !session )
3003 session = ssl->session;
3004
Paul Bakker1ef83d62012-04-11 12:09:53 +00003005 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3006
Paul Bakker48916f92012-09-16 19:57:18 +00003007 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3008 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
3010 /*
3011 * SSLv3:
3012 * hash =
3013 * MD5( master + pad2 +
3014 * MD5( handshake + sender + master + pad1 ) )
3015 * + SHA1( master + pad2 +
3016 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003017 */
3018
Paul Bakker90995b52013-06-24 19:20:35 +02003019#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003020 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003022#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker90995b52013-06-24 19:20:35 +02003024#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003025 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003028
Paul Bakker3c2122f2013-06-24 19:03:14 +02003029 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3030 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003031
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003033
Paul Bakker3c2122f2013-06-24 19:03:14 +02003034 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003035 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036 md5_update( &md5, padbuf, 48 );
3037 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003038
Paul Bakker3c2122f2013-06-24 19:03:14 +02003039 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003040 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003041 sha1_update( &sha1, padbuf, 40 );
3042 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Paul Bakker1ef83d62012-04-11 12:09:53 +00003044 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003045
Paul Bakker1ef83d62012-04-11 12:09:53 +00003046 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003047 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 md5_update( &md5, padbuf, 48 );
3049 md5_update( &md5, md5sum, 16 );
3050 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003051
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003053 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054 sha1_update( &sha1, padbuf , 40 );
3055 sha1_update( &sha1, sha1sum, 20 );
3056 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003057
Paul Bakker1ef83d62012-04-11 12:09:53 +00003058 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003059
Paul Bakker5b4af392014-06-26 12:09:34 +02003060 md5_free( &md5 );
3061 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakker34617722014-06-13 17:20:13 +02003063 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3064 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3065 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003066
3067 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3068}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003069#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003071#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072static void ssl_calc_finished_tls(
3073 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003074{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003076 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003077 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Paul Bakker48916f92012-09-16 19:57:18 +00003081 ssl_session *session = ssl->session_negotiate;
3082 if( !session )
3083 session = ssl->session;
3084
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker48916f92012-09-16 19:57:18 +00003087 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3088 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker1ef83d62012-04-11 12:09:53 +00003090 /*
3091 * TLSv1:
3092 * hash = PRF( master, finished_label,
3093 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3094 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Paul Bakker90995b52013-06-24 19:20:35 +02003096#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003097 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3098 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003099#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100
Paul Bakker90995b52013-06-24 19:20:35 +02003101#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3103 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003104#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003105
3106 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003107 ? "client finished"
3108 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003109
3110 md5_finish( &md5, padbuf );
3111 sha1_finish( &sha1, padbuf + 16 );
3112
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003113 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003114 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003115
3116 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3117
Paul Bakker5b4af392014-06-26 12:09:34 +02003118 md5_free( &md5 );
3119 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120
Paul Bakker34617722014-06-13 17:20:13 +02003121 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122
3123 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3124}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003125#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003126
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003127#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3128#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003129static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130 ssl_context *ssl, unsigned char *buf, int from )
3131{
3132 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003133 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003134 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003135 unsigned char padbuf[32];
3136
Paul Bakker48916f92012-09-16 19:57:18 +00003137 ssl_session *session = ssl->session_negotiate;
3138 if( !session )
3139 session = ssl->session;
3140
Paul Bakker380da532012-04-18 16:10:25 +00003141 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142
Paul Bakker9e36f042013-06-30 14:34:05 +02003143 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003144
3145 /*
3146 * TLSv1.2:
3147 * hash = PRF( master, finished_label,
3148 * Hash( handshake ) )[0.11]
3149 */
3150
Paul Bakker9e36f042013-06-30 14:34:05 +02003151#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003152 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003153 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003154#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003155
3156 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003157 ? "client finished"
3158 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159
Paul Bakker9e36f042013-06-30 14:34:05 +02003160 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003161
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003162 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003163 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003164
3165 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3166
Paul Bakker5b4af392014-06-26 12:09:34 +02003167 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168
Paul Bakker34617722014-06-13 17:20:13 +02003169 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003170
3171 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3172}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003173#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003174
Paul Bakker9e36f042013-06-30 14:34:05 +02003175#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003176static void ssl_calc_finished_tls_sha384(
3177 ssl_context *ssl, unsigned char *buf, int from )
3178{
3179 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003180 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003181 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003182 unsigned char padbuf[48];
3183
Paul Bakker48916f92012-09-16 19:57:18 +00003184 ssl_session *session = ssl->session_negotiate;
3185 if( !session )
3186 session = ssl->session;
3187
Paul Bakker380da532012-04-18 16:10:25 +00003188 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003189
Paul Bakker9e36f042013-06-30 14:34:05 +02003190 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003191
3192 /*
3193 * TLSv1.2:
3194 * hash = PRF( master, finished_label,
3195 * Hash( handshake ) )[0.11]
3196 */
3197
Paul Bakker9e36f042013-06-30 14:34:05 +02003198#if !defined(POLARSSL_SHA512_ALT)
3199 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3200 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003201#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003202
3203 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003204 ? "client finished"
3205 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003206
Paul Bakker9e36f042013-06-30 14:34:05 +02003207 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003208
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003209 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003210 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003211
3212 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3213
Paul Bakker5b4af392014-06-26 12:09:34 +02003214 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003215
Paul Bakker34617722014-06-13 17:20:13 +02003216 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003217
3218 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3219}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003220#endif /* POLARSSL_SHA512_C */
3221#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003222
Paul Bakker48916f92012-09-16 19:57:18 +00003223void ssl_handshake_wrapup( ssl_context *ssl )
3224{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003225 int resume = ssl->handshake->resume;
3226
Paul Bakker48916f92012-09-16 19:57:18 +00003227 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3228
3229 /*
3230 * Free our handshake params
3231 */
3232 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003233 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003234 ssl->handshake = NULL;
3235
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003236 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003237 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003238 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003239 ssl->renego_records_seen = 0;
3240 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003241
Paul Bakker48916f92012-09-16 19:57:18 +00003242 /*
3243 * Switch in our now active transform context
3244 */
3245 if( ssl->transform )
3246 {
3247 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003248 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003249 }
3250 ssl->transform = ssl->transform_negotiate;
3251 ssl->transform_negotiate = NULL;
3252
Paul Bakker0a597072012-09-25 21:55:46 +00003253 if( ssl->session )
3254 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003255#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3256 /* RFC 7366 3.1: keep the EtM state */
3257 ssl->session_negotiate->encrypt_then_mac =
3258 ssl->session->encrypt_then_mac;
3259#endif
3260
Paul Bakker0a597072012-09-25 21:55:46 +00003261 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003262 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003263 }
3264 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003265 ssl->session_negotiate = NULL;
3266
Paul Bakker0a597072012-09-25 21:55:46 +00003267 /*
3268 * Add cache entry
3269 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003270 if( ssl->f_set_cache != NULL &&
3271 ssl->session->length != 0 &&
3272 resume == 0 )
3273 {
Paul Bakker0a597072012-09-25 21:55:46 +00003274 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3275 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003276 }
Paul Bakker0a597072012-09-25 21:55:46 +00003277
Paul Bakker48916f92012-09-16 19:57:18 +00003278 ssl->state++;
3279
3280 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3281}
3282
Paul Bakker1ef83d62012-04-11 12:09:53 +00003283int ssl_write_finished( ssl_context *ssl )
3284{
3285 int ret, hash_len;
3286
3287 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3288
Paul Bakker92be97b2013-01-02 17:30:03 +01003289 /*
3290 * Set the out_msg pointer to the correct location based on IV length
3291 */
3292 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3293 {
3294 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3295 ssl->transform_negotiate->fixed_ivlen;
3296 }
3297 else
3298 ssl->out_msg = ssl->out_iv;
3299
Paul Bakker48916f92012-09-16 19:57:18 +00003300 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003301
3302 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3304
Paul Bakker48916f92012-09-16 19:57:18 +00003305 ssl->verify_data_len = hash_len;
3306 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3307
Paul Bakker5121ce52009-01-03 21:22:43 +00003308 ssl->out_msglen = 4 + hash_len;
3309 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3310 ssl->out_msg[0] = SSL_HS_FINISHED;
3311
3312 /*
3313 * In case of session resuming, invert the client and server
3314 * ChangeCipherSpec messages order.
3315 */
Paul Bakker0a597072012-09-25 21:55:46 +00003316 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003317 {
3318 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003319 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003320 else
3321 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3322 }
3323 else
3324 ssl->state++;
3325
Paul Bakker48916f92012-09-16 19:57:18 +00003326 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003327 * Switch to our negotiated transform and session parameters for outbound
3328 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003329 */
3330 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3331 ssl->transform_out = ssl->transform_negotiate;
3332 ssl->session_out = ssl->session_negotiate;
3333 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Paul Bakker07eb38b2012-12-19 14:42:06 +01003335#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003336 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003337 {
3338 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3339 {
3340 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3341 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3342 }
3343 }
3344#endif
3345
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3347 {
3348 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3349 return( ret );
3350 }
3351
3352 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3353
3354 return( 0 );
3355}
3356
3357int ssl_parse_finished( ssl_context *ssl )
3358{
Paul Bakker23986e52011-04-24 08:57:21 +00003359 int ret;
3360 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003361 unsigned char buf[36];
3362
3363 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3364
Paul Bakker48916f92012-09-16 19:57:18 +00003365 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003366
Paul Bakker48916f92012-09-16 19:57:18 +00003367 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003368 * Switch to our negotiated transform and session parameters for inbound
3369 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003370 */
3371 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3372 ssl->transform_in = ssl->transform_negotiate;
3373 ssl->session_in = ssl->session_negotiate;
3374 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
Paul Bakker92be97b2013-01-02 17:30:03 +01003376 /*
3377 * Set the in_msg pointer to the correct location based on IV length
3378 */
3379 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3380 {
3381 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3382 ssl->transform_negotiate->fixed_ivlen;
3383 }
3384 else
3385 ssl->in_msg = ssl->in_iv;
3386
Paul Bakker07eb38b2012-12-19 14:42:06 +01003387#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003388 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003389 {
3390 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3391 {
3392 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3393 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3394 }
3395 }
3396#endif
3397
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3399 {
3400 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3401 return( ret );
3402 }
3403
3404 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3405 {
3406 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003407 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 }
3409
Paul Bakker1ef83d62012-04-11 12:09:53 +00003410 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3412
3413 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3414 ssl->in_hslen != 4 + hash_len )
3415 {
3416 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003417 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003418 }
3419
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003420 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003421 {
3422 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003423 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003424 }
3425
Paul Bakker48916f92012-09-16 19:57:18 +00003426 ssl->verify_data_len = hash_len;
3427 memcpy( ssl->peer_verify_data, buf, hash_len );
3428
Paul Bakker0a597072012-09-25 21:55:46 +00003429 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003430 {
3431 if( ssl->endpoint == SSL_IS_CLIENT )
3432 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3433
3434 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003435 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003436 }
3437 else
3438 ssl->state++;
3439
3440 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3441
3442 return( 0 );
3443}
3444
Paul Bakker968afaa2014-07-09 11:09:24 +02003445static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003446{
3447 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3448
3449#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3450 defined(POLARSSL_SSL_PROTO_TLS1_1)
3451 md5_init( &handshake->fin_md5 );
3452 sha1_init( &handshake->fin_sha1 );
3453 md5_starts( &handshake->fin_md5 );
3454 sha1_starts( &handshake->fin_sha1 );
3455#endif
3456#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3457#if defined(POLARSSL_SHA256_C)
3458 sha256_init( &handshake->fin_sha256 );
3459 sha256_starts( &handshake->fin_sha256, 0 );
3460#endif
3461#if defined(POLARSSL_SHA512_C)
3462 sha512_init( &handshake->fin_sha512 );
3463 sha512_starts( &handshake->fin_sha512, 1 );
3464#endif
3465#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3466
3467 handshake->update_checksum = ssl_update_checksum_start;
3468 handshake->sig_alg = SSL_HASH_SHA1;
3469
3470#if defined(POLARSSL_DHM_C)
3471 dhm_init( &handshake->dhm_ctx );
3472#endif
3473#if defined(POLARSSL_ECDH_C)
3474 ecdh_init( &handshake->ecdh_ctx );
3475#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003476}
3477
3478static void ssl_transform_init( ssl_transform *transform )
3479{
3480 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003481
3482 cipher_init( &transform->cipher_ctx_enc );
3483 cipher_init( &transform->cipher_ctx_dec );
3484
3485 md_init( &transform->md_ctx_enc );
3486 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003487}
3488
3489void ssl_session_init( ssl_session *session )
3490{
3491 memset( session, 0, sizeof(ssl_session) );
3492}
3493
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003494static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003495{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003496 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003497 if( ssl->transform_negotiate )
3498 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003499 if( ssl->session_negotiate )
3500 ssl_session_free( ssl->session_negotiate );
3501 if( ssl->handshake )
3502 ssl_handshake_free( ssl->handshake );
3503
3504 /*
3505 * Either the pointers are now NULL or cleared properly and can be freed.
3506 * Now allocate missing structures.
3507 */
3508 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003509 {
3510 ssl->transform_negotiate =
3511 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3512 }
Paul Bakker48916f92012-09-16 19:57:18 +00003513
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003514 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003515 {
3516 ssl->session_negotiate =
3517 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3518 }
Paul Bakker48916f92012-09-16 19:57:18 +00003519
Paul Bakker82788fb2014-10-20 13:59:19 +02003520 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003521 {
3522 ssl->handshake = (ssl_handshake_params *)
3523 polarssl_malloc( sizeof(ssl_handshake_params) );
3524 }
Paul Bakker48916f92012-09-16 19:57:18 +00003525
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003526 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003527 if( ssl->handshake == NULL ||
3528 ssl->transform_negotiate == NULL ||
3529 ssl->session_negotiate == NULL )
3530 {
3531 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003532
3533 polarssl_free( ssl->handshake );
3534 polarssl_free( ssl->transform_negotiate );
3535 polarssl_free( ssl->session_negotiate );
3536
3537 ssl->handshake = NULL;
3538 ssl->transform_negotiate = NULL;
3539 ssl->session_negotiate = NULL;
3540
Paul Bakker48916f92012-09-16 19:57:18 +00003541 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3542 }
3543
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003544 /* Initialize structures */
3545 ssl_session_init( ssl->session_negotiate );
3546 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003547 ssl_handshake_params_init( ssl->handshake );
3548
3549#if defined(POLARSSL_X509_CRT_PARSE_C)
3550 ssl->handshake->key_cert = ssl->key_cert;
3551#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003552
Paul Bakker48916f92012-09-16 19:57:18 +00003553 return( 0 );
3554}
3555
Paul Bakker5121ce52009-01-03 21:22:43 +00003556/*
3557 * Initialize an SSL context
3558 */
3559int ssl_init( ssl_context *ssl )
3560{
Paul Bakker48916f92012-09-16 19:57:18 +00003561 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003562 int len = SSL_BUFFER_LEN;
3563
3564 memset( ssl, 0, sizeof( ssl_context ) );
3565
Paul Bakker62f2dee2012-09-28 07:31:51 +00003566 /*
3567 * Sane defaults
3568 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003569 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3570 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3571 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3572 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003573
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003574 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003575
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003576 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3577
Paul Bakker62f2dee2012-09-28 07:31:51 +00003578#if defined(POLARSSL_DHM_C)
3579 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3580 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3581 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3582 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3583 {
3584 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3585 return( ret );
3586 }
3587#endif
3588
3589 /*
3590 * Prepare base structures
3591 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003592 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003593 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003594 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 ssl->in_msg = ssl->in_ctr + 13;
3596
3597 if( ssl->in_ctr == NULL )
3598 {
3599 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003600 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 }
3602
Paul Bakker6e339b52013-07-03 13:37:05 +02003603 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003604 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003605 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003606 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003607
3608 if( ssl->out_ctr == NULL )
3609 {
3610 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003611 polarssl_free( ssl->in_ctr );
3612 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003613 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003614 }
3615
3616 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3617 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3618
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003619#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3620 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3621#endif
3622
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003623#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3624 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3625#endif
3626
Paul Bakker606b4ba2013-08-14 16:52:14 +02003627#if defined(POLARSSL_SSL_SESSION_TICKETS)
3628 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3629#endif
3630
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003631#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003632 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003633#endif
3634
Paul Bakker48916f92012-09-16 19:57:18 +00003635 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637
3638 return( 0 );
3639}
3640
3641/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003642 * Reset an initialized and used SSL context for re-use while retaining
3643 * all application-set variables, function pointers and data.
3644 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003645int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003646{
Paul Bakker48916f92012-09-16 19:57:18 +00003647 int ret;
3648
Paul Bakker7eb013f2011-10-06 12:37:39 +00003649 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003650 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3651 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3652
3653 ssl->verify_data_len = 0;
3654 memset( ssl->own_verify_data, 0, 36 );
3655 memset( ssl->peer_verify_data, 0, 36 );
3656
Paul Bakker7eb013f2011-10-06 12:37:39 +00003657 ssl->in_offt = NULL;
3658
Paul Bakker92be97b2013-01-02 17:30:03 +01003659 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003660 ssl->in_msgtype = 0;
3661 ssl->in_msglen = 0;
3662 ssl->in_left = 0;
3663
3664 ssl->in_hslen = 0;
3665 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003666 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003667
Paul Bakker92be97b2013-01-02 17:30:03 +01003668 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003669 ssl->out_msgtype = 0;
3670 ssl->out_msglen = 0;
3671 ssl->out_left = 0;
3672
Paul Bakker48916f92012-09-16 19:57:18 +00003673 ssl->transform_in = NULL;
3674 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003675
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003676 ssl->renego_records_seen = 0;
3677
Paul Bakker7eb013f2011-10-06 12:37:39 +00003678 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3679 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003680
3681#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003682 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003683 {
3684 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003685 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003686 {
3687 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3688 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3689 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003690 }
3691#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003692
Paul Bakker48916f92012-09-16 19:57:18 +00003693 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003694 {
Paul Bakker48916f92012-09-16 19:57:18 +00003695 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003696 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003697 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003698 }
Paul Bakker48916f92012-09-16 19:57:18 +00003699
Paul Bakkerc0463502013-02-14 11:19:38 +01003700 if( ssl->session )
3701 {
3702 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003703 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003704 ssl->session = NULL;
3705 }
3706
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003707#if defined(POLARSSL_SSL_ALPN)
3708 ssl->alpn_chosen = NULL;
3709#endif
3710
Paul Bakker48916f92012-09-16 19:57:18 +00003711 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3712 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003713
3714 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003715}
3716
Paul Bakkera503a632013-08-14 13:48:06 +02003717#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003718static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3719{
3720 aes_free( &tkeys->enc );
3721 aes_free( &tkeys->dec );
3722
3723 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3724}
3725
Paul Bakker7eb013f2011-10-06 12:37:39 +00003726/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003727 * Allocate and initialize ticket keys
3728 */
3729static int ssl_ticket_keys_init( ssl_context *ssl )
3730{
3731 int ret;
3732 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003733 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003734
3735 if( ssl->ticket_keys != NULL )
3736 return( 0 );
3737
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003738 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3739 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003740 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3741
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003742 aes_init( &tkeys->enc );
3743 aes_init( &tkeys->dec );
3744
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003745 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003746 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003747 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003748 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003749 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003750 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003751
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003752 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3753 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3754 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3755 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003756 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003757 polarssl_free( tkeys );
3758 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003759 }
3760
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003761 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003762 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003763 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003764 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003765 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003766 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003767
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003768 ssl->ticket_keys = tkeys;
3769
3770 return( 0 );
3771}
Paul Bakkera503a632013-08-14 13:48:06 +02003772#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003773
3774/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003775 * SSL set accessors
3776 */
3777void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3778{
3779 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003780
Paul Bakker606b4ba2013-08-14 16:52:14 +02003781#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003782 if( endpoint == SSL_IS_CLIENT )
3783 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003784#endif
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +01003785
3786#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
3787 if( endpoint == SSL_IS_SERVER )
3788 ssl->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
3789#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003790}
3791
3792void ssl_set_authmode( ssl_context *ssl, int authmode )
3793{
3794 ssl->authmode = authmode;
3795}
3796
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003797#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003798void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003799 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003800 void *p_vrfy )
3801{
3802 ssl->f_vrfy = f_vrfy;
3803 ssl->p_vrfy = p_vrfy;
3804}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003805#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003806
Paul Bakker5121ce52009-01-03 21:22:43 +00003807void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003808 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 void *p_rng )
3810{
3811 ssl->f_rng = f_rng;
3812 ssl->p_rng = p_rng;
3813}
3814
3815void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003816 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003817 void *p_dbg )
3818{
3819 ssl->f_dbg = f_dbg;
3820 ssl->p_dbg = p_dbg;
3821}
3822
3823void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003824 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003825 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003826{
3827 ssl->f_recv = f_recv;
3828 ssl->f_send = f_send;
3829 ssl->p_recv = p_recv;
3830 ssl->p_send = p_send;
3831}
3832
Paul Bakker0a597072012-09-25 21:55:46 +00003833void ssl_set_session_cache( ssl_context *ssl,
3834 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3835 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003836{
Paul Bakker0a597072012-09-25 21:55:46 +00003837 ssl->f_get_cache = f_get_cache;
3838 ssl->p_get_cache = p_get_cache;
3839 ssl->f_set_cache = f_set_cache;
3840 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003841}
3842
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003843int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003844{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003845 int ret;
3846
3847 if( ssl == NULL ||
3848 session == NULL ||
3849 ssl->session_negotiate == NULL ||
3850 ssl->endpoint != SSL_IS_CLIENT )
3851 {
3852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3853 }
3854
3855 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3856 return( ret );
3857
Paul Bakker0a597072012-09-25 21:55:46 +00003858 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003859
3860 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003861}
3862
Paul Bakkerb68cad62012-08-23 08:34:18 +00003863void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003864{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003865 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3866 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3867 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3868 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3869}
3870
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003871void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3872 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003873 int major, int minor )
3874{
3875 if( major != SSL_MAJOR_VERSION_3 )
3876 return;
3877
3878 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3879 return;
3880
3881 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003882}
3883
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003884#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003885/* Add a new (empty) key_cert entry an return a pointer to it */
3886static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3887{
3888 ssl_key_cert *key_cert, *last;
3889
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003890 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3891 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003892 return( NULL );
3893
3894 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3895
3896 /* Append the new key_cert to the (possibly empty) current list */
3897 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003898 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003899 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003900 if( ssl->handshake != NULL )
3901 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003902 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003903 else
3904 {
3905 last = ssl->key_cert;
3906 while( last->next != NULL )
3907 last = last->next;
3908 last->next = key_cert;
3909 }
3910
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003911 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003912}
3913
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003914void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003915 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003916{
3917 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003918 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 ssl->peer_cn = peer_cn;
3920}
3921
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003922int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003923 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003924{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003925 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3926
3927 if( key_cert == NULL )
3928 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3929
3930 key_cert->cert = own_cert;
3931 key_cert->key = pk_key;
3932
3933 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003934}
3935
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003936#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003937int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003938 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003939{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003940 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003941 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003942
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003943 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003944 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3945
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003946 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3947 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003948 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003949
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003950 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003951
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003952 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003953 if( ret != 0 )
3954 return( ret );
3955
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003956 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003957 return( ret );
3958
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003959 key_cert->cert = own_cert;
3960 key_cert->key_own_alloc = 1;
3961
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003962 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003963}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003964#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003965
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003966int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003967 void *rsa_key,
3968 rsa_decrypt_func rsa_decrypt,
3969 rsa_sign_func rsa_sign,
3970 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003971{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003972 int ret;
3973 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003974
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003976 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3977
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003978 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3979 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003980 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003981
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003982 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003983
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003984 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3985 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3986 return( ret );
3987
3988 key_cert->cert = own_cert;
3989 key_cert->key_own_alloc = 1;
3990
3991 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003992}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003993#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003994
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003995#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003996int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3997 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003998{
Paul Bakker6db455e2013-09-18 17:29:31 +02003999 if( psk == NULL || psk_identity == NULL )
4000 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4001
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004002 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004003 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4004
Paul Bakker6db455e2013-09-18 17:29:31 +02004005 if( ssl->psk != NULL )
4006 {
4007 polarssl_free( ssl->psk );
4008 polarssl_free( ssl->psk_identity );
4009 }
4010
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004011 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004012 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004013
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004014 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004015 ssl->psk_identity = (unsigned char *)
4016 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004017
4018 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4019 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4020
4021 memcpy( ssl->psk, psk, ssl->psk_len );
4022 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004023
4024 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004025}
4026
4027void ssl_set_psk_cb( ssl_context *ssl,
4028 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4029 size_t),
4030 void *p_psk )
4031{
4032 ssl->f_psk = f_psk;
4033 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004034}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004035#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004036
Paul Bakker48916f92012-09-16 19:57:18 +00004037#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004038int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004039{
4040 int ret;
4041
Paul Bakker48916f92012-09-16 19:57:18 +00004042 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004043 {
4044 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4045 return( ret );
4046 }
4047
Paul Bakker48916f92012-09-16 19:57:18 +00004048 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004049 {
4050 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4051 return( ret );
4052 }
4053
4054 return( 0 );
4055}
4056
Paul Bakker1b57b062011-01-06 15:48:19 +00004057int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4058{
4059 int ret;
4060
Paul Bakker66d5d072014-06-17 16:39:18 +02004061 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004062 {
4063 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4064 return( ret );
4065 }
4066
Paul Bakker66d5d072014-06-17 16:39:18 +02004067 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004068 {
4069 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4070 return( ret );
4071 }
4072
4073 return( 0 );
4074}
Paul Bakker48916f92012-09-16 19:57:18 +00004075#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004076
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004077#if defined(POLARSSL_SSL_SET_CURVES)
4078/*
4079 * Set the allowed elliptic curves
4080 */
4081void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4082{
4083 ssl->curve_list = curve_list;
4084}
4085#endif
4086
Paul Bakker0be444a2013-08-27 21:55:01 +02004087#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004088int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004089{
4090 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004091 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004092
4093 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004094
4095 if( ssl->hostname_len + 1 == 0 )
4096 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4097
Paul Bakker6e339b52013-07-03 13:37:05 +02004098 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004099
Paul Bakkerb15b8512012-01-13 13:44:06 +00004100 if( ssl->hostname == NULL )
4101 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4102
Paul Bakker3c2122f2013-06-24 19:03:14 +02004103 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004104 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004105
Paul Bakker40ea7de2009-05-03 10:18:48 +00004106 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004107
4108 return( 0 );
4109}
4110
Paul Bakker5701cdc2012-09-27 21:49:42 +00004111void ssl_set_sni( ssl_context *ssl,
4112 int (*f_sni)(void *, ssl_context *,
4113 const unsigned char *, size_t),
4114 void *p_sni )
4115{
4116 ssl->f_sni = f_sni;
4117 ssl->p_sni = p_sni;
4118}
Paul Bakker0be444a2013-08-27 21:55:01 +02004119#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004120
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004121#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004122int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004123{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004124 size_t cur_len, tot_len;
4125 const char **p;
4126
4127 /*
4128 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4129 * truncated". Check lengths now rather than later.
4130 */
4131 tot_len = 0;
4132 for( p = protos; *p != NULL; p++ )
4133 {
4134 cur_len = strlen( *p );
4135 tot_len += cur_len;
4136
4137 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4138 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4139 }
4140
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004141 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004142
4143 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004144}
4145
4146const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4147{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004148 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004149}
4150#endif /* POLARSSL_SSL_ALPN */
4151
Paul Bakker490ecc82011-10-06 13:04:09 +00004152void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4153{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004154 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4155 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4156 {
4157 ssl->max_major_ver = major;
4158 ssl->max_minor_ver = minor;
4159 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004160}
4161
Paul Bakker1d29fb52012-09-28 13:28:45 +00004162void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4163{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004164 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4165 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4166 {
4167 ssl->min_major_ver = major;
4168 ssl->min_minor_ver = minor;
4169 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004170}
4171
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004172#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4173void ssl_set_fallback( ssl_context *ssl, char fallback )
4174{
4175 ssl->fallback = fallback;
4176}
4177#endif
4178
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004179#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4180void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4181{
4182 ssl->encrypt_then_mac = etm;
4183}
4184#endif
4185
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004186#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4187void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4188{
4189 ssl->extended_ms = ems;
4190}
4191#endif
4192
Paul Bakker05decb22013-08-15 13:33:48 +02004193#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004194int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4195{
Paul Bakker77e257e2013-12-16 15:29:52 +01004196 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004197 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004198 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004199 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004200 }
4201
4202 ssl->mfl_code = mfl_code;
4203
4204 return( 0 );
4205}
Paul Bakker05decb22013-08-15 13:33:48 +02004206#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004207
Paul Bakker1f2bc622013-08-15 13:45:55 +02004208#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004209int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004210{
Paul Bakker8c1ede62013-07-19 14:14:37 +02004211 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004212
4213 return( 0 );
4214}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004215#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004216
Paul Bakker48916f92012-09-16 19:57:18 +00004217void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4218{
4219 ssl->disable_renegotiation = renegotiation;
4220}
4221
4222void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4223{
4224 ssl->allow_legacy_renegotiation = allow_legacy;
4225}
4226
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004227void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4228{
4229 ssl->renego_max_records = max_records;
4230}
4231
Paul Bakkera503a632013-08-14 13:48:06 +02004232#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004233int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4234{
4235 ssl->session_tickets = use_tickets;
4236
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004237 if( ssl->endpoint == SSL_IS_CLIENT )
4238 return( 0 );
4239
4240 if( ssl->f_rng == NULL )
4241 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4242
4243 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004244}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004245
4246void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4247{
4248 ssl->ticket_lifetime = lifetime;
4249}
Paul Bakkera503a632013-08-14 13:48:06 +02004250#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004251
Paul Bakker5121ce52009-01-03 21:22:43 +00004252/*
4253 * SSL get accessors
4254 */
Paul Bakker23986e52011-04-24 08:57:21 +00004255size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004256{
4257 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4258}
4259
Paul Bakkerff60ee62010-03-16 21:09:09 +00004260int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004261{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004262 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004263}
4264
Paul Bakkere3166ce2011-01-27 17:40:50 +00004265const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004266{
Paul Bakker926c8e42013-03-06 10:23:34 +01004267 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004268 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004269
Paul Bakkere3166ce2011-01-27 17:40:50 +00004270 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004271}
4272
Paul Bakker43ca69c2011-01-15 17:35:19 +00004273const char *ssl_get_version( const ssl_context *ssl )
4274{
4275 switch( ssl->minor_ver )
4276 {
4277 case SSL_MINOR_VERSION_0:
4278 return( "SSLv3.0" );
4279
4280 case SSL_MINOR_VERSION_1:
4281 return( "TLSv1.0" );
4282
4283 case SSL_MINOR_VERSION_2:
4284 return( "TLSv1.1" );
4285
Paul Bakker1ef83d62012-04-11 12:09:53 +00004286 case SSL_MINOR_VERSION_3:
4287 return( "TLSv1.2" );
4288
Paul Bakker43ca69c2011-01-15 17:35:19 +00004289 default:
4290 break;
4291 }
4292 return( "unknown" );
4293}
4294
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004295#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004296const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004297{
4298 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004299 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004300
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004301 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004302}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004303#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004304
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004305int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4306{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004307 if( ssl == NULL ||
4308 dst == NULL ||
4309 ssl->session == NULL ||
4310 ssl->endpoint != SSL_IS_CLIENT )
4311 {
4312 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4313 }
4314
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004315 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004316}
4317
Paul Bakker5121ce52009-01-03 21:22:43 +00004318/*
Paul Bakker1961b702013-01-25 14:49:24 +01004319 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004320 */
Paul Bakker1961b702013-01-25 14:49:24 +01004321int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004322{
Paul Bakker40e46942009-01-03 21:51:57 +00004323 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004324
Paul Bakker40e46942009-01-03 21:51:57 +00004325#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004326 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004327 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004328#endif
4329
Paul Bakker40e46942009-01-03 21:51:57 +00004330#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004331 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004332 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004333#endif
4334
Paul Bakker1961b702013-01-25 14:49:24 +01004335 return( ret );
4336}
4337
4338/*
4339 * Perform the SSL handshake
4340 */
4341int ssl_handshake( ssl_context *ssl )
4342{
4343 int ret = 0;
4344
4345 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4346
4347 while( ssl->state != SSL_HANDSHAKE_OVER )
4348 {
4349 ret = ssl_handshake_step( ssl );
4350
4351 if( ret != 0 )
4352 break;
4353 }
4354
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4356
4357 return( ret );
4358}
4359
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004360#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004361/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004362 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004363 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004364static int ssl_write_hello_request( ssl_context *ssl )
4365{
4366 int ret;
4367
4368 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4369
4370 ssl->out_msglen = 4;
4371 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4372 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4373
4374 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4375 {
4376 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4377 return( ret );
4378 }
4379
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004380 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4381
4382 return( 0 );
4383}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004384#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004385
4386/*
4387 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004388 * - any side: calling ssl_renegotiate(),
4389 * - client: receiving a HelloRequest during ssl_read(),
4390 * - server: receiving any handshake message on server during ssl_read() after
4391 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004392 * If the handshake doesn't complete due to waiting for I/O, it will continue
4393 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004394 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004395static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004396{
4397 int ret;
4398
4399 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4400
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004401 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4402 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004403
4404 ssl->state = SSL_HELLO_REQUEST;
4405 ssl->renegotiation = SSL_RENEGOTIATION;
4406
Paul Bakker48916f92012-09-16 19:57:18 +00004407 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4408 {
4409 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4410 return( ret );
4411 }
4412
4413 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4414
4415 return( 0 );
4416}
4417
4418/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004419 * Renegotiate current connection on client,
4420 * or request renegotiation on server
4421 */
4422int ssl_renegotiate( ssl_context *ssl )
4423{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004424 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004425
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004426#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004427 /* On server, just send the request */
4428 if( ssl->endpoint == SSL_IS_SERVER )
4429 {
4430 if( ssl->state != SSL_HANDSHAKE_OVER )
4431 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4432
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004433 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4434
4435 /* Did we already try/start sending HelloRequest? */
4436 if( ssl->out_left != 0 )
4437 return( ssl_flush_output( ssl ) );
4438
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004439 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004440 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004441#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004442
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004443#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004444 /*
4445 * On client, either start the renegotiation process or,
4446 * if already in progress, continue the handshake
4447 */
4448 if( ssl->renegotiation != SSL_RENEGOTIATION )
4449 {
4450 if( ssl->state != SSL_HANDSHAKE_OVER )
4451 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4452
4453 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4454 {
4455 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4456 return( ret );
4457 }
4458 }
4459 else
4460 {
4461 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4462 {
4463 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4464 return( ret );
4465 }
4466 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004467#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004468
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004469 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004470}
4471
4472/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004473 * Receive application data decrypted from the SSL layer
4474 */
Paul Bakker23986e52011-04-24 08:57:21 +00004475int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004476{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004477 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004478 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004479
4480 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4481
4482 if( ssl->state != SSL_HANDSHAKE_OVER )
4483 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004484 ret = ssl_handshake( ssl );
4485 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4486 {
4487 record_read = 1;
4488 }
4489 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004490 {
4491 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4492 return( ret );
4493 }
4494 }
4495
4496 if( ssl->in_offt == NULL )
4497 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004498 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004499 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004500 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4501 {
4502 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4503 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004504
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004505 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4506 return( ret );
4507 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004508 }
4509
4510 if( ssl->in_msglen == 0 &&
4511 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4512 {
4513 /*
4514 * OpenSSL sends empty messages to randomize the IV
4515 */
4516 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4517 {
Paul Bakker831a7552011-05-18 13:32:51 +00004518 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4519 return( 0 );
4520
Paul Bakker5121ce52009-01-03 21:22:43 +00004521 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4522 return( ret );
4523 }
4524 }
4525
Paul Bakker48916f92012-09-16 19:57:18 +00004526 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4527 {
4528 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4529
4530 if( ssl->endpoint == SSL_IS_CLIENT &&
4531 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4532 ssl->in_hslen != 4 ) )
4533 {
4534 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4535 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4536 }
4537
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004538 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4539 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004540 ssl->allow_legacy_renegotiation ==
4541 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004542 {
4543 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4544
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004545#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004546 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004547 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004548 /*
4549 * SSLv3 does not have a "no_renegotiation" alert
4550 */
4551 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4552 return( ret );
4553 }
4554 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004555#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004556#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4557 defined(POLARSSL_SSL_PROTO_TLS1_2)
4558 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004559 {
4560 if( ( ret = ssl_send_alert_message( ssl,
4561 SSL_ALERT_LEVEL_WARNING,
4562 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4563 {
4564 return( ret );
4565 }
Paul Bakker48916f92012-09-16 19:57:18 +00004566 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004567 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004568#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4569 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004570 {
4571 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004572 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004573 }
Paul Bakker48916f92012-09-16 19:57:18 +00004574 }
4575 else
4576 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004577 ret = ssl_start_renegotiation( ssl );
4578 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4579 {
4580 record_read = 1;
4581 }
4582 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004583 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004584 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004585 return( ret );
4586 }
Paul Bakker48916f92012-09-16 19:57:18 +00004587 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004588
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004589 /* If a non-handshake record was read during renego, fallthrough,
4590 * else tell the user they should call ssl_read() again */
4591 if( ! record_read )
4592 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004593 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004594 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4595 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004596 ssl->renego_records_seen++;
4597
4598 if( ssl->renego_max_records >= 0 &&
4599 ssl->renego_records_seen > ssl->renego_max_records )
4600 {
4601 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4602 "but not honored by client" ) );
4603 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4604 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004605 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004606
4607 /* Fatal and closure alerts handled by ssl_read_record() */
4608 if( ssl->in_msgtype == SSL_MSG_ALERT )
4609 {
4610 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4611 return( POLARSSL_ERR_NET_WANT_READ );
4612 }
4613
4614 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 {
4616 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004617 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004618 }
4619
4620 ssl->in_offt = ssl->in_msg;
4621 }
4622
4623 n = ( len < ssl->in_msglen )
4624 ? len : ssl->in_msglen;
4625
4626 memcpy( buf, ssl->in_offt, n );
4627 ssl->in_msglen -= n;
4628
4629 if( ssl->in_msglen == 0 )
4630 /* all bytes consumed */
4631 ssl->in_offt = NULL;
4632 else
4633 /* more data available */
4634 ssl->in_offt += n;
4635
4636 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4637
Paul Bakker23986e52011-04-24 08:57:21 +00004638 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004639}
4640
4641/*
4642 * Send application data to be encrypted by the SSL layer
4643 */
Paul Bakker23986e52011-04-24 08:57:21 +00004644int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004645{
Paul Bakker23986e52011-04-24 08:57:21 +00004646 int ret;
4647 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004648 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004649
4650 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4651
4652 if( ssl->state != SSL_HANDSHAKE_OVER )
4653 {
4654 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4655 {
4656 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4657 return( ret );
4658 }
4659 }
4660
Paul Bakker05decb22013-08-15 13:33:48 +02004661#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004662 /*
4663 * Assume mfl_code is correct since it was checked when set
4664 */
4665 max_len = mfl_code_to_length[ssl->mfl_code];
4666
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004667 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004668 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004669 */
4670 if( ssl->session_out != NULL &&
4671 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4672 {
4673 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4674 }
Paul Bakker05decb22013-08-15 13:33:48 +02004675#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004676
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004677 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004678
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 if( ssl->out_left != 0 )
4680 {
4681 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4682 {
4683 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4684 return( ret );
4685 }
4686 }
Paul Bakker887bd502011-06-08 13:10:54 +00004687 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004688 {
Paul Bakker887bd502011-06-08 13:10:54 +00004689 ssl->out_msglen = n;
4690 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4691 memcpy( ssl->out_msg, buf, n );
4692
4693 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4694 {
4695 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4696 return( ret );
4697 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004698 }
4699
4700 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4701
Paul Bakker23986e52011-04-24 08:57:21 +00004702 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703}
4704
4705/*
4706 * Notify the peer that the connection is being closed
4707 */
4708int ssl_close_notify( ssl_context *ssl )
4709{
4710 int ret;
4711
4712 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4713
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004714 if( ssl->out_left != 0 )
4715 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004716
4717 if( ssl->state == SSL_HANDSHAKE_OVER )
4718 {
Paul Bakker48916f92012-09-16 19:57:18 +00004719 if( ( ret = ssl_send_alert_message( ssl,
4720 SSL_ALERT_LEVEL_WARNING,
4721 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004722 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004723 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004724 return( ret );
4725 }
4726 }
4727
4728 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4729
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004730 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004731}
4732
Paul Bakker48916f92012-09-16 19:57:18 +00004733void ssl_transform_free( ssl_transform *transform )
4734{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004735 if( transform == NULL )
4736 return;
4737
Paul Bakker48916f92012-09-16 19:57:18 +00004738#if defined(POLARSSL_ZLIB_SUPPORT)
4739 deflateEnd( &transform->ctx_deflate );
4740 inflateEnd( &transform->ctx_inflate );
4741#endif
4742
Paul Bakker84bbeb52014-07-01 14:53:22 +02004743 cipher_free( &transform->cipher_ctx_enc );
4744 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004745
Paul Bakker84bbeb52014-07-01 14:53:22 +02004746 md_free( &transform->md_ctx_enc );
4747 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004748
Paul Bakker34617722014-06-13 17:20:13 +02004749 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004750}
4751
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004752#if defined(POLARSSL_X509_CRT_PARSE_C)
4753static void ssl_key_cert_free( ssl_key_cert *key_cert )
4754{
4755 ssl_key_cert *cur = key_cert, *next;
4756
4757 while( cur != NULL )
4758 {
4759 next = cur->next;
4760
4761 if( cur->key_own_alloc )
4762 {
4763 pk_free( cur->key );
4764 polarssl_free( cur->key );
4765 }
4766 polarssl_free( cur );
4767
4768 cur = next;
4769 }
4770}
4771#endif /* POLARSSL_X509_CRT_PARSE_C */
4772
Paul Bakker48916f92012-09-16 19:57:18 +00004773void ssl_handshake_free( ssl_handshake_params *handshake )
4774{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004775 if( handshake == NULL )
4776 return;
4777
Paul Bakker48916f92012-09-16 19:57:18 +00004778#if defined(POLARSSL_DHM_C)
4779 dhm_free( &handshake->dhm_ctx );
4780#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004781#if defined(POLARSSL_ECDH_C)
4782 ecdh_free( &handshake->ecdh_ctx );
4783#endif
4784
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004785#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004786 /* explicit void pointer cast for buggy MS compiler */
4787 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004788#endif
4789
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004790#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4791 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4792 /*
4793 * Free only the linked list wrapper, not the keys themselves
4794 * since the belong to the SNI callback
4795 */
4796 if( handshake->sni_key_cert != NULL )
4797 {
4798 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4799
4800 while( cur != NULL )
4801 {
4802 next = cur->next;
4803 polarssl_free( cur );
4804 cur = next;
4805 }
4806 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004807#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004808
Paul Bakker34617722014-06-13 17:20:13 +02004809 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004810}
4811
4812void ssl_session_free( ssl_session *session )
4813{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004814 if( session == NULL )
4815 return;
4816
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004817#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004818 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004819 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004820 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004821 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004822 }
Paul Bakkered27a042013-04-18 22:46:23 +02004823#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004824
Paul Bakkera503a632013-08-14 13:48:06 +02004825#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004826 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004827#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004828
Paul Bakker34617722014-06-13 17:20:13 +02004829 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004830}
4831
Paul Bakker5121ce52009-01-03 21:22:43 +00004832/*
4833 * Free an SSL context
4834 */
4835void ssl_free( ssl_context *ssl )
4836{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004837 if( ssl == NULL )
4838 return;
4839
Paul Bakker5121ce52009-01-03 21:22:43 +00004840 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4841
Paul Bakker5121ce52009-01-03 21:22:43 +00004842 if( ssl->out_ctr != NULL )
4843 {
Paul Bakker34617722014-06-13 17:20:13 +02004844 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004845 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 }
4847
4848 if( ssl->in_ctr != NULL )
4849 {
Paul Bakker34617722014-06-13 17:20:13 +02004850 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004851 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 }
4853
Paul Bakker16770332013-10-11 09:59:44 +02004854#if defined(POLARSSL_ZLIB_SUPPORT)
4855 if( ssl->compress_buf != NULL )
4856 {
Paul Bakker34617722014-06-13 17:20:13 +02004857 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004858 polarssl_free( ssl->compress_buf );
4859 }
4860#endif
4861
Paul Bakker40e46942009-01-03 21:51:57 +00004862#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004863 mpi_free( &ssl->dhm_P );
4864 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004865#endif
4866
Paul Bakker48916f92012-09-16 19:57:18 +00004867 if( ssl->transform )
4868 {
4869 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004870 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004871 }
4872
4873 if( ssl->handshake )
4874 {
4875 ssl_handshake_free( ssl->handshake );
4876 ssl_transform_free( ssl->transform_negotiate );
4877 ssl_session_free( ssl->session_negotiate );
4878
Paul Bakker6e339b52013-07-03 13:37:05 +02004879 polarssl_free( ssl->handshake );
4880 polarssl_free( ssl->transform_negotiate );
4881 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004882 }
4883
Paul Bakkerc0463502013-02-14 11:19:38 +01004884 if( ssl->session )
4885 {
4886 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004887 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004888 }
4889
Paul Bakkera503a632013-08-14 13:48:06 +02004890#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004891 if( ssl->ticket_keys )
4892 {
4893 ssl_ticket_keys_free( ssl->ticket_keys );
4894 polarssl_free( ssl->ticket_keys );
4895 }
Paul Bakkera503a632013-08-14 13:48:06 +02004896#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004897
Paul Bakker0be444a2013-08-27 21:55:01 +02004898#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004899 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004900 {
Paul Bakker34617722014-06-13 17:20:13 +02004901 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004902 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 ssl->hostname_len = 0;
4904 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004905#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004906
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004907#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004908 if( ssl->psk != NULL )
4909 {
Paul Bakker34617722014-06-13 17:20:13 +02004910 polarssl_zeroize( ssl->psk, ssl->psk_len );
4911 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004912 polarssl_free( ssl->psk );
4913 polarssl_free( ssl->psk_identity );
4914 ssl->psk_len = 0;
4915 ssl->psk_identity_len = 0;
4916 }
4917#endif
4918
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004919#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004920 ssl_key_cert_free( ssl->key_cert );
4921#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004922
Paul Bakker05ef8352012-05-08 09:17:57 +00004923#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4924 if( ssl_hw_record_finish != NULL )
4925 {
4926 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4927 ssl_hw_record_finish( ssl );
4928 }
4929#endif
4930
Paul Bakker5121ce52009-01-03 21:22:43 +00004931 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004932
Paul Bakker86f04f42013-02-14 11:20:09 +01004933 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004934 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004935}
4936
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004937#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004938/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004939 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004940 */
4941unsigned char ssl_sig_from_pk( pk_context *pk )
4942{
4943#if defined(POLARSSL_RSA_C)
4944 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4945 return( SSL_SIG_RSA );
4946#endif
4947#if defined(POLARSSL_ECDSA_C)
4948 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4949 return( SSL_SIG_ECDSA );
4950#endif
4951 return( SSL_SIG_ANON );
4952}
4953
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004954pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4955{
4956 switch( sig )
4957 {
4958#if defined(POLARSSL_RSA_C)
4959 case SSL_SIG_RSA:
4960 return( POLARSSL_PK_RSA );
4961#endif
4962#if defined(POLARSSL_ECDSA_C)
4963 case SSL_SIG_ECDSA:
4964 return( POLARSSL_PK_ECDSA );
4965#endif
4966 default:
4967 return( POLARSSL_PK_NONE );
4968 }
4969}
Paul Bakker9af723c2014-05-01 13:03:14 +02004970#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004971
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004972/*
4973 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4974 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004975md_type_t ssl_md_alg_from_hash( unsigned char hash )
4976{
4977 switch( hash )
4978 {
4979#if defined(POLARSSL_MD5_C)
4980 case SSL_HASH_MD5:
4981 return( POLARSSL_MD_MD5 );
4982#endif
4983#if defined(POLARSSL_SHA1_C)
4984 case SSL_HASH_SHA1:
4985 return( POLARSSL_MD_SHA1 );
4986#endif
4987#if defined(POLARSSL_SHA256_C)
4988 case SSL_HASH_SHA224:
4989 return( POLARSSL_MD_SHA224 );
4990 case SSL_HASH_SHA256:
4991 return( POLARSSL_MD_SHA256 );
4992#endif
4993#if defined(POLARSSL_SHA512_C)
4994 case SSL_HASH_SHA384:
4995 return( POLARSSL_MD_SHA384 );
4996 case SSL_HASH_SHA512:
4997 return( POLARSSL_MD_SHA512 );
4998#endif
4999 default:
5000 return( POLARSSL_MD_NONE );
5001 }
5002}
5003
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005004#if defined(POLARSSL_SSL_SET_CURVES)
5005/*
5006 * Check is a curve proposed by the peer is in our list.
5007 * Return 1 if we're willing to use it, 0 otherwise.
5008 */
5009int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5010{
5011 const ecp_group_id *gid;
5012
5013 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5014 if( *gid == grp_id )
5015 return( 1 );
5016
5017 return( 0 );
5018}
Paul Bakker9af723c2014-05-01 13:03:14 +02005019#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005020
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005021#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005022int ssl_check_cert_usage( const x509_crt *cert,
5023 const ssl_ciphersuite_t *ciphersuite,
5024 int cert_endpoint )
5025{
5026#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5027 int usage = 0;
5028#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005029#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5030 const char *ext_oid;
5031 size_t ext_len;
5032#endif
5033
5034#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5035 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5036 ((void) cert);
5037 ((void) cert_endpoint);
5038#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005039
5040#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5041 if( cert_endpoint == SSL_IS_SERVER )
5042 {
5043 /* Server part of the key exchange */
5044 switch( ciphersuite->key_exchange )
5045 {
5046 case POLARSSL_KEY_EXCHANGE_RSA:
5047 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5048 usage = KU_KEY_ENCIPHERMENT;
5049 break;
5050
5051 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5052 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5053 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5054 usage = KU_DIGITAL_SIGNATURE;
5055 break;
5056
5057 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5058 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5059 usage = KU_KEY_AGREEMENT;
5060 break;
5061
5062 /* Don't use default: we want warnings when adding new values */
5063 case POLARSSL_KEY_EXCHANGE_NONE:
5064 case POLARSSL_KEY_EXCHANGE_PSK:
5065 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5066 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5067 usage = 0;
5068 }
5069 }
5070 else
5071 {
5072 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5073 usage = KU_DIGITAL_SIGNATURE;
5074 }
5075
5076 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5077 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005078#else
5079 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005080#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5081
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005082#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5083 if( cert_endpoint == SSL_IS_SERVER )
5084 {
5085 ext_oid = OID_SERVER_AUTH;
5086 ext_len = OID_SIZE( OID_SERVER_AUTH );
5087 }
5088 else
5089 {
5090 ext_oid = OID_CLIENT_AUTH;
5091 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5092 }
5093
5094 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5095 return( -1 );
5096#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5097
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005098 return( 0 );
5099}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005100#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005101
5102#endif /* POLARSSL_SSL_TLS_C */