blob: 736eac66832f02e9051d5de9bf4eda301692fa05 [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
Paul Bakker48916f92012-09-16 19:57:18 +0000473 handshake->tls_prf( handshake->premaster, handshake->pmslen,
474 "master secret",
475 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakker34617722014-06-13 17:20:13 +0200477 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 }
479 else
480 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
481
482 /*
483 * Swap the client and server random values.
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 memcpy( tmp, handshake->randbytes, 64 );
486 memcpy( handshake->randbytes, tmp + 32, 32 );
487 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200488 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 /*
491 * SSLv3:
492 * key block =
493 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
494 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
495 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
496 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
497 * ...
498 *
499 * TLSv1:
500 * key block = PRF( master, "key expansion", randbytes )
501 */
Paul Bakker48916f92012-09-16 19:57:18 +0000502 handshake->tls_prf( session->master, 48, "key expansion",
503 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker48916f92012-09-16 19:57:18 +0000505 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
506 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
507 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
508 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
510
Paul Bakker34617722014-06-13 17:20:13 +0200511 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 /*
514 * Determine the appropriate key, IV and MAC length.
515 */
Paul Bakker68884e32013-01-07 18:20:04 +0100516
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200517 transform->keylen = cipher_info->key_length / 8;
518
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200519 if( cipher_info->mode == POLARSSL_MODE_GCM ||
520 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200522 transform->maclen = 0;
523
Paul Bakker68884e32013-01-07 18:20:04 +0100524 transform->ivlen = 12;
525 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200526
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200527 /* Minimum length is expicit IV + tag */
528 transform->minlen = transform->ivlen - transform->fixed_ivlen
529 + ( transform->ciphersuite_info->flags &
530 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100531 }
532 else
533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 int ret;
535
536 /* Initialize HMAC contexts */
537 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
538 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100539 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200540 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
541 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100542 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200544 /* Get MAC length */
545 transform->maclen = md_get_size( md_info );
546
547#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
548 /*
549 * If HMAC is to be truncated, we shall keep the leftmost bytes,
550 * (rfc 6066 page 13 or rfc 2104 section 4),
551 * so we only need to adjust the length here.
552 */
553 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
554 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
555#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
556
557 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100558 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200560 /* Minimum length */
561 if( cipher_info->mode == POLARSSL_MODE_STREAM )
562 transform->minlen = transform->maclen;
563 else
Paul Bakker68884e32013-01-07 18:20:04 +0100564 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200565 /*
566 * GenericBlockCipher:
567 * first multiple of blocklen greater than maclen
568 * + IV except for SSL3 and TLS 1.0
569 */
570 transform->minlen = transform->maclen
571 + cipher_info->block_size
572 - transform->maclen % cipher_info->block_size;
573
574#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
575 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
576 ssl->minor_ver == SSL_MINOR_VERSION_1 )
577 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100578 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200579#endif
580#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
581 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
582 ssl->minor_ver == SSL_MINOR_VERSION_3 )
583 {
584 transform->minlen += transform->ivlen;
585 }
586 else
587#endif
588 {
589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
591 }
Paul Bakker68884e32013-01-07 18:20:04 +0100592 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 }
594
595 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000596 transform->keylen, transform->minlen, transform->ivlen,
597 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * Finally setup the cipher contexts, IVs and MAC secrets.
601 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100602#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 if( ssl->endpoint == SSL_IS_CLIENT )
604 {
Paul Bakker48916f92012-09-16 19:57:18 +0000605 key1 = keyblk + transform->maclen * 2;
606 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
Paul Bakker68884e32013-01-07 18:20:04 +0100608 mac_enc = keyblk;
609 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000611 /*
612 * This is not used in TLS v1.1.
613 */
Paul Bakker48916f92012-09-16 19:57:18 +0000614 iv_copy_len = ( transform->fixed_ivlen ) ?
615 transform->fixed_ivlen : transform->ivlen;
616 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
617 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000618 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 }
620 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100621#endif /* POLARSSL_SSL_CLI_C */
622#if defined(POLARSSL_SSL_SRV_C)
623 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 {
Paul Bakker48916f92012-09-16 19:57:18 +0000625 key1 = keyblk + transform->maclen * 2 + transform->keylen;
626 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
Paul Bakker68884e32013-01-07 18:20:04 +0100628 mac_enc = keyblk + transform->maclen;
629 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000631 /*
632 * This is not used in TLS v1.1.
633 */
Paul Bakker48916f92012-09-16 19:57:18 +0000634 iv_copy_len = ( transform->fixed_ivlen ) ?
635 transform->fixed_ivlen : transform->ivlen;
636 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
637 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000638 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100640 else
641#endif /* POLARSSL_SSL_SRV_C */
642 {
643 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
644 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
645 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200647#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100648 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
649 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100650 if( transform->maclen > sizeof transform->mac_enc )
651 {
652 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200653 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100654 }
655
Paul Bakker68884e32013-01-07 18:20:04 +0100656 memcpy( transform->mac_enc, mac_enc, transform->maclen );
657 memcpy( transform->mac_dec, mac_dec, transform->maclen );
658 }
659 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200660#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200661#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
662 defined(POLARSSL_SSL_PROTO_TLS1_2)
663 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100664 {
665 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
666 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
667 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200668 else
669#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200670 {
671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200672 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200673 }
Paul Bakker68884e32013-01-07 18:20:04 +0100674
Paul Bakker05ef8352012-05-08 09:17:57 +0000675#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200676 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 {
678 int ret = 0;
679
680 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
681
Paul Bakker07eb38b2012-12-19 14:42:06 +0100682 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
683 transform->iv_enc, transform->iv_dec,
684 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100685 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100686 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000687 {
688 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200689 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000690 }
691 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200692#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000693
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200694 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
695 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200697 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
698 return( ret );
699 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200700
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200701 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
702 cipher_info ) ) != 0 )
703 {
704 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
705 return( ret );
706 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200707
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200708 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
709 cipher_info->key_length,
710 POLARSSL_ENCRYPT ) ) != 0 )
711 {
712 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
713 return( ret );
714 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200715
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200716 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
717 cipher_info->key_length,
718 POLARSSL_DECRYPT ) ) != 0 )
719 {
720 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
721 return( ret );
722 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200723
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200724#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200725 if( cipher_info->mode == POLARSSL_MODE_CBC )
726 {
727 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
728 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200729 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200730 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
731 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200732 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200733
734 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
735 POLARSSL_PADDING_NONE ) ) != 0 )
736 {
737 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
738 return( ret );
739 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200741#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000742
Paul Bakker34617722014-06-13 17:20:13 +0200743 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Paul Bakker2770fbd2012-07-03 13:30:23 +0000745#if defined(POLARSSL_ZLIB_SUPPORT)
746 // Initialize compression
747 //
Paul Bakker48916f92012-09-16 19:57:18 +0000748 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000749 {
Paul Bakker16770332013-10-11 09:59:44 +0200750 if( ssl->compress_buf == NULL )
751 {
752 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
753 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
754 if( ssl->compress_buf == NULL )
755 {
756 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
757 SSL_BUFFER_LEN ) );
758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
759 }
760 }
761
Paul Bakker2770fbd2012-07-03 13:30:23 +0000762 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
763
Paul Bakker48916f92012-09-16 19:57:18 +0000764 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
765 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000766
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200767 if( deflateInit( &transform->ctx_deflate,
768 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000769 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000770 {
771 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
772 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
773 }
774 }
775#endif /* POLARSSL_ZLIB_SUPPORT */
776
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
778
779 return( 0 );
780}
781
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200782#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000783void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000784{
785 md5_context md5;
786 sha1_context sha1;
787 unsigned char pad_1[48];
788 unsigned char pad_2[48];
789
Paul Bakker380da532012-04-18 16:10:25 +0000790 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakker48916f92012-09-16 19:57:18 +0000792 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
793 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000794
Paul Bakker380da532012-04-18 16:10:25 +0000795 memset( pad_1, 0x36, 48 );
796 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Paul Bakker48916f92012-09-16 19:57:18 +0000798 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 md5_update( &md5, pad_1, 48 );
800 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000801
Paul Bakker380da532012-04-18 16:10:25 +0000802 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000803 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000804 md5_update( &md5, pad_2, 48 );
805 md5_update( &md5, hash, 16 );
806 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000807
Paul Bakker48916f92012-09-16 19:57:18 +0000808 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000809 sha1_update( &sha1, pad_1, 40 );
810 sha1_finish( &sha1, hash + 16 );
811
812 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000813 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000814 sha1_update( &sha1, pad_2, 40 );
815 sha1_update( &sha1, hash + 16, 20 );
816 sha1_finish( &sha1, hash + 16 );
817
818 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
819 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
820
Paul Bakker5b4af392014-06-26 12:09:34 +0200821 md5_free( &md5 );
822 sha1_free( &sha1 );
823
Paul Bakker380da532012-04-18 16:10:25 +0000824 return;
825}
Paul Bakker9af723c2014-05-01 13:03:14 +0200826#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000827
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000829void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
830{
831 md5_context md5;
832 sha1_context sha1;
833
834 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
835
Paul Bakker48916f92012-09-16 19:57:18 +0000836 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
837 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000838
Paul Bakker48916f92012-09-16 19:57:18 +0000839 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000840 sha1_finish( &sha1, hash + 16 );
841
842 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
843 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
844
Paul Bakker5b4af392014-06-26 12:09:34 +0200845 md5_free( &md5 );
846 sha1_free( &sha1 );
847
Paul Bakker380da532012-04-18 16:10:25 +0000848 return;
849}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200850#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000851
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852#if defined(POLARSSL_SSL_PROTO_TLS1_2)
853#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000854void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
855{
Paul Bakker9e36f042013-06-30 14:34:05 +0200856 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000857
858 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
859
Paul Bakker9e36f042013-06-30 14:34:05 +0200860 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
861 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000862
863 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
864 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
865
Paul Bakker5b4af392014-06-26 12:09:34 +0200866 sha256_free( &sha256 );
867
Paul Bakker380da532012-04-18 16:10:25 +0000868 return;
869}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200870#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000871
Paul Bakker9e36f042013-06-30 14:34:05 +0200872#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000873void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
874{
Paul Bakker9e36f042013-06-30 14:34:05 +0200875 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000876
877 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
878
Paul Bakker9e36f042013-06-30 14:34:05 +0200879 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
880 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Paul Bakkerca4ab492012-04-18 14:23:57 +0000882 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
884
Paul Bakker5b4af392014-06-26 12:09:34 +0200885 sha512_free( &sha512 );
886
Paul Bakker5121ce52009-01-03 21:22:43 +0000887 return;
888}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200889#endif /* POLARSSL_SHA512_C */
890#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200892#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200893int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
894{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200895 unsigned char *p = ssl->handshake->premaster;
896 unsigned char *end = p + sizeof( ssl->handshake->premaster );
897
898 /*
899 * PMS = struct {
900 * opaque other_secret<0..2^16-1>;
901 * opaque psk<0..2^16-1>;
902 * };
903 * with "other_secret" depending on the particular key exchange
904 */
905#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
906 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
907 {
908 if( end - p < 2 + (int) ssl->psk_len )
909 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
910
911 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
912 *(p++) = (unsigned char)( ssl->psk_len );
913 p += ssl->psk_len;
914 }
915 else
916#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200917#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
918 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
919 {
920 /*
921 * other_secret already set by the ClientKeyExchange message,
922 * and is 48 bytes long
923 */
924 *p++ = 0;
925 *p++ = 48;
926 p += 48;
927 }
928 else
929#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
931 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
932 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200933 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200934 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200935
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200936 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200937 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200938 p + 2, &len,
939 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200940 {
941 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
942 return( ret );
943 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200944 *(p++) = (unsigned char)( len >> 8 );
945 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200946 p += len;
947
948 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
949 }
950 else
951#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
952#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
953 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
954 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200955 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200956 size_t zlen;
957
958 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200959 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200960 ssl->f_rng, ssl->p_rng ) ) != 0 )
961 {
962 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
963 return( ret );
964 }
965
966 *(p++) = (unsigned char)( zlen >> 8 );
967 *(p++) = (unsigned char)( zlen );
968 p += zlen;
969
970 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
971 }
972 else
973#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
974 {
975 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200976 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200977 }
978
979 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100980 if( end - p < 2 + (int) ssl->psk_len )
981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
982
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
984 *(p++) = (unsigned char)( ssl->psk_len );
985 memcpy( p, ssl->psk, ssl->psk_len );
986 p += ssl->psk_len;
987
988 ssl->handshake->pmslen = p - ssl->handshake->premaster;
989
990 return( 0 );
991}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200992#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200993
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000995/*
996 * SSLv3.0 MAC functions
997 */
Paul Bakker68884e32013-01-07 18:20:04 +0100998static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
999 unsigned char *buf, size_t len,
1000 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001001{
1002 unsigned char header[11];
1003 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001004 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001005 int md_size = md_get_size( md_ctx->md_info );
1006 int md_type = md_get_type( md_ctx->md_info );
1007
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001008 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001009 if( md_type == POLARSSL_MD_MD5 )
1010 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001011 else
Paul Bakker68884e32013-01-07 18:20:04 +01001012 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014 memcpy( header, ctr, 8 );
1015 header[ 8] = (unsigned char) type;
1016 header[ 9] = (unsigned char)( len >> 8 );
1017 header[10] = (unsigned char)( len );
1018
Paul Bakker68884e32013-01-07 18:20:04 +01001019 memset( padding, 0x36, padlen );
1020 md_starts( md_ctx );
1021 md_update( md_ctx, secret, md_size );
1022 md_update( md_ctx, padding, padlen );
1023 md_update( md_ctx, header, 11 );
1024 md_update( md_ctx, buf, len );
1025 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Paul Bakker68884e32013-01-07 18:20:04 +01001027 memset( padding, 0x5C, padlen );
1028 md_starts( md_ctx );
1029 md_update( md_ctx, secret, md_size );
1030 md_update( md_ctx, padding, padlen );
1031 md_update( md_ctx, buf + len, md_size );
1032 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001033}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001034#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001035
Paul Bakker5121ce52009-01-03 21:22:43 +00001036/*
1037 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001038 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001039static int ssl_encrypt_buf( ssl_context *ssl )
1040{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001041 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001042 const cipher_mode_t mode = cipher_get_cipher_mode(
1043 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001044
1045 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1046
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001047 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1048 ssl->out_msg, ssl->out_msglen );
1049
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001051 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001053#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1054 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1055 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001056 if( mode != POLARSSL_MODE_GCM &&
1057 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001058 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001059#if defined(POLARSSL_SSL_PROTO_SSL3)
1060 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1061 {
1062 ssl_mac( &ssl->transform_out->md_ctx_enc,
1063 ssl->transform_out->mac_enc,
1064 ssl->out_msg, ssl->out_msglen,
1065 ssl->out_ctr, ssl->out_msgtype );
1066 }
1067 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068#endif
1069#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001070 defined(POLARSSL_SSL_PROTO_TLS1_2)
1071 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1072 {
1073 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1074 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1075 ssl->out_msg, ssl->out_msglen );
1076 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1077 ssl->out_msg + ssl->out_msglen );
1078 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1079 }
1080 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001081#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 {
1083 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001084 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001085 }
1086
1087 SSL_DEBUG_BUF( 4, "computed mac",
1088 ssl->out_msg + ssl->out_msglen,
1089 ssl->transform_out->maclen );
1090
1091 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001092 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001093#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001095 /*
1096 * Encrypt
1097 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001098#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001099 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001100 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001101 int ret;
1102 size_t olen = 0;
1103
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1105 "including %d bytes of padding",
1106 ssl->out_msglen, 0 ) );
1107
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001108 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001109 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001110 ssl->transform_out->ivlen,
1111 ssl->out_msg, ssl->out_msglen,
1112 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001113 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001114 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001115 return( ret );
1116 }
1117
1118 if( ssl->out_msglen != olen )
1119 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001120 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001121 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001122 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001123 }
Paul Bakker68884e32013-01-07 18:20:04 +01001124 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001125#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001126#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1127 if( mode == POLARSSL_MODE_GCM ||
1128 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001129 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001130 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001131 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001132 unsigned char *enc_msg;
1133 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001134 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1135 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001136
Paul Bakkerca4ab492012-04-18 14:23:57 +00001137 memcpy( add_data, ssl->out_ctr, 8 );
1138 add_data[8] = ssl->out_msgtype;
1139 add_data[9] = ssl->major_ver;
1140 add_data[10] = ssl->minor_ver;
1141 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1142 add_data[12] = ssl->out_msglen & 0xFF;
1143
1144 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1145 add_data, 13 );
1146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 /*
1148 * Generate IV
1149 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001150#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001151 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001152 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1153 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001154 if( ret != 0 )
1155 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001156
Paul Bakker68884e32013-01-07 18:20:04 +01001157 memcpy( ssl->out_iv,
1158 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1159 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001160#else
1161 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1162 {
1163 /* Reminder if we ever add an AEAD mode with a different size */
1164 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1165 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1166 }
1167
1168 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1169 ssl->out_ctr, 8 );
1170 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1171#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001172
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001173 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001174 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001175
Paul Bakker68884e32013-01-07 18:20:04 +01001176 /*
1177 * Fix pointer positions and message length with added IV
1178 */
1179 enc_msg = ssl->out_msg;
1180 enc_msglen = ssl->out_msglen;
1181 ssl->out_msglen += ssl->transform_out->ivlen -
1182 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001183
Paul Bakker68884e32013-01-07 18:20:04 +01001184 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1185 "including %d bytes of padding",
1186 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001187
Paul Bakker68884e32013-01-07 18:20:04 +01001188 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001189 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001190 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001191 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1192 ssl->transform_out->iv_enc,
1193 ssl->transform_out->ivlen,
1194 add_data, 13,
1195 enc_msg, enc_msglen,
1196 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001197 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001198 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001199 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001200 return( ret );
1201 }
1202
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001203 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001204 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001205 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001206 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001207 }
1208
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001209 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001210
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001211 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001214#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001215#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1216 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001217 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001219 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001221 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222
Paul Bakker48916f92012-09-16 19:57:18 +00001223 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1224 ssl->transform_out->ivlen;
1225 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001226 padlen = 0;
1227
1228 for( i = 0; i <= padlen; i++ )
1229 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1230
1231 ssl->out_msglen += padlen + 1;
1232
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 enc_msglen = ssl->out_msglen;
1234 enc_msg = ssl->out_msg;
1235
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001236#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001237 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001238 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1239 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001241 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242 {
1243 /*
1244 * Generate IV
1245 */
Paul Bakker48916f92012-09-16 19:57:18 +00001246 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1247 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001248 if( ret != 0 )
1249 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250
Paul Bakker92be97b2013-01-02 17:30:03 +01001251 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001252 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253
1254 /*
1255 * Fix pointer positions and message length with added IV
1256 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001257 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001258 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001259 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001260 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001261#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001262
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001264 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001265 ssl->out_msglen, ssl->transform_out->ivlen,
1266 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001268 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001270 ssl->transform_out->ivlen,
1271 enc_msg, enc_msglen,
1272 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001273 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001274 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001275 return( ret );
1276 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001277
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 if( enc_msglen != olen )
1279 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001280 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001281 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001282 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001283
1284#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001285 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1286 {
1287 /*
1288 * Save IV in SSL3 and TLS1
1289 */
1290 memcpy( ssl->transform_out->iv_enc,
1291 ssl->transform_out->cipher_ctx_enc.iv,
1292 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001293 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001295 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001296 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001297#endif /* POLARSSL_CIPHER_MODE_CBC &&
1298 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001299 {
1300 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001301 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303
Paul Bakkerca4ab492012-04-18 14:23:57 +00001304 for( i = 8; i > 0; i-- )
1305 if( ++ssl->out_ctr[i - 1] != 0 )
1306 break;
1307
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001308 /* The loops goes to its end iff the counter is wrapping */
1309 if( i == 0 )
1310 {
1311 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1312 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1313 }
1314
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1316
1317 return( 0 );
1318}
1319
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001320#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001321
Paul Bakker5121ce52009-01-03 21:22:43 +00001322static int ssl_decrypt_buf( ssl_context *ssl )
1323{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001324 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001325 const cipher_mode_t mode = cipher_get_cipher_mode(
1326 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001327#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1328 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1329 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1330 size_t padlen = 0, correct = 1;
1331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
1333 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1334
Paul Bakker48916f92012-09-16 19:57:18 +00001335 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 {
1337 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001338 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001339 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001342#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001343 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001344 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001345 int ret;
1346 size_t olen = 0;
1347
Paul Bakker68884e32013-01-07 18:20:04 +01001348 padlen = 0;
1349
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001350 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001351 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001352 ssl->transform_in->ivlen,
1353 ssl->in_msg, ssl->in_msglen,
1354 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001355 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001356 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001357 return( ret );
1358 }
1359
1360 if( ssl->in_msglen != olen )
1361 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001362 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001363 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 }
Paul Bakker68884e32013-01-07 18:20:04 +01001366 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001367#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001368#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1369 if( mode == POLARSSL_MODE_GCM ||
1370 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001371 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001372 int ret;
1373 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001374 unsigned char *dec_msg;
1375 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001376 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001377 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1378 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001379 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1380 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001381
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001382 if( ssl->in_msglen < explicit_iv_len + taglen )
1383 {
1384 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1385 "+ taglen (%d)", ssl->in_msglen,
1386 explicit_iv_len, taglen ) );
1387 return( POLARSSL_ERR_SSL_INVALID_MAC );
1388 }
1389 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1390
Paul Bakker68884e32013-01-07 18:20:04 +01001391 dec_msg = ssl->in_msg;
1392 dec_msg_result = ssl->in_msg;
1393 ssl->in_msglen = dec_msglen;
1394
1395 memcpy( add_data, ssl->in_ctr, 8 );
1396 add_data[8] = ssl->in_msgtype;
1397 add_data[9] = ssl->major_ver;
1398 add_data[10] = ssl->minor_ver;
1399 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1400 add_data[12] = ssl->in_msglen & 0xFF;
1401
1402 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1403 add_data, 13 );
1404
1405 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1406 ssl->in_iv,
1407 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1408
1409 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1410 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001411 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001412
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001413 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001414 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001415 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001416 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1417 ssl->transform_in->iv_dec,
1418 ssl->transform_in->ivlen,
1419 add_data, 13,
1420 dec_msg, dec_msglen,
1421 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001422 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001423 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001424 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1425
1426 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1427 return( POLARSSL_ERR_SSL_INVALID_MAC );
1428
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 return( ret );
1430 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001431
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001432 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001433 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001434 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001435 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001436 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001437 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001438 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001439#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001440#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1441 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001442 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 {
Paul Bakker45829992013-01-03 14:52:21 +01001444 /*
1445 * Decrypt and check the padding
1446 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001447 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001448 unsigned char *dec_msg;
1449 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001450 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001451 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001452 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001453
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 /*
Paul Bakker45829992013-01-03 14:52:21 +01001455 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001456 */
Paul Bakker48916f92012-09-16 19:57:18 +00001457 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 {
1459 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001460 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001461 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001462 }
1463
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001464#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001465 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1466 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001467#endif
Paul Bakker45829992013-01-03 14:52:21 +01001468
1469 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1470 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1471 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001472 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1473 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1474 ssl->transform_in->ivlen,
1475 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001476 return( POLARSSL_ERR_SSL_INVALID_MAC );
1477 }
1478
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479 dec_msglen = ssl->in_msglen;
1480 dec_msg = ssl->in_msg;
1481 dec_msg_result = ssl->in_msg;
1482
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001483#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001484 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001485 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001486 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001487 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001488 {
Paul Bakker48916f92012-09-16 19:57:18 +00001489 dec_msglen -= ssl->transform_in->ivlen;
1490 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001491
Paul Bakker48916f92012-09-16 19:57:18 +00001492 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001493 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001494 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001495#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001496
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001497 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001498 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001499 ssl->transform_in->ivlen,
1500 dec_msg, dec_msglen,
1501 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001502 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001503 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001504 return( ret );
1505 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001506
Paul Bakkercca5b812013-08-31 17:40:26 +02001507 if( dec_msglen != olen )
1508 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001509 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001510 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001511 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001512
1513#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001514 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1515 {
1516 /*
1517 * Save IV in SSL3 and TLS1
1518 */
1519 memcpy( ssl->transform_in->iv_dec,
1520 ssl->transform_in->cipher_ctx_dec.iv,
1521 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001523#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001524
1525 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001526
1527 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1528 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001529#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001530 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1531 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001532#endif
Paul Bakker45829992013-01-03 14:52:21 +01001533 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001534 correct = 0;
1535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001536
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001537#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1539 {
Paul Bakker48916f92012-09-16 19:57:18 +00001540 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001541 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001542#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1544 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001545 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001546#endif
Paul Bakker45829992013-01-03 14:52:21 +01001547 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 }
1549 }
1550 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001551#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001552#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1553 defined(POLARSSL_SSL_PROTO_TLS1_2)
1554 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 {
1556 /*
Paul Bakker45829992013-01-03 14:52:21 +01001557 * TLSv1+: always check the padding up to the first failure
1558 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001559 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001560 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001561 size_t padding_idx = ssl->in_msglen - padlen - 1;
1562
Paul Bakker956c9e02013-12-19 14:42:28 +01001563 /*
1564 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001565 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001566 *
Paul Bakker61885c72014-04-25 12:59:03 +02001567 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1568 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001569 *
1570 * In both cases we reset padding_idx to a safe value (0) to
1571 * prevent out-of-buffer reads.
1572 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001573 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001574 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1575 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001576
1577 padding_idx *= correct;
1578
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001579 for( i = 1; i <= 256; i++ )
1580 {
1581 real_count &= ( i <= padlen );
1582 pad_count += real_count *
1583 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1584 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001585
1586 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001587
Paul Bakkerd66f0702013-01-31 16:57:45 +01001588#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001589 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001590 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001591#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001592 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001594 else
1595#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1596 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001597 {
1598 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001599 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001600 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001601 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001602 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001603#endif /* POLARSSL_CIPHER_MODE_CBC &&
1604 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001605 {
1606 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001607 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
1610 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1611 ssl->in_msg, ssl->in_msglen );
1612
1613 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001614 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001615 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001616#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1617 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1618 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001619 if( mode != POLARSSL_MODE_GCM &&
1620 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001621 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001622 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1623
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001624 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001626 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1627 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001629 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001631#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001632 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1633 {
1634 ssl_mac( &ssl->transform_in->md_ctx_dec,
1635 ssl->transform_in->mac_dec,
1636 ssl->in_msg, ssl->in_msglen,
1637 ssl->in_ctr, ssl->in_msgtype );
1638 }
1639 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001640#endif /* POLARSSL_SSL_PROTO_SSL3 */
1641#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001642 defined(POLARSSL_SSL_PROTO_TLS1_2)
1643 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1644 {
1645 /*
1646 * Process MAC and always update for padlen afterwards to make
1647 * total time independent of padlen
1648 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001649 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001650 *
1651 * Known timing attacks:
1652 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1653 *
1654 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1655 * correctly. (We round down instead of up, so -56 is the correct
1656 * value for our calculations instead of -55)
1657 */
1658 size_t j, extra_run = 0;
1659 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1660 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001661
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001663
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001664 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1665 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1666 ssl->in_msglen );
1667 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1668 ssl->in_msg + ssl->in_msglen );
1669 for( j = 0; j < extra_run; j++ )
1670 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001671
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1673 }
1674 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001675#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 POLARSSL_SSL_PROTO_TLS1_2 */
1677 {
1678 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001679 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001680 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1683 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1684 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001686 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001687 ssl->transform_in->maclen ) != 0 )
1688 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001689#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001690 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001691#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 correct = 0;
1693 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001695 /*
1696 * Finally check the correct flag
1697 */
1698 if( correct == 0 )
1699 return( POLARSSL_ERR_SSL_INVALID_MAC );
1700 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001701#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
1703 if( ssl->in_msglen == 0 )
1704 {
1705 ssl->nb_zero++;
1706
1707 /*
1708 * Three or more empty messages may be a DoS attack
1709 * (excessive CPU consumption).
1710 */
1711 if( ssl->nb_zero > 3 )
1712 {
1713 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1714 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001715 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 }
1717 }
1718 else
1719 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001720
Paul Bakker23986e52011-04-24 08:57:21 +00001721 for( i = 8; i > 0; i-- )
1722 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 break;
1724
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001725 /* The loops goes to its end iff the counter is wrapping */
1726 if( i == 0 )
1727 {
1728 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1729 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1730 }
1731
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1733
1734 return( 0 );
1735}
1736
Paul Bakker2770fbd2012-07-03 13:30:23 +00001737#if defined(POLARSSL_ZLIB_SUPPORT)
1738/*
1739 * Compression/decompression functions
1740 */
1741static int ssl_compress_buf( ssl_context *ssl )
1742{
1743 int ret;
1744 unsigned char *msg_post = ssl->out_msg;
1745 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001746 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001747
1748 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1749
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001750 if( len_pre == 0 )
1751 return( 0 );
1752
Paul Bakker2770fbd2012-07-03 13:30:23 +00001753 memcpy( msg_pre, ssl->out_msg, len_pre );
1754
1755 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1756 ssl->out_msglen ) );
1757
1758 SSL_DEBUG_BUF( 4, "before compression: output payload",
1759 ssl->out_msg, ssl->out_msglen );
1760
Paul Bakker48916f92012-09-16 19:57:18 +00001761 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1762 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1763 ssl->transform_out->ctx_deflate.next_out = msg_post;
1764 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001765
Paul Bakker48916f92012-09-16 19:57:18 +00001766 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001767 if( ret != Z_OK )
1768 {
1769 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1770 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1771 }
1772
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001773 ssl->out_msglen = SSL_BUFFER_LEN -
1774 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001775
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1777 ssl->out_msglen ) );
1778
1779 SSL_DEBUG_BUF( 4, "after compression: output payload",
1780 ssl->out_msg, ssl->out_msglen );
1781
1782 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1783
1784 return( 0 );
1785}
1786
1787static int ssl_decompress_buf( ssl_context *ssl )
1788{
1789 int ret;
1790 unsigned char *msg_post = ssl->in_msg;
1791 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001792 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001793
1794 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1795
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001796 if( len_pre == 0 )
1797 return( 0 );
1798
Paul Bakker2770fbd2012-07-03 13:30:23 +00001799 memcpy( msg_pre, ssl->in_msg, len_pre );
1800
1801 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1802 ssl->in_msglen ) );
1803
1804 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1805 ssl->in_msg, ssl->in_msglen );
1806
Paul Bakker48916f92012-09-16 19:57:18 +00001807 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1808 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1809 ssl->transform_in->ctx_inflate.next_out = msg_post;
1810 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001811
Paul Bakker48916f92012-09-16 19:57:18 +00001812 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813 if( ret != Z_OK )
1814 {
1815 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1816 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1817 }
1818
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001819 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1820 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001821
Paul Bakker2770fbd2012-07-03 13:30:23 +00001822 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1823 ssl->in_msglen ) );
1824
1825 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1826 ssl->in_msg, ssl->in_msglen );
1827
1828 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1829
1830 return( 0 );
1831}
1832#endif /* POLARSSL_ZLIB_SUPPORT */
1833
Paul Bakker5121ce52009-01-03 21:22:43 +00001834/*
1835 * Fill the input message buffer
1836 */
Paul Bakker23986e52011-04-24 08:57:21 +00001837int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001838{
Paul Bakker23986e52011-04-24 08:57:21 +00001839 int ret;
1840 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
1842 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1843
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001844 if( nb_want > SSL_BUFFER_LEN - 8 )
1845 {
1846 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1848 }
1849
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 while( ssl->in_left < nb_want )
1851 {
1852 len = nb_want - ssl->in_left;
1853 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1854
1855 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1856 ssl->in_left, nb_want ) );
1857 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1858
Paul Bakker831a7552011-05-18 13:32:51 +00001859 if( ret == 0 )
1860 return( POLARSSL_ERR_SSL_CONN_EOF );
1861
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 if( ret < 0 )
1863 return( ret );
1864
1865 ssl->in_left += ret;
1866 }
1867
1868 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1869
1870 return( 0 );
1871}
1872
1873/*
1874 * Flush any data not yet written
1875 */
1876int ssl_flush_output( ssl_context *ssl )
1877{
1878 int ret;
1879 unsigned char *buf;
1880
1881 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1882
1883 while( ssl->out_left > 0 )
1884 {
1885 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1886 5 + ssl->out_msglen, ssl->out_left ) );
1887
Paul Bakker5bd42292012-12-19 14:40:42 +01001888 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001890
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1892
1893 if( ret <= 0 )
1894 return( ret );
1895
1896 ssl->out_left -= ret;
1897 }
1898
1899 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1900
1901 return( 0 );
1902}
1903
1904/*
1905 * Record layer functions
1906 */
1907int ssl_write_record( ssl_context *ssl )
1908{
Paul Bakker05ef8352012-05-08 09:17:57 +00001909 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001910 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
1912 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1913
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1915 {
1916 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1917 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1918 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1919
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001920 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1921 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001922 }
1923
Paul Bakker2770fbd2012-07-03 13:30:23 +00001924#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001925 if( ssl->transform_out != NULL &&
1926 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927 {
1928 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1929 {
1930 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1931 return( ret );
1932 }
1933
1934 len = ssl->out_msglen;
1935 }
1936#endif /*POLARSSL_ZLIB_SUPPORT */
1937
Paul Bakker05ef8352012-05-08 09:17:57 +00001938#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001939 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001941 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Paul Bakker05ef8352012-05-08 09:17:57 +00001943 ret = ssl_hw_record_write( ssl );
1944 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1945 {
1946 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001947 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001948 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001949
1950 if( ret == 0 )
1951 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001952 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001953#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001954 if( !done )
1955 {
1956 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1957 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1958 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1960 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001961
Paul Bakker48916f92012-09-16 19:57:18 +00001962 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001963 {
1964 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1965 {
1966 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1967 return( ret );
1968 }
1969
1970 len = ssl->out_msglen;
1971 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1972 ssl->out_hdr[4] = (unsigned char)( len );
1973 }
1974
1975 ssl->out_left = 5 + ssl->out_msglen;
1976
1977 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1978 "version = [%d:%d], msglen = %d",
1979 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1980 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1981
1982 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001983 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 }
1985
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1987 {
1988 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1989 return( ret );
1990 }
1991
1992 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1993
1994 return( 0 );
1995}
1996
1997int ssl_read_record( ssl_context *ssl )
1998{
Paul Bakker05ef8352012-05-08 09:17:57 +00001999 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002000
2001 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2002
2003 if( ssl->in_hslen != 0 &&
2004 ssl->in_hslen < ssl->in_msglen )
2005 {
2006 /*
2007 * Get next Handshake message in the current record
2008 */
2009 ssl->in_msglen -= ssl->in_hslen;
2010
Paul Bakker8934a982011-08-05 11:11:53 +00002011 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002012 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002013
2014 ssl->in_hslen = 4;
2015 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2016
2017 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2018 " %d, type = %d, hslen = %d",
2019 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2020
2021 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2022 {
2023 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002024 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 }
2026
2027 if( ssl->in_msglen < ssl->in_hslen )
2028 {
2029 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002030 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031 }
2032
Paul Bakker4224bc02014-04-08 14:36:50 +02002033 if( ssl->state != SSL_HANDSHAKE_OVER )
2034 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002035
2036 return( 0 );
2037 }
2038
2039 ssl->in_hslen = 0;
2040
2041 /*
2042 * Read the record header and validate it
2043 */
2044 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2045 {
2046 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2047 return( ret );
2048 }
2049
2050 ssl->in_msgtype = ssl->in_hdr[0];
2051 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2052
2053 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2054 "version = [%d:%d], msglen = %d",
2055 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2056 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2057
2058 if( ssl->in_hdr[1] != ssl->major_ver )
2059 {
2060 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002061 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 }
2063
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002064 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 {
2066 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002067 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 }
2069
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002070 /* Sanity check (outer boundaries) */
2071 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2072 {
2073 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2074 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2075 }
2076
Paul Bakker5121ce52009-01-03 21:22:43 +00002077 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002078 * Make sure the message length is acceptable for the current transform
2079 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 */
Paul Bakker48916f92012-09-16 19:57:18 +00002081 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002083 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 {
2085 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002086 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 }
2088 }
2089 else
2090 {
Paul Bakker48916f92012-09-16 19:57:18 +00002091 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 {
2093 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002094 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002097#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002098 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002099 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
2101 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002102 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002106#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2107 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002108 /*
2109 * TLS encrypted messages can have up to 256 bytes of padding
2110 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002111 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002112 ssl->in_msglen > ssl->transform_in->minlen +
2113 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
2115 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002116 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002117 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002119 }
2120
2121 /*
2122 * Read and optionally decrypt the message contents
2123 */
2124 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2125 {
2126 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2127 return( ret );
2128 }
2129
2130 SSL_DEBUG_BUF( 4, "input record from network",
2131 ssl->in_hdr, 5 + ssl->in_msglen );
2132
Paul Bakker05ef8352012-05-08 09:17:57 +00002133#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002134 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002135 {
2136 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2137
2138 ret = ssl_hw_record_read( ssl );
2139 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2140 {
2141 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002142 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002143 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002144
2145 if( ret == 0 )
2146 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002147 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002148#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002149 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002150 {
2151 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2152 {
Paul Bakker40865c82013-01-31 17:13:13 +01002153#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2154 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2155 {
2156 ssl_send_alert_message( ssl,
2157 SSL_ALERT_LEVEL_FATAL,
2158 SSL_ALERT_MSG_BAD_RECORD_MAC );
2159 }
2160#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002161 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2162 return( ret );
2163 }
2164
2165 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2166 ssl->in_msg, ssl->in_msglen );
2167
2168 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2169 {
2170 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002171 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002172 }
2173 }
2174
Paul Bakker2770fbd2012-07-03 13:30:23 +00002175#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002176 if( ssl->transform_in != NULL &&
2177 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002178 {
2179 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2180 {
2181 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2182 return( ret );
2183 }
2184
2185 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2186 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2187 }
2188#endif /* POLARSSL_ZLIB_SUPPORT */
2189
Paul Bakker0a925182012-04-16 06:46:41 +00002190 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2191 ssl->in_msgtype != SSL_MSG_ALERT &&
2192 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2193 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2194 {
2195 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2196
Paul Bakker48916f92012-09-16 19:57:18 +00002197 if( ( ret = ssl_send_alert_message( ssl,
2198 SSL_ALERT_LEVEL_FATAL,
2199 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002200 {
2201 return( ret );
2202 }
2203
2204 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2205 }
2206
Paul Bakker5121ce52009-01-03 21:22:43 +00002207 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2208 {
2209 ssl->in_hslen = 4;
2210 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2211
2212 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2213 " %d, type = %d, hslen = %d",
2214 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2215
2216 /*
2217 * Additional checks to validate the handshake header
2218 */
2219 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2220 {
2221 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002222 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 }
2224
2225 if( ssl->in_msglen < ssl->in_hslen )
2226 {
2227 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002228 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230
Paul Bakker48916f92012-09-16 19:57:18 +00002231 if( ssl->state != SSL_HANDSHAKE_OVER )
2232 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 }
2234
2235 if( ssl->in_msgtype == SSL_MSG_ALERT )
2236 {
2237 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2238 ssl->in_msg[0], ssl->in_msg[1] ) );
2239
2240 /*
2241 * Ignore non-fatal alerts, except close_notify
2242 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002243 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002245 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2246 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002247 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 }
2249
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002250 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2251 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002252 {
2253 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002254 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 }
2256 }
2257
2258 ssl->in_left = 0;
2259
2260 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2261
2262 return( 0 );
2263}
2264
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002265int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2266{
2267 int ret;
2268
2269 if( ( ret = ssl_send_alert_message( ssl,
2270 SSL_ALERT_LEVEL_FATAL,
2271 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2272 {
2273 return( ret );
2274 }
2275
2276 return( 0 );
2277}
2278
Paul Bakker0a925182012-04-16 06:46:41 +00002279int ssl_send_alert_message( ssl_context *ssl,
2280 unsigned char level,
2281 unsigned char message )
2282{
2283 int ret;
2284
2285 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2286
2287 ssl->out_msgtype = SSL_MSG_ALERT;
2288 ssl->out_msglen = 2;
2289 ssl->out_msg[0] = level;
2290 ssl->out_msg[1] = message;
2291
2292 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2293 {
2294 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2295 return( ret );
2296 }
2297
2298 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2299
2300 return( 0 );
2301}
2302
Paul Bakker5121ce52009-01-03 21:22:43 +00002303/*
2304 * Handshake functions
2305 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002306#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2307 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2308 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2309 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2310 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2311 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2312 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002313int ssl_write_certificate( ssl_context *ssl )
2314{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002315 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002316
2317 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2318
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002320 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2321 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002322 {
2323 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2324 ssl->state++;
2325 return( 0 );
2326 }
2327
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002328 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2329 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330}
2331
2332int ssl_parse_certificate( ssl_context *ssl )
2333{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002334 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2335
2336 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2337
2338 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002341 {
2342 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2343 ssl->state++;
2344 return( 0 );
2345 }
2346
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002347 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2348 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349}
2350#else
2351int ssl_write_certificate( ssl_context *ssl )
2352{
2353 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2354 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002355 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2357
2358 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2359
2360 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002361 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2362 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002363 {
2364 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2365 ssl->state++;
2366 return( 0 );
2367 }
2368
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002369#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 if( ssl->endpoint == SSL_IS_CLIENT )
2371 {
2372 if( ssl->client_auth == 0 )
2373 {
2374 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2375 ssl->state++;
2376 return( 0 );
2377 }
2378
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002379#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 /*
2381 * If using SSLv3 and got no cert, send an Alert message
2382 * (otherwise an empty Certificate message will be sent).
2383 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002384 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2386 {
2387 ssl->out_msglen = 2;
2388 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002389 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2390 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002391
2392 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2393 goto write_msg;
2394 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002395#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002397#endif /* POLARSSL_SSL_CLI_C */
2398#if defined(POLARSSL_SSL_SRV_C)
2399 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002401 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 {
2403 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002404 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002407#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002409 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411 /*
2412 * 0 . 0 handshake type
2413 * 1 . 3 handshake length
2414 * 4 . 6 length of all certs
2415 * 7 . 9 length of cert. 1
2416 * 10 . n-1 peer certificate
2417 * n . n+2 length of cert. 2
2418 * n+3 . ... upper level cert, etc.
2419 */
2420 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002421 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Paul Bakker29087132010-03-21 21:03:34 +00002423 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002424 {
2425 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002426 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 {
2428 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2429 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002430 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 }
2432
2433 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2434 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2435 ssl->out_msg[i + 2] = (unsigned char)( n );
2436
2437 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2438 i += n; crt = crt->next;
2439 }
2440
2441 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2442 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2443 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2444
2445 ssl->out_msglen = i;
2446 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2447 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2448
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002449#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002450write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002451#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
2453 ssl->state++;
2454
2455 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2456 {
2457 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2458 return( ret );
2459 }
2460
2461 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2462
Paul Bakkered27a042013-04-18 22:46:23 +02002463 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464}
2465
2466int ssl_parse_certificate( ssl_context *ssl )
2467{
Paul Bakkered27a042013-04-18 22:46:23 +02002468 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002469 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002470 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002471
2472 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2473
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002474 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002475 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2476 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002477 {
2478 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2479 ssl->state++;
2480 return( 0 );
2481 }
2482
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002483#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002485 ( ssl->authmode == SSL_VERIFY_NONE ||
2486 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002488 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2490 ssl->state++;
2491 return( 0 );
2492 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002493#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002494
2495 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2496 {
2497 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2498 return( ret );
2499 }
2500
2501 ssl->state++;
2502
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002503#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002504#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 /*
2506 * Check if the client sent an empty certificate
2507 */
2508 if( ssl->endpoint == SSL_IS_SERVER &&
2509 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2510 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002511 if( ssl->in_msglen == 2 &&
2512 ssl->in_msgtype == SSL_MSG_ALERT &&
2513 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2514 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002515 {
2516 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2517
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002518 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2520 return( 0 );
2521 else
Paul Bakker40e46942009-01-03 21:51:57 +00002522 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
2524 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002525#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002526
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002527#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2528 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 if( ssl->endpoint == SSL_IS_SERVER &&
2530 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2531 {
2532 if( ssl->in_hslen == 7 &&
2533 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2534 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2535 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2536 {
2537 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2538
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002539 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002541 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 else
2543 return( 0 );
2544 }
2545 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002546#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2547 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002548#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002549
2550 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2551 {
2552 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002553 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 }
2555
2556 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2557 {
2558 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002559 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
2561
2562 /*
2563 * Same message structure as in ssl_write_certificate()
2564 */
2565 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2566
2567 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2568 {
2569 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002570 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002571 }
2572
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002573 /* In case we tried to reuse a session but it failed */
2574 if( ssl->session_negotiate->peer_cert != NULL )
2575 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002576 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002577 polarssl_free( ssl->session_negotiate->peer_cert );
2578 }
2579
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002580 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2581 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002582 {
2583 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002584 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002585 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
Paul Bakkerb6b09562013-09-18 14:17:41 +02002588 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002589
2590 i = 7;
2591
2592 while( i < ssl->in_hslen )
2593 {
2594 if( ssl->in_msg[i] != 0 )
2595 {
2596 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002597 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599
2600 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2601 | (unsigned int) ssl->in_msg[i + 2];
2602 i += 3;
2603
2604 if( n < 128 || i + n > ssl->in_hslen )
2605 {
2606 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002607 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
2609
Paul Bakkerddf26b42013-09-18 13:46:23 +02002610 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2611 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002612 if( ret != 0 )
2613 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002614 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 return( ret );
2616 }
2617
2618 i += n;
2619 }
2620
Paul Bakker48916f92012-09-16 19:57:18 +00002621 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002622
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002623 /*
2624 * On client, make sure the server cert doesn't change during renego to
2625 * avoid "triple handshake" attack: https://secure-resumption.com/
2626 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002627#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002628 if( ssl->endpoint == SSL_IS_CLIENT &&
2629 ssl->renegotiation == SSL_RENEGOTIATION )
2630 {
2631 if( ssl->session->peer_cert == NULL )
2632 {
2633 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2634 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2635 }
2636
2637 if( ssl->session->peer_cert->raw.len !=
2638 ssl->session_negotiate->peer_cert->raw.len ||
2639 memcmp( ssl->session->peer_cert->raw.p,
2640 ssl->session_negotiate->peer_cert->raw.p,
2641 ssl->session->peer_cert->raw.len ) != 0 )
2642 {
2643 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2645 }
2646 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002647#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002648
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 if( ssl->authmode != SSL_VERIFY_NONE )
2650 {
2651 if( ssl->ca_chain == NULL )
2652 {
2653 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002654 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002655 }
2656
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002657 /*
2658 * Main check: verify certificate
2659 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002660 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2661 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2662 &ssl->session_negotiate->verify_result,
2663 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
2665 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002666 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002668 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002669
2670 /*
2671 * Secondary checks: always done, but change 'ret' only if it was 0
2672 */
2673
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002674#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002675 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002676 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002677
2678 /* If certificate uses an EC key, make sure the curve is OK */
2679 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2680 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2681 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002682 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002683 if( ret == 0 )
2684 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002685 }
2686 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002687#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002688
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002689 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2690 ciphersuite_info,
2691 ! ssl->endpoint ) != 0 )
2692 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002693 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002694 if( ret == 0 )
2695 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2696 }
2697
Paul Bakker5121ce52009-01-03 21:22:43 +00002698 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2699 ret = 0;
2700 }
2701
2702 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2703
2704 return( ret );
2705}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002706#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2707 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2708 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2709 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2710 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2711 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2712 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002713
2714int ssl_write_change_cipher_spec( ssl_context *ssl )
2715{
2716 int ret;
2717
2718 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2719
2720 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2721 ssl->out_msglen = 1;
2722 ssl->out_msg[0] = 1;
2723
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 ssl->state++;
2725
2726 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2727 {
2728 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2729 return( ret );
2730 }
2731
2732 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2733
2734 return( 0 );
2735}
2736
2737int ssl_parse_change_cipher_spec( ssl_context *ssl )
2738{
2739 int ret;
2740
2741 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2742
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2744 {
2745 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2746 return( ret );
2747 }
2748
2749 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2750 {
2751 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002752 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 }
2754
2755 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2756 {
2757 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002758 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
2761 ssl->state++;
2762
2763 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2764
2765 return( 0 );
2766}
2767
Paul Bakker41c83d32013-03-20 14:39:14 +01002768void ssl_optimize_checksum( ssl_context *ssl,
2769 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002770{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002771 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002772
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002773#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2774 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002775 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002776 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002777 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002778#endif
2779#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2780#if defined(POLARSSL_SHA512_C)
2781 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2782 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2783 else
2784#endif
2785#if defined(POLARSSL_SHA256_C)
2786 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002787 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788 else
2789#endif
2790#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002791 {
2792 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002793 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002794 }
Paul Bakker380da532012-04-18 16:10:25 +00002795}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002796
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002797static void ssl_update_checksum_start( ssl_context *ssl,
2798 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002799{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002800#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2801 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002802 md5_update( &ssl->handshake->fin_md5 , buf, len );
2803 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002804#endif
2805#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2806#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002807 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002808#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002809#if defined(POLARSSL_SHA512_C)
2810 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002811#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002813}
2814
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002815#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2816 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002817static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2818 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002819{
Paul Bakker48916f92012-09-16 19:57:18 +00002820 md5_update( &ssl->handshake->fin_md5 , buf, len );
2821 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002822}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002823#endif
Paul Bakker380da532012-04-18 16:10:25 +00002824
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002825#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2826#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002827static void ssl_update_checksum_sha256( ssl_context *ssl,
2828 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002829{
Paul Bakker9e36f042013-06-30 14:34:05 +02002830 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002831}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002832#endif
Paul Bakker380da532012-04-18 16:10:25 +00002833
Paul Bakker9e36f042013-06-30 14:34:05 +02002834#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002835static void ssl_update_checksum_sha384( ssl_context *ssl,
2836 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002837{
Paul Bakker9e36f042013-06-30 14:34:05 +02002838 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002839}
Paul Bakker769075d2012-11-24 11:26:46 +01002840#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002841#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002842
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002843#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002844static void ssl_calc_finished_ssl(
2845 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002846{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002847 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002848 md5_context md5;
2849 sha1_context sha1;
2850
Paul Bakker5121ce52009-01-03 21:22:43 +00002851 unsigned char padbuf[48];
2852 unsigned char md5sum[16];
2853 unsigned char sha1sum[20];
2854
Paul Bakker48916f92012-09-16 19:57:18 +00002855 ssl_session *session = ssl->session_negotiate;
2856 if( !session )
2857 session = ssl->session;
2858
Paul Bakker1ef83d62012-04-11 12:09:53 +00002859 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2860
Paul Bakker48916f92012-09-16 19:57:18 +00002861 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2862 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
2864 /*
2865 * SSLv3:
2866 * hash =
2867 * MD5( master + pad2 +
2868 * MD5( handshake + sender + master + pad1 ) )
2869 * + SHA1( master + pad2 +
2870 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 */
2872
Paul Bakker90995b52013-06-24 19:20:35 +02002873#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002875 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002876#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakker90995b52013-06-24 19:20:35 +02002878#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002880 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002881#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Paul Bakker3c2122f2013-06-24 19:03:14 +02002883 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2884 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker3c2122f2013-06-24 19:03:14 +02002888 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002889 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 md5_update( &md5, padbuf, 48 );
2891 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Paul Bakker3c2122f2013-06-24 19:03:14 +02002893 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002894 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002895 sha1_update( &sha1, padbuf, 40 );
2896 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002901 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902 md5_update( &md5, padbuf, 48 );
2903 md5_update( &md5, md5sum, 16 );
2904 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker1ef83d62012-04-11 12:09:53 +00002906 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002907 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908 sha1_update( &sha1, padbuf , 40 );
2909 sha1_update( &sha1, sha1sum, 20 );
2910 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002911
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Paul Bakker5b4af392014-06-26 12:09:34 +02002914 md5_free( &md5 );
2915 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakker34617722014-06-13 17:20:13 +02002917 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2918 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2919 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
2921 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2922}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002923#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002924
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002925#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002926static void ssl_calc_finished_tls(
2927 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002928{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002929 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002930 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002931 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002933 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Paul Bakker48916f92012-09-16 19:57:18 +00002935 ssl_session *session = ssl->session_negotiate;
2936 if( !session )
2937 session = ssl->session;
2938
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002940
Paul Bakker48916f92012-09-16 19:57:18 +00002941 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2942 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Paul Bakker1ef83d62012-04-11 12:09:53 +00002944 /*
2945 * TLSv1:
2946 * hash = PRF( master, finished_label,
2947 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2948 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakker90995b52013-06-24 19:20:35 +02002950#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002951 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2952 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002953#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954
Paul Bakker90995b52013-06-24 19:20:35 +02002955#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2957 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002958#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002959
2960 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002961 ? "client finished"
2962 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002963
2964 md5_finish( &md5, padbuf );
2965 sha1_finish( &sha1, padbuf + 16 );
2966
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002967 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002968 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969
2970 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2971
Paul Bakker5b4af392014-06-26 12:09:34 +02002972 md5_free( &md5 );
2973 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974
Paul Bakker34617722014-06-13 17:20:13 +02002975 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002976
2977 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2978}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002979#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002980
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002981#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2982#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002983static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984 ssl_context *ssl, unsigned char *buf, int from )
2985{
2986 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002987 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002988 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989 unsigned char padbuf[32];
2990
Paul Bakker48916f92012-09-16 19:57:18 +00002991 ssl_session *session = ssl->session_negotiate;
2992 if( !session )
2993 session = ssl->session;
2994
Paul Bakker380da532012-04-18 16:10:25 +00002995 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002996
Paul Bakker9e36f042013-06-30 14:34:05 +02002997 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998
2999 /*
3000 * TLSv1.2:
3001 * hash = PRF( master, finished_label,
3002 * Hash( handshake ) )[0.11]
3003 */
3004
Paul Bakker9e36f042013-06-30 14:34:05 +02003005#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003007 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003008#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003009
3010 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003011 ? "client finished"
3012 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003013
Paul Bakker9e36f042013-06-30 14:34:05 +02003014 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003016 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003017 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003018
3019 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3020
Paul Bakker5b4af392014-06-26 12:09:34 +02003021 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022
Paul Bakker34617722014-06-13 17:20:13 +02003023 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024
3025 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3026}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003027#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028
Paul Bakker9e36f042013-06-30 14:34:05 +02003029#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003030static void ssl_calc_finished_tls_sha384(
3031 ssl_context *ssl, unsigned char *buf, int from )
3032{
3033 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003034 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003035 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036 unsigned char padbuf[48];
3037
Paul Bakker48916f92012-09-16 19:57:18 +00003038 ssl_session *session = ssl->session_negotiate;
3039 if( !session )
3040 session = ssl->session;
3041
Paul Bakker380da532012-04-18 16:10:25 +00003042 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003043
Paul Bakker9e36f042013-06-30 14:34:05 +02003044 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003045
3046 /*
3047 * TLSv1.2:
3048 * hash = PRF( master, finished_label,
3049 * Hash( handshake ) )[0.11]
3050 */
3051
Paul Bakker9e36f042013-06-30 14:34:05 +02003052#if !defined(POLARSSL_SHA512_ALT)
3053 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3054 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003055#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003056
3057 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003058 ? "client finished"
3059 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003060
Paul Bakker9e36f042013-06-30 14:34:05 +02003061 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003062
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003063 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003064 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003065
3066 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3067
Paul Bakker5b4af392014-06-26 12:09:34 +02003068 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003069
Paul Bakker34617722014-06-13 17:20:13 +02003070 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003071
3072 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3073}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003074#endif /* POLARSSL_SHA512_C */
3075#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003076
Paul Bakker48916f92012-09-16 19:57:18 +00003077void ssl_handshake_wrapup( ssl_context *ssl )
3078{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003079 int resume = ssl->handshake->resume;
3080
Paul Bakker48916f92012-09-16 19:57:18 +00003081 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3082
3083 /*
3084 * Free our handshake params
3085 */
3086 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003087 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003088 ssl->handshake = NULL;
3089
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003090 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003091 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003092 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003093 ssl->renego_records_seen = 0;
3094 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003095
Paul Bakker48916f92012-09-16 19:57:18 +00003096 /*
3097 * Switch in our now active transform context
3098 */
3099 if( ssl->transform )
3100 {
3101 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003102 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003103 }
3104 ssl->transform = ssl->transform_negotiate;
3105 ssl->transform_negotiate = NULL;
3106
Paul Bakker0a597072012-09-25 21:55:46 +00003107 if( ssl->session )
3108 {
3109 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003110 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003111 }
3112 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003113 ssl->session_negotiate = NULL;
3114
Paul Bakker0a597072012-09-25 21:55:46 +00003115 /*
3116 * Add cache entry
3117 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003118 if( ssl->f_set_cache != NULL &&
3119 ssl->session->length != 0 &&
3120 resume == 0 )
3121 {
Paul Bakker0a597072012-09-25 21:55:46 +00003122 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3123 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003124 }
Paul Bakker0a597072012-09-25 21:55:46 +00003125
Paul Bakker48916f92012-09-16 19:57:18 +00003126 ssl->state++;
3127
3128 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3129}
3130
Paul Bakker1ef83d62012-04-11 12:09:53 +00003131int ssl_write_finished( ssl_context *ssl )
3132{
3133 int ret, hash_len;
3134
3135 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3136
Paul Bakker92be97b2013-01-02 17:30:03 +01003137 /*
3138 * Set the out_msg pointer to the correct location based on IV length
3139 */
3140 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3141 {
3142 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3143 ssl->transform_negotiate->fixed_ivlen;
3144 }
3145 else
3146 ssl->out_msg = ssl->out_iv;
3147
Paul Bakker48916f92012-09-16 19:57:18 +00003148 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
3150 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3152
Paul Bakker48916f92012-09-16 19:57:18 +00003153 ssl->verify_data_len = hash_len;
3154 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3155
Paul Bakker5121ce52009-01-03 21:22:43 +00003156 ssl->out_msglen = 4 + hash_len;
3157 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3158 ssl->out_msg[0] = SSL_HS_FINISHED;
3159
3160 /*
3161 * In case of session resuming, invert the client and server
3162 * ChangeCipherSpec messages order.
3163 */
Paul Bakker0a597072012-09-25 21:55:46 +00003164 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003165 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003166#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003168 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003169#endif
3170#if defined(POLARSSL_SSL_SRV_C)
3171 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003172 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003173#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003174 }
3175 else
3176 ssl->state++;
3177
Paul Bakker48916f92012-09-16 19:57:18 +00003178 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003179 * Switch to our negotiated transform and session parameters for outbound
3180 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003181 */
3182 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3183 ssl->transform_out = ssl->transform_negotiate;
3184 ssl->session_out = ssl->session_negotiate;
3185 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003186
Paul Bakker07eb38b2012-12-19 14:42:06 +01003187#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003188 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003189 {
3190 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3191 {
3192 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3193 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3194 }
3195 }
3196#endif
3197
Paul Bakker5121ce52009-01-03 21:22:43 +00003198 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3199 {
3200 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3201 return( ret );
3202 }
3203
3204 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3205
3206 return( 0 );
3207}
3208
3209int ssl_parse_finished( ssl_context *ssl )
3210{
Paul Bakker23986e52011-04-24 08:57:21 +00003211 int ret;
3212 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003213 unsigned char buf[36];
3214
3215 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3216
Paul Bakker48916f92012-09-16 19:57:18 +00003217 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003218
Paul Bakker48916f92012-09-16 19:57:18 +00003219 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003220 * Switch to our negotiated transform and session parameters for inbound
3221 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003222 */
3223 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3224 ssl->transform_in = ssl->transform_negotiate;
3225 ssl->session_in = ssl->session_negotiate;
3226 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003227
Paul Bakker92be97b2013-01-02 17:30:03 +01003228 /*
3229 * Set the in_msg pointer to the correct location based on IV length
3230 */
3231 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3232 {
3233 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3234 ssl->transform_negotiate->fixed_ivlen;
3235 }
3236 else
3237 ssl->in_msg = ssl->in_iv;
3238
Paul Bakker07eb38b2012-12-19 14:42:06 +01003239#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003240 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003241 {
3242 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3243 {
3244 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3245 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3246 }
3247 }
3248#endif
3249
Paul Bakker5121ce52009-01-03 21:22:43 +00003250 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3251 {
3252 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3253 return( ret );
3254 }
3255
3256 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3257 {
3258 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003259 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
3261
Paul Bakker1ef83d62012-04-11 12:09:53 +00003262 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003263 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3264
3265 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3266 ssl->in_hslen != 4 + hash_len )
3267 {
3268 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003269 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
3271
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003272 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003273 {
3274 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003275 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Paul Bakker48916f92012-09-16 19:57:18 +00003278 ssl->verify_data_len = hash_len;
3279 memcpy( ssl->peer_verify_data, buf, hash_len );
3280
Paul Bakker0a597072012-09-25 21:55:46 +00003281 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003283#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 if( ssl->endpoint == SSL_IS_CLIENT )
3285 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003286#endif
3287#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003289 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 }
3292 else
3293 ssl->state++;
3294
3295 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3296
3297 return( 0 );
3298}
3299
Paul Bakker968afaa2014-07-09 11:09:24 +02003300static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003301{
3302 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3303
3304#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3305 defined(POLARSSL_SSL_PROTO_TLS1_1)
3306 md5_init( &handshake->fin_md5 );
3307 sha1_init( &handshake->fin_sha1 );
3308 md5_starts( &handshake->fin_md5 );
3309 sha1_starts( &handshake->fin_sha1 );
3310#endif
3311#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3312#if defined(POLARSSL_SHA256_C)
3313 sha256_init( &handshake->fin_sha256 );
3314 sha256_starts( &handshake->fin_sha256, 0 );
3315#endif
3316#if defined(POLARSSL_SHA512_C)
3317 sha512_init( &handshake->fin_sha512 );
3318 sha512_starts( &handshake->fin_sha512, 1 );
3319#endif
3320#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3321
3322 handshake->update_checksum = ssl_update_checksum_start;
3323 handshake->sig_alg = SSL_HASH_SHA1;
3324
3325#if defined(POLARSSL_DHM_C)
3326 dhm_init( &handshake->dhm_ctx );
3327#endif
3328#if defined(POLARSSL_ECDH_C)
3329 ecdh_init( &handshake->ecdh_ctx );
3330#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003331}
3332
3333static void ssl_transform_init( ssl_transform *transform )
3334{
3335 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003336
3337 cipher_init( &transform->cipher_ctx_enc );
3338 cipher_init( &transform->cipher_ctx_dec );
3339
3340 md_init( &transform->md_ctx_enc );
3341 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003342}
3343
3344void ssl_session_init( ssl_session *session )
3345{
3346 memset( session, 0, sizeof(ssl_session) );
3347}
3348
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003349static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003350{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003351 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003352 if( ssl->transform_negotiate )
3353 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003354 if( ssl->session_negotiate )
3355 ssl_session_free( ssl->session_negotiate );
3356 if( ssl->handshake )
3357 ssl_handshake_free( ssl->handshake );
3358
3359 /*
3360 * Either the pointers are now NULL or cleared properly and can be freed.
3361 * Now allocate missing structures.
3362 */
3363 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003364 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003365 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
3366 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003367 }
Paul Bakker48916f92012-09-16 19:57:18 +00003368
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003369 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003370 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003371 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
3372 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003373 }
Paul Bakker48916f92012-09-16 19:57:18 +00003374
Paul Bakker82788fb2014-10-20 13:59:19 +02003375 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003376 {
3377 ssl->handshake = (ssl_handshake_params *)
3378 polarssl_malloc( sizeof(ssl_handshake_params) );
3379 }
Paul Bakker48916f92012-09-16 19:57:18 +00003380
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003381 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003382 if( ssl->handshake == NULL ||
3383 ssl->transform_negotiate == NULL ||
3384 ssl->session_negotiate == NULL )
3385 {
3386 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003387
3388 polarssl_free( ssl->handshake );
3389 polarssl_free( ssl->transform_negotiate );
3390 polarssl_free( ssl->session_negotiate );
3391
3392 ssl->handshake = NULL;
3393 ssl->transform_negotiate = NULL;
3394 ssl->session_negotiate = NULL;
3395
Paul Bakker48916f92012-09-16 19:57:18 +00003396 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3397 }
3398
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003399 /* Initialize structures */
3400 ssl_session_init( ssl->session_negotiate );
3401 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003402 ssl_handshake_params_init( ssl->handshake );
3403
3404#if defined(POLARSSL_X509_CRT_PARSE_C)
3405 ssl->handshake->key_cert = ssl->key_cert;
3406#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003407
Paul Bakker48916f92012-09-16 19:57:18 +00003408 return( 0 );
3409}
3410
Paul Bakker5121ce52009-01-03 21:22:43 +00003411/*
3412 * Initialize an SSL context
3413 */
3414int ssl_init( ssl_context *ssl )
3415{
Paul Bakker48916f92012-09-16 19:57:18 +00003416 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 int len = SSL_BUFFER_LEN;
3418
3419 memset( ssl, 0, sizeof( ssl_context ) );
3420
Paul Bakker62f2dee2012-09-28 07:31:51 +00003421 /*
3422 * Sane defaults
3423 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003424 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3425 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3426 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3427 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003428
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003429 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003430
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003431 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3432
Paul Bakker62f2dee2012-09-28 07:31:51 +00003433#if defined(POLARSSL_DHM_C)
3434 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3435 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3436 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3437 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3438 {
3439 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3440 return( ret );
3441 }
3442#endif
3443
3444 /*
3445 * Prepare base structures
3446 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003447 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003449 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003450 ssl->in_msg = ssl->in_ctr + 13;
3451
3452 if( ssl->in_ctr == NULL )
3453 {
3454 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003455 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 }
3457
Paul Bakker6e339b52013-07-03 13:37:05 +02003458 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003460 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003461 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003462
3463 if( ssl->out_ctr == NULL )
3464 {
3465 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003466 polarssl_free( ssl->in_ctr );
3467 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003468 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 }
3470
3471 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3472 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3473
Paul Bakker606b4ba2013-08-14 16:52:14 +02003474#if defined(POLARSSL_SSL_SESSION_TICKETS)
3475 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3476#endif
3477
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003478#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003479 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003480#endif
3481
Paul Bakker48916f92012-09-16 19:57:18 +00003482 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3483 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003484
3485 return( 0 );
3486}
3487
3488/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003489 * Reset an initialized and used SSL context for re-use while retaining
3490 * all application-set variables, function pointers and data.
3491 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003492int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003493{
Paul Bakker48916f92012-09-16 19:57:18 +00003494 int ret;
3495
Paul Bakker7eb013f2011-10-06 12:37:39 +00003496 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003497 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3498 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3499
3500 ssl->verify_data_len = 0;
3501 memset( ssl->own_verify_data, 0, 36 );
3502 memset( ssl->peer_verify_data, 0, 36 );
3503
Paul Bakker7eb013f2011-10-06 12:37:39 +00003504 ssl->in_offt = NULL;
3505
Paul Bakker92be97b2013-01-02 17:30:03 +01003506 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003507 ssl->in_msgtype = 0;
3508 ssl->in_msglen = 0;
3509 ssl->in_left = 0;
3510
3511 ssl->in_hslen = 0;
3512 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003513 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003514
Paul Bakker92be97b2013-01-02 17:30:03 +01003515 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003516 ssl->out_msgtype = 0;
3517 ssl->out_msglen = 0;
3518 ssl->out_left = 0;
3519
Paul Bakker48916f92012-09-16 19:57:18 +00003520 ssl->transform_in = NULL;
3521 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003522
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003523 ssl->renego_records_seen = 0;
3524
Paul Bakker7eb013f2011-10-06 12:37:39 +00003525 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3526 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003527
3528#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003529 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003530 {
3531 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003532 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003533 {
3534 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3535 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3536 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003537 }
3538#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539
Paul Bakker48916f92012-09-16 19:57:18 +00003540 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003541 {
Paul Bakker48916f92012-09-16 19:57:18 +00003542 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003543 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003544 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003545 }
Paul Bakker48916f92012-09-16 19:57:18 +00003546
Paul Bakkerc0463502013-02-14 11:19:38 +01003547 if( ssl->session )
3548 {
3549 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003550 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003551 ssl->session = NULL;
3552 }
3553
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003554#if defined(POLARSSL_SSL_ALPN)
3555 ssl->alpn_chosen = NULL;
3556#endif
3557
Paul Bakker48916f92012-09-16 19:57:18 +00003558 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3559 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003560
3561 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003562}
3563
Paul Bakkera503a632013-08-14 13:48:06 +02003564#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003565static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3566{
3567 aes_free( &tkeys->enc );
3568 aes_free( &tkeys->dec );
3569
3570 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3571}
3572
Paul Bakker7eb013f2011-10-06 12:37:39 +00003573/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003574 * Allocate and initialize ticket keys
3575 */
3576static int ssl_ticket_keys_init( ssl_context *ssl )
3577{
3578 int ret;
3579 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003580 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003581
3582 if( ssl->ticket_keys != NULL )
3583 return( 0 );
3584
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003585 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3586 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003587 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3588
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003589 aes_init( &tkeys->enc );
3590 aes_init( &tkeys->dec );
3591
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003592 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003593 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003594 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003595 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003596 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003597 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003598
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003599 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3600 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3601 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3602 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003603 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003604 polarssl_free( tkeys );
3605 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003606 }
3607
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003608 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003609 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003610 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003611 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003612 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003613 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003614
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003615 ssl->ticket_keys = tkeys;
3616
3617 return( 0 );
3618}
Paul Bakkera503a632013-08-14 13:48:06 +02003619#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003620
3621/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003622 * SSL set accessors
3623 */
3624void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3625{
3626 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003627
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003628#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3629 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003630 if( endpoint == SSL_IS_CLIENT )
3631 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003632#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003633}
3634
3635void ssl_set_authmode( ssl_context *ssl, int authmode )
3636{
3637 ssl->authmode = authmode;
3638}
3639
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003640#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003641void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003642 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003643 void *p_vrfy )
3644{
3645 ssl->f_vrfy = f_vrfy;
3646 ssl->p_vrfy = p_vrfy;
3647}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003648#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003649
Paul Bakker5121ce52009-01-03 21:22:43 +00003650void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003651 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003652 void *p_rng )
3653{
3654 ssl->f_rng = f_rng;
3655 ssl->p_rng = p_rng;
3656}
3657
3658void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003659 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003660 void *p_dbg )
3661{
3662 ssl->f_dbg = f_dbg;
3663 ssl->p_dbg = p_dbg;
3664}
3665
3666void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003667 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003668 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003669{
3670 ssl->f_recv = f_recv;
3671 ssl->f_send = f_send;
3672 ssl->p_recv = p_recv;
3673 ssl->p_send = p_send;
3674}
3675
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003676#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003677void ssl_set_session_cache( ssl_context *ssl,
3678 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3679 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003680{
Paul Bakker0a597072012-09-25 21:55:46 +00003681 ssl->f_get_cache = f_get_cache;
3682 ssl->p_get_cache = p_get_cache;
3683 ssl->f_set_cache = f_set_cache;
3684 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003685}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003686#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003687
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003688#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003689int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003690{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003691 int ret;
3692
3693 if( ssl == NULL ||
3694 session == NULL ||
3695 ssl->session_negotiate == NULL ||
3696 ssl->endpoint != SSL_IS_CLIENT )
3697 {
3698 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3699 }
3700
3701 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3702 return( ret );
3703
Paul Bakker0a597072012-09-25 21:55:46 +00003704 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003705
3706 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003708#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003709
Paul Bakkerb68cad62012-08-23 08:34:18 +00003710void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003711{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003712 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3713 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3714 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3715 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3716}
3717
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003718void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3719 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003720 int major, int minor )
3721{
3722 if( major != SSL_MAJOR_VERSION_3 )
3723 return;
3724
3725 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3726 return;
3727
3728 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003729}
3730
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003731#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003732/* Add a new (empty) key_cert entry an return a pointer to it */
3733static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3734{
3735 ssl_key_cert *key_cert, *last;
3736
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003737 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3738 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003739 return( NULL );
3740
3741 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3742
3743 /* Append the new key_cert to the (possibly empty) current list */
3744 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003745 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003746 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003747 if( ssl->handshake != NULL )
3748 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003749 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003750 else
3751 {
3752 last = ssl->key_cert;
3753 while( last->next != NULL )
3754 last = last->next;
3755 last->next = key_cert;
3756 }
3757
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003758 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003759}
3760
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003761void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003762 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003763{
3764 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003765 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 ssl->peer_cn = peer_cn;
3767}
3768
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003769int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003770 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003771{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003772 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3773
3774 if( key_cert == NULL )
3775 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3776
3777 key_cert->cert = own_cert;
3778 key_cert->key = pk_key;
3779
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003780 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003781}
3782
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003783#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003784int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003785 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003786{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003787 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003788 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003789
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003790 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003791 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3792
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003793 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3794 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003795 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003796
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003797 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003798
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003799 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003800 if( ret != 0 )
3801 return( ret );
3802
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003803 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003804 return( ret );
3805
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003806 key_cert->cert = own_cert;
3807 key_cert->key_own_alloc = 1;
3808
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003809 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003810}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003811#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003812
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003813int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003814 void *rsa_key,
3815 rsa_decrypt_func rsa_decrypt,
3816 rsa_sign_func rsa_sign,
3817 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003818{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003819 int ret;
3820 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003821
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003822 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003823 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3824
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003825 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3826 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003827 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003828
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003829 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003830
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003831 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3832 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3833 return( ret );
3834
3835 key_cert->cert = own_cert;
3836 key_cert->key_own_alloc = 1;
3837
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003838 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00003839}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003840#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003841
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003842#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003843int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3844 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003845{
Paul Bakker6db455e2013-09-18 17:29:31 +02003846 if( psk == NULL || psk_identity == NULL )
3847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3848
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003849 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003850 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3851
Paul Bakker6db455e2013-09-18 17:29:31 +02003852 if( ssl->psk != NULL )
3853 {
3854 polarssl_free( ssl->psk );
3855 polarssl_free( ssl->psk_identity );
3856 }
3857
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003858 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003859 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003860
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003861 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003862 ssl->psk_identity = (unsigned char *)
3863 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003864
3865 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3866 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3867
3868 memcpy( ssl->psk, psk, ssl->psk_len );
3869 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003870
3871 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003872}
3873
3874void ssl_set_psk_cb( ssl_context *ssl,
3875 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3876 size_t),
3877 void *p_psk )
3878{
3879 ssl->f_psk = f_psk;
3880 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003881}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003882#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003883
Paul Bakker48916f92012-09-16 19:57:18 +00003884#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003885int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003886{
3887 int ret;
3888
Paul Bakker48916f92012-09-16 19:57:18 +00003889 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003890 {
3891 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3892 return( ret );
3893 }
3894
Paul Bakker48916f92012-09-16 19:57:18 +00003895 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003896 {
3897 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3898 return( ret );
3899 }
3900
3901 return( 0 );
3902}
3903
Paul Bakker1b57b062011-01-06 15:48:19 +00003904int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3905{
3906 int ret;
3907
Paul Bakker66d5d072014-06-17 16:39:18 +02003908 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003909 {
3910 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3911 return( ret );
3912 }
3913
Paul Bakker66d5d072014-06-17 16:39:18 +02003914 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003915 {
3916 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3917 return( ret );
3918 }
3919
3920 return( 0 );
3921}
Paul Bakker48916f92012-09-16 19:57:18 +00003922#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003923
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003924#if defined(POLARSSL_SSL_SET_CURVES)
3925/*
3926 * Set the allowed elliptic curves
3927 */
3928void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3929{
3930 ssl->curve_list = curve_list;
3931}
3932#endif
3933
Paul Bakker0be444a2013-08-27 21:55:01 +02003934#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003935int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003936{
3937 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003938 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003939
3940 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003941
3942 if( ssl->hostname_len + 1 == 0 )
3943 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3944
Paul Bakker6e339b52013-07-03 13:37:05 +02003945 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003946
Paul Bakkerb15b8512012-01-13 13:44:06 +00003947 if( ssl->hostname == NULL )
3948 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3949
Paul Bakker3c2122f2013-06-24 19:03:14 +02003950 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003951 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003952
Paul Bakker40ea7de2009-05-03 10:18:48 +00003953 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003954
3955 return( 0 );
3956}
3957
Paul Bakker5701cdc2012-09-27 21:49:42 +00003958void ssl_set_sni( ssl_context *ssl,
3959 int (*f_sni)(void *, ssl_context *,
3960 const unsigned char *, size_t),
3961 void *p_sni )
3962{
3963 ssl->f_sni = f_sni;
3964 ssl->p_sni = p_sni;
3965}
Paul Bakker0be444a2013-08-27 21:55:01 +02003966#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003967
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003968#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003969int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003970{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003971 size_t cur_len, tot_len;
3972 const char **p;
3973
3974 /*
3975 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3976 * truncated". Check lengths now rather than later.
3977 */
3978 tot_len = 0;
3979 for( p = protos; *p != NULL; p++ )
3980 {
3981 cur_len = strlen( *p );
3982 tot_len += cur_len;
3983
3984 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3985 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3986 }
3987
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003988 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003989
3990 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003991}
3992
3993const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3994{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003995 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003996}
3997#endif /* POLARSSL_SSL_ALPN */
3998
Paul Bakker490ecc82011-10-06 13:04:09 +00003999void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4000{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004001 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4002 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4003 {
4004 ssl->max_major_ver = major;
4005 ssl->max_minor_ver = minor;
4006 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004007}
4008
Paul Bakker1d29fb52012-09-28 13:28:45 +00004009void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4010{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004011 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4012 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4013 {
4014 ssl->min_major_ver = major;
4015 ssl->min_minor_ver = minor;
4016 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004017}
4018
Paul Bakker05decb22013-08-15 13:33:48 +02004019#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004020int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4021{
Paul Bakker77e257e2013-12-16 15:29:52 +01004022 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004023 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004024 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004025 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004026 }
4027
4028 ssl->mfl_code = mfl_code;
4029
4030 return( 0 );
4031}
Paul Bakker05decb22013-08-15 13:33:48 +02004032#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004033
Paul Bakker1f2bc622013-08-15 13:45:55 +02004034#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004035int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004036{
4037 if( ssl->endpoint != SSL_IS_CLIENT )
4038 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4039
Paul Bakker8c1ede62013-07-19 14:14:37 +02004040 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004041
4042 return( 0 );
4043}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004044#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004045
Paul Bakker48916f92012-09-16 19:57:18 +00004046void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4047{
4048 ssl->disable_renegotiation = renegotiation;
4049}
4050
4051void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4052{
4053 ssl->allow_legacy_renegotiation = allow_legacy;
4054}
4055
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004056void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4057{
4058 ssl->renego_max_records = max_records;
4059}
4060
Paul Bakkera503a632013-08-14 13:48:06 +02004061#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004062int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4063{
4064 ssl->session_tickets = use_tickets;
4065
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004066#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004067 if( ssl->endpoint == SSL_IS_CLIENT )
4068 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004069#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004070
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004071 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4072 return( 0 );
4073
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004074 if( ssl->f_rng == NULL )
4075 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4076
4077 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004078}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004079
4080void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4081{
4082 ssl->ticket_lifetime = lifetime;
4083}
Paul Bakkera503a632013-08-14 13:48:06 +02004084#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004085
Paul Bakker5121ce52009-01-03 21:22:43 +00004086/*
4087 * SSL get accessors
4088 */
Paul Bakker23986e52011-04-24 08:57:21 +00004089size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004090{
4091 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4092}
4093
Paul Bakkerff60ee62010-03-16 21:09:09 +00004094int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004095{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004096 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004097}
4098
Paul Bakkere3166ce2011-01-27 17:40:50 +00004099const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004100{
Paul Bakker926c8e42013-03-06 10:23:34 +01004101 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004102 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004103
Paul Bakkere3166ce2011-01-27 17:40:50 +00004104 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004105}
4106
Paul Bakker43ca69c2011-01-15 17:35:19 +00004107const char *ssl_get_version( const ssl_context *ssl )
4108{
4109 switch( ssl->minor_ver )
4110 {
4111 case SSL_MINOR_VERSION_0:
4112 return( "SSLv3.0" );
4113
4114 case SSL_MINOR_VERSION_1:
4115 return( "TLSv1.0" );
4116
4117 case SSL_MINOR_VERSION_2:
4118 return( "TLSv1.1" );
4119
Paul Bakker1ef83d62012-04-11 12:09:53 +00004120 case SSL_MINOR_VERSION_3:
4121 return( "TLSv1.2" );
4122
Paul Bakker43ca69c2011-01-15 17:35:19 +00004123 default:
4124 break;
4125 }
4126 return( "unknown" );
4127}
4128
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004129#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004130const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004131{
4132 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004133 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004134
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004135 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004136}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004137#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004138
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004139#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004140int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4141{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004142 if( ssl == NULL ||
4143 dst == NULL ||
4144 ssl->session == NULL ||
4145 ssl->endpoint != SSL_IS_CLIENT )
4146 {
4147 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4148 }
4149
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004150 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004151}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004152#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004153
Paul Bakker5121ce52009-01-03 21:22:43 +00004154/*
Paul Bakker1961b702013-01-25 14:49:24 +01004155 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004156 */
Paul Bakker1961b702013-01-25 14:49:24 +01004157int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004158{
Paul Bakker40e46942009-01-03 21:51:57 +00004159 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004160
Paul Bakker40e46942009-01-03 21:51:57 +00004161#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004162 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004163 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004164#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004165#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004166 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004167 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004168#endif
4169
Paul Bakker1961b702013-01-25 14:49:24 +01004170 return( ret );
4171}
4172
4173/*
4174 * Perform the SSL handshake
4175 */
4176int ssl_handshake( ssl_context *ssl )
4177{
4178 int ret = 0;
4179
4180 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4181
4182 while( ssl->state != SSL_HANDSHAKE_OVER )
4183 {
4184 ret = ssl_handshake_step( ssl );
4185
4186 if( ret != 0 )
4187 break;
4188 }
4189
Paul Bakker5121ce52009-01-03 21:22:43 +00004190 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4191
4192 return( ret );
4193}
4194
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004195#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004196/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004197 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004198 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004199static int ssl_write_hello_request( ssl_context *ssl )
4200{
4201 int ret;
4202
4203 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4204
4205 ssl->out_msglen = 4;
4206 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4207 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4208
4209 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4210 {
4211 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4212 return( ret );
4213 }
4214
4215 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4216
4217 return( 0 );
4218}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004219#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004220
4221/*
4222 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004223 * - any side: calling ssl_renegotiate(),
4224 * - client: receiving a HelloRequest during ssl_read(),
4225 * - server: receiving any handshake message on server during ssl_read() after
4226 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004227 * If the handshake doesn't complete due to waiting for I/O, it will continue
4228 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004229 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004230static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004231{
4232 int ret;
4233
4234 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4235
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004236 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4237 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004238
4239 ssl->state = SSL_HELLO_REQUEST;
4240 ssl->renegotiation = SSL_RENEGOTIATION;
4241
Paul Bakker48916f92012-09-16 19:57:18 +00004242 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4243 {
4244 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4245 return( ret );
4246 }
4247
4248 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4249
4250 return( 0 );
4251}
4252
4253/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004254 * Renegotiate current connection on client,
4255 * or request renegotiation on server
4256 */
4257int ssl_renegotiate( ssl_context *ssl )
4258{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004259 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004260
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004261#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004262 /* On server, just send the request */
4263 if( ssl->endpoint == SSL_IS_SERVER )
4264 {
4265 if( ssl->state != SSL_HANDSHAKE_OVER )
4266 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4267
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004268 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4269
4270 /* Did we already try/start sending HelloRequest? */
4271 if( ssl->out_left != 0 )
4272 return( ssl_flush_output( ssl ) );
4273
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004274 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004275 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004276#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004277
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004278#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004279 /*
4280 * On client, either start the renegotiation process or,
4281 * if already in progress, continue the handshake
4282 */
4283 if( ssl->renegotiation != SSL_RENEGOTIATION )
4284 {
4285 if( ssl->state != SSL_HANDSHAKE_OVER )
4286 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4287
4288 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4289 {
4290 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4291 return( ret );
4292 }
4293 }
4294 else
4295 {
4296 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4297 {
4298 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4299 return( ret );
4300 }
4301 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004302#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004303
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004304 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004305}
4306
4307/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 * Receive application data decrypted from the SSL layer
4309 */
Paul Bakker23986e52011-04-24 08:57:21 +00004310int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004311{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004312 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004313 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004314
4315 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4316
4317 if( ssl->state != SSL_HANDSHAKE_OVER )
4318 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004319 ret = ssl_handshake( ssl );
4320 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4321 {
4322 record_read = 1;
4323 }
4324 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 {
4326 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4327 return( ret );
4328 }
4329 }
4330
4331 if( ssl->in_offt == NULL )
4332 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004333 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004335 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4336 {
4337 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4338 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004339
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004340 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4341 return( ret );
4342 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004343 }
4344
4345 if( ssl->in_msglen == 0 &&
4346 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4347 {
4348 /*
4349 * OpenSSL sends empty messages to randomize the IV
4350 */
4351 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4352 {
Paul Bakker831a7552011-05-18 13:32:51 +00004353 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4354 return( 0 );
4355
Paul Bakker5121ce52009-01-03 21:22:43 +00004356 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4357 return( ret );
4358 }
4359 }
4360
Paul Bakker48916f92012-09-16 19:57:18 +00004361 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4362 {
4363 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4364
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004365#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004366 if( ssl->endpoint == SSL_IS_CLIENT &&
4367 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4368 ssl->in_hslen != 4 ) )
4369 {
4370 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4371 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4372 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004373#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004374
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004375 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4376 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004377 ssl->allow_legacy_renegotiation ==
4378 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004379 {
4380 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4381
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004382#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004383 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004384 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004385 /*
4386 * SSLv3 does not have a "no_renegotiation" alert
4387 */
4388 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4389 return( ret );
4390 }
4391 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004392#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004393#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4394 defined(POLARSSL_SSL_PROTO_TLS1_2)
4395 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004396 {
4397 if( ( ret = ssl_send_alert_message( ssl,
4398 SSL_ALERT_LEVEL_WARNING,
4399 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4400 {
4401 return( ret );
4402 }
Paul Bakker48916f92012-09-16 19:57:18 +00004403 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004404 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004405#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4406 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004407 {
4408 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004409 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004410 }
Paul Bakker48916f92012-09-16 19:57:18 +00004411 }
4412 else
4413 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004414 ret = ssl_start_renegotiation( ssl );
4415 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4416 {
4417 record_read = 1;
4418 }
4419 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004420 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004421 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004422 return( ret );
4423 }
Paul Bakker48916f92012-09-16 19:57:18 +00004424 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004425
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004426 /* If a non-handshake record was read during renego, fallthrough,
4427 * else tell the user they should call ssl_read() again */
4428 if( ! record_read )
4429 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004430 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004431 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4432 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004433 ssl->renego_records_seen++;
4434
4435 if( ssl->renego_max_records >= 0 &&
4436 ssl->renego_records_seen > ssl->renego_max_records )
4437 {
4438 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4439 "but not honored by client" ) );
4440 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4441 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004442 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004443
4444 /* Fatal and closure alerts handled by ssl_read_record() */
4445 if( ssl->in_msgtype == SSL_MSG_ALERT )
4446 {
4447 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4448 return( POLARSSL_ERR_NET_WANT_READ );
4449 }
4450
4451 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004452 {
4453 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004454 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004455 }
4456
4457 ssl->in_offt = ssl->in_msg;
4458 }
4459
4460 n = ( len < ssl->in_msglen )
4461 ? len : ssl->in_msglen;
4462
4463 memcpy( buf, ssl->in_offt, n );
4464 ssl->in_msglen -= n;
4465
4466 if( ssl->in_msglen == 0 )
4467 /* all bytes consumed */
4468 ssl->in_offt = NULL;
4469 else
4470 /* more data available */
4471 ssl->in_offt += n;
4472
4473 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4474
Paul Bakker23986e52011-04-24 08:57:21 +00004475 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004476}
4477
4478/*
4479 * Send application data to be encrypted by the SSL layer
4480 */
Paul Bakker23986e52011-04-24 08:57:21 +00004481int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004482{
Paul Bakker23986e52011-04-24 08:57:21 +00004483 int ret;
4484 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004485 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004486
4487 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4488
4489 if( ssl->state != SSL_HANDSHAKE_OVER )
4490 {
4491 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4492 {
4493 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4494 return( ret );
4495 }
4496 }
4497
Paul Bakker05decb22013-08-15 13:33:48 +02004498#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004499 /*
4500 * Assume mfl_code is correct since it was checked when set
4501 */
4502 max_len = mfl_code_to_length[ssl->mfl_code];
4503
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004504 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004505 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004506 */
4507 if( ssl->session_out != NULL &&
4508 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4509 {
4510 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4511 }
Paul Bakker05decb22013-08-15 13:33:48 +02004512#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004513
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004514 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004515
Paul Bakker5121ce52009-01-03 21:22:43 +00004516 if( ssl->out_left != 0 )
4517 {
4518 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4519 {
4520 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4521 return( ret );
4522 }
4523 }
Paul Bakker887bd502011-06-08 13:10:54 +00004524 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004525 {
Paul Bakker887bd502011-06-08 13:10:54 +00004526 ssl->out_msglen = n;
4527 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4528 memcpy( ssl->out_msg, buf, n );
4529
4530 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4531 {
4532 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4533 return( ret );
4534 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004535 }
4536
4537 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4538
Paul Bakker23986e52011-04-24 08:57:21 +00004539 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004540}
4541
4542/*
4543 * Notify the peer that the connection is being closed
4544 */
4545int ssl_close_notify( ssl_context *ssl )
4546{
4547 int ret;
4548
4549 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4550
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004551 if( ssl->out_left != 0 )
4552 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004553
4554 if( ssl->state == SSL_HANDSHAKE_OVER )
4555 {
Paul Bakker48916f92012-09-16 19:57:18 +00004556 if( ( ret = ssl_send_alert_message( ssl,
4557 SSL_ALERT_LEVEL_WARNING,
4558 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004559 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004560 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004561 return( ret );
4562 }
4563 }
4564
4565 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4566
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004567 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004568}
4569
Paul Bakker48916f92012-09-16 19:57:18 +00004570void ssl_transform_free( ssl_transform *transform )
4571{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004572 if( transform == NULL )
4573 return;
4574
Paul Bakker48916f92012-09-16 19:57:18 +00004575#if defined(POLARSSL_ZLIB_SUPPORT)
4576 deflateEnd( &transform->ctx_deflate );
4577 inflateEnd( &transform->ctx_inflate );
4578#endif
4579
Paul Bakker84bbeb52014-07-01 14:53:22 +02004580 cipher_free( &transform->cipher_ctx_enc );
4581 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004582
Paul Bakker84bbeb52014-07-01 14:53:22 +02004583 md_free( &transform->md_ctx_enc );
4584 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004585
Paul Bakker34617722014-06-13 17:20:13 +02004586 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004587}
4588
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004589#if defined(POLARSSL_X509_CRT_PARSE_C)
4590static void ssl_key_cert_free( ssl_key_cert *key_cert )
4591{
4592 ssl_key_cert *cur = key_cert, *next;
4593
4594 while( cur != NULL )
4595 {
4596 next = cur->next;
4597
4598 if( cur->key_own_alloc )
4599 {
4600 pk_free( cur->key );
4601 polarssl_free( cur->key );
4602 }
4603 polarssl_free( cur );
4604
4605 cur = next;
4606 }
4607}
4608#endif /* POLARSSL_X509_CRT_PARSE_C */
4609
Paul Bakker48916f92012-09-16 19:57:18 +00004610void ssl_handshake_free( ssl_handshake_params *handshake )
4611{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004612 if( handshake == NULL )
4613 return;
4614
Paul Bakker48916f92012-09-16 19:57:18 +00004615#if defined(POLARSSL_DHM_C)
4616 dhm_free( &handshake->dhm_ctx );
4617#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004618#if defined(POLARSSL_ECDH_C)
4619 ecdh_free( &handshake->ecdh_ctx );
4620#endif
4621
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004622#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004623 /* explicit void pointer cast for buggy MS compiler */
4624 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004625#endif
4626
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004627#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4628 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4629 /*
4630 * Free only the linked list wrapper, not the keys themselves
4631 * since the belong to the SNI callback
4632 */
4633 if( handshake->sni_key_cert != NULL )
4634 {
4635 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4636
4637 while( cur != NULL )
4638 {
4639 next = cur->next;
4640 polarssl_free( cur );
4641 cur = next;
4642 }
4643 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004644#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004645
Paul Bakker34617722014-06-13 17:20:13 +02004646 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004647}
4648
4649void ssl_session_free( ssl_session *session )
4650{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004651 if( session == NULL )
4652 return;
4653
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004654#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004655 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004656 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004657 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004658 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004659 }
Paul Bakkered27a042013-04-18 22:46:23 +02004660#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004661
Paul Bakkera503a632013-08-14 13:48:06 +02004662#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004663 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004664#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004665
Paul Bakker34617722014-06-13 17:20:13 +02004666 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004667}
4668
Paul Bakker5121ce52009-01-03 21:22:43 +00004669/*
4670 * Free an SSL context
4671 */
4672void ssl_free( ssl_context *ssl )
4673{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004674 if( ssl == NULL )
4675 return;
4676
Paul Bakker5121ce52009-01-03 21:22:43 +00004677 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4678
Paul Bakker5121ce52009-01-03 21:22:43 +00004679 if( ssl->out_ctr != NULL )
4680 {
Paul Bakker34617722014-06-13 17:20:13 +02004681 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004682 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004683 }
4684
4685 if( ssl->in_ctr != NULL )
4686 {
Paul Bakker34617722014-06-13 17:20:13 +02004687 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004688 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004689 }
4690
Paul Bakker16770332013-10-11 09:59:44 +02004691#if defined(POLARSSL_ZLIB_SUPPORT)
4692 if( ssl->compress_buf != NULL )
4693 {
Paul Bakker34617722014-06-13 17:20:13 +02004694 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004695 polarssl_free( ssl->compress_buf );
4696 }
4697#endif
4698
Paul Bakker40e46942009-01-03 21:51:57 +00004699#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004700 mpi_free( &ssl->dhm_P );
4701 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004702#endif
4703
Paul Bakker48916f92012-09-16 19:57:18 +00004704 if( ssl->transform )
4705 {
4706 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004707 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004708 }
4709
4710 if( ssl->handshake )
4711 {
4712 ssl_handshake_free( ssl->handshake );
4713 ssl_transform_free( ssl->transform_negotiate );
4714 ssl_session_free( ssl->session_negotiate );
4715
Paul Bakker6e339b52013-07-03 13:37:05 +02004716 polarssl_free( ssl->handshake );
4717 polarssl_free( ssl->transform_negotiate );
4718 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004719 }
4720
Paul Bakkerc0463502013-02-14 11:19:38 +01004721 if( ssl->session )
4722 {
4723 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004724 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004725 }
4726
Paul Bakkera503a632013-08-14 13:48:06 +02004727#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004728 if( ssl->ticket_keys )
4729 {
4730 ssl_ticket_keys_free( ssl->ticket_keys );
4731 polarssl_free( ssl->ticket_keys );
4732 }
Paul Bakkera503a632013-08-14 13:48:06 +02004733#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004734
Paul Bakker0be444a2013-08-27 21:55:01 +02004735#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004736 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004737 {
Paul Bakker34617722014-06-13 17:20:13 +02004738 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004739 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004740 ssl->hostname_len = 0;
4741 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004742#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004743
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004744#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004745 if( ssl->psk != NULL )
4746 {
Paul Bakker34617722014-06-13 17:20:13 +02004747 polarssl_zeroize( ssl->psk, ssl->psk_len );
4748 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004749 polarssl_free( ssl->psk );
4750 polarssl_free( ssl->psk_identity );
4751 ssl->psk_len = 0;
4752 ssl->psk_identity_len = 0;
4753 }
4754#endif
4755
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004756#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004757 ssl_key_cert_free( ssl->key_cert );
4758#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004759
Paul Bakker05ef8352012-05-08 09:17:57 +00004760#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4761 if( ssl_hw_record_finish != NULL )
4762 {
4763 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4764 ssl_hw_record_finish( ssl );
4765 }
4766#endif
4767
Paul Bakker5121ce52009-01-03 21:22:43 +00004768 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004769
Paul Bakker86f04f42013-02-14 11:20:09 +01004770 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004771 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004772}
4773
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004774#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004775/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004776 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004777 */
4778unsigned char ssl_sig_from_pk( pk_context *pk )
4779{
4780#if defined(POLARSSL_RSA_C)
4781 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4782 return( SSL_SIG_RSA );
4783#endif
4784#if defined(POLARSSL_ECDSA_C)
4785 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4786 return( SSL_SIG_ECDSA );
4787#endif
4788 return( SSL_SIG_ANON );
4789}
4790
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004791pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4792{
4793 switch( sig )
4794 {
4795#if defined(POLARSSL_RSA_C)
4796 case SSL_SIG_RSA:
4797 return( POLARSSL_PK_RSA );
4798#endif
4799#if defined(POLARSSL_ECDSA_C)
4800 case SSL_SIG_ECDSA:
4801 return( POLARSSL_PK_ECDSA );
4802#endif
4803 default:
4804 return( POLARSSL_PK_NONE );
4805 }
4806}
Paul Bakker9af723c2014-05-01 13:03:14 +02004807#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004808
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004809/*
4810 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4811 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004812md_type_t ssl_md_alg_from_hash( unsigned char hash )
4813{
4814 switch( hash )
4815 {
4816#if defined(POLARSSL_MD5_C)
4817 case SSL_HASH_MD5:
4818 return( POLARSSL_MD_MD5 );
4819#endif
4820#if defined(POLARSSL_SHA1_C)
4821 case SSL_HASH_SHA1:
4822 return( POLARSSL_MD_SHA1 );
4823#endif
4824#if defined(POLARSSL_SHA256_C)
4825 case SSL_HASH_SHA224:
4826 return( POLARSSL_MD_SHA224 );
4827 case SSL_HASH_SHA256:
4828 return( POLARSSL_MD_SHA256 );
4829#endif
4830#if defined(POLARSSL_SHA512_C)
4831 case SSL_HASH_SHA384:
4832 return( POLARSSL_MD_SHA384 );
4833 case SSL_HASH_SHA512:
4834 return( POLARSSL_MD_SHA512 );
4835#endif
4836 default:
4837 return( POLARSSL_MD_NONE );
4838 }
4839}
4840
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004841#if defined(POLARSSL_SSL_SET_CURVES)
4842/*
4843 * Check is a curve proposed by the peer is in our list.
4844 * Return 1 if we're willing to use it, 0 otherwise.
4845 */
4846int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4847{
4848 const ecp_group_id *gid;
4849
4850 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4851 if( *gid == grp_id )
4852 return( 1 );
4853
4854 return( 0 );
4855}
Paul Bakker9af723c2014-05-01 13:03:14 +02004856#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004857
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004858#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004859int ssl_check_cert_usage( const x509_crt *cert,
4860 const ssl_ciphersuite_t *ciphersuite,
4861 int cert_endpoint )
4862{
4863#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4864 int usage = 0;
4865#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004866#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4867 const char *ext_oid;
4868 size_t ext_len;
4869#endif
4870
4871#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4872 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4873 ((void) cert);
4874 ((void) cert_endpoint);
4875#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004876
4877#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4878 if( cert_endpoint == SSL_IS_SERVER )
4879 {
4880 /* Server part of the key exchange */
4881 switch( ciphersuite->key_exchange )
4882 {
4883 case POLARSSL_KEY_EXCHANGE_RSA:
4884 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4885 usage = KU_KEY_ENCIPHERMENT;
4886 break;
4887
4888 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4889 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4890 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4891 usage = KU_DIGITAL_SIGNATURE;
4892 break;
4893
4894 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4895 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4896 usage = KU_KEY_AGREEMENT;
4897 break;
4898
4899 /* Don't use default: we want warnings when adding new values */
4900 case POLARSSL_KEY_EXCHANGE_NONE:
4901 case POLARSSL_KEY_EXCHANGE_PSK:
4902 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4903 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4904 usage = 0;
4905 }
4906 }
4907 else
4908 {
4909 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4910 usage = KU_DIGITAL_SIGNATURE;
4911 }
4912
4913 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4914 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004915#else
4916 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004917#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4918
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004919#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4920 if( cert_endpoint == SSL_IS_SERVER )
4921 {
4922 ext_oid = OID_SERVER_AUTH;
4923 ext_len = OID_SIZE( OID_SERVER_AUTH );
4924 }
4925 else
4926 {
4927 ext_oid = OID_CLIENT_AUTH;
4928 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4929 }
4930
4931 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4932 return( -1 );
4933#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4934
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004935 return( 0 );
4936}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004937#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004938
4939#endif /* POLARSSL_SSL_TLS_C */