blob: f635505557c4174195e4be3e487784ab72f33442 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
604 * first multiple of blocklen greater than maclen
605 * + IV except for SSL3 and TLS 1.0
606 */
607 transform->minlen = transform->maclen
608 + cipher_info->block_size
609 - transform->maclen % cipher_info->block_size;
610
611#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
612 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
613 ssl->minor_ver == SSL_MINOR_VERSION_1 )
614 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100615 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200616#endif
617#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
618 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
619 ssl->minor_ver == SSL_MINOR_VERSION_3 )
620 {
621 transform->minlen += transform->ivlen;
622 }
623 else
624#endif
625 {
626 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
627 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
628 }
Paul Bakker68884e32013-01-07 18:20:04 +0100629 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
632 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000633 transform->keylen, transform->minlen, transform->ivlen,
634 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 /*
637 * Finally setup the cipher contexts, IVs and MAC secrets.
638 */
639 if( ssl->endpoint == SSL_IS_CLIENT )
640 {
Paul Bakker48916f92012-09-16 19:57:18 +0000641 key1 = keyblk + transform->maclen * 2;
642 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Paul Bakker68884e32013-01-07 18:20:04 +0100644 mac_enc = keyblk;
645 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000647 /*
648 * This is not used in TLS v1.1.
649 */
Paul Bakker48916f92012-09-16 19:57:18 +0000650 iv_copy_len = ( transform->fixed_ivlen ) ?
651 transform->fixed_ivlen : transform->ivlen;
652 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
653 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
656 else
657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 key1 = keyblk + transform->maclen * 2 + transform->keylen;
659 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Paul Bakker68884e32013-01-07 18:20:04 +0100661 mac_enc = keyblk + transform->maclen;
662 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000664 /*
665 * This is not used in TLS v1.1.
666 */
Paul Bakker48916f92012-09-16 19:57:18 +0000667 iv_copy_len = ( transform->fixed_ivlen ) ?
668 transform->fixed_ivlen : transform->ivlen;
669 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
670 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
673
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100675 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
676 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100677 if( transform->maclen > sizeof transform->mac_enc )
678 {
679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100681 }
682
Paul Bakker68884e32013-01-07 18:20:04 +0100683 memcpy( transform->mac_enc, mac_enc, transform->maclen );
684 memcpy( transform->mac_dec, mac_dec, transform->maclen );
685 }
686 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
689 defined(POLARSSL_SSL_PROTO_TLS1_2)
690 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100691 {
692 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
693 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695 else
696#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200700 }
Paul Bakker68884e32013-01-07 18:20:04 +0100701
Paul Bakker05ef8352012-05-08 09:17:57 +0000702#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200703 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000704 {
705 int ret = 0;
706
707 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
708
Paul Bakker07eb38b2012-12-19 14:42:06 +0100709 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
710 transform->iv_enc, transform->iv_dec,
711 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100712 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100713 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000714 {
715 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200716 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000717 }
718 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200719#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000720
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200721 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
722 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200724 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
725 return( ret );
726 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200727
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
729 cipher_info ) ) != 0 )
730 {
731 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
732 return( ret );
733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200734
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
736 cipher_info->key_length,
737 POLARSSL_ENCRYPT ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
740 return( ret );
741 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200742
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
744 cipher_info->key_length,
745 POLARSSL_DECRYPT ) ) != 0 )
746 {
747 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
748 return( ret );
749 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200750
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200751#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( cipher_info->mode == POLARSSL_MODE_CBC )
753 {
754 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
755 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200756 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200757 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
758 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200759 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760
761 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
762 POLARSSL_PADDING_NONE ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
765 return( ret );
766 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Paul Bakker34617722014-06-13 17:20:13 +0200770 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772#if defined(POLARSSL_ZLIB_SUPPORT)
773 // Initialize compression
774 //
Paul Bakker48916f92012-09-16 19:57:18 +0000775 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000776 {
Paul Bakker16770332013-10-11 09:59:44 +0200777 if( ssl->compress_buf == NULL )
778 {
779 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
780 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
781 if( ssl->compress_buf == NULL )
782 {
783 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
784 SSL_BUFFER_LEN ) );
785 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
786 }
787 }
788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
792 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200794 if( deflateInit( &transform->ctx_deflate,
795 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000796 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000797 {
798 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
799 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
800 }
801 }
802#endif /* POLARSSL_ZLIB_SUPPORT */
803
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
805
806 return( 0 );
807}
808
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000810void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000811{
812 md5_context md5;
813 sha1_context sha1;
814 unsigned char pad_1[48];
815 unsigned char pad_2[48];
816
Paul Bakker380da532012-04-18 16:10:25 +0000817 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Paul Bakker48916f92012-09-16 19:57:18 +0000819 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
820 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakker380da532012-04-18 16:10:25 +0000822 memset( pad_1, 0x36, 48 );
823 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakker48916f92012-09-16 19:57:18 +0000825 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000826 md5_update( &md5, pad_1, 48 );
827 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker380da532012-04-18 16:10:25 +0000829 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000830 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000831 md5_update( &md5, pad_2, 48 );
832 md5_update( &md5, hash, 16 );
833 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker48916f92012-09-16 19:57:18 +0000835 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000836 sha1_update( &sha1, pad_1, 40 );
837 sha1_finish( &sha1, hash + 16 );
838
839 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000840 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000841 sha1_update( &sha1, pad_2, 40 );
842 sha1_update( &sha1, hash + 16, 20 );
843 sha1_finish( &sha1, hash + 16 );
844
845 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
846 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
847
Paul Bakker5b4af392014-06-26 12:09:34 +0200848 md5_free( &md5 );
849 sha1_free( &sha1 );
850
Paul Bakker380da532012-04-18 16:10:25 +0000851 return;
852}
Paul Bakker9af723c2014-05-01 13:03:14 +0200853#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000854
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
857{
858 md5_context md5;
859 sha1_context sha1;
860
861 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
862
Paul Bakker48916f92012-09-16 19:57:18 +0000863 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
864 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakker48916f92012-09-16 19:57:18 +0000866 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000867 sha1_finish( &sha1, hash + 16 );
868
869 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
870 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
871
Paul Bakker5b4af392014-06-26 12:09:34 +0200872 md5_free( &md5 );
873 sha1_free( &sha1 );
874
Paul Bakker380da532012-04-18 16:10:25 +0000875 return;
876}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200877#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000878
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200879#if defined(POLARSSL_SSL_PROTO_TLS1_2)
880#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000881void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
882{
Paul Bakker9e36f042013-06-30 14:34:05 +0200883 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000884
885 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
886
Paul Bakker9e36f042013-06-30 14:34:05 +0200887 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
888 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000889
890 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
891 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
892
Paul Bakker5b4af392014-06-26 12:09:34 +0200893 sha256_free( &sha256 );
894
Paul Bakker380da532012-04-18 16:10:25 +0000895 return;
896}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000898
Paul Bakker9e36f042013-06-30 14:34:05 +0200899#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
907 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakkerca4ab492012-04-18 14:23:57 +0000909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha512_free( &sha512 );
913
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA512_C */
917#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200919#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
921{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200922 unsigned char *p = ssl->handshake->premaster;
923 unsigned char *end = p + sizeof( ssl->handshake->premaster );
924
925 /*
926 * PMS = struct {
927 * opaque other_secret<0..2^16-1>;
928 * opaque psk<0..2^16-1>;
929 * };
930 * with "other_secret" depending on the particular key exchange
931 */
932#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
934 {
935 if( end - p < 2 + (int) ssl->psk_len )
936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
937
938 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
939 *(p++) = (unsigned char)( ssl->psk_len );
940 p += ssl->psk_len;
941 }
942 else
943#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200944#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
945 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
946 {
947 /*
948 * other_secret already set by the ClientKeyExchange message,
949 * and is 48 bytes long
950 */
951 *p++ = 0;
952 *p++ = 48;
953 p += 48;
954 }
955 else
956#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
958 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
959 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200960 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200961 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200963 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200965 p + 2, &len,
966 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200967 {
968 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
969 return( ret );
970 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200971 *(p++) = (unsigned char)( len >> 8 );
972 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 p += len;
974
975 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
979#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 size_t zlen;
984
985 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200986 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200987 ssl->f_rng, ssl->p_rng ) ) != 0 )
988 {
989 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
990 return( ret );
991 }
992
993 *(p++) = (unsigned char)( zlen >> 8 );
994 *(p++) = (unsigned char)( zlen );
995 p += zlen;
996
997 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1001 {
1002 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001003 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 }
1005
1006 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001007 if( end - p < 2 + (int) ssl->psk_len )
1008 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1009
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1011 *(p++) = (unsigned char)( ssl->psk_len );
1012 memcpy( p, ssl->psk, ssl->psk_len );
1013 p += ssl->psk_len;
1014
1015 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1016
1017 return( 0 );
1018}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001019#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001022/*
1023 * SSLv3.0 MAC functions
1024 */
Paul Bakker68884e32013-01-07 18:20:04 +01001025static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1026 unsigned char *buf, size_t len,
1027 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001028{
1029 unsigned char header[11];
1030 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001031 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001032 int md_size = md_get_size( md_ctx->md_info );
1033 int md_type = md_get_type( md_ctx->md_info );
1034
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001035 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001036 if( md_type == POLARSSL_MD_MD5 )
1037 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001038 else
Paul Bakker68884e32013-01-07 18:20:04 +01001039 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
1041 memcpy( header, ctr, 8 );
1042 header[ 8] = (unsigned char) type;
1043 header[ 9] = (unsigned char)( len >> 8 );
1044 header[10] = (unsigned char)( len );
1045
Paul Bakker68884e32013-01-07 18:20:04 +01001046 memset( padding, 0x36, padlen );
1047 md_starts( md_ctx );
1048 md_update( md_ctx, secret, md_size );
1049 md_update( md_ctx, padding, padlen );
1050 md_update( md_ctx, header, 11 );
1051 md_update( md_ctx, buf, len );
1052 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakker68884e32013-01-07 18:20:04 +01001054 memset( padding, 0x5C, padlen );
1055 md_starts( md_ctx );
1056 md_update( md_ctx, secret, md_size );
1057 md_update( md_ctx, padding, padlen );
1058 md_update( md_ctx, buf + len, md_size );
1059 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001060}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001062
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001063#define MAC_NONE 0
1064#define MAC_PLAINTEXT 1
1065#define MAC_CIPHERTEXT 2
1066
1067/*
1068 * Is MAC applied on ciphertext, cleartext or not at all?
1069 */
1070static char ssl_get_mac_order( ssl_context *ssl,
1071 const ssl_session *session,
1072 cipher_mode_t mode )
1073{
1074#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
1075 if( mode == POLARSSL_MODE_STREAM )
1076 return( MAC_PLAINTEXT );
1077#endif
1078
1079#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1080 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
1081 if( mode == POLARSSL_MODE_CBC )
1082 {
1083#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1084 if( session != NULL && session->encrypt_then_mac == SSL_ETM_ENABLED )
1085 {
1086 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1087 return( MAC_CIPHERTEXT );
1088 }
1089#endif
1090
1091 return( MAC_PLAINTEXT );
1092 }
1093#endif
1094
1095 return( MAC_NONE );
1096}
1097
Paul Bakker5121ce52009-01-03 21:22:43 +00001098/*
1099 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001100 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001101static int ssl_encrypt_buf( ssl_context *ssl )
1102{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001103 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001104 const cipher_mode_t mode = cipher_get_cipher_mode(
1105 &ssl->transform_out->cipher_ctx_enc );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001106 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001107
1108 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1109
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001110 mac_order = ssl_get_mac_order( ssl, ssl->session_out, mode );
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001111
Paul Bakker5121ce52009-01-03 21:22:43 +00001112 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001113 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00001114 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001115#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1116 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1117 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001118 if( mac_order == MAC_PLAINTEXT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001120#if defined(POLARSSL_SSL_PROTO_SSL3)
1121 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1122 {
1123 ssl_mac( &ssl->transform_out->md_ctx_enc,
1124 ssl->transform_out->mac_enc,
1125 ssl->out_msg, ssl->out_msglen,
1126 ssl->out_ctr, ssl->out_msgtype );
1127 }
1128 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001129#endif
1130#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001131 defined(POLARSSL_SSL_PROTO_TLS1_2)
1132 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1133 {
1134 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1135 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1136 ssl->out_msg, ssl->out_msglen );
1137 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1138 ssl->out_msg + ssl->out_msglen );
1139 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1140 }
1141 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001142#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001143 {
1144 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001145 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001146 }
1147
1148 SSL_DEBUG_BUF( 4, "computed mac",
1149 ssl->out_msg + ssl->out_msglen,
1150 ssl->transform_out->maclen );
1151
1152 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001153 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001154#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001156 /*
1157 * Encrypt
1158 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001159#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001160 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001162 int ret;
1163 size_t olen = 0;
1164
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1166 "including %d bytes of padding",
1167 ssl->out_msglen, 0 ) );
1168
1169 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1170 ssl->out_msg, ssl->out_msglen );
1171
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001172 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001173 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001174 ssl->transform_out->ivlen,
1175 ssl->out_msg, ssl->out_msglen,
1176 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001177 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001178 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001179 return( ret );
1180 }
1181
1182 if( ssl->out_msglen != olen )
1183 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001184 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001185 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001186 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 }
Paul Bakker68884e32013-01-07 18:20:04 +01001188 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001189#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001190#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1191 if( mode == POLARSSL_MODE_GCM ||
1192 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001193 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001194 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001195 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001196 unsigned char *enc_msg;
1197 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001198 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1199 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001200
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201 memcpy( add_data, ssl->out_ctr, 8 );
1202 add_data[8] = ssl->out_msgtype;
1203 add_data[9] = ssl->major_ver;
1204 add_data[10] = ssl->minor_ver;
1205 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1206 add_data[12] = ssl->out_msglen & 0xFF;
1207
1208 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1209 add_data, 13 );
1210
Paul Bakker68884e32013-01-07 18:20:04 +01001211 /*
1212 * Generate IV
1213 */
1214 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001215 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1216 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001217 if( ret != 0 )
1218 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001219
Paul Bakker68884e32013-01-07 18:20:04 +01001220 memcpy( ssl->out_iv,
1221 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1222 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001223
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001224 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001225 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001226
Paul Bakker68884e32013-01-07 18:20:04 +01001227 /*
1228 * Fix pointer positions and message length with added IV
1229 */
1230 enc_msg = ssl->out_msg;
1231 enc_msglen = ssl->out_msglen;
1232 ssl->out_msglen += ssl->transform_out->ivlen -
1233 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001234
Paul Bakker68884e32013-01-07 18:20:04 +01001235 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1236 "including %d bytes of padding",
1237 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001238
Paul Bakker68884e32013-01-07 18:20:04 +01001239 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1240 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001241
Paul Bakker68884e32013-01-07 18:20:04 +01001242 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001243 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001244 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001245 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1246 ssl->transform_out->iv_enc,
1247 ssl->transform_out->ivlen,
1248 add_data, 13,
1249 enc_msg, enc_msglen,
1250 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001251 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001252 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001253 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001254 return( ret );
1255 }
1256
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001257 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001258 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001259 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001260 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001261 }
1262
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001263 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001264
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001265 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001268#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001269#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1270 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001271 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001274 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001275 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001276
Paul Bakker48916f92012-09-16 19:57:18 +00001277 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1278 ssl->transform_out->ivlen;
1279 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 padlen = 0;
1281
1282 for( i = 0; i <= padlen; i++ )
1283 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1284
1285 ssl->out_msglen += padlen + 1;
1286
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001287 enc_msglen = ssl->out_msglen;
1288 enc_msg = ssl->out_msg;
1289
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001290#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001291 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001292 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1293 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001294 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001295 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001296 {
1297 /*
1298 * Generate IV
1299 */
Paul Bakker48916f92012-09-16 19:57:18 +00001300 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1301 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001302 if( ret != 0 )
1303 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001304
Paul Bakker92be97b2013-01-02 17:30:03 +01001305 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001306 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001307
1308 /*
1309 * Fix pointer positions and message length with added IV
1310 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001311 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001312 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001313 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001314 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001315#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001318 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001319 ssl->out_msglen, ssl->transform_out->ivlen,
1320 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
1322 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001323 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001325 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001326 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001327 ssl->transform_out->ivlen,
1328 enc_msg, enc_msglen,
1329 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001330 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001331 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001332 return( ret );
1333 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001334
Paul Bakkercca5b812013-08-31 17:40:26 +02001335 if( enc_msglen != olen )
1336 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001337 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001338 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001339 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001340
1341#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001342 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1343 {
1344 /*
1345 * Save IV in SSL3 and TLS1
1346 */
1347 memcpy( ssl->transform_out->iv_enc,
1348 ssl->transform_out->cipher_ctx_enc.iv,
1349 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001351#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001352
1353#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1354 if( mac_order == MAC_CIPHERTEXT )
1355 {
1356 /*
1357 * MAC(MAC_write_key, seq_num +
1358 * TLSCipherText.type +
1359 * TLSCipherText.version +
1360 * TLSCipherText.length +
1361 * IV + // except for TLS 1.0
1362 * ENC(content + padding + padding_length));
1363 */
1364 size_t final_len = ssl->out_msglen + ssl->transform_out->maclen;
1365 unsigned char pseudo_hdr[13];
1366
1367 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1368 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
1369 pseudo_hdr[11] = (unsigned char)( ( final_len >> 8 ) & 0xFF );
1370 pseudo_hdr[12] = (unsigned char)( ( final_len ) & 0xFF );
1371
1372 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1373 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1374 ssl->out_iv, ssl->out_msglen );
1375 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1376 ssl->out_iv + ssl->out_msglen );
1377 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1378
1379 ssl->out_msglen += ssl->transform_out->maclen;
1380 }
1381#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001383 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001384#endif /* POLARSSL_CIPHER_MODE_CBC &&
1385 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001386 {
1387 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001388 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001389 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Paul Bakkerca4ab492012-04-18 14:23:57 +00001391 for( i = 8; i > 0; i-- )
1392 if( ++ssl->out_ctr[i - 1] != 0 )
1393 break;
1394
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001395 /* The loops goes to its end iff the counter is wrapping */
1396 if( i == 0 )
1397 {
1398 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1399 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1400 }
1401
Paul Bakker5121ce52009-01-03 21:22:43 +00001402 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1403
1404 return( 0 );
1405}
1406
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001407#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001408
Paul Bakker5121ce52009-01-03 21:22:43 +00001409static int ssl_decrypt_buf( ssl_context *ssl )
1410{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001411 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001412 const cipher_mode_t mode = cipher_get_cipher_mode(
1413 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001414#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1415 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1416 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1417 size_t padlen = 0, correct = 1;
1418#endif
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001419 char mac_order;
Paul Bakker5121ce52009-01-03 21:22:43 +00001420
1421 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1422
Paul Bakker48916f92012-09-16 19:57:18 +00001423 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 {
1425 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001426 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001427 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428 }
1429
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001430 mac_order = ssl_get_mac_order( ssl, ssl->session_in, mode );
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001431
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001432#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001433 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001434 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001435 int ret;
1436 size_t olen = 0;
1437
Paul Bakker68884e32013-01-07 18:20:04 +01001438 padlen = 0;
1439
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001440 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001441 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001442 ssl->transform_in->ivlen,
1443 ssl->in_msg, ssl->in_msglen,
1444 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001445 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001446 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001447 return( ret );
1448 }
1449
1450 if( ssl->in_msglen != olen )
1451 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001452 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001453 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001454 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001455 }
Paul Bakker68884e32013-01-07 18:20:04 +01001456 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001457#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001458#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1459 if( mode == POLARSSL_MODE_GCM ||
1460 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001461 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001462 int ret;
1463 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001464 unsigned char *dec_msg;
1465 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001466 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001467 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1468 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001469 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1470 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001471
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001472 if( ssl->in_msglen < explicit_iv_len + taglen )
1473 {
1474 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1475 "+ taglen (%d)", ssl->in_msglen,
1476 explicit_iv_len, taglen ) );
1477 return( POLARSSL_ERR_SSL_INVALID_MAC );
1478 }
1479 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1480
Paul Bakker68884e32013-01-07 18:20:04 +01001481 dec_msg = ssl->in_msg;
1482 dec_msg_result = ssl->in_msg;
1483 ssl->in_msglen = dec_msglen;
1484
1485 memcpy( add_data, ssl->in_ctr, 8 );
1486 add_data[8] = ssl->in_msgtype;
1487 add_data[9] = ssl->major_ver;
1488 add_data[10] = ssl->minor_ver;
1489 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1490 add_data[12] = ssl->in_msglen & 0xFF;
1491
1492 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1493 add_data, 13 );
1494
1495 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1496 ssl->in_iv,
1497 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1498
1499 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1500 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001501 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001502
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001503 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001504 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001505 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001506 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1507 ssl->transform_in->iv_dec,
1508 ssl->transform_in->ivlen,
1509 add_data, 13,
1510 dec_msg, dec_msglen,
1511 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001512 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001513 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001514 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1515
1516 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1517 return( POLARSSL_ERR_SSL_INVALID_MAC );
1518
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001519 return( ret );
1520 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001521
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001522 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001523 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001524 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001525 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001526 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001527 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001528 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001529#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001530#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1531 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001532 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001533 {
Paul Bakker45829992013-01-03 14:52:21 +01001534 /*
1535 * Decrypt and check the padding
1536 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001537 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001538 unsigned char *dec_msg;
1539 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001540 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001541 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001542 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001543
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 /*
Paul Bakker45829992013-01-03 14:52:21 +01001545 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001547#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001548 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1549 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001550#endif
Paul Bakker45829992013-01-03 14:52:21 +01001551
1552 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1553 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1554 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001555 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1556 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1557 ssl->transform_in->ivlen,
1558 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001559 return( POLARSSL_ERR_SSL_INVALID_MAC );
1560 }
1561
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001562 dec_msglen = ssl->in_msglen;
1563 dec_msg = ssl->in_msg;
1564 dec_msg_result = ssl->in_msg;
1565
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001566 /*
1567 * Authenticate before decrypt if enabled
1568 */
1569#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1570 if( mac_order == MAC_CIPHERTEXT )
1571 {
1572 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
1573
1574 dec_msglen -= ssl->transform_in->maclen;
1575 ssl->in_msglen -= ssl->transform_in->maclen;
1576
1577 // TODO: adjust for DTLS
1578 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1579 ssl->in_ctr, 13 );
1580 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1581 ssl->in_iv, ssl->in_msglen );
1582 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1583 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1584
1585 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1586 ssl->transform_in->maclen );
1587 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1588 ssl->transform_in->maclen );
1589
1590 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1591 ssl->transform_in->maclen ) != 0 )
1592 {
1593 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1594
1595 return( POLARSSL_ERR_SSL_INVALID_MAC );
1596 }
1597 }
1598#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1599
1600 /*
1601 * Check length sanity
1602 */
1603 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1604 {
1605 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1606 ssl->in_msglen, ssl->transform_in->ivlen ) );
1607 return( POLARSSL_ERR_SSL_INVALID_MAC );
1608 }
1609
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001610#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001611 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001612 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001613 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001614 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001615 {
Paul Bakker48916f92012-09-16 19:57:18 +00001616 dec_msglen -= ssl->transform_in->ivlen;
1617 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001618
Paul Bakker48916f92012-09-16 19:57:18 +00001619 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001620 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001621 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001622#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001623
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001624 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001625 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001626 ssl->transform_in->ivlen,
1627 dec_msg, dec_msglen,
1628 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001629 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001630 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001631 return( ret );
1632 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001633
Paul Bakkercca5b812013-08-31 17:40:26 +02001634 if( dec_msglen != olen )
1635 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001636 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001637 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001638 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001639
1640#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001641 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1642 {
1643 /*
1644 * Save IV in SSL3 and TLS1
1645 */
1646 memcpy( ssl->transform_in->iv_dec,
1647 ssl->transform_in->cipher_ctx_dec.iv,
1648 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001650#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
1652 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001653
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001654 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
1655 mac_order == MAC_PLAINTEXT )
Paul Bakker45829992013-01-03 14:52:21 +01001656 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001657#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001658 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1659 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001660#endif
Paul Bakker45829992013-01-03 14:52:21 +01001661 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001662 correct = 0;
1663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001665#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1667 {
Paul Bakker48916f92012-09-16 19:57:18 +00001668 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001669 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001670#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1672 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001673 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001674#endif
Paul Bakker45829992013-01-03 14:52:21 +01001675 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676 }
1677 }
1678 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001679#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001680#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1681 defined(POLARSSL_SSL_PROTO_TLS1_2)
1682 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 {
1684 /*
Paul Bakker45829992013-01-03 14:52:21 +01001685 * TLSv1+: always check the padding up to the first failure
1686 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001688 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001689 size_t padding_idx = ssl->in_msglen - padlen - 1;
1690
Paul Bakker956c9e02013-12-19 14:42:28 +01001691 /*
1692 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001693 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001694 *
Paul Bakker61885c72014-04-25 12:59:03 +02001695 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1696 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001697 *
1698 * In both cases we reset padding_idx to a safe value (0) to
1699 * prevent out-of-buffer reads.
1700 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001701 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001702 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1703 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001704
1705 padding_idx *= correct;
1706
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001707 for( i = 1; i <= 256; i++ )
1708 {
1709 real_count &= ( i <= padlen );
1710 pad_count += real_count *
1711 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1712 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001713
1714 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001715
Paul Bakkerd66f0702013-01-31 16:57:45 +01001716#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001717 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001718 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001719#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001720 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001722 else
1723#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1724 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001725 {
1726 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001727 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001728 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001729
1730 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001732 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001733#endif /* POLARSSL_CIPHER_MODE_CBC &&
1734 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001735 {
1736 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001737 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
1740 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1741 ssl->in_msg, ssl->in_msglen );
1742
1743 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001744 * Authenticate if not done yet.
1745 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001747#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1748 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1749 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001750 if( mac_order == MAC_PLAINTEXT )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001751 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001752 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1753
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001754 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001755
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001756 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1757 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001759 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001760
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001761#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001762 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1763 {
1764 ssl_mac( &ssl->transform_in->md_ctx_dec,
1765 ssl->transform_in->mac_dec,
1766 ssl->in_msg, ssl->in_msglen,
1767 ssl->in_ctr, ssl->in_msgtype );
1768 }
1769 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001770#endif /* POLARSSL_SSL_PROTO_SSL3 */
1771#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001772 defined(POLARSSL_SSL_PROTO_TLS1_2)
1773 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1774 {
1775 /*
1776 * Process MAC and always update for padlen afterwards to make
1777 * total time independent of padlen
1778 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001779 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001780 *
1781 * Known timing attacks:
1782 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1783 *
1784 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1785 * correctly. (We round down instead of up, so -56 is the correct
1786 * value for our calculations instead of -55)
1787 */
1788 size_t j, extra_run = 0;
1789 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1790 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001791
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001792 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001793
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001794 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1795 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1796 ssl->in_msglen );
1797 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1798 ssl->in_msg + ssl->in_msglen );
1799 for( j = 0; j < extra_run; j++ )
1800 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001801
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001802 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1803 }
1804 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001805#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001806 POLARSSL_SSL_PROTO_TLS1_2 */
1807 {
1808 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001809 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001810 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001812 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1813 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1814 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001815
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001816 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001817 ssl->transform_in->maclen ) != 0 )
1818 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001819#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001820 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001821#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001822 correct = 0;
1823 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001825 /*
1826 * Finally check the correct flag
1827 */
1828 if( correct == 0 )
1829 return( POLARSSL_ERR_SSL_INVALID_MAC );
1830 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001831#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
1833 if( ssl->in_msglen == 0 )
1834 {
1835 ssl->nb_zero++;
1836
1837 /*
1838 * Three or more empty messages may be a DoS attack
1839 * (excessive CPU consumption).
1840 */
1841 if( ssl->nb_zero > 3 )
1842 {
1843 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1844 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001845 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 }
1847 }
1848 else
1849 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001850
Paul Bakker23986e52011-04-24 08:57:21 +00001851 for( i = 8; i > 0; i-- )
1852 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 break;
1854
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001855 /* The loops goes to its end iff the counter is wrapping */
1856 if( i == 0 )
1857 {
1858 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1859 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1860 }
1861
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1863
1864 return( 0 );
1865}
1866
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001867#undef MAC_NONE
1868#undef MAC_PLAINTEXT
1869#undef MAC_CIPHERTEXT
1870
Paul Bakker2770fbd2012-07-03 13:30:23 +00001871#if defined(POLARSSL_ZLIB_SUPPORT)
1872/*
1873 * Compression/decompression functions
1874 */
1875static int ssl_compress_buf( ssl_context *ssl )
1876{
1877 int ret;
1878 unsigned char *msg_post = ssl->out_msg;
1879 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001880 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001881
1882 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1883
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001884 if( len_pre == 0 )
1885 return( 0 );
1886
Paul Bakker2770fbd2012-07-03 13:30:23 +00001887 memcpy( msg_pre, ssl->out_msg, len_pre );
1888
1889 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1890 ssl->out_msglen ) );
1891
1892 SSL_DEBUG_BUF( 4, "before compression: output payload",
1893 ssl->out_msg, ssl->out_msglen );
1894
Paul Bakker48916f92012-09-16 19:57:18 +00001895 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1896 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1897 ssl->transform_out->ctx_deflate.next_out = msg_post;
1898 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001899
Paul Bakker48916f92012-09-16 19:57:18 +00001900 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001901 if( ret != Z_OK )
1902 {
1903 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1904 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1905 }
1906
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001907 ssl->out_msglen = SSL_BUFFER_LEN -
1908 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001909
Paul Bakker2770fbd2012-07-03 13:30:23 +00001910 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1911 ssl->out_msglen ) );
1912
1913 SSL_DEBUG_BUF( 4, "after compression: output payload",
1914 ssl->out_msg, ssl->out_msglen );
1915
1916 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1917
1918 return( 0 );
1919}
1920
1921static int ssl_decompress_buf( ssl_context *ssl )
1922{
1923 int ret;
1924 unsigned char *msg_post = ssl->in_msg;
1925 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001926 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001927
1928 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1929
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001930 if( len_pre == 0 )
1931 return( 0 );
1932
Paul Bakker2770fbd2012-07-03 13:30:23 +00001933 memcpy( msg_pre, ssl->in_msg, len_pre );
1934
1935 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1936 ssl->in_msglen ) );
1937
1938 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1939 ssl->in_msg, ssl->in_msglen );
1940
Paul Bakker48916f92012-09-16 19:57:18 +00001941 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1942 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1943 ssl->transform_in->ctx_inflate.next_out = msg_post;
1944 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001945
Paul Bakker48916f92012-09-16 19:57:18 +00001946 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001947 if( ret != Z_OK )
1948 {
1949 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1950 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1951 }
1952
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001953 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1954 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001955
Paul Bakker2770fbd2012-07-03 13:30:23 +00001956 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1957 ssl->in_msglen ) );
1958
1959 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1960 ssl->in_msg, ssl->in_msglen );
1961
1962 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1963
1964 return( 0 );
1965}
1966#endif /* POLARSSL_ZLIB_SUPPORT */
1967
Paul Bakker5121ce52009-01-03 21:22:43 +00001968/*
1969 * Fill the input message buffer
1970 */
Paul Bakker23986e52011-04-24 08:57:21 +00001971int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001972{
Paul Bakker23986e52011-04-24 08:57:21 +00001973 int ret;
1974 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1977
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001978 if( nb_want > SSL_BUFFER_LEN - 8 )
1979 {
1980 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1981 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1982 }
1983
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 while( ssl->in_left < nb_want )
1985 {
1986 len = nb_want - ssl->in_left;
1987 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1988
1989 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1990 ssl->in_left, nb_want ) );
1991 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1992
Paul Bakker831a7552011-05-18 13:32:51 +00001993 if( ret == 0 )
1994 return( POLARSSL_ERR_SSL_CONN_EOF );
1995
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 if( ret < 0 )
1997 return( ret );
1998
1999 ssl->in_left += ret;
2000 }
2001
2002 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2003
2004 return( 0 );
2005}
2006
2007/*
2008 * Flush any data not yet written
2009 */
2010int ssl_flush_output( ssl_context *ssl )
2011{
2012 int ret;
2013 unsigned char *buf;
2014
2015 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2016
2017 while( ssl->out_left > 0 )
2018 {
2019 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2020 5 + ssl->out_msglen, ssl->out_left ) );
2021
Paul Bakker5bd42292012-12-19 14:40:42 +01002022 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002024
Paul Bakker5121ce52009-01-03 21:22:43 +00002025 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2026
2027 if( ret <= 0 )
2028 return( ret );
2029
2030 ssl->out_left -= ret;
2031 }
2032
2033 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2034
2035 return( 0 );
2036}
2037
2038/*
2039 * Record layer functions
2040 */
2041int ssl_write_record( ssl_context *ssl )
2042{
Paul Bakker05ef8352012-05-08 09:17:57 +00002043 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002044 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002045
2046 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2047
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2049 {
2050 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2051 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2052 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2053
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002054 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2055 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002056 }
2057
Paul Bakker2770fbd2012-07-03 13:30:23 +00002058#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002059 if( ssl->transform_out != NULL &&
2060 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002061 {
2062 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2063 {
2064 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2065 return( ret );
2066 }
2067
2068 len = ssl->out_msglen;
2069 }
2070#endif /*POLARSSL_ZLIB_SUPPORT */
2071
Paul Bakker05ef8352012-05-08 09:17:57 +00002072#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002073 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002075 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002076
Paul Bakker05ef8352012-05-08 09:17:57 +00002077 ret = ssl_hw_record_write( ssl );
2078 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2079 {
2080 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002081 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002082 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002083
2084 if( ret == 0 )
2085 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002086 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002087#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002088 if( !done )
2089 {
2090 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2091 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2092 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2094 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002095
Paul Bakker48916f92012-09-16 19:57:18 +00002096 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002097 {
2098 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2099 {
2100 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2101 return( ret );
2102 }
2103
2104 len = ssl->out_msglen;
2105 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2106 ssl->out_hdr[4] = (unsigned char)( len );
2107 }
2108
2109 ssl->out_left = 5 + ssl->out_msglen;
2110
2111 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2112 "version = [%d:%d], msglen = %d",
2113 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2114 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2115
2116 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002117 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 }
2119
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2121 {
2122 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2123 return( ret );
2124 }
2125
2126 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2127
2128 return( 0 );
2129}
2130
2131int ssl_read_record( ssl_context *ssl )
2132{
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002134
2135 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2136
2137 if( ssl->in_hslen != 0 &&
2138 ssl->in_hslen < ssl->in_msglen )
2139 {
2140 /*
2141 * Get next Handshake message in the current record
2142 */
2143 ssl->in_msglen -= ssl->in_hslen;
2144
Paul Bakker8934a982011-08-05 11:11:53 +00002145 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002146 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
2148 ssl->in_hslen = 4;
2149 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2150
2151 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2152 " %d, type = %d, hslen = %d",
2153 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2154
2155 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2156 {
2157 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002158 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 }
2160
2161 if( ssl->in_msglen < ssl->in_hslen )
2162 {
2163 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002164 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 }
2166
Paul Bakker4224bc02014-04-08 14:36:50 +02002167 if( ssl->state != SSL_HANDSHAKE_OVER )
2168 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 return( 0 );
2171 }
2172
2173 ssl->in_hslen = 0;
2174
2175 /*
2176 * Read the record header and validate it
2177 */
2178 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2179 {
2180 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2181 return( ret );
2182 }
2183
2184 ssl->in_msgtype = ssl->in_hdr[0];
2185 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2186
2187 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2188 "version = [%d:%d], msglen = %d",
2189 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2190 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2191
2192 if( ssl->in_hdr[1] != ssl->major_ver )
2193 {
2194 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002195 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002196 }
2197
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002198 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002199 {
2200 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002201 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002202 }
2203
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002204 /* Sanity check (outer boundaries) */
2205 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2206 {
2207 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2209 }
2210
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002212 * Make sure the message length is acceptable for the current transform
2213 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002214 */
Paul Bakker48916f92012-09-16 19:57:18 +00002215 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002217 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 {
2219 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002220 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002221 }
2222 }
2223 else
2224 {
Paul Bakker48916f92012-09-16 19:57:18 +00002225 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 {
2227 SSL_DEBUG_MSG( 1, ( "bad message 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 Bakkerd2f068e2013-08-27 21:19:20 +02002231#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002233 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 {
2235 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002236 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002238#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002239
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002240#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2241 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002242 /*
2243 * TLS encrypted messages can have up to 256 bytes of padding
2244 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002245 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002246 ssl->in_msglen > ssl->transform_in->minlen +
2247 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
2249 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002250 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002252#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
2255 /*
2256 * Read and optionally decrypt the message contents
2257 */
2258 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2259 {
2260 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2261 return( ret );
2262 }
2263
2264 SSL_DEBUG_BUF( 4, "input record from network",
2265 ssl->in_hdr, 5 + ssl->in_msglen );
2266
Paul Bakker05ef8352012-05-08 09:17:57 +00002267#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002268 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002269 {
2270 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2271
2272 ret = ssl_hw_record_read( ssl );
2273 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2274 {
2275 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002276 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002277 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002278
2279 if( ret == 0 )
2280 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002281 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002282#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002283 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002284 {
2285 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2286 {
Paul Bakker40865c82013-01-31 17:13:13 +01002287#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2288 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2289 {
2290 ssl_send_alert_message( ssl,
2291 SSL_ALERT_LEVEL_FATAL,
2292 SSL_ALERT_MSG_BAD_RECORD_MAC );
2293 }
2294#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2296 return( ret );
2297 }
2298
2299 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2300 ssl->in_msg, ssl->in_msglen );
2301
2302 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2303 {
2304 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002305 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002306 }
2307 }
2308
Paul Bakker2770fbd2012-07-03 13:30:23 +00002309#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002310 if( ssl->transform_in != NULL &&
2311 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002312 {
2313 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2314 {
2315 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2316 return( ret );
2317 }
2318
2319 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2320 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2321 }
2322#endif /* POLARSSL_ZLIB_SUPPORT */
2323
Paul Bakker0a925182012-04-16 06:46:41 +00002324 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2325 ssl->in_msgtype != SSL_MSG_ALERT &&
2326 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2327 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2328 {
2329 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2330
Paul Bakker48916f92012-09-16 19:57:18 +00002331 if( ( ret = ssl_send_alert_message( ssl,
2332 SSL_ALERT_LEVEL_FATAL,
2333 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002334 {
2335 return( ret );
2336 }
2337
2338 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2339 }
2340
Paul Bakker5121ce52009-01-03 21:22:43 +00002341 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2342 {
2343 ssl->in_hslen = 4;
2344 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2345
2346 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2347 " %d, type = %d, hslen = %d",
2348 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2349
2350 /*
2351 * Additional checks to validate the handshake header
2352 */
2353 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2354 {
2355 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002356 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002357 }
2358
2359 if( ssl->in_msglen < ssl->in_hslen )
2360 {
2361 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002362 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 }
2364
Paul Bakker48916f92012-09-16 19:57:18 +00002365 if( ssl->state != SSL_HANDSHAKE_OVER )
2366 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 }
2368
2369 if( ssl->in_msgtype == SSL_MSG_ALERT )
2370 {
2371 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2372 ssl->in_msg[0], ssl->in_msg[1] ) );
2373
2374 /*
2375 * Ignore non-fatal alerts, except close_notify
2376 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002377 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002378 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002379 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2380 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002381 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 }
2383
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002384 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2385 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 {
2387 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002388 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 }
2390 }
2391
2392 ssl->in_left = 0;
2393
2394 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2395
2396 return( 0 );
2397}
2398
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002399int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2400{
2401 int ret;
2402
2403 if( ( ret = ssl_send_alert_message( ssl,
2404 SSL_ALERT_LEVEL_FATAL,
2405 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2406 {
2407 return( ret );
2408 }
2409
2410 return( 0 );
2411}
2412
Paul Bakker0a925182012-04-16 06:46:41 +00002413int ssl_send_alert_message( ssl_context *ssl,
2414 unsigned char level,
2415 unsigned char message )
2416{
2417 int ret;
2418
2419 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2420
2421 ssl->out_msgtype = SSL_MSG_ALERT;
2422 ssl->out_msglen = 2;
2423 ssl->out_msg[0] = level;
2424 ssl->out_msg[1] = message;
2425
2426 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2427 {
2428 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2429 return( ret );
2430 }
2431
2432 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2433
2434 return( 0 );
2435}
2436
Paul Bakker5121ce52009-01-03 21:22:43 +00002437/*
2438 * Handshake functions
2439 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002440#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2441 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2442 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2443 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2444 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2445 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2446 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002447int ssl_write_certificate( ssl_context *ssl )
2448{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002449 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
2451 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2452
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002453 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002454 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2455 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002456 {
2457 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2458 ssl->state++;
2459 return( 0 );
2460 }
2461
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002462 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2463 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002464}
2465
2466int ssl_parse_certificate( ssl_context *ssl )
2467{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2469
2470 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2471
2472 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002473 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2474 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002475 {
2476 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2477 ssl->state++;
2478 return( 0 );
2479 }
2480
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002481 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2482 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483}
2484#else
2485int ssl_write_certificate( ssl_context *ssl )
2486{
2487 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2488 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002489 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2491
2492 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2493
2494 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002495 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2496 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497 {
2498 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2499 ssl->state++;
2500 return( 0 );
2501 }
2502
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 if( ssl->endpoint == SSL_IS_CLIENT )
2504 {
2505 if( ssl->client_auth == 0 )
2506 {
2507 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2508 ssl->state++;
2509 return( 0 );
2510 }
2511
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002512#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 /*
2514 * If using SSLv3 and got no cert, send an Alert message
2515 * (otherwise an empty Certificate message will be sent).
2516 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002517 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2519 {
2520 ssl->out_msglen = 2;
2521 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002522 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2523 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
2525 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2526 goto write_msg;
2527 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002528#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 }
2530 else /* SSL_IS_SERVER */
2531 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002532 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002533 {
2534 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002535 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537 }
2538
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002539 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
2541 /*
2542 * 0 . 0 handshake type
2543 * 1 . 3 handshake length
2544 * 4 . 6 length of all certs
2545 * 7 . 9 length of cert. 1
2546 * 10 . n-1 peer certificate
2547 * n . n+2 length of cert. 2
2548 * n+3 . ... upper level cert, etc.
2549 */
2550 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002551 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
Paul Bakker29087132010-03-21 21:03:34 +00002553 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002554 {
2555 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002556 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002557 {
2558 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2559 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002560 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002561 }
2562
2563 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2564 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2565 ssl->out_msg[i + 2] = (unsigned char)( n );
2566
2567 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2568 i += n; crt = crt->next;
2569 }
2570
2571 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2572 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2573 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2574
2575 ssl->out_msglen = i;
2576 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2577 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2578
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002579#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002580write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002581#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
2583 ssl->state++;
2584
2585 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2586 {
2587 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2588 return( ret );
2589 }
2590
2591 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2592
Paul Bakkered27a042013-04-18 22:46:23 +02002593 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594}
2595
2596int ssl_parse_certificate( ssl_context *ssl )
2597{
Paul Bakkered27a042013-04-18 22:46:23 +02002598 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002599 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002600 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2603
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002604 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002605 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2606 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002607 {
2608 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2609 ssl->state++;
2610 return( 0 );
2611 }
2612
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002614 ( ssl->authmode == SSL_VERIFY_NONE ||
2615 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002616 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002617 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2619 ssl->state++;
2620 return( 0 );
2621 }
2622
2623 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2624 {
2625 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2626 return( ret );
2627 }
2628
2629 ssl->state++;
2630
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002631#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002632 /*
2633 * Check if the client sent an empty certificate
2634 */
2635 if( ssl->endpoint == SSL_IS_SERVER &&
2636 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2637 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002638 if( ssl->in_msglen == 2 &&
2639 ssl->in_msgtype == SSL_MSG_ALERT &&
2640 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2641 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 {
2643 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2644
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002645 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002646 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2647 return( 0 );
2648 else
Paul Bakker40e46942009-01-03 21:51:57 +00002649 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 }
2651 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002652#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002653
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002654#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2655 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 if( ssl->endpoint == SSL_IS_SERVER &&
2657 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2658 {
2659 if( ssl->in_hslen == 7 &&
2660 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2661 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2662 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2663 {
2664 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2665
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002666 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002667 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002668 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002669 else
2670 return( 0 );
2671 }
2672 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002673#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2674 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002675
2676 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2677 {
2678 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002679 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002680 }
2681
2682 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2683 {
2684 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002685 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 }
2687
2688 /*
2689 * Same message structure as in ssl_write_certificate()
2690 */
2691 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2692
2693 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2694 {
2695 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002696 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002697 }
2698
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002699 /* In case we tried to reuse a session but it failed */
2700 if( ssl->session_negotiate->peer_cert != NULL )
2701 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002702 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002703 polarssl_free( ssl->session_negotiate->peer_cert );
2704 }
2705
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002706 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2707 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 {
2709 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002710 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002711 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
Paul Bakkerb6b09562013-09-18 14:17:41 +02002714 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716 i = 7;
2717
2718 while( i < ssl->in_hslen )
2719 {
2720 if( ssl->in_msg[i] != 0 )
2721 {
2722 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002723 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
2725
2726 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2727 | (unsigned int) ssl->in_msg[i + 2];
2728 i += 3;
2729
2730 if( n < 128 || i + n > ssl->in_hslen )
2731 {
2732 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002733 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
2735
Paul Bakkerddf26b42013-09-18 13:46:23 +02002736 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2737 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 if( ret != 0 )
2739 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002740 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 return( ret );
2742 }
2743
2744 i += n;
2745 }
2746
Paul Bakker48916f92012-09-16 19:57:18 +00002747 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002749 /*
2750 * On client, make sure the server cert doesn't change during renego to
2751 * avoid "triple handshake" attack: https://secure-resumption.com/
2752 */
2753 if( ssl->endpoint == SSL_IS_CLIENT &&
2754 ssl->renegotiation == SSL_RENEGOTIATION )
2755 {
2756 if( ssl->session->peer_cert == NULL )
2757 {
2758 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2759 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2760 }
2761
2762 if( ssl->session->peer_cert->raw.len !=
2763 ssl->session_negotiate->peer_cert->raw.len ||
2764 memcmp( ssl->session->peer_cert->raw.p,
2765 ssl->session_negotiate->peer_cert->raw.p,
2766 ssl->session->peer_cert->raw.len ) != 0 )
2767 {
2768 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2769 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2770 }
2771 }
2772
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 if( ssl->authmode != SSL_VERIFY_NONE )
2774 {
2775 if( ssl->ca_chain == NULL )
2776 {
2777 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002778 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002779 }
2780
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002781 /*
2782 * Main check: verify certificate
2783 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002784 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2785 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2786 &ssl->session_negotiate->verify_result,
2787 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788
2789 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002790 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002791 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002792 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002793
2794 /*
2795 * Secondary checks: always done, but change 'ret' only if it was 0
2796 */
2797
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002798#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002799 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002800 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002801
2802 /* If certificate uses an EC key, make sure the curve is OK */
2803 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2804 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2805 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002806 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002807 if( ret == 0 )
2808 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002809 }
2810 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002811#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002812
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002813 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2814 ciphersuite_info,
2815 ! ssl->endpoint ) != 0 )
2816 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002817 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002818 if( ret == 0 )
2819 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2820 }
2821
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2823 ret = 0;
2824 }
2825
2826 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2827
2828 return( ret );
2829}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002830#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2831 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2832 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2833 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2834 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2835 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2836 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
2838int ssl_write_change_cipher_spec( ssl_context *ssl )
2839{
2840 int ret;
2841
2842 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2843
2844 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2845 ssl->out_msglen = 1;
2846 ssl->out_msg[0] = 1;
2847
Paul Bakker5121ce52009-01-03 21:22:43 +00002848 ssl->state++;
2849
2850 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2851 {
2852 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2853 return( ret );
2854 }
2855
2856 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2857
2858 return( 0 );
2859}
2860
2861int ssl_parse_change_cipher_spec( ssl_context *ssl )
2862{
2863 int ret;
2864
2865 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2866
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2868 {
2869 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2870 return( ret );
2871 }
2872
2873 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2874 {
2875 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002876 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 }
2878
2879 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2880 {
2881 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002882 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 }
2884
2885 ssl->state++;
2886
2887 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2888
2889 return( 0 );
2890}
2891
Paul Bakker41c83d32013-03-20 14:39:14 +01002892void ssl_optimize_checksum( ssl_context *ssl,
2893 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002894{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002895 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002896
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002897#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2898 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002899 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002900 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002901 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002902#endif
2903#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2904#if defined(POLARSSL_SHA512_C)
2905 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2906 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2907 else
2908#endif
2909#if defined(POLARSSL_SHA256_C)
2910 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002911 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002912 else
2913#endif
2914#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002915 {
2916 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002918 }
Paul Bakker380da532012-04-18 16:10:25 +00002919}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002920
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002921static void ssl_update_checksum_start( ssl_context *ssl,
2922 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002923{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2925 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002926 md5_update( &ssl->handshake->fin_md5 , buf, len );
2927 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002928#endif
2929#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2930#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002931 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002932#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002933#if defined(POLARSSL_SHA512_C)
2934 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002935#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002936#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002937}
2938
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002939#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2940 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002941static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2942 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002943{
Paul Bakker48916f92012-09-16 19:57:18 +00002944 md5_update( &ssl->handshake->fin_md5 , buf, len );
2945 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002946}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002947#endif
Paul Bakker380da532012-04-18 16:10:25 +00002948
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002949#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2950#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002951static void ssl_update_checksum_sha256( ssl_context *ssl,
2952 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002953{
Paul Bakker9e36f042013-06-30 14:34:05 +02002954 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002955}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002956#endif
Paul Bakker380da532012-04-18 16:10:25 +00002957
Paul Bakker9e36f042013-06-30 14:34:05 +02002958#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002959static void ssl_update_checksum_sha384( ssl_context *ssl,
2960 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002961{
Paul Bakker9e36f042013-06-30 14:34:05 +02002962 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002963}
Paul Bakker769075d2012-11-24 11:26:46 +01002964#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002966
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002968static void ssl_calc_finished_ssl(
2969 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002970{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002971 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972 md5_context md5;
2973 sha1_context sha1;
2974
Paul Bakker5121ce52009-01-03 21:22:43 +00002975 unsigned char padbuf[48];
2976 unsigned char md5sum[16];
2977 unsigned char sha1sum[20];
2978
Paul Bakker48916f92012-09-16 19:57:18 +00002979 ssl_session *session = ssl->session_negotiate;
2980 if( !session )
2981 session = ssl->session;
2982
Paul Bakker1ef83d62012-04-11 12:09:53 +00002983 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2984
Paul Bakker48916f92012-09-16 19:57:18 +00002985 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2986 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
2988 /*
2989 * SSLv3:
2990 * hash =
2991 * MD5( master + pad2 +
2992 * MD5( handshake + sender + master + pad1 ) )
2993 * + SHA1( master + pad2 +
2994 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002995 */
2996
Paul Bakker90995b52013-06-24 19:20:35 +02002997#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003000#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Paul Bakker90995b52013-06-24 19:20:35 +02003002#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003003 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003005#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003006
Paul Bakker3c2122f2013-06-24 19:03:14 +02003007 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3008 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003009
Paul Bakker1ef83d62012-04-11 12:09:53 +00003010 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003011
Paul Bakker3c2122f2013-06-24 19:03:14 +02003012 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003013 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014 md5_update( &md5, padbuf, 48 );
3015 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakker3c2122f2013-06-24 19:03:14 +02003017 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003018 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019 sha1_update( &sha1, padbuf, 40 );
3020 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Paul Bakker1ef83d62012-04-11 12:09:53 +00003022 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003023
Paul Bakker1ef83d62012-04-11 12:09:53 +00003024 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003025 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003026 md5_update( &md5, padbuf, 48 );
3027 md5_update( &md5, md5sum, 16 );
3028 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003029
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003031 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 sha1_update( &sha1, padbuf , 40 );
3033 sha1_update( &sha1, sha1sum, 20 );
3034 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
Paul Bakker1ef83d62012-04-11 12:09:53 +00003036 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003037
Paul Bakker5b4af392014-06-26 12:09:34 +02003038 md5_free( &md5 );
3039 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003040
Paul Bakker34617722014-06-13 17:20:13 +02003041 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3042 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3043 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044
3045 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3046}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003047#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003048
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003049#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003050static void ssl_calc_finished_tls(
3051 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003052{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003054 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003055 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003057 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker48916f92012-09-16 19:57:18 +00003059 ssl_session *session = ssl->session_negotiate;
3060 if( !session )
3061 session = ssl->session;
3062
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003064
Paul Bakker48916f92012-09-16 19:57:18 +00003065 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3066 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003067
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068 /*
3069 * TLSv1:
3070 * hash = PRF( master, finished_label,
3071 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3072 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003073
Paul Bakker90995b52013-06-24 19:20:35 +02003074#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3076 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003077#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003078
Paul Bakker90995b52013-06-24 19:20:35 +02003079#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3081 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003082#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003083
3084 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003085 ? "client finished"
3086 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003087
3088 md5_finish( &md5, padbuf );
3089 sha1_finish( &sha1, padbuf + 16 );
3090
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003091 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003092 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003093
3094 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3095
Paul Bakker5b4af392014-06-26 12:09:34 +02003096 md5_free( &md5 );
3097 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003098
Paul Bakker34617722014-06-13 17:20:13 +02003099 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003100
3101 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3102}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003103#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003105#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3106#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003107static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003108 ssl_context *ssl, unsigned char *buf, int from )
3109{
3110 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003111 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003112 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003113 unsigned char padbuf[32];
3114
Paul Bakker48916f92012-09-16 19:57:18 +00003115 ssl_session *session = ssl->session_negotiate;
3116 if( !session )
3117 session = ssl->session;
3118
Paul Bakker380da532012-04-18 16:10:25 +00003119 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120
Paul Bakker9e36f042013-06-30 14:34:05 +02003121 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003122
3123 /*
3124 * TLSv1.2:
3125 * hash = PRF( master, finished_label,
3126 * Hash( handshake ) )[0.11]
3127 */
3128
Paul Bakker9e36f042013-06-30 14:34:05 +02003129#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003130 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003131 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003132#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003133
3134 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003135 ? "client finished"
3136 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003137
Paul Bakker9e36f042013-06-30 14:34:05 +02003138 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003139
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003140 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003141 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003142
3143 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3144
Paul Bakker5b4af392014-06-26 12:09:34 +02003145 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003146
Paul Bakker34617722014-06-13 17:20:13 +02003147 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003148
3149 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3150}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003151#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003152
Paul Bakker9e36f042013-06-30 14:34:05 +02003153#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003154static void ssl_calc_finished_tls_sha384(
3155 ssl_context *ssl, unsigned char *buf, int from )
3156{
3157 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003158 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003159 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003160 unsigned char padbuf[48];
3161
Paul Bakker48916f92012-09-16 19:57:18 +00003162 ssl_session *session = ssl->session_negotiate;
3163 if( !session )
3164 session = ssl->session;
3165
Paul Bakker380da532012-04-18 16:10:25 +00003166 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003167
Paul Bakker9e36f042013-06-30 14:34:05 +02003168 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003169
3170 /*
3171 * TLSv1.2:
3172 * hash = PRF( master, finished_label,
3173 * Hash( handshake ) )[0.11]
3174 */
3175
Paul Bakker9e36f042013-06-30 14:34:05 +02003176#if !defined(POLARSSL_SHA512_ALT)
3177 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3178 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003179#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003180
3181 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003182 ? "client finished"
3183 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003184
Paul Bakker9e36f042013-06-30 14:34:05 +02003185 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003186
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003187 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003188 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003189
3190 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3191
Paul Bakker5b4af392014-06-26 12:09:34 +02003192 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003193
Paul Bakker34617722014-06-13 17:20:13 +02003194 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003195
3196 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3197}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003198#endif /* POLARSSL_SHA512_C */
3199#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003200
Paul Bakker48916f92012-09-16 19:57:18 +00003201void ssl_handshake_wrapup( ssl_context *ssl )
3202{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003203 int resume = ssl->handshake->resume;
3204
Paul Bakker48916f92012-09-16 19:57:18 +00003205 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3206
3207 /*
3208 * Free our handshake params
3209 */
3210 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003211 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003212 ssl->handshake = NULL;
3213
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003214 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003215 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003216 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003217 ssl->renego_records_seen = 0;
3218 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003219
Paul Bakker48916f92012-09-16 19:57:18 +00003220 /*
3221 * Switch in our now active transform context
3222 */
3223 if( ssl->transform )
3224 {
3225 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003226 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003227 }
3228 ssl->transform = ssl->transform_negotiate;
3229 ssl->transform_negotiate = NULL;
3230
Paul Bakker0a597072012-09-25 21:55:46 +00003231 if( ssl->session )
3232 {
3233 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003234 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003235 }
3236 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003237 ssl->session_negotiate = NULL;
3238
Paul Bakker0a597072012-09-25 21:55:46 +00003239 /*
3240 * Add cache entry
3241 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003242 if( ssl->f_set_cache != NULL &&
3243 ssl->session->length != 0 &&
3244 resume == 0 )
3245 {
Paul Bakker0a597072012-09-25 21:55:46 +00003246 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3247 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003248 }
Paul Bakker0a597072012-09-25 21:55:46 +00003249
Paul Bakker48916f92012-09-16 19:57:18 +00003250 ssl->state++;
3251
3252 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3253}
3254
Paul Bakker1ef83d62012-04-11 12:09:53 +00003255int ssl_write_finished( ssl_context *ssl )
3256{
3257 int ret, hash_len;
3258
3259 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3260
Paul Bakker92be97b2013-01-02 17:30:03 +01003261 /*
3262 * Set the out_msg pointer to the correct location based on IV length
3263 */
3264 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3265 {
3266 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3267 ssl->transform_negotiate->fixed_ivlen;
3268 }
3269 else
3270 ssl->out_msg = ssl->out_iv;
3271
Paul Bakker48916f92012-09-16 19:57:18 +00003272 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003273
3274 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003275 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3276
Paul Bakker48916f92012-09-16 19:57:18 +00003277 ssl->verify_data_len = hash_len;
3278 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3279
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 ssl->out_msglen = 4 + hash_len;
3281 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3282 ssl->out_msg[0] = SSL_HS_FINISHED;
3283
3284 /*
3285 * In case of session resuming, invert the client and server
3286 * ChangeCipherSpec messages order.
3287 */
Paul Bakker0a597072012-09-25 21:55:46 +00003288 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 {
3290 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003291 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003292 else
3293 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3294 }
3295 else
3296 ssl->state++;
3297
Paul Bakker48916f92012-09-16 19:57:18 +00003298 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003299 * Switch to our negotiated transform and session parameters for outbound
3300 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003301 */
3302 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3303 ssl->transform_out = ssl->transform_negotiate;
3304 ssl->session_out = ssl->session_negotiate;
3305 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003306
Paul Bakker07eb38b2012-12-19 14:42:06 +01003307#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003308 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003309 {
3310 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3311 {
3312 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3313 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3314 }
3315 }
3316#endif
3317
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3319 {
3320 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3321 return( ret );
3322 }
3323
3324 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3325
3326 return( 0 );
3327}
3328
3329int ssl_parse_finished( ssl_context *ssl )
3330{
Paul Bakker23986e52011-04-24 08:57:21 +00003331 int ret;
3332 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 unsigned char buf[36];
3334
3335 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3336
Paul Bakker48916f92012-09-16 19:57:18 +00003337 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Paul Bakker48916f92012-09-16 19:57:18 +00003339 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003340 * Switch to our negotiated transform and session parameters for inbound
3341 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003342 */
3343 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3344 ssl->transform_in = ssl->transform_negotiate;
3345 ssl->session_in = ssl->session_negotiate;
3346 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003347
Paul Bakker92be97b2013-01-02 17:30:03 +01003348 /*
3349 * Set the in_msg pointer to the correct location based on IV length
3350 */
3351 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3352 {
3353 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3354 ssl->transform_negotiate->fixed_ivlen;
3355 }
3356 else
3357 ssl->in_msg = ssl->in_iv;
3358
Paul Bakker07eb38b2012-12-19 14:42:06 +01003359#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003360 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003361 {
3362 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3363 {
3364 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3365 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3366 }
3367 }
3368#endif
3369
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3371 {
3372 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3373 return( ret );
3374 }
3375
3376 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3377 {
3378 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003379 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 }
3381
Paul Bakker1ef83d62012-04-11 12:09:53 +00003382 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003383 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3384
3385 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3386 ssl->in_hslen != 4 + hash_len )
3387 {
3388 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003389 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 }
3391
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003392 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003393 {
3394 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003395 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 }
3397
Paul Bakker48916f92012-09-16 19:57:18 +00003398 ssl->verify_data_len = hash_len;
3399 memcpy( ssl->peer_verify_data, buf, hash_len );
3400
Paul Bakker0a597072012-09-25 21:55:46 +00003401 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 {
3403 if( ssl->endpoint == SSL_IS_CLIENT )
3404 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3405
3406 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003407 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 }
3409 else
3410 ssl->state++;
3411
3412 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3413
3414 return( 0 );
3415}
3416
Paul Bakker968afaa2014-07-09 11:09:24 +02003417static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003418{
3419 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3420
3421#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3422 defined(POLARSSL_SSL_PROTO_TLS1_1)
3423 md5_init( &handshake->fin_md5 );
3424 sha1_init( &handshake->fin_sha1 );
3425 md5_starts( &handshake->fin_md5 );
3426 sha1_starts( &handshake->fin_sha1 );
3427#endif
3428#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3429#if defined(POLARSSL_SHA256_C)
3430 sha256_init( &handshake->fin_sha256 );
3431 sha256_starts( &handshake->fin_sha256, 0 );
3432#endif
3433#if defined(POLARSSL_SHA512_C)
3434 sha512_init( &handshake->fin_sha512 );
3435 sha512_starts( &handshake->fin_sha512, 1 );
3436#endif
3437#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3438
3439 handshake->update_checksum = ssl_update_checksum_start;
3440 handshake->sig_alg = SSL_HASH_SHA1;
3441
3442#if defined(POLARSSL_DHM_C)
3443 dhm_init( &handshake->dhm_ctx );
3444#endif
3445#if defined(POLARSSL_ECDH_C)
3446 ecdh_init( &handshake->ecdh_ctx );
3447#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003448}
3449
3450static void ssl_transform_init( ssl_transform *transform )
3451{
3452 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003453
3454 cipher_init( &transform->cipher_ctx_enc );
3455 cipher_init( &transform->cipher_ctx_dec );
3456
3457 md_init( &transform->md_ctx_enc );
3458 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003459}
3460
3461void ssl_session_init( ssl_session *session )
3462{
3463 memset( session, 0, sizeof(ssl_session) );
3464}
3465
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003466static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003467{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003468 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003469 if( ssl->transform_negotiate )
3470 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003471 if( ssl->session_negotiate )
3472 ssl_session_free( ssl->session_negotiate );
3473 if( ssl->handshake )
3474 ssl_handshake_free( ssl->handshake );
3475
3476 /*
3477 * Either the pointers are now NULL or cleared properly and can be freed.
3478 * Now allocate missing structures.
3479 */
3480 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003481 {
3482 ssl->transform_negotiate =
3483 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3484 }
Paul Bakker48916f92012-09-16 19:57:18 +00003485
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003486 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003487 {
3488 ssl->session_negotiate =
3489 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3490 }
Paul Bakker48916f92012-09-16 19:57:18 +00003491
Paul Bakker82788fb2014-10-20 13:59:19 +02003492 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003493 {
3494 ssl->handshake = (ssl_handshake_params *)
3495 polarssl_malloc( sizeof(ssl_handshake_params) );
3496 }
Paul Bakker48916f92012-09-16 19:57:18 +00003497
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003498 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003499 if( ssl->handshake == NULL ||
3500 ssl->transform_negotiate == NULL ||
3501 ssl->session_negotiate == NULL )
3502 {
3503 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003504
3505 polarssl_free( ssl->handshake );
3506 polarssl_free( ssl->transform_negotiate );
3507 polarssl_free( ssl->session_negotiate );
3508
3509 ssl->handshake = NULL;
3510 ssl->transform_negotiate = NULL;
3511 ssl->session_negotiate = NULL;
3512
Paul Bakker48916f92012-09-16 19:57:18 +00003513 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3514 }
3515
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003516 /* Initialize structures */
3517 ssl_session_init( ssl->session_negotiate );
3518 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003519 ssl_handshake_params_init( ssl->handshake );
3520
3521#if defined(POLARSSL_X509_CRT_PARSE_C)
3522 ssl->handshake->key_cert = ssl->key_cert;
3523#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003524
Paul Bakker48916f92012-09-16 19:57:18 +00003525 return( 0 );
3526}
3527
Paul Bakker5121ce52009-01-03 21:22:43 +00003528/*
3529 * Initialize an SSL context
3530 */
3531int ssl_init( ssl_context *ssl )
3532{
Paul Bakker48916f92012-09-16 19:57:18 +00003533 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003534 int len = SSL_BUFFER_LEN;
3535
3536 memset( ssl, 0, sizeof( ssl_context ) );
3537
Paul Bakker62f2dee2012-09-28 07:31:51 +00003538 /*
3539 * Sane defaults
3540 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003541 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3542 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3543 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3544 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003545
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003546 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003547
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003548 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3549
Paul Bakker62f2dee2012-09-28 07:31:51 +00003550#if defined(POLARSSL_DHM_C)
3551 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3552 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3553 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3554 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3555 {
3556 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3557 return( ret );
3558 }
3559#endif
3560
3561 /*
3562 * Prepare base structures
3563 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003564 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003566 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003567 ssl->in_msg = ssl->in_ctr + 13;
3568
3569 if( ssl->in_ctr == NULL )
3570 {
3571 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003572 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 }
3574
Paul Bakker6e339b52013-07-03 13:37:05 +02003575 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003576 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003577 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003578 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003579
3580 if( ssl->out_ctr == NULL )
3581 {
3582 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003583 polarssl_free( ssl->in_ctr );
3584 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003585 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003586 }
3587
3588 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3589 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3590
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003591#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3592 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3593#endif
3594
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003595#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3596 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3597#endif
3598
Paul Bakker606b4ba2013-08-14 16:52:14 +02003599#if defined(POLARSSL_SSL_SESSION_TICKETS)
3600 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3601#endif
3602
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003603#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003604 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003605#endif
3606
Paul Bakker48916f92012-09-16 19:57:18 +00003607 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3608 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003609
3610 return( 0 );
3611}
3612
3613/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003614 * Reset an initialized and used SSL context for re-use while retaining
3615 * all application-set variables, function pointers and data.
3616 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003617int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003618{
Paul Bakker48916f92012-09-16 19:57:18 +00003619 int ret;
3620
Paul Bakker7eb013f2011-10-06 12:37:39 +00003621 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003622 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3623 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3624
3625 ssl->verify_data_len = 0;
3626 memset( ssl->own_verify_data, 0, 36 );
3627 memset( ssl->peer_verify_data, 0, 36 );
3628
Paul Bakker7eb013f2011-10-06 12:37:39 +00003629 ssl->in_offt = NULL;
3630
Paul Bakker92be97b2013-01-02 17:30:03 +01003631 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003632 ssl->in_msgtype = 0;
3633 ssl->in_msglen = 0;
3634 ssl->in_left = 0;
3635
3636 ssl->in_hslen = 0;
3637 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003638 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003639
Paul Bakker92be97b2013-01-02 17:30:03 +01003640 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003641 ssl->out_msgtype = 0;
3642 ssl->out_msglen = 0;
3643 ssl->out_left = 0;
3644
Paul Bakker48916f92012-09-16 19:57:18 +00003645 ssl->transform_in = NULL;
3646 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003647
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003648 ssl->renego_records_seen = 0;
3649
Paul Bakker7eb013f2011-10-06 12:37:39 +00003650 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3651 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003652
3653#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003654 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003655 {
3656 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003657 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003658 {
3659 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3660 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3661 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003662 }
3663#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003664
Paul Bakker48916f92012-09-16 19:57:18 +00003665 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003666 {
Paul Bakker48916f92012-09-16 19:57:18 +00003667 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003668 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003669 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003670 }
Paul Bakker48916f92012-09-16 19:57:18 +00003671
Paul Bakkerc0463502013-02-14 11:19:38 +01003672 if( ssl->session )
3673 {
3674 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003675 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003676 ssl->session = NULL;
3677 }
3678
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003679#if defined(POLARSSL_SSL_ALPN)
3680 ssl->alpn_chosen = NULL;
3681#endif
3682
Paul Bakker48916f92012-09-16 19:57:18 +00003683 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3684 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003685
3686 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003687}
3688
Paul Bakkera503a632013-08-14 13:48:06 +02003689#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003690static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3691{
3692 aes_free( &tkeys->enc );
3693 aes_free( &tkeys->dec );
3694
3695 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3696}
3697
Paul Bakker7eb013f2011-10-06 12:37:39 +00003698/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003699 * Allocate and initialize ticket keys
3700 */
3701static int ssl_ticket_keys_init( ssl_context *ssl )
3702{
3703 int ret;
3704 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003705 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003706
3707 if( ssl->ticket_keys != NULL )
3708 return( 0 );
3709
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003710 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3711 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003712 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3713
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003714 aes_init( &tkeys->enc );
3715 aes_init( &tkeys->dec );
3716
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003717 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003718 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003719 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003720 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003721 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003722 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003723
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003724 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3725 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3726 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3727 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003728 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003729 polarssl_free( tkeys );
3730 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003731 }
3732
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003733 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003734 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003735 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003736 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003737 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003738 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003739
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003740 ssl->ticket_keys = tkeys;
3741
3742 return( 0 );
3743}
Paul Bakkera503a632013-08-14 13:48:06 +02003744#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003745
3746/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 * SSL set accessors
3748 */
3749void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3750{
3751 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003752
Paul Bakker606b4ba2013-08-14 16:52:14 +02003753#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003754 if( endpoint == SSL_IS_CLIENT )
3755 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003757}
3758
3759void ssl_set_authmode( ssl_context *ssl, int authmode )
3760{
3761 ssl->authmode = authmode;
3762}
3763
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003764#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003765void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003766 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003767 void *p_vrfy )
3768{
3769 ssl->f_vrfy = f_vrfy;
3770 ssl->p_vrfy = p_vrfy;
3771}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003772#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003773
Paul Bakker5121ce52009-01-03 21:22:43 +00003774void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003775 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003776 void *p_rng )
3777{
3778 ssl->f_rng = f_rng;
3779 ssl->p_rng = p_rng;
3780}
3781
3782void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003783 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003784 void *p_dbg )
3785{
3786 ssl->f_dbg = f_dbg;
3787 ssl->p_dbg = p_dbg;
3788}
3789
3790void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003791 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003792 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003793{
3794 ssl->f_recv = f_recv;
3795 ssl->f_send = f_send;
3796 ssl->p_recv = p_recv;
3797 ssl->p_send = p_send;
3798}
3799
Paul Bakker0a597072012-09-25 21:55:46 +00003800void ssl_set_session_cache( ssl_context *ssl,
3801 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3802 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003803{
Paul Bakker0a597072012-09-25 21:55:46 +00003804 ssl->f_get_cache = f_get_cache;
3805 ssl->p_get_cache = p_get_cache;
3806 ssl->f_set_cache = f_set_cache;
3807 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003808}
3809
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003810int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003811{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003812 int ret;
3813
3814 if( ssl == NULL ||
3815 session == NULL ||
3816 ssl->session_negotiate == NULL ||
3817 ssl->endpoint != SSL_IS_CLIENT )
3818 {
3819 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3820 }
3821
3822 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3823 return( ret );
3824
Paul Bakker0a597072012-09-25 21:55:46 +00003825 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003826
3827 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003828}
3829
Paul Bakkerb68cad62012-08-23 08:34:18 +00003830void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003831{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003832 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3833 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3834 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3835 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3836}
3837
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003838void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3839 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003840 int major, int minor )
3841{
3842 if( major != SSL_MAJOR_VERSION_3 )
3843 return;
3844
3845 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3846 return;
3847
3848 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003849}
3850
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003851#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003852/* Add a new (empty) key_cert entry an return a pointer to it */
3853static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3854{
3855 ssl_key_cert *key_cert, *last;
3856
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003857 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3858 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003859 return( NULL );
3860
3861 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3862
3863 /* Append the new key_cert to the (possibly empty) current list */
3864 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003865 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003866 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003867 if( ssl->handshake != NULL )
3868 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003869 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003870 else
3871 {
3872 last = ssl->key_cert;
3873 while( last->next != NULL )
3874 last = last->next;
3875 last->next = key_cert;
3876 }
3877
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003878 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003879}
3880
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003881void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003882 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003883{
3884 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003885 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003886 ssl->peer_cn = peer_cn;
3887}
3888
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003889int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003890 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003891{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003892 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3893
3894 if( key_cert == NULL )
3895 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3896
3897 key_cert->cert = own_cert;
3898 key_cert->key = pk_key;
3899
3900 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003901}
3902
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003903#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003904int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003905 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003906{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003907 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003908 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003909
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003910 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003911 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3912
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003913 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3914 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003915 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003916
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003917 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003918
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003919 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003920 if( ret != 0 )
3921 return( ret );
3922
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003923 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003924 return( ret );
3925
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003926 key_cert->cert = own_cert;
3927 key_cert->key_own_alloc = 1;
3928
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003929 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003930}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003931#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003932
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003933int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003934 void *rsa_key,
3935 rsa_decrypt_func rsa_decrypt,
3936 rsa_sign_func rsa_sign,
3937 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003938{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003939 int ret;
3940 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003941
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003942 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003943 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3944
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003945 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3946 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003947 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003948
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003949 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003950
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003951 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3952 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3953 return( ret );
3954
3955 key_cert->cert = own_cert;
3956 key_cert->key_own_alloc = 1;
3957
3958 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003959}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003960#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003961
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003962#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003963int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3964 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003965{
Paul Bakker6db455e2013-09-18 17:29:31 +02003966 if( psk == NULL || psk_identity == NULL )
3967 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3968
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003969 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003970 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3971
Paul Bakker6db455e2013-09-18 17:29:31 +02003972 if( ssl->psk != NULL )
3973 {
3974 polarssl_free( ssl->psk );
3975 polarssl_free( ssl->psk_identity );
3976 }
3977
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003978 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003979 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003980
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003981 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003982 ssl->psk_identity = (unsigned char *)
3983 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003984
3985 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3986 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3987
3988 memcpy( ssl->psk, psk, ssl->psk_len );
3989 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003990
3991 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003992}
3993
3994void ssl_set_psk_cb( ssl_context *ssl,
3995 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3996 size_t),
3997 void *p_psk )
3998{
3999 ssl->f_psk = f_psk;
4000 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004001}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004002#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004003
Paul Bakker48916f92012-09-16 19:57:18 +00004004#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004005int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004006{
4007 int ret;
4008
Paul Bakker48916f92012-09-16 19:57:18 +00004009 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 {
4011 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4012 return( ret );
4013 }
4014
Paul Bakker48916f92012-09-16 19:57:18 +00004015 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004016 {
4017 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4018 return( ret );
4019 }
4020
4021 return( 0 );
4022}
4023
Paul Bakker1b57b062011-01-06 15:48:19 +00004024int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4025{
4026 int ret;
4027
Paul Bakker66d5d072014-06-17 16:39:18 +02004028 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004029 {
4030 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4031 return( ret );
4032 }
4033
Paul Bakker66d5d072014-06-17 16:39:18 +02004034 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004035 {
4036 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4037 return( ret );
4038 }
4039
4040 return( 0 );
4041}
Paul Bakker48916f92012-09-16 19:57:18 +00004042#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004043
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004044#if defined(POLARSSL_SSL_SET_CURVES)
4045/*
4046 * Set the allowed elliptic curves
4047 */
4048void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4049{
4050 ssl->curve_list = curve_list;
4051}
4052#endif
4053
Paul Bakker0be444a2013-08-27 21:55:01 +02004054#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004055int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004056{
4057 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004058 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004059
4060 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004061
4062 if( ssl->hostname_len + 1 == 0 )
4063 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4064
Paul Bakker6e339b52013-07-03 13:37:05 +02004065 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066
Paul Bakkerb15b8512012-01-13 13:44:06 +00004067 if( ssl->hostname == NULL )
4068 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4069
Paul Bakker3c2122f2013-06-24 19:03:14 +02004070 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004071 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004072
Paul Bakker40ea7de2009-05-03 10:18:48 +00004073 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004074
4075 return( 0 );
4076}
4077
Paul Bakker5701cdc2012-09-27 21:49:42 +00004078void ssl_set_sni( ssl_context *ssl,
4079 int (*f_sni)(void *, ssl_context *,
4080 const unsigned char *, size_t),
4081 void *p_sni )
4082{
4083 ssl->f_sni = f_sni;
4084 ssl->p_sni = p_sni;
4085}
Paul Bakker0be444a2013-08-27 21:55:01 +02004086#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004087
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004088#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004089int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004090{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004091 size_t cur_len, tot_len;
4092 const char **p;
4093
4094 /*
4095 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4096 * truncated". Check lengths now rather than later.
4097 */
4098 tot_len = 0;
4099 for( p = protos; *p != NULL; p++ )
4100 {
4101 cur_len = strlen( *p );
4102 tot_len += cur_len;
4103
4104 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4105 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4106 }
4107
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004108 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004109
4110 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004111}
4112
4113const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4114{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004115 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004116}
4117#endif /* POLARSSL_SSL_ALPN */
4118
Paul Bakker490ecc82011-10-06 13:04:09 +00004119void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4120{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004121 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4122 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4123 {
4124 ssl->max_major_ver = major;
4125 ssl->max_minor_ver = minor;
4126 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004127}
4128
Paul Bakker1d29fb52012-09-28 13:28:45 +00004129void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4130{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004131 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4132 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4133 {
4134 ssl->min_major_ver = major;
4135 ssl->min_minor_ver = minor;
4136 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004137}
4138
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004139#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4140void ssl_set_fallback( ssl_context *ssl, char fallback )
4141{
4142 ssl->fallback = fallback;
4143}
4144#endif
4145
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004146#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4147void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4148{
4149 ssl->encrypt_then_mac = etm;
4150}
4151#endif
4152
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004153#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4154void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4155{
4156 ssl->extended_ms = ems;
4157}
4158#endif
4159
Paul Bakker05decb22013-08-15 13:33:48 +02004160#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004161int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4162{
Paul Bakker77e257e2013-12-16 15:29:52 +01004163 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004164 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004165 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004167 }
4168
4169 ssl->mfl_code = mfl_code;
4170
4171 return( 0 );
4172}
Paul Bakker05decb22013-08-15 13:33:48 +02004173#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004174
Paul Bakker1f2bc622013-08-15 13:45:55 +02004175#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004176int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004177{
4178 if( ssl->endpoint != SSL_IS_CLIENT )
4179 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4180
Paul Bakker8c1ede62013-07-19 14:14:37 +02004181 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004182
4183 return( 0 );
4184}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004185#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004186
Paul Bakker48916f92012-09-16 19:57:18 +00004187void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4188{
4189 ssl->disable_renegotiation = renegotiation;
4190}
4191
4192void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4193{
4194 ssl->allow_legacy_renegotiation = allow_legacy;
4195}
4196
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004197void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4198{
4199 ssl->renego_max_records = max_records;
4200}
4201
Paul Bakkera503a632013-08-14 13:48:06 +02004202#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004203int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4204{
4205 ssl->session_tickets = use_tickets;
4206
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004207 if( ssl->endpoint == SSL_IS_CLIENT )
4208 return( 0 );
4209
4210 if( ssl->f_rng == NULL )
4211 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4212
4213 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004214}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004215
4216void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4217{
4218 ssl->ticket_lifetime = lifetime;
4219}
Paul Bakkera503a632013-08-14 13:48:06 +02004220#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004221
Paul Bakker5121ce52009-01-03 21:22:43 +00004222/*
4223 * SSL get accessors
4224 */
Paul Bakker23986e52011-04-24 08:57:21 +00004225size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004226{
4227 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4228}
4229
Paul Bakkerff60ee62010-03-16 21:09:09 +00004230int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004231{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004232 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004233}
4234
Paul Bakkere3166ce2011-01-27 17:40:50 +00004235const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004236{
Paul Bakker926c8e42013-03-06 10:23:34 +01004237 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004238 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004239
Paul Bakkere3166ce2011-01-27 17:40:50 +00004240 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004241}
4242
Paul Bakker43ca69c2011-01-15 17:35:19 +00004243const char *ssl_get_version( const ssl_context *ssl )
4244{
4245 switch( ssl->minor_ver )
4246 {
4247 case SSL_MINOR_VERSION_0:
4248 return( "SSLv3.0" );
4249
4250 case SSL_MINOR_VERSION_1:
4251 return( "TLSv1.0" );
4252
4253 case SSL_MINOR_VERSION_2:
4254 return( "TLSv1.1" );
4255
Paul Bakker1ef83d62012-04-11 12:09:53 +00004256 case SSL_MINOR_VERSION_3:
4257 return( "TLSv1.2" );
4258
Paul Bakker43ca69c2011-01-15 17:35:19 +00004259 default:
4260 break;
4261 }
4262 return( "unknown" );
4263}
4264
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004265#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004266const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004267{
4268 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004269 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004270
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004271 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004272}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004273#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004274
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004275int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4276{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004277 if( ssl == NULL ||
4278 dst == NULL ||
4279 ssl->session == NULL ||
4280 ssl->endpoint != SSL_IS_CLIENT )
4281 {
4282 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4283 }
4284
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004285 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004286}
4287
Paul Bakker5121ce52009-01-03 21:22:43 +00004288/*
Paul Bakker1961b702013-01-25 14:49:24 +01004289 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004290 */
Paul Bakker1961b702013-01-25 14:49:24 +01004291int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004292{
Paul Bakker40e46942009-01-03 21:51:57 +00004293 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004294
Paul Bakker40e46942009-01-03 21:51:57 +00004295#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004296 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004297 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004298#endif
4299
Paul Bakker40e46942009-01-03 21:51:57 +00004300#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004301 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004302 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004303#endif
4304
Paul Bakker1961b702013-01-25 14:49:24 +01004305 return( ret );
4306}
4307
4308/*
4309 * Perform the SSL handshake
4310 */
4311int ssl_handshake( ssl_context *ssl )
4312{
4313 int ret = 0;
4314
4315 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4316
4317 while( ssl->state != SSL_HANDSHAKE_OVER )
4318 {
4319 ret = ssl_handshake_step( ssl );
4320
4321 if( ret != 0 )
4322 break;
4323 }
4324
Paul Bakker5121ce52009-01-03 21:22:43 +00004325 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4326
4327 return( ret );
4328}
4329
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004330#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004331/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004332 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004333 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004334static int ssl_write_hello_request( ssl_context *ssl )
4335{
4336 int ret;
4337
4338 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4339
4340 ssl->out_msglen = 4;
4341 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4342 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4343
4344 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4345 {
4346 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4347 return( ret );
4348 }
4349
4350 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4351
4352 return( 0 );
4353}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004354#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004355
4356/*
4357 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004358 * - any side: calling ssl_renegotiate(),
4359 * - client: receiving a HelloRequest during ssl_read(),
4360 * - server: receiving any handshake message on server during ssl_read() after
4361 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004362 * If the handshake doesn't complete due to waiting for I/O, it will continue
4363 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004364 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004365static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004366{
4367 int ret;
4368
4369 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4370
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004371 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4372 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004373
4374 ssl->state = SSL_HELLO_REQUEST;
4375 ssl->renegotiation = SSL_RENEGOTIATION;
4376
Paul Bakker48916f92012-09-16 19:57:18 +00004377 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4378 {
4379 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4380 return( ret );
4381 }
4382
4383 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4384
4385 return( 0 );
4386}
4387
4388/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004389 * Renegotiate current connection on client,
4390 * or request renegotiation on server
4391 */
4392int ssl_renegotiate( ssl_context *ssl )
4393{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004394 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004395
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004396#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004397 /* On server, just send the request */
4398 if( ssl->endpoint == SSL_IS_SERVER )
4399 {
4400 if( ssl->state != SSL_HANDSHAKE_OVER )
4401 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4402
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004403 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4404
4405 /* Did we already try/start sending HelloRequest? */
4406 if( ssl->out_left != 0 )
4407 return( ssl_flush_output( ssl ) );
4408
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004409 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004410 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004411#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004412
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004413#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004414 /*
4415 * On client, either start the renegotiation process or,
4416 * if already in progress, continue the handshake
4417 */
4418 if( ssl->renegotiation != SSL_RENEGOTIATION )
4419 {
4420 if( ssl->state != SSL_HANDSHAKE_OVER )
4421 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4422
4423 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4424 {
4425 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4426 return( ret );
4427 }
4428 }
4429 else
4430 {
4431 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4432 {
4433 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4434 return( ret );
4435 }
4436 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004437#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004438
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004439 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004440}
4441
4442/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004443 * Receive application data decrypted from the SSL layer
4444 */
Paul Bakker23986e52011-04-24 08:57:21 +00004445int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004446{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004447 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004448 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004449
4450 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4451
4452 if( ssl->state != SSL_HANDSHAKE_OVER )
4453 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004454 ret = ssl_handshake( ssl );
4455 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4456 {
4457 record_read = 1;
4458 }
4459 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 {
4461 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4462 return( ret );
4463 }
4464 }
4465
4466 if( ssl->in_offt == NULL )
4467 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004468 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004469 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004470 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4471 {
4472 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4473 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004474
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004475 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4476 return( ret );
4477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004478 }
4479
4480 if( ssl->in_msglen == 0 &&
4481 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4482 {
4483 /*
4484 * OpenSSL sends empty messages to randomize the IV
4485 */
4486 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4487 {
Paul Bakker831a7552011-05-18 13:32:51 +00004488 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4489 return( 0 );
4490
Paul Bakker5121ce52009-01-03 21:22:43 +00004491 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4492 return( ret );
4493 }
4494 }
4495
Paul Bakker48916f92012-09-16 19:57:18 +00004496 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4497 {
4498 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4499
4500 if( ssl->endpoint == SSL_IS_CLIENT &&
4501 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4502 ssl->in_hslen != 4 ) )
4503 {
4504 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4505 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4506 }
4507
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004508 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4509 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004510 ssl->allow_legacy_renegotiation ==
4511 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004512 {
4513 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4514
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004515#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004516 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004517 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004518 /*
4519 * SSLv3 does not have a "no_renegotiation" alert
4520 */
4521 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4522 return( ret );
4523 }
4524 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004525#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004526#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4527 defined(POLARSSL_SSL_PROTO_TLS1_2)
4528 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004529 {
4530 if( ( ret = ssl_send_alert_message( ssl,
4531 SSL_ALERT_LEVEL_WARNING,
4532 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4533 {
4534 return( ret );
4535 }
Paul Bakker48916f92012-09-16 19:57:18 +00004536 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004537 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004538#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4539 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004540 {
4541 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004542 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004543 }
Paul Bakker48916f92012-09-16 19:57:18 +00004544 }
4545 else
4546 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004547 ret = ssl_start_renegotiation( ssl );
4548 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4549 {
4550 record_read = 1;
4551 }
4552 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004553 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004554 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004555 return( ret );
4556 }
Paul Bakker48916f92012-09-16 19:57:18 +00004557 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004558
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004559 /* If a non-handshake record was read during renego, fallthrough,
4560 * else tell the user they should call ssl_read() again */
4561 if( ! record_read )
4562 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004563 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004564 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4565 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004566 ssl->renego_records_seen++;
4567
4568 if( ssl->renego_max_records >= 0 &&
4569 ssl->renego_records_seen > ssl->renego_max_records )
4570 {
4571 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4572 "but not honored by client" ) );
4573 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4574 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004575 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004576
4577 /* Fatal and closure alerts handled by ssl_read_record() */
4578 if( ssl->in_msgtype == SSL_MSG_ALERT )
4579 {
4580 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4581 return( POLARSSL_ERR_NET_WANT_READ );
4582 }
4583
4584 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004585 {
4586 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004587 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004588 }
4589
4590 ssl->in_offt = ssl->in_msg;
4591 }
4592
4593 n = ( len < ssl->in_msglen )
4594 ? len : ssl->in_msglen;
4595
4596 memcpy( buf, ssl->in_offt, n );
4597 ssl->in_msglen -= n;
4598
4599 if( ssl->in_msglen == 0 )
4600 /* all bytes consumed */
4601 ssl->in_offt = NULL;
4602 else
4603 /* more data available */
4604 ssl->in_offt += n;
4605
4606 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4607
Paul Bakker23986e52011-04-24 08:57:21 +00004608 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004609}
4610
4611/*
4612 * Send application data to be encrypted by the SSL layer
4613 */
Paul Bakker23986e52011-04-24 08:57:21 +00004614int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004615{
Paul Bakker23986e52011-04-24 08:57:21 +00004616 int ret;
4617 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004618 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004619
4620 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4621
4622 if( ssl->state != SSL_HANDSHAKE_OVER )
4623 {
4624 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4625 {
4626 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4627 return( ret );
4628 }
4629 }
4630
Paul Bakker05decb22013-08-15 13:33:48 +02004631#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004632 /*
4633 * Assume mfl_code is correct since it was checked when set
4634 */
4635 max_len = mfl_code_to_length[ssl->mfl_code];
4636
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004637 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004638 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004639 */
4640 if( ssl->session_out != NULL &&
4641 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4642 {
4643 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4644 }
Paul Bakker05decb22013-08-15 13:33:48 +02004645#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004646
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004647 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004648
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 if( ssl->out_left != 0 )
4650 {
4651 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4652 {
4653 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4654 return( ret );
4655 }
4656 }
Paul Bakker887bd502011-06-08 13:10:54 +00004657 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004658 {
Paul Bakker887bd502011-06-08 13:10:54 +00004659 ssl->out_msglen = n;
4660 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4661 memcpy( ssl->out_msg, buf, n );
4662
4663 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4664 {
4665 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4666 return( ret );
4667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004668 }
4669
4670 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4671
Paul Bakker23986e52011-04-24 08:57:21 +00004672 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004673}
4674
4675/*
4676 * Notify the peer that the connection is being closed
4677 */
4678int ssl_close_notify( ssl_context *ssl )
4679{
4680 int ret;
4681
4682 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4683
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004684 if( ssl->out_left != 0 )
4685 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004686
4687 if( ssl->state == SSL_HANDSHAKE_OVER )
4688 {
Paul Bakker48916f92012-09-16 19:57:18 +00004689 if( ( ret = ssl_send_alert_message( ssl,
4690 SSL_ALERT_LEVEL_WARNING,
4691 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004692 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004693 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004694 return( ret );
4695 }
4696 }
4697
4698 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4699
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004700 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004701}
4702
Paul Bakker48916f92012-09-16 19:57:18 +00004703void ssl_transform_free( ssl_transform *transform )
4704{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004705 if( transform == NULL )
4706 return;
4707
Paul Bakker48916f92012-09-16 19:57:18 +00004708#if defined(POLARSSL_ZLIB_SUPPORT)
4709 deflateEnd( &transform->ctx_deflate );
4710 inflateEnd( &transform->ctx_inflate );
4711#endif
4712
Paul Bakker84bbeb52014-07-01 14:53:22 +02004713 cipher_free( &transform->cipher_ctx_enc );
4714 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004715
Paul Bakker84bbeb52014-07-01 14:53:22 +02004716 md_free( &transform->md_ctx_enc );
4717 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004718
Paul Bakker34617722014-06-13 17:20:13 +02004719 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004720}
4721
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004722#if defined(POLARSSL_X509_CRT_PARSE_C)
4723static void ssl_key_cert_free( ssl_key_cert *key_cert )
4724{
4725 ssl_key_cert *cur = key_cert, *next;
4726
4727 while( cur != NULL )
4728 {
4729 next = cur->next;
4730
4731 if( cur->key_own_alloc )
4732 {
4733 pk_free( cur->key );
4734 polarssl_free( cur->key );
4735 }
4736 polarssl_free( cur );
4737
4738 cur = next;
4739 }
4740}
4741#endif /* POLARSSL_X509_CRT_PARSE_C */
4742
Paul Bakker48916f92012-09-16 19:57:18 +00004743void ssl_handshake_free( ssl_handshake_params *handshake )
4744{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004745 if( handshake == NULL )
4746 return;
4747
Paul Bakker48916f92012-09-16 19:57:18 +00004748#if defined(POLARSSL_DHM_C)
4749 dhm_free( &handshake->dhm_ctx );
4750#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004751#if defined(POLARSSL_ECDH_C)
4752 ecdh_free( &handshake->ecdh_ctx );
4753#endif
4754
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004755#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004756 /* explicit void pointer cast for buggy MS compiler */
4757 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004758#endif
4759
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004760#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4761 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4762 /*
4763 * Free only the linked list wrapper, not the keys themselves
4764 * since the belong to the SNI callback
4765 */
4766 if( handshake->sni_key_cert != NULL )
4767 {
4768 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4769
4770 while( cur != NULL )
4771 {
4772 next = cur->next;
4773 polarssl_free( cur );
4774 cur = next;
4775 }
4776 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004777#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004778
Paul Bakker34617722014-06-13 17:20:13 +02004779 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004780}
4781
4782void ssl_session_free( ssl_session *session )
4783{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004784 if( session == NULL )
4785 return;
4786
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004787#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004788 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004789 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004790 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004791 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004792 }
Paul Bakkered27a042013-04-18 22:46:23 +02004793#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004794
Paul Bakkera503a632013-08-14 13:48:06 +02004795#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004796 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004797#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004798
Paul Bakker34617722014-06-13 17:20:13 +02004799 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004800}
4801
Paul Bakker5121ce52009-01-03 21:22:43 +00004802/*
4803 * Free an SSL context
4804 */
4805void ssl_free( ssl_context *ssl )
4806{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004807 if( ssl == NULL )
4808 return;
4809
Paul Bakker5121ce52009-01-03 21:22:43 +00004810 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4811
Paul Bakker5121ce52009-01-03 21:22:43 +00004812 if( ssl->out_ctr != NULL )
4813 {
Paul Bakker34617722014-06-13 17:20:13 +02004814 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004815 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004816 }
4817
4818 if( ssl->in_ctr != NULL )
4819 {
Paul Bakker34617722014-06-13 17:20:13 +02004820 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004821 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004822 }
4823
Paul Bakker16770332013-10-11 09:59:44 +02004824#if defined(POLARSSL_ZLIB_SUPPORT)
4825 if( ssl->compress_buf != NULL )
4826 {
Paul Bakker34617722014-06-13 17:20:13 +02004827 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004828 polarssl_free( ssl->compress_buf );
4829 }
4830#endif
4831
Paul Bakker40e46942009-01-03 21:51:57 +00004832#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004833 mpi_free( &ssl->dhm_P );
4834 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004835#endif
4836
Paul Bakker48916f92012-09-16 19:57:18 +00004837 if( ssl->transform )
4838 {
4839 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004840 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004841 }
4842
4843 if( ssl->handshake )
4844 {
4845 ssl_handshake_free( ssl->handshake );
4846 ssl_transform_free( ssl->transform_negotiate );
4847 ssl_session_free( ssl->session_negotiate );
4848
Paul Bakker6e339b52013-07-03 13:37:05 +02004849 polarssl_free( ssl->handshake );
4850 polarssl_free( ssl->transform_negotiate );
4851 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004852 }
4853
Paul Bakkerc0463502013-02-14 11:19:38 +01004854 if( ssl->session )
4855 {
4856 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004857 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004858 }
4859
Paul Bakkera503a632013-08-14 13:48:06 +02004860#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004861 if( ssl->ticket_keys )
4862 {
4863 ssl_ticket_keys_free( ssl->ticket_keys );
4864 polarssl_free( ssl->ticket_keys );
4865 }
Paul Bakkera503a632013-08-14 13:48:06 +02004866#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004867
Paul Bakker0be444a2013-08-27 21:55:01 +02004868#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004869 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004870 {
Paul Bakker34617722014-06-13 17:20:13 +02004871 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004872 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004873 ssl->hostname_len = 0;
4874 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004876
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004877#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004878 if( ssl->psk != NULL )
4879 {
Paul Bakker34617722014-06-13 17:20:13 +02004880 polarssl_zeroize( ssl->psk, ssl->psk_len );
4881 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004882 polarssl_free( ssl->psk );
4883 polarssl_free( ssl->psk_identity );
4884 ssl->psk_len = 0;
4885 ssl->psk_identity_len = 0;
4886 }
4887#endif
4888
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004889#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004890 ssl_key_cert_free( ssl->key_cert );
4891#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004892
Paul Bakker05ef8352012-05-08 09:17:57 +00004893#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4894 if( ssl_hw_record_finish != NULL )
4895 {
4896 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4897 ssl_hw_record_finish( ssl );
4898 }
4899#endif
4900
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004902
Paul Bakker86f04f42013-02-14 11:20:09 +01004903 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004904 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004905}
4906
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004907#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004908/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004909 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004910 */
4911unsigned char ssl_sig_from_pk( pk_context *pk )
4912{
4913#if defined(POLARSSL_RSA_C)
4914 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4915 return( SSL_SIG_RSA );
4916#endif
4917#if defined(POLARSSL_ECDSA_C)
4918 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4919 return( SSL_SIG_ECDSA );
4920#endif
4921 return( SSL_SIG_ANON );
4922}
4923
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004924pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4925{
4926 switch( sig )
4927 {
4928#if defined(POLARSSL_RSA_C)
4929 case SSL_SIG_RSA:
4930 return( POLARSSL_PK_RSA );
4931#endif
4932#if defined(POLARSSL_ECDSA_C)
4933 case SSL_SIG_ECDSA:
4934 return( POLARSSL_PK_ECDSA );
4935#endif
4936 default:
4937 return( POLARSSL_PK_NONE );
4938 }
4939}
Paul Bakker9af723c2014-05-01 13:03:14 +02004940#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004941
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004942/*
4943 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4944 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004945md_type_t ssl_md_alg_from_hash( unsigned char hash )
4946{
4947 switch( hash )
4948 {
4949#if defined(POLARSSL_MD5_C)
4950 case SSL_HASH_MD5:
4951 return( POLARSSL_MD_MD5 );
4952#endif
4953#if defined(POLARSSL_SHA1_C)
4954 case SSL_HASH_SHA1:
4955 return( POLARSSL_MD_SHA1 );
4956#endif
4957#if defined(POLARSSL_SHA256_C)
4958 case SSL_HASH_SHA224:
4959 return( POLARSSL_MD_SHA224 );
4960 case SSL_HASH_SHA256:
4961 return( POLARSSL_MD_SHA256 );
4962#endif
4963#if defined(POLARSSL_SHA512_C)
4964 case SSL_HASH_SHA384:
4965 return( POLARSSL_MD_SHA384 );
4966 case SSL_HASH_SHA512:
4967 return( POLARSSL_MD_SHA512 );
4968#endif
4969 default:
4970 return( POLARSSL_MD_NONE );
4971 }
4972}
4973
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004974#if defined(POLARSSL_SSL_SET_CURVES)
4975/*
4976 * Check is a curve proposed by the peer is in our list.
4977 * Return 1 if we're willing to use it, 0 otherwise.
4978 */
4979int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4980{
4981 const ecp_group_id *gid;
4982
4983 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4984 if( *gid == grp_id )
4985 return( 1 );
4986
4987 return( 0 );
4988}
Paul Bakker9af723c2014-05-01 13:03:14 +02004989#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004990
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004991#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004992int ssl_check_cert_usage( const x509_crt *cert,
4993 const ssl_ciphersuite_t *ciphersuite,
4994 int cert_endpoint )
4995{
4996#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4997 int usage = 0;
4998#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004999#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5000 const char *ext_oid;
5001 size_t ext_len;
5002#endif
5003
5004#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5005 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5006 ((void) cert);
5007 ((void) cert_endpoint);
5008#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005009
5010#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5011 if( cert_endpoint == SSL_IS_SERVER )
5012 {
5013 /* Server part of the key exchange */
5014 switch( ciphersuite->key_exchange )
5015 {
5016 case POLARSSL_KEY_EXCHANGE_RSA:
5017 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5018 usage = KU_KEY_ENCIPHERMENT;
5019 break;
5020
5021 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5022 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5023 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5024 usage = KU_DIGITAL_SIGNATURE;
5025 break;
5026
5027 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5028 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5029 usage = KU_KEY_AGREEMENT;
5030 break;
5031
5032 /* Don't use default: we want warnings when adding new values */
5033 case POLARSSL_KEY_EXCHANGE_NONE:
5034 case POLARSSL_KEY_EXCHANGE_PSK:
5035 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5036 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5037 usage = 0;
5038 }
5039 }
5040 else
5041 {
5042 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5043 usage = KU_DIGITAL_SIGNATURE;
5044 }
5045
5046 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5047 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005048#else
5049 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005050#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5051
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005052#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5053 if( cert_endpoint == SSL_IS_SERVER )
5054 {
5055 ext_oid = OID_SERVER_AUTH;
5056 ext_len = OID_SIZE( OID_SERVER_AUTH );
5057 }
5058 else
5059 {
5060 ext_oid = OID_CLIENT_AUTH;
5061 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5062 }
5063
5064 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5065 return( -1 );
5066#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5067
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005068 return( 0 );
5069}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005070#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005071
5072#endif /* POLARSSL_SSL_TLS_C */