blob: e8f2b7fd475e94b6d1221367ccb4b09a5231d9d7 [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
Paul Bakker5121ce52009-01-03 21:22:43 +00001063/*
1064 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001065 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001066static int ssl_encrypt_buf( ssl_context *ssl )
1067{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001068 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001069 const cipher_mode_t mode = cipher_get_cipher_mode(
1070 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
1072 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1073
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001074#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1075 if( ssl->session_out != NULL &&
1076 ssl->session_out->encrypt_then_mac == SSL_ETM_ENABLED )
1077 {
1078 // WIP
1079 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1080 }
1081#endif
1082
Paul Bakker5121ce52009-01-03 21:22:43 +00001083 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001084 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001085 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001086#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1087 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1088 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001089 if( mode != POLARSSL_MODE_GCM &&
1090 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001092#if defined(POLARSSL_SSL_PROTO_SSL3)
1093 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1094 {
1095 ssl_mac( &ssl->transform_out->md_ctx_enc,
1096 ssl->transform_out->mac_enc,
1097 ssl->out_msg, ssl->out_msglen,
1098 ssl->out_ctr, ssl->out_msgtype );
1099 }
1100 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001101#endif
1102#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001103 defined(POLARSSL_SSL_PROTO_TLS1_2)
1104 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1105 {
1106 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1107 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1108 ssl->out_msg, ssl->out_msglen );
1109 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1110 ssl->out_msg + ssl->out_msglen );
1111 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1112 }
1113 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001114#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001115 {
1116 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001117 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001118 }
1119
1120 SSL_DEBUG_BUF( 4, "computed mac",
1121 ssl->out_msg + ssl->out_msglen,
1122 ssl->transform_out->maclen );
1123
1124 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001125 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001126#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001128 /*
1129 * Encrypt
1130 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001131#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001132 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001134 int ret;
1135 size_t olen = 0;
1136
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1138 "including %d bytes of padding",
1139 ssl->out_msglen, 0 ) );
1140
1141 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1142 ssl->out_msg, ssl->out_msglen );
1143
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001144 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001145 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001146 ssl->transform_out->ivlen,
1147 ssl->out_msg, ssl->out_msglen,
1148 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001149 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001150 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001151 return( ret );
1152 }
1153
1154 if( ssl->out_msglen != olen )
1155 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001156 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001157 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001158 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 }
Paul Bakker68884e32013-01-07 18:20:04 +01001160 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001161#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001162#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1163 if( mode == POLARSSL_MODE_GCM ||
1164 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001165 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001166 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001167 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001168 unsigned char *enc_msg;
1169 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001170 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1171 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001172
Paul Bakkerca4ab492012-04-18 14:23:57 +00001173 memcpy( add_data, ssl->out_ctr, 8 );
1174 add_data[8] = ssl->out_msgtype;
1175 add_data[9] = ssl->major_ver;
1176 add_data[10] = ssl->minor_ver;
1177 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1178 add_data[12] = ssl->out_msglen & 0xFF;
1179
1180 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1181 add_data, 13 );
1182
Paul Bakker68884e32013-01-07 18:20:04 +01001183 /*
1184 * Generate IV
1185 */
1186 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001187 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1188 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001189 if( ret != 0 )
1190 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001191
Paul Bakker68884e32013-01-07 18:20:04 +01001192 memcpy( ssl->out_iv,
1193 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1194 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001195
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001196 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001197 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001198
Paul Bakker68884e32013-01-07 18:20:04 +01001199 /*
1200 * Fix pointer positions and message length with added IV
1201 */
1202 enc_msg = ssl->out_msg;
1203 enc_msglen = ssl->out_msglen;
1204 ssl->out_msglen += ssl->transform_out->ivlen -
1205 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001206
Paul Bakker68884e32013-01-07 18:20:04 +01001207 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1208 "including %d bytes of padding",
1209 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001210
Paul Bakker68884e32013-01-07 18:20:04 +01001211 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1212 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001213
Paul Bakker68884e32013-01-07 18:20:04 +01001214 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001215 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001216 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001217 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1218 ssl->transform_out->iv_enc,
1219 ssl->transform_out->ivlen,
1220 add_data, 13,
1221 enc_msg, enc_msglen,
1222 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001223 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001224 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001225 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001226 return( ret );
1227 }
1228
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001229 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001230 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001231 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001232 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001233 }
1234
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001235 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001236
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001237 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001238 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001240#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001241#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1242 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001243 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001245 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001247 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001248
Paul Bakker48916f92012-09-16 19:57:18 +00001249 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1250 ssl->transform_out->ivlen;
1251 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 padlen = 0;
1253
1254 for( i = 0; i <= padlen; i++ )
1255 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1256
1257 ssl->out_msglen += padlen + 1;
1258
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001259 enc_msglen = ssl->out_msglen;
1260 enc_msg = ssl->out_msg;
1261
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001262#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001263 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001264 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1265 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001266 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001267 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001268 {
1269 /*
1270 * Generate IV
1271 */
Paul Bakker48916f92012-09-16 19:57:18 +00001272 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1273 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001274 if( ret != 0 )
1275 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001276
Paul Bakker92be97b2013-01-02 17:30:03 +01001277 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001278 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001279
1280 /*
1281 * Fix pointer positions and message length with added IV
1282 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001283 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001284 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001285 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001286 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001287#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001290 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001291 ssl->out_msglen, ssl->transform_out->ivlen,
1292 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
1294 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001295 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001296
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001297 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001298 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001299 ssl->transform_out->ivlen,
1300 enc_msg, enc_msglen,
1301 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001302 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001303 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001304 return( ret );
1305 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001306
Paul Bakkercca5b812013-08-31 17:40:26 +02001307 if( enc_msglen != olen )
1308 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001309 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001310 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001311 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001312
1313#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001314 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1315 {
1316 /*
1317 * Save IV in SSL3 and TLS1
1318 */
1319 memcpy( ssl->transform_out->iv_enc,
1320 ssl->transform_out->cipher_ctx_enc.iv,
1321 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001323#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001325 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001326#endif /* POLARSSL_CIPHER_MODE_CBC &&
1327 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001328 {
1329 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001330 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Paul Bakkerca4ab492012-04-18 14:23:57 +00001333 for( i = 8; i > 0; i-- )
1334 if( ++ssl->out_ctr[i - 1] != 0 )
1335 break;
1336
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001337 /* The loops goes to its end iff the counter is wrapping */
1338 if( i == 0 )
1339 {
1340 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1341 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1342 }
1343
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1345
1346 return( 0 );
1347}
1348
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001349#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001350
Paul Bakker5121ce52009-01-03 21:22:43 +00001351static int ssl_decrypt_buf( ssl_context *ssl )
1352{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001353 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001354 const cipher_mode_t mode = cipher_get_cipher_mode(
1355 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001356#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1357 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1358 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1359 size_t padlen = 0, correct = 1;
1360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
1362 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1363
Paul Bakker48916f92012-09-16 19:57:18 +00001364 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 {
1366 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001367 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001368 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 }
1370
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001371#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001372 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001373 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001374 int ret;
1375 size_t olen = 0;
1376
Paul Bakker68884e32013-01-07 18:20:04 +01001377 padlen = 0;
1378
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001379 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001380 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001381 ssl->transform_in->ivlen,
1382 ssl->in_msg, ssl->in_msglen,
1383 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001384 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001385 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001386 return( ret );
1387 }
1388
1389 if( ssl->in_msglen != olen )
1390 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001391 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001392 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001393 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 }
Paul Bakker68884e32013-01-07 18:20:04 +01001395 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001396#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001397#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1398 if( mode == POLARSSL_MODE_GCM ||
1399 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001400 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001401 int ret;
1402 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001403 unsigned char *dec_msg;
1404 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001405 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001406 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1407 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001408 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1409 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001410
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001411 if( ssl->in_msglen < explicit_iv_len + taglen )
1412 {
1413 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1414 "+ taglen (%d)", ssl->in_msglen,
1415 explicit_iv_len, taglen ) );
1416 return( POLARSSL_ERR_SSL_INVALID_MAC );
1417 }
1418 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1419
Paul Bakker68884e32013-01-07 18:20:04 +01001420 dec_msg = ssl->in_msg;
1421 dec_msg_result = ssl->in_msg;
1422 ssl->in_msglen = dec_msglen;
1423
1424 memcpy( add_data, ssl->in_ctr, 8 );
1425 add_data[8] = ssl->in_msgtype;
1426 add_data[9] = ssl->major_ver;
1427 add_data[10] = ssl->minor_ver;
1428 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1429 add_data[12] = ssl->in_msglen & 0xFF;
1430
1431 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1432 add_data, 13 );
1433
1434 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1435 ssl->in_iv,
1436 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1437
1438 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1439 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001440 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001441
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001442 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001443 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001444 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001445 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1446 ssl->transform_in->iv_dec,
1447 ssl->transform_in->ivlen,
1448 add_data, 13,
1449 dec_msg, dec_msglen,
1450 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001451 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001452 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001453 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1454
1455 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1456 return( POLARSSL_ERR_SSL_INVALID_MAC );
1457
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001458 return( ret );
1459 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001460
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001461 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001462 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001463 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001464 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001465 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001466 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001467 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001468#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001469#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1470 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001471 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001472 {
Paul Bakker45829992013-01-03 14:52:21 +01001473 /*
1474 * Decrypt and check the padding
1475 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001476 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001477 unsigned char *dec_msg;
1478 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001479 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001480 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001481 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001482
Paul Bakker5121ce52009-01-03 21:22:43 +00001483 /*
Paul Bakker45829992013-01-03 14:52:21 +01001484 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001485 */
Paul Bakker48916f92012-09-16 19:57:18 +00001486 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001487 {
1488 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001489 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001490 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 }
1492
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001493#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001494 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1495 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001496#endif
Paul Bakker45829992013-01-03 14:52:21 +01001497
1498 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1499 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1500 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001501 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1502 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1503 ssl->transform_in->ivlen,
1504 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001505 return( POLARSSL_ERR_SSL_INVALID_MAC );
1506 }
1507
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001508 dec_msglen = ssl->in_msglen;
1509 dec_msg = ssl->in_msg;
1510 dec_msg_result = ssl->in_msg;
1511
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001512#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001513 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001514 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001515 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001516 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001517 {
Paul Bakker48916f92012-09-16 19:57:18 +00001518 dec_msglen -= ssl->transform_in->ivlen;
1519 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001520
Paul Bakker48916f92012-09-16 19:57:18 +00001521 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001522 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001523 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001524#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001525
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001526 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001527 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001528 ssl->transform_in->ivlen,
1529 dec_msg, dec_msglen,
1530 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001531 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001532 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001533 return( ret );
1534 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001535
Paul Bakkercca5b812013-08-31 17:40:26 +02001536 if( dec_msglen != olen )
1537 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001538 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001539 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001540 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001541
1542#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001543 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1544 {
1545 /*
1546 * Save IV in SSL3 and TLS1
1547 */
1548 memcpy( ssl->transform_in->iv_dec,
1549 ssl->transform_in->cipher_ctx_dec.iv,
1550 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001552#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001553
1554 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001555
1556 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1557 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001558#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001559 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1560 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001561#endif
Paul Bakker45829992013-01-03 14:52:21 +01001562 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001563 correct = 0;
1564 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001565
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001566#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001567 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1568 {
Paul Bakker48916f92012-09-16 19:57:18 +00001569 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001571#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1573 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001574 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001575#endif
Paul Bakker45829992013-01-03 14:52:21 +01001576 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 }
1578 }
1579 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001580#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001581#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1582 defined(POLARSSL_SSL_PROTO_TLS1_2)
1583 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 {
1585 /*
Paul Bakker45829992013-01-03 14:52:21 +01001586 * TLSv1+: always check the padding up to the first failure
1587 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001589 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001590 size_t padding_idx = ssl->in_msglen - padlen - 1;
1591
Paul Bakker956c9e02013-12-19 14:42:28 +01001592 /*
1593 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001594 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001595 *
Paul Bakker61885c72014-04-25 12:59:03 +02001596 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1597 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001598 *
1599 * In both cases we reset padding_idx to a safe value (0) to
1600 * prevent out-of-buffer reads.
1601 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001602 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001603 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1604 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001605
1606 padding_idx *= correct;
1607
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001608 for( i = 1; i <= 256; i++ )
1609 {
1610 real_count &= ( i <= padlen );
1611 pad_count += real_count *
1612 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1613 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001614
1615 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001616
Paul Bakkerd66f0702013-01-31 16:57:45 +01001617#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001618 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001619 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001620#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001621 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001622 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001623 else
1624#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1625 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001626 {
1627 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001628 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001629 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001630 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001631 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001632#endif /* POLARSSL_CIPHER_MODE_CBC &&
1633 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001634 {
1635 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001636 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001637 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
1639 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1640 ssl->in_msg, ssl->in_msglen );
1641
1642 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001643 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001645#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1646 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1647 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001648 if( mode != POLARSSL_MODE_GCM &&
1649 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001651 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1652
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001653 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001655 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1656 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001661 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1662 {
1663 ssl_mac( &ssl->transform_in->md_ctx_dec,
1664 ssl->transform_in->mac_dec,
1665 ssl->in_msg, ssl->in_msglen,
1666 ssl->in_ctr, ssl->in_msgtype );
1667 }
1668 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001669#endif /* POLARSSL_SSL_PROTO_SSL3 */
1670#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001671 defined(POLARSSL_SSL_PROTO_TLS1_2)
1672 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1673 {
1674 /*
1675 * Process MAC and always update for padlen afterwards to make
1676 * total time independent of padlen
1677 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001678 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001679 *
1680 * Known timing attacks:
1681 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1682 *
1683 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1684 * correctly. (We round down instead of up, so -56 is the correct
1685 * value for our calculations instead of -55)
1686 */
1687 size_t j, extra_run = 0;
1688 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1689 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001690
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001692
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001693 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1694 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1695 ssl->in_msglen );
1696 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1697 ssl->in_msg + ssl->in_msglen );
1698 for( j = 0; j < extra_run; j++ )
1699 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001700
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001701 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1702 }
1703 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001704#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001705 POLARSSL_SSL_PROTO_TLS1_2 */
1706 {
1707 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001708 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001709 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001711 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1712 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1713 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001715 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001716 ssl->transform_in->maclen ) != 0 )
1717 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001718#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001719 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001721 correct = 0;
1722 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001724 /*
1725 * Finally check the correct flag
1726 */
1727 if( correct == 0 )
1728 return( POLARSSL_ERR_SSL_INVALID_MAC );
1729 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001730#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001731
1732 if( ssl->in_msglen == 0 )
1733 {
1734 ssl->nb_zero++;
1735
1736 /*
1737 * Three or more empty messages may be a DoS attack
1738 * (excessive CPU consumption).
1739 */
1740 if( ssl->nb_zero > 3 )
1741 {
1742 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1743 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001744 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001745 }
1746 }
1747 else
1748 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001749
Paul Bakker23986e52011-04-24 08:57:21 +00001750 for( i = 8; i > 0; i-- )
1751 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 break;
1753
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001754 /* The loops goes to its end iff the counter is wrapping */
1755 if( i == 0 )
1756 {
1757 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1758 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1759 }
1760
Paul Bakker5121ce52009-01-03 21:22:43 +00001761 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1762
1763 return( 0 );
1764}
1765
Paul Bakker2770fbd2012-07-03 13:30:23 +00001766#if defined(POLARSSL_ZLIB_SUPPORT)
1767/*
1768 * Compression/decompression functions
1769 */
1770static int ssl_compress_buf( ssl_context *ssl )
1771{
1772 int ret;
1773 unsigned char *msg_post = ssl->out_msg;
1774 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001775 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776
1777 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1778
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001779 if( len_pre == 0 )
1780 return( 0 );
1781
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782 memcpy( msg_pre, ssl->out_msg, len_pre );
1783
1784 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1785 ssl->out_msglen ) );
1786
1787 SSL_DEBUG_BUF( 4, "before compression: output payload",
1788 ssl->out_msg, ssl->out_msglen );
1789
Paul Bakker48916f92012-09-16 19:57:18 +00001790 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1791 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1792 ssl->transform_out->ctx_deflate.next_out = msg_post;
1793 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 if( ret != Z_OK )
1797 {
1798 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1799 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1800 }
1801
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001802 ssl->out_msglen = SSL_BUFFER_LEN -
1803 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001804
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1806 ssl->out_msglen ) );
1807
1808 SSL_DEBUG_BUF( 4, "after compression: output payload",
1809 ssl->out_msg, ssl->out_msglen );
1810
1811 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1812
1813 return( 0 );
1814}
1815
1816static int ssl_decompress_buf( ssl_context *ssl )
1817{
1818 int ret;
1819 unsigned char *msg_post = ssl->in_msg;
1820 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001821 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001822
1823 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1824
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001825 if( len_pre == 0 )
1826 return( 0 );
1827
Paul Bakker2770fbd2012-07-03 13:30:23 +00001828 memcpy( msg_pre, ssl->in_msg, len_pre );
1829
1830 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1831 ssl->in_msglen ) );
1832
1833 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1834 ssl->in_msg, ssl->in_msglen );
1835
Paul Bakker48916f92012-09-16 19:57:18 +00001836 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1837 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1838 ssl->transform_in->ctx_inflate.next_out = msg_post;
1839 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001840
Paul Bakker48916f92012-09-16 19:57:18 +00001841 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842 if( ret != Z_OK )
1843 {
1844 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1845 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1846 }
1847
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001848 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1849 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001850
Paul Bakker2770fbd2012-07-03 13:30:23 +00001851 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1852 ssl->in_msglen ) );
1853
1854 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1855 ssl->in_msg, ssl->in_msglen );
1856
1857 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1858
1859 return( 0 );
1860}
1861#endif /* POLARSSL_ZLIB_SUPPORT */
1862
Paul Bakker5121ce52009-01-03 21:22:43 +00001863/*
1864 * Fill the input message buffer
1865 */
Paul Bakker23986e52011-04-24 08:57:21 +00001866int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001867{
Paul Bakker23986e52011-04-24 08:57:21 +00001868 int ret;
1869 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1872
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001873 if( nb_want > SSL_BUFFER_LEN - 8 )
1874 {
1875 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1876 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1877 }
1878
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 while( ssl->in_left < nb_want )
1880 {
1881 len = nb_want - ssl->in_left;
1882 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1883
1884 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1885 ssl->in_left, nb_want ) );
1886 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1887
Paul Bakker831a7552011-05-18 13:32:51 +00001888 if( ret == 0 )
1889 return( POLARSSL_ERR_SSL_CONN_EOF );
1890
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 if( ret < 0 )
1892 return( ret );
1893
1894 ssl->in_left += ret;
1895 }
1896
1897 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1898
1899 return( 0 );
1900}
1901
1902/*
1903 * Flush any data not yet written
1904 */
1905int ssl_flush_output( ssl_context *ssl )
1906{
1907 int ret;
1908 unsigned char *buf;
1909
1910 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1911
1912 while( ssl->out_left > 0 )
1913 {
1914 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1915 5 + ssl->out_msglen, ssl->out_left ) );
1916
Paul Bakker5bd42292012-12-19 14:40:42 +01001917 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1921
1922 if( ret <= 0 )
1923 return( ret );
1924
1925 ssl->out_left -= ret;
1926 }
1927
1928 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1929
1930 return( 0 );
1931}
1932
1933/*
1934 * Record layer functions
1935 */
1936int ssl_write_record( ssl_context *ssl )
1937{
Paul Bakker05ef8352012-05-08 09:17:57 +00001938 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001939 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001940
1941 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1944 {
1945 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1946 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1947 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1948
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001949 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1950 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 }
1952
Paul Bakker2770fbd2012-07-03 13:30:23 +00001953#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001954 if( ssl->transform_out != NULL &&
1955 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001956 {
1957 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1958 {
1959 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1960 return( ret );
1961 }
1962
1963 len = ssl->out_msglen;
1964 }
1965#endif /*POLARSSL_ZLIB_SUPPORT */
1966
Paul Bakker05ef8352012-05-08 09:17:57 +00001967#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001968 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001970 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Paul Bakker05ef8352012-05-08 09:17:57 +00001972 ret = ssl_hw_record_write( ssl );
1973 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1974 {
1975 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001976 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001977 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001978
1979 if( ret == 0 )
1980 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001981 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001982#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001983 if( !done )
1984 {
1985 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1986 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1987 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001988 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1989 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001990
Paul Bakker48916f92012-09-16 19:57:18 +00001991 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001992 {
1993 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1994 {
1995 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1996 return( ret );
1997 }
1998
1999 len = ssl->out_msglen;
2000 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2001 ssl->out_hdr[4] = (unsigned char)( len );
2002 }
2003
2004 ssl->out_left = 5 + ssl->out_msglen;
2005
2006 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2007 "version = [%d:%d], msglen = %d",
2008 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2009 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2010
2011 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002012 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013 }
2014
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2016 {
2017 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2018 return( ret );
2019 }
2020
2021 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2022
2023 return( 0 );
2024}
2025
2026int ssl_read_record( ssl_context *ssl )
2027{
Paul Bakker05ef8352012-05-08 09:17:57 +00002028 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002029
2030 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2031
2032 if( ssl->in_hslen != 0 &&
2033 ssl->in_hslen < ssl->in_msglen )
2034 {
2035 /*
2036 * Get next Handshake message in the current record
2037 */
2038 ssl->in_msglen -= ssl->in_hslen;
2039
Paul Bakker8934a982011-08-05 11:11:53 +00002040 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002041 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002042
2043 ssl->in_hslen = 4;
2044 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2045
2046 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2047 " %d, type = %d, hslen = %d",
2048 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2049
2050 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2051 {
2052 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002053 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 }
2055
2056 if( ssl->in_msglen < ssl->in_hslen )
2057 {
2058 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002059 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 }
2061
Paul Bakker4224bc02014-04-08 14:36:50 +02002062 if( ssl->state != SSL_HANDSHAKE_OVER )
2063 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002064
2065 return( 0 );
2066 }
2067
2068 ssl->in_hslen = 0;
2069
2070 /*
2071 * Read the record header and validate it
2072 */
2073 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2074 {
2075 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2076 return( ret );
2077 }
2078
2079 ssl->in_msgtype = ssl->in_hdr[0];
2080 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2081
2082 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2083 "version = [%d:%d], msglen = %d",
2084 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2085 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2086
2087 if( ssl->in_hdr[1] != ssl->major_ver )
2088 {
2089 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
2092
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002093 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 {
2095 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002096 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 }
2098
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002099 /* Sanity check (outer boundaries) */
2100 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2101 {
2102 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2103 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2104 }
2105
Paul Bakker5121ce52009-01-03 21:22:43 +00002106 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002107 * Make sure the message length is acceptable for the current transform
2108 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002109 */
Paul Bakker48916f92012-09-16 19:57:18 +00002110 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002111 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002112 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 {
2114 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002115 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 }
2117 }
2118 else
2119 {
Paul Bakker48916f92012-09-16 19:57:18 +00002120 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121 {
2122 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002123 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002124 }
2125
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002126#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002128 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002129 {
2130 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002131 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002133#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002135#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2136 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 /*
2138 * TLS encrypted messages can have up to 256 bytes of padding
2139 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002140 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002141 ssl->in_msglen > ssl->transform_in->minlen +
2142 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002143 {
2144 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002145 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002148 }
2149
2150 /*
2151 * Read and optionally decrypt the message contents
2152 */
2153 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2154 {
2155 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2156 return( ret );
2157 }
2158
2159 SSL_DEBUG_BUF( 4, "input record from network",
2160 ssl->in_hdr, 5 + ssl->in_msglen );
2161
Paul Bakker05ef8352012-05-08 09:17:57 +00002162#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002163 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002164 {
2165 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2166
2167 ret = ssl_hw_record_read( ssl );
2168 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2169 {
2170 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002171 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002172 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002173
2174 if( ret == 0 )
2175 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002176 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002177#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002178 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002179 {
2180 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2181 {
Paul Bakker40865c82013-01-31 17:13:13 +01002182#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2183 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2184 {
2185 ssl_send_alert_message( ssl,
2186 SSL_ALERT_LEVEL_FATAL,
2187 SSL_ALERT_MSG_BAD_RECORD_MAC );
2188 }
2189#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002190 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2191 return( ret );
2192 }
2193
2194 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2195 ssl->in_msg, ssl->in_msglen );
2196
2197 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2198 {
2199 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002200 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 }
2202 }
2203
Paul Bakker2770fbd2012-07-03 13:30:23 +00002204#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002205 if( ssl->transform_in != NULL &&
2206 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002207 {
2208 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2209 {
2210 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2211 return( ret );
2212 }
2213
2214 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2215 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2216 }
2217#endif /* POLARSSL_ZLIB_SUPPORT */
2218
Paul Bakker0a925182012-04-16 06:46:41 +00002219 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2220 ssl->in_msgtype != SSL_MSG_ALERT &&
2221 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2222 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2223 {
2224 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2225
Paul Bakker48916f92012-09-16 19:57:18 +00002226 if( ( ret = ssl_send_alert_message( ssl,
2227 SSL_ALERT_LEVEL_FATAL,
2228 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002229 {
2230 return( ret );
2231 }
2232
2233 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2234 }
2235
Paul Bakker5121ce52009-01-03 21:22:43 +00002236 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2237 {
2238 ssl->in_hslen = 4;
2239 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2240
2241 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2242 " %d, type = %d, hslen = %d",
2243 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2244
2245 /*
2246 * Additional checks to validate the handshake header
2247 */
2248 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2249 {
2250 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002251 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 }
2253
2254 if( ssl->in_msglen < ssl->in_hslen )
2255 {
2256 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002257 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002258 }
2259
Paul Bakker48916f92012-09-16 19:57:18 +00002260 if( ssl->state != SSL_HANDSHAKE_OVER )
2261 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 }
2263
2264 if( ssl->in_msgtype == SSL_MSG_ALERT )
2265 {
2266 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2267 ssl->in_msg[0], ssl->in_msg[1] ) );
2268
2269 /*
2270 * Ignore non-fatal alerts, except close_notify
2271 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002272 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002274 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2275 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002276 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277 }
2278
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002279 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2280 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 {
2282 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002283 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 }
2285 }
2286
2287 ssl->in_left = 0;
2288
2289 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2290
2291 return( 0 );
2292}
2293
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002294int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2295{
2296 int ret;
2297
2298 if( ( ret = ssl_send_alert_message( ssl,
2299 SSL_ALERT_LEVEL_FATAL,
2300 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2301 {
2302 return( ret );
2303 }
2304
2305 return( 0 );
2306}
2307
Paul Bakker0a925182012-04-16 06:46:41 +00002308int ssl_send_alert_message( ssl_context *ssl,
2309 unsigned char level,
2310 unsigned char message )
2311{
2312 int ret;
2313
2314 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2315
2316 ssl->out_msgtype = SSL_MSG_ALERT;
2317 ssl->out_msglen = 2;
2318 ssl->out_msg[0] = level;
2319 ssl->out_msg[1] = message;
2320
2321 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2322 {
2323 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2324 return( ret );
2325 }
2326
2327 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2328
2329 return( 0 );
2330}
2331
Paul Bakker5121ce52009-01-03 21:22:43 +00002332/*
2333 * Handshake functions
2334 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002335#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2336 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2337 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2338 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2339 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2340 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2341 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002342int ssl_write_certificate( ssl_context *ssl )
2343{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002344 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002345
2346 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2347
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002348 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002349 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2350 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002351 {
2352 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2353 ssl->state++;
2354 return( 0 );
2355 }
2356
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002357 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2358 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359}
2360
2361int ssl_parse_certificate( ssl_context *ssl )
2362{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2364
2365 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2366
2367 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002368 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2369 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002370 {
2371 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2372 ssl->state++;
2373 return( 0 );
2374 }
2375
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002376 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2377 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378}
2379#else
2380int ssl_write_certificate( ssl_context *ssl )
2381{
2382 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2383 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002384 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002385 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2386
2387 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2388
2389 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002390 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2391 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002392 {
2393 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2394 ssl->state++;
2395 return( 0 );
2396 }
2397
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 if( ssl->endpoint == SSL_IS_CLIENT )
2399 {
2400 if( ssl->client_auth == 0 )
2401 {
2402 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2403 ssl->state++;
2404 return( 0 );
2405 }
2406
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002407#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 /*
2409 * If using SSLv3 and got no cert, send an Alert message
2410 * (otherwise an empty Certificate message will be sent).
2411 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002412 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2414 {
2415 ssl->out_msglen = 2;
2416 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2418 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002419
2420 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2421 goto write_msg;
2422 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002423#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 }
2425 else /* SSL_IS_SERVER */
2426 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002427 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 {
2429 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002430 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
2432 }
2433
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002434 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
2436 /*
2437 * 0 . 0 handshake type
2438 * 1 . 3 handshake length
2439 * 4 . 6 length of all certs
2440 * 7 . 9 length of cert. 1
2441 * 10 . n-1 peer certificate
2442 * n . n+2 length of cert. 2
2443 * n+3 . ... upper level cert, etc.
2444 */
2445 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002446 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Paul Bakker29087132010-03-21 21:03:34 +00002448 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 {
2450 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002451 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 {
2453 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2454 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002455 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
2457
2458 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2459 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2460 ssl->out_msg[i + 2] = (unsigned char)( n );
2461
2462 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2463 i += n; crt = crt->next;
2464 }
2465
2466 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2467 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2468 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2469
2470 ssl->out_msglen = i;
2471 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2472 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2473
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002474#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002475write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002477
2478 ssl->state++;
2479
2480 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2481 {
2482 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2483 return( ret );
2484 }
2485
2486 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2487
Paul Bakkered27a042013-04-18 22:46:23 +02002488 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002489}
2490
2491int ssl_parse_certificate( ssl_context *ssl )
2492{
Paul Bakkered27a042013-04-18 22:46:23 +02002493 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002494 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002495 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2498
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002499 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002500 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2501 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002502 {
2503 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2504 ssl->state++;
2505 return( 0 );
2506 }
2507
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002509 ( ssl->authmode == SSL_VERIFY_NONE ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002511 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002512 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2514 ssl->state++;
2515 return( 0 );
2516 }
2517
2518 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2519 {
2520 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2521 return( ret );
2522 }
2523
2524 ssl->state++;
2525
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002526#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002527 /*
2528 * Check if the client sent an empty certificate
2529 */
2530 if( ssl->endpoint == SSL_IS_SERVER &&
2531 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2532 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002533 if( ssl->in_msglen == 2 &&
2534 ssl->in_msgtype == SSL_MSG_ALERT &&
2535 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2536 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002537 {
2538 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2539
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002540 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002541 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2542 return( 0 );
2543 else
Paul Bakker40e46942009-01-03 21:51:57 +00002544 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
2546 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002547#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002548
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002549#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2550 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002551 if( ssl->endpoint == SSL_IS_SERVER &&
2552 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2553 {
2554 if( ssl->in_hslen == 7 &&
2555 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2556 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2557 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2558 {
2559 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2560
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002561 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002563 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 else
2565 return( 0 );
2566 }
2567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002568#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2569 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002570
2571 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2572 {
2573 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002574 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576
2577 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2578 {
2579 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002580 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
2582
2583 /*
2584 * Same message structure as in ssl_write_certificate()
2585 */
2586 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2587
2588 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2589 {
2590 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002591 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 }
2593
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002594 /* In case we tried to reuse a session but it failed */
2595 if( ssl->session_negotiate->peer_cert != NULL )
2596 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002597 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002598 polarssl_free( ssl->session_negotiate->peer_cert );
2599 }
2600
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002601 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2602 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 {
2604 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002605 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002606 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002607 }
2608
Paul Bakkerb6b09562013-09-18 14:17:41 +02002609 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
2611 i = 7;
2612
2613 while( i < ssl->in_hslen )
2614 {
2615 if( ssl->in_msg[i] != 0 )
2616 {
2617 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002618 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002619 }
2620
2621 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2622 | (unsigned int) ssl->in_msg[i + 2];
2623 i += 3;
2624
2625 if( n < 128 || i + n > ssl->in_hslen )
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
Paul Bakkerddf26b42013-09-18 13:46:23 +02002631 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2632 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 if( ret != 0 )
2634 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002635 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 return( ret );
2637 }
2638
2639 i += n;
2640 }
2641
Paul Bakker48916f92012-09-16 19:57:18 +00002642 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002644 /*
2645 * On client, make sure the server cert doesn't change during renego to
2646 * avoid "triple handshake" attack: https://secure-resumption.com/
2647 */
2648 if( ssl->endpoint == SSL_IS_CLIENT &&
2649 ssl->renegotiation == SSL_RENEGOTIATION )
2650 {
2651 if( ssl->session->peer_cert == NULL )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2654 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2655 }
2656
2657 if( ssl->session->peer_cert->raw.len !=
2658 ssl->session_negotiate->peer_cert->raw.len ||
2659 memcmp( ssl->session->peer_cert->raw.p,
2660 ssl->session_negotiate->peer_cert->raw.p,
2661 ssl->session->peer_cert->raw.len ) != 0 )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2664 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2665 }
2666 }
2667
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 if( ssl->authmode != SSL_VERIFY_NONE )
2669 {
2670 if( ssl->ca_chain == NULL )
2671 {
2672 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002673 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 }
2675
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002676 /*
2677 * Main check: verify certificate
2678 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002679 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2680 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2681 &ssl->session_negotiate->verify_result,
2682 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002683
2684 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002685 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002687 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002688
2689 /*
2690 * Secondary checks: always done, but change 'ret' only if it was 0
2691 */
2692
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002693#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002694 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002695 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002696
2697 /* If certificate uses an EC key, make sure the curve is OK */
2698 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2699 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2700 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002701 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002702 if( ret == 0 )
2703 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002704 }
2705 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002706#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002707
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002708 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2709 ciphersuite_info,
2710 ! ssl->endpoint ) != 0 )
2711 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002712 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002713 if( ret == 0 )
2714 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2715 }
2716
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2718 ret = 0;
2719 }
2720
2721 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2722
2723 return( ret );
2724}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002725#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2726 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2727 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2728 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2729 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2730 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2731 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002732
2733int ssl_write_change_cipher_spec( ssl_context *ssl )
2734{
2735 int ret;
2736
2737 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2738
2739 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2740 ssl->out_msglen = 1;
2741 ssl->out_msg[0] = 1;
2742
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 ssl->state++;
2744
2745 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2746 {
2747 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2748 return( ret );
2749 }
2750
2751 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2752
2753 return( 0 );
2754}
2755
2756int ssl_parse_change_cipher_spec( ssl_context *ssl )
2757{
2758 int ret;
2759
2760 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2761
Paul Bakker5121ce52009-01-03 21:22:43 +00002762 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2763 {
2764 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2765 return( ret );
2766 }
2767
2768 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2769 {
2770 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002771 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 }
2773
2774 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002777 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
2780 ssl->state++;
2781
2782 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2783
2784 return( 0 );
2785}
2786
Paul Bakker41c83d32013-03-20 14:39:14 +01002787void ssl_optimize_checksum( ssl_context *ssl,
2788 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002789{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002790 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002791
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002792#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2793 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002794 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002795 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002796 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002797#endif
2798#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2799#if defined(POLARSSL_SHA512_C)
2800 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2801 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2802 else
2803#endif
2804#if defined(POLARSSL_SHA256_C)
2805 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002806 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002807 else
2808#endif
2809#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002810 {
2811 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002813 }
Paul Bakker380da532012-04-18 16:10:25 +00002814}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002815
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002816static void ssl_update_checksum_start( ssl_context *ssl,
2817 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002818{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002819#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2820 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002821 md5_update( &ssl->handshake->fin_md5 , buf, len );
2822 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#endif
2824#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2825#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002826 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002827#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002828#if defined(POLARSSL_SHA512_C)
2829 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002830#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002831#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002832}
2833
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002834#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2835 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002836static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2837 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002838{
Paul Bakker48916f92012-09-16 19:57:18 +00002839 md5_update( &ssl->handshake->fin_md5 , buf, len );
2840 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002841}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002842#endif
Paul Bakker380da532012-04-18 16:10:25 +00002843
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002844#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2845#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002846static void ssl_update_checksum_sha256( ssl_context *ssl,
2847 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002848{
Paul Bakker9e36f042013-06-30 14:34:05 +02002849 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002850}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002851#endif
Paul Bakker380da532012-04-18 16:10:25 +00002852
Paul Bakker9e36f042013-06-30 14:34:05 +02002853#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002854static void ssl_update_checksum_sha384( ssl_context *ssl,
2855 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002856{
Paul Bakker9e36f042013-06-30 14:34:05 +02002857 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002858}
Paul Bakker769075d2012-11-24 11:26:46 +01002859#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002860#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002861
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002862#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002863static void ssl_calc_finished_ssl(
2864 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002866 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002867 md5_context md5;
2868 sha1_context sha1;
2869
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 unsigned char padbuf[48];
2871 unsigned char md5sum[16];
2872 unsigned char sha1sum[20];
2873
Paul Bakker48916f92012-09-16 19:57:18 +00002874 ssl_session *session = ssl->session_negotiate;
2875 if( !session )
2876 session = ssl->session;
2877
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2879
Paul Bakker48916f92012-09-16 19:57:18 +00002880 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2881 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
2883 /*
2884 * SSLv3:
2885 * hash =
2886 * MD5( master + pad2 +
2887 * MD5( handshake + sender + master + pad1 ) )
2888 * + SHA1( master + pad2 +
2889 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 */
2891
Paul Bakker90995b52013-06-24 19:20:35 +02002892#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002895#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
Paul Bakker90995b52013-06-24 19:20:35 +02002897#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002899 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002900#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Paul Bakker3c2122f2013-06-24 19:03:14 +02002902 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2903 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002904
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Paul Bakker3c2122f2013-06-24 19:03:14 +02002907 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002908 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 md5_update( &md5, padbuf, 48 );
2910 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker3c2122f2013-06-24 19:03:14 +02002912 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002913 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002914 sha1_update( &sha1, padbuf, 40 );
2915 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakker1ef83d62012-04-11 12:09:53 +00002917 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002920 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002921 md5_update( &md5, padbuf, 48 );
2922 md5_update( &md5, md5sum, 16 );
2923 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002926 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 sha1_update( &sha1, padbuf , 40 );
2928 sha1_update( &sha1, sha1sum, 20 );
2929 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002932
Paul Bakker5b4af392014-06-26 12:09:34 +02002933 md5_free( &md5 );
2934 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakker34617722014-06-13 17:20:13 +02002936 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2937 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2938 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
2940 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2941}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002942#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002944#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945static void ssl_calc_finished_tls(
2946 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002947{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002948 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002949 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Paul Bakker48916f92012-09-16 19:57:18 +00002954 ssl_session *session = ssl->session_negotiate;
2955 if( !session )
2956 session = ssl->session;
2957
Paul Bakker1ef83d62012-04-11 12:09:53 +00002958 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker48916f92012-09-16 19:57:18 +00002960 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2961 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963 /*
2964 * TLSv1:
2965 * hash = PRF( master, finished_label,
2966 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2967 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Paul Bakker90995b52013-06-24 19:20:35 +02002969#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002970 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2971 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002972#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973
Paul Bakker90995b52013-06-24 19:20:35 +02002974#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2976 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002977#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978
2979 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002980 ? "client finished"
2981 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
2983 md5_finish( &md5, padbuf );
2984 sha1_finish( &sha1, padbuf + 16 );
2985
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002986 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002987 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002988
2989 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2990
Paul Bakker5b4af392014-06-26 12:09:34 +02002991 md5_free( &md5 );
2992 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002993
Paul Bakker34617722014-06-13 17:20:13 +02002994 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
2996 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2997}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002998#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003000#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3001#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003002static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003003 ssl_context *ssl, unsigned char *buf, int from )
3004{
3005 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003006 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003007 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003008 unsigned char padbuf[32];
3009
Paul Bakker48916f92012-09-16 19:57:18 +00003010 ssl_session *session = ssl->session_negotiate;
3011 if( !session )
3012 session = ssl->session;
3013
Paul Bakker380da532012-04-18 16:10:25 +00003014 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015
Paul Bakker9e36f042013-06-30 14:34:05 +02003016 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017
3018 /*
3019 * TLSv1.2:
3020 * hash = PRF( master, finished_label,
3021 * Hash( handshake ) )[0.11]
3022 */
3023
Paul Bakker9e36f042013-06-30 14:34:05 +02003024#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003025 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003026 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003027#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028
3029 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003030 ? "client finished"
3031 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032
Paul Bakker9e36f042013-06-30 14:34:05 +02003033 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003035 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003036 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037
3038 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3039
Paul Bakker5b4af392014-06-26 12:09:34 +02003040 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003041
Paul Bakker34617722014-06-13 17:20:13 +02003042 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003043
3044 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3045}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003047
Paul Bakker9e36f042013-06-30 14:34:05 +02003048#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003049static void ssl_calc_finished_tls_sha384(
3050 ssl_context *ssl, unsigned char *buf, int from )
3051{
3052 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003053 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003054 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003055 unsigned char padbuf[48];
3056
Paul Bakker48916f92012-09-16 19:57:18 +00003057 ssl_session *session = ssl->session_negotiate;
3058 if( !session )
3059 session = ssl->session;
3060
Paul Bakker380da532012-04-18 16:10:25 +00003061 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003062
Paul Bakker9e36f042013-06-30 14:34:05 +02003063 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003064
3065 /*
3066 * TLSv1.2:
3067 * hash = PRF( master, finished_label,
3068 * Hash( handshake ) )[0.11]
3069 */
3070
Paul Bakker9e36f042013-06-30 14:34:05 +02003071#if !defined(POLARSSL_SHA512_ALT)
3072 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3073 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003074#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003075
3076 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003077 ? "client finished"
3078 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079
Paul Bakker9e36f042013-06-30 14:34:05 +02003080 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003081
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003082 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003083 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003084
3085 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3086
Paul Bakker5b4af392014-06-26 12:09:34 +02003087 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003088
Paul Bakker34617722014-06-13 17:20:13 +02003089 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003090
3091 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3092}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003093#endif /* POLARSSL_SHA512_C */
3094#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003095
Paul Bakker48916f92012-09-16 19:57:18 +00003096void ssl_handshake_wrapup( ssl_context *ssl )
3097{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003098 int resume = ssl->handshake->resume;
3099
Paul Bakker48916f92012-09-16 19:57:18 +00003100 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3101
3102 /*
3103 * Free our handshake params
3104 */
3105 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003106 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003107 ssl->handshake = NULL;
3108
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003109 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003110 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003111 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003112 ssl->renego_records_seen = 0;
3113 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 /*
3116 * Switch in our now active transform context
3117 */
3118 if( ssl->transform )
3119 {
3120 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003121 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003122 }
3123 ssl->transform = ssl->transform_negotiate;
3124 ssl->transform_negotiate = NULL;
3125
Paul Bakker0a597072012-09-25 21:55:46 +00003126 if( ssl->session )
3127 {
3128 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003129 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003130 }
3131 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003132 ssl->session_negotiate = NULL;
3133
Paul Bakker0a597072012-09-25 21:55:46 +00003134 /*
3135 * Add cache entry
3136 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003137 if( ssl->f_set_cache != NULL &&
3138 ssl->session->length != 0 &&
3139 resume == 0 )
3140 {
Paul Bakker0a597072012-09-25 21:55:46 +00003141 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3142 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003143 }
Paul Bakker0a597072012-09-25 21:55:46 +00003144
Paul Bakker48916f92012-09-16 19:57:18 +00003145 ssl->state++;
3146
3147 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3148}
3149
Paul Bakker1ef83d62012-04-11 12:09:53 +00003150int ssl_write_finished( ssl_context *ssl )
3151{
3152 int ret, hash_len;
3153
3154 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3155
Paul Bakker92be97b2013-01-02 17:30:03 +01003156 /*
3157 * Set the out_msg pointer to the correct location based on IV length
3158 */
3159 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3160 {
3161 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3162 ssl->transform_negotiate->fixed_ivlen;
3163 }
3164 else
3165 ssl->out_msg = ssl->out_iv;
3166
Paul Bakker48916f92012-09-16 19:57:18 +00003167 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003168
3169 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3171
Paul Bakker48916f92012-09-16 19:57:18 +00003172 ssl->verify_data_len = hash_len;
3173 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3174
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 ssl->out_msglen = 4 + hash_len;
3176 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3177 ssl->out_msg[0] = SSL_HS_FINISHED;
3178
3179 /*
3180 * In case of session resuming, invert the client and server
3181 * ChangeCipherSpec messages order.
3182 */
Paul Bakker0a597072012-09-25 21:55:46 +00003183 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 {
3185 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003186 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003187 else
3188 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3189 }
3190 else
3191 ssl->state++;
3192
Paul Bakker48916f92012-09-16 19:57:18 +00003193 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003194 * Switch to our negotiated transform and session parameters for outbound
3195 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003196 */
3197 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3198 ssl->transform_out = ssl->transform_negotiate;
3199 ssl->session_out = ssl->session_negotiate;
3200 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003201
Paul Bakker07eb38b2012-12-19 14:42:06 +01003202#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003203 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003204 {
3205 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3206 {
3207 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3208 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3209 }
3210 }
3211#endif
3212
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3214 {
3215 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3216 return( ret );
3217 }
3218
3219 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3220
3221 return( 0 );
3222}
3223
3224int ssl_parse_finished( ssl_context *ssl )
3225{
Paul Bakker23986e52011-04-24 08:57:21 +00003226 int ret;
3227 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 unsigned char buf[36];
3229
3230 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3231
Paul Bakker48916f92012-09-16 19:57:18 +00003232 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakker48916f92012-09-16 19:57:18 +00003234 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003235 * Switch to our negotiated transform and session parameters for inbound
3236 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003237 */
3238 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3239 ssl->transform_in = ssl->transform_negotiate;
3240 ssl->session_in = ssl->session_negotiate;
3241 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242
Paul Bakker92be97b2013-01-02 17:30:03 +01003243 /*
3244 * Set the in_msg pointer to the correct location based on IV length
3245 */
3246 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3247 {
3248 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3249 ssl->transform_negotiate->fixed_ivlen;
3250 }
3251 else
3252 ssl->in_msg = ssl->in_iv;
3253
Paul Bakker07eb38b2012-12-19 14:42:06 +01003254#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003255 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003256 {
3257 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3258 {
3259 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3260 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3261 }
3262 }
3263#endif
3264
Paul Bakker5121ce52009-01-03 21:22:43 +00003265 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3266 {
3267 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3268 return( ret );
3269 }
3270
3271 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3272 {
3273 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003274 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 }
3276
Paul Bakker1ef83d62012-04-11 12:09:53 +00003277 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3279
3280 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3281 ssl->in_hslen != 4 + hash_len )
3282 {
3283 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003284 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003287 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 {
3289 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003290 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 }
3292
Paul Bakker48916f92012-09-16 19:57:18 +00003293 ssl->verify_data_len = hash_len;
3294 memcpy( ssl->peer_verify_data, buf, hash_len );
3295
Paul Bakker0a597072012-09-25 21:55:46 +00003296 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 {
3298 if( ssl->endpoint == SSL_IS_CLIENT )
3299 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3300
3301 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003302 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 }
3304 else
3305 ssl->state++;
3306
3307 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3308
3309 return( 0 );
3310}
3311
Paul Bakker968afaa2014-07-09 11:09:24 +02003312static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003313{
3314 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3315
3316#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3317 defined(POLARSSL_SSL_PROTO_TLS1_1)
3318 md5_init( &handshake->fin_md5 );
3319 sha1_init( &handshake->fin_sha1 );
3320 md5_starts( &handshake->fin_md5 );
3321 sha1_starts( &handshake->fin_sha1 );
3322#endif
3323#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3324#if defined(POLARSSL_SHA256_C)
3325 sha256_init( &handshake->fin_sha256 );
3326 sha256_starts( &handshake->fin_sha256, 0 );
3327#endif
3328#if defined(POLARSSL_SHA512_C)
3329 sha512_init( &handshake->fin_sha512 );
3330 sha512_starts( &handshake->fin_sha512, 1 );
3331#endif
3332#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3333
3334 handshake->update_checksum = ssl_update_checksum_start;
3335 handshake->sig_alg = SSL_HASH_SHA1;
3336
3337#if defined(POLARSSL_DHM_C)
3338 dhm_init( &handshake->dhm_ctx );
3339#endif
3340#if defined(POLARSSL_ECDH_C)
3341 ecdh_init( &handshake->ecdh_ctx );
3342#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003343}
3344
3345static void ssl_transform_init( ssl_transform *transform )
3346{
3347 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003348
3349 cipher_init( &transform->cipher_ctx_enc );
3350 cipher_init( &transform->cipher_ctx_dec );
3351
3352 md_init( &transform->md_ctx_enc );
3353 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003354}
3355
3356void ssl_session_init( ssl_session *session )
3357{
3358 memset( session, 0, sizeof(ssl_session) );
3359}
3360
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003361static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003362{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003363 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003364 if( ssl->transform_negotiate )
3365 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003366 if( ssl->session_negotiate )
3367 ssl_session_free( ssl->session_negotiate );
3368 if( ssl->handshake )
3369 ssl_handshake_free( ssl->handshake );
3370
3371 /*
3372 * Either the pointers are now NULL or cleared properly and can be freed.
3373 * Now allocate missing structures.
3374 */
3375 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003376 {
3377 ssl->transform_negotiate =
3378 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3379 }
Paul Bakker48916f92012-09-16 19:57:18 +00003380
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003381 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003382 {
3383 ssl->session_negotiate =
3384 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3385 }
Paul Bakker48916f92012-09-16 19:57:18 +00003386
Paul Bakker82788fb2014-10-20 13:59:19 +02003387 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003388 {
3389 ssl->handshake = (ssl_handshake_params *)
3390 polarssl_malloc( sizeof(ssl_handshake_params) );
3391 }
Paul Bakker48916f92012-09-16 19:57:18 +00003392
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003393 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003394 if( ssl->handshake == NULL ||
3395 ssl->transform_negotiate == NULL ||
3396 ssl->session_negotiate == NULL )
3397 {
3398 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003399
3400 polarssl_free( ssl->handshake );
3401 polarssl_free( ssl->transform_negotiate );
3402 polarssl_free( ssl->session_negotiate );
3403
3404 ssl->handshake = NULL;
3405 ssl->transform_negotiate = NULL;
3406 ssl->session_negotiate = NULL;
3407
Paul Bakker48916f92012-09-16 19:57:18 +00003408 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3409 }
3410
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003411 /* Initialize structures */
3412 ssl_session_init( ssl->session_negotiate );
3413 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003414 ssl_handshake_params_init( ssl->handshake );
3415
3416#if defined(POLARSSL_X509_CRT_PARSE_C)
3417 ssl->handshake->key_cert = ssl->key_cert;
3418#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003419
Paul Bakker48916f92012-09-16 19:57:18 +00003420 return( 0 );
3421}
3422
Paul Bakker5121ce52009-01-03 21:22:43 +00003423/*
3424 * Initialize an SSL context
3425 */
3426int ssl_init( ssl_context *ssl )
3427{
Paul Bakker48916f92012-09-16 19:57:18 +00003428 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 int len = SSL_BUFFER_LEN;
3430
3431 memset( ssl, 0, sizeof( ssl_context ) );
3432
Paul Bakker62f2dee2012-09-28 07:31:51 +00003433 /*
3434 * Sane defaults
3435 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003436 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3437 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3438 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3439 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003440
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003441 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003442
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003443 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3444
Paul Bakker62f2dee2012-09-28 07:31:51 +00003445#if defined(POLARSSL_DHM_C)
3446 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3447 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3448 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3449 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3450 {
3451 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3452 return( ret );
3453 }
3454#endif
3455
3456 /*
3457 * Prepare base structures
3458 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003459 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003460 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003461 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 ssl->in_msg = ssl->in_ctr + 13;
3463
3464 if( ssl->in_ctr == NULL )
3465 {
3466 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003467 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003468 }
3469
Paul Bakker6e339b52013-07-03 13:37:05 +02003470 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003472 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003473 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003474
3475 if( ssl->out_ctr == NULL )
3476 {
3477 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003478 polarssl_free( ssl->in_ctr );
3479 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003480 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003481 }
3482
3483 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3484 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3485
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003486#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3487 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3488#endif
3489
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003490#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3491 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3492#endif
3493
Paul Bakker606b4ba2013-08-14 16:52:14 +02003494#if defined(POLARSSL_SSL_SESSION_TICKETS)
3495 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3496#endif
3497
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003498#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003499 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003500#endif
3501
Paul Bakker48916f92012-09-16 19:57:18 +00003502 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3503 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
3505 return( 0 );
3506}
3507
3508/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003509 * Reset an initialized and used SSL context for re-use while retaining
3510 * all application-set variables, function pointers and data.
3511 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003513{
Paul Bakker48916f92012-09-16 19:57:18 +00003514 int ret;
3515
Paul Bakker7eb013f2011-10-06 12:37:39 +00003516 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003517 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3518 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3519
3520 ssl->verify_data_len = 0;
3521 memset( ssl->own_verify_data, 0, 36 );
3522 memset( ssl->peer_verify_data, 0, 36 );
3523
Paul Bakker7eb013f2011-10-06 12:37:39 +00003524 ssl->in_offt = NULL;
3525
Paul Bakker92be97b2013-01-02 17:30:03 +01003526 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003527 ssl->in_msgtype = 0;
3528 ssl->in_msglen = 0;
3529 ssl->in_left = 0;
3530
3531 ssl->in_hslen = 0;
3532 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003533 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003534
Paul Bakker92be97b2013-01-02 17:30:03 +01003535 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003536 ssl->out_msgtype = 0;
3537 ssl->out_msglen = 0;
3538 ssl->out_left = 0;
3539
Paul Bakker48916f92012-09-16 19:57:18 +00003540 ssl->transform_in = NULL;
3541 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003542
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003543 ssl->renego_records_seen = 0;
3544
Paul Bakker7eb013f2011-10-06 12:37:39 +00003545 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3546 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003547
3548#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003549 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003550 {
3551 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003552 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003553 {
3554 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3555 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3556 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003557 }
3558#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003559
Paul Bakker48916f92012-09-16 19:57:18 +00003560 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003561 {
Paul Bakker48916f92012-09-16 19:57:18 +00003562 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003563 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003564 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003565 }
Paul Bakker48916f92012-09-16 19:57:18 +00003566
Paul Bakkerc0463502013-02-14 11:19:38 +01003567 if( ssl->session )
3568 {
3569 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003570 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003571 ssl->session = NULL;
3572 }
3573
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003574#if defined(POLARSSL_SSL_ALPN)
3575 ssl->alpn_chosen = NULL;
3576#endif
3577
Paul Bakker48916f92012-09-16 19:57:18 +00003578 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3579 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003580
3581 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003582}
3583
Paul Bakkera503a632013-08-14 13:48:06 +02003584#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003585static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3586{
3587 aes_free( &tkeys->enc );
3588 aes_free( &tkeys->dec );
3589
3590 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3591}
3592
Paul Bakker7eb013f2011-10-06 12:37:39 +00003593/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003594 * Allocate and initialize ticket keys
3595 */
3596static int ssl_ticket_keys_init( ssl_context *ssl )
3597{
3598 int ret;
3599 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003600 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003601
3602 if( ssl->ticket_keys != NULL )
3603 return( 0 );
3604
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003605 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3606 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003607 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3608
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003609 aes_init( &tkeys->enc );
3610 aes_init( &tkeys->dec );
3611
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003612 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003613 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003614 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003615 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003616 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003617 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003618
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003619 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3620 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3621 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3622 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003623 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003624 polarssl_free( tkeys );
3625 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003626 }
3627
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003628 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003629 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003630 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003631 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003632 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003633 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003634
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003635 ssl->ticket_keys = tkeys;
3636
3637 return( 0 );
3638}
Paul Bakkera503a632013-08-14 13:48:06 +02003639#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003640
3641/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003642 * SSL set accessors
3643 */
3644void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3645{
3646 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003647
Paul Bakker606b4ba2013-08-14 16:52:14 +02003648#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003649 if( endpoint == SSL_IS_CLIENT )
3650 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003651#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003652}
3653
3654void ssl_set_authmode( ssl_context *ssl, int authmode )
3655{
3656 ssl->authmode = authmode;
3657}
3658
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003659#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003660void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003661 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003662 void *p_vrfy )
3663{
3664 ssl->f_vrfy = f_vrfy;
3665 ssl->p_vrfy = p_vrfy;
3666}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003667#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003668
Paul Bakker5121ce52009-01-03 21:22:43 +00003669void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003670 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003671 void *p_rng )
3672{
3673 ssl->f_rng = f_rng;
3674 ssl->p_rng = p_rng;
3675}
3676
3677void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003678 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003679 void *p_dbg )
3680{
3681 ssl->f_dbg = f_dbg;
3682 ssl->p_dbg = p_dbg;
3683}
3684
3685void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003686 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003687 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003688{
3689 ssl->f_recv = f_recv;
3690 ssl->f_send = f_send;
3691 ssl->p_recv = p_recv;
3692 ssl->p_send = p_send;
3693}
3694
Paul Bakker0a597072012-09-25 21:55:46 +00003695void ssl_set_session_cache( ssl_context *ssl,
3696 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3697 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003698{
Paul Bakker0a597072012-09-25 21:55:46 +00003699 ssl->f_get_cache = f_get_cache;
3700 ssl->p_get_cache = p_get_cache;
3701 ssl->f_set_cache = f_set_cache;
3702 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703}
3704
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003705int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003706{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003707 int ret;
3708
3709 if( ssl == NULL ||
3710 session == NULL ||
3711 ssl->session_negotiate == NULL ||
3712 ssl->endpoint != SSL_IS_CLIENT )
3713 {
3714 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3715 }
3716
3717 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3718 return( ret );
3719
Paul Bakker0a597072012-09-25 21:55:46 +00003720 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003721
3722 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003723}
3724
Paul Bakkerb68cad62012-08-23 08:34:18 +00003725void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003726{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003727 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3728 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3729 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3730 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3731}
3732
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003733void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3734 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003735 int major, int minor )
3736{
3737 if( major != SSL_MAJOR_VERSION_3 )
3738 return;
3739
3740 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3741 return;
3742
3743 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003744}
3745
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003746#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003747/* Add a new (empty) key_cert entry an return a pointer to it */
3748static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3749{
3750 ssl_key_cert *key_cert, *last;
3751
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003752 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3753 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003754 return( NULL );
3755
3756 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3757
3758 /* Append the new key_cert to the (possibly empty) current list */
3759 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003760 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003761 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003762 if( ssl->handshake != NULL )
3763 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003764 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003765 else
3766 {
3767 last = ssl->key_cert;
3768 while( last->next != NULL )
3769 last = last->next;
3770 last->next = key_cert;
3771 }
3772
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003773 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003774}
3775
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003776void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003777 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003778{
3779 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003780 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003781 ssl->peer_cn = peer_cn;
3782}
3783
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003784int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003785 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003786{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003787 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3788
3789 if( key_cert == NULL )
3790 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3791
3792 key_cert->cert = own_cert;
3793 key_cert->key = pk_key;
3794
3795 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003796}
3797
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003798#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003799int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003800 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003801{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003802 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003803 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003804
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003805 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003806 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3807
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003808 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3809 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003810 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003811
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003812 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003813
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003814 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003815 if( ret != 0 )
3816 return( ret );
3817
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003818 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003819 return( ret );
3820
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003821 key_cert->cert = own_cert;
3822 key_cert->key_own_alloc = 1;
3823
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003824 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003825}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003826#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003827
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003828int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003829 void *rsa_key,
3830 rsa_decrypt_func rsa_decrypt,
3831 rsa_sign_func rsa_sign,
3832 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003833{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003834 int ret;
3835 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003836
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003837 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003838 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3839
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003840 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3841 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003842 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003843
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003844 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003845
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003846 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3847 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3848 return( ret );
3849
3850 key_cert->cert = own_cert;
3851 key_cert->key_own_alloc = 1;
3852
3853 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003854}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003855#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003856
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003857#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003858int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3859 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003860{
Paul Bakker6db455e2013-09-18 17:29:31 +02003861 if( psk == NULL || psk_identity == NULL )
3862 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3863
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003864 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003865 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3866
Paul Bakker6db455e2013-09-18 17:29:31 +02003867 if( ssl->psk != NULL )
3868 {
3869 polarssl_free( ssl->psk );
3870 polarssl_free( ssl->psk_identity );
3871 }
3872
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003873 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003874 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003875
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003876 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003877 ssl->psk_identity = (unsigned char *)
3878 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003879
3880 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3881 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3882
3883 memcpy( ssl->psk, psk, ssl->psk_len );
3884 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003885
3886 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003887}
3888
3889void ssl_set_psk_cb( ssl_context *ssl,
3890 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3891 size_t),
3892 void *p_psk )
3893{
3894 ssl->f_psk = f_psk;
3895 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003896}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003897#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003898
Paul Bakker48916f92012-09-16 19:57:18 +00003899#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003900int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003901{
3902 int ret;
3903
Paul Bakker48916f92012-09-16 19:57:18 +00003904 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003905 {
3906 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3907 return( ret );
3908 }
3909
Paul Bakker48916f92012-09-16 19:57:18 +00003910 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 {
3912 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3913 return( ret );
3914 }
3915
3916 return( 0 );
3917}
3918
Paul Bakker1b57b062011-01-06 15:48:19 +00003919int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3920{
3921 int ret;
3922
Paul Bakker66d5d072014-06-17 16:39:18 +02003923 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003924 {
3925 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3926 return( ret );
3927 }
3928
Paul Bakker66d5d072014-06-17 16:39:18 +02003929 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003930 {
3931 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3932 return( ret );
3933 }
3934
3935 return( 0 );
3936}
Paul Bakker48916f92012-09-16 19:57:18 +00003937#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003938
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003939#if defined(POLARSSL_SSL_SET_CURVES)
3940/*
3941 * Set the allowed elliptic curves
3942 */
3943void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3944{
3945 ssl->curve_list = curve_list;
3946}
3947#endif
3948
Paul Bakker0be444a2013-08-27 21:55:01 +02003949#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003950int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003951{
3952 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003953 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003954
3955 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003956
3957 if( ssl->hostname_len + 1 == 0 )
3958 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3959
Paul Bakker6e339b52013-07-03 13:37:05 +02003960 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003961
Paul Bakkerb15b8512012-01-13 13:44:06 +00003962 if( ssl->hostname == NULL )
3963 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3964
Paul Bakker3c2122f2013-06-24 19:03:14 +02003965 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003967
Paul Bakker40ea7de2009-05-03 10:18:48 +00003968 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
3970 return( 0 );
3971}
3972
Paul Bakker5701cdc2012-09-27 21:49:42 +00003973void ssl_set_sni( ssl_context *ssl,
3974 int (*f_sni)(void *, ssl_context *,
3975 const unsigned char *, size_t),
3976 void *p_sni )
3977{
3978 ssl->f_sni = f_sni;
3979 ssl->p_sni = p_sni;
3980}
Paul Bakker0be444a2013-08-27 21:55:01 +02003981#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003982
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003983#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003984int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003985{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003986 size_t cur_len, tot_len;
3987 const char **p;
3988
3989 /*
3990 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3991 * truncated". Check lengths now rather than later.
3992 */
3993 tot_len = 0;
3994 for( p = protos; *p != NULL; p++ )
3995 {
3996 cur_len = strlen( *p );
3997 tot_len += cur_len;
3998
3999 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4000 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4001 }
4002
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004003 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004004
4005 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004006}
4007
4008const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4009{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004010 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004011}
4012#endif /* POLARSSL_SSL_ALPN */
4013
Paul Bakker490ecc82011-10-06 13:04:09 +00004014void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4015{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004016 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4017 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4018 {
4019 ssl->max_major_ver = major;
4020 ssl->max_minor_ver = minor;
4021 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004022}
4023
Paul Bakker1d29fb52012-09-28 13:28:45 +00004024void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4025{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004026 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4027 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4028 {
4029 ssl->min_major_ver = major;
4030 ssl->min_minor_ver = minor;
4031 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004032}
4033
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004034#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4035void ssl_set_fallback( ssl_context *ssl, char fallback )
4036{
4037 ssl->fallback = fallback;
4038}
4039#endif
4040
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004041#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4042void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4043{
4044 ssl->encrypt_then_mac = etm;
4045}
4046#endif
4047
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004048#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4049void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4050{
4051 ssl->extended_ms = ems;
4052}
4053#endif
4054
Paul Bakker05decb22013-08-15 13:33:48 +02004055#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004056int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4057{
Paul Bakker77e257e2013-12-16 15:29:52 +01004058 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004059 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004060 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004061 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004062 }
4063
4064 ssl->mfl_code = mfl_code;
4065
4066 return( 0 );
4067}
Paul Bakker05decb22013-08-15 13:33:48 +02004068#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004069
Paul Bakker1f2bc622013-08-15 13:45:55 +02004070#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004071int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004072{
4073 if( ssl->endpoint != SSL_IS_CLIENT )
4074 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4075
Paul Bakker8c1ede62013-07-19 14:14:37 +02004076 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004077
4078 return( 0 );
4079}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004080#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004081
Paul Bakker48916f92012-09-16 19:57:18 +00004082void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4083{
4084 ssl->disable_renegotiation = renegotiation;
4085}
4086
4087void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4088{
4089 ssl->allow_legacy_renegotiation = allow_legacy;
4090}
4091
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004092void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4093{
4094 ssl->renego_max_records = max_records;
4095}
4096
Paul Bakkera503a632013-08-14 13:48:06 +02004097#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004098int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4099{
4100 ssl->session_tickets = use_tickets;
4101
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004102 if( ssl->endpoint == SSL_IS_CLIENT )
4103 return( 0 );
4104
4105 if( ssl->f_rng == NULL )
4106 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4107
4108 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004109}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004110
4111void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4112{
4113 ssl->ticket_lifetime = lifetime;
4114}
Paul Bakkera503a632013-08-14 13:48:06 +02004115#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004116
Paul Bakker5121ce52009-01-03 21:22:43 +00004117/*
4118 * SSL get accessors
4119 */
Paul Bakker23986e52011-04-24 08:57:21 +00004120size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121{
4122 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4123}
4124
Paul Bakkerff60ee62010-03-16 21:09:09 +00004125int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004126{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004127 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004128}
4129
Paul Bakkere3166ce2011-01-27 17:40:50 +00004130const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004131{
Paul Bakker926c8e42013-03-06 10:23:34 +01004132 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004133 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004134
Paul Bakkere3166ce2011-01-27 17:40:50 +00004135 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004136}
4137
Paul Bakker43ca69c2011-01-15 17:35:19 +00004138const char *ssl_get_version( const ssl_context *ssl )
4139{
4140 switch( ssl->minor_ver )
4141 {
4142 case SSL_MINOR_VERSION_0:
4143 return( "SSLv3.0" );
4144
4145 case SSL_MINOR_VERSION_1:
4146 return( "TLSv1.0" );
4147
4148 case SSL_MINOR_VERSION_2:
4149 return( "TLSv1.1" );
4150
Paul Bakker1ef83d62012-04-11 12:09:53 +00004151 case SSL_MINOR_VERSION_3:
4152 return( "TLSv1.2" );
4153
Paul Bakker43ca69c2011-01-15 17:35:19 +00004154 default:
4155 break;
4156 }
4157 return( "unknown" );
4158}
4159
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004160#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004161const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004162{
4163 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004164 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004165
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004166 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004167}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004168#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004169
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004170int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4171{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004172 if( ssl == NULL ||
4173 dst == NULL ||
4174 ssl->session == NULL ||
4175 ssl->endpoint != SSL_IS_CLIENT )
4176 {
4177 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4178 }
4179
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004180 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004181}
4182
Paul Bakker5121ce52009-01-03 21:22:43 +00004183/*
Paul Bakker1961b702013-01-25 14:49:24 +01004184 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004185 */
Paul Bakker1961b702013-01-25 14:49:24 +01004186int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004187{
Paul Bakker40e46942009-01-03 21:51:57 +00004188 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004189
Paul Bakker40e46942009-01-03 21:51:57 +00004190#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004191 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004192 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004193#endif
4194
Paul Bakker40e46942009-01-03 21:51:57 +00004195#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004196 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004197 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004198#endif
4199
Paul Bakker1961b702013-01-25 14:49:24 +01004200 return( ret );
4201}
4202
4203/*
4204 * Perform the SSL handshake
4205 */
4206int ssl_handshake( ssl_context *ssl )
4207{
4208 int ret = 0;
4209
4210 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4211
4212 while( ssl->state != SSL_HANDSHAKE_OVER )
4213 {
4214 ret = ssl_handshake_step( ssl );
4215
4216 if( ret != 0 )
4217 break;
4218 }
4219
Paul Bakker5121ce52009-01-03 21:22:43 +00004220 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4221
4222 return( ret );
4223}
4224
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004225#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004226/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004227 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004228 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004229static int ssl_write_hello_request( ssl_context *ssl )
4230{
4231 int ret;
4232
4233 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4234
4235 ssl->out_msglen = 4;
4236 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4237 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4238
4239 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4240 {
4241 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4242 return( ret );
4243 }
4244
4245 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4246
4247 return( 0 );
4248}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004249#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004250
4251/*
4252 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004253 * - any side: calling ssl_renegotiate(),
4254 * - client: receiving a HelloRequest during ssl_read(),
4255 * - server: receiving any handshake message on server during ssl_read() after
4256 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004257 * If the handshake doesn't complete due to waiting for I/O, it will continue
4258 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004259 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004260static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004261{
4262 int ret;
4263
4264 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4265
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004266 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4267 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004268
4269 ssl->state = SSL_HELLO_REQUEST;
4270 ssl->renegotiation = SSL_RENEGOTIATION;
4271
Paul Bakker48916f92012-09-16 19:57:18 +00004272 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4273 {
4274 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4275 return( ret );
4276 }
4277
4278 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4279
4280 return( 0 );
4281}
4282
4283/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004284 * Renegotiate current connection on client,
4285 * or request renegotiation on server
4286 */
4287int ssl_renegotiate( ssl_context *ssl )
4288{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004289 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004290
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004291#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004292 /* On server, just send the request */
4293 if( ssl->endpoint == SSL_IS_SERVER )
4294 {
4295 if( ssl->state != SSL_HANDSHAKE_OVER )
4296 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4297
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004298 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4299
4300 /* Did we already try/start sending HelloRequest? */
4301 if( ssl->out_left != 0 )
4302 return( ssl_flush_output( ssl ) );
4303
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004304 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004305 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004306#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004307
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004308#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004309 /*
4310 * On client, either start the renegotiation process or,
4311 * if already in progress, continue the handshake
4312 */
4313 if( ssl->renegotiation != SSL_RENEGOTIATION )
4314 {
4315 if( ssl->state != SSL_HANDSHAKE_OVER )
4316 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4317
4318 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4319 {
4320 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4321 return( ret );
4322 }
4323 }
4324 else
4325 {
4326 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4327 {
4328 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4329 return( ret );
4330 }
4331 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004332#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004333
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004334 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004335}
4336
4337/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004338 * Receive application data decrypted from the SSL layer
4339 */
Paul Bakker23986e52011-04-24 08:57:21 +00004340int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004341{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004342 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004343 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004344
4345 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4346
4347 if( ssl->state != SSL_HANDSHAKE_OVER )
4348 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004349 ret = ssl_handshake( ssl );
4350 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4351 {
4352 record_read = 1;
4353 }
4354 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004355 {
4356 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4357 return( ret );
4358 }
4359 }
4360
4361 if( ssl->in_offt == NULL )
4362 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004363 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004364 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004365 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4366 {
4367 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4368 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004369
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004370 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4371 return( ret );
4372 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004373 }
4374
4375 if( ssl->in_msglen == 0 &&
4376 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4377 {
4378 /*
4379 * OpenSSL sends empty messages to randomize the IV
4380 */
4381 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4382 {
Paul Bakker831a7552011-05-18 13:32:51 +00004383 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4384 return( 0 );
4385
Paul Bakker5121ce52009-01-03 21:22:43 +00004386 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4387 return( ret );
4388 }
4389 }
4390
Paul Bakker48916f92012-09-16 19:57:18 +00004391 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4392 {
4393 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4394
4395 if( ssl->endpoint == SSL_IS_CLIENT &&
4396 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4397 ssl->in_hslen != 4 ) )
4398 {
4399 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4400 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4401 }
4402
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004403 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4404 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004405 ssl->allow_legacy_renegotiation ==
4406 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004407 {
4408 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4409
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004410#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004411 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004412 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004413 /*
4414 * SSLv3 does not have a "no_renegotiation" alert
4415 */
4416 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4417 return( ret );
4418 }
4419 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004420#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004421#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4422 defined(POLARSSL_SSL_PROTO_TLS1_2)
4423 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004424 {
4425 if( ( ret = ssl_send_alert_message( ssl,
4426 SSL_ALERT_LEVEL_WARNING,
4427 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4428 {
4429 return( ret );
4430 }
Paul Bakker48916f92012-09-16 19:57:18 +00004431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004432 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004433#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4434 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004435 {
4436 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004437 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004438 }
Paul Bakker48916f92012-09-16 19:57:18 +00004439 }
4440 else
4441 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004442 ret = ssl_start_renegotiation( ssl );
4443 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4444 {
4445 record_read = 1;
4446 }
4447 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004448 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004449 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004450 return( ret );
4451 }
Paul Bakker48916f92012-09-16 19:57:18 +00004452 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004453
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004454 /* If a non-handshake record was read during renego, fallthrough,
4455 * else tell the user they should call ssl_read() again */
4456 if( ! record_read )
4457 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004458 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004459 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4460 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004461 ssl->renego_records_seen++;
4462
4463 if( ssl->renego_max_records >= 0 &&
4464 ssl->renego_records_seen > ssl->renego_max_records )
4465 {
4466 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4467 "but not honored by client" ) );
4468 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4469 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004470 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004471
4472 /* Fatal and closure alerts handled by ssl_read_record() */
4473 if( ssl->in_msgtype == SSL_MSG_ALERT )
4474 {
4475 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4476 return( POLARSSL_ERR_NET_WANT_READ );
4477 }
4478
4479 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004480 {
4481 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004482 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004483 }
4484
4485 ssl->in_offt = ssl->in_msg;
4486 }
4487
4488 n = ( len < ssl->in_msglen )
4489 ? len : ssl->in_msglen;
4490
4491 memcpy( buf, ssl->in_offt, n );
4492 ssl->in_msglen -= n;
4493
4494 if( ssl->in_msglen == 0 )
4495 /* all bytes consumed */
4496 ssl->in_offt = NULL;
4497 else
4498 /* more data available */
4499 ssl->in_offt += n;
4500
4501 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4502
Paul Bakker23986e52011-04-24 08:57:21 +00004503 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004504}
4505
4506/*
4507 * Send application data to be encrypted by the SSL layer
4508 */
Paul Bakker23986e52011-04-24 08:57:21 +00004509int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004510{
Paul Bakker23986e52011-04-24 08:57:21 +00004511 int ret;
4512 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004513 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004514
4515 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4516
4517 if( ssl->state != SSL_HANDSHAKE_OVER )
4518 {
4519 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4520 {
4521 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4522 return( ret );
4523 }
4524 }
4525
Paul Bakker05decb22013-08-15 13:33:48 +02004526#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004527 /*
4528 * Assume mfl_code is correct since it was checked when set
4529 */
4530 max_len = mfl_code_to_length[ssl->mfl_code];
4531
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004532 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004533 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004534 */
4535 if( ssl->session_out != NULL &&
4536 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4537 {
4538 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4539 }
Paul Bakker05decb22013-08-15 13:33:48 +02004540#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004541
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004542 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004543
Paul Bakker5121ce52009-01-03 21:22:43 +00004544 if( ssl->out_left != 0 )
4545 {
4546 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4547 {
4548 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4549 return( ret );
4550 }
4551 }
Paul Bakker887bd502011-06-08 13:10:54 +00004552 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004553 {
Paul Bakker887bd502011-06-08 13:10:54 +00004554 ssl->out_msglen = n;
4555 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4556 memcpy( ssl->out_msg, buf, n );
4557
4558 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4559 {
4560 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4561 return( ret );
4562 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004563 }
4564
4565 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4566
Paul Bakker23986e52011-04-24 08:57:21 +00004567 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004568}
4569
4570/*
4571 * Notify the peer that the connection is being closed
4572 */
4573int ssl_close_notify( ssl_context *ssl )
4574{
4575 int ret;
4576
4577 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4578
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004579 if( ssl->out_left != 0 )
4580 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004581
4582 if( ssl->state == SSL_HANDSHAKE_OVER )
4583 {
Paul Bakker48916f92012-09-16 19:57:18 +00004584 if( ( ret = ssl_send_alert_message( ssl,
4585 SSL_ALERT_LEVEL_WARNING,
4586 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004587 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004588 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004589 return( ret );
4590 }
4591 }
4592
4593 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4594
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004595 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004596}
4597
Paul Bakker48916f92012-09-16 19:57:18 +00004598void ssl_transform_free( ssl_transform *transform )
4599{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004600 if( transform == NULL )
4601 return;
4602
Paul Bakker48916f92012-09-16 19:57:18 +00004603#if defined(POLARSSL_ZLIB_SUPPORT)
4604 deflateEnd( &transform->ctx_deflate );
4605 inflateEnd( &transform->ctx_inflate );
4606#endif
4607
Paul Bakker84bbeb52014-07-01 14:53:22 +02004608 cipher_free( &transform->cipher_ctx_enc );
4609 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004610
Paul Bakker84bbeb52014-07-01 14:53:22 +02004611 md_free( &transform->md_ctx_enc );
4612 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004613
Paul Bakker34617722014-06-13 17:20:13 +02004614 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004615}
4616
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004617#if defined(POLARSSL_X509_CRT_PARSE_C)
4618static void ssl_key_cert_free( ssl_key_cert *key_cert )
4619{
4620 ssl_key_cert *cur = key_cert, *next;
4621
4622 while( cur != NULL )
4623 {
4624 next = cur->next;
4625
4626 if( cur->key_own_alloc )
4627 {
4628 pk_free( cur->key );
4629 polarssl_free( cur->key );
4630 }
4631 polarssl_free( cur );
4632
4633 cur = next;
4634 }
4635}
4636#endif /* POLARSSL_X509_CRT_PARSE_C */
4637
Paul Bakker48916f92012-09-16 19:57:18 +00004638void ssl_handshake_free( ssl_handshake_params *handshake )
4639{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004640 if( handshake == NULL )
4641 return;
4642
Paul Bakker48916f92012-09-16 19:57:18 +00004643#if defined(POLARSSL_DHM_C)
4644 dhm_free( &handshake->dhm_ctx );
4645#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004646#if defined(POLARSSL_ECDH_C)
4647 ecdh_free( &handshake->ecdh_ctx );
4648#endif
4649
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004650#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004651 /* explicit void pointer cast for buggy MS compiler */
4652 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004653#endif
4654
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004655#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4656 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4657 /*
4658 * Free only the linked list wrapper, not the keys themselves
4659 * since the belong to the SNI callback
4660 */
4661 if( handshake->sni_key_cert != NULL )
4662 {
4663 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4664
4665 while( cur != NULL )
4666 {
4667 next = cur->next;
4668 polarssl_free( cur );
4669 cur = next;
4670 }
4671 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004672#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004673
Paul Bakker34617722014-06-13 17:20:13 +02004674 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004675}
4676
4677void ssl_session_free( ssl_session *session )
4678{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004679 if( session == NULL )
4680 return;
4681
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004682#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004683 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004684 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004685 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004686 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004687 }
Paul Bakkered27a042013-04-18 22:46:23 +02004688#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004689
Paul Bakkera503a632013-08-14 13:48:06 +02004690#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004691 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004692#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004693
Paul Bakker34617722014-06-13 17:20:13 +02004694 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004695}
4696
Paul Bakker5121ce52009-01-03 21:22:43 +00004697/*
4698 * Free an SSL context
4699 */
4700void ssl_free( ssl_context *ssl )
4701{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004702 if( ssl == NULL )
4703 return;
4704
Paul Bakker5121ce52009-01-03 21:22:43 +00004705 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4706
Paul Bakker5121ce52009-01-03 21:22:43 +00004707 if( ssl->out_ctr != NULL )
4708 {
Paul Bakker34617722014-06-13 17:20:13 +02004709 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004710 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004711 }
4712
4713 if( ssl->in_ctr != NULL )
4714 {
Paul Bakker34617722014-06-13 17:20:13 +02004715 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004716 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004717 }
4718
Paul Bakker16770332013-10-11 09:59:44 +02004719#if defined(POLARSSL_ZLIB_SUPPORT)
4720 if( ssl->compress_buf != NULL )
4721 {
Paul Bakker34617722014-06-13 17:20:13 +02004722 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004723 polarssl_free( ssl->compress_buf );
4724 }
4725#endif
4726
Paul Bakker40e46942009-01-03 21:51:57 +00004727#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004728 mpi_free( &ssl->dhm_P );
4729 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004730#endif
4731
Paul Bakker48916f92012-09-16 19:57:18 +00004732 if( ssl->transform )
4733 {
4734 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004735 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004736 }
4737
4738 if( ssl->handshake )
4739 {
4740 ssl_handshake_free( ssl->handshake );
4741 ssl_transform_free( ssl->transform_negotiate );
4742 ssl_session_free( ssl->session_negotiate );
4743
Paul Bakker6e339b52013-07-03 13:37:05 +02004744 polarssl_free( ssl->handshake );
4745 polarssl_free( ssl->transform_negotiate );
4746 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004747 }
4748
Paul Bakkerc0463502013-02-14 11:19:38 +01004749 if( ssl->session )
4750 {
4751 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004752 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004753 }
4754
Paul Bakkera503a632013-08-14 13:48:06 +02004755#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004756 if( ssl->ticket_keys )
4757 {
4758 ssl_ticket_keys_free( ssl->ticket_keys );
4759 polarssl_free( ssl->ticket_keys );
4760 }
Paul Bakkera503a632013-08-14 13:48:06 +02004761#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004762
Paul Bakker0be444a2013-08-27 21:55:01 +02004763#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004764 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004765 {
Paul Bakker34617722014-06-13 17:20:13 +02004766 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004767 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004768 ssl->hostname_len = 0;
4769 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004770#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004771
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004772#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004773 if( ssl->psk != NULL )
4774 {
Paul Bakker34617722014-06-13 17:20:13 +02004775 polarssl_zeroize( ssl->psk, ssl->psk_len );
4776 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004777 polarssl_free( ssl->psk );
4778 polarssl_free( ssl->psk_identity );
4779 ssl->psk_len = 0;
4780 ssl->psk_identity_len = 0;
4781 }
4782#endif
4783
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004784#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004785 ssl_key_cert_free( ssl->key_cert );
4786#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004787
Paul Bakker05ef8352012-05-08 09:17:57 +00004788#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4789 if( ssl_hw_record_finish != NULL )
4790 {
4791 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4792 ssl_hw_record_finish( ssl );
4793 }
4794#endif
4795
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004797
Paul Bakker86f04f42013-02-14 11:20:09 +01004798 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004799 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004800}
4801
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004802#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004803/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004804 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004805 */
4806unsigned char ssl_sig_from_pk( pk_context *pk )
4807{
4808#if defined(POLARSSL_RSA_C)
4809 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4810 return( SSL_SIG_RSA );
4811#endif
4812#if defined(POLARSSL_ECDSA_C)
4813 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4814 return( SSL_SIG_ECDSA );
4815#endif
4816 return( SSL_SIG_ANON );
4817}
4818
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004819pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4820{
4821 switch( sig )
4822 {
4823#if defined(POLARSSL_RSA_C)
4824 case SSL_SIG_RSA:
4825 return( POLARSSL_PK_RSA );
4826#endif
4827#if defined(POLARSSL_ECDSA_C)
4828 case SSL_SIG_ECDSA:
4829 return( POLARSSL_PK_ECDSA );
4830#endif
4831 default:
4832 return( POLARSSL_PK_NONE );
4833 }
4834}
Paul Bakker9af723c2014-05-01 13:03:14 +02004835#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004836
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004837/*
4838 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4839 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004840md_type_t ssl_md_alg_from_hash( unsigned char hash )
4841{
4842 switch( hash )
4843 {
4844#if defined(POLARSSL_MD5_C)
4845 case SSL_HASH_MD5:
4846 return( POLARSSL_MD_MD5 );
4847#endif
4848#if defined(POLARSSL_SHA1_C)
4849 case SSL_HASH_SHA1:
4850 return( POLARSSL_MD_SHA1 );
4851#endif
4852#if defined(POLARSSL_SHA256_C)
4853 case SSL_HASH_SHA224:
4854 return( POLARSSL_MD_SHA224 );
4855 case SSL_HASH_SHA256:
4856 return( POLARSSL_MD_SHA256 );
4857#endif
4858#if defined(POLARSSL_SHA512_C)
4859 case SSL_HASH_SHA384:
4860 return( POLARSSL_MD_SHA384 );
4861 case SSL_HASH_SHA512:
4862 return( POLARSSL_MD_SHA512 );
4863#endif
4864 default:
4865 return( POLARSSL_MD_NONE );
4866 }
4867}
4868
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004869#if defined(POLARSSL_SSL_SET_CURVES)
4870/*
4871 * Check is a curve proposed by the peer is in our list.
4872 * Return 1 if we're willing to use it, 0 otherwise.
4873 */
4874int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4875{
4876 const ecp_group_id *gid;
4877
4878 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4879 if( *gid == grp_id )
4880 return( 1 );
4881
4882 return( 0 );
4883}
Paul Bakker9af723c2014-05-01 13:03:14 +02004884#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004885
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004886#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004887int ssl_check_cert_usage( const x509_crt *cert,
4888 const ssl_ciphersuite_t *ciphersuite,
4889 int cert_endpoint )
4890{
4891#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4892 int usage = 0;
4893#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004894#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4895 const char *ext_oid;
4896 size_t ext_len;
4897#endif
4898
4899#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4900 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4901 ((void) cert);
4902 ((void) cert_endpoint);
4903#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004904
4905#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4906 if( cert_endpoint == SSL_IS_SERVER )
4907 {
4908 /* Server part of the key exchange */
4909 switch( ciphersuite->key_exchange )
4910 {
4911 case POLARSSL_KEY_EXCHANGE_RSA:
4912 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4913 usage = KU_KEY_ENCIPHERMENT;
4914 break;
4915
4916 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4917 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4918 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4919 usage = KU_DIGITAL_SIGNATURE;
4920 break;
4921
4922 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4923 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4924 usage = KU_KEY_AGREEMENT;
4925 break;
4926
4927 /* Don't use default: we want warnings when adding new values */
4928 case POLARSSL_KEY_EXCHANGE_NONE:
4929 case POLARSSL_KEY_EXCHANGE_PSK:
4930 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4931 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4932 usage = 0;
4933 }
4934 }
4935 else
4936 {
4937 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4938 usage = KU_DIGITAL_SIGNATURE;
4939 }
4940
4941 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4942 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004943#else
4944 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004945#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4946
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004947#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4948 if( cert_endpoint == SSL_IS_SERVER )
4949 {
4950 ext_oid = OID_SERVER_AUTH;
4951 ext_len = OID_SIZE( OID_SERVER_AUTH );
4952 }
4953 else
4954 {
4955 ext_oid = OID_CLIENT_AUTH;
4956 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4957 }
4958
4959 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4960 return( -1 );
4961#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4962
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004963 return( 0 );
4964}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004965#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004966
4967#endif /* POLARSSL_SSL_TLS_C */