blob: 6b9df336952bd139084e9f1d47f276122bc18028 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100604 * 1. if EtM is in use: one block plus MAC
605 * otherwise: * first multiple of blocklen greater than maclen
606 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200607 */
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +0100608#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
609 if( session->encrypt_then_mac == SSL_ETM_ENABLED )
610 {
611 transform->minlen = transform->maclen
612 + cipher_info->block_size;
613 }
614 else
615#endif
616 {
617 transform->minlen = transform->maclen
618 + cipher_info->block_size
619 - transform->maclen % cipher_info->block_size;
620 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200621
622#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
623 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
624 ssl->minor_ver == SSL_MINOR_VERSION_1 )
625 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100626 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200627#endif
628#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
629 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
630 ssl->minor_ver == SSL_MINOR_VERSION_3 )
631 {
632 transform->minlen += transform->ivlen;
633 }
634 else
635#endif
636 {
637 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
638 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
639 }
Paul Bakker68884e32013-01-07 18:20:04 +0100640 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 }
642
643 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000644 transform->keylen, transform->minlen, transform->ivlen,
645 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
647 /*
648 * Finally setup the cipher contexts, IVs and MAC secrets.
649 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100650#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 if( ssl->endpoint == SSL_IS_CLIENT )
652 {
Paul Bakker48916f92012-09-16 19:57:18 +0000653 key1 = keyblk + transform->maclen * 2;
654 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
Paul Bakker68884e32013-01-07 18:20:04 +0100656 mac_enc = keyblk;
657 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000658
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000659 /*
660 * This is not used in TLS v1.1.
661 */
Paul Bakker48916f92012-09-16 19:57:18 +0000662 iv_copy_len = ( transform->fixed_ivlen ) ?
663 transform->fixed_ivlen : transform->ivlen;
664 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
665 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000666 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 }
668 else
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100669#endif /* POLARSSL_SSL_CLI_C */
670#if defined(POLARSSL_SSL_SRV_C)
671 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 {
Paul Bakker48916f92012-09-16 19:57:18 +0000673 key1 = keyblk + transform->maclen * 2 + transform->keylen;
674 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000675
Paul Bakker68884e32013-01-07 18:20:04 +0100676 mac_enc = keyblk + transform->maclen;
677 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000679 /*
680 * This is not used in TLS v1.1.
681 */
Paul Bakker48916f92012-09-16 19:57:18 +0000682 iv_copy_len = ( transform->fixed_ivlen ) ?
683 transform->fixed_ivlen : transform->ivlen;
684 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
685 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000686 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +0100688 else
689#endif /* POLARSSL_SSL_SRV_C */
690 {
691 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
692 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
693 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100696 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
697 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100698 if( transform->maclen > sizeof transform->mac_enc )
699 {
700 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200701 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100702 }
703
Paul Bakker68884e32013-01-07 18:20:04 +0100704 memcpy( transform->mac_enc, mac_enc, transform->maclen );
705 memcpy( transform->mac_dec, mac_dec, transform->maclen );
706 }
707 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200708#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200709#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
710 defined(POLARSSL_SSL_PROTO_TLS1_2)
711 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100712 {
713 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
714 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200716 else
717#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200718 {
719 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200720 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200721 }
Paul Bakker68884e32013-01-07 18:20:04 +0100722
Paul Bakker05ef8352012-05-08 09:17:57 +0000723#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200724 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000725 {
726 int ret = 0;
727
728 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
729
Paul Bakker07eb38b2012-12-19 14:42:06 +0100730 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
731 transform->iv_enc, transform->iv_dec,
732 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100733 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100734 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000735 {
736 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200737 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000738 }
739 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200740#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000741
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200742 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
743 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200745 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
746 return( ret );
747 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200748
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200749 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
750 cipher_info ) ) != 0 )
751 {
752 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
753 return( ret );
754 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200755
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200756 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
757 cipher_info->key_length,
758 POLARSSL_ENCRYPT ) ) != 0 )
759 {
760 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
761 return( ret );
762 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200763
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200764 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
765 cipher_info->key_length,
766 POLARSSL_DECRYPT ) ) != 0 )
767 {
768 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
769 return( ret );
770 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200771
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200772#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200773 if( cipher_info->mode == POLARSSL_MODE_CBC )
774 {
775 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
776 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200777 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200778 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
779 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200780 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200781
782 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
783 POLARSSL_PADDING_NONE ) ) != 0 )
784 {
785 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
786 return( ret );
787 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000788 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200789#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000790
Paul Bakker34617722014-06-13 17:20:13 +0200791 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793#if defined(POLARSSL_ZLIB_SUPPORT)
794 // Initialize compression
795 //
Paul Bakker48916f92012-09-16 19:57:18 +0000796 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000797 {
Paul Bakker16770332013-10-11 09:59:44 +0200798 if( ssl->compress_buf == NULL )
799 {
800 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
801 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
802 if( ssl->compress_buf == NULL )
803 {
804 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
805 SSL_BUFFER_LEN ) );
806 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
807 }
808 }
809
Paul Bakker2770fbd2012-07-03 13:30:23 +0000810 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
811
Paul Bakker48916f92012-09-16 19:57:18 +0000812 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
813 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000814
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200815 if( deflateInit( &transform->ctx_deflate,
816 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000817 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000818 {
819 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
820 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
821 }
822 }
823#endif /* POLARSSL_ZLIB_SUPPORT */
824
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
826
827 return( 0 );
828}
829
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200830#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000831void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000832{
833 md5_context md5;
834 sha1_context sha1;
835 unsigned char pad_1[48];
836 unsigned char pad_2[48];
837
Paul Bakker380da532012-04-18 16:10:25 +0000838 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000839
Paul Bakker48916f92012-09-16 19:57:18 +0000840 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
841 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000842
Paul Bakker380da532012-04-18 16:10:25 +0000843 memset( pad_1, 0x36, 48 );
844 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Paul Bakker48916f92012-09-16 19:57:18 +0000846 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000847 md5_update( &md5, pad_1, 48 );
848 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849
Paul Bakker380da532012-04-18 16:10:25 +0000850 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000851 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000852 md5_update( &md5, pad_2, 48 );
853 md5_update( &md5, hash, 16 );
854 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Paul Bakker48916f92012-09-16 19:57:18 +0000856 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000857 sha1_update( &sha1, pad_1, 40 );
858 sha1_finish( &sha1, hash + 16 );
859
860 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000861 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000862 sha1_update( &sha1, pad_2, 40 );
863 sha1_update( &sha1, hash + 16, 20 );
864 sha1_finish( &sha1, hash + 16 );
865
866 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
867 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
868
Paul Bakker5b4af392014-06-26 12:09:34 +0200869 md5_free( &md5 );
870 sha1_free( &sha1 );
871
Paul Bakker380da532012-04-18 16:10:25 +0000872 return;
873}
Paul Bakker9af723c2014-05-01 13:03:14 +0200874#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000875
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200876#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000877void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
878{
879 md5_context md5;
880 sha1_context sha1;
881
882 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
883
Paul Bakker48916f92012-09-16 19:57:18 +0000884 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
885 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000886
Paul Bakker48916f92012-09-16 19:57:18 +0000887 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000888 sha1_finish( &sha1, hash + 16 );
889
890 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
891 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
892
Paul Bakker5b4af392014-06-26 12:09:34 +0200893 md5_free( &md5 );
894 sha1_free( &sha1 );
895
Paul Bakker380da532012-04-18 16:10:25 +0000896 return;
897}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200898#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000899
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200900#if defined(POLARSSL_SSL_PROTO_TLS1_2)
901#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000902void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
903{
Paul Bakker9e36f042013-06-30 14:34:05 +0200904 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000905
906 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
907
Paul Bakker9e36f042013-06-30 14:34:05 +0200908 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
909 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000910
911 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
912 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
913
Paul Bakker5b4af392014-06-26 12:09:34 +0200914 sha256_free( &sha256 );
915
Paul Bakker380da532012-04-18 16:10:25 +0000916 return;
917}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200918#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000919
Paul Bakker9e36f042013-06-30 14:34:05 +0200920#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000921void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
922{
Paul Bakker9e36f042013-06-30 14:34:05 +0200923 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000924
925 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
926
Paul Bakker9e36f042013-06-30 14:34:05 +0200927 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
928 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
Paul Bakkerca4ab492012-04-18 14:23:57 +0000930 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000931 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
932
Paul Bakker5b4af392014-06-26 12:09:34 +0200933 sha512_free( &sha512 );
934
Paul Bakker5121ce52009-01-03 21:22:43 +0000935 return;
936}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200937#endif /* POLARSSL_SHA512_C */
938#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000939
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200940#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200941int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
942{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200943 unsigned char *p = ssl->handshake->premaster;
944 unsigned char *end = p + sizeof( ssl->handshake->premaster );
945
946 /*
947 * PMS = struct {
948 * opaque other_secret<0..2^16-1>;
949 * opaque psk<0..2^16-1>;
950 * };
951 * with "other_secret" depending on the particular key exchange
952 */
953#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
954 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
955 {
956 if( end - p < 2 + (int) ssl->psk_len )
957 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
958
959 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
960 *(p++) = (unsigned char)( ssl->psk_len );
961 p += ssl->psk_len;
962 }
963 else
964#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200965#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
966 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
967 {
968 /*
969 * other_secret already set by the ClientKeyExchange message,
970 * and is 48 bytes long
971 */
972 *p++ = 0;
973 *p++ = 48;
974 p += 48;
975 }
976 else
977#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200978#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
979 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
980 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200981 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200982 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200984 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200985 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200986 p + 2, &len,
987 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200988 {
989 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
990 return( ret );
991 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200992 *(p++) = (unsigned char)( len >> 8 );
993 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200994 p += len;
995
996 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
997 }
998 else
999#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
1000#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1001 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1002 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001003 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 size_t zlen;
1005
1006 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001007 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001008 ssl->f_rng, ssl->p_rng ) ) != 0 )
1009 {
1010 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
1011 return( ret );
1012 }
1013
1014 *(p++) = (unsigned char)( zlen >> 8 );
1015 *(p++) = (unsigned char)( zlen );
1016 p += zlen;
1017
1018 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
1019 }
1020 else
1021#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1022 {
1023 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001024 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001025 }
1026
1027 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001028 if( end - p < 2 + (int) ssl->psk_len )
1029 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1030
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001031 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1032 *(p++) = (unsigned char)( ssl->psk_len );
1033 memcpy( p, ssl->psk, ssl->psk_len );
1034 p += ssl->psk_len;
1035
1036 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1037
1038 return( 0 );
1039}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001040#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001041
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001043/*
1044 * SSLv3.0 MAC functions
1045 */
Paul Bakker68884e32013-01-07 18:20:04 +01001046static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1047 unsigned char *buf, size_t len,
1048 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001049{
1050 unsigned char header[11];
1051 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001052 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001053 int md_size = md_get_size( md_ctx->md_info );
1054 int md_type = md_get_type( md_ctx->md_info );
1055
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001056 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001057 if( md_type == POLARSSL_MD_MD5 )
1058 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001059 else
Paul Bakker68884e32013-01-07 18:20:04 +01001060 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001061
1062 memcpy( header, ctr, 8 );
1063 header[ 8] = (unsigned char) type;
1064 header[ 9] = (unsigned char)( len >> 8 );
1065 header[10] = (unsigned char)( len );
1066
Paul Bakker68884e32013-01-07 18:20:04 +01001067 memset( padding, 0x36, padlen );
1068 md_starts( md_ctx );
1069 md_update( md_ctx, secret, md_size );
1070 md_update( md_ctx, padding, padlen );
1071 md_update( md_ctx, header, 11 );
1072 md_update( md_ctx, buf, len );
1073 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001074
Paul Bakker68884e32013-01-07 18:20:04 +01001075 memset( padding, 0x5C, padlen );
1076 md_starts( md_ctx );
1077 md_update( md_ctx, secret, md_size );
1078 md_update( md_ctx, padding, padlen );
1079 md_update( md_ctx, buf + len, md_size );
1080 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001081}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001082#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001083
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001084#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1085 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1086 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1087#define POLARSSL_SOME_MODES_USE_MAC
1088#endif
1089
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001090/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001092 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001093static int ssl_encrypt_buf( ssl_context *ssl )
1094{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001095 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001096 cipher_mode_t mode;
1097 int auth_done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
1099 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1100
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001101 if( ssl->session_out == NULL || ssl->transform_out == NULL )
1102 {
1103 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1104 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1105 }
1106
1107 mode = cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc );
1108
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01001109 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1110 ssl->out_msg, ssl->out_msglen );
1111
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é-Gonnard8e4b3372014-11-17 15:06:13 +01001115#if defined(POLARSSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001116 if( mode == POLARSSL_MODE_STREAM ||
1117 ( mode == POLARSSL_MODE_CBC
1118#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1119 && ssl->session_out->encrypt_then_mac == SSL_ETM_DISABLED
1120#endif
1121 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00001122 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001123#if defined(POLARSSL_SSL_PROTO_SSL3)
1124 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1125 {
1126 ssl_mac( &ssl->transform_out->md_ctx_enc,
1127 ssl->transform_out->mac_enc,
1128 ssl->out_msg, ssl->out_msglen,
1129 ssl->out_ctr, ssl->out_msgtype );
1130 }
1131 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001132#endif
1133#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001134 defined(POLARSSL_SSL_PROTO_TLS1_2)
1135 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1136 {
1137 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1138 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1139 ssl->out_msg, ssl->out_msglen );
1140 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1141 ssl->out_msg + ssl->out_msglen );
1142 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1143 }
1144 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001145#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001146 {
1147 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001148 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001149 }
1150
1151 SSL_DEBUG_BUF( 4, "computed mac",
1152 ssl->out_msg + ssl->out_msglen,
1153 ssl->transform_out->maclen );
1154
1155 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001156 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02001157 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001158#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001160 /*
1161 * Encrypt
1162 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001163#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001164 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001166 int ret;
1167 size_t olen = 0;
1168
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1170 "including %d bytes of padding",
1171 ssl->out_msglen, 0 ) );
1172
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001173 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001174 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001175 ssl->transform_out->ivlen,
1176 ssl->out_msg, ssl->out_msglen,
1177 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001178 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001179 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001180 return( ret );
1181 }
1182
1183 if( ssl->out_msglen != olen )
1184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001187 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 }
Paul Bakker68884e32013-01-07 18:20:04 +01001189 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001190#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1192 if( mode == POLARSSL_MODE_GCM ||
1193 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001194 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001195 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197 unsigned char *enc_msg;
1198 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001199 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1200 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakkerca4ab492012-04-18 14:23:57 +00001202 memcpy( add_data, ssl->out_ctr, 8 );
1203 add_data[8] = ssl->out_msgtype;
1204 add_data[9] = ssl->major_ver;
1205 add_data[10] = ssl->minor_ver;
1206 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1207 add_data[12] = ssl->out_msglen & 0xFF;
1208
1209 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1210 add_data, 13 );
1211
Paul Bakker68884e32013-01-07 18:20:04 +01001212 /*
1213 * Generate IV
1214 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001215#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001216 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001217 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1218 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001219 if( ret != 0 )
1220 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001221
Paul Bakker68884e32013-01-07 18:20:04 +01001222 memcpy( ssl->out_iv,
1223 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1224 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001225#else
1226 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1227 {
1228 /* Reminder if we ever add an AEAD mode with a different size */
1229 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1230 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1231 }
1232
1233 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1234 ssl->out_ctr, 8 );
1235 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1236#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001237
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001238 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001239 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001240
Paul Bakker68884e32013-01-07 18:20:04 +01001241 /*
1242 * Fix pointer positions and message length with added IV
1243 */
1244 enc_msg = ssl->out_msg;
1245 enc_msglen = ssl->out_msglen;
1246 ssl->out_msglen += ssl->transform_out->ivlen -
1247 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001248
Paul Bakker68884e32013-01-07 18:20:04 +01001249 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1250 "including %d bytes of padding",
1251 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001252
Paul Bakker68884e32013-01-07 18:20:04 +01001253 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001254 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001255 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001256 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1257 ssl->transform_out->iv_enc,
1258 ssl->transform_out->ivlen,
1259 add_data, 13,
1260 enc_msg, enc_msglen,
1261 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001262 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001263 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001264 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001265 return( ret );
1266 }
1267
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001268 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001269 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001270 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001271 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001272 }
1273
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001274 ssl->out_msglen += taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001275 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001276
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001277 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001278 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001280#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001281#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1282 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001283 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001284 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001285 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001286 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001287 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001288
Paul Bakker48916f92012-09-16 19:57:18 +00001289 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1290 ssl->transform_out->ivlen;
1291 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 padlen = 0;
1293
1294 for( i = 0; i <= padlen; i++ )
1295 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1296
1297 ssl->out_msglen += padlen + 1;
1298
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001299 enc_msglen = ssl->out_msglen;
1300 enc_msg = ssl->out_msg;
1301
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001302#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001303 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001304 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1305 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001306 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001307 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001308 {
1309 /*
1310 * Generate IV
1311 */
Paul Bakker48916f92012-09-16 19:57:18 +00001312 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1313 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001314 if( ret != 0 )
1315 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001316
Paul Bakker92be97b2013-01-02 17:30:03 +01001317 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001319
1320 /*
1321 * Fix pointer positions and message length with added IV
1322 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001323 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001324 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001325 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001326 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001327#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001328
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001330 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001331 ssl->out_msglen, ssl->transform_out->ivlen,
1332 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001334 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001335 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001336 ssl->transform_out->ivlen,
1337 enc_msg, enc_msglen,
1338 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001339 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001340 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001341 return( ret );
1342 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001343
Paul Bakkercca5b812013-08-31 17:40:26 +02001344 if( enc_msglen != olen )
1345 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001346 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001347 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001348 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001349
1350#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001351 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1352 {
1353 /*
1354 * Save IV in SSL3 and TLS1
1355 */
1356 memcpy( ssl->transform_out->iv_enc,
1357 ssl->transform_out->cipher_ctx_enc.iv,
1358 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001360#endif
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001361
1362#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001363 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001364 {
1365 /*
1366 * MAC(MAC_write_key, seq_num +
1367 * TLSCipherText.type +
1368 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001369 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001370 * IV + // except for TLS 1.0
1371 * ENC(content + padding + padding_length));
1372 */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001373 unsigned char pseudo_hdr[13];
1374
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001375 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1376
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001377 memcpy( pseudo_hdr + 0, ssl->out_ctr, 8 );
1378 memcpy( pseudo_hdr + 8, ssl->out_hdr, 3 );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001379 pseudo_hdr[11] = (unsigned char)( ( ssl->out_msglen >> 8 ) & 0xFF );
1380 pseudo_hdr[12] = (unsigned char)( ( ssl->out_msglen ) & 0xFF );
1381
1382 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001383
1384 md_hmac_update( &ssl->transform_out->md_ctx_enc, pseudo_hdr, 13 );
1385 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1386 ssl->out_iv, ssl->out_msglen );
1387 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1388 ssl->out_iv + ssl->out_msglen );
1389 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1390
1391 ssl->out_msglen += ssl->transform_out->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001392 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001393 }
1394#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001395 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001396 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001397#endif /* POLARSSL_CIPHER_MODE_CBC &&
1398 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001399 {
1400 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001401 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001402 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001404 /* Make extra sure authentication was performed, exactly once */
1405 if( auth_done != 1 )
1406 {
1407 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1408 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1409 }
1410
Paul Bakkerca4ab492012-04-18 14:23:57 +00001411 for( i = 8; i > 0; i-- )
1412 if( ++ssl->out_ctr[i - 1] != 0 )
1413 break;
1414
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001415 /* The loops goes to its end iff the counter is wrapping */
1416 if( i == 0 )
1417 {
1418 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1419 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1420 }
1421
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1423
1424 return( 0 );
1425}
1426
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001427#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001428
Paul Bakker5121ce52009-01-03 21:22:43 +00001429static int ssl_decrypt_buf( ssl_context *ssl )
1430{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001431 size_t i;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001432 cipher_mode_t mode;
1433 int auth_done = 0;
Manuel Pégourié-Gonnard8e4b3372014-11-17 15:06:13 +01001434#if defined(POLARSSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01001435 size_t padlen = 0, correct = 1;
1436#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
1438 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1439
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001440 if( ssl->session_in == NULL || ssl->transform_in == NULL )
1441 {
1442 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1443 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1444 }
1445
1446 mode = cipher_get_cipher_mode( &ssl->transform_in->cipher_ctx_dec );
1447
Paul Bakker48916f92012-09-16 19:57:18 +00001448 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001449 {
1450 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001451 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001452 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001453 }
1454
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001455#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001456 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001457 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001458 int ret;
1459 size_t olen = 0;
1460
Paul Bakker68884e32013-01-07 18:20:04 +01001461 padlen = 0;
1462
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001463 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001464 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001465 ssl->transform_in->ivlen,
1466 ssl->in_msg, ssl->in_msglen,
1467 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001468 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001469 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001470 return( ret );
1471 }
1472
1473 if( ssl->in_msglen != olen )
1474 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001475 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001476 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001477 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 }
Paul Bakker68884e32013-01-07 18:20:04 +01001479 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001480#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001481#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1482 if( mode == POLARSSL_MODE_GCM ||
1483 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001484 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001485 int ret;
1486 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001487 unsigned char *dec_msg;
1488 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001489 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001490 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1491 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001492 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1493 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001494
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001495 if( ssl->in_msglen < explicit_iv_len + taglen )
1496 {
1497 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1498 "+ taglen (%d)", ssl->in_msglen,
1499 explicit_iv_len, taglen ) );
1500 return( POLARSSL_ERR_SSL_INVALID_MAC );
1501 }
1502 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1503
Paul Bakker68884e32013-01-07 18:20:04 +01001504 dec_msg = ssl->in_msg;
1505 dec_msg_result = ssl->in_msg;
1506 ssl->in_msglen = dec_msglen;
1507
1508 memcpy( add_data, ssl->in_ctr, 8 );
1509 add_data[8] = ssl->in_msgtype;
1510 add_data[9] = ssl->major_ver;
1511 add_data[10] = ssl->minor_ver;
1512 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1513 add_data[12] = ssl->in_msglen & 0xFF;
1514
1515 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1516 add_data, 13 );
1517
1518 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1519 ssl->in_iv,
1520 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1521
1522 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1523 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001524 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001525
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001526 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001527 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001528 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001529 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1530 ssl->transform_in->iv_dec,
1531 ssl->transform_in->ivlen,
1532 add_data, 13,
1533 dec_msg, dec_msglen,
1534 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001535 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001536 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001537 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1538
1539 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1540 return( POLARSSL_ERR_SSL_INVALID_MAC );
1541
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001542 return( ret );
1543 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001544 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001545
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001546 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001547 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001548 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001549 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001550 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001551 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001552 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001553#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001554#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1555 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001556 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001557 {
Paul Bakker45829992013-01-03 14:52:21 +01001558 /*
1559 * Decrypt and check the padding
1560 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001561 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001562 unsigned char *dec_msg;
1563 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001564 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001565 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001567
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 /*
Paul Bakker45829992013-01-03 14:52:21 +01001569 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001570 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001571#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001572 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1573 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001574#endif
Paul Bakker45829992013-01-03 14:52:21 +01001575
1576 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1577 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1578 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001579 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1580 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1581 ssl->transform_in->ivlen,
1582 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001583 return( POLARSSL_ERR_SSL_INVALID_MAC );
1584 }
1585
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001586 dec_msglen = ssl->in_msglen;
1587 dec_msg = ssl->in_msg;
1588 dec_msg_result = ssl->in_msg;
1589
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001590 /*
1591 * Authenticate before decrypt if enabled
1592 */
1593#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001594 if( ssl->session_in->encrypt_then_mac == SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001595 {
1596 unsigned char computed_mac[POLARSSL_SSL_MAX_MAC_SIZE];
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001597 unsigned char pseudo_hdr[13];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001598
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001599 SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
1600
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001601 dec_msglen -= ssl->transform_in->maclen;
1602 ssl->in_msglen -= ssl->transform_in->maclen;
1603
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01001604 memcpy( pseudo_hdr + 0, ssl->in_ctr, 8 );
1605 memcpy( pseudo_hdr + 8, ssl->in_hdr, 3 );
1606 pseudo_hdr[11] = (unsigned char)( ( ssl->in_msglen >> 8 ) & 0xFF );
1607 pseudo_hdr[12] = (unsigned char)( ( ssl->in_msglen ) & 0xFF );
1608
1609 SSL_DEBUG_BUF( 4, "MAC'd meta-data", pseudo_hdr, 13 );
1610
1611 md_hmac_update( &ssl->transform_in->md_ctx_dec, pseudo_hdr, 13 );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001612 md_hmac_update( &ssl->transform_in->md_ctx_dec,
1613 ssl->in_iv, ssl->in_msglen );
1614 md_hmac_finish( &ssl->transform_in->md_ctx_dec, computed_mac );
1615 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1616
1617 SSL_DEBUG_BUF( 4, "message mac", ssl->in_iv + ssl->in_msglen,
1618 ssl->transform_in->maclen );
1619 SSL_DEBUG_BUF( 4, "computed mac", computed_mac,
1620 ssl->transform_in->maclen );
1621
1622 if( safer_memcmp( ssl->in_iv + ssl->in_msglen, computed_mac,
1623 ssl->transform_in->maclen ) != 0 )
1624 {
1625 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
1626
1627 return( POLARSSL_ERR_SSL_INVALID_MAC );
1628 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001629 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001630 }
1631#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1632
1633 /*
1634 * Check length sanity
1635 */
1636 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
1637 {
1638 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
1639 ssl->in_msglen, ssl->transform_in->ivlen ) );
1640 return( POLARSSL_ERR_SSL_INVALID_MAC );
1641 }
1642
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001643#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001644 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001645 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001646 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001647 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001648 {
Paul Bakker48916f92012-09-16 19:57:18 +00001649 dec_msglen -= ssl->transform_in->ivlen;
1650 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001651
Paul Bakker48916f92012-09-16 19:57:18 +00001652 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001653 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001654 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001655#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001656
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001657 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001658 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001659 ssl->transform_in->ivlen,
1660 dec_msg, dec_msglen,
1661 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001662 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001663 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001664 return( ret );
1665 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001666
Paul Bakkercca5b812013-08-31 17:40:26 +02001667 if( dec_msglen != olen )
1668 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001669 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001670 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001671 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001672
1673#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001674 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1675 {
1676 /*
1677 * Save IV in SSL3 and TLS1
1678 */
1679 memcpy( ssl->transform_in->iv_dec,
1680 ssl->transform_in->cipher_ctx_dec.iv,
1681 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001683#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001686
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001687 if( ssl->in_msglen < ssl->transform_in->maclen + padlen &&
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001688 auth_done == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001689 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001690#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001691 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1692 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001693#endif
Paul Bakker45829992013-01-03 14:52:21 +01001694 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001695 correct = 0;
1696 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001698#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1700 {
Paul Bakker48916f92012-09-16 19:57:18 +00001701 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001703#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001704 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1705 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001706 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001707#endif
Paul Bakker45829992013-01-03 14:52:21 +01001708 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001709 }
1710 }
1711 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001712#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001713#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1714 defined(POLARSSL_SSL_PROTO_TLS1_2)
1715 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001716 {
1717 /*
Paul Bakker45829992013-01-03 14:52:21 +01001718 * TLSv1+: always check the padding up to the first failure
1719 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001720 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001721 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001722 size_t padding_idx = ssl->in_msglen - padlen - 1;
1723
Paul Bakker956c9e02013-12-19 14:42:28 +01001724 /*
1725 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001726 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001727 *
Paul Bakker61885c72014-04-25 12:59:03 +02001728 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1729 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001730 *
1731 * In both cases we reset padding_idx to a safe value (0) to
1732 * prevent out-of-buffer reads.
1733 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001734 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001735 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1736 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001737
1738 padding_idx *= correct;
1739
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001740 for( i = 1; i <= 256; i++ )
1741 {
1742 real_count &= ( i <= padlen );
1743 pad_count += real_count *
1744 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1745 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001746
1747 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001748
Paul Bakkerd66f0702013-01-31 16:57:45 +01001749#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001750 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001751 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001752#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001753 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001755 else
1756#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1757 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001758 {
1759 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001760 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001761 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001762
1763 ssl->in_msglen -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001765 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001766#endif /* POLARSSL_CIPHER_MODE_CBC &&
1767 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001768 {
1769 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001770 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001771 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001772
1773 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1774 ssl->in_msg, ssl->in_msglen );
1775
1776 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001777 * Authenticate if not done yet.
1778 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00001779 */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001780#if defined(POLARSSL_SOME_MODES_USE_MAC)
1781 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001782 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001783 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1784
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01001785 ssl->in_msglen -= ssl->transform_in->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001787 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1788 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001790 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001792#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001793 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1794 {
1795 ssl_mac( &ssl->transform_in->md_ctx_dec,
1796 ssl->transform_in->mac_dec,
1797 ssl->in_msg, ssl->in_msglen,
1798 ssl->in_ctr, ssl->in_msgtype );
1799 }
1800 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001801#endif /* POLARSSL_SSL_PROTO_SSL3 */
1802#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001803 defined(POLARSSL_SSL_PROTO_TLS1_2)
1804 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1805 {
1806 /*
1807 * Process MAC and always update for padlen afterwards to make
1808 * total time independent of padlen
1809 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001810 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001811 *
1812 * Known timing attacks:
1813 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1814 *
1815 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1816 * correctly. (We round down instead of up, so -56 is the correct
1817 * value for our calculations instead of -55)
1818 */
1819 size_t j, extra_run = 0;
1820 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1821 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001822
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001823 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001824
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001825 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1826 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1827 ssl->in_msglen );
1828 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1829 ssl->in_msg + ssl->in_msglen );
1830 for( j = 0; j < extra_run; j++ )
1831 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001832
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001833 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1834 }
1835 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001836#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001837 POLARSSL_SSL_PROTO_TLS1_2 */
1838 {
1839 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001840 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001841 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001843 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1844 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1845 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001847 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001848 ssl->transform_in->maclen ) != 0 )
1849 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001850#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001851 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001852#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001853 correct = 0;
1854 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001855 auth_done++;
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001857 /*
1858 * Finally check the correct flag
1859 */
1860 if( correct == 0 )
1861 return( POLARSSL_ERR_SSL_INVALID_MAC );
1862 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01001863#endif /* POLARSSL_SOME_MODES_USE_MAC */
1864
1865 /* Make extra sure authentication was performed, exactly once */
1866 if( auth_done != 1 )
1867 {
1868 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1869 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1870 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
1872 if( ssl->in_msglen == 0 )
1873 {
1874 ssl->nb_zero++;
1875
1876 /*
1877 * Three or more empty messages may be a DoS attack
1878 * (excessive CPU consumption).
1879 */
1880 if( ssl->nb_zero > 3 )
1881 {
1882 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1883 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001884 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 }
1886 }
1887 else
1888 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001889
Paul Bakker23986e52011-04-24 08:57:21 +00001890 for( i = 8; i > 0; i-- )
1891 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 break;
1893
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001894 /* The loops goes to its end iff the counter is wrapping */
1895 if( i == 0 )
1896 {
1897 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1898 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1899 }
1900
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1902
1903 return( 0 );
1904}
1905
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001906#undef MAC_NONE
1907#undef MAC_PLAINTEXT
1908#undef MAC_CIPHERTEXT
1909
Paul Bakker2770fbd2012-07-03 13:30:23 +00001910#if defined(POLARSSL_ZLIB_SUPPORT)
1911/*
1912 * Compression/decompression functions
1913 */
1914static int ssl_compress_buf( ssl_context *ssl )
1915{
1916 int ret;
1917 unsigned char *msg_post = ssl->out_msg;
1918 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001919 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001920
1921 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1922
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001923 if( len_pre == 0 )
1924 return( 0 );
1925
Paul Bakker2770fbd2012-07-03 13:30:23 +00001926 memcpy( msg_pre, ssl->out_msg, len_pre );
1927
1928 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1929 ssl->out_msglen ) );
1930
1931 SSL_DEBUG_BUF( 4, "before compression: output payload",
1932 ssl->out_msg, ssl->out_msglen );
1933
Paul Bakker48916f92012-09-16 19:57:18 +00001934 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1935 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1936 ssl->transform_out->ctx_deflate.next_out = msg_post;
1937 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001938
Paul Bakker48916f92012-09-16 19:57:18 +00001939 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001940 if( ret != Z_OK )
1941 {
1942 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1943 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1944 }
1945
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001946 ssl->out_msglen = SSL_BUFFER_LEN -
1947 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001948
Paul Bakker2770fbd2012-07-03 13:30:23 +00001949 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1950 ssl->out_msglen ) );
1951
1952 SSL_DEBUG_BUF( 4, "after compression: output payload",
1953 ssl->out_msg, ssl->out_msglen );
1954
1955 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1956
1957 return( 0 );
1958}
1959
1960static int ssl_decompress_buf( ssl_context *ssl )
1961{
1962 int ret;
1963 unsigned char *msg_post = ssl->in_msg;
1964 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001965 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001966
1967 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1968
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001969 if( len_pre == 0 )
1970 return( 0 );
1971
Paul Bakker2770fbd2012-07-03 13:30:23 +00001972 memcpy( msg_pre, ssl->in_msg, len_pre );
1973
1974 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1975 ssl->in_msglen ) );
1976
1977 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1978 ssl->in_msg, ssl->in_msglen );
1979
Paul Bakker48916f92012-09-16 19:57:18 +00001980 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1981 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1982 ssl->transform_in->ctx_inflate.next_out = msg_post;
1983 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001984
Paul Bakker48916f92012-09-16 19:57:18 +00001985 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001986 if( ret != Z_OK )
1987 {
1988 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1989 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1990 }
1991
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001992 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1993 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001994
Paul Bakker2770fbd2012-07-03 13:30:23 +00001995 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1996 ssl->in_msglen ) );
1997
1998 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1999 ssl->in_msg, ssl->in_msglen );
2000
2001 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
2002
2003 return( 0 );
2004}
2005#endif /* POLARSSL_ZLIB_SUPPORT */
2006
Paul Bakker5121ce52009-01-03 21:22:43 +00002007/*
2008 * Fill the input message buffer
2009 */
Paul Bakker23986e52011-04-24 08:57:21 +00002010int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00002011{
Paul Bakker23986e52011-04-24 08:57:21 +00002012 int ret;
2013 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002014
2015 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
2016
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002017 if( nb_want > SSL_BUFFER_LEN - 8 )
2018 {
2019 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
2020 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2021 }
2022
Paul Bakker5121ce52009-01-03 21:22:43 +00002023 while( ssl->in_left < nb_want )
2024 {
2025 len = nb_want - ssl->in_left;
2026 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
2027
2028 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
2029 ssl->in_left, nb_want ) );
2030 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
2031
Paul Bakker831a7552011-05-18 13:32:51 +00002032 if( ret == 0 )
2033 return( POLARSSL_ERR_SSL_CONN_EOF );
2034
Paul Bakker5121ce52009-01-03 21:22:43 +00002035 if( ret < 0 )
2036 return( ret );
2037
2038 ssl->in_left += ret;
2039 }
2040
2041 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
2042
2043 return( 0 );
2044}
2045
2046/*
2047 * Flush any data not yet written
2048 */
2049int ssl_flush_output( ssl_context *ssl )
2050{
2051 int ret;
2052 unsigned char *buf;
2053
2054 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
2055
2056 while( ssl->out_left > 0 )
2057 {
2058 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
2059 5 + ssl->out_msglen, ssl->out_left ) );
2060
Paul Bakker5bd42292012-12-19 14:40:42 +01002061 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00002062 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00002063
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
2065
2066 if( ret <= 0 )
2067 return( ret );
2068
2069 ssl->out_left -= ret;
2070 }
2071
2072 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
2073
2074 return( 0 );
2075}
2076
2077/*
2078 * Record layer functions
2079 */
2080int ssl_write_record( ssl_context *ssl )
2081{
Paul Bakker05ef8352012-05-08 09:17:57 +00002082 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00002083 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002084
2085 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
2086
Paul Bakker5121ce52009-01-03 21:22:43 +00002087 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
2088 {
2089 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
2090 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
2091 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
2092
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01002093 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
2094 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00002095 }
2096
Paul Bakker2770fbd2012-07-03 13:30:23 +00002097#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002098 if( ssl->transform_out != NULL &&
2099 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002100 {
2101 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
2102 {
2103 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
2104 return( ret );
2105 }
2106
2107 len = ssl->out_msglen;
2108 }
2109#endif /*POLARSSL_ZLIB_SUPPORT */
2110
Paul Bakker05ef8352012-05-08 09:17:57 +00002111#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002112 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 {
Paul Bakker05ef8352012-05-08 09:17:57 +00002114 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002115
Paul Bakker05ef8352012-05-08 09:17:57 +00002116 ret = ssl_hw_record_write( ssl );
2117 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2118 {
2119 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002120 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002122
2123 if( ret == 0 )
2124 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002125 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002126#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00002127 if( !done )
2128 {
2129 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
2130 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
2131 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00002132 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2133 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00002134
Paul Bakker48916f92012-09-16 19:57:18 +00002135 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002136 {
2137 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
2138 {
2139 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
2140 return( ret );
2141 }
2142
2143 len = ssl->out_msglen;
2144 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
2145 ssl->out_hdr[4] = (unsigned char)( len );
2146 }
2147
2148 ssl->out_left = 5 + ssl->out_msglen;
2149
2150 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
2151 "version = [%d:%d], msglen = %d",
2152 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2153 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2154
2155 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002156 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 }
2158
Paul Bakker5121ce52009-01-03 21:22:43 +00002159 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2160 {
2161 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2162 return( ret );
2163 }
2164
2165 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2166
2167 return( 0 );
2168}
2169
2170int ssl_read_record( ssl_context *ssl )
2171{
Paul Bakker05ef8352012-05-08 09:17:57 +00002172 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002173
2174 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2175
2176 if( ssl->in_hslen != 0 &&
2177 ssl->in_hslen < ssl->in_msglen )
2178 {
2179 /*
2180 * Get next Handshake message in the current record
2181 */
2182 ssl->in_msglen -= ssl->in_hslen;
2183
Paul Bakker8934a982011-08-05 11:11:53 +00002184 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002185 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
2187 ssl->in_hslen = 4;
2188 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2189
2190 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2191 " %d, type = %d, hslen = %d",
2192 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2193
2194 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2195 {
2196 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002197 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 }
2199
2200 if( ssl->in_msglen < ssl->in_hslen )
2201 {
2202 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002203 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 }
2205
Paul Bakker4224bc02014-04-08 14:36:50 +02002206 if( ssl->state != SSL_HANDSHAKE_OVER )
2207 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002208
2209 return( 0 );
2210 }
2211
2212 ssl->in_hslen = 0;
2213
2214 /*
2215 * Read the record header and validate it
2216 */
2217 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2218 {
2219 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2220 return( ret );
2221 }
2222
2223 ssl->in_msgtype = ssl->in_hdr[0];
2224 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2225
2226 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2227 "version = [%d:%d], msglen = %d",
2228 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2229 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2230
2231 if( ssl->in_hdr[1] != ssl->major_ver )
2232 {
2233 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002234 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 }
2236
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002237 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
2239 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002240 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002243 /* Sanity check (outer boundaries) */
2244 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2245 {
2246 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2247 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2248 }
2249
Paul Bakker5121ce52009-01-03 21:22:43 +00002250 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002251 * Make sure the message length is acceptable for the current transform
2252 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 */
Paul Bakker48916f92012-09-16 19:57:18 +00002254 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002256 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
2258 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002259 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 }
2261 }
2262 else
2263 {
Paul Bakker48916f92012-09-16 19:57:18 +00002264 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002265 {
2266 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002267 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002270#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002272 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
2274 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002275 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002278
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002279#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2280 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 /*
2282 * TLS encrypted messages can have up to 256 bytes of padding
2283 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002284 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002285 ssl->in_msglen > ssl->transform_in->minlen +
2286 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
2288 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002289 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002290 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
2293
2294 /*
2295 * Read and optionally decrypt the message contents
2296 */
2297 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2298 {
2299 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2300 return( ret );
2301 }
2302
2303 SSL_DEBUG_BUF( 4, "input record from network",
2304 ssl->in_hdr, 5 + ssl->in_msglen );
2305
Paul Bakker05ef8352012-05-08 09:17:57 +00002306#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002307 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002308 {
2309 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2310
2311 ret = ssl_hw_record_read( ssl );
2312 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2313 {
2314 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002315 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002316 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002317
2318 if( ret == 0 )
2319 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002320 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002321#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002322 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002323 {
2324 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2325 {
Paul Bakker40865c82013-01-31 17:13:13 +01002326#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2327 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2328 {
2329 ssl_send_alert_message( ssl,
2330 SSL_ALERT_LEVEL_FATAL,
2331 SSL_ALERT_MSG_BAD_RECORD_MAC );
2332 }
2333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2335 return( ret );
2336 }
2337
2338 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2339 ssl->in_msg, ssl->in_msglen );
2340
2341 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2342 {
2343 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002344 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002345 }
2346 }
2347
Paul Bakker2770fbd2012-07-03 13:30:23 +00002348#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002349 if( ssl->transform_in != NULL &&
2350 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002351 {
2352 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2353 {
2354 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2355 return( ret );
2356 }
2357
2358 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2359 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2360 }
2361#endif /* POLARSSL_ZLIB_SUPPORT */
2362
Paul Bakker0a925182012-04-16 06:46:41 +00002363 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2364 ssl->in_msgtype != SSL_MSG_ALERT &&
2365 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2366 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2367 {
2368 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2369
Paul Bakker48916f92012-09-16 19:57:18 +00002370 if( ( ret = ssl_send_alert_message( ssl,
2371 SSL_ALERT_LEVEL_FATAL,
2372 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002373 {
2374 return( ret );
2375 }
2376
2377 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2378 }
2379
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2381 {
2382 ssl->in_hslen = 4;
2383 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2384
2385 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2386 " %d, type = %d, hslen = %d",
2387 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2388
2389 /*
2390 * Additional checks to validate the handshake header
2391 */
2392 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2393 {
2394 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002395 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 }
2397
2398 if( ssl->in_msglen < ssl->in_hslen )
2399 {
2400 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002401 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 }
2403
Paul Bakker48916f92012-09-16 19:57:18 +00002404 if( ssl->state != SSL_HANDSHAKE_OVER )
2405 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 }
2407
2408 if( ssl->in_msgtype == SSL_MSG_ALERT )
2409 {
2410 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2411 ssl->in_msg[0], ssl->in_msg[1] ) );
2412
2413 /*
2414 * Ignore non-fatal alerts, except close_notify
2415 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002418 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2419 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002420 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 }
2422
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2424 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
2426 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002427 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002428 }
2429 }
2430
2431 ssl->in_left = 0;
2432
2433 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2434
2435 return( 0 );
2436}
2437
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002438int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2439{
2440 int ret;
2441
2442 if( ( ret = ssl_send_alert_message( ssl,
2443 SSL_ALERT_LEVEL_FATAL,
2444 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2445 {
2446 return( ret );
2447 }
2448
2449 return( 0 );
2450}
2451
Paul Bakker0a925182012-04-16 06:46:41 +00002452int ssl_send_alert_message( ssl_context *ssl,
2453 unsigned char level,
2454 unsigned char message )
2455{
2456 int ret;
2457
2458 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2459
2460 ssl->out_msgtype = SSL_MSG_ALERT;
2461 ssl->out_msglen = 2;
2462 ssl->out_msg[0] = level;
2463 ssl->out_msg[1] = message;
2464
2465 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2466 {
2467 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2468 return( ret );
2469 }
2470
2471 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2472
2473 return( 0 );
2474}
2475
Paul Bakker5121ce52009-01-03 21:22:43 +00002476/*
2477 * Handshake functions
2478 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002479#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2480 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2481 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2482 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2483 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2484 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2485 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002486int ssl_write_certificate( ssl_context *ssl )
2487{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002488 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002489
2490 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2491
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002492 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002493 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2494 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002495 {
2496 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2497 ssl->state++;
2498 return( 0 );
2499 }
2500
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002501 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2502 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002503}
2504
2505int ssl_parse_certificate( ssl_context *ssl )
2506{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2508
2509 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2510
2511 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002512 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2513 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002514 {
2515 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2516 ssl->state++;
2517 return( 0 );
2518 }
2519
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002520 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2521 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522}
2523#else
2524int ssl_write_certificate( ssl_context *ssl )
2525{
2526 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2527 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002528 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002529 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2530
2531 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2532
2533 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002534 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2535 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536 {
2537 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2538 ssl->state++;
2539 return( 0 );
2540 }
2541
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002542#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002543 if( ssl->endpoint == SSL_IS_CLIENT )
2544 {
2545 if( ssl->client_auth == 0 )
2546 {
2547 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2548 ssl->state++;
2549 return( 0 );
2550 }
2551
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002552#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 /*
2554 * If using SSLv3 and got no cert, send an Alert message
2555 * (otherwise an empty Certificate message will be sent).
2556 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002557 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002558 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2559 {
2560 ssl->out_msglen = 2;
2561 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002562 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2563 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
2565 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2566 goto write_msg;
2567 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002568#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002569 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002570#endif /* POLARSSL_SSL_CLI_C */
2571#if defined(POLARSSL_SSL_SRV_C)
2572 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00002573 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002574 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 {
2576 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002577 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578 }
2579 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002580#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002581
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002582 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583
2584 /*
2585 * 0 . 0 handshake type
2586 * 1 . 3 handshake length
2587 * 4 . 6 length of all certs
2588 * 7 . 9 length of cert. 1
2589 * 10 . n-1 peer certificate
2590 * n . n+2 length of cert. 2
2591 * n+3 . ... upper level cert, etc.
2592 */
2593 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002594 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002595
Paul Bakker29087132010-03-21 21:03:34 +00002596 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002597 {
2598 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002599 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 {
2601 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2602 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002603 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 }
2605
2606 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2607 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2608 ssl->out_msg[i + 2] = (unsigned char)( n );
2609
2610 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2611 i += n; crt = crt->next;
2612 }
2613
2614 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2615 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2616 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2617
2618 ssl->out_msglen = i;
2619 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2620 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2621
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002622#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002623write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002624#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002625
2626 ssl->state++;
2627
2628 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2629 {
2630 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2631 return( ret );
2632 }
2633
2634 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2635
Paul Bakkered27a042013-04-18 22:46:23 +02002636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637}
2638
2639int ssl_parse_certificate( ssl_context *ssl )
2640{
Paul Bakkered27a042013-04-18 22:46:23 +02002641 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002642 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002643 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2646
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002647 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002648 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2649 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002650 {
2651 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2652 ssl->state++;
2653 return( 0 );
2654 }
2655
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002656#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002658 ( ssl->authmode == SSL_VERIFY_NONE ||
2659 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002661 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2663 ssl->state++;
2664 return( 0 );
2665 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002667
2668 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2669 {
2670 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2671 return( ret );
2672 }
2673
2674 ssl->state++;
2675
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002676#if defined(POLARSSL_SSL_SRV_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002677#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 /*
2679 * Check if the client sent an empty certificate
2680 */
2681 if( ssl->endpoint == SSL_IS_SERVER &&
2682 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2683 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002684 if( ssl->in_msglen == 2 &&
2685 ssl->in_msgtype == SSL_MSG_ALERT &&
2686 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2687 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002688 {
2689 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2690
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002691 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2693 return( 0 );
2694 else
Paul Bakker40e46942009-01-03 21:51:57 +00002695 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 }
2697 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002698#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002700#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2701 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 if( ssl->endpoint == SSL_IS_SERVER &&
2703 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2704 {
2705 if( ssl->in_hslen == 7 &&
2706 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2707 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2708 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2711
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002712 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002714 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 else
2716 return( 0 );
2717 }
2718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2720 POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01002721#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
2723 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2724 {
2725 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002726 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002727 }
2728
2729 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2730 {
2731 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002732 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
2734
2735 /*
2736 * Same message structure as in ssl_write_certificate()
2737 */
2738 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2739
2740 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2741 {
2742 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002743 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 }
2745
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002746 /* In case we tried to reuse a session but it failed */
2747 if( ssl->session_negotiate->peer_cert != NULL )
2748 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002749 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002750 polarssl_free( ssl->session_negotiate->peer_cert );
2751 }
2752
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002753 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2754 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002755 {
2756 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002757 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002758 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
Paul Bakkerb6b09562013-09-18 14:17:41 +02002761 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002762
2763 i = 7;
2764
2765 while( i < ssl->in_hslen )
2766 {
2767 if( ssl->in_msg[i] != 0 )
2768 {
2769 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002770 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 }
2772
2773 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2774 | (unsigned int) ssl->in_msg[i + 2];
2775 i += 3;
2776
2777 if( n < 128 || i + n > ssl->in_hslen )
2778 {
2779 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002780 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 }
2782
Paul Bakkerddf26b42013-09-18 13:46:23 +02002783 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2784 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785 if( ret != 0 )
2786 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002787 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002788 return( ret );
2789 }
2790
2791 i += n;
2792 }
2793
Paul Bakker48916f92012-09-16 19:57:18 +00002794 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002796 /*
2797 * On client, make sure the server cert doesn't change during renego to
2798 * avoid "triple handshake" attack: https://secure-resumption.com/
2799 */
Paul Bakkerf6080b82015-01-13 16:18:23 +01002800#if defined(POLARSSL_SSL_RENEGOTIATION) && defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002801 if( ssl->endpoint == SSL_IS_CLIENT &&
2802 ssl->renegotiation == SSL_RENEGOTIATION )
2803 {
2804 if( ssl->session->peer_cert == NULL )
2805 {
2806 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2807 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2808 }
2809
2810 if( ssl->session->peer_cert->raw.len !=
2811 ssl->session_negotiate->peer_cert->raw.len ||
2812 memcmp( ssl->session->peer_cert->raw.p,
2813 ssl->session_negotiate->peer_cert->raw.p,
2814 ssl->session->peer_cert->raw.len ) != 0 )
2815 {
2816 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2817 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2818 }
2819 }
Paul Bakkerf6080b82015-01-13 16:18:23 +01002820#endif /* POLARSSL_SSL_RENEGOTIATION && POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002821
Paul Bakker5121ce52009-01-03 21:22:43 +00002822 if( ssl->authmode != SSL_VERIFY_NONE )
2823 {
2824 if( ssl->ca_chain == NULL )
2825 {
2826 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002827 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 }
2829
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002830 /*
2831 * Main check: verify certificate
2832 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002833 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2834 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2835 &ssl->session_negotiate->verify_result,
2836 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837
2838 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002839 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002841 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002842
2843 /*
2844 * Secondary checks: always done, but change 'ret' only if it was 0
2845 */
2846
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002847#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002848 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002849 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002850
2851 /* If certificate uses an EC key, make sure the curve is OK */
2852 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2853 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2854 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002855 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002856 if( ret == 0 )
2857 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002858 }
2859 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002860#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002862 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2863 ciphersuite_info,
2864 ! ssl->endpoint ) != 0 )
2865 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002866 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002867 if( ret == 0 )
2868 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2869 }
2870
Paul Bakker5121ce52009-01-03 21:22:43 +00002871 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2872 ret = 0;
2873 }
2874
2875 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2876
2877 return( ret );
2878}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002879#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2880 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2881 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2882 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2883 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2884 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2885 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002886
2887int ssl_write_change_cipher_spec( ssl_context *ssl )
2888{
2889 int ret;
2890
2891 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2892
2893 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2894 ssl->out_msglen = 1;
2895 ssl->out_msg[0] = 1;
2896
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 ssl->state++;
2898
2899 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2900 {
2901 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2902 return( ret );
2903 }
2904
2905 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2906
2907 return( 0 );
2908}
2909
2910int ssl_parse_change_cipher_spec( ssl_context *ssl )
2911{
2912 int ret;
2913
2914 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2915
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2917 {
2918 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2919 return( ret );
2920 }
2921
2922 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2923 {
2924 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002925 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
2927
2928 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2929 {
2930 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002931 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 }
2933
2934 ssl->state++;
2935
2936 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2937
2938 return( 0 );
2939}
2940
Paul Bakker41c83d32013-03-20 14:39:14 +01002941void ssl_optimize_checksum( ssl_context *ssl,
2942 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002943{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002944 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002945
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2947 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002948 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002949 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002950 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#endif
2952#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2953#if defined(POLARSSL_SHA512_C)
2954 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2955 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2956 else
2957#endif
2958#if defined(POLARSSL_SHA256_C)
2959 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002960 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002961 else
2962#endif
2963#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002964 {
2965 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002966 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002967 }
Paul Bakker380da532012-04-18 16:10:25 +00002968}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002969
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002970static void ssl_update_checksum_start( ssl_context *ssl,
2971 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002972{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002973#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2974 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002975 md5_update( &ssl->handshake->fin_md5 , buf, len );
2976 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002977#endif
2978#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2979#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002980 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002981#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002982#if defined(POLARSSL_SHA512_C)
2983 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002984#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002985#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002986}
2987
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002988#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2989 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002990static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2991 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002992{
Paul Bakker48916f92012-09-16 19:57:18 +00002993 md5_update( &ssl->handshake->fin_md5 , buf, len );
2994 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002995}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002996#endif
Paul Bakker380da532012-04-18 16:10:25 +00002997
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002998#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2999#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003000static void ssl_update_checksum_sha256( ssl_context *ssl,
3001 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003002{
Paul Bakker9e36f042013-06-30 14:34:05 +02003003 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003004}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003005#endif
Paul Bakker380da532012-04-18 16:10:25 +00003006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003008static void ssl_update_checksum_sha384( ssl_context *ssl,
3009 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00003010{
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00003012}
Paul Bakker769075d2012-11-24 11:26:46 +01003013#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003014#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00003015
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003016#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003017static void ssl_calc_finished_ssl(
3018 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003019{
Paul Bakker3c2122f2013-06-24 19:03:14 +02003020 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003021 md5_context md5;
3022 sha1_context sha1;
3023
Paul Bakker5121ce52009-01-03 21:22:43 +00003024 unsigned char padbuf[48];
3025 unsigned char md5sum[16];
3026 unsigned char sha1sum[20];
3027
Paul Bakker48916f92012-09-16 19:57:18 +00003028 ssl_session *session = ssl->session_negotiate;
3029 if( !session )
3030 session = ssl->session;
3031
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
3033
Paul Bakker48916f92012-09-16 19:57:18 +00003034 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3035 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003036
3037 /*
3038 * SSLv3:
3039 * hash =
3040 * MD5( master + pad2 +
3041 * MD5( handshake + sender + master + pad1 ) )
3042 * + SHA1( master + pad2 +
3043 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 */
3045
Paul Bakker90995b52013-06-24 19:20:35 +02003046#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003048 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakker90995b52013-06-24 19:20:35 +02003051#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00003052 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003053 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003055
Paul Bakker3c2122f2013-06-24 19:03:14 +02003056 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
3057 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Paul Bakker1ef83d62012-04-11 12:09:53 +00003059 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060
Paul Bakker3c2122f2013-06-24 19:03:14 +02003061 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003062 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003063 md5_update( &md5, padbuf, 48 );
3064 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003065
Paul Bakker3c2122f2013-06-24 19:03:14 +02003066 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00003067 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003068 sha1_update( &sha1, padbuf, 40 );
3069 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00003070
Paul Bakker1ef83d62012-04-11 12:09:53 +00003071 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003072
Paul Bakker1ef83d62012-04-11 12:09:53 +00003073 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00003074 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003075 md5_update( &md5, padbuf, 48 );
3076 md5_update( &md5, md5sum, 16 );
3077 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078
Paul Bakker1ef83d62012-04-11 12:09:53 +00003079 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00003080 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 sha1_update( &sha1, padbuf , 40 );
3082 sha1_update( &sha1, sha1sum, 20 );
3083 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003084
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003086
Paul Bakker5b4af392014-06-26 12:09:34 +02003087 md5_free( &md5 );
3088 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Paul Bakker34617722014-06-13 17:20:13 +02003090 polarssl_zeroize( padbuf, sizeof( padbuf ) );
3091 polarssl_zeroize( md5sum, sizeof( md5sum ) );
3092 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003093
3094 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3095}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003096#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003098#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003099static void ssl_calc_finished_tls(
3100 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00003101{
Paul Bakker1ef83d62012-04-11 12:09:53 +00003102 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003103 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003104 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00003105 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003106 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Paul Bakker48916f92012-09-16 19:57:18 +00003108 ssl_session *session = ssl->session_negotiate;
3109 if( !session )
3110 session = ssl->session;
3111
Paul Bakker1ef83d62012-04-11 12:09:53 +00003112 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Paul Bakker48916f92012-09-16 19:57:18 +00003114 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
3115 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003116
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117 /*
3118 * TLSv1:
3119 * hash = PRF( master, finished_label,
3120 * MD5( handshake ) + SHA1( handshake ) )[0..11]
3121 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Paul Bakker90995b52013-06-24 19:20:35 +02003123#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
3125 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003126#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003127
Paul Bakker90995b52013-06-24 19:20:35 +02003128#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
3130 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003131#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003132
3133 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003134 ? "client finished"
3135 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003136
3137 md5_finish( &md5, padbuf );
3138 sha1_finish( &sha1, padbuf + 16 );
3139
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, 36, 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 md5_free( &md5 );
3146 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003147
Paul Bakker34617722014-06-13 17:20:13 +02003148 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003149
3150 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3151}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003152#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003153
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003154#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3155#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003156static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00003157 ssl_context *ssl, unsigned char *buf, int from )
3158{
3159 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003160 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003161 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003162 unsigned char padbuf[32];
3163
Paul Bakker48916f92012-09-16 19:57:18 +00003164 ssl_session *session = ssl->session_negotiate;
3165 if( !session )
3166 session = ssl->session;
3167
Paul Bakker380da532012-04-18 16:10:25 +00003168 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003169
Paul Bakker9e36f042013-06-30 14:34:05 +02003170 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003171
3172 /*
3173 * TLSv1.2:
3174 * hash = PRF( master, finished_label,
3175 * Hash( handshake ) )[0.11]
3176 */
3177
Paul Bakker9e36f042013-06-30 14:34:05 +02003178#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003179 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003180 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003181#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003182
3183 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003184 ? "client finished"
3185 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003186
Paul Bakker9e36f042013-06-30 14:34:05 +02003187 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003188
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003189 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003190 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003191
3192 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3193
Paul Bakker5b4af392014-06-26 12:09:34 +02003194 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003195
Paul Bakker34617722014-06-13 17:20:13 +02003196 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003197
3198 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3199}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003200#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003201
Paul Bakker9e36f042013-06-30 14:34:05 +02003202#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003203static void ssl_calc_finished_tls_sha384(
3204 ssl_context *ssl, unsigned char *buf, int from )
3205{
3206 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003207 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003208 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003209 unsigned char padbuf[48];
3210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ssl_session *session = ssl->session_negotiate;
3212 if( !session )
3213 session = ssl->session;
3214
Paul Bakker380da532012-04-18 16:10:25 +00003215 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003216
Paul Bakker9e36f042013-06-30 14:34:05 +02003217 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003218
3219 /*
3220 * TLSv1.2:
3221 * hash = PRF( master, finished_label,
3222 * Hash( handshake ) )[0.11]
3223 */
3224
Paul Bakker9e36f042013-06-30 14:34:05 +02003225#if !defined(POLARSSL_SHA512_ALT)
3226 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3227 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003228#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003229
3230 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003231 ? "client finished"
3232 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003233
Paul Bakker9e36f042013-06-30 14:34:05 +02003234 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003235
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003236 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003237 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003238
3239 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3240
Paul Bakker5b4af392014-06-26 12:09:34 +02003241 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003242
Paul Bakker34617722014-06-13 17:20:13 +02003243 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003244
3245 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3246}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003247#endif /* POLARSSL_SHA512_C */
3248#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003249
Paul Bakker48916f92012-09-16 19:57:18 +00003250void ssl_handshake_wrapup( ssl_context *ssl )
3251{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003252 int resume = ssl->handshake->resume;
3253
Paul Bakker48916f92012-09-16 19:57:18 +00003254 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3255
3256 /*
3257 * Free our handshake params
3258 */
3259 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003260 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003261 ssl->handshake = NULL;
3262
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003263#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003264 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003265 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003266 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003267 ssl->renego_records_seen = 0;
3268 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003269#endif
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003270
Paul Bakker48916f92012-09-16 19:57:18 +00003271 /*
3272 * Switch in our now active transform context
3273 */
3274 if( ssl->transform )
3275 {
3276 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003277 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003278 }
3279 ssl->transform = ssl->transform_negotiate;
3280 ssl->transform_negotiate = NULL;
3281
Paul Bakker0a597072012-09-25 21:55:46 +00003282 if( ssl->session )
3283 {
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01003284#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3285 /* RFC 7366 3.1: keep the EtM state */
3286 ssl->session_negotiate->encrypt_then_mac =
3287 ssl->session->encrypt_then_mac;
3288#endif
3289
Paul Bakker0a597072012-09-25 21:55:46 +00003290 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003291 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003292 }
3293 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003294 ssl->session_negotiate = NULL;
3295
Paul Bakker0a597072012-09-25 21:55:46 +00003296 /*
3297 * Add cache entry
3298 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003299 if( ssl->f_set_cache != NULL &&
3300 ssl->session->length != 0 &&
3301 resume == 0 )
3302 {
Paul Bakker0a597072012-09-25 21:55:46 +00003303 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3304 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003305 }
Paul Bakker0a597072012-09-25 21:55:46 +00003306
Paul Bakker48916f92012-09-16 19:57:18 +00003307 ssl->state++;
3308
3309 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3310}
3311
Paul Bakker1ef83d62012-04-11 12:09:53 +00003312int ssl_write_finished( ssl_context *ssl )
3313{
3314 int ret, hash_len;
3315
3316 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3317
Paul Bakker92be97b2013-01-02 17:30:03 +01003318 /*
3319 * Set the out_msg pointer to the correct location based on IV length
3320 */
3321 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3322 {
3323 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3324 ssl->transform_negotiate->fixed_ivlen;
3325 }
3326 else
3327 ssl->out_msg = ssl->out_iv;
3328
Paul Bakker48916f92012-09-16 19:57:18 +00003329 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003330
3331 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3333
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003334#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003335 ssl->verify_data_len = hash_len;
3336 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003337#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003338
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 ssl->out_msglen = 4 + hash_len;
3340 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3341 ssl->out_msg[0] = SSL_HS_FINISHED;
3342
3343 /*
3344 * In case of session resuming, invert the client and server
3345 * ChangeCipherSpec messages order.
3346 */
Paul Bakker0a597072012-09-25 21:55:46 +00003347 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003349#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003350 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003351 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003352#endif
3353#if defined(POLARSSL_SSL_SRV_C)
3354 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00003355 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 }
3358 else
3359 ssl->state++;
3360
Paul Bakker48916f92012-09-16 19:57:18 +00003361 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003362 * Switch to our negotiated transform and session parameters for outbound
3363 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003364 */
3365 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3366 ssl->transform_out = ssl->transform_negotiate;
3367 ssl->session_out = ssl->session_negotiate;
3368 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003369
Paul Bakker07eb38b2012-12-19 14:42:06 +01003370#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003371 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003372 {
3373 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3374 {
3375 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3376 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3377 }
3378 }
3379#endif
3380
Paul Bakker5121ce52009-01-03 21:22:43 +00003381 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3382 {
3383 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3384 return( ret );
3385 }
3386
3387 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3388
3389 return( 0 );
3390}
3391
3392int ssl_parse_finished( ssl_context *ssl )
3393{
Paul Bakker23986e52011-04-24 08:57:21 +00003394 int ret;
3395 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 unsigned char buf[36];
3397
3398 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3399
Paul Bakker48916f92012-09-16 19:57:18 +00003400 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401
Paul Bakker48916f92012-09-16 19:57:18 +00003402 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003403 * Switch to our negotiated transform and session parameters for inbound
3404 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003405 */
3406 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3407 ssl->transform_in = ssl->transform_negotiate;
3408 ssl->session_in = ssl->session_negotiate;
3409 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003410
Paul Bakker92be97b2013-01-02 17:30:03 +01003411 /*
3412 * Set the in_msg pointer to the correct location based on IV length
3413 */
3414 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3415 {
3416 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3417 ssl->transform_negotiate->fixed_ivlen;
3418 }
3419 else
3420 ssl->in_msg = ssl->in_iv;
3421
Paul Bakker07eb38b2012-12-19 14:42:06 +01003422#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003423 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003424 {
3425 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3426 {
3427 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3428 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3429 }
3430 }
3431#endif
3432
Paul Bakker5121ce52009-01-03 21:22:43 +00003433 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3434 {
3435 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3436 return( ret );
3437 }
3438
3439 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3440 {
3441 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003442 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003443 }
3444
Paul Bakker1ef83d62012-04-11 12:09:53 +00003445 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003446 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3447
3448 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3449 ssl->in_hslen != 4 + hash_len )
3450 {
3451 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003452 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 }
3454
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003455 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003456 {
3457 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003458 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003461#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003462 ssl->verify_data_len = hash_len;
3463 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003464#endif
Paul Bakker48916f92012-09-16 19:57:18 +00003465
Paul Bakker0a597072012-09-25 21:55:46 +00003466 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003467 {
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003468#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 if( ssl->endpoint == SSL_IS_CLIENT )
3470 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003471#endif
3472#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003473 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003474 ssl->state = SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003476 }
3477 else
3478 ssl->state++;
3479
3480 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3481
3482 return( 0 );
3483}
3484
Paul Bakker968afaa2014-07-09 11:09:24 +02003485static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003486{
3487 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3488
3489#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3490 defined(POLARSSL_SSL_PROTO_TLS1_1)
3491 md5_init( &handshake->fin_md5 );
3492 sha1_init( &handshake->fin_sha1 );
3493 md5_starts( &handshake->fin_md5 );
3494 sha1_starts( &handshake->fin_sha1 );
3495#endif
3496#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3497#if defined(POLARSSL_SHA256_C)
3498 sha256_init( &handshake->fin_sha256 );
3499 sha256_starts( &handshake->fin_sha256, 0 );
3500#endif
3501#if defined(POLARSSL_SHA512_C)
3502 sha512_init( &handshake->fin_sha512 );
3503 sha512_starts( &handshake->fin_sha512, 1 );
3504#endif
3505#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3506
3507 handshake->update_checksum = ssl_update_checksum_start;
3508 handshake->sig_alg = SSL_HASH_SHA1;
3509
3510#if defined(POLARSSL_DHM_C)
3511 dhm_init( &handshake->dhm_ctx );
3512#endif
3513#if defined(POLARSSL_ECDH_C)
3514 ecdh_init( &handshake->ecdh_ctx );
3515#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003516}
3517
3518static void ssl_transform_init( ssl_transform *transform )
3519{
3520 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003521
3522 cipher_init( &transform->cipher_ctx_enc );
3523 cipher_init( &transform->cipher_ctx_dec );
3524
3525 md_init( &transform->md_ctx_enc );
3526 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003527}
3528
3529void ssl_session_init( ssl_session *session )
3530{
3531 memset( session, 0, sizeof(ssl_session) );
3532}
3533
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003534static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003535{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003536 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003537 if( ssl->transform_negotiate )
3538 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003539 if( ssl->session_negotiate )
3540 ssl_session_free( ssl->session_negotiate );
3541 if( ssl->handshake )
3542 ssl_handshake_free( ssl->handshake );
3543
3544 /*
3545 * Either the pointers are now NULL or cleared properly and can be freed.
3546 * Now allocate missing structures.
3547 */
3548 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003549 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003550 ssl->transform_negotiate = (ssl_transform *) polarssl_malloc(
3551 sizeof(ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003552 }
Paul Bakker48916f92012-09-16 19:57:18 +00003553
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003554 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003555 {
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +01003556 ssl->session_negotiate = (ssl_session *) polarssl_malloc(
3557 sizeof(ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003558 }
Paul Bakker48916f92012-09-16 19:57:18 +00003559
Paul Bakker82788fb2014-10-20 13:59:19 +02003560 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003561 {
3562 ssl->handshake = (ssl_handshake_params *)
3563 polarssl_malloc( sizeof(ssl_handshake_params) );
3564 }
Paul Bakker48916f92012-09-16 19:57:18 +00003565
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003566 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003567 if( ssl->handshake == NULL ||
3568 ssl->transform_negotiate == NULL ||
3569 ssl->session_negotiate == NULL )
3570 {
3571 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003572
3573 polarssl_free( ssl->handshake );
3574 polarssl_free( ssl->transform_negotiate );
3575 polarssl_free( ssl->session_negotiate );
3576
3577 ssl->handshake = NULL;
3578 ssl->transform_negotiate = NULL;
3579 ssl->session_negotiate = NULL;
3580
Paul Bakker48916f92012-09-16 19:57:18 +00003581 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3582 }
3583
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003584 /* Initialize structures */
3585 ssl_session_init( ssl->session_negotiate );
3586 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003587 ssl_handshake_params_init( ssl->handshake );
3588
3589#if defined(POLARSSL_X509_CRT_PARSE_C)
3590 ssl->handshake->key_cert = ssl->key_cert;
3591#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003592
Paul Bakker48916f92012-09-16 19:57:18 +00003593 return( 0 );
3594}
3595
Paul Bakker5121ce52009-01-03 21:22:43 +00003596/*
3597 * Initialize an SSL context
3598 */
3599int ssl_init( ssl_context *ssl )
3600{
Paul Bakker48916f92012-09-16 19:57:18 +00003601 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003602 int len = SSL_BUFFER_LEN;
3603
3604 memset( ssl, 0, sizeof( ssl_context ) );
3605
Paul Bakker62f2dee2012-09-28 07:31:51 +00003606 /*
3607 * Sane defaults
3608 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003609 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3610 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3611 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3612 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003613
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003614 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003615
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003616#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003617 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003618 memset( ssl->renego_period, 0xFF, 7 );
3619 ssl->renego_period[7] = 0x00;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003620#endif
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003621
Paul Bakker62f2dee2012-09-28 07:31:51 +00003622#if defined(POLARSSL_DHM_C)
3623 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3624 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3625 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3626 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3627 {
3628 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3629 return( ret );
3630 }
3631#endif
3632
3633 /*
3634 * Prepare base structures
3635 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003636 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003638 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 ssl->in_msg = ssl->in_ctr + 13;
3640
3641 if( ssl->in_ctr == NULL )
3642 {
3643 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003644 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 }
3646
Paul Bakker6e339b52013-07-03 13:37:05 +02003647 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003649 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003650 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003651
3652 if( ssl->out_ctr == NULL )
3653 {
3654 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003655 polarssl_free( ssl->in_ctr );
3656 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003657 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 }
3659
3660 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3661 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3662
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003663#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
3664 ssl->encrypt_then_mac = SSL_ETM_ENABLED;
3665#endif
3666
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003667#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3668 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3669#endif
3670
Paul Bakker606b4ba2013-08-14 16:52:14 +02003671#if defined(POLARSSL_SSL_SESSION_TICKETS)
3672 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3673#endif
3674
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003675#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003676 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003677#endif
3678
Paul Bakker48916f92012-09-16 19:57:18 +00003679 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3680 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003681
3682 return( 0 );
3683}
3684
3685/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003686 * Reset an initialized and used SSL context for re-use while retaining
3687 * all application-set variables, function pointers and data.
3688 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003689int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003690{
Paul Bakker48916f92012-09-16 19:57:18 +00003691 int ret;
3692
Paul Bakker7eb013f2011-10-06 12:37:39 +00003693 ssl->state = SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003694
3695#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00003696 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003697 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003698
3699 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard61860192014-11-04 13:05:42 +01003700 memset( ssl->own_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
3701 memset( ssl->peer_verify_data, 0, SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003702#endif
3703 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00003704
Paul Bakker7eb013f2011-10-06 12:37:39 +00003705 ssl->in_offt = NULL;
3706
Paul Bakker92be97b2013-01-02 17:30:03 +01003707 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003708 ssl->in_msgtype = 0;
3709 ssl->in_msglen = 0;
3710 ssl->in_left = 0;
3711
3712 ssl->in_hslen = 0;
3713 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003714 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003715
Paul Bakker92be97b2013-01-02 17:30:03 +01003716 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003717 ssl->out_msgtype = 0;
3718 ssl->out_msglen = 0;
3719 ssl->out_left = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003720#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01003721 if( ssl->split_done != SSL_CBC_RECORD_SPLITTING_DISABLED )
3722 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01003723#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00003724
Paul Bakker48916f92012-09-16 19:57:18 +00003725 ssl->transform_in = NULL;
3726 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003727
3728 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3729 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003730
3731#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003732 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003733 {
3734 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003735 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003736 {
3737 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3738 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3739 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003740 }
3741#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003742
Paul Bakker48916f92012-09-16 19:57:18 +00003743 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003744 {
Paul Bakker48916f92012-09-16 19:57:18 +00003745 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003746 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003747 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003748 }
Paul Bakker48916f92012-09-16 19:57:18 +00003749
Paul Bakkerc0463502013-02-14 11:19:38 +01003750 if( ssl->session )
3751 {
3752 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003753 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003754 ssl->session = NULL;
3755 }
3756
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003757#if defined(POLARSSL_SSL_ALPN)
3758 ssl->alpn_chosen = NULL;
3759#endif
3760
Paul Bakker48916f92012-09-16 19:57:18 +00003761 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3762 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003763
3764 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003765}
3766
Paul Bakkera503a632013-08-14 13:48:06 +02003767#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003768static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3769{
3770 aes_free( &tkeys->enc );
3771 aes_free( &tkeys->dec );
3772
3773 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3774}
3775
Paul Bakker7eb013f2011-10-06 12:37:39 +00003776/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003777 * Allocate and initialize ticket keys
3778 */
3779static int ssl_ticket_keys_init( ssl_context *ssl )
3780{
3781 int ret;
3782 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003783 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003784
3785 if( ssl->ticket_keys != NULL )
3786 return( 0 );
3787
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003788 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3789 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003790 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3791
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003792 aes_init( &tkeys->enc );
3793 aes_init( &tkeys->dec );
3794
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003795 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003796 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003797 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003798 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003799 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003800 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003801
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003802 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3803 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3804 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3805 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003806 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003807 polarssl_free( tkeys );
3808 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003809 }
3810
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003811 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003812 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003813 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003814 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003815 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003816 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003817
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003818 ssl->ticket_keys = tkeys;
3819
3820 return( 0 );
3821}
Paul Bakkera503a632013-08-14 13:48:06 +02003822#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003823
3824/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003825 * SSL set accessors
3826 */
3827void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3828{
3829 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003830
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003831#if defined(POLARSSL_SSL_SESSION_TICKETS) && \
3832 defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003833 if( endpoint == SSL_IS_CLIENT )
3834 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003836}
3837
3838void ssl_set_authmode( ssl_context *ssl, int authmode )
3839{
3840 ssl->authmode = authmode;
3841}
3842
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003843#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003844void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003845 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003846 void *p_vrfy )
3847{
3848 ssl->f_vrfy = f_vrfy;
3849 ssl->p_vrfy = p_vrfy;
3850}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003851#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003852
Paul Bakker5121ce52009-01-03 21:22:43 +00003853void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003854 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003855 void *p_rng )
3856{
3857 ssl->f_rng = f_rng;
3858 ssl->p_rng = p_rng;
3859}
3860
3861void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003862 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 void *p_dbg )
3864{
3865 ssl->f_dbg = f_dbg;
3866 ssl->p_dbg = p_dbg;
3867}
3868
3869void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003870 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003871 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003872{
3873 ssl->f_recv = f_recv;
3874 ssl->f_send = f_send;
3875 ssl->p_recv = p_recv;
3876 ssl->p_send = p_send;
3877}
3878
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003879#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker0a597072012-09-25 21:55:46 +00003880void ssl_set_session_cache( ssl_context *ssl,
3881 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3882 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003883{
Paul Bakker0a597072012-09-25 21:55:46 +00003884 ssl->f_get_cache = f_get_cache;
3885 ssl->p_get_cache = p_get_cache;
3886 ssl->f_set_cache = f_set_cache;
3887 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003888}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003889#endif /* POLARSSL_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003890
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003891#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003892int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003893{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003894 int ret;
3895
3896 if( ssl == NULL ||
3897 session == NULL ||
3898 ssl->session_negotiate == NULL ||
3899 ssl->endpoint != SSL_IS_CLIENT )
3900 {
3901 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3902 }
3903
3904 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3905 return( ret );
3906
Paul Bakker0a597072012-09-25 21:55:46 +00003907 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003908
3909 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003910}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01003911#endif /* POLARSSL_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003912
Paul Bakkerb68cad62012-08-23 08:34:18 +00003913void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003914{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003915 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3916 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3917 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3918 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3919}
3920
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003921void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3922 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003923 int major, int minor )
3924{
3925 if( major != SSL_MAJOR_VERSION_3 )
3926 return;
3927
3928 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3929 return;
3930
3931 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003932}
3933
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003934#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003935/* Add a new (empty) key_cert entry an return a pointer to it */
3936static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3937{
3938 ssl_key_cert *key_cert, *last;
3939
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003940 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3941 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003942 return( NULL );
3943
3944 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3945
3946 /* Append the new key_cert to the (possibly empty) current list */
3947 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003948 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003949 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003950 if( ssl->handshake != NULL )
3951 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003952 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003953 else
3954 {
3955 last = ssl->key_cert;
3956 while( last->next != NULL )
3957 last = last->next;
3958 last->next = key_cert;
3959 }
3960
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003961 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003962}
3963
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003964void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003965 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003966{
3967 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003968 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 ssl->peer_cn = peer_cn;
3970}
3971
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003972int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003973 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003974{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003975 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3976
3977 if( key_cert == NULL )
3978 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3979
3980 key_cert->cert = own_cert;
3981 key_cert->key = pk_key;
3982
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01003983 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003984}
3985
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003986#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003987int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003988 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003989{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003990 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003991 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003992
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003993 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003994 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3995
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003996 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3997 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003998 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003999
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004000 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004001
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004002 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004003 if( ret != 0 )
4004 return( ret );
4005
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004006 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004007 return( ret );
4008
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004009 key_cert->cert = own_cert;
4010 key_cert->key_own_alloc = 1;
4011
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004012 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004013}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02004014#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004016int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02004017 void *rsa_key,
4018 rsa_decrypt_func rsa_decrypt,
4019 rsa_sign_func rsa_sign,
4020 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00004021{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004022 int ret;
4023 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004024
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004025 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004026 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4027
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004028 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
4029 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004030 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004031
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004032 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004033
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004034 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
4035 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
4036 return( ret );
4037
4038 key_cert->cert = own_cert;
4039 key_cert->key_own_alloc = 1;
4040
Manuel Pégourié-Gonnard27e3edb2014-11-06 17:32:48 +01004041 return( pk_check_pair( &key_cert->cert->pk, key_cert->key ) );
Paul Bakker43b7e352011-01-18 15:27:19 +00004042}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004043#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00004044
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004045#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004046int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
4047 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004048{
Paul Bakker6db455e2013-09-18 17:29:31 +02004049 if( psk == NULL || psk_identity == NULL )
4050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4051
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02004052 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01004053 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4054
Paul Bakker6db455e2013-09-18 17:29:31 +02004055 if( ssl->psk != NULL )
4056 {
4057 polarssl_free( ssl->psk );
4058 polarssl_free( ssl->psk_identity );
4059 }
4060
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004061 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004062 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02004063
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02004064 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004065 ssl->psk_identity = (unsigned char *)
4066 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004067
4068 if( ssl->psk == NULL || ssl->psk_identity == NULL )
4069 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4070
4071 memcpy( ssl->psk, psk, ssl->psk_len );
4072 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02004073
4074 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02004075}
4076
4077void ssl_set_psk_cb( ssl_context *ssl,
4078 int (*f_psk)(void *, ssl_context *, const unsigned char *,
4079 size_t),
4080 void *p_psk )
4081{
4082 ssl->f_psk = f_psk;
4083 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02004084}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004085#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00004086
Paul Bakker48916f92012-09-16 19:57:18 +00004087#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004088int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00004089{
4090 int ret;
4091
Paul Bakker48916f92012-09-16 19:57:18 +00004092 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004093 {
4094 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4095 return( ret );
4096 }
4097
Paul Bakker48916f92012-09-16 19:57:18 +00004098 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004099 {
4100 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
4101 return( ret );
4102 }
4103
4104 return( 0 );
4105}
4106
Paul Bakker1b57b062011-01-06 15:48:19 +00004107int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
4108{
4109 int ret;
4110
Paul Bakker66d5d072014-06-17 16:39:18 +02004111 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004112 {
4113 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4114 return( ret );
4115 }
4116
Paul Bakker66d5d072014-06-17 16:39:18 +02004117 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00004118 {
4119 SSL_DEBUG_RET( 1, "mpi_copy", ret );
4120 return( ret );
4121 }
4122
4123 return( 0 );
4124}
Paul Bakker48916f92012-09-16 19:57:18 +00004125#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00004126
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01004127#if defined(POLARSSL_SSL_SET_CURVES)
4128/*
4129 * Set the allowed elliptic curves
4130 */
4131void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
4132{
4133 ssl->curve_list = curve_list;
4134}
4135#endif
4136
Paul Bakker0be444a2013-08-27 21:55:01 +02004137#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00004138int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00004139{
4140 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00004141 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00004142
4143 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02004144
4145 if( ssl->hostname_len + 1 == 0 )
4146 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4147
Paul Bakker6e339b52013-07-03 13:37:05 +02004148 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004149
Paul Bakkerb15b8512012-01-13 13:44:06 +00004150 if( ssl->hostname == NULL )
4151 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
4152
Paul Bakker3c2122f2013-06-24 19:03:14 +02004153 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00004154 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02004155
Paul Bakker40ea7de2009-05-03 10:18:48 +00004156 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00004157
4158 return( 0 );
4159}
4160
Paul Bakker5701cdc2012-09-27 21:49:42 +00004161void ssl_set_sni( ssl_context *ssl,
4162 int (*f_sni)(void *, ssl_context *,
4163 const unsigned char *, size_t),
4164 void *p_sni )
4165{
4166 ssl->f_sni = f_sni;
4167 ssl->p_sni = p_sni;
4168}
Paul Bakker0be444a2013-08-27 21:55:01 +02004169#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00004170
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004171#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004172int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004173{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004174 size_t cur_len, tot_len;
4175 const char **p;
4176
4177 /*
4178 * "Empty strings MUST NOT be included and byte strings MUST NOT be
4179 * truncated". Check lengths now rather than later.
4180 */
4181 tot_len = 0;
4182 for( p = protos; *p != NULL; p++ )
4183 {
4184 cur_len = strlen( *p );
4185 tot_len += cur_len;
4186
4187 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
4188 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4189 }
4190
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004191 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02004192
4193 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004194}
4195
4196const char *ssl_get_alpn_protocol( const ssl_context *ssl )
4197{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004198 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02004199}
4200#endif /* POLARSSL_SSL_ALPN */
4201
Paul Bakker490ecc82011-10-06 13:04:09 +00004202void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4203{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004204 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4205 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4206 {
4207 ssl->max_major_ver = major;
4208 ssl->max_minor_ver = minor;
4209 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004210}
4211
Paul Bakker1d29fb52012-09-28 13:28:45 +00004212void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4213{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004214 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4215 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4216 {
4217 ssl->min_major_ver = major;
4218 ssl->min_minor_ver = minor;
4219 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004220}
4221
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004222#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4223void ssl_set_fallback( ssl_context *ssl, char fallback )
4224{
4225 ssl->fallback = fallback;
4226}
4227#endif
4228
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01004229#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
4230void ssl_set_encrypt_then_mac( ssl_context *ssl, char etm )
4231{
4232 ssl->encrypt_then_mac = etm;
4233}
4234#endif
4235
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004236#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4237void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4238{
4239 ssl->extended_ms = ems;
4240}
4241#endif
4242
Paul Bakker05decb22013-08-15 13:33:48 +02004243#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004244int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4245{
Paul Bakker77e257e2013-12-16 15:29:52 +01004246 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004247 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004248 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004249 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004250 }
4251
4252 ssl->mfl_code = mfl_code;
4253
4254 return( 0 );
4255}
Paul Bakker05decb22013-08-15 13:33:48 +02004256#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004257
Paul Bakker1f2bc622013-08-15 13:45:55 +02004258#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004259int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004260{
4261 if( ssl->endpoint != SSL_IS_CLIENT )
4262 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4263
Paul Bakker8c1ede62013-07-19 14:14:37 +02004264 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004265
4266 return( 0 );
4267}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004268#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004269
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004270#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4271void ssl_set_cbc_record_splitting( ssl_context *ssl, char split )
4272{
4273 ssl->split_done = split;
4274}
4275#endif
4276
Paul Bakker48916f92012-09-16 19:57:18 +00004277void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4278{
4279 ssl->allow_legacy_renegotiation = allow_legacy;
4280}
4281
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004282#if defined(POLARSSL_SSL_RENEGOTIATION)
4283void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4284{
4285 ssl->disable_renegotiation = renegotiation;
4286}
4287
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004288void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4289{
4290 ssl->renego_max_records = max_records;
4291}
4292
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004293void ssl_set_renegotiation_period( ssl_context *ssl,
4294 const unsigned char period[8] )
4295{
4296 memcpy( ssl->renego_period, period, 8 );
4297}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004298#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004299
Paul Bakkera503a632013-08-14 13:48:06 +02004300#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004301int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4302{
4303 ssl->session_tickets = use_tickets;
4304
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004305#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004306 if( ssl->endpoint == SSL_IS_CLIENT )
4307 return( 0 );
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004308#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004309
Manuel Pégourié-Gonnard2457fa02014-11-21 09:23:11 +01004310 if( use_tickets == SSL_SESSION_TICKETS_DISABLED )
4311 return( 0 );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004312
4313 if( ssl->f_rng == NULL )
4314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4315
4316 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004317}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004318
4319void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4320{
4321 ssl->ticket_lifetime = lifetime;
4322}
Paul Bakkera503a632013-08-14 13:48:06 +02004323#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004324
Paul Bakker5121ce52009-01-03 21:22:43 +00004325/*
4326 * SSL get accessors
4327 */
4328size_t ssl_get_bytes_avail( const ssl_context *ssl )
4329{
4330 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4331}
4332
Paul Bakkerff60ee62010-03-16 21:09:09 +00004333int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004334{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004335 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004336}
4337
Paul Bakkere3166ce2011-01-27 17:40:50 +00004338const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004339{
Paul Bakker926c8e42013-03-06 10:23:34 +01004340 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004341 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004342
Paul Bakkere3166ce2011-01-27 17:40:50 +00004343 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004344}
4345
Paul Bakker43ca69c2011-01-15 17:35:19 +00004346const char *ssl_get_version( const ssl_context *ssl )
4347{
4348 switch( ssl->minor_ver )
4349 {
4350 case SSL_MINOR_VERSION_0:
4351 return( "SSLv3.0" );
4352
4353 case SSL_MINOR_VERSION_1:
4354 return( "TLSv1.0" );
4355
4356 case SSL_MINOR_VERSION_2:
4357 return( "TLSv1.1" );
4358
Paul Bakker1ef83d62012-04-11 12:09:53 +00004359 case SSL_MINOR_VERSION_3:
4360 return( "TLSv1.2" );
4361
Paul Bakker43ca69c2011-01-15 17:35:19 +00004362 default:
4363 break;
4364 }
4365 return( "unknown" );
4366}
4367
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004368#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004369const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004370{
4371 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004372 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004373
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004374 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004375}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004376#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004377
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004378#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004379int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4380{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004381 if( ssl == NULL ||
4382 dst == NULL ||
4383 ssl->session == NULL ||
4384 ssl->endpoint != SSL_IS_CLIENT )
4385 {
4386 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4387 }
4388
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004389 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004390}
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004391#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004392
Paul Bakker5121ce52009-01-03 21:22:43 +00004393/*
Paul Bakker1961b702013-01-25 14:49:24 +01004394 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004395 */
Paul Bakker1961b702013-01-25 14:49:24 +01004396int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004397{
Paul Bakker40e46942009-01-03 21:51:57 +00004398 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004399
Paul Bakker40e46942009-01-03 21:51:57 +00004400#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004401 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004402 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004403#endif
Paul Bakker40e46942009-01-03 21:51:57 +00004404#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004405 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004406 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004407#endif
4408
Paul Bakker1961b702013-01-25 14:49:24 +01004409 return( ret );
4410}
4411
4412/*
4413 * Perform the SSL handshake
4414 */
4415int ssl_handshake( ssl_context *ssl )
4416{
4417 int ret = 0;
4418
4419 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4420
4421 while( ssl->state != SSL_HANDSHAKE_OVER )
4422 {
4423 ret = ssl_handshake_step( ssl );
4424
4425 if( ret != 0 )
4426 break;
4427 }
4428
Paul Bakker5121ce52009-01-03 21:22:43 +00004429 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4430
4431 return( ret );
4432}
4433
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004434#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004435#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004436/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004437 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004438 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004439static int ssl_write_hello_request( ssl_context *ssl )
4440{
4441 int ret;
4442
4443 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4444
4445 ssl->out_msglen = 4;
4446 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4447 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4448
4449 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4450 {
4451 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4452 return( ret );
4453 }
4454
4455 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4456
4457 return( 0 );
4458}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004459#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004460
4461/*
4462 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004463 * - any side: calling ssl_renegotiate(),
4464 * - client: receiving a HelloRequest during ssl_read(),
4465 * - server: receiving any handshake message on server during ssl_read() after
4466 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004467 * If the handshake doesn't complete due to waiting for I/O, it will continue
4468 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004469 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004470static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004471{
4472 int ret;
4473
4474 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4475
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004476 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4477 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004478
4479 ssl->state = SSL_HELLO_REQUEST;
4480 ssl->renegotiation = SSL_RENEGOTIATION;
4481
Paul Bakker48916f92012-09-16 19:57:18 +00004482 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4483 {
4484 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4485 return( ret );
4486 }
4487
4488 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4489
4490 return( 0 );
4491}
4492
4493/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004494 * Renegotiate current connection on client,
4495 * or request renegotiation on server
4496 */
4497int ssl_renegotiate( ssl_context *ssl )
4498{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004499 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004500
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004501#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004502 /* On server, just send the request */
4503 if( ssl->endpoint == SSL_IS_SERVER )
4504 {
4505 if( ssl->state != SSL_HANDSHAKE_OVER )
4506 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4507
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004508 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4509
4510 /* Did we already try/start sending HelloRequest? */
4511 if( ssl->out_left != 0 )
4512 return( ssl_flush_output( ssl ) );
4513
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004514 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004515 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004516#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004517
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004518#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004519 /*
4520 * On client, either start the renegotiation process or,
4521 * if already in progress, continue the handshake
4522 */
4523 if( ssl->renegotiation != SSL_RENEGOTIATION )
4524 {
4525 if( ssl->state != SSL_HANDSHAKE_OVER )
4526 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4527
4528 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4529 {
4530 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4531 return( ret );
4532 }
4533 }
4534 else
4535 {
4536 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4537 {
4538 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4539 return( ret );
4540 }
4541 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004542#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004543
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004544 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004545}
4546
4547/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004548 * Check record counters and renegotiate if they're above the limit.
4549 */
4550static int ssl_check_ctr_renegotiate( ssl_context *ssl )
4551{
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004552 if( ssl->state != SSL_HANDSHAKE_OVER ||
4553 ssl->renegotiation == SSL_RENEGOTIATION_PENDING ||
4554 ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED )
4555 {
4556 return( 0 );
4557 }
4558
4559 // TODO: adapt for DTLS
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004560 if( memcmp( ssl->in_ctr, ssl->renego_period, 8 ) <= 0 &&
4561 memcmp( ssl->out_ctr, ssl->renego_period, 8 ) <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004562 {
4563 return( 0 );
4564 }
4565
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01004566 SSL_DEBUG_MSG( 0, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004567 return( ssl_renegotiate( ssl ) );
4568}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004569#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00004570
4571/*
4572 * Receive application data decrypted from the SSL layer
4573 */
Paul Bakker23986e52011-04-24 08:57:21 +00004574int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004575{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004576 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004577 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004578
4579 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4580
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004581#if defined(POLARSSL_SSL_RENEGOTIATION)
4582 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4583 {
4584 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4585 return( ret );
4586 }
4587#endif
4588
Paul Bakker5121ce52009-01-03 21:22:43 +00004589 if( ssl->state != SSL_HANDSHAKE_OVER )
4590 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004591 ret = ssl_handshake( ssl );
4592 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4593 {
4594 record_read = 1;
4595 }
4596 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004597 {
4598 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4599 return( ret );
4600 }
4601 }
4602
4603 if( ssl->in_offt == NULL )
4604 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004605 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004606 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004607 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4608 {
4609 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4610 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004611
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004612 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4613 return( ret );
4614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004615 }
4616
4617 if( ssl->in_msglen == 0 &&
4618 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4619 {
4620 /*
4621 * OpenSSL sends empty messages to randomize the IV
4622 */
4623 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4624 {
Paul Bakker831a7552011-05-18 13:32:51 +00004625 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4626 return( 0 );
4627
Paul Bakker5121ce52009-01-03 21:22:43 +00004628 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4629 return( ret );
4630 }
4631 }
4632
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004633#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00004634 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4635 {
4636 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4637
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004638#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004639 if( ssl->endpoint == SSL_IS_CLIENT &&
4640 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4641 ssl->in_hslen != 4 ) )
4642 {
4643 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4644 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4645 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01004646#endif
Paul Bakker48916f92012-09-16 19:57:18 +00004647
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004648 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4649 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004650 ssl->allow_legacy_renegotiation ==
4651 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004652 {
4653 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4654
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004655#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004656 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004657 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004658 /*
4659 * SSLv3 does not have a "no_renegotiation" alert
4660 */
4661 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4662 return( ret );
4663 }
4664 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004665#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004666#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4667 defined(POLARSSL_SSL_PROTO_TLS1_2)
4668 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004669 {
4670 if( ( ret = ssl_send_alert_message( ssl,
4671 SSL_ALERT_LEVEL_WARNING,
4672 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4673 {
4674 return( ret );
4675 }
Paul Bakker48916f92012-09-16 19:57:18 +00004676 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004677 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004678#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4679 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004680 {
4681 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004682 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004683 }
Paul Bakker48916f92012-09-16 19:57:18 +00004684 }
4685 else
4686 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004687 ret = ssl_start_renegotiation( ssl );
4688 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4689 {
4690 record_read = 1;
4691 }
4692 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004693 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004694 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004695 return( ret );
4696 }
Paul Bakker48916f92012-09-16 19:57:18 +00004697 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004698
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004699 /* If a non-handshake record was read during renego, fallthrough,
4700 * else tell the user they should call ssl_read() again */
4701 if( ! record_read )
4702 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004703 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004704 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4705 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004706 ssl->renego_records_seen++;
4707
4708 if( ssl->renego_max_records >= 0 &&
4709 ssl->renego_records_seen > ssl->renego_max_records )
4710 {
4711 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4712 "but not honored by client" ) );
4713 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4714 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004715 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01004716#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004717
4718 /* Fatal and closure alerts handled by ssl_read_record() */
4719 if( ssl->in_msgtype == SSL_MSG_ALERT )
4720 {
4721 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4722 return( POLARSSL_ERR_NET_WANT_READ );
4723 }
4724
4725 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004726 {
4727 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004728 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004729 }
4730
4731 ssl->in_offt = ssl->in_msg;
4732 }
4733
4734 n = ( len < ssl->in_msglen )
4735 ? len : ssl->in_msglen;
4736
4737 memcpy( buf, ssl->in_offt, n );
4738 ssl->in_msglen -= n;
4739
4740 if( ssl->in_msglen == 0 )
4741 /* all bytes consumed */
4742 ssl->in_offt = NULL;
4743 else
4744 /* more data available */
4745 ssl->in_offt += n;
4746
4747 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4748
Paul Bakker23986e52011-04-24 08:57:21 +00004749 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004750}
4751
4752/*
4753 * Send application data to be encrypted by the SSL layer
4754 */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004755#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4756static int ssl_write_real( ssl_context *ssl, const unsigned char *buf, size_t len )
4757#else
Paul Bakker23986e52011-04-24 08:57:21 +00004758int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004759#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004760{
Paul Bakker23986e52011-04-24 08:57:21 +00004761 int ret;
4762 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004763 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004764
4765 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4766
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01004767#if defined(POLARSSL_SSL_RENEGOTIATION)
4768 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
4769 {
4770 SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
4771 return( ret );
4772 }
4773#endif
4774
Paul Bakker5121ce52009-01-03 21:22:43 +00004775 if( ssl->state != SSL_HANDSHAKE_OVER )
4776 {
4777 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4778 {
4779 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4780 return( ret );
4781 }
4782 }
4783
Paul Bakker05decb22013-08-15 13:33:48 +02004784#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004785 /*
4786 * Assume mfl_code is correct since it was checked when set
4787 */
4788 max_len = mfl_code_to_length[ssl->mfl_code];
4789
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004790 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004791 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004792 */
4793 if( ssl->session_out != NULL &&
4794 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4795 {
4796 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4797 }
Paul Bakker05decb22013-08-15 13:33:48 +02004798#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004799
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004800 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004801
Paul Bakker5121ce52009-01-03 21:22:43 +00004802 if( ssl->out_left != 0 )
4803 {
4804 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4805 {
4806 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4807 return( ret );
4808 }
4809 }
Paul Bakker887bd502011-06-08 13:10:54 +00004810 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004811 {
Paul Bakker887bd502011-06-08 13:10:54 +00004812 ssl->out_msglen = n;
4813 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4814 memcpy( ssl->out_msg, buf, n );
4815
4816 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4817 {
4818 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4819 return( ret );
4820 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004821 }
4822
4823 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4824
Paul Bakker23986e52011-04-24 08:57:21 +00004825 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004826}
4827
4828/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004829 * Write application data, doing 1/n-1 splitting if necessary.
4830 *
4831 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004832 * then the caller will call us again with the same arguments, so
4833 * remember wether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004834 */
4835#if defined(POLARSSL_SSL_CBC_RECORD_SPLITTING)
4836int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
4837{
4838 int ret;
4839
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01004840 if( ssl->split_done == SSL_CBC_RECORD_SPLITTING_DISABLED ||
4841 len <= 1 ||
4842 ssl->minor_ver > SSL_MINOR_VERSION_1 ||
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004843 cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
4844 != POLARSSL_MODE_CBC )
4845 {
4846 return( ssl_write_real( ssl, buf, len ) );
4847 }
4848
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004849 if( ssl->split_done == 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004850 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004851 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004852 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004853 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004854 }
4855
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01004856 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
4857 return( ret );
4858 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01004859
4860 return( ret + 1 );
4861}
4862#endif /* POLARSSL_SSL_CBC_RECORD_SPLITTING */
4863
4864/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004865 * Notify the peer that the connection is being closed
4866 */
4867int ssl_close_notify( ssl_context *ssl )
4868{
4869 int ret;
4870
4871 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4872
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004873 if( ssl->out_left != 0 )
4874 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004875
4876 if( ssl->state == SSL_HANDSHAKE_OVER )
4877 {
Paul Bakker48916f92012-09-16 19:57:18 +00004878 if( ( ret = ssl_send_alert_message( ssl,
4879 SSL_ALERT_LEVEL_WARNING,
4880 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004881 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004882 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004883 return( ret );
4884 }
4885 }
4886
4887 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4888
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004889 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004890}
4891
Paul Bakker48916f92012-09-16 19:57:18 +00004892void ssl_transform_free( ssl_transform *transform )
4893{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004894 if( transform == NULL )
4895 return;
4896
Paul Bakker48916f92012-09-16 19:57:18 +00004897#if defined(POLARSSL_ZLIB_SUPPORT)
4898 deflateEnd( &transform->ctx_deflate );
4899 inflateEnd( &transform->ctx_inflate );
4900#endif
4901
Paul Bakker84bbeb52014-07-01 14:53:22 +02004902 cipher_free( &transform->cipher_ctx_enc );
4903 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004904
Paul Bakker84bbeb52014-07-01 14:53:22 +02004905 md_free( &transform->md_ctx_enc );
4906 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004907
Paul Bakker34617722014-06-13 17:20:13 +02004908 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004909}
4910
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004911#if defined(POLARSSL_X509_CRT_PARSE_C)
4912static void ssl_key_cert_free( ssl_key_cert *key_cert )
4913{
4914 ssl_key_cert *cur = key_cert, *next;
4915
4916 while( cur != NULL )
4917 {
4918 next = cur->next;
4919
4920 if( cur->key_own_alloc )
4921 {
4922 pk_free( cur->key );
4923 polarssl_free( cur->key );
4924 }
4925 polarssl_free( cur );
4926
4927 cur = next;
4928 }
4929}
4930#endif /* POLARSSL_X509_CRT_PARSE_C */
4931
Paul Bakker48916f92012-09-16 19:57:18 +00004932void ssl_handshake_free( ssl_handshake_params *handshake )
4933{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004934 if( handshake == NULL )
4935 return;
4936
Paul Bakker48916f92012-09-16 19:57:18 +00004937#if defined(POLARSSL_DHM_C)
4938 dhm_free( &handshake->dhm_ctx );
4939#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004940#if defined(POLARSSL_ECDH_C)
4941 ecdh_free( &handshake->ecdh_ctx );
4942#endif
4943
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004944#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004945 /* explicit void pointer cast for buggy MS compiler */
4946 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004947#endif
4948
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004949#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4950 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4951 /*
4952 * Free only the linked list wrapper, not the keys themselves
4953 * since the belong to the SNI callback
4954 */
4955 if( handshake->sni_key_cert != NULL )
4956 {
4957 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4958
4959 while( cur != NULL )
4960 {
4961 next = cur->next;
4962 polarssl_free( cur );
4963 cur = next;
4964 }
4965 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004966#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004967
Paul Bakker34617722014-06-13 17:20:13 +02004968 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004969}
4970
4971void ssl_session_free( ssl_session *session )
4972{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004973 if( session == NULL )
4974 return;
4975
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004976#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004977 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004978 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004979 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004980 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004981 }
Paul Bakkered27a042013-04-18 22:46:23 +02004982#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004983
Paul Bakkera503a632013-08-14 13:48:06 +02004984#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004985 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004986#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004987
Paul Bakker34617722014-06-13 17:20:13 +02004988 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004989}
4990
Paul Bakker5121ce52009-01-03 21:22:43 +00004991/*
4992 * Free an SSL context
4993 */
4994void ssl_free( ssl_context *ssl )
4995{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004996 if( ssl == NULL )
4997 return;
4998
Paul Bakker5121ce52009-01-03 21:22:43 +00004999 SSL_DEBUG_MSG( 2, ( "=> free" ) );
5000
Paul Bakker5121ce52009-01-03 21:22:43 +00005001 if( ssl->out_ctr != NULL )
5002 {
Paul Bakker34617722014-06-13 17:20:13 +02005003 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005004 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005005 }
5006
5007 if( ssl->in_ctr != NULL )
5008 {
Paul Bakker34617722014-06-13 17:20:13 +02005009 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02005010 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00005011 }
5012
Paul Bakker16770332013-10-11 09:59:44 +02005013#if defined(POLARSSL_ZLIB_SUPPORT)
5014 if( ssl->compress_buf != NULL )
5015 {
Paul Bakker34617722014-06-13 17:20:13 +02005016 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02005017 polarssl_free( ssl->compress_buf );
5018 }
5019#endif
5020
Paul Bakker40e46942009-01-03 21:51:57 +00005021#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00005022 mpi_free( &ssl->dhm_P );
5023 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00005024#endif
5025
Paul Bakker48916f92012-09-16 19:57:18 +00005026 if( ssl->transform )
5027 {
5028 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02005029 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00005030 }
5031
5032 if( ssl->handshake )
5033 {
5034 ssl_handshake_free( ssl->handshake );
5035 ssl_transform_free( ssl->transform_negotiate );
5036 ssl_session_free( ssl->session_negotiate );
5037
Paul Bakker6e339b52013-07-03 13:37:05 +02005038 polarssl_free( ssl->handshake );
5039 polarssl_free( ssl->transform_negotiate );
5040 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00005041 }
5042
Paul Bakkerc0463502013-02-14 11:19:38 +01005043 if( ssl->session )
5044 {
5045 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02005046 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01005047 }
5048
Paul Bakkera503a632013-08-14 13:48:06 +02005049#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02005050 if( ssl->ticket_keys )
5051 {
5052 ssl_ticket_keys_free( ssl->ticket_keys );
5053 polarssl_free( ssl->ticket_keys );
5054 }
Paul Bakkera503a632013-08-14 13:48:06 +02005055#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02005056
Paul Bakker0be444a2013-08-27 21:55:01 +02005057#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02005058 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005059 {
Paul Bakker34617722014-06-13 17:20:13 +02005060 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02005061 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00005062 ssl->hostname_len = 0;
5063 }
Paul Bakker0be444a2013-08-27 21:55:01 +02005064#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00005065
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02005066#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02005067 if( ssl->psk != NULL )
5068 {
Paul Bakker34617722014-06-13 17:20:13 +02005069 polarssl_zeroize( ssl->psk, ssl->psk_len );
5070 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02005071 polarssl_free( ssl->psk );
5072 polarssl_free( ssl->psk_identity );
5073 ssl->psk_len = 0;
5074 ssl->psk_identity_len = 0;
5075 }
5076#endif
5077
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02005078#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02005079 ssl_key_cert_free( ssl->key_cert );
5080#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02005081
Paul Bakker05ef8352012-05-08 09:17:57 +00005082#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
5083 if( ssl_hw_record_finish != NULL )
5084 {
5085 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
5086 ssl_hw_record_finish( ssl );
5087 }
5088#endif
5089
Paul Bakker5121ce52009-01-03 21:22:43 +00005090 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00005091
Paul Bakker86f04f42013-02-14 11:20:09 +01005092 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02005093 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005094}
5095
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005096#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005097/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005098 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005099 */
5100unsigned char ssl_sig_from_pk( pk_context *pk )
5101{
5102#if defined(POLARSSL_RSA_C)
5103 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
5104 return( SSL_SIG_RSA );
5105#endif
5106#if defined(POLARSSL_ECDSA_C)
5107 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
5108 return( SSL_SIG_ECDSA );
5109#endif
5110 return( SSL_SIG_ANON );
5111}
5112
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005113pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
5114{
5115 switch( sig )
5116 {
5117#if defined(POLARSSL_RSA_C)
5118 case SSL_SIG_RSA:
5119 return( POLARSSL_PK_RSA );
5120#endif
5121#if defined(POLARSSL_ECDSA_C)
5122 case SSL_SIG_ECDSA:
5123 return( POLARSSL_PK_ECDSA );
5124#endif
5125 default:
5126 return( POLARSSL_PK_NONE );
5127 }
5128}
Paul Bakker9af723c2014-05-01 13:03:14 +02005129#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005130
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005131/*
5132 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
5133 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005134md_type_t ssl_md_alg_from_hash( unsigned char hash )
5135{
5136 switch( hash )
5137 {
5138#if defined(POLARSSL_MD5_C)
5139 case SSL_HASH_MD5:
5140 return( POLARSSL_MD_MD5 );
5141#endif
5142#if defined(POLARSSL_SHA1_C)
5143 case SSL_HASH_SHA1:
5144 return( POLARSSL_MD_SHA1 );
5145#endif
5146#if defined(POLARSSL_SHA256_C)
5147 case SSL_HASH_SHA224:
5148 return( POLARSSL_MD_SHA224 );
5149 case SSL_HASH_SHA256:
5150 return( POLARSSL_MD_SHA256 );
5151#endif
5152#if defined(POLARSSL_SHA512_C)
5153 case SSL_HASH_SHA384:
5154 return( POLARSSL_MD_SHA384 );
5155 case SSL_HASH_SHA512:
5156 return( POLARSSL_MD_SHA512 );
5157#endif
5158 default:
5159 return( POLARSSL_MD_NONE );
5160 }
5161}
5162
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005163#if defined(POLARSSL_SSL_SET_CURVES)
5164/*
5165 * Check is a curve proposed by the peer is in our list.
5166 * Return 1 if we're willing to use it, 0 otherwise.
5167 */
5168int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
5169{
5170 const ecp_group_id *gid;
5171
5172 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
5173 if( *gid == grp_id )
5174 return( 1 );
5175
5176 return( 0 );
5177}
Paul Bakker9af723c2014-05-01 13:03:14 +02005178#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005179
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005180#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005181int ssl_check_cert_usage( const x509_crt *cert,
5182 const ssl_ciphersuite_t *ciphersuite,
5183 int cert_endpoint )
5184{
5185#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5186 int usage = 0;
5187#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005188#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5189 const char *ext_oid;
5190 size_t ext_len;
5191#endif
5192
5193#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
5194 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5195 ((void) cert);
5196 ((void) cert_endpoint);
5197#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005198
5199#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
5200 if( cert_endpoint == SSL_IS_SERVER )
5201 {
5202 /* Server part of the key exchange */
5203 switch( ciphersuite->key_exchange )
5204 {
5205 case POLARSSL_KEY_EXCHANGE_RSA:
5206 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
5207 usage = KU_KEY_ENCIPHERMENT;
5208 break;
5209
5210 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
5211 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
5212 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
5213 usage = KU_DIGITAL_SIGNATURE;
5214 break;
5215
5216 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
5217 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
5218 usage = KU_KEY_AGREEMENT;
5219 break;
5220
5221 /* Don't use default: we want warnings when adding new values */
5222 case POLARSSL_KEY_EXCHANGE_NONE:
5223 case POLARSSL_KEY_EXCHANGE_PSK:
5224 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
5225 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
5226 usage = 0;
5227 }
5228 }
5229 else
5230 {
5231 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
5232 usage = KU_DIGITAL_SIGNATURE;
5233 }
5234
5235 if( x509_crt_check_key_usage( cert, usage ) != 0 )
5236 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005237#else
5238 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005239#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
5240
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005241#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
5242 if( cert_endpoint == SSL_IS_SERVER )
5243 {
5244 ext_oid = OID_SERVER_AUTH;
5245 ext_len = OID_SIZE( OID_SERVER_AUTH );
5246 }
5247 else
5248 {
5249 ext_oid = OID_CLIENT_AUTH;
5250 ext_len = OID_SIZE( OID_CLIENT_AUTH );
5251 }
5252
5253 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
5254 return( -1 );
5255#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
5256
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005257 return( 0 );
5258}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02005259#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005260
5261#endif /* POLARSSL_SSL_TLS_C */