blob: 5ffb35ef7973602a896bc5c37daa7182264e20a2 [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:
604 * first multiple of blocklen greater than maclen
605 * + IV except for SSL3 and TLS 1.0
606 */
607 transform->minlen = transform->maclen
608 + cipher_info->block_size
609 - transform->maclen % cipher_info->block_size;
610
611#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
612 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
613 ssl->minor_ver == SSL_MINOR_VERSION_1 )
614 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100615 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200616#endif
617#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
618 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
619 ssl->minor_ver == SSL_MINOR_VERSION_3 )
620 {
621 transform->minlen += transform->ivlen;
622 }
623 else
624#endif
625 {
626 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
627 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
628 }
Paul Bakker68884e32013-01-07 18:20:04 +0100629 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
632 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000633 transform->keylen, transform->minlen, transform->ivlen,
634 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 /*
637 * Finally setup the cipher contexts, IVs and MAC secrets.
638 */
639 if( ssl->endpoint == SSL_IS_CLIENT )
640 {
Paul Bakker48916f92012-09-16 19:57:18 +0000641 key1 = keyblk + transform->maclen * 2;
642 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Paul Bakker68884e32013-01-07 18:20:04 +0100644 mac_enc = keyblk;
645 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000647 /*
648 * This is not used in TLS v1.1.
649 */
Paul Bakker48916f92012-09-16 19:57:18 +0000650 iv_copy_len = ( transform->fixed_ivlen ) ?
651 transform->fixed_ivlen : transform->ivlen;
652 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
653 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
656 else
657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 key1 = keyblk + transform->maclen * 2 + transform->keylen;
659 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Paul Bakker68884e32013-01-07 18:20:04 +0100661 mac_enc = keyblk + transform->maclen;
662 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000664 /*
665 * This is not used in TLS v1.1.
666 */
Paul Bakker48916f92012-09-16 19:57:18 +0000667 iv_copy_len = ( transform->fixed_ivlen ) ?
668 transform->fixed_ivlen : transform->ivlen;
669 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
670 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
673
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100675 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
676 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100677 if( transform->maclen > sizeof transform->mac_enc )
678 {
679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100681 }
682
Paul Bakker68884e32013-01-07 18:20:04 +0100683 memcpy( transform->mac_enc, mac_enc, transform->maclen );
684 memcpy( transform->mac_dec, mac_dec, transform->maclen );
685 }
686 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
689 defined(POLARSSL_SSL_PROTO_TLS1_2)
690 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100691 {
692 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
693 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695 else
696#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200700 }
Paul Bakker68884e32013-01-07 18:20:04 +0100701
Paul Bakker05ef8352012-05-08 09:17:57 +0000702#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200703 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000704 {
705 int ret = 0;
706
707 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
708
Paul Bakker07eb38b2012-12-19 14:42:06 +0100709 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
710 transform->iv_enc, transform->iv_dec,
711 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100712 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100713 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000714 {
715 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200716 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000717 }
718 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200719#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000720
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200721 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
722 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200724 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
725 return( ret );
726 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200727
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
729 cipher_info ) ) != 0 )
730 {
731 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
732 return( ret );
733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200734
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
736 cipher_info->key_length,
737 POLARSSL_ENCRYPT ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
740 return( ret );
741 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200742
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
744 cipher_info->key_length,
745 POLARSSL_DECRYPT ) ) != 0 )
746 {
747 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
748 return( ret );
749 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200750
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200751#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( cipher_info->mode == POLARSSL_MODE_CBC )
753 {
754 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
755 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200756 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200757 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
758 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200759 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760
761 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
762 POLARSSL_PADDING_NONE ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
765 return( ret );
766 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Paul Bakker34617722014-06-13 17:20:13 +0200770 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772#if defined(POLARSSL_ZLIB_SUPPORT)
773 // Initialize compression
774 //
Paul Bakker48916f92012-09-16 19:57:18 +0000775 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000776 {
Paul Bakker16770332013-10-11 09:59:44 +0200777 if( ssl->compress_buf == NULL )
778 {
779 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
780 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
781 if( ssl->compress_buf == NULL )
782 {
783 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
784 SSL_BUFFER_LEN ) );
785 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
786 }
787 }
788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
792 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200794 if( deflateInit( &transform->ctx_deflate,
795 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000796 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000797 {
798 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
799 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
800 }
801 }
802#endif /* POLARSSL_ZLIB_SUPPORT */
803
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
805
806 return( 0 );
807}
808
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000810void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000811{
812 md5_context md5;
813 sha1_context sha1;
814 unsigned char pad_1[48];
815 unsigned char pad_2[48];
816
Paul Bakker380da532012-04-18 16:10:25 +0000817 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Paul Bakker48916f92012-09-16 19:57:18 +0000819 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
820 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakker380da532012-04-18 16:10:25 +0000822 memset( pad_1, 0x36, 48 );
823 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakker48916f92012-09-16 19:57:18 +0000825 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000826 md5_update( &md5, pad_1, 48 );
827 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker380da532012-04-18 16:10:25 +0000829 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000830 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000831 md5_update( &md5, pad_2, 48 );
832 md5_update( &md5, hash, 16 );
833 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker48916f92012-09-16 19:57:18 +0000835 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000836 sha1_update( &sha1, pad_1, 40 );
837 sha1_finish( &sha1, hash + 16 );
838
839 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000840 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000841 sha1_update( &sha1, pad_2, 40 );
842 sha1_update( &sha1, hash + 16, 20 );
843 sha1_finish( &sha1, hash + 16 );
844
845 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
846 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
847
Paul Bakker5b4af392014-06-26 12:09:34 +0200848 md5_free( &md5 );
849 sha1_free( &sha1 );
850
Paul Bakker380da532012-04-18 16:10:25 +0000851 return;
852}
Paul Bakker9af723c2014-05-01 13:03:14 +0200853#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000854
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
857{
858 md5_context md5;
859 sha1_context sha1;
860
861 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
862
Paul Bakker48916f92012-09-16 19:57:18 +0000863 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
864 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakker48916f92012-09-16 19:57:18 +0000866 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000867 sha1_finish( &sha1, hash + 16 );
868
869 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
870 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
871
Paul Bakker5b4af392014-06-26 12:09:34 +0200872 md5_free( &md5 );
873 sha1_free( &sha1 );
874
Paul Bakker380da532012-04-18 16:10:25 +0000875 return;
876}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200877#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000878
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200879#if defined(POLARSSL_SSL_PROTO_TLS1_2)
880#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000881void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
882{
Paul Bakker9e36f042013-06-30 14:34:05 +0200883 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000884
885 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
886
Paul Bakker9e36f042013-06-30 14:34:05 +0200887 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
888 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000889
890 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
891 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
892
Paul Bakker5b4af392014-06-26 12:09:34 +0200893 sha256_free( &sha256 );
894
Paul Bakker380da532012-04-18 16:10:25 +0000895 return;
896}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000898
Paul Bakker9e36f042013-06-30 14:34:05 +0200899#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
907 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakkerca4ab492012-04-18 14:23:57 +0000909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha512_free( &sha512 );
913
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA512_C */
917#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200919#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
921{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200922 unsigned char *p = ssl->handshake->premaster;
923 unsigned char *end = p + sizeof( ssl->handshake->premaster );
924
925 /*
926 * PMS = struct {
927 * opaque other_secret<0..2^16-1>;
928 * opaque psk<0..2^16-1>;
929 * };
930 * with "other_secret" depending on the particular key exchange
931 */
932#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
934 {
935 if( end - p < 2 + (int) ssl->psk_len )
936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
937
938 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
939 *(p++) = (unsigned char)( ssl->psk_len );
940 p += ssl->psk_len;
941 }
942 else
943#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200944#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
945 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
946 {
947 /*
948 * other_secret already set by the ClientKeyExchange message,
949 * and is 48 bytes long
950 */
951 *p++ = 0;
952 *p++ = 48;
953 p += 48;
954 }
955 else
956#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
958 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
959 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200960 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200961 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200963 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200965 p + 2, &len,
966 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200967 {
968 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
969 return( ret );
970 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200971 *(p++) = (unsigned char)( len >> 8 );
972 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 p += len;
974
975 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
979#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 size_t zlen;
984
985 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200986 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200987 ssl->f_rng, ssl->p_rng ) ) != 0 )
988 {
989 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
990 return( ret );
991 }
992
993 *(p++) = (unsigned char)( zlen >> 8 );
994 *(p++) = (unsigned char)( zlen );
995 p += zlen;
996
997 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1001 {
1002 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001003 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 }
1005
1006 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001007 if( end - p < 2 + (int) ssl->psk_len )
1008 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1009
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1011 *(p++) = (unsigned char)( ssl->psk_len );
1012 memcpy( p, ssl->psk, ssl->psk_len );
1013 p += ssl->psk_len;
1014
1015 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1016
1017 return( 0 );
1018}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001019#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001022/*
1023 * SSLv3.0 MAC functions
1024 */
Paul Bakker68884e32013-01-07 18:20:04 +01001025static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1026 unsigned char *buf, size_t len,
1027 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001028{
1029 unsigned char header[11];
1030 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001031 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001032 int md_size = md_get_size( md_ctx->md_info );
1033 int md_type = md_get_type( md_ctx->md_info );
1034
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001035 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001036 if( md_type == POLARSSL_MD_MD5 )
1037 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001038 else
Paul Bakker68884e32013-01-07 18:20:04 +01001039 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
1041 memcpy( header, ctr, 8 );
1042 header[ 8] = (unsigned char) type;
1043 header[ 9] = (unsigned char)( len >> 8 );
1044 header[10] = (unsigned char)( len );
1045
Paul Bakker68884e32013-01-07 18:20:04 +01001046 memset( padding, 0x36, padlen );
1047 md_starts( md_ctx );
1048 md_update( md_ctx, secret, md_size );
1049 md_update( md_ctx, padding, padlen );
1050 md_update( md_ctx, header, 11 );
1051 md_update( md_ctx, buf, len );
1052 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakker68884e32013-01-07 18:20:04 +01001054 memset( padding, 0x5C, padlen );
1055 md_starts( md_ctx );
1056 md_update( md_ctx, secret, md_size );
1057 md_update( md_ctx, padding, padlen );
1058 md_update( md_ctx, buf + len, md_size );
1059 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001060}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001062
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001063#define MAC_NONE 0
1064#define MAC_PLAINTEXT 1
1065#define MAC_CIPHERTEXT 2
1066
1067/*
1068 * Is MAC applied on ciphertext, cleartext or not at all?
1069 */
1070static char ssl_get_mac_order( ssl_context *ssl,
1071 const ssl_session *session,
1072 cipher_mode_t mode )
1073{
1074#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1075 if( mode == POLARSSL_MODE_STREAM )
1076 return( MAC_PLAINTEXT );
1077#endif
1078
1079#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1080 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1081 if( mode == POLARSSL_MODE_CBC )
1082 {
1083#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1084 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1085 {
1086 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1087 return( MAC_CIPHERTEXT );
1088 }
1089#endif
1090
1091 return( MAC_PLAINTEXT );
1092 }
1093#endif
1094
1095 return( MAC_NONE );
1096}
1097
Paul Bakker5121ce52009-01-03 21:22:43 +00001098/*
1099 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001100 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001101static int ssl_encrypt_buf( ssl_context *ssl )
1102{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001103 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001104 const cipher_mode_t mode = cipher_get_cipher_mode(
1105 &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001106 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001110 mac_order = ssl_get_mac_order( ssl, ssl->session_out, mode );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001111
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001113 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001115#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1116 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1117 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001118 if( mac_order == MAC_PLAINTEXT
1119 || mac_order == MAC_CIPHERTEXT ) // WIP!
Paul Bakker5121ce52009-01-03 21:22:43 +00001120 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001121#if defined(POLARSSL_SSL_PROTO_SSL3)
1122 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1123 {
1124 ssl_mac( &ssl->transform_out->md_ctx_enc,
1125 ssl->transform_out->mac_enc,
1126 ssl->out_msg, ssl->out_msglen,
1127 ssl->out_ctr, ssl->out_msgtype );
1128 }
1129 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001130#endif
1131#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001132 defined(POLARSSL_SSL_PROTO_TLS1_2)
1133 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1134 {
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1136 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1137 ssl->out_msg, ssl->out_msglen );
1138 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg + ssl->out_msglen );
1140 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1141 }
1142 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001143#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001144 {
1145 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001146 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001147 }
1148
1149 SSL_DEBUG_BUF( 4, "computed mac",
1150 ssl->out_msg + ssl->out_msglen,
1151 ssl->transform_out->maclen );
1152
1153 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001154 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001155#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001156
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001157 /*
1158 * Encrypt
1159 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001160#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001161 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001163 int ret;
1164 size_t olen = 0;
1165
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1167 "including %d bytes of padding",
1168 ssl->out_msglen, 0 ) );
1169
1170 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1171 ssl->out_msg, ssl->out_msglen );
1172
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 ssl->transform_out->ivlen,
1176 ssl->out_msg, ssl->out_msglen,
1177 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001178 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001179 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180 return( ret );
1181 }
1182
1183 if( ssl->out_msglen != olen )
1184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1192 if( mode == POLARSSL_MODE_GCM ||
1193 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 unsigned char *enc_msg;
1198 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1200 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 memcpy( add_data, ssl->out_ctr, 8 );
1203 add_data[8] = ssl->out_msgtype;
1204 add_data[9] = ssl->major_ver;
1205 add_data[10] = ssl->minor_ver;
1206 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1207 add_data[12] = ssl->out_msglen & 0xFF;
1208
1209 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1210 add_data, 13 );
1211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
1213 * Generate IV
1214 */
1215 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001216 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1217 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001218 if( ret != 0 )
1219 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001220
Paul Bakker68884e32013-01-07 18:20:04 +01001221 memcpy( ssl->out_iv,
1222 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1223 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001224
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001225 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001226 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001227
Paul Bakker68884e32013-01-07 18:20:04 +01001228 /*
1229 * Fix pointer positions and message length with added IV
1230 */
1231 enc_msg = ssl->out_msg;
1232 enc_msglen = ssl->out_msglen;
1233 ssl->out_msglen += ssl->transform_out->ivlen -
1234 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001235
Paul Bakker68884e32013-01-07 18:20:04 +01001236 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1237 "including %d bytes of padding",
1238 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001239
Paul Bakker68884e32013-01-07 18:20:04 +01001240 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1241 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001242
Paul Bakker68884e32013-01-07 18:20:04 +01001243 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001244 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001245 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001246 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1247 ssl->transform_out->iv_enc,
1248 ssl->transform_out->ivlen,
1249 add_data, 13,
1250 enc_msg, enc_msglen,
1251 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001252 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001253 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 return( ret );
1256 }
1257
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001258 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001259 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001260 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001261 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001262 }
1263
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001264 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001265
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001266 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001269#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001270#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1271 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001272 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001273 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001274 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001275 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001276 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001277
Paul Bakker48916f92012-09-16 19:57:18 +00001278 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1279 ssl->transform_out->ivlen;
1280 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001281 padlen = 0;
1282
1283 for( i = 0; i <= padlen; i++ )
1284 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1285
1286 ssl->out_msglen += padlen + 1;
1287
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288 enc_msglen = ssl->out_msglen;
1289 enc_msg = ssl->out_msg;
1290
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001291#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001292 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001293 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1294 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001295 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001296 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001297 {
1298 /*
1299 * Generate IV
1300 */
Paul Bakker48916f92012-09-16 19:57:18 +00001301 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1302 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001303 if( ret != 0 )
1304 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001305
Paul Bakker92be97b2013-01-02 17:30:03 +01001306 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001307 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308
1309 /*
1310 * Fix pointer positions and message length with added IV
1311 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001312 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001313 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001314 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001315 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001316#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001320 ssl->out_msglen, ssl->transform_out->ivlen,
1321 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322
1323 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001324 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001326 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001327 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001328 ssl->transform_out->ivlen,
1329 enc_msg, enc_msglen,
1330 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001331 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001332 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001333 return( ret );
1334 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001335
Paul Bakkercca5b812013-08-31 17:40:26 +02001336 if( enc_msglen != olen )
1337 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001338 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001339 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001340 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001341
1342#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001343 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1344 {
1345 /*
1346 * Save IV in SSL3 and TLS1
1347 */
1348 memcpy( ssl->transform_out->iv_enc,
1349 ssl->transform_out->cipher_ctx_enc.iv,
1350 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001354 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001355#endif /* POLARSSL_CIPHER_MODE_CBC &&
1356 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001357 {
1358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Paul Bakkerca4ab492012-04-18 14:23:57 +00001362 for( i = 8; i > 0; i-- )
1363 if( ++ssl->out_ctr[i - 1] != 0 )
1364 break;
1365
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001366 /* The loops goes to its end iff the counter is wrapping */
1367 if( i == 0 )
1368 {
1369 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1370 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1371 }
1372
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1374
1375 return( 0 );
1376}
1377
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001378#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001379
Paul Bakker5121ce52009-01-03 21:22:43 +00001380static int ssl_decrypt_buf( ssl_context *ssl )
1381{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001382 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001383 const cipher_mode_t mode = cipher_get_cipher_mode(
1384 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001385#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1386 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1387 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1388 size_t padlen = 0, correct = 1;
1389#endif
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001390 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001391
1392 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1393
Paul Bakker48916f92012-09-16 19:57:18 +00001394 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 {
1396 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001397 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001398 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001399 }
1400
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001401 mac_order = ssl_get_mac_order( ssl, ssl->session_in, mode );
1402 (void) mac_order; // WIP
1403
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001404#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001405 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001406 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001407 int ret;
1408 size_t olen = 0;
1409
Paul Bakker68884e32013-01-07 18:20:04 +01001410 padlen = 0;
1411
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001412 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001413 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001414 ssl->transform_in->ivlen,
1415 ssl->in_msg, ssl->in_msglen,
1416 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001417 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001418 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001419 return( ret );
1420 }
1421
1422 if( ssl->in_msglen != olen )
1423 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001424 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001425 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001426 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 }
Paul Bakker68884e32013-01-07 18:20:04 +01001428 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001429#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001430#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1431 if( mode == POLARSSL_MODE_GCM ||
1432 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001433 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001434 int ret;
1435 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001436 unsigned char *dec_msg;
1437 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001438 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001439 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1440 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001441 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1442 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001443
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001444 if( ssl->in_msglen < explicit_iv_len + taglen )
1445 {
1446 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1447 "+ taglen (%d)", ssl->in_msglen,
1448 explicit_iv_len, taglen ) );
1449 return( POLARSSL_ERR_SSL_INVALID_MAC );
1450 }
1451 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1452
Paul Bakker68884e32013-01-07 18:20:04 +01001453 dec_msg = ssl->in_msg;
1454 dec_msg_result = ssl->in_msg;
1455 ssl->in_msglen = dec_msglen;
1456
1457 memcpy( add_data, ssl->in_ctr, 8 );
1458 add_data[8] = ssl->in_msgtype;
1459 add_data[9] = ssl->major_ver;
1460 add_data[10] = ssl->minor_ver;
1461 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1462 add_data[12] = ssl->in_msglen & 0xFF;
1463
1464 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1465 add_data, 13 );
1466
1467 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1468 ssl->in_iv,
1469 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1470
1471 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1472 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001473 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001474
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001475 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001476 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001477 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001478 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1479 ssl->transform_in->iv_dec,
1480 ssl->transform_in->ivlen,
1481 add_data, 13,
1482 dec_msg, dec_msglen,
1483 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001484 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001485 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001486 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1487
1488 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1489 return( POLARSSL_ERR_SSL_INVALID_MAC );
1490
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001491 return( ret );
1492 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001493
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001494 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001495 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001496 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001497 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001498 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001501#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001502#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1503 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001504 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 {
Paul Bakker45829992013-01-03 14:52:21 +01001506 /*
1507 * Decrypt and check the padding
1508 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001509 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001510 unsigned char *dec_msg;
1511 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001512 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001513 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001514 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001515
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 /*
Paul Bakker45829992013-01-03 14:52:21 +01001517 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 */
Paul Bakker48916f92012-09-16 19:57:18 +00001519 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001520 {
1521 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001522 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001523 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 }
1525
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001526#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001527 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1528 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001529#endif
Paul Bakker45829992013-01-03 14:52:21 +01001530
1531 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1532 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1533 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001534 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1535 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1536 ssl->transform_in->ivlen,
1537 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001538 return( POLARSSL_ERR_SSL_INVALID_MAC );
1539 }
1540
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001541 dec_msglen = ssl->in_msglen;
1542 dec_msg = ssl->in_msg;
1543 dec_msg_result = ssl->in_msg;
1544
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001545#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001546 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001547 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001548 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001549 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001550 {
Paul Bakker48916f92012-09-16 19:57:18 +00001551 dec_msglen -= ssl->transform_in->ivlen;
1552 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001553
Paul Bakker48916f92012-09-16 19:57:18 +00001554 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001555 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001556 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001557#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001558
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001559 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001560 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001561 ssl->transform_in->ivlen,
1562 dec_msg, dec_msglen,
1563 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001564 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001565 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001566 return( ret );
1567 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001568
Paul Bakkercca5b812013-08-31 17:40:26 +02001569 if( dec_msglen != olen )
1570 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001571 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001572 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001573 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001574
1575#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001576 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1577 {
1578 /*
1579 * Save IV in SSL3 and TLS1
1580 */
1581 memcpy( ssl->transform_in->iv_dec,
1582 ssl->transform_in->cipher_ctx_dec.iv,
1583 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001585#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
1587 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001588
1589 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1590 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001591#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001592 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1593 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001594#endif
Paul Bakker45829992013-01-03 14:52:21 +01001595 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001596 correct = 0;
1597 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001599#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001600 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1601 {
Paul Bakker48916f92012-09-16 19:57:18 +00001602 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001604#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1606 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001607 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001608#endif
Paul Bakker45829992013-01-03 14:52:21 +01001609 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 }
1611 }
1612 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001613#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001614#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1615 defined(POLARSSL_SSL_PROTO_TLS1_2)
1616 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 {
1618 /*
Paul Bakker45829992013-01-03 14:52:21 +01001619 * TLSv1+: always check the padding up to the first failure
1620 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001622 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001623 size_t padding_idx = ssl->in_msglen - padlen - 1;
1624
Paul Bakker956c9e02013-12-19 14:42:28 +01001625 /*
1626 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001627 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001628 *
Paul Bakker61885c72014-04-25 12:59:03 +02001629 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1630 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001631 *
1632 * In both cases we reset padding_idx to a safe value (0) to
1633 * prevent out-of-buffer reads.
1634 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001635 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001636 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1637 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001638
1639 padding_idx *= correct;
1640
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001641 for( i = 1; i <= 256; i++ )
1642 {
1643 real_count &= ( i <= padlen );
1644 pad_count += real_count *
1645 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1646 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001647
1648 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001649
Paul Bakkerd66f0702013-01-31 16:57:45 +01001650#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001651 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001652 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001653#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001656 else
1657#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1658 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001659 {
1660 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001661 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001662 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001663 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001664 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001665#endif /* POLARSSL_CIPHER_MODE_CBC &&
1666 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001667 {
1668 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001669 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
1672 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1673 ssl->in_msg, ssl->in_msglen );
1674
1675 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001676 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001677 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1679 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1680 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001681 if( mode != POLARSSL_MODE_GCM &&
1682 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001684 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1685
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1689 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001693#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001694 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1695 {
1696 ssl_mac( &ssl->transform_in->md_ctx_dec,
1697 ssl->transform_in->mac_dec,
1698 ssl->in_msg, ssl->in_msglen,
1699 ssl->in_ctr, ssl->in_msgtype );
1700 }
1701 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001702#endif /* POLARSSL_SSL_PROTO_SSL3 */
1703#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001704 defined(POLARSSL_SSL_PROTO_TLS1_2)
1705 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1706 {
1707 /*
1708 * Process MAC and always update for padlen afterwards to make
1709 * total time independent of padlen
1710 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001711 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 *
1713 * Known timing attacks:
1714 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1715 *
1716 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1717 * correctly. (We round down instead of up, so -56 is the correct
1718 * value for our calculations instead of -55)
1719 */
1720 size_t j, extra_run = 0;
1721 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1722 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001723
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001725
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001726 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1727 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1728 ssl->in_msglen );
1729 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1730 ssl->in_msg + ssl->in_msglen );
1731 for( j = 0; j < extra_run; j++ )
1732 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001733
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001734 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1735 }
1736 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001737#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001738 POLARSSL_SSL_PROTO_TLS1_2 */
1739 {
1740 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001741 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001742 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001744 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1745 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1746 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001748 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001749 ssl->transform_in->maclen ) != 0 )
1750 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001751#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001752 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001754 correct = 0;
1755 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001757 /*
1758 * Finally check the correct flag
1759 */
1760 if( correct == 0 )
1761 return( POLARSSL_ERR_SSL_INVALID_MAC );
1762 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001763#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
1765 if( ssl->in_msglen == 0 )
1766 {
1767 ssl->nb_zero++;
1768
1769 /*
1770 * Three or more empty messages may be a DoS attack
1771 * (excessive CPU consumption).
1772 */
1773 if( ssl->nb_zero > 3 )
1774 {
1775 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1776 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001777 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001778 }
1779 }
1780 else
1781 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001782
Paul Bakker23986e52011-04-24 08:57:21 +00001783 for( i = 8; i > 0; i-- )
1784 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 break;
1786
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001787 /* The loops goes to its end iff the counter is wrapping */
1788 if( i == 0 )
1789 {
1790 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1791 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1792 }
1793
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1795
1796 return( 0 );
1797}
1798
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001799#undef MAC_NONE
1800#undef MAC_PLAINTEXT
1801#undef MAC_CIPHERTEXT
1802
Paul Bakker2770fbd2012-07-03 13:30:23 +00001803#if defined(POLARSSL_ZLIB_SUPPORT)
1804/*
1805 * Compression/decompression functions
1806 */
1807static int ssl_compress_buf( ssl_context *ssl )
1808{
1809 int ret;
1810 unsigned char *msg_post = ssl->out_msg;
1811 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001812 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813
1814 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1815
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001816 if( len_pre == 0 )
1817 return( 0 );
1818
Paul Bakker2770fbd2012-07-03 13:30:23 +00001819 memcpy( msg_pre, ssl->out_msg, len_pre );
1820
1821 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1822 ssl->out_msglen ) );
1823
1824 SSL_DEBUG_BUF( 4, "before compression: output payload",
1825 ssl->out_msg, ssl->out_msglen );
1826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1828 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1829 ssl->transform_out->ctx_deflate.next_out = msg_post;
1830 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001831
Paul Bakker48916f92012-09-16 19:57:18 +00001832 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833 if( ret != Z_OK )
1834 {
1835 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1836 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1837 }
1838
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001839 ssl->out_msglen = SSL_BUFFER_LEN -
1840 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001841
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1843 ssl->out_msglen ) );
1844
1845 SSL_DEBUG_BUF( 4, "after compression: output payload",
1846 ssl->out_msg, ssl->out_msglen );
1847
1848 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1849
1850 return( 0 );
1851}
1852
1853static int ssl_decompress_buf( ssl_context *ssl )
1854{
1855 int ret;
1856 unsigned char *msg_post = ssl->in_msg;
1857 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001858 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001859
1860 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1861
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001862 if( len_pre == 0 )
1863 return( 0 );
1864
Paul Bakker2770fbd2012-07-03 13:30:23 +00001865 memcpy( msg_pre, ssl->in_msg, len_pre );
1866
1867 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1868 ssl->in_msglen ) );
1869
1870 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1871 ssl->in_msg, ssl->in_msglen );
1872
Paul Bakker48916f92012-09-16 19:57:18 +00001873 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1874 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1875 ssl->transform_in->ctx_inflate.next_out = msg_post;
1876 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001877
Paul Bakker48916f92012-09-16 19:57:18 +00001878 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001879 if( ret != Z_OK )
1880 {
1881 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1882 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1883 }
1884
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001885 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1886 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001887
Paul Bakker2770fbd2012-07-03 13:30:23 +00001888 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1889 ssl->in_msglen ) );
1890
1891 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1892 ssl->in_msg, ssl->in_msglen );
1893
1894 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1895
1896 return( 0 );
1897}
1898#endif /* POLARSSL_ZLIB_SUPPORT */
1899
Paul Bakker5121ce52009-01-03 21:22:43 +00001900/*
1901 * Fill the input message buffer
1902 */
Paul Bakker23986e52011-04-24 08:57:21 +00001903int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001904{
Paul Bakker23986e52011-04-24 08:57:21 +00001905 int ret;
1906 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
1908 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1909
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001910 if( nb_want > SSL_BUFFER_LEN - 8 )
1911 {
1912 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1913 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1914 }
1915
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 while( ssl->in_left < nb_want )
1917 {
1918 len = nb_want - ssl->in_left;
1919 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1920
1921 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1922 ssl->in_left, nb_want ) );
1923 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1924
Paul Bakker831a7552011-05-18 13:32:51 +00001925 if( ret == 0 )
1926 return( POLARSSL_ERR_SSL_CONN_EOF );
1927
Paul Bakker5121ce52009-01-03 21:22:43 +00001928 if( ret < 0 )
1929 return( ret );
1930
1931 ssl->in_left += ret;
1932 }
1933
1934 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1935
1936 return( 0 );
1937}
1938
1939/*
1940 * Flush any data not yet written
1941 */
1942int ssl_flush_output( ssl_context *ssl )
1943{
1944 int ret;
1945 unsigned char *buf;
1946
1947 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1948
1949 while( ssl->out_left > 0 )
1950 {
1951 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1952 5 + ssl->out_msglen, ssl->out_left ) );
1953
Paul Bakker5bd42292012-12-19 14:40:42 +01001954 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001956
Paul Bakker5121ce52009-01-03 21:22:43 +00001957 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1958
1959 if( ret <= 0 )
1960 return( ret );
1961
1962 ssl->out_left -= ret;
1963 }
1964
1965 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1966
1967 return( 0 );
1968}
1969
1970/*
1971 * Record layer functions
1972 */
1973int ssl_write_record( ssl_context *ssl )
1974{
Paul Bakker05ef8352012-05-08 09:17:57 +00001975 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001976 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
1978 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1979
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1981 {
1982 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1983 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1984 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1985
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001986 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1987 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 }
1989
Paul Bakker2770fbd2012-07-03 13:30:23 +00001990#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001991 if( ssl->transform_out != NULL &&
1992 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001993 {
1994 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1995 {
1996 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1997 return( ret );
1998 }
1999
2000 len = ssl->out_msglen;
2001 }
2002#endif /*POLARSSL_ZLIB_SUPPORT */
2003
Paul Bakker05ef8352012-05-08 09:17:57 +00002004#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002005 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002007 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Paul Bakker05ef8352012-05-08 09:17:57 +00002009 ret = ssl_hw_record_write( ssl );
2010 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2011 {
2012 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002013 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002014 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002015
2016 if( ret == 0 )
2017 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002018 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002019#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002020 if( !done )
2021 {
2022 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2023 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2024 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2026 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002027
Paul Bakker48916f92012-09-16 19:57:18 +00002028 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002029 {
2030 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2031 {
2032 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2033 return( ret );
2034 }
2035
2036 len = ssl->out_msglen;
2037 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2038 ssl->out_hdr[4] = (unsigned char)( len );
2039 }
2040
2041 ssl->out_left = 5 + ssl->out_msglen;
2042
2043 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2044 "version = [%d:%d], msglen = %d",
2045 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2046 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2047
2048 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002049 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002050 }
2051
Paul Bakker5121ce52009-01-03 21:22:43 +00002052 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2053 {
2054 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2055 return( ret );
2056 }
2057
2058 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2059
2060 return( 0 );
2061}
2062
2063int ssl_read_record( ssl_context *ssl )
2064{
Paul Bakker05ef8352012-05-08 09:17:57 +00002065 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
2067 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2068
2069 if( ssl->in_hslen != 0 &&
2070 ssl->in_hslen < ssl->in_msglen )
2071 {
2072 /*
2073 * Get next Handshake message in the current record
2074 */
2075 ssl->in_msglen -= ssl->in_hslen;
2076
Paul Bakker8934a982011-08-05 11:11:53 +00002077 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002078 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002079
2080 ssl->in_hslen = 4;
2081 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2082
2083 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2084 " %d, type = %d, hslen = %d",
2085 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2086
2087 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2088 {
2089 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
2092
2093 if( ssl->in_msglen < ssl->in_hslen )
2094 {
2095 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002096 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098
Paul Bakker4224bc02014-04-08 14:36:50 +02002099 if( ssl->state != SSL_HANDSHAKE_OVER )
2100 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
2102 return( 0 );
2103 }
2104
2105 ssl->in_hslen = 0;
2106
2107 /*
2108 * Read the record header and validate it
2109 */
2110 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2111 {
2112 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2113 return( ret );
2114 }
2115
2116 ssl->in_msgtype = ssl->in_hdr[0];
2117 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2118
2119 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2120 "version = [%d:%d], msglen = %d",
2121 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2122 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2123
2124 if( ssl->in_hdr[1] != ssl->major_ver )
2125 {
2126 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002127 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 }
2129
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002130 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 {
2132 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002133 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 }
2135
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002136 /* Sanity check (outer boundaries) */
2137 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2138 {
2139 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2140 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2141 }
2142
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002144 * Make sure the message length is acceptable for the current transform
2145 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 */
Paul Bakker48916f92012-09-16 19:57:18 +00002147 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002149 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
2151 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002152 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002153 }
2154 }
2155 else
2156 {
Paul Bakker48916f92012-09-16 19:57:18 +00002157 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 {
2159 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002160 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 }
2162
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002163#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002165 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002166 {
2167 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002168 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002170#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002171
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002172#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2173 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002174 /*
2175 * TLS encrypted messages can have up to 256 bytes of padding
2176 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002177 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002178 ssl->in_msglen > ssl->transform_in->minlen +
2179 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002180 {
2181 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002182 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002183 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002184#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002185 }
2186
2187 /*
2188 * Read and optionally decrypt the message contents
2189 */
2190 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2191 {
2192 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2193 return( ret );
2194 }
2195
2196 SSL_DEBUG_BUF( 4, "input record from network",
2197 ssl->in_hdr, 5 + ssl->in_msglen );
2198
Paul Bakker05ef8352012-05-08 09:17:57 +00002199#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002200 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002201 {
2202 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2203
2204 ret = ssl_hw_record_read( ssl );
2205 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2206 {
2207 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002208 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002209 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002210
2211 if( ret == 0 )
2212 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002213 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002214#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002215 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
2217 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2218 {
Paul Bakker40865c82013-01-31 17:13:13 +01002219#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2220 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2221 {
2222 ssl_send_alert_message( ssl,
2223 SSL_ALERT_LEVEL_FATAL,
2224 SSL_ALERT_MSG_BAD_RECORD_MAC );
2225 }
2226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2228 return( ret );
2229 }
2230
2231 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2232 ssl->in_msg, ssl->in_msglen );
2233
2234 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2235 {
2236 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002237 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 }
2239 }
2240
Paul Bakker2770fbd2012-07-03 13:30:23 +00002241#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002242 if( ssl->transform_in != NULL &&
2243 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002244 {
2245 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2246 {
2247 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2248 return( ret );
2249 }
2250
2251 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2252 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2253 }
2254#endif /* POLARSSL_ZLIB_SUPPORT */
2255
Paul Bakker0a925182012-04-16 06:46:41 +00002256 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2257 ssl->in_msgtype != SSL_MSG_ALERT &&
2258 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2259 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2260 {
2261 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2262
Paul Bakker48916f92012-09-16 19:57:18 +00002263 if( ( ret = ssl_send_alert_message( ssl,
2264 SSL_ALERT_LEVEL_FATAL,
2265 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002266 {
2267 return( ret );
2268 }
2269
2270 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2271 }
2272
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2274 {
2275 ssl->in_hslen = 4;
2276 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2277
2278 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2279 " %d, type = %d, hslen = %d",
2280 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2281
2282 /*
2283 * Additional checks to validate the handshake header
2284 */
2285 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2286 {
2287 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002288 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
2290
2291 if( ssl->in_msglen < ssl->in_hslen )
2292 {
2293 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002294 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
2296
Paul Bakker48916f92012-09-16 19:57:18 +00002297 if( ssl->state != SSL_HANDSHAKE_OVER )
2298 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002299 }
2300
2301 if( ssl->in_msgtype == SSL_MSG_ALERT )
2302 {
2303 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2304 ssl->in_msg[0], ssl->in_msg[1] ) );
2305
2306 /*
2307 * Ignore non-fatal alerts, except close_notify
2308 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002309 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002310 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002311 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2312 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002313 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002314 }
2315
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002316 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2317 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 {
2319 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002320 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 }
2322 }
2323
2324 ssl->in_left = 0;
2325
2326 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2327
2328 return( 0 );
2329}
2330
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002331int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2332{
2333 int ret;
2334
2335 if( ( ret = ssl_send_alert_message( ssl,
2336 SSL_ALERT_LEVEL_FATAL,
2337 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2338 {
2339 return( ret );
2340 }
2341
2342 return( 0 );
2343}
2344
Paul Bakker0a925182012-04-16 06:46:41 +00002345int ssl_send_alert_message( ssl_context *ssl,
2346 unsigned char level,
2347 unsigned char message )
2348{
2349 int ret;
2350
2351 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2352
2353 ssl->out_msgtype = SSL_MSG_ALERT;
2354 ssl->out_msglen = 2;
2355 ssl->out_msg[0] = level;
2356 ssl->out_msg[1] = message;
2357
2358 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2359 {
2360 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2361 return( ret );
2362 }
2363
2364 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2365
2366 return( 0 );
2367}
2368
Paul Bakker5121ce52009-01-03 21:22:43 +00002369/*
2370 * Handshake functions
2371 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002372#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2373 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2374 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2375 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2376 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2377 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2378 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002379int ssl_write_certificate( ssl_context *ssl )
2380{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002381 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002382
2383 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2384
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002385 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002386 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2387 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002388 {
2389 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2390 ssl->state++;
2391 return( 0 );
2392 }
2393
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002394 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2395 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002396}
2397
2398int ssl_parse_certificate( ssl_context *ssl )
2399{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2401
2402 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2403
2404 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002405 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2406 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 {
2408 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2409 ssl->state++;
2410 return( 0 );
2411 }
2412
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002413 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2414 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415}
2416#else
2417int ssl_write_certificate( ssl_context *ssl )
2418{
2419 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2420 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002421 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002422 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2423
2424 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2425
2426 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002427 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2428 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002429 {
2430 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2431 ssl->state++;
2432 return( 0 );
2433 }
2434
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 if( ssl->endpoint == SSL_IS_CLIENT )
2436 {
2437 if( ssl->client_auth == 0 )
2438 {
2439 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2440 ssl->state++;
2441 return( 0 );
2442 }
2443
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002444#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002445 /*
2446 * If using SSLv3 and got no cert, send an Alert message
2447 * (otherwise an empty Certificate message will be sent).
2448 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002449 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002450 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2451 {
2452 ssl->out_msglen = 2;
2453 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2455 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
2457 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2458 goto write_msg;
2459 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462 else /* SSL_IS_SERVER */
2463 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002464 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 {
2466 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002467 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 }
2469 }
2470
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002471 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472
2473 /*
2474 * 0 . 0 handshake type
2475 * 1 . 3 handshake length
2476 * 4 . 6 length of all certs
2477 * 7 . 9 length of cert. 1
2478 * 10 . n-1 peer certificate
2479 * n . n+2 length of cert. 2
2480 * n+3 . ... upper level cert, etc.
2481 */
2482 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002483 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002484
Paul Bakker29087132010-03-21 21:03:34 +00002485 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
2487 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002488 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 {
2490 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2491 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002492 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002493 }
2494
2495 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2496 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2497 ssl->out_msg[i + 2] = (unsigned char)( n );
2498
2499 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2500 i += n; crt = crt->next;
2501 }
2502
2503 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2504 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2505 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2506
2507 ssl->out_msglen = i;
2508 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2509 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002511#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002512write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002514
2515 ssl->state++;
2516
2517 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2518 {
2519 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2520 return( ret );
2521 }
2522
2523 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2524
Paul Bakkered27a042013-04-18 22:46:23 +02002525 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526}
2527
2528int ssl_parse_certificate( ssl_context *ssl )
2529{
Paul Bakkered27a042013-04-18 22:46:23 +02002530 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002531 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002532 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
2534 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2535
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002537 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2538 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002539 {
2540 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2541 ssl->state++;
2542 return( 0 );
2543 }
2544
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002546 ( ssl->authmode == SSL_VERIFY_NONE ||
2547 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002549 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2551 ssl->state++;
2552 return( 0 );
2553 }
2554
2555 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2556 {
2557 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2558 return( ret );
2559 }
2560
2561 ssl->state++;
2562
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002563#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 /*
2565 * Check if the client sent an empty certificate
2566 */
2567 if( ssl->endpoint == SSL_IS_SERVER &&
2568 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2569 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002570 if( ssl->in_msglen == 2 &&
2571 ssl->in_msgtype == SSL_MSG_ALERT &&
2572 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2573 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 {
2575 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2576
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002577 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2579 return( 0 );
2580 else
Paul Bakker40e46942009-01-03 21:51:57 +00002581 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 }
2583 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002584#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002585
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002586#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2587 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 if( ssl->endpoint == SSL_IS_SERVER &&
2589 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2590 {
2591 if( ssl->in_hslen == 7 &&
2592 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2593 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2594 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2595 {
2596 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2597
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002598 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002600 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 else
2602 return( 0 );
2603 }
2604 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002605#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2606 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002607
2608 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002611 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 }
2613
2614 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2615 {
2616 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002617 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
2619
2620 /*
2621 * Same message structure as in ssl_write_certificate()
2622 */
2623 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2624
2625 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2626 {
2627 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002628 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002629 }
2630
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002631 /* In case we tried to reuse a session but it failed */
2632 if( ssl->session_negotiate->peer_cert != NULL )
2633 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002634 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002635 polarssl_free( ssl->session_negotiate->peer_cert );
2636 }
2637
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002638 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2639 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 {
2641 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002642 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002643 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 }
2645
Paul Bakkerb6b09562013-09-18 14:17:41 +02002646 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
2648 i = 7;
2649
2650 while( i < ssl->in_hslen )
2651 {
2652 if( ssl->in_msg[i] != 0 )
2653 {
2654 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002655 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 }
2657
2658 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2659 | (unsigned int) ssl->in_msg[i + 2];
2660 i += 3;
2661
2662 if( n < 128 || i + n > ssl->in_hslen )
2663 {
2664 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002665 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 }
2667
Paul Bakkerddf26b42013-09-18 13:46:23 +02002668 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2669 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 if( ret != 0 )
2671 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002672 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 return( ret );
2674 }
2675
2676 i += n;
2677 }
2678
Paul Bakker48916f92012-09-16 19:57:18 +00002679 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002681 /*
2682 * On client, make sure the server cert doesn't change during renego to
2683 * avoid "triple handshake" attack: https://secure-resumption.com/
2684 */
2685 if( ssl->endpoint == SSL_IS_CLIENT &&
2686 ssl->renegotiation == SSL_RENEGOTIATION )
2687 {
2688 if( ssl->session->peer_cert == NULL )
2689 {
2690 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2691 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2692 }
2693
2694 if( ssl->session->peer_cert->raw.len !=
2695 ssl->session_negotiate->peer_cert->raw.len ||
2696 memcmp( ssl->session->peer_cert->raw.p,
2697 ssl->session_negotiate->peer_cert->raw.p,
2698 ssl->session->peer_cert->raw.len ) != 0 )
2699 {
2700 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2701 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2702 }
2703 }
2704
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 if( ssl->authmode != SSL_VERIFY_NONE )
2706 {
2707 if( ssl->ca_chain == NULL )
2708 {
2709 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002710 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
2712
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002713 /*
2714 * Main check: verify certificate
2715 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002716 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2717 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2718 &ssl->session_negotiate->verify_result,
2719 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
2721 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002722 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002724 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002725
2726 /*
2727 * Secondary checks: always done, but change 'ret' only if it was 0
2728 */
2729
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002730#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002731 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002732 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002733
2734 /* If certificate uses an EC key, make sure the curve is OK */
2735 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2736 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2737 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002738 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002739 if( ret == 0 )
2740 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002741 }
2742 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002743#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002745 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2746 ciphersuite_info,
2747 ! ssl->endpoint ) != 0 )
2748 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002749 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002750 if( ret == 0 )
2751 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2752 }
2753
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2755 ret = 0;
2756 }
2757
2758 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2759
2760 return( ret );
2761}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002762#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2763 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2764 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2765 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2766 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2767 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2768 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002769
2770int ssl_write_change_cipher_spec( ssl_context *ssl )
2771{
2772 int ret;
2773
2774 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2775
2776 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2777 ssl->out_msglen = 1;
2778 ssl->out_msg[0] = 1;
2779
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 ssl->state++;
2781
2782 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2783 {
2784 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2785 return( ret );
2786 }
2787
2788 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2789
2790 return( 0 );
2791}
2792
2793int ssl_parse_change_cipher_spec( ssl_context *ssl )
2794{
2795 int ret;
2796
2797 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2798
Paul Bakker5121ce52009-01-03 21:22:43 +00002799 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2800 {
2801 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2802 return( ret );
2803 }
2804
2805 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2806 {
2807 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002808 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002809 }
2810
2811 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2812 {
2813 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002814 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002815 }
2816
2817 ssl->state++;
2818
2819 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2820
2821 return( 0 );
2822}
2823
Paul Bakker41c83d32013-03-20 14:39:14 +01002824void ssl_optimize_checksum( ssl_context *ssl,
2825 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002826{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002827 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002828
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002829#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2830 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002831 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002832 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002833 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002834#endif
2835#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2836#if defined(POLARSSL_SHA512_C)
2837 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2838 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2839 else
2840#endif
2841#if defined(POLARSSL_SHA256_C)
2842 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002843 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002844 else
2845#endif
2846#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002847 {
2848 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002849 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002850 }
Paul Bakker380da532012-04-18 16:10:25 +00002851}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002852
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002853static void ssl_update_checksum_start( ssl_context *ssl,
2854 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002855{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002856#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2857 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002858 md5_update( &ssl->handshake->fin_md5 , buf, len );
2859 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002860#endif
2861#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2862#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002863 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002864#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002865#if defined(POLARSSL_SHA512_C)
2866 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002867#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002868#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002869}
2870
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002871#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2872 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002873static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2874 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002875{
Paul Bakker48916f92012-09-16 19:57:18 +00002876 md5_update( &ssl->handshake->fin_md5 , buf, len );
2877 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002878}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002879#endif
Paul Bakker380da532012-04-18 16:10:25 +00002880
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002881#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2882#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002883static void ssl_update_checksum_sha256( ssl_context *ssl,
2884 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002885{
Paul Bakker9e36f042013-06-30 14:34:05 +02002886 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002887}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002888#endif
Paul Bakker380da532012-04-18 16:10:25 +00002889
Paul Bakker9e36f042013-06-30 14:34:05 +02002890#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002891static void ssl_update_checksum_sha384( ssl_context *ssl,
2892 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002893{
Paul Bakker9e36f042013-06-30 14:34:05 +02002894 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002895}
Paul Bakker769075d2012-11-24 11:26:46 +01002896#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002897#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002898
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002899#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900static void ssl_calc_finished_ssl(
2901 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002902{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002903 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002904 md5_context md5;
2905 sha1_context sha1;
2906
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 unsigned char padbuf[48];
2908 unsigned char md5sum[16];
2909 unsigned char sha1sum[20];
2910
Paul Bakker48916f92012-09-16 19:57:18 +00002911 ssl_session *session = ssl->session_negotiate;
2912 if( !session )
2913 session = ssl->session;
2914
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2916
Paul Bakker48916f92012-09-16 19:57:18 +00002917 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2918 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
2920 /*
2921 * SSLv3:
2922 * hash =
2923 * MD5( master + pad2 +
2924 * MD5( handshake + sender + master + pad1 ) )
2925 * + SHA1( master + pad2 +
2926 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 */
2928
Paul Bakker90995b52013-06-24 19:20:35 +02002929#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002932#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002933
Paul Bakker90995b52013-06-24 19:20:35 +02002934#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002937#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Paul Bakker3c2122f2013-06-24 19:03:14 +02002939 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2940 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakker3c2122f2013-06-24 19:03:14 +02002944 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002945 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002946 md5_update( &md5, padbuf, 48 );
2947 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakker3c2122f2013-06-24 19:03:14 +02002949 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002950 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 sha1_update( &sha1, padbuf, 40 );
2952 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002957 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 md5_update( &md5, padbuf, 48 );
2959 md5_update( &md5, md5sum, 16 );
2960 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002961
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002963 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964 sha1_update( &sha1, padbuf , 40 );
2965 sha1_update( &sha1, sha1sum, 20 );
2966 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Paul Bakker5b4af392014-06-26 12:09:34 +02002970 md5_free( &md5 );
2971 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Paul Bakker34617722014-06-13 17:20:13 +02002973 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2974 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2975 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
2977 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2978}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002979#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002981#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982static void ssl_calc_finished_tls(
2983 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002984{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002986 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002987 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002988 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker48916f92012-09-16 19:57:18 +00002991 ssl_session *session = ssl->session_negotiate;
2992 if( !session )
2993 session = ssl->session;
2994
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002996
Paul Bakker48916f92012-09-16 19:57:18 +00002997 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2998 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000 /*
3001 * TLSv1:
3002 * hash = PRF( master, finished_label,
3003 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3004 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Paul Bakker90995b52013-06-24 19:20:35 +02003006#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003007 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3008 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003009#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003010
Paul Bakker90995b52013-06-24 19:20:35 +02003011#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003012 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3013 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003014#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015
3016 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003017 ? "client finished"
3018 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019
3020 md5_finish( &md5, padbuf );
3021 sha1_finish( &sha1, padbuf + 16 );
3022
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003023 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003024 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003025
3026 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3027
Paul Bakker5b4af392014-06-26 12:09:34 +02003028 md5_free( &md5 );
3029 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030
Paul Bakker34617722014-06-13 17:20:13 +02003031 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032
3033 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3034}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003037#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3038#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003039static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003040 ssl_context *ssl, unsigned char *buf, int from )
3041{
3042 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003043 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003044 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003045 unsigned char padbuf[32];
3046
Paul Bakker48916f92012-09-16 19:57:18 +00003047 ssl_session *session = ssl->session_negotiate;
3048 if( !session )
3049 session = ssl->session;
3050
Paul Bakker380da532012-04-18 16:10:25 +00003051 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003052
Paul Bakker9e36f042013-06-30 14:34:05 +02003053 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003054
3055 /*
3056 * TLSv1.2:
3057 * hash = PRF( master, finished_label,
3058 * Hash( handshake ) )[0.11]
3059 */
3060
Paul Bakker9e36f042013-06-30 14:34:05 +02003061#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003062 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003063 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003064#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003065
3066 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003067 ? "client finished"
3068 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003069
Paul Bakker9e36f042013-06-30 14:34:05 +02003070 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003072 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003073 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003074
3075 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3076
Paul Bakker5b4af392014-06-26 12:09:34 +02003077 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078
Paul Bakker34617722014-06-13 17:20:13 +02003079 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080
3081 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3082}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003083#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003084
Paul Bakker9e36f042013-06-30 14:34:05 +02003085#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086static void ssl_calc_finished_tls_sha384(
3087 ssl_context *ssl, unsigned char *buf, int from )
3088{
3089 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003090 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003091 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003092 unsigned char padbuf[48];
3093
Paul Bakker48916f92012-09-16 19:57:18 +00003094 ssl_session *session = ssl->session_negotiate;
3095 if( !session )
3096 session = ssl->session;
3097
Paul Bakker380da532012-04-18 16:10:25 +00003098 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003099
Paul Bakker9e36f042013-06-30 14:34:05 +02003100 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003101
3102 /*
3103 * TLSv1.2:
3104 * hash = PRF( master, finished_label,
3105 * Hash( handshake ) )[0.11]
3106 */
3107
Paul Bakker9e36f042013-06-30 14:34:05 +02003108#if !defined(POLARSSL_SHA512_ALT)
3109 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3110 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003111#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003112
3113 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003114 ? "client finished"
3115 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003116
Paul Bakker9e36f042013-06-30 14:34:05 +02003117 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003118
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003119 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003120 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003121
3122 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3123
Paul Bakker5b4af392014-06-26 12:09:34 +02003124 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003125
Paul Bakker34617722014-06-13 17:20:13 +02003126 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003127
3128 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3129}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003130#endif /* POLARSSL_SHA512_C */
3131#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003132
Paul Bakker48916f92012-09-16 19:57:18 +00003133void ssl_handshake_wrapup( ssl_context *ssl )
3134{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003135 int resume = ssl->handshake->resume;
3136
Paul Bakker48916f92012-09-16 19:57:18 +00003137 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3138
3139 /*
3140 * Free our handshake params
3141 */
3142 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003143 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003144 ssl->handshake = NULL;
3145
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003146 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003147 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003148 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003149 ssl->renego_records_seen = 0;
3150 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003151
Paul Bakker48916f92012-09-16 19:57:18 +00003152 /*
3153 * Switch in our now active transform context
3154 */
3155 if( ssl->transform )
3156 {
3157 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003158 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003159 }
3160 ssl->transform = ssl->transform_negotiate;
3161 ssl->transform_negotiate = NULL;
3162
Paul Bakker0a597072012-09-25 21:55:46 +00003163 if( ssl->session )
3164 {
3165 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003166 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003167 }
3168 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003169 ssl->session_negotiate = NULL;
3170
Paul Bakker0a597072012-09-25 21:55:46 +00003171 /*
3172 * Add cache entry
3173 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003174 if( ssl->f_set_cache != NULL &&
3175 ssl->session->length != 0 &&
3176 resume == 0 )
3177 {
Paul Bakker0a597072012-09-25 21:55:46 +00003178 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3179 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003180 }
Paul Bakker0a597072012-09-25 21:55:46 +00003181
Paul Bakker48916f92012-09-16 19:57:18 +00003182 ssl->state++;
3183
3184 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3185}
3186
Paul Bakker1ef83d62012-04-11 12:09:53 +00003187int ssl_write_finished( ssl_context *ssl )
3188{
3189 int ret, hash_len;
3190
3191 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3192
Paul Bakker92be97b2013-01-02 17:30:03 +01003193 /*
3194 * Set the out_msg pointer to the correct location based on IV length
3195 */
3196 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3197 {
3198 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3199 ssl->transform_negotiate->fixed_ivlen;
3200 }
3201 else
3202 ssl->out_msg = ssl->out_iv;
3203
Paul Bakker48916f92012-09-16 19:57:18 +00003204 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003205
3206 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003207 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3208
Paul Bakker48916f92012-09-16 19:57:18 +00003209 ssl->verify_data_len = hash_len;
3210 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3211
Paul Bakker5121ce52009-01-03 21:22:43 +00003212 ssl->out_msglen = 4 + hash_len;
3213 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3214 ssl->out_msg[0] = SSL_HS_FINISHED;
3215
3216 /*
3217 * In case of session resuming, invert the client and server
3218 * ChangeCipherSpec messages order.
3219 */
Paul Bakker0a597072012-09-25 21:55:46 +00003220 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 {
3222 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 else
3225 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3226 }
3227 else
3228 ssl->state++;
3229
Paul Bakker48916f92012-09-16 19:57:18 +00003230 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003231 * Switch to our negotiated transform and session parameters for outbound
3232 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003233 */
3234 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3235 ssl->transform_out = ssl->transform_negotiate;
3236 ssl->session_out = ssl->session_negotiate;
3237 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003238
Paul Bakker07eb38b2012-12-19 14:42:06 +01003239#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003240 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003241 {
3242 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3243 {
3244 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3245 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3246 }
3247 }
3248#endif
3249
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3251 {
3252 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3253 return( ret );
3254 }
3255
3256 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3257
3258 return( 0 );
3259}
3260
3261int ssl_parse_finished( ssl_context *ssl )
3262{
Paul Bakker23986e52011-04-24 08:57:21 +00003263 int ret;
3264 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 unsigned char buf[36];
3266
3267 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3268
Paul Bakker48916f92012-09-16 19:57:18 +00003269 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003270
Paul Bakker48916f92012-09-16 19:57:18 +00003271 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003272 * Switch to our negotiated transform and session parameters for inbound
3273 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003274 */
3275 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3276 ssl->transform_in = ssl->transform_negotiate;
3277 ssl->session_in = ssl->session_negotiate;
3278 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003279
Paul Bakker92be97b2013-01-02 17:30:03 +01003280 /*
3281 * Set the in_msg pointer to the correct location based on IV length
3282 */
3283 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3284 {
3285 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3286 ssl->transform_negotiate->fixed_ivlen;
3287 }
3288 else
3289 ssl->in_msg = ssl->in_iv;
3290
Paul Bakker07eb38b2012-12-19 14:42:06 +01003291#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003292 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003293 {
3294 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3295 {
3296 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3297 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3298 }
3299 }
3300#endif
3301
Paul Bakker5121ce52009-01-03 21:22:43 +00003302 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3303 {
3304 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3305 return( ret );
3306 }
3307
3308 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3309 {
3310 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003311 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 }
3313
Paul Bakker1ef83d62012-04-11 12:09:53 +00003314 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3316
3317 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3318 ssl->in_hslen != 4 + hash_len )
3319 {
3320 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003321 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003322 }
3323
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003324 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 {
3326 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003327 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 }
3329
Paul Bakker48916f92012-09-16 19:57:18 +00003330 ssl->verify_data_len = hash_len;
3331 memcpy( ssl->peer_verify_data, buf, hash_len );
3332
Paul Bakker0a597072012-09-25 21:55:46 +00003333 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003334 {
3335 if( ssl->endpoint == SSL_IS_CLIENT )
3336 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3337
3338 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003339 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003340 }
3341 else
3342 ssl->state++;
3343
3344 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3345
3346 return( 0 );
3347}
3348
Paul Bakker968afaa2014-07-09 11:09:24 +02003349static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003350{
3351 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3352
3353#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3354 defined(POLARSSL_SSL_PROTO_TLS1_1)
3355 md5_init( &handshake->fin_md5 );
3356 sha1_init( &handshake->fin_sha1 );
3357 md5_starts( &handshake->fin_md5 );
3358 sha1_starts( &handshake->fin_sha1 );
3359#endif
3360#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3361#if defined(POLARSSL_SHA256_C)
3362 sha256_init( &handshake->fin_sha256 );
3363 sha256_starts( &handshake->fin_sha256, 0 );
3364#endif
3365#if defined(POLARSSL_SHA512_C)
3366 sha512_init( &handshake->fin_sha512 );
3367 sha512_starts( &handshake->fin_sha512, 1 );
3368#endif
3369#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3370
3371 handshake->update_checksum = ssl_update_checksum_start;
3372 handshake->sig_alg = SSL_HASH_SHA1;
3373
3374#if defined(POLARSSL_DHM_C)
3375 dhm_init( &handshake->dhm_ctx );
3376#endif
3377#if defined(POLARSSL_ECDH_C)
3378 ecdh_init( &handshake->ecdh_ctx );
3379#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003380}
3381
3382static void ssl_transform_init( ssl_transform *transform )
3383{
3384 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003385
3386 cipher_init( &transform->cipher_ctx_enc );
3387 cipher_init( &transform->cipher_ctx_dec );
3388
3389 md_init( &transform->md_ctx_enc );
3390 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003391}
3392
3393void ssl_session_init( ssl_session *session )
3394{
3395 memset( session, 0, sizeof(ssl_session) );
3396}
3397
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003398static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003399{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003400 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003401 if( ssl->transform_negotiate )
3402 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003403 if( ssl->session_negotiate )
3404 ssl_session_free( ssl->session_negotiate );
3405 if( ssl->handshake )
3406 ssl_handshake_free( ssl->handshake );
3407
3408 /*
3409 * Either the pointers are now NULL or cleared properly and can be freed.
3410 * Now allocate missing structures.
3411 */
3412 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003413 {
3414 ssl->transform_negotiate =
3415 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3416 }
Paul Bakker48916f92012-09-16 19:57:18 +00003417
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003418 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003419 {
3420 ssl->session_negotiate =
3421 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3422 }
Paul Bakker48916f92012-09-16 19:57:18 +00003423
Paul Bakker82788fb2014-10-20 13:59:19 +02003424 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003425 {
3426 ssl->handshake = (ssl_handshake_params *)
3427 polarssl_malloc( sizeof(ssl_handshake_params) );
3428 }
Paul Bakker48916f92012-09-16 19:57:18 +00003429
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003430 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003431 if( ssl->handshake == NULL ||
3432 ssl->transform_negotiate == NULL ||
3433 ssl->session_negotiate == NULL )
3434 {
3435 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003436
3437 polarssl_free( ssl->handshake );
3438 polarssl_free( ssl->transform_negotiate );
3439 polarssl_free( ssl->session_negotiate );
3440
3441 ssl->handshake = NULL;
3442 ssl->transform_negotiate = NULL;
3443 ssl->session_negotiate = NULL;
3444
Paul Bakker48916f92012-09-16 19:57:18 +00003445 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3446 }
3447
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003448 /* Initialize structures */
3449 ssl_session_init( ssl->session_negotiate );
3450 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003451 ssl_handshake_params_init( ssl->handshake );
3452
3453#if defined(POLARSSL_X509_CRT_PARSE_C)
3454 ssl->handshake->key_cert = ssl->key_cert;
3455#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003456
Paul Bakker48916f92012-09-16 19:57:18 +00003457 return( 0 );
3458}
3459
Paul Bakker5121ce52009-01-03 21:22:43 +00003460/*
3461 * Initialize an SSL context
3462 */
3463int ssl_init( ssl_context *ssl )
3464{
Paul Bakker48916f92012-09-16 19:57:18 +00003465 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003466 int len = SSL_BUFFER_LEN;
3467
3468 memset( ssl, 0, sizeof( ssl_context ) );
3469
Paul Bakker62f2dee2012-09-28 07:31:51 +00003470 /*
3471 * Sane defaults
3472 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003473 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3474 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3475 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3476 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003477
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003478 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003479
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003480 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3481
Paul Bakker62f2dee2012-09-28 07:31:51 +00003482#if defined(POLARSSL_DHM_C)
3483 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3484 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3485 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3486 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3487 {
3488 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3489 return( ret );
3490 }
3491#endif
3492
3493 /*
3494 * Prepare base structures
3495 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003496 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003497 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003498 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 ssl->in_msg = ssl->in_ctr + 13;
3500
3501 if( ssl->in_ctr == NULL )
3502 {
3503 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003504 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003505 }
3506
Paul Bakker6e339b52013-07-03 13:37:05 +02003507 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003508 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003509 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003510 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
3512 if( ssl->out_ctr == NULL )
3513 {
3514 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003515 polarssl_free( ssl->in_ctr );
3516 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003517 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003518 }
3519
3520 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3521 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3522
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003523#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3524 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3525#endif
3526
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003527#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3528 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3529#endif
3530
Paul Bakker606b4ba2013-08-14 16:52:14 +02003531#if defined(POLARSSL_SSL_SESSION_TICKETS)
3532 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3533#endif
3534
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003535#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003536 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003537#endif
3538
Paul Bakker48916f92012-09-16 19:57:18 +00003539 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3540 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
3542 return( 0 );
3543}
3544
3545/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003546 * Reset an initialized and used SSL context for re-use while retaining
3547 * all application-set variables, function pointers and data.
3548 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003549int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003550{
Paul Bakker48916f92012-09-16 19:57:18 +00003551 int ret;
3552
Paul Bakker7eb013f2011-10-06 12:37:39 +00003553 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003554 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3555 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3556
3557 ssl->verify_data_len = 0;
3558 memset( ssl->own_verify_data, 0, 36 );
3559 memset( ssl->peer_verify_data, 0, 36 );
3560
Paul Bakker7eb013f2011-10-06 12:37:39 +00003561 ssl->in_offt = NULL;
3562
Paul Bakker92be97b2013-01-02 17:30:03 +01003563 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003564 ssl->in_msgtype = 0;
3565 ssl->in_msglen = 0;
3566 ssl->in_left = 0;
3567
3568 ssl->in_hslen = 0;
3569 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003570 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003571
Paul Bakker92be97b2013-01-02 17:30:03 +01003572 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003573 ssl->out_msgtype = 0;
3574 ssl->out_msglen = 0;
3575 ssl->out_left = 0;
3576
Paul Bakker48916f92012-09-16 19:57:18 +00003577 ssl->transform_in = NULL;
3578 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003579
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003580 ssl->renego_records_seen = 0;
3581
Paul Bakker7eb013f2011-10-06 12:37:39 +00003582 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3583 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003584
3585#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003586 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003587 {
3588 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003589 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003590 {
3591 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3592 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3593 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003594 }
3595#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003596
Paul Bakker48916f92012-09-16 19:57:18 +00003597 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003598 {
Paul Bakker48916f92012-09-16 19:57:18 +00003599 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003600 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003601 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003602 }
Paul Bakker48916f92012-09-16 19:57:18 +00003603
Paul Bakkerc0463502013-02-14 11:19:38 +01003604 if( ssl->session )
3605 {
3606 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003607 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003608 ssl->session = NULL;
3609 }
3610
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003611#if defined(POLARSSL_SSL_ALPN)
3612 ssl->alpn_chosen = NULL;
3613#endif
3614
Paul Bakker48916f92012-09-16 19:57:18 +00003615 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3616 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617
3618 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003619}
3620
Paul Bakkera503a632013-08-14 13:48:06 +02003621#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003622static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3623{
3624 aes_free( &tkeys->enc );
3625 aes_free( &tkeys->dec );
3626
3627 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3628}
3629
Paul Bakker7eb013f2011-10-06 12:37:39 +00003630/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003631 * Allocate and initialize ticket keys
3632 */
3633static int ssl_ticket_keys_init( ssl_context *ssl )
3634{
3635 int ret;
3636 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003637 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003638
3639 if( ssl->ticket_keys != NULL )
3640 return( 0 );
3641
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003642 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3643 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003644 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3645
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003646 aes_init( &tkeys->enc );
3647 aes_init( &tkeys->dec );
3648
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003649 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003650 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003651 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003652 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003653 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003654 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003655
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003656 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3657 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3658 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3659 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003660 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003661 polarssl_free( tkeys );
3662 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003663 }
3664
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003665 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003666 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003667 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003668 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003669 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003670 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003671
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003672 ssl->ticket_keys = tkeys;
3673
3674 return( 0 );
3675}
Paul Bakkera503a632013-08-14 13:48:06 +02003676#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003677
3678/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 * SSL set accessors
3680 */
3681void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3682{
3683 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003684
Paul Bakker606b4ba2013-08-14 16:52:14 +02003685#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003686 if( endpoint == SSL_IS_CLIENT )
3687 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003688#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003689}
3690
3691void ssl_set_authmode( ssl_context *ssl, int authmode )
3692{
3693 ssl->authmode = authmode;
3694}
3695
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003696#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003697void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003698 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003699 void *p_vrfy )
3700{
3701 ssl->f_vrfy = f_vrfy;
3702 ssl->p_vrfy = p_vrfy;
3703}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003704#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003705
Paul Bakker5121ce52009-01-03 21:22:43 +00003706void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003707 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 void *p_rng )
3709{
3710 ssl->f_rng = f_rng;
3711 ssl->p_rng = p_rng;
3712}
3713
3714void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003715 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 void *p_dbg )
3717{
3718 ssl->f_dbg = f_dbg;
3719 ssl->p_dbg = p_dbg;
3720}
3721
3722void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003723 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003724 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003725{
3726 ssl->f_recv = f_recv;
3727 ssl->f_send = f_send;
3728 ssl->p_recv = p_recv;
3729 ssl->p_send = p_send;
3730}
3731
Paul Bakker0a597072012-09-25 21:55:46 +00003732void ssl_set_session_cache( ssl_context *ssl,
3733 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3734 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003735{
Paul Bakker0a597072012-09-25 21:55:46 +00003736 ssl->f_get_cache = f_get_cache;
3737 ssl->p_get_cache = p_get_cache;
3738 ssl->f_set_cache = f_set_cache;
3739 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003740}
3741
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003742int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003743{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003744 int ret;
3745
3746 if( ssl == NULL ||
3747 session == NULL ||
3748 ssl->session_negotiate == NULL ||
3749 ssl->endpoint != SSL_IS_CLIENT )
3750 {
3751 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3752 }
3753
3754 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3755 return( ret );
3756
Paul Bakker0a597072012-09-25 21:55:46 +00003757 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003758
3759 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003760}
3761
Paul Bakkerb68cad62012-08-23 08:34:18 +00003762void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003763{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003764 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3765 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3766 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3767 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3768}
3769
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003770void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3771 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003772 int major, int minor )
3773{
3774 if( major != SSL_MAJOR_VERSION_3 )
3775 return;
3776
3777 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3778 return;
3779
3780 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003781}
3782
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003783#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003784/* Add a new (empty) key_cert entry an return a pointer to it */
3785static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3786{
3787 ssl_key_cert *key_cert, *last;
3788
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003789 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3790 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003791 return( NULL );
3792
3793 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3794
3795 /* Append the new key_cert to the (possibly empty) current list */
3796 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003797 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003798 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003799 if( ssl->handshake != NULL )
3800 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003801 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003802 else
3803 {
3804 last = ssl->key_cert;
3805 while( last->next != NULL )
3806 last = last->next;
3807 last->next = key_cert;
3808 }
3809
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003810 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003811}
3812
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003813void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003814 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003815{
3816 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003817 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003818 ssl->peer_cn = peer_cn;
3819}
3820
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003821int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003822 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003823{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003824 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3825
3826 if( key_cert == NULL )
3827 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3828
3829 key_cert->cert = own_cert;
3830 key_cert->key = pk_key;
3831
3832 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003833}
3834
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003835#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003836int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003837 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003838{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003839 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003840 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003841
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003842 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003843 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3844
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003845 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3846 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003847 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003848
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003849 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003850
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003851 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003852 if( ret != 0 )
3853 return( ret );
3854
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003855 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003856 return( ret );
3857
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003858 key_cert->cert = own_cert;
3859 key_cert->key_own_alloc = 1;
3860
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003861 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003862}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003863#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003864
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003865int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003866 void *rsa_key,
3867 rsa_decrypt_func rsa_decrypt,
3868 rsa_sign_func rsa_sign,
3869 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003870{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003871 int ret;
3872 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003873
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003874 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003875 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3876
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003877 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3878 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003879 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003880
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003881 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003882
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003883 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3884 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3885 return( ret );
3886
3887 key_cert->cert = own_cert;
3888 key_cert->key_own_alloc = 1;
3889
3890 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003891}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003892#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003893
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003894#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003895int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3896 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003897{
Paul Bakker6db455e2013-09-18 17:29:31 +02003898 if( psk == NULL || psk_identity == NULL )
3899 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3900
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003901 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003902 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3903
Paul Bakker6db455e2013-09-18 17:29:31 +02003904 if( ssl->psk != NULL )
3905 {
3906 polarssl_free( ssl->psk );
3907 polarssl_free( ssl->psk_identity );
3908 }
3909
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003910 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003911 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003912
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003913 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003914 ssl->psk_identity = (unsigned char *)
3915 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003916
3917 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3918 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3919
3920 memcpy( ssl->psk, psk, ssl->psk_len );
3921 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003922
3923 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003924}
3925
3926void ssl_set_psk_cb( ssl_context *ssl,
3927 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3928 size_t),
3929 void *p_psk )
3930{
3931 ssl->f_psk = f_psk;
3932 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003933}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003934#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003935
Paul Bakker48916f92012-09-16 19:57:18 +00003936#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003937int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003938{
3939 int ret;
3940
Paul Bakker48916f92012-09-16 19:57:18 +00003941 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 {
3943 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3944 return( ret );
3945 }
3946
Paul Bakker48916f92012-09-16 19:57:18 +00003947 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003948 {
3949 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3950 return( ret );
3951 }
3952
3953 return( 0 );
3954}
3955
Paul Bakker1b57b062011-01-06 15:48:19 +00003956int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3957{
3958 int ret;
3959
Paul Bakker66d5d072014-06-17 16:39:18 +02003960 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003961 {
3962 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3963 return( ret );
3964 }
3965
Paul Bakker66d5d072014-06-17 16:39:18 +02003966 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003967 {
3968 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3969 return( ret );
3970 }
3971
3972 return( 0 );
3973}
Paul Bakker48916f92012-09-16 19:57:18 +00003974#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003975
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003976#if defined(POLARSSL_SSL_SET_CURVES)
3977/*
3978 * Set the allowed elliptic curves
3979 */
3980void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3981{
3982 ssl->curve_list = curve_list;
3983}
3984#endif
3985
Paul Bakker0be444a2013-08-27 21:55:01 +02003986#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003987int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003988{
3989 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003990 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003991
3992 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003993
3994 if( ssl->hostname_len + 1 == 0 )
3995 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3996
Paul Bakker6e339b52013-07-03 13:37:05 +02003997 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003998
Paul Bakkerb15b8512012-01-13 13:44:06 +00003999 if( ssl->hostname == NULL )
4000 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4001
Paul Bakker3c2122f2013-06-24 19:03:14 +02004002 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004003 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004004
Paul Bakker40ea7de2009-05-03 10:18:48 +00004005 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
4007 return( 0 );
4008}
4009
Paul Bakker5701cdc2012-09-27 21:49:42 +00004010void ssl_set_sni( ssl_context *ssl,
4011 int (*f_sni)(void *, ssl_context *,
4012 const unsigned char *, size_t),
4013 void *p_sni )
4014{
4015 ssl->f_sni = f_sni;
4016 ssl->p_sni = p_sni;
4017}
Paul Bakker0be444a2013-08-27 21:55:01 +02004018#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004019
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004020#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004021int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004022{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004023 size_t cur_len, tot_len;
4024 const char **p;
4025
4026 /*
4027 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4028 * truncated". Check lengths now rather than later.
4029 */
4030 tot_len = 0;
4031 for( p = protos; *p != NULL; p++ )
4032 {
4033 cur_len = strlen( *p );
4034 tot_len += cur_len;
4035
4036 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4037 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4038 }
4039
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004040 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004041
4042 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004043}
4044
4045const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4046{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004047 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004048}
4049#endif /* POLARSSL_SSL_ALPN */
4050
Paul Bakker490ecc82011-10-06 13:04:09 +00004051void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4052{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004053 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4054 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4055 {
4056 ssl->max_major_ver = major;
4057 ssl->max_minor_ver = minor;
4058 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004059}
4060
Paul Bakker1d29fb52012-09-28 13:28:45 +00004061void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4062{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004063 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4064 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4065 {
4066 ssl->min_major_ver = major;
4067 ssl->min_minor_ver = minor;
4068 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004069}
4070
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004071#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4072void ssl_set_fallback( ssl_context *ssl, char fallback )
4073{
4074 ssl->fallback = fallback;
4075}
4076#endif
4077
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004078#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4079void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4080{
4081 ssl->encrypt_then_mac = etm;
4082}
4083#endif
4084
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004085#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4086void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4087{
4088 ssl->extended_ms = ems;
4089}
4090#endif
4091
Paul Bakker05decb22013-08-15 13:33:48 +02004092#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004093int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4094{
Paul Bakker77e257e2013-12-16 15:29:52 +01004095 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004096 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004097 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004098 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004099 }
4100
4101 ssl->mfl_code = mfl_code;
4102
4103 return( 0 );
4104}
Paul Bakker05decb22013-08-15 13:33:48 +02004105#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004106
Paul Bakker1f2bc622013-08-15 13:45:55 +02004107#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004108int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004109{
4110 if( ssl->endpoint != SSL_IS_CLIENT )
4111 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4112
Paul Bakker8c1ede62013-07-19 14:14:37 +02004113 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004114
4115 return( 0 );
4116}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004117#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004118
Paul Bakker48916f92012-09-16 19:57:18 +00004119void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4120{
4121 ssl->disable_renegotiation = renegotiation;
4122}
4123
4124void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4125{
4126 ssl->allow_legacy_renegotiation = allow_legacy;
4127}
4128
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004129void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4130{
4131 ssl->renego_max_records = max_records;
4132}
4133
Paul Bakkera503a632013-08-14 13:48:06 +02004134#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004135int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4136{
4137 ssl->session_tickets = use_tickets;
4138
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004139 if( ssl->endpoint == SSL_IS_CLIENT )
4140 return( 0 );
4141
4142 if( ssl->f_rng == NULL )
4143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4144
4145 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004146}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004147
4148void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4149{
4150 ssl->ticket_lifetime = lifetime;
4151}
Paul Bakkera503a632013-08-14 13:48:06 +02004152#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004153
Paul Bakker5121ce52009-01-03 21:22:43 +00004154/*
4155 * SSL get accessors
4156 */
Paul Bakker23986e52011-04-24 08:57:21 +00004157size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004158{
4159 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4160}
4161
Paul Bakkerff60ee62010-03-16 21:09:09 +00004162int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004163{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004164 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004165}
4166
Paul Bakkere3166ce2011-01-27 17:40:50 +00004167const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004168{
Paul Bakker926c8e42013-03-06 10:23:34 +01004169 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004170 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004171
Paul Bakkere3166ce2011-01-27 17:40:50 +00004172 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004173}
4174
Paul Bakker43ca69c2011-01-15 17:35:19 +00004175const char *ssl_get_version( const ssl_context *ssl )
4176{
4177 switch( ssl->minor_ver )
4178 {
4179 case SSL_MINOR_VERSION_0:
4180 return( "SSLv3.0" );
4181
4182 case SSL_MINOR_VERSION_1:
4183 return( "TLSv1.0" );
4184
4185 case SSL_MINOR_VERSION_2:
4186 return( "TLSv1.1" );
4187
Paul Bakker1ef83d62012-04-11 12:09:53 +00004188 case SSL_MINOR_VERSION_3:
4189 return( "TLSv1.2" );
4190
Paul Bakker43ca69c2011-01-15 17:35:19 +00004191 default:
4192 break;
4193 }
4194 return( "unknown" );
4195}
4196
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004197#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004198const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004199{
4200 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004201 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004202
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004203 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004204}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004205#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004206
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004207int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4208{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004209 if( ssl == NULL ||
4210 dst == NULL ||
4211 ssl->session == NULL ||
4212 ssl->endpoint != SSL_IS_CLIENT )
4213 {
4214 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4215 }
4216
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004217 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004218}
4219
Paul Bakker5121ce52009-01-03 21:22:43 +00004220/*
Paul Bakker1961b702013-01-25 14:49:24 +01004221 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004222 */
Paul Bakker1961b702013-01-25 14:49:24 +01004223int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004224{
Paul Bakker40e46942009-01-03 21:51:57 +00004225 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004226
Paul Bakker40e46942009-01-03 21:51:57 +00004227#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004228 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004229 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004230#endif
4231
Paul Bakker40e46942009-01-03 21:51:57 +00004232#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004233 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004234 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004235#endif
4236
Paul Bakker1961b702013-01-25 14:49:24 +01004237 return( ret );
4238}
4239
4240/*
4241 * Perform the SSL handshake
4242 */
4243int ssl_handshake( ssl_context *ssl )
4244{
4245 int ret = 0;
4246
4247 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4248
4249 while( ssl->state != SSL_HANDSHAKE_OVER )
4250 {
4251 ret = ssl_handshake_step( ssl );
4252
4253 if( ret != 0 )
4254 break;
4255 }
4256
Paul Bakker5121ce52009-01-03 21:22:43 +00004257 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4258
4259 return( ret );
4260}
4261
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004262#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004263/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004264 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004265 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004266static int ssl_write_hello_request( ssl_context *ssl )
4267{
4268 int ret;
4269
4270 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4271
4272 ssl->out_msglen = 4;
4273 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4274 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4275
4276 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4277 {
4278 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4279 return( ret );
4280 }
4281
4282 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4283
4284 return( 0 );
4285}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004286#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004287
4288/*
4289 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004290 * - any side: calling ssl_renegotiate(),
4291 * - client: receiving a HelloRequest during ssl_read(),
4292 * - server: receiving any handshake message on server during ssl_read() after
4293 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004294 * If the handshake doesn't complete due to waiting for I/O, it will continue
4295 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004296 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004297static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004298{
4299 int ret;
4300
4301 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4302
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004303 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4304 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004305
4306 ssl->state = SSL_HELLO_REQUEST;
4307 ssl->renegotiation = SSL_RENEGOTIATION;
4308
Paul Bakker48916f92012-09-16 19:57:18 +00004309 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4310 {
4311 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4312 return( ret );
4313 }
4314
4315 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4316
4317 return( 0 );
4318}
4319
4320/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004321 * Renegotiate current connection on client,
4322 * or request renegotiation on server
4323 */
4324int ssl_renegotiate( ssl_context *ssl )
4325{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004326 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004327
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004328#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004329 /* On server, just send the request */
4330 if( ssl->endpoint == SSL_IS_SERVER )
4331 {
4332 if( ssl->state != SSL_HANDSHAKE_OVER )
4333 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4334
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004335 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4336
4337 /* Did we already try/start sending HelloRequest? */
4338 if( ssl->out_left != 0 )
4339 return( ssl_flush_output( ssl ) );
4340
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004341 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004342 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004343#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004344
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004345#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004346 /*
4347 * On client, either start the renegotiation process or,
4348 * if already in progress, continue the handshake
4349 */
4350 if( ssl->renegotiation != SSL_RENEGOTIATION )
4351 {
4352 if( ssl->state != SSL_HANDSHAKE_OVER )
4353 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4354
4355 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4356 {
4357 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4358 return( ret );
4359 }
4360 }
4361 else
4362 {
4363 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4364 {
4365 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4366 return( ret );
4367 }
4368 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004369#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004370
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004371 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004372}
4373
4374/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004375 * Receive application data decrypted from the SSL layer
4376 */
Paul Bakker23986e52011-04-24 08:57:21 +00004377int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004378{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004379 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004380 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004381
4382 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4383
4384 if( ssl->state != SSL_HANDSHAKE_OVER )
4385 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004386 ret = ssl_handshake( ssl );
4387 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4388 {
4389 record_read = 1;
4390 }
4391 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004392 {
4393 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4394 return( ret );
4395 }
4396 }
4397
4398 if( ssl->in_offt == NULL )
4399 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004400 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004402 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4403 {
4404 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4405 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004406
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004407 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4408 return( ret );
4409 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004410 }
4411
4412 if( ssl->in_msglen == 0 &&
4413 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4414 {
4415 /*
4416 * OpenSSL sends empty messages to randomize the IV
4417 */
4418 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4419 {
Paul Bakker831a7552011-05-18 13:32:51 +00004420 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4421 return( 0 );
4422
Paul Bakker5121ce52009-01-03 21:22:43 +00004423 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4424 return( ret );
4425 }
4426 }
4427
Paul Bakker48916f92012-09-16 19:57:18 +00004428 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4429 {
4430 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4431
4432 if( ssl->endpoint == SSL_IS_CLIENT &&
4433 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4434 ssl->in_hslen != 4 ) )
4435 {
4436 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4437 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4438 }
4439
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004440 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4441 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004442 ssl->allow_legacy_renegotiation ==
4443 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004444 {
4445 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4446
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004447#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004448 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004449 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004450 /*
4451 * SSLv3 does not have a "no_renegotiation" alert
4452 */
4453 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4454 return( ret );
4455 }
4456 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004457#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004458#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4459 defined(POLARSSL_SSL_PROTO_TLS1_2)
4460 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004461 {
4462 if( ( ret = ssl_send_alert_message( ssl,
4463 SSL_ALERT_LEVEL_WARNING,
4464 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4465 {
4466 return( ret );
4467 }
Paul Bakker48916f92012-09-16 19:57:18 +00004468 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004469 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004470#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4471 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004472 {
4473 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004474 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004475 }
Paul Bakker48916f92012-09-16 19:57:18 +00004476 }
4477 else
4478 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004479 ret = ssl_start_renegotiation( ssl );
4480 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4481 {
4482 record_read = 1;
4483 }
4484 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004485 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004486 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004487 return( ret );
4488 }
Paul Bakker48916f92012-09-16 19:57:18 +00004489 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004490
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004491 /* If a non-handshake record was read during renego, fallthrough,
4492 * else tell the user they should call ssl_read() again */
4493 if( ! record_read )
4494 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004495 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004496 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4497 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004498 ssl->renego_records_seen++;
4499
4500 if( ssl->renego_max_records >= 0 &&
4501 ssl->renego_records_seen > ssl->renego_max_records )
4502 {
4503 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4504 "but not honored by client" ) );
4505 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4506 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004507 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004508
4509 /* Fatal and closure alerts handled by ssl_read_record() */
4510 if( ssl->in_msgtype == SSL_MSG_ALERT )
4511 {
4512 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4513 return( POLARSSL_ERR_NET_WANT_READ );
4514 }
4515
4516 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004517 {
4518 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004519 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004520 }
4521
4522 ssl->in_offt = ssl->in_msg;
4523 }
4524
4525 n = ( len < ssl->in_msglen )
4526 ? len : ssl->in_msglen;
4527
4528 memcpy( buf, ssl->in_offt, n );
4529 ssl->in_msglen -= n;
4530
4531 if( ssl->in_msglen == 0 )
4532 /* all bytes consumed */
4533 ssl->in_offt = NULL;
4534 else
4535 /* more data available */
4536 ssl->in_offt += n;
4537
4538 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4539
Paul Bakker23986e52011-04-24 08:57:21 +00004540 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004541}
4542
4543/*
4544 * Send application data to be encrypted by the SSL layer
4545 */
Paul Bakker23986e52011-04-24 08:57:21 +00004546int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004547{
Paul Bakker23986e52011-04-24 08:57:21 +00004548 int ret;
4549 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004550 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004551
4552 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4553
4554 if( ssl->state != SSL_HANDSHAKE_OVER )
4555 {
4556 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4557 {
4558 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4559 return( ret );
4560 }
4561 }
4562
Paul Bakker05decb22013-08-15 13:33:48 +02004563#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004564 /*
4565 * Assume mfl_code is correct since it was checked when set
4566 */
4567 max_len = mfl_code_to_length[ssl->mfl_code];
4568
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004569 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004570 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004571 */
4572 if( ssl->session_out != NULL &&
4573 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4574 {
4575 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4576 }
Paul Bakker05decb22013-08-15 13:33:48 +02004577#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004578
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004579 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004580
Paul Bakker5121ce52009-01-03 21:22:43 +00004581 if( ssl->out_left != 0 )
4582 {
4583 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4584 {
4585 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4586 return( ret );
4587 }
4588 }
Paul Bakker887bd502011-06-08 13:10:54 +00004589 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004590 {
Paul Bakker887bd502011-06-08 13:10:54 +00004591 ssl->out_msglen = n;
4592 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4593 memcpy( ssl->out_msg, buf, n );
4594
4595 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4596 {
4597 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4598 return( ret );
4599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004600 }
4601
4602 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4603
Paul Bakker23986e52011-04-24 08:57:21 +00004604 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004605}
4606
4607/*
4608 * Notify the peer that the connection is being closed
4609 */
4610int ssl_close_notify( ssl_context *ssl )
4611{
4612 int ret;
4613
4614 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4615
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004616 if( ssl->out_left != 0 )
4617 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004618
4619 if( ssl->state == SSL_HANDSHAKE_OVER )
4620 {
Paul Bakker48916f92012-09-16 19:57:18 +00004621 if( ( ret = ssl_send_alert_message( ssl,
4622 SSL_ALERT_LEVEL_WARNING,
4623 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004624 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004625 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004626 return( ret );
4627 }
4628 }
4629
4630 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4631
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004632 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004633}
4634
Paul Bakker48916f92012-09-16 19:57:18 +00004635void ssl_transform_free( ssl_transform *transform )
4636{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004637 if( transform == NULL )
4638 return;
4639
Paul Bakker48916f92012-09-16 19:57:18 +00004640#if defined(POLARSSL_ZLIB_SUPPORT)
4641 deflateEnd( &transform->ctx_deflate );
4642 inflateEnd( &transform->ctx_inflate );
4643#endif
4644
Paul Bakker84bbeb52014-07-01 14:53:22 +02004645 cipher_free( &transform->cipher_ctx_enc );
4646 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004647
Paul Bakker84bbeb52014-07-01 14:53:22 +02004648 md_free( &transform->md_ctx_enc );
4649 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004650
Paul Bakker34617722014-06-13 17:20:13 +02004651 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004652}
4653
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004654#if defined(POLARSSL_X509_CRT_PARSE_C)
4655static void ssl_key_cert_free( ssl_key_cert *key_cert )
4656{
4657 ssl_key_cert *cur = key_cert, *next;
4658
4659 while( cur != NULL )
4660 {
4661 next = cur->next;
4662
4663 if( cur->key_own_alloc )
4664 {
4665 pk_free( cur->key );
4666 polarssl_free( cur->key );
4667 }
4668 polarssl_free( cur );
4669
4670 cur = next;
4671 }
4672}
4673#endif /* POLARSSL_X509_CRT_PARSE_C */
4674
Paul Bakker48916f92012-09-16 19:57:18 +00004675void ssl_handshake_free( ssl_handshake_params *handshake )
4676{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004677 if( handshake == NULL )
4678 return;
4679
Paul Bakker48916f92012-09-16 19:57:18 +00004680#if defined(POLARSSL_DHM_C)
4681 dhm_free( &handshake->dhm_ctx );
4682#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004683#if defined(POLARSSL_ECDH_C)
4684 ecdh_free( &handshake->ecdh_ctx );
4685#endif
4686
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004687#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004688 /* explicit void pointer cast for buggy MS compiler */
4689 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004690#endif
4691
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004692#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4693 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4694 /*
4695 * Free only the linked list wrapper, not the keys themselves
4696 * since the belong to the SNI callback
4697 */
4698 if( handshake->sni_key_cert != NULL )
4699 {
4700 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4701
4702 while( cur != NULL )
4703 {
4704 next = cur->next;
4705 polarssl_free( cur );
4706 cur = next;
4707 }
4708 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004709#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004710
Paul Bakker34617722014-06-13 17:20:13 +02004711 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004712}
4713
4714void ssl_session_free( ssl_session *session )
4715{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004716 if( session == NULL )
4717 return;
4718
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004719#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004720 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004721 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004722 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004723 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004724 }
Paul Bakkered27a042013-04-18 22:46:23 +02004725#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004726
Paul Bakkera503a632013-08-14 13:48:06 +02004727#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004728 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004729#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004730
Paul Bakker34617722014-06-13 17:20:13 +02004731 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004732}
4733
Paul Bakker5121ce52009-01-03 21:22:43 +00004734/*
4735 * Free an SSL context
4736 */
4737void ssl_free( ssl_context *ssl )
4738{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004739 if( ssl == NULL )
4740 return;
4741
Paul Bakker5121ce52009-01-03 21:22:43 +00004742 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4743
Paul Bakker5121ce52009-01-03 21:22:43 +00004744 if( ssl->out_ctr != NULL )
4745 {
Paul Bakker34617722014-06-13 17:20:13 +02004746 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004747 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004748 }
4749
4750 if( ssl->in_ctr != NULL )
4751 {
Paul Bakker34617722014-06-13 17:20:13 +02004752 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004753 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 }
4755
Paul Bakker16770332013-10-11 09:59:44 +02004756#if defined(POLARSSL_ZLIB_SUPPORT)
4757 if( ssl->compress_buf != NULL )
4758 {
Paul Bakker34617722014-06-13 17:20:13 +02004759 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004760 polarssl_free( ssl->compress_buf );
4761 }
4762#endif
4763
Paul Bakker40e46942009-01-03 21:51:57 +00004764#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004765 mpi_free( &ssl->dhm_P );
4766 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004767#endif
4768
Paul Bakker48916f92012-09-16 19:57:18 +00004769 if( ssl->transform )
4770 {
4771 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004772 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004773 }
4774
4775 if( ssl->handshake )
4776 {
4777 ssl_handshake_free( ssl->handshake );
4778 ssl_transform_free( ssl->transform_negotiate );
4779 ssl_session_free( ssl->session_negotiate );
4780
Paul Bakker6e339b52013-07-03 13:37:05 +02004781 polarssl_free( ssl->handshake );
4782 polarssl_free( ssl->transform_negotiate );
4783 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004784 }
4785
Paul Bakkerc0463502013-02-14 11:19:38 +01004786 if( ssl->session )
4787 {
4788 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004789 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004790 }
4791
Paul Bakkera503a632013-08-14 13:48:06 +02004792#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004793 if( ssl->ticket_keys )
4794 {
4795 ssl_ticket_keys_free( ssl->ticket_keys );
4796 polarssl_free( ssl->ticket_keys );
4797 }
Paul Bakkera503a632013-08-14 13:48:06 +02004798#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004799
Paul Bakker0be444a2013-08-27 21:55:01 +02004800#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004801 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 {
Paul Bakker34617722014-06-13 17:20:13 +02004803 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004804 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004805 ssl->hostname_len = 0;
4806 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004807#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004808
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004809#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004810 if( ssl->psk != NULL )
4811 {
Paul Bakker34617722014-06-13 17:20:13 +02004812 polarssl_zeroize( ssl->psk, ssl->psk_len );
4813 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004814 polarssl_free( ssl->psk );
4815 polarssl_free( ssl->psk_identity );
4816 ssl->psk_len = 0;
4817 ssl->psk_identity_len = 0;
4818 }
4819#endif
4820
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004821#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004822 ssl_key_cert_free( ssl->key_cert );
4823#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004824
Paul Bakker05ef8352012-05-08 09:17:57 +00004825#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4826 if( ssl_hw_record_finish != NULL )
4827 {
4828 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4829 ssl_hw_record_finish( ssl );
4830 }
4831#endif
4832
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004834
Paul Bakker86f04f42013-02-14 11:20:09 +01004835 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004836 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004837}
4838
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004839#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004840/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004841 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004842 */
4843unsigned char ssl_sig_from_pk( pk_context *pk )
4844{
4845#if defined(POLARSSL_RSA_C)
4846 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4847 return( SSL_SIG_RSA );
4848#endif
4849#if defined(POLARSSL_ECDSA_C)
4850 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4851 return( SSL_SIG_ECDSA );
4852#endif
4853 return( SSL_SIG_ANON );
4854}
4855
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004856pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4857{
4858 switch( sig )
4859 {
4860#if defined(POLARSSL_RSA_C)
4861 case SSL_SIG_RSA:
4862 return( POLARSSL_PK_RSA );
4863#endif
4864#if defined(POLARSSL_ECDSA_C)
4865 case SSL_SIG_ECDSA:
4866 return( POLARSSL_PK_ECDSA );
4867#endif
4868 default:
4869 return( POLARSSL_PK_NONE );
4870 }
4871}
Paul Bakker9af723c2014-05-01 13:03:14 +02004872#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004873
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004874/*
4875 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4876 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004877md_type_t ssl_md_alg_from_hash( unsigned char hash )
4878{
4879 switch( hash )
4880 {
4881#if defined(POLARSSL_MD5_C)
4882 case SSL_HASH_MD5:
4883 return( POLARSSL_MD_MD5 );
4884#endif
4885#if defined(POLARSSL_SHA1_C)
4886 case SSL_HASH_SHA1:
4887 return( POLARSSL_MD_SHA1 );
4888#endif
4889#if defined(POLARSSL_SHA256_C)
4890 case SSL_HASH_SHA224:
4891 return( POLARSSL_MD_SHA224 );
4892 case SSL_HASH_SHA256:
4893 return( POLARSSL_MD_SHA256 );
4894#endif
4895#if defined(POLARSSL_SHA512_C)
4896 case SSL_HASH_SHA384:
4897 return( POLARSSL_MD_SHA384 );
4898 case SSL_HASH_SHA512:
4899 return( POLARSSL_MD_SHA512 );
4900#endif
4901 default:
4902 return( POLARSSL_MD_NONE );
4903 }
4904}
4905
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004906#if defined(POLARSSL_SSL_SET_CURVES)
4907/*
4908 * Check is a curve proposed by the peer is in our list.
4909 * Return 1 if we're willing to use it, 0 otherwise.
4910 */
4911int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4912{
4913 const ecp_group_id *gid;
4914
4915 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4916 if( *gid == grp_id )
4917 return( 1 );
4918
4919 return( 0 );
4920}
Paul Bakker9af723c2014-05-01 13:03:14 +02004921#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004922
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004923#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004924int ssl_check_cert_usage( const x509_crt *cert,
4925 const ssl_ciphersuite_t *ciphersuite,
4926 int cert_endpoint )
4927{
4928#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4929 int usage = 0;
4930#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004931#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4932 const char *ext_oid;
4933 size_t ext_len;
4934#endif
4935
4936#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4937 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4938 ((void) cert);
4939 ((void) cert_endpoint);
4940#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004941
4942#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4943 if( cert_endpoint == SSL_IS_SERVER )
4944 {
4945 /* Server part of the key exchange */
4946 switch( ciphersuite->key_exchange )
4947 {
4948 case POLARSSL_KEY_EXCHANGE_RSA:
4949 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4950 usage = KU_KEY_ENCIPHERMENT;
4951 break;
4952
4953 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4954 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4955 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4956 usage = KU_DIGITAL_SIGNATURE;
4957 break;
4958
4959 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4960 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4961 usage = KU_KEY_AGREEMENT;
4962 break;
4963
4964 /* Don't use default: we want warnings when adding new values */
4965 case POLARSSL_KEY_EXCHANGE_NONE:
4966 case POLARSSL_KEY_EXCHANGE_PSK:
4967 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4968 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4969 usage = 0;
4970 }
4971 }
4972 else
4973 {
4974 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4975 usage = KU_DIGITAL_SIGNATURE;
4976 }
4977
4978 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4979 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004980#else
4981 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004982#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4983
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004984#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4985 if( cert_endpoint == SSL_IS_SERVER )
4986 {
4987 ext_oid = OID_SERVER_AUTH;
4988 ext_len = OID_SIZE( OID_SERVER_AUTH );
4989 }
4990 else
4991 {
4992 ext_oid = OID_CLIENT_AUTH;
4993 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4994 }
4995
4996 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4997 return( -1 );
4998#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4999
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005000 return( 0 );
5001}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005002#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005003
5004#endif /* POLARSSL_SSL_TLS_C */