blob: c8b7fa2a928233079df22ec27bcc84023055111d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200473#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
474 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200475 {
476 unsigned char session_hash[48];
477 size_t hash_len;
478
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200479 SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200480
481 ssl->handshake->calc_verify( ssl, session_hash );
482
483#if defined(POLARSSL_SSL_PROTO_TLS1_2)
484 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
485 {
486#if defined(POLARSSL_SHA512_C)
487 if( ssl->transform_negotiate->ciphersuite_info->mac ==
488 POLARSSL_MD_SHA384 )
489 {
490 hash_len = 48;
491 }
492 else
493#endif
494 hash_len = 32;
495 }
496 else
497#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
498 hash_len = 36;
499
500 SSL_DEBUG_BUF( 3, "session hash", session_hash, hash_len );
501
502 handshake->tls_prf( handshake->premaster, handshake->pmslen,
503 "extended master secret",
504 session_hash, hash_len, session->master, 48 );
505
506 }
507 else
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200508#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000509 handshake->tls_prf( handshake->premaster, handshake->pmslen,
510 "master secret",
511 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +0200513
Paul Bakker34617722014-06-13 17:20:13 +0200514 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 }
516 else
517 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
518
519 /*
520 * Swap the client and server random values.
521 */
Paul Bakker48916f92012-09-16 19:57:18 +0000522 memcpy( tmp, handshake->randbytes, 64 );
523 memcpy( handshake->randbytes, tmp + 32, 32 );
524 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200525 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000526
527 /*
528 * SSLv3:
529 * key block =
530 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
531 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
532 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
533 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
534 * ...
535 *
536 * TLSv1:
537 * key block = PRF( master, "key expansion", randbytes )
538 */
Paul Bakker48916f92012-09-16 19:57:18 +0000539 handshake->tls_prf( session->master, 48, "key expansion",
540 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Paul Bakker48916f92012-09-16 19:57:18 +0000542 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
543 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
544 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
545 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
547
Paul Bakker34617722014-06-13 17:20:13 +0200548 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 /*
551 * Determine the appropriate key, IV and MAC length.
552 */
Paul Bakker68884e32013-01-07 18:20:04 +0100553
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200554 transform->keylen = cipher_info->key_length / 8;
555
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200556 if( cipher_info->mode == POLARSSL_MODE_GCM ||
557 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200559 transform->maclen = 0;
560
Paul Bakker68884e32013-01-07 18:20:04 +0100561 transform->ivlen = 12;
562 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200563
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200564 /* Minimum length is expicit IV + tag */
565 transform->minlen = transform->ivlen - transform->fixed_ivlen
566 + ( transform->ciphersuite_info->flags &
567 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100568 }
569 else
570 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200571 int ret;
572
573 /* Initialize HMAC contexts */
574 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
575 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100576 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200577 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
578 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200581 /* Get MAC length */
582 transform->maclen = md_get_size( md_info );
583
584#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
585 /*
586 * If HMAC is to be truncated, we shall keep the leftmost bytes,
587 * (rfc 6066 page 13 or rfc 2104 section 4),
588 * so we only need to adjust the length here.
589 */
590 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
591 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
592#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
593
594 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100595 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200597 /* Minimum length */
598 if( cipher_info->mode == POLARSSL_MODE_STREAM )
599 transform->minlen = transform->maclen;
600 else
Paul Bakker68884e32013-01-07 18:20:04 +0100601 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200602 /*
603 * GenericBlockCipher:
604 * first multiple of blocklen greater than maclen
605 * + IV except for SSL3 and TLS 1.0
606 */
607 transform->minlen = transform->maclen
608 + cipher_info->block_size
609 - transform->maclen % cipher_info->block_size;
610
611#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
612 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
613 ssl->minor_ver == SSL_MINOR_VERSION_1 )
614 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100615 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200616#endif
617#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
618 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
619 ssl->minor_ver == SSL_MINOR_VERSION_3 )
620 {
621 transform->minlen += transform->ivlen;
622 }
623 else
624#endif
625 {
626 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
627 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
628 }
Paul Bakker68884e32013-01-07 18:20:04 +0100629 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 }
631
632 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000633 transform->keylen, transform->minlen, transform->ivlen,
634 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635
636 /*
637 * Finally setup the cipher contexts, IVs and MAC secrets.
638 */
639 if( ssl->endpoint == SSL_IS_CLIENT )
640 {
Paul Bakker48916f92012-09-16 19:57:18 +0000641 key1 = keyblk + transform->maclen * 2;
642 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643
Paul Bakker68884e32013-01-07 18:20:04 +0100644 mac_enc = keyblk;
645 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000646
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000647 /*
648 * This is not used in TLS v1.1.
649 */
Paul Bakker48916f92012-09-16 19:57:18 +0000650 iv_copy_len = ( transform->fixed_ivlen ) ?
651 transform->fixed_ivlen : transform->ivlen;
652 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
653 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000654 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 }
656 else
657 {
Paul Bakker48916f92012-09-16 19:57:18 +0000658 key1 = keyblk + transform->maclen * 2 + transform->keylen;
659 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Paul Bakker68884e32013-01-07 18:20:04 +0100661 mac_enc = keyblk + transform->maclen;
662 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000664 /*
665 * This is not used in TLS v1.1.
666 */
Paul Bakker48916f92012-09-16 19:57:18 +0000667 iv_copy_len = ( transform->fixed_ivlen ) ?
668 transform->fixed_ivlen : transform->ivlen;
669 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
670 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000671 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
673
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200674#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100675 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
676 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100677 if( transform->maclen > sizeof transform->mac_enc )
678 {
679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100681 }
682
Paul Bakker68884e32013-01-07 18:20:04 +0100683 memcpy( transform->mac_enc, mac_enc, transform->maclen );
684 memcpy( transform->mac_dec, mac_dec, transform->maclen );
685 }
686 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200687#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200688#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
689 defined(POLARSSL_SSL_PROTO_TLS1_2)
690 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100691 {
692 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
693 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
694 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200695 else
696#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200697 {
698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200700 }
Paul Bakker68884e32013-01-07 18:20:04 +0100701
Paul Bakker05ef8352012-05-08 09:17:57 +0000702#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200703 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000704 {
705 int ret = 0;
706
707 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
708
Paul Bakker07eb38b2012-12-19 14:42:06 +0100709 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
710 transform->iv_enc, transform->iv_dec,
711 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100712 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100713 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000714 {
715 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200716 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000717 }
718 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200719#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000720
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200721 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
722 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200724 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
725 return( ret );
726 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200727
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200728 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
729 cipher_info ) ) != 0 )
730 {
731 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
732 return( ret );
733 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200734
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200735 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
736 cipher_info->key_length,
737 POLARSSL_ENCRYPT ) ) != 0 )
738 {
739 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
740 return( ret );
741 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200742
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200743 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
744 cipher_info->key_length,
745 POLARSSL_DECRYPT ) ) != 0 )
746 {
747 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
748 return( ret );
749 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200750
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200751#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200752 if( cipher_info->mode == POLARSSL_MODE_CBC )
753 {
754 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
755 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200756 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200757 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
758 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200759 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200760
761 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
762 POLARSSL_PADDING_NONE ) ) != 0 )
763 {
764 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
765 return( ret );
766 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200768#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
Paul Bakker34617722014-06-13 17:20:13 +0200770 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000771
Paul Bakker2770fbd2012-07-03 13:30:23 +0000772#if defined(POLARSSL_ZLIB_SUPPORT)
773 // Initialize compression
774 //
Paul Bakker48916f92012-09-16 19:57:18 +0000775 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000776 {
Paul Bakker16770332013-10-11 09:59:44 +0200777 if( ssl->compress_buf == NULL )
778 {
779 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
780 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
781 if( ssl->compress_buf == NULL )
782 {
783 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
784 SSL_BUFFER_LEN ) );
785 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
786 }
787 }
788
Paul Bakker2770fbd2012-07-03 13:30:23 +0000789 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
790
Paul Bakker48916f92012-09-16 19:57:18 +0000791 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
792 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000793
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200794 if( deflateInit( &transform->ctx_deflate,
795 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000796 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000797 {
798 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
799 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
800 }
801 }
802#endif /* POLARSSL_ZLIB_SUPPORT */
803
Paul Bakker5121ce52009-01-03 21:22:43 +0000804 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
805
806 return( 0 );
807}
808
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200809#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000810void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000811{
812 md5_context md5;
813 sha1_context sha1;
814 unsigned char pad_1[48];
815 unsigned char pad_2[48];
816
Paul Bakker380da532012-04-18 16:10:25 +0000817 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000818
Paul Bakker48916f92012-09-16 19:57:18 +0000819 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
820 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000821
Paul Bakker380da532012-04-18 16:10:25 +0000822 memset( pad_1, 0x36, 48 );
823 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000824
Paul Bakker48916f92012-09-16 19:57:18 +0000825 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000826 md5_update( &md5, pad_1, 48 );
827 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000828
Paul Bakker380da532012-04-18 16:10:25 +0000829 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000830 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000831 md5_update( &md5, pad_2, 48 );
832 md5_update( &md5, hash, 16 );
833 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
Paul Bakker48916f92012-09-16 19:57:18 +0000835 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000836 sha1_update( &sha1, pad_1, 40 );
837 sha1_finish( &sha1, hash + 16 );
838
839 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000840 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000841 sha1_update( &sha1, pad_2, 40 );
842 sha1_update( &sha1, hash + 16, 20 );
843 sha1_finish( &sha1, hash + 16 );
844
845 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
846 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
847
Paul Bakker5b4af392014-06-26 12:09:34 +0200848 md5_free( &md5 );
849 sha1_free( &sha1 );
850
Paul Bakker380da532012-04-18 16:10:25 +0000851 return;
852}
Paul Bakker9af723c2014-05-01 13:03:14 +0200853#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000854
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000856void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
857{
858 md5_context md5;
859 sha1_context sha1;
860
861 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
862
Paul Bakker48916f92012-09-16 19:57:18 +0000863 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
864 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000865
Paul Bakker48916f92012-09-16 19:57:18 +0000866 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000867 sha1_finish( &sha1, hash + 16 );
868
869 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
870 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
871
Paul Bakker5b4af392014-06-26 12:09:34 +0200872 md5_free( &md5 );
873 sha1_free( &sha1 );
874
Paul Bakker380da532012-04-18 16:10:25 +0000875 return;
876}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200877#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000878
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200879#if defined(POLARSSL_SSL_PROTO_TLS1_2)
880#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000881void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
882{
Paul Bakker9e36f042013-06-30 14:34:05 +0200883 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000884
885 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
886
Paul Bakker9e36f042013-06-30 14:34:05 +0200887 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
888 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000889
890 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
891 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
892
Paul Bakker5b4af392014-06-26 12:09:34 +0200893 sha256_free( &sha256 );
894
Paul Bakker380da532012-04-18 16:10:25 +0000895 return;
896}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200897#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000898
Paul Bakker9e36f042013-06-30 14:34:05 +0200899#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000900void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
901{
Paul Bakker9e36f042013-06-30 14:34:05 +0200902 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000903
904 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
905
Paul Bakker9e36f042013-06-30 14:34:05 +0200906 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
907 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
Paul Bakkerca4ab492012-04-18 14:23:57 +0000909 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000910 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
911
Paul Bakker5b4af392014-06-26 12:09:34 +0200912 sha512_free( &sha512 );
913
Paul Bakker5121ce52009-01-03 21:22:43 +0000914 return;
915}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200916#endif /* POLARSSL_SHA512_C */
917#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000918
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200919#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
921{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200922 unsigned char *p = ssl->handshake->premaster;
923 unsigned char *end = p + sizeof( ssl->handshake->premaster );
924
925 /*
926 * PMS = struct {
927 * opaque other_secret<0..2^16-1>;
928 * opaque psk<0..2^16-1>;
929 * };
930 * with "other_secret" depending on the particular key exchange
931 */
932#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
933 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
934 {
935 if( end - p < 2 + (int) ssl->psk_len )
936 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
937
938 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
939 *(p++) = (unsigned char)( ssl->psk_len );
940 p += ssl->psk_len;
941 }
942 else
943#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200944#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
945 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
946 {
947 /*
948 * other_secret already set by the ClientKeyExchange message,
949 * and is 48 bytes long
950 */
951 *p++ = 0;
952 *p++ = 48;
953 p += 48;
954 }
955 else
956#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200957#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
958 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
959 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200960 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200961 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200962
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200963 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200964 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200965 p + 2, &len,
966 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200967 {
968 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
969 return( ret );
970 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200971 *(p++) = (unsigned char)( len >> 8 );
972 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 p += len;
974
975 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
976 }
977 else
978#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
979#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
980 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
981 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200982 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983 size_t zlen;
984
985 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200986 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200987 ssl->f_rng, ssl->p_rng ) ) != 0 )
988 {
989 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
990 return( ret );
991 }
992
993 *(p++) = (unsigned char)( zlen >> 8 );
994 *(p++) = (unsigned char)( zlen );
995 p += zlen;
996
997 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
998 }
999 else
1000#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
1001 {
1002 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001003 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001004 }
1005
1006 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001007 if( end - p < 2 + (int) ssl->psk_len )
1008 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1009
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001010 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
1011 *(p++) = (unsigned char)( ssl->psk_len );
1012 memcpy( p, ssl->psk, ssl->psk_len );
1013 p += ssl->psk_len;
1014
1015 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1016
1017 return( 0 );
1018}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001019#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001020
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001022/*
1023 * SSLv3.0 MAC functions
1024 */
Paul Bakker68884e32013-01-07 18:20:04 +01001025static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
1026 unsigned char *buf, size_t len,
1027 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +00001028{
1029 unsigned char header[11];
1030 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001031 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +01001032 int md_size = md_get_size( md_ctx->md_info );
1033 int md_type = md_get_type( md_ctx->md_info );
1034
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001035 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +01001036 if( md_type == POLARSSL_MD_MD5 )
1037 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001038 else
Paul Bakker68884e32013-01-07 18:20:04 +01001039 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
1041 memcpy( header, ctr, 8 );
1042 header[ 8] = (unsigned char) type;
1043 header[ 9] = (unsigned char)( len >> 8 );
1044 header[10] = (unsigned char)( len );
1045
Paul Bakker68884e32013-01-07 18:20:04 +01001046 memset( padding, 0x36, padlen );
1047 md_starts( md_ctx );
1048 md_update( md_ctx, secret, md_size );
1049 md_update( md_ctx, padding, padlen );
1050 md_update( md_ctx, header, 11 );
1051 md_update( md_ctx, buf, len );
1052 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Paul Bakker68884e32013-01-07 18:20:04 +01001054 memset( padding, 0x5C, padlen );
1055 md_starts( md_ctx );
1056 md_update( md_ctx, secret, md_size );
1057 md_update( md_ctx, padding, padlen );
1058 md_update( md_ctx, buf + len, md_size );
1059 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001060}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001061#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001062
Paul Bakker5121ce52009-01-03 21:22:43 +00001063/*
1064 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001065 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001066static int ssl_encrypt_buf( ssl_context *ssl )
1067{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001068 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001069 const cipher_mode_t mode = cipher_get_cipher_mode(
1070 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001071
1072 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1073
1074 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001075 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001077#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1078 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1079 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001080 if( mode != POLARSSL_MODE_GCM &&
1081 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001082 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001083#if defined(POLARSSL_SSL_PROTO_SSL3)
1084 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1085 {
1086 ssl_mac( &ssl->transform_out->md_ctx_enc,
1087 ssl->transform_out->mac_enc,
1088 ssl->out_msg, ssl->out_msglen,
1089 ssl->out_ctr, ssl->out_msgtype );
1090 }
1091 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001092#endif
1093#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001094 defined(POLARSSL_SSL_PROTO_TLS1_2)
1095 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1096 {
1097 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1098 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1099 ssl->out_msg, ssl->out_msglen );
1100 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1101 ssl->out_msg + ssl->out_msglen );
1102 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1103 }
1104 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001105#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001106 {
1107 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001108 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001109 }
1110
1111 SSL_DEBUG_BUF( 4, "computed mac",
1112 ssl->out_msg + ssl->out_msglen,
1113 ssl->transform_out->maclen );
1114
1115 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001116 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001117#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001119 /*
1120 * Encrypt
1121 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001122#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001123 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001124 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001125 int ret;
1126 size_t olen = 0;
1127
Paul Bakker5121ce52009-01-03 21:22:43 +00001128 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1129 "including %d bytes of padding",
1130 ssl->out_msglen, 0 ) );
1131
1132 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1133 ssl->out_msg, ssl->out_msglen );
1134
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001135 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001136 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001137 ssl->transform_out->ivlen,
1138 ssl->out_msg, ssl->out_msglen,
1139 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001140 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001141 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001142 return( ret );
1143 }
1144
1145 if( ssl->out_msglen != olen )
1146 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001147 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001148 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001150 }
Paul Bakker68884e32013-01-07 18:20:04 +01001151 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001152#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001153#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1154 if( mode == POLARSSL_MODE_GCM ||
1155 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001156 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001157 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001158 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001159 unsigned char *enc_msg;
1160 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001161 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1162 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001163
Paul Bakkerca4ab492012-04-18 14:23:57 +00001164 memcpy( add_data, ssl->out_ctr, 8 );
1165 add_data[8] = ssl->out_msgtype;
1166 add_data[9] = ssl->major_ver;
1167 add_data[10] = ssl->minor_ver;
1168 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1169 add_data[12] = ssl->out_msglen & 0xFF;
1170
1171 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1172 add_data, 13 );
1173
Paul Bakker68884e32013-01-07 18:20:04 +01001174 /*
1175 * Generate IV
1176 */
1177 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001178 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1179 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001180 if( ret != 0 )
1181 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001182
Paul Bakker68884e32013-01-07 18:20:04 +01001183 memcpy( ssl->out_iv,
1184 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1185 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001186
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001187 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001188 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001189
Paul Bakker68884e32013-01-07 18:20:04 +01001190 /*
1191 * Fix pointer positions and message length with added IV
1192 */
1193 enc_msg = ssl->out_msg;
1194 enc_msglen = ssl->out_msglen;
1195 ssl->out_msglen += ssl->transform_out->ivlen -
1196 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001197
Paul Bakker68884e32013-01-07 18:20:04 +01001198 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1199 "including %d bytes of padding",
1200 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001201
Paul Bakker68884e32013-01-07 18:20:04 +01001202 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1203 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001204
Paul Bakker68884e32013-01-07 18:20:04 +01001205 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001206 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001207 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001208 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1209 ssl->transform_out->iv_enc,
1210 ssl->transform_out->ivlen,
1211 add_data, 13,
1212 enc_msg, enc_msglen,
1213 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001214 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001215 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001216 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001217 return( ret );
1218 }
1219
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001220 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001221 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001222 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001223 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001224 }
1225
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001226 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001227
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001228 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001229 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001231#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001232#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1233 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001234 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001236 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001237 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001238 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001239
Paul Bakker48916f92012-09-16 19:57:18 +00001240 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1241 ssl->transform_out->ivlen;
1242 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 padlen = 0;
1244
1245 for( i = 0; i <= padlen; i++ )
1246 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1247
1248 ssl->out_msglen += padlen + 1;
1249
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250 enc_msglen = ssl->out_msglen;
1251 enc_msg = ssl->out_msg;
1252
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001253#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001254 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001255 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1256 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001258 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001259 {
1260 /*
1261 * Generate IV
1262 */
Paul Bakker48916f92012-09-16 19:57:18 +00001263 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1264 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001265 if( ret != 0 )
1266 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001267
Paul Bakker92be97b2013-01-02 17:30:03 +01001268 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001269 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001270
1271 /*
1272 * Fix pointer positions and message length with added IV
1273 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001274 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001275 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001276 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001277 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001278#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001279
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001281 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001282 ssl->out_msglen, ssl->transform_out->ivlen,
1283 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
1285 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001286 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001287
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001288 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001289 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001290 ssl->transform_out->ivlen,
1291 enc_msg, enc_msglen,
1292 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001293 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001294 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001295 return( ret );
1296 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001297
Paul Bakkercca5b812013-08-31 17:40:26 +02001298 if( enc_msglen != olen )
1299 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001300 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001301 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001302 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001303
1304#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001305 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1306 {
1307 /*
1308 * Save IV in SSL3 and TLS1
1309 */
1310 memcpy( ssl->transform_out->iv_enc,
1311 ssl->transform_out->cipher_ctx_enc.iv,
1312 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001313 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001316 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001317#endif /* POLARSSL_CIPHER_MODE_CBC &&
1318 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001319 {
1320 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001321 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001323
Paul Bakkerca4ab492012-04-18 14:23:57 +00001324 for( i = 8; i > 0; i-- )
1325 if( ++ssl->out_ctr[i - 1] != 0 )
1326 break;
1327
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001328 /* The loops goes to its end iff the counter is wrapping */
1329 if( i == 0 )
1330 {
1331 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1332 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1333 }
1334
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1336
1337 return( 0 );
1338}
1339
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001340#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001341
Paul Bakker5121ce52009-01-03 21:22:43 +00001342static int ssl_decrypt_buf( ssl_context *ssl )
1343{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001344 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001345 const cipher_mode_t mode = cipher_get_cipher_mode(
1346 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001347#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1348 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1349 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1350 size_t padlen = 0, correct = 1;
1351#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
1353 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1354
Paul Bakker48916f92012-09-16 19:57:18 +00001355 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 {
1357 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001358 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001359 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 }
1361
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001362#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001363 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001364 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001365 int ret;
1366 size_t olen = 0;
1367
Paul Bakker68884e32013-01-07 18:20:04 +01001368 padlen = 0;
1369
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001370 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001371 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001372 ssl->transform_in->ivlen,
1373 ssl->in_msg, ssl->in_msglen,
1374 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001375 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001376 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001377 return( ret );
1378 }
1379
1380 if( ssl->in_msglen != olen )
1381 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001382 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001383 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
Paul Bakker68884e32013-01-07 18:20:04 +01001386 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001387#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001388#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1389 if( mode == POLARSSL_MODE_GCM ||
1390 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001391 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001392 int ret;
1393 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001394 unsigned char *dec_msg;
1395 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001396 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001397 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1398 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001399 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1400 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001401
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001402 if( ssl->in_msglen < explicit_iv_len + taglen )
1403 {
1404 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1405 "+ taglen (%d)", ssl->in_msglen,
1406 explicit_iv_len, taglen ) );
1407 return( POLARSSL_ERR_SSL_INVALID_MAC );
1408 }
1409 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1410
Paul Bakker68884e32013-01-07 18:20:04 +01001411 dec_msg = ssl->in_msg;
1412 dec_msg_result = ssl->in_msg;
1413 ssl->in_msglen = dec_msglen;
1414
1415 memcpy( add_data, ssl->in_ctr, 8 );
1416 add_data[8] = ssl->in_msgtype;
1417 add_data[9] = ssl->major_ver;
1418 add_data[10] = ssl->minor_ver;
1419 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1420 add_data[12] = ssl->in_msglen & 0xFF;
1421
1422 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1423 add_data, 13 );
1424
1425 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1426 ssl->in_iv,
1427 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1428
1429 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1430 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001431 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001432
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001433 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001434 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001435 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001436 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1437 ssl->transform_in->iv_dec,
1438 ssl->transform_in->ivlen,
1439 add_data, 13,
1440 dec_msg, dec_msglen,
1441 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001442 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001443 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001444 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1445
1446 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1447 return( POLARSSL_ERR_SSL_INVALID_MAC );
1448
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001449 return( ret );
1450 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001451
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001452 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001453 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001456 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001457 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001459#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001460#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1461 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001462 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001463 {
Paul Bakker45829992013-01-03 14:52:21 +01001464 /*
1465 * Decrypt and check the padding
1466 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001467 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001468 unsigned char *dec_msg;
1469 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001470 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001471 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001472 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001473
Paul Bakker5121ce52009-01-03 21:22:43 +00001474 /*
Paul Bakker45829992013-01-03 14:52:21 +01001475 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001476 */
Paul Bakker48916f92012-09-16 19:57:18 +00001477 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001478 {
1479 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001480 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001481 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 }
1483
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001484#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001485 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1486 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001487#endif
Paul Bakker45829992013-01-03 14:52:21 +01001488
1489 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1490 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1491 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001492 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1493 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1494 ssl->transform_in->ivlen,
1495 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001496 return( POLARSSL_ERR_SSL_INVALID_MAC );
1497 }
1498
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001499 dec_msglen = ssl->in_msglen;
1500 dec_msg = ssl->in_msg;
1501 dec_msg_result = ssl->in_msg;
1502
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001503#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001504 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001505 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001506 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001507 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001508 {
Paul Bakker48916f92012-09-16 19:57:18 +00001509 dec_msglen -= ssl->transform_in->ivlen;
1510 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001511
Paul Bakker48916f92012-09-16 19:57:18 +00001512 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001513 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001514 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001515#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001516
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001517 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001518 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001519 ssl->transform_in->ivlen,
1520 dec_msg, dec_msglen,
1521 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001522 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001523 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001524 return( ret );
1525 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526
Paul Bakkercca5b812013-08-31 17:40:26 +02001527 if( dec_msglen != olen )
1528 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001529 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001530 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001531 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001532
1533#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001534 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1535 {
1536 /*
1537 * Save IV in SSL3 and TLS1
1538 */
1539 memcpy( ssl->transform_in->iv_dec,
1540 ssl->transform_in->cipher_ctx_dec.iv,
1541 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001543#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001544
1545 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001546
1547 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1548 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001549#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001550 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1551 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001552#endif
Paul Bakker45829992013-01-03 14:52:21 +01001553 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001554 correct = 0;
1555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001557#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001558 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1559 {
Paul Bakker48916f92012-09-16 19:57:18 +00001560 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001561 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001562#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001563 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1564 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001565 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001566#endif
Paul Bakker45829992013-01-03 14:52:21 +01001567 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001568 }
1569 }
1570 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001571#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001572#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1573 defined(POLARSSL_SSL_PROTO_TLS1_2)
1574 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001575 {
1576 /*
Paul Bakker45829992013-01-03 14:52:21 +01001577 * TLSv1+: always check the padding up to the first failure
1578 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001579 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001580 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001581 size_t padding_idx = ssl->in_msglen - padlen - 1;
1582
Paul Bakker956c9e02013-12-19 14:42:28 +01001583 /*
1584 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001585 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001586 *
Paul Bakker61885c72014-04-25 12:59:03 +02001587 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1588 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001589 *
1590 * In both cases we reset padding_idx to a safe value (0) to
1591 * prevent out-of-buffer reads.
1592 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001593 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001594 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1595 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001596
1597 padding_idx *= correct;
1598
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001599 for( i = 1; i <= 256; i++ )
1600 {
1601 real_count &= ( i <= padlen );
1602 pad_count += real_count *
1603 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1604 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001605
1606 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001607
Paul Bakkerd66f0702013-01-31 16:57:45 +01001608#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001609 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001610 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001611#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001612 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001614 else
1615#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1616 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001617 {
1618 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001619 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001621 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001622 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001623#endif /* POLARSSL_CIPHER_MODE_CBC &&
1624 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001625 {
1626 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001627 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001628 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
1630 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1631 ssl->in_msg, ssl->in_msglen );
1632
1633 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001634 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001636#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1637 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1638 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001639 if( mode != POLARSSL_MODE_GCM &&
1640 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001641 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001642 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1643
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001644 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1647 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001648
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001649 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001651#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001652 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1653 {
1654 ssl_mac( &ssl->transform_in->md_ctx_dec,
1655 ssl->transform_in->mac_dec,
1656 ssl->in_msg, ssl->in_msglen,
1657 ssl->in_ctr, ssl->in_msgtype );
1658 }
1659 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001660#endif /* POLARSSL_SSL_PROTO_SSL3 */
1661#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001662 defined(POLARSSL_SSL_PROTO_TLS1_2)
1663 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1664 {
1665 /*
1666 * Process MAC and always update for padlen afterwards to make
1667 * total time independent of padlen
1668 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001669 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001670 *
1671 * Known timing attacks:
1672 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1673 *
1674 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1675 * correctly. (We round down instead of up, so -56 is the correct
1676 * value for our calculations instead of -55)
1677 */
1678 size_t j, extra_run = 0;
1679 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1680 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001681
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001682 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001683
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001684 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1685 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1686 ssl->in_msglen );
1687 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1688 ssl->in_msg + ssl->in_msglen );
1689 for( j = 0; j < extra_run; j++ )
1690 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001691
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001692 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1693 }
1694 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001695#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001696 POLARSSL_SSL_PROTO_TLS1_2 */
1697 {
1698 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001699 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001702 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1703 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1704 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001705
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001706 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001707 ssl->transform_in->maclen ) != 0 )
1708 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001709#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001710 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001711#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001712 correct = 0;
1713 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001715 /*
1716 * Finally check the correct flag
1717 */
1718 if( correct == 0 )
1719 return( POLARSSL_ERR_SSL_INVALID_MAC );
1720 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001721#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
1723 if( ssl->in_msglen == 0 )
1724 {
1725 ssl->nb_zero++;
1726
1727 /*
1728 * Three or more empty messages may be a DoS attack
1729 * (excessive CPU consumption).
1730 */
1731 if( ssl->nb_zero > 3 )
1732 {
1733 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1734 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001735 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 }
1737 }
1738 else
1739 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001740
Paul Bakker23986e52011-04-24 08:57:21 +00001741 for( i = 8; i > 0; i-- )
1742 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001743 break;
1744
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001745 /* The loops goes to its end iff the counter is wrapping */
1746 if( i == 0 )
1747 {
1748 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1749 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1750 }
1751
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1753
1754 return( 0 );
1755}
1756
Paul Bakker2770fbd2012-07-03 13:30:23 +00001757#if defined(POLARSSL_ZLIB_SUPPORT)
1758/*
1759 * Compression/decompression functions
1760 */
1761static int ssl_compress_buf( ssl_context *ssl )
1762{
1763 int ret;
1764 unsigned char *msg_post = ssl->out_msg;
1765 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001766 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001767
1768 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1769
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001770 if( len_pre == 0 )
1771 return( 0 );
1772
Paul Bakker2770fbd2012-07-03 13:30:23 +00001773 memcpy( msg_pre, ssl->out_msg, len_pre );
1774
1775 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1776 ssl->out_msglen ) );
1777
1778 SSL_DEBUG_BUF( 4, "before compression: output payload",
1779 ssl->out_msg, ssl->out_msglen );
1780
Paul Bakker48916f92012-09-16 19:57:18 +00001781 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1782 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1783 ssl->transform_out->ctx_deflate.next_out = msg_post;
1784 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001785
Paul Bakker48916f92012-09-16 19:57:18 +00001786 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001787 if( ret != Z_OK )
1788 {
1789 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1790 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1791 }
1792
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001793 ssl->out_msglen = SSL_BUFFER_LEN -
1794 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1797 ssl->out_msglen ) );
1798
1799 SSL_DEBUG_BUF( 4, "after compression: output payload",
1800 ssl->out_msg, ssl->out_msglen );
1801
1802 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1803
1804 return( 0 );
1805}
1806
1807static int ssl_decompress_buf( ssl_context *ssl )
1808{
1809 int ret;
1810 unsigned char *msg_post = ssl->in_msg;
1811 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001812 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001813
1814 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1815
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001816 if( len_pre == 0 )
1817 return( 0 );
1818
Paul Bakker2770fbd2012-07-03 13:30:23 +00001819 memcpy( msg_pre, ssl->in_msg, len_pre );
1820
1821 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1822 ssl->in_msglen ) );
1823
1824 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1825 ssl->in_msg, ssl->in_msglen );
1826
Paul Bakker48916f92012-09-16 19:57:18 +00001827 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1828 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1829 ssl->transform_in->ctx_inflate.next_out = msg_post;
1830 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001831
Paul Bakker48916f92012-09-16 19:57:18 +00001832 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001833 if( ret != Z_OK )
1834 {
1835 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1836 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1837 }
1838
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001839 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1840 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001841
Paul Bakker2770fbd2012-07-03 13:30:23 +00001842 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1843 ssl->in_msglen ) );
1844
1845 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1846 ssl->in_msg, ssl->in_msglen );
1847
1848 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1849
1850 return( 0 );
1851}
1852#endif /* POLARSSL_ZLIB_SUPPORT */
1853
Paul Bakker5121ce52009-01-03 21:22:43 +00001854/*
1855 * Fill the input message buffer
1856 */
Paul Bakker23986e52011-04-24 08:57:21 +00001857int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001858{
Paul Bakker23986e52011-04-24 08:57:21 +00001859 int ret;
1860 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
1862 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1863
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001864 if( nb_want > SSL_BUFFER_LEN - 8 )
1865 {
1866 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1867 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1868 }
1869
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 while( ssl->in_left < nb_want )
1871 {
1872 len = nb_want - ssl->in_left;
1873 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1874
1875 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1876 ssl->in_left, nb_want ) );
1877 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1878
Paul Bakker831a7552011-05-18 13:32:51 +00001879 if( ret == 0 )
1880 return( POLARSSL_ERR_SSL_CONN_EOF );
1881
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 if( ret < 0 )
1883 return( ret );
1884
1885 ssl->in_left += ret;
1886 }
1887
1888 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1889
1890 return( 0 );
1891}
1892
1893/*
1894 * Flush any data not yet written
1895 */
1896int ssl_flush_output( ssl_context *ssl )
1897{
1898 int ret;
1899 unsigned char *buf;
1900
1901 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1902
1903 while( ssl->out_left > 0 )
1904 {
1905 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1906 5 + ssl->out_msglen, ssl->out_left ) );
1907
Paul Bakker5bd42292012-12-19 14:40:42 +01001908 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001910
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1912
1913 if( ret <= 0 )
1914 return( ret );
1915
1916 ssl->out_left -= ret;
1917 }
1918
1919 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1920
1921 return( 0 );
1922}
1923
1924/*
1925 * Record layer functions
1926 */
1927int ssl_write_record( ssl_context *ssl )
1928{
Paul Bakker05ef8352012-05-08 09:17:57 +00001929 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001930 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001931
1932 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1933
Paul Bakker5121ce52009-01-03 21:22:43 +00001934 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1935 {
1936 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1937 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1938 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1939
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001940 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1941 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 }
1943
Paul Bakker2770fbd2012-07-03 13:30:23 +00001944#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001945 if( ssl->transform_out != NULL &&
1946 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001947 {
1948 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1949 {
1950 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1951 return( ret );
1952 }
1953
1954 len = ssl->out_msglen;
1955 }
1956#endif /*POLARSSL_ZLIB_SUPPORT */
1957
Paul Bakker05ef8352012-05-08 09:17:57 +00001958#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001959 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001960 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001961 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Paul Bakker05ef8352012-05-08 09:17:57 +00001963 ret = ssl_hw_record_write( ssl );
1964 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1965 {
1966 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001967 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001968 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001969
1970 if( ret == 0 )
1971 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001972 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001973#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001974 if( !done )
1975 {
1976 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1977 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1978 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1980 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001981
Paul Bakker48916f92012-09-16 19:57:18 +00001982 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001983 {
1984 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1985 {
1986 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1987 return( ret );
1988 }
1989
1990 len = ssl->out_msglen;
1991 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1992 ssl->out_hdr[4] = (unsigned char)( len );
1993 }
1994
1995 ssl->out_left = 5 + ssl->out_msglen;
1996
1997 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1998 "version = [%d:%d], msglen = %d",
1999 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
2000 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
2001
2002 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01002003 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 }
2005
Paul Bakker5121ce52009-01-03 21:22:43 +00002006 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2007 {
2008 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
2009 return( ret );
2010 }
2011
2012 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
2013
2014 return( 0 );
2015}
2016
2017int ssl_read_record( ssl_context *ssl )
2018{
Paul Bakker05ef8352012-05-08 09:17:57 +00002019 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002020
2021 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
2022
2023 if( ssl->in_hslen != 0 &&
2024 ssl->in_hslen < ssl->in_msglen )
2025 {
2026 /*
2027 * Get next Handshake message in the current record
2028 */
2029 ssl->in_msglen -= ssl->in_hslen;
2030
Paul Bakker8934a982011-08-05 11:11:53 +00002031 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002032 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002033
2034 ssl->in_hslen = 4;
2035 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2036
2037 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2038 " %d, type = %d, hslen = %d",
2039 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2040
2041 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2042 {
2043 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002044 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 }
2046
2047 if( ssl->in_msglen < ssl->in_hslen )
2048 {
2049 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002050 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 }
2052
Paul Bakker4224bc02014-04-08 14:36:50 +02002053 if( ssl->state != SSL_HANDSHAKE_OVER )
2054 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002055
2056 return( 0 );
2057 }
2058
2059 ssl->in_hslen = 0;
2060
2061 /*
2062 * Read the record header and validate it
2063 */
2064 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2065 {
2066 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2067 return( ret );
2068 }
2069
2070 ssl->in_msgtype = ssl->in_hdr[0];
2071 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2072
2073 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2074 "version = [%d:%d], msglen = %d",
2075 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2076 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2077
2078 if( ssl->in_hdr[1] != ssl->major_ver )
2079 {
2080 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002081 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002082 }
2083
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002084 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 {
2086 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002087 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 }
2089
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002090 /* Sanity check (outer boundaries) */
2091 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2092 {
2093 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2094 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2095 }
2096
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002098 * Make sure the message length is acceptable for the current transform
2099 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 */
Paul Bakker48916f92012-09-16 19:57:18 +00002101 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002103 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 {
2105 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002106 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002107 }
2108 }
2109 else
2110 {
Paul Bakker48916f92012-09-16 19:57:18 +00002111 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 {
2113 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002114 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 }
2116
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002117#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002118 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002119 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120 {
2121 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002122 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002123 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002124#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002125
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002126#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2127 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002128 /*
2129 * TLS encrypted messages can have up to 256 bytes of padding
2130 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002131 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002132 ssl->in_msglen > ssl->transform_in->minlen +
2133 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002134 {
2135 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002136 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002137 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 }
2140
2141 /*
2142 * Read and optionally decrypt the message contents
2143 */
2144 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2145 {
2146 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2147 return( ret );
2148 }
2149
2150 SSL_DEBUG_BUF( 4, "input record from network",
2151 ssl->in_hdr, 5 + ssl->in_msglen );
2152
Paul Bakker05ef8352012-05-08 09:17:57 +00002153#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002154 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002155 {
2156 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2157
2158 ret = ssl_hw_record_read( ssl );
2159 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2160 {
2161 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002162 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002163 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002164
2165 if( ret == 0 )
2166 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002167 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002168#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002169 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002170 {
2171 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2172 {
Paul Bakker40865c82013-01-31 17:13:13 +01002173#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2174 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2175 {
2176 ssl_send_alert_message( ssl,
2177 SSL_ALERT_LEVEL_FATAL,
2178 SSL_ALERT_MSG_BAD_RECORD_MAC );
2179 }
2180#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002181 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2182 return( ret );
2183 }
2184
2185 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2186 ssl->in_msg, ssl->in_msglen );
2187
2188 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2189 {
2190 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002191 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
2193 }
2194
Paul Bakker2770fbd2012-07-03 13:30:23 +00002195#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002196 if( ssl->transform_in != NULL &&
2197 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002198 {
2199 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2200 {
2201 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2202 return( ret );
2203 }
2204
2205 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2206 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2207 }
2208#endif /* POLARSSL_ZLIB_SUPPORT */
2209
Paul Bakker0a925182012-04-16 06:46:41 +00002210 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2211 ssl->in_msgtype != SSL_MSG_ALERT &&
2212 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2213 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2214 {
2215 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2216
Paul Bakker48916f92012-09-16 19:57:18 +00002217 if( ( ret = ssl_send_alert_message( ssl,
2218 SSL_ALERT_LEVEL_FATAL,
2219 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002220 {
2221 return( ret );
2222 }
2223
2224 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2225 }
2226
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2228 {
2229 ssl->in_hslen = 4;
2230 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2231
2232 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2233 " %d, type = %d, hslen = %d",
2234 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2235
2236 /*
2237 * Additional checks to validate the handshake header
2238 */
2239 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2240 {
2241 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002242 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002243 }
2244
2245 if( ssl->in_msglen < ssl->in_hslen )
2246 {
2247 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002248 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 }
2250
Paul Bakker48916f92012-09-16 19:57:18 +00002251 if( ssl->state != SSL_HANDSHAKE_OVER )
2252 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002253 }
2254
2255 if( ssl->in_msgtype == SSL_MSG_ALERT )
2256 {
2257 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2258 ssl->in_msg[0], ssl->in_msg[1] ) );
2259
2260 /*
2261 * Ignore non-fatal alerts, except close_notify
2262 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002263 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002264 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002265 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2266 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002267 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 }
2269
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002270 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2271 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002272 {
2273 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002274 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 }
2276 }
2277
2278 ssl->in_left = 0;
2279
2280 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2281
2282 return( 0 );
2283}
2284
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002285int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2286{
2287 int ret;
2288
2289 if( ( ret = ssl_send_alert_message( ssl,
2290 SSL_ALERT_LEVEL_FATAL,
2291 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2292 {
2293 return( ret );
2294 }
2295
2296 return( 0 );
2297}
2298
Paul Bakker0a925182012-04-16 06:46:41 +00002299int ssl_send_alert_message( ssl_context *ssl,
2300 unsigned char level,
2301 unsigned char message )
2302{
2303 int ret;
2304
2305 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2306
2307 ssl->out_msgtype = SSL_MSG_ALERT;
2308 ssl->out_msglen = 2;
2309 ssl->out_msg[0] = level;
2310 ssl->out_msg[1] = message;
2311
2312 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2313 {
2314 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2315 return( ret );
2316 }
2317
2318 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2319
2320 return( 0 );
2321}
2322
Paul Bakker5121ce52009-01-03 21:22:43 +00002323/*
2324 * Handshake functions
2325 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002326#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2327 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2328 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2329 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2330 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2331 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2332 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002333int ssl_write_certificate( ssl_context *ssl )
2334{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002335 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002336
2337 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2338
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002339 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2341 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002342 {
2343 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2344 ssl->state++;
2345 return( 0 );
2346 }
2347
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002348 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2349 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350}
2351
2352int ssl_parse_certificate( ssl_context *ssl )
2353{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002354 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2355
2356 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2357
2358 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002359 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2360 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361 {
2362 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2363 ssl->state++;
2364 return( 0 );
2365 }
2366
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002367 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2368 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002369}
2370#else
2371int ssl_write_certificate( ssl_context *ssl )
2372{
2373 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2374 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002375 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2377
2378 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2379
2380 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002381 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2382 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 {
2384 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2385 ssl->state++;
2386 return( 0 );
2387 }
2388
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 if( ssl->endpoint == SSL_IS_CLIENT )
2390 {
2391 if( ssl->client_auth == 0 )
2392 {
2393 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2394 ssl->state++;
2395 return( 0 );
2396 }
2397
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002398#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002399 /*
2400 * If using SSLv3 and got no cert, send an Alert message
2401 * (otherwise an empty Certificate message will be sent).
2402 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002403 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2405 {
2406 ssl->out_msglen = 2;
2407 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002408 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2409 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2412 goto write_msg;
2413 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002414#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002415 }
2416 else /* SSL_IS_SERVER */
2417 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002418 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 {
2420 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002421 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 }
2423 }
2424
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002425 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002426
2427 /*
2428 * 0 . 0 handshake type
2429 * 1 . 3 handshake length
2430 * 4 . 6 length of all certs
2431 * 7 . 9 length of cert. 1
2432 * 10 . n-1 peer certificate
2433 * n . n+2 length of cert. 2
2434 * n+3 . ... upper level cert, etc.
2435 */
2436 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002437 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
Paul Bakker29087132010-03-21 21:03:34 +00002439 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002440 {
2441 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002442 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 {
2444 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2445 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002446 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
2449 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2450 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2451 ssl->out_msg[i + 2] = (unsigned char)( n );
2452
2453 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2454 i += n; crt = crt->next;
2455 }
2456
2457 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2458 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2459 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2460
2461 ssl->out_msglen = i;
2462 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2463 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2464
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002465#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002466write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002467#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
2469 ssl->state++;
2470
2471 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2472 {
2473 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2474 return( ret );
2475 }
2476
2477 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2478
Paul Bakkered27a042013-04-18 22:46:23 +02002479 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002480}
2481
2482int ssl_parse_certificate( ssl_context *ssl )
2483{
Paul Bakkered27a042013-04-18 22:46:23 +02002484 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002485 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002486 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
2488 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2489
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002491 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2492 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002493 {
2494 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2495 ssl->state++;
2496 return( 0 );
2497 }
2498
Paul Bakker5121ce52009-01-03 21:22:43 +00002499 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002500 ( ssl->authmode == SSL_VERIFY_NONE ||
2501 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002503 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2505 ssl->state++;
2506 return( 0 );
2507 }
2508
2509 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2510 {
2511 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2512 return( ret );
2513 }
2514
2515 ssl->state++;
2516
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002517#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 /*
2519 * Check if the client sent an empty certificate
2520 */
2521 if( ssl->endpoint == SSL_IS_SERVER &&
2522 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2523 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002524 if( ssl->in_msglen == 2 &&
2525 ssl->in_msgtype == SSL_MSG_ALERT &&
2526 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2527 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 {
2529 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2530
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002531 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2533 return( 0 );
2534 else
Paul Bakker40e46942009-01-03 21:51:57 +00002535 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
2537 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002538#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002540#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2541 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 if( ssl->endpoint == SSL_IS_SERVER &&
2543 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2544 {
2545 if( ssl->in_hslen == 7 &&
2546 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2547 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2548 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2549 {
2550 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2551
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002552 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002553 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002554 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002555 else
2556 return( 0 );
2557 }
2558 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002559#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2560 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
2562 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2563 {
2564 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002565 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002566 }
2567
2568 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2569 {
2570 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002571 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 }
2573
2574 /*
2575 * Same message structure as in ssl_write_certificate()
2576 */
2577 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2578
2579 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2580 {
2581 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002582 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 }
2584
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002585 /* In case we tried to reuse a session but it failed */
2586 if( ssl->session_negotiate->peer_cert != NULL )
2587 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002588 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002589 polarssl_free( ssl->session_negotiate->peer_cert );
2590 }
2591
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002592 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2593 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 {
2595 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002596 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002597 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 }
2599
Paul Bakkerb6b09562013-09-18 14:17:41 +02002600 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002601
2602 i = 7;
2603
2604 while( i < ssl->in_hslen )
2605 {
2606 if( ssl->in_msg[i] != 0 )
2607 {
2608 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002609 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
2611
2612 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2613 | (unsigned int) ssl->in_msg[i + 2];
2614 i += 3;
2615
2616 if( n < 128 || i + n > ssl->in_hslen )
2617 {
2618 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002619 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002620 }
2621
Paul Bakkerddf26b42013-09-18 13:46:23 +02002622 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2623 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002624 if( ret != 0 )
2625 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002626 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 return( ret );
2628 }
2629
2630 i += n;
2631 }
2632
Paul Bakker48916f92012-09-16 19:57:18 +00002633 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002635 /*
2636 * On client, make sure the server cert doesn't change during renego to
2637 * avoid "triple handshake" attack: https://secure-resumption.com/
2638 */
2639 if( ssl->endpoint == SSL_IS_CLIENT &&
2640 ssl->renegotiation == SSL_RENEGOTIATION )
2641 {
2642 if( ssl->session->peer_cert == NULL )
2643 {
2644 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2645 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2646 }
2647
2648 if( ssl->session->peer_cert->raw.len !=
2649 ssl->session_negotiate->peer_cert->raw.len ||
2650 memcmp( ssl->session->peer_cert->raw.p,
2651 ssl->session_negotiate->peer_cert->raw.p,
2652 ssl->session->peer_cert->raw.len ) != 0 )
2653 {
2654 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2655 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2656 }
2657 }
2658
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 if( ssl->authmode != SSL_VERIFY_NONE )
2660 {
2661 if( ssl->ca_chain == NULL )
2662 {
2663 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002664 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 }
2666
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002667 /*
2668 * Main check: verify certificate
2669 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002670 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2671 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2672 &ssl->session_negotiate->verify_result,
2673 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
2675 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002676 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002677 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002678 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002679
2680 /*
2681 * Secondary checks: always done, but change 'ret' only if it was 0
2682 */
2683
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002684#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002685 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002686 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002687
2688 /* If certificate uses an EC key, make sure the curve is OK */
2689 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2690 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2691 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002692 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002693 if( ret == 0 )
2694 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002695 }
2696 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002697#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002698
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002699 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2700 ciphersuite_info,
2701 ! ssl->endpoint ) != 0 )
2702 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002703 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002704 if( ret == 0 )
2705 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2706 }
2707
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2709 ret = 0;
2710 }
2711
2712 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2713
2714 return( ret );
2715}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002716#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2717 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2718 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2719 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2720 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2721 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2722 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
2724int ssl_write_change_cipher_spec( ssl_context *ssl )
2725{
2726 int ret;
2727
2728 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2729
2730 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2731 ssl->out_msglen = 1;
2732 ssl->out_msg[0] = 1;
2733
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 ssl->state++;
2735
2736 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2737 {
2738 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2739 return( ret );
2740 }
2741
2742 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2743
2744 return( 0 );
2745}
2746
2747int ssl_parse_change_cipher_spec( ssl_context *ssl )
2748{
2749 int ret;
2750
2751 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2752
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2754 {
2755 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2756 return( ret );
2757 }
2758
2759 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2760 {
2761 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002762 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 }
2764
2765 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2766 {
2767 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002768 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002769 }
2770
2771 ssl->state++;
2772
2773 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2774
2775 return( 0 );
2776}
2777
Paul Bakker41c83d32013-03-20 14:39:14 +01002778void ssl_optimize_checksum( ssl_context *ssl,
2779 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002780{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002781 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002782
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2784 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002785 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002786 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002787 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788#endif
2789#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2790#if defined(POLARSSL_SHA512_C)
2791 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2792 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2793 else
2794#endif
2795#if defined(POLARSSL_SHA256_C)
2796 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002797 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798 else
2799#endif
2800#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002801 {
2802 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002803 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002804 }
Paul Bakker380da532012-04-18 16:10:25 +00002805}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002806
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002807static void ssl_update_checksum_start( ssl_context *ssl,
2808 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002809{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2811 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002812 md5_update( &ssl->handshake->fin_md5 , buf, len );
2813 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002814#endif
2815#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2816#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002817 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002818#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002819#if defined(POLARSSL_SHA512_C)
2820 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002821#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002822#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002823}
2824
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002825#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2826 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002827static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2828 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002829{
Paul Bakker48916f92012-09-16 19:57:18 +00002830 md5_update( &ssl->handshake->fin_md5 , buf, len );
2831 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002832}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002833#endif
Paul Bakker380da532012-04-18 16:10:25 +00002834
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002835#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2836#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002837static void ssl_update_checksum_sha256( ssl_context *ssl,
2838 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002839{
Paul Bakker9e36f042013-06-30 14:34:05 +02002840 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002841}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002842#endif
Paul Bakker380da532012-04-18 16:10:25 +00002843
Paul Bakker9e36f042013-06-30 14:34:05 +02002844#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002845static void ssl_update_checksum_sha384( ssl_context *ssl,
2846 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002847{
Paul Bakker9e36f042013-06-30 14:34:05 +02002848 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002849}
Paul Bakker769075d2012-11-24 11:26:46 +01002850#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002851#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002852
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002853#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002854static void ssl_calc_finished_ssl(
2855 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002856{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002857 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002858 md5_context md5;
2859 sha1_context sha1;
2860
Paul Bakker5121ce52009-01-03 21:22:43 +00002861 unsigned char padbuf[48];
2862 unsigned char md5sum[16];
2863 unsigned char sha1sum[20];
2864
Paul Bakker48916f92012-09-16 19:57:18 +00002865 ssl_session *session = ssl->session_negotiate;
2866 if( !session )
2867 session = ssl->session;
2868
Paul Bakker1ef83d62012-04-11 12:09:53 +00002869 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2870
Paul Bakker48916f92012-09-16 19:57:18 +00002871 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2872 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
2874 /*
2875 * SSLv3:
2876 * hash =
2877 * MD5( master + pad2 +
2878 * MD5( handshake + sender + master + pad1 ) )
2879 * + SHA1( master + pad2 +
2880 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 */
2882
Paul Bakker90995b52013-06-24 19:20:35 +02002883#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002885 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002886#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker90995b52013-06-24 19:20:35 +02002888#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002890 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002891#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Paul Bakker3c2122f2013-06-24 19:03:14 +02002893 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2894 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Paul Bakker1ef83d62012-04-11 12:09:53 +00002896 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakker3c2122f2013-06-24 19:03:14 +02002898 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002899 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002900 md5_update( &md5, padbuf, 48 );
2901 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakker3c2122f2013-06-24 19:03:14 +02002903 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002904 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905 sha1_update( &sha1, padbuf, 40 );
2906 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907
Paul Bakker1ef83d62012-04-11 12:09:53 +00002908 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakker1ef83d62012-04-11 12:09:53 +00002910 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002911 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912 md5_update( &md5, padbuf, 48 );
2913 md5_update( &md5, md5sum, 16 );
2914 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002917 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002918 sha1_update( &sha1, padbuf , 40 );
2919 sha1_update( &sha1, sha1sum, 20 );
2920 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakker1ef83d62012-04-11 12:09:53 +00002922 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002923
Paul Bakker5b4af392014-06-26 12:09:34 +02002924 md5_free( &md5 );
2925 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakker34617722014-06-13 17:20:13 +02002927 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2928 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2929 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002930
2931 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2932}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002933#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002934
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002936static void ssl_calc_finished_tls(
2937 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002938{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002940 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002941 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002943 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Paul Bakker48916f92012-09-16 19:57:18 +00002945 ssl_session *session = ssl->session_negotiate;
2946 if( !session )
2947 session = ssl->session;
2948
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Paul Bakker48916f92012-09-16 19:57:18 +00002951 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2952 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Paul Bakker1ef83d62012-04-11 12:09:53 +00002954 /*
2955 * TLSv1:
2956 * hash = PRF( master, finished_label,
2957 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2958 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker90995b52013-06-24 19:20:35 +02002960#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002961 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2962 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002963#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964
Paul Bakker90995b52013-06-24 19:20:35 +02002965#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2967 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002968#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969
2970 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002971 ? "client finished"
2972 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002973
2974 md5_finish( &md5, padbuf );
2975 sha1_finish( &sha1, padbuf + 16 );
2976
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002977 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002978 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002979
2980 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2981
Paul Bakker5b4af392014-06-26 12:09:34 +02002982 md5_free( &md5 );
2983 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984
Paul Bakker34617722014-06-13 17:20:13 +02002985 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002986
2987 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2988}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002989#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002990
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002991#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2992#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002993static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994 ssl_context *ssl, unsigned char *buf, int from )
2995{
2996 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002997 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002998 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999 unsigned char padbuf[32];
3000
Paul Bakker48916f92012-09-16 19:57:18 +00003001 ssl_session *session = ssl->session_negotiate;
3002 if( !session )
3003 session = ssl->session;
3004
Paul Bakker380da532012-04-18 16:10:25 +00003005 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003006
Paul Bakker9e36f042013-06-30 14:34:05 +02003007 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003008
3009 /*
3010 * TLSv1.2:
3011 * hash = PRF( master, finished_label,
3012 * Hash( handshake ) )[0.11]
3013 */
3014
Paul Bakker9e36f042013-06-30 14:34:05 +02003015#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00003016 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02003017 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003018#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00003019
3020 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003021 ? "client finished"
3022 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00003023
Paul Bakker9e36f042013-06-30 14:34:05 +02003024 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003025
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003026 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003027 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003028
3029 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3030
Paul Bakker5b4af392014-06-26 12:09:34 +02003031 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003032
Paul Bakker34617722014-06-13 17:20:13 +02003033 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034
3035 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3036}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003037#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003038
Paul Bakker9e36f042013-06-30 14:34:05 +02003039#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003040static void ssl_calc_finished_tls_sha384(
3041 ssl_context *ssl, unsigned char *buf, int from )
3042{
3043 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003044 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003045 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003046 unsigned char padbuf[48];
3047
Paul Bakker48916f92012-09-16 19:57:18 +00003048 ssl_session *session = ssl->session_negotiate;
3049 if( !session )
3050 session = ssl->session;
3051
Paul Bakker380da532012-04-18 16:10:25 +00003052 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003053
Paul Bakker9e36f042013-06-30 14:34:05 +02003054 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003055
3056 /*
3057 * TLSv1.2:
3058 * hash = PRF( master, finished_label,
3059 * Hash( handshake ) )[0.11]
3060 */
3061
Paul Bakker9e36f042013-06-30 14:34:05 +02003062#if !defined(POLARSSL_SHA512_ALT)
3063 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3064 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003065#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003066
3067 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003068 ? "client finished"
3069 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003070
Paul Bakker9e36f042013-06-30 14:34:05 +02003071 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003072
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003073 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003074 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003075
3076 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3077
Paul Bakker5b4af392014-06-26 12:09:34 +02003078 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003079
Paul Bakker34617722014-06-13 17:20:13 +02003080 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003081
3082 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3083}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003084#endif /* POLARSSL_SHA512_C */
3085#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003086
Paul Bakker48916f92012-09-16 19:57:18 +00003087void ssl_handshake_wrapup( ssl_context *ssl )
3088{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003089 int resume = ssl->handshake->resume;
3090
Paul Bakker48916f92012-09-16 19:57:18 +00003091 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3092
3093 /*
3094 * Free our handshake params
3095 */
3096 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003097 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003098 ssl->handshake = NULL;
3099
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003100 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003101 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003102 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003103 ssl->renego_records_seen = 0;
3104 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003105
Paul Bakker48916f92012-09-16 19:57:18 +00003106 /*
3107 * Switch in our now active transform context
3108 */
3109 if( ssl->transform )
3110 {
3111 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003112 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003113 }
3114 ssl->transform = ssl->transform_negotiate;
3115 ssl->transform_negotiate = NULL;
3116
Paul Bakker0a597072012-09-25 21:55:46 +00003117 if( ssl->session )
3118 {
3119 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003120 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003121 }
3122 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003123 ssl->session_negotiate = NULL;
3124
Paul Bakker0a597072012-09-25 21:55:46 +00003125 /*
3126 * Add cache entry
3127 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003128 if( ssl->f_set_cache != NULL &&
3129 ssl->session->length != 0 &&
3130 resume == 0 )
3131 {
Paul Bakker0a597072012-09-25 21:55:46 +00003132 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3133 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003134 }
Paul Bakker0a597072012-09-25 21:55:46 +00003135
Paul Bakker48916f92012-09-16 19:57:18 +00003136 ssl->state++;
3137
3138 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3139}
3140
Paul Bakker1ef83d62012-04-11 12:09:53 +00003141int ssl_write_finished( ssl_context *ssl )
3142{
3143 int ret, hash_len;
3144
3145 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3146
Paul Bakker92be97b2013-01-02 17:30:03 +01003147 /*
3148 * Set the out_msg pointer to the correct location based on IV length
3149 */
3150 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3151 {
3152 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3153 ssl->transform_negotiate->fixed_ivlen;
3154 }
3155 else
3156 ssl->out_msg = ssl->out_iv;
3157
Paul Bakker48916f92012-09-16 19:57:18 +00003158 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003159
3160 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3162
Paul Bakker48916f92012-09-16 19:57:18 +00003163 ssl->verify_data_len = hash_len;
3164 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3165
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 ssl->out_msglen = 4 + hash_len;
3167 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3168 ssl->out_msg[0] = SSL_HS_FINISHED;
3169
3170 /*
3171 * In case of session resuming, invert the client and server
3172 * ChangeCipherSpec messages order.
3173 */
Paul Bakker0a597072012-09-25 21:55:46 +00003174 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 {
3176 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003177 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 else
3179 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3180 }
3181 else
3182 ssl->state++;
3183
Paul Bakker48916f92012-09-16 19:57:18 +00003184 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003185 * Switch to our negotiated transform and session parameters for outbound
3186 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003187 */
3188 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3189 ssl->transform_out = ssl->transform_negotiate;
3190 ssl->session_out = ssl->session_negotiate;
3191 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003192
Paul Bakker07eb38b2012-12-19 14:42:06 +01003193#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003194 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003195 {
3196 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3197 {
3198 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3199 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3200 }
3201 }
3202#endif
3203
Paul Bakker5121ce52009-01-03 21:22:43 +00003204 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3205 {
3206 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3207 return( ret );
3208 }
3209
3210 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3211
3212 return( 0 );
3213}
3214
3215int ssl_parse_finished( ssl_context *ssl )
3216{
Paul Bakker23986e52011-04-24 08:57:21 +00003217 int ret;
3218 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003219 unsigned char buf[36];
3220
3221 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224
Paul Bakker48916f92012-09-16 19:57:18 +00003225 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003226 * Switch to our negotiated transform and session parameters for inbound
3227 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003228 */
3229 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3230 ssl->transform_in = ssl->transform_negotiate;
3231 ssl->session_in = ssl->session_negotiate;
3232 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003233
Paul Bakker92be97b2013-01-02 17:30:03 +01003234 /*
3235 * Set the in_msg pointer to the correct location based on IV length
3236 */
3237 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3238 {
3239 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3240 ssl->transform_negotiate->fixed_ivlen;
3241 }
3242 else
3243 ssl->in_msg = ssl->in_iv;
3244
Paul Bakker07eb38b2012-12-19 14:42:06 +01003245#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003246 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003247 {
3248 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3249 {
3250 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3251 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3252 }
3253 }
3254#endif
3255
Paul Bakker5121ce52009-01-03 21:22:43 +00003256 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3257 {
3258 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3259 return( ret );
3260 }
3261
3262 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3263 {
3264 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003265 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003266 }
3267
Paul Bakker1ef83d62012-04-11 12:09:53 +00003268 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003269 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3270
3271 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3272 ssl->in_hslen != 4 + hash_len )
3273 {
3274 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003275 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003278 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 {
3280 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003281 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 }
3283
Paul Bakker48916f92012-09-16 19:57:18 +00003284 ssl->verify_data_len = hash_len;
3285 memcpy( ssl->peer_verify_data, buf, hash_len );
3286
Paul Bakker0a597072012-09-25 21:55:46 +00003287 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003288 {
3289 if( ssl->endpoint == SSL_IS_CLIENT )
3290 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3291
3292 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003293 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294 }
3295 else
3296 ssl->state++;
3297
3298 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3299
3300 return( 0 );
3301}
3302
Paul Bakker968afaa2014-07-09 11:09:24 +02003303static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003304{
3305 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3306
3307#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3308 defined(POLARSSL_SSL_PROTO_TLS1_1)
3309 md5_init( &handshake->fin_md5 );
3310 sha1_init( &handshake->fin_sha1 );
3311 md5_starts( &handshake->fin_md5 );
3312 sha1_starts( &handshake->fin_sha1 );
3313#endif
3314#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3315#if defined(POLARSSL_SHA256_C)
3316 sha256_init( &handshake->fin_sha256 );
3317 sha256_starts( &handshake->fin_sha256, 0 );
3318#endif
3319#if defined(POLARSSL_SHA512_C)
3320 sha512_init( &handshake->fin_sha512 );
3321 sha512_starts( &handshake->fin_sha512, 1 );
3322#endif
3323#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3324
3325 handshake->update_checksum = ssl_update_checksum_start;
3326 handshake->sig_alg = SSL_HASH_SHA1;
3327
3328#if defined(POLARSSL_DHM_C)
3329 dhm_init( &handshake->dhm_ctx );
3330#endif
3331#if defined(POLARSSL_ECDH_C)
3332 ecdh_init( &handshake->ecdh_ctx );
3333#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003334}
3335
3336static void ssl_transform_init( ssl_transform *transform )
3337{
3338 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003339
3340 cipher_init( &transform->cipher_ctx_enc );
3341 cipher_init( &transform->cipher_ctx_dec );
3342
3343 md_init( &transform->md_ctx_enc );
3344 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003345}
3346
3347void ssl_session_init( ssl_session *session )
3348{
3349 memset( session, 0, sizeof(ssl_session) );
3350}
3351
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003352static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003353{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003354 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003355 if( ssl->transform_negotiate )
3356 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003357 if( ssl->session_negotiate )
3358 ssl_session_free( ssl->session_negotiate );
3359 if( ssl->handshake )
3360 ssl_handshake_free( ssl->handshake );
3361
3362 /*
3363 * Either the pointers are now NULL or cleared properly and can be freed.
3364 * Now allocate missing structures.
3365 */
3366 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003367 {
3368 ssl->transform_negotiate =
3369 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3370 }
Paul Bakker48916f92012-09-16 19:57:18 +00003371
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003372 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003373 {
3374 ssl->session_negotiate =
3375 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3376 }
Paul Bakker48916f92012-09-16 19:57:18 +00003377
Paul Bakker82788fb2014-10-20 13:59:19 +02003378 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003379 {
3380 ssl->handshake = (ssl_handshake_params *)
3381 polarssl_malloc( sizeof(ssl_handshake_params) );
3382 }
Paul Bakker48916f92012-09-16 19:57:18 +00003383
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003384 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003385 if( ssl->handshake == NULL ||
3386 ssl->transform_negotiate == NULL ||
3387 ssl->session_negotiate == NULL )
3388 {
3389 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003390
3391 polarssl_free( ssl->handshake );
3392 polarssl_free( ssl->transform_negotiate );
3393 polarssl_free( ssl->session_negotiate );
3394
3395 ssl->handshake = NULL;
3396 ssl->transform_negotiate = NULL;
3397 ssl->session_negotiate = NULL;
3398
Paul Bakker48916f92012-09-16 19:57:18 +00003399 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3400 }
3401
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003402 /* Initialize structures */
3403 ssl_session_init( ssl->session_negotiate );
3404 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003405 ssl_handshake_params_init( ssl->handshake );
3406
3407#if defined(POLARSSL_X509_CRT_PARSE_C)
3408 ssl->handshake->key_cert = ssl->key_cert;
3409#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003410
Paul Bakker48916f92012-09-16 19:57:18 +00003411 return( 0 );
3412}
3413
Paul Bakker5121ce52009-01-03 21:22:43 +00003414/*
3415 * Initialize an SSL context
3416 */
3417int ssl_init( ssl_context *ssl )
3418{
Paul Bakker48916f92012-09-16 19:57:18 +00003419 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003420 int len = SSL_BUFFER_LEN;
3421
3422 memset( ssl, 0, sizeof( ssl_context ) );
3423
Paul Bakker62f2dee2012-09-28 07:31:51 +00003424 /*
3425 * Sane defaults
3426 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003427 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3428 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3429 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3430 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003431
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003432 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003433
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003434 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3435
Paul Bakker62f2dee2012-09-28 07:31:51 +00003436#if defined(POLARSSL_DHM_C)
3437 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3438 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3439 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3440 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3441 {
3442 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3443 return( ret );
3444 }
3445#endif
3446
3447 /*
3448 * Prepare base structures
3449 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003450 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003451 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003452 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 ssl->in_msg = ssl->in_ctr + 13;
3454
3455 if( ssl->in_ctr == NULL )
3456 {
3457 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003458 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003459 }
3460
Paul Bakker6e339b52013-07-03 13:37:05 +02003461 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003463 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003464 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003465
3466 if( ssl->out_ctr == NULL )
3467 {
3468 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003469 polarssl_free( ssl->in_ctr );
3470 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003471 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003472 }
3473
3474 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3475 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3476
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003477#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
3478 ssl->extended_ms = SSL_EXTENDED_MS_ENABLED;
3479#endif
3480
Paul Bakker606b4ba2013-08-14 16:52:14 +02003481#if defined(POLARSSL_SSL_SESSION_TICKETS)
3482 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3483#endif
3484
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003485#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003486 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003487#endif
3488
Paul Bakker48916f92012-09-16 19:57:18 +00003489 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3490 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003491
3492 return( 0 );
3493}
3494
3495/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003496 * Reset an initialized and used SSL context for re-use while retaining
3497 * all application-set variables, function pointers and data.
3498 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003499int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003500{
Paul Bakker48916f92012-09-16 19:57:18 +00003501 int ret;
3502
Paul Bakker7eb013f2011-10-06 12:37:39 +00003503 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003504 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3505 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3506
3507 ssl->verify_data_len = 0;
3508 memset( ssl->own_verify_data, 0, 36 );
3509 memset( ssl->peer_verify_data, 0, 36 );
3510
Paul Bakker7eb013f2011-10-06 12:37:39 +00003511 ssl->in_offt = NULL;
3512
Paul Bakker92be97b2013-01-02 17:30:03 +01003513 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003514 ssl->in_msgtype = 0;
3515 ssl->in_msglen = 0;
3516 ssl->in_left = 0;
3517
3518 ssl->in_hslen = 0;
3519 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003520 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003521
Paul Bakker92be97b2013-01-02 17:30:03 +01003522 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003523 ssl->out_msgtype = 0;
3524 ssl->out_msglen = 0;
3525 ssl->out_left = 0;
3526
Paul Bakker48916f92012-09-16 19:57:18 +00003527 ssl->transform_in = NULL;
3528 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003529
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003530 ssl->renego_records_seen = 0;
3531
Paul Bakker7eb013f2011-10-06 12:37:39 +00003532 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3533 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003534
3535#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003536 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003537 {
3538 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003539 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003540 {
3541 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3542 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3543 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003544 }
3545#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003546
Paul Bakker48916f92012-09-16 19:57:18 +00003547 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003548 {
Paul Bakker48916f92012-09-16 19:57:18 +00003549 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003550 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003551 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003552 }
Paul Bakker48916f92012-09-16 19:57:18 +00003553
Paul Bakkerc0463502013-02-14 11:19:38 +01003554 if( ssl->session )
3555 {
3556 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003557 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003558 ssl->session = NULL;
3559 }
3560
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003561#if defined(POLARSSL_SSL_ALPN)
3562 ssl->alpn_chosen = NULL;
3563#endif
3564
Paul Bakker48916f92012-09-16 19:57:18 +00003565 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3566 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003567
3568 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003569}
3570
Paul Bakkera503a632013-08-14 13:48:06 +02003571#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003572static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3573{
3574 aes_free( &tkeys->enc );
3575 aes_free( &tkeys->dec );
3576
3577 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3578}
3579
Paul Bakker7eb013f2011-10-06 12:37:39 +00003580/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003581 * Allocate and initialize ticket keys
3582 */
3583static int ssl_ticket_keys_init( ssl_context *ssl )
3584{
3585 int ret;
3586 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003587 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003588
3589 if( ssl->ticket_keys != NULL )
3590 return( 0 );
3591
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003592 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3593 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003594 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3595
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003596 aes_init( &tkeys->enc );
3597 aes_init( &tkeys->dec );
3598
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003599 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003600 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003601 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003602 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003603 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003604 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003605
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003606 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3607 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3608 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3609 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003610 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003611 polarssl_free( tkeys );
3612 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003613 }
3614
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003615 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003616 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003617 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003618 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003619 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003620 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003621
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003622 ssl->ticket_keys = tkeys;
3623
3624 return( 0 );
3625}
Paul Bakkera503a632013-08-14 13:48:06 +02003626#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003627
3628/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003629 * SSL set accessors
3630 */
3631void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3632{
3633 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003634
Paul Bakker606b4ba2013-08-14 16:52:14 +02003635#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003636 if( endpoint == SSL_IS_CLIENT )
3637 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003638#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003639}
3640
3641void ssl_set_authmode( ssl_context *ssl, int authmode )
3642{
3643 ssl->authmode = authmode;
3644}
3645
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003646#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003647void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003648 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003649 void *p_vrfy )
3650{
3651 ssl->f_vrfy = f_vrfy;
3652 ssl->p_vrfy = p_vrfy;
3653}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003654#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003655
Paul Bakker5121ce52009-01-03 21:22:43 +00003656void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003657 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003658 void *p_rng )
3659{
3660 ssl->f_rng = f_rng;
3661 ssl->p_rng = p_rng;
3662}
3663
3664void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003665 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 void *p_dbg )
3667{
3668 ssl->f_dbg = f_dbg;
3669 ssl->p_dbg = p_dbg;
3670}
3671
3672void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003673 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003674 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003675{
3676 ssl->f_recv = f_recv;
3677 ssl->f_send = f_send;
3678 ssl->p_recv = p_recv;
3679 ssl->p_send = p_send;
3680}
3681
Paul Bakker0a597072012-09-25 21:55:46 +00003682void ssl_set_session_cache( ssl_context *ssl,
3683 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3684 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685{
Paul Bakker0a597072012-09-25 21:55:46 +00003686 ssl->f_get_cache = f_get_cache;
3687 ssl->p_get_cache = p_get_cache;
3688 ssl->f_set_cache = f_set_cache;
3689 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003690}
3691
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003692int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003694 int ret;
3695
3696 if( ssl == NULL ||
3697 session == NULL ||
3698 ssl->session_negotiate == NULL ||
3699 ssl->endpoint != SSL_IS_CLIENT )
3700 {
3701 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3702 }
3703
3704 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3705 return( ret );
3706
Paul Bakker0a597072012-09-25 21:55:46 +00003707 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003708
3709 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003710}
3711
Paul Bakkerb68cad62012-08-23 08:34:18 +00003712void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003713{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003714 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3715 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3716 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3717 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3718}
3719
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003720void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3721 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003722 int major, int minor )
3723{
3724 if( major != SSL_MAJOR_VERSION_3 )
3725 return;
3726
3727 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3728 return;
3729
3730 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003731}
3732
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003733#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003734/* Add a new (empty) key_cert entry an return a pointer to it */
3735static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3736{
3737 ssl_key_cert *key_cert, *last;
3738
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003739 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3740 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003741 return( NULL );
3742
3743 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3744
3745 /* Append the new key_cert to the (possibly empty) current list */
3746 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003747 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003748 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003749 if( ssl->handshake != NULL )
3750 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003751 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003752 else
3753 {
3754 last = ssl->key_cert;
3755 while( last->next != NULL )
3756 last = last->next;
3757 last->next = key_cert;
3758 }
3759
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003760 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003761}
3762
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003763void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003764 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003765{
3766 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003767 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 ssl->peer_cn = peer_cn;
3769}
3770
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003771int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003772 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003773{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003774 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3775
3776 if( key_cert == NULL )
3777 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3778
3779 key_cert->cert = own_cert;
3780 key_cert->key = pk_key;
3781
3782 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003783}
3784
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003785#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003786int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003787 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003788{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003789 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003790 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003791
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003792 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003793 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3794
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003795 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3796 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003797 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003798
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003799 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003800
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003801 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003802 if( ret != 0 )
3803 return( ret );
3804
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003805 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003806 return( ret );
3807
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003808 key_cert->cert = own_cert;
3809 key_cert->key_own_alloc = 1;
3810
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003811 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003812}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003813#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003814
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003815int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003816 void *rsa_key,
3817 rsa_decrypt_func rsa_decrypt,
3818 rsa_sign_func rsa_sign,
3819 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003820{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003821 int ret;
3822 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003823
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003824 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003825 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3826
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003827 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3828 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003829 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003830
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003831 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003832
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003833 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3834 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3835 return( ret );
3836
3837 key_cert->cert = own_cert;
3838 key_cert->key_own_alloc = 1;
3839
3840 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003841}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003842#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003843
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003844#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003845int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3846 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003847{
Paul Bakker6db455e2013-09-18 17:29:31 +02003848 if( psk == NULL || psk_identity == NULL )
3849 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3850
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003851 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003852 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3853
Paul Bakker6db455e2013-09-18 17:29:31 +02003854 if( ssl->psk != NULL )
3855 {
3856 polarssl_free( ssl->psk );
3857 polarssl_free( ssl->psk_identity );
3858 }
3859
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003860 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003861 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003862
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003863 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003864 ssl->psk_identity = (unsigned char *)
3865 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003866
3867 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3868 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3869
3870 memcpy( ssl->psk, psk, ssl->psk_len );
3871 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003872
3873 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003874}
3875
3876void ssl_set_psk_cb( ssl_context *ssl,
3877 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3878 size_t),
3879 void *p_psk )
3880{
3881 ssl->f_psk = f_psk;
3882 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003883}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003884#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003885
Paul Bakker48916f92012-09-16 19:57:18 +00003886#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003887int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003888{
3889 int ret;
3890
Paul Bakker48916f92012-09-16 19:57:18 +00003891 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003892 {
3893 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3894 return( ret );
3895 }
3896
Paul Bakker48916f92012-09-16 19:57:18 +00003897 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003898 {
3899 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3900 return( ret );
3901 }
3902
3903 return( 0 );
3904}
3905
Paul Bakker1b57b062011-01-06 15:48:19 +00003906int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3907{
3908 int ret;
3909
Paul Bakker66d5d072014-06-17 16:39:18 +02003910 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003911 {
3912 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3913 return( ret );
3914 }
3915
Paul Bakker66d5d072014-06-17 16:39:18 +02003916 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003917 {
3918 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3919 return( ret );
3920 }
3921
3922 return( 0 );
3923}
Paul Bakker48916f92012-09-16 19:57:18 +00003924#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003925
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003926#if defined(POLARSSL_SSL_SET_CURVES)
3927/*
3928 * Set the allowed elliptic curves
3929 */
3930void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3931{
3932 ssl->curve_list = curve_list;
3933}
3934#endif
3935
Paul Bakker0be444a2013-08-27 21:55:01 +02003936#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003937int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003938{
3939 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003941
3942 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003943
3944 if( ssl->hostname_len + 1 == 0 )
3945 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3946
Paul Bakker6e339b52013-07-03 13:37:05 +02003947 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
Paul Bakkerb15b8512012-01-13 13:44:06 +00003949 if( ssl->hostname == NULL )
3950 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3951
Paul Bakker3c2122f2013-06-24 19:03:14 +02003952 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003953 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003954
Paul Bakker40ea7de2009-05-03 10:18:48 +00003955 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
3957 return( 0 );
3958}
3959
Paul Bakker5701cdc2012-09-27 21:49:42 +00003960void ssl_set_sni( ssl_context *ssl,
3961 int (*f_sni)(void *, ssl_context *,
3962 const unsigned char *, size_t),
3963 void *p_sni )
3964{
3965 ssl->f_sni = f_sni;
3966 ssl->p_sni = p_sni;
3967}
Paul Bakker0be444a2013-08-27 21:55:01 +02003968#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003969
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003970#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003971int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003972{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003973 size_t cur_len, tot_len;
3974 const char **p;
3975
3976 /*
3977 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3978 * truncated". Check lengths now rather than later.
3979 */
3980 tot_len = 0;
3981 for( p = protos; *p != NULL; p++ )
3982 {
3983 cur_len = strlen( *p );
3984 tot_len += cur_len;
3985
3986 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3987 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3988 }
3989
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003990 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003991
3992 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003993}
3994
3995const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3996{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003997 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003998}
3999#endif /* POLARSSL_SSL_ALPN */
4000
Paul Bakker490ecc82011-10-06 13:04:09 +00004001void ssl_set_max_version( ssl_context *ssl, int major, int minor )
4002{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004003 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4004 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4005 {
4006 ssl->max_major_ver = major;
4007 ssl->max_minor_ver = minor;
4008 }
Paul Bakker490ecc82011-10-06 13:04:09 +00004009}
4010
Paul Bakker1d29fb52012-09-28 13:28:45 +00004011void ssl_set_min_version( ssl_context *ssl, int major, int minor )
4012{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004013 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
4014 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
4015 {
4016 ssl->min_major_ver = major;
4017 ssl->min_minor_ver = minor;
4018 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00004019}
4020
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02004021#if defined(POLARSSL_SSL_FALLBACK_SCSV) && defined(POLARSSL_SSL_CLI_C)
4022void ssl_set_fallback( ssl_context *ssl, char fallback )
4023{
4024 ssl->fallback = fallback;
4025}
4026#endif
4027
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02004028#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
4029void ssl_set_extended_master_secret( ssl_context *ssl, char ems )
4030{
4031 ssl->extended_ms = ems;
4032}
4033#endif
4034
Paul Bakker05decb22013-08-15 13:33:48 +02004035#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004036int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4037{
Paul Bakker77e257e2013-12-16 15:29:52 +01004038 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004039 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004040 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004041 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004042 }
4043
4044 ssl->mfl_code = mfl_code;
4045
4046 return( 0 );
4047}
Paul Bakker05decb22013-08-15 13:33:48 +02004048#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004049
Paul Bakker1f2bc622013-08-15 13:45:55 +02004050#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004051int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004052{
4053 if( ssl->endpoint != SSL_IS_CLIENT )
4054 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4055
Paul Bakker8c1ede62013-07-19 14:14:37 +02004056 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004057
4058 return( 0 );
4059}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004060#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004061
Paul Bakker48916f92012-09-16 19:57:18 +00004062void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4063{
4064 ssl->disable_renegotiation = renegotiation;
4065}
4066
4067void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4068{
4069 ssl->allow_legacy_renegotiation = allow_legacy;
4070}
4071
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004072void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4073{
4074 ssl->renego_max_records = max_records;
4075}
4076
Paul Bakkera503a632013-08-14 13:48:06 +02004077#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004078int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4079{
4080 ssl->session_tickets = use_tickets;
4081
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004082 if( ssl->endpoint == SSL_IS_CLIENT )
4083 return( 0 );
4084
4085 if( ssl->f_rng == NULL )
4086 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4087
4088 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004089}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004090
4091void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4092{
4093 ssl->ticket_lifetime = lifetime;
4094}
Paul Bakkera503a632013-08-14 13:48:06 +02004095#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004096
Paul Bakker5121ce52009-01-03 21:22:43 +00004097/*
4098 * SSL get accessors
4099 */
Paul Bakker23986e52011-04-24 08:57:21 +00004100size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004101{
4102 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4103}
4104
Paul Bakkerff60ee62010-03-16 21:09:09 +00004105int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004106{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004107 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004108}
4109
Paul Bakkere3166ce2011-01-27 17:40:50 +00004110const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004111{
Paul Bakker926c8e42013-03-06 10:23:34 +01004112 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004113 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004114
Paul Bakkere3166ce2011-01-27 17:40:50 +00004115 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004116}
4117
Paul Bakker43ca69c2011-01-15 17:35:19 +00004118const char *ssl_get_version( const ssl_context *ssl )
4119{
4120 switch( ssl->minor_ver )
4121 {
4122 case SSL_MINOR_VERSION_0:
4123 return( "SSLv3.0" );
4124
4125 case SSL_MINOR_VERSION_1:
4126 return( "TLSv1.0" );
4127
4128 case SSL_MINOR_VERSION_2:
4129 return( "TLSv1.1" );
4130
Paul Bakker1ef83d62012-04-11 12:09:53 +00004131 case SSL_MINOR_VERSION_3:
4132 return( "TLSv1.2" );
4133
Paul Bakker43ca69c2011-01-15 17:35:19 +00004134 default:
4135 break;
4136 }
4137 return( "unknown" );
4138}
4139
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004140#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004141const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004142{
4143 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004144 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004145
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004146 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004147}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004148#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004149
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004150int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4151{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004152 if( ssl == NULL ||
4153 dst == NULL ||
4154 ssl->session == NULL ||
4155 ssl->endpoint != SSL_IS_CLIENT )
4156 {
4157 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4158 }
4159
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004160 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004161}
4162
Paul Bakker5121ce52009-01-03 21:22:43 +00004163/*
Paul Bakker1961b702013-01-25 14:49:24 +01004164 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004165 */
Paul Bakker1961b702013-01-25 14:49:24 +01004166int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004167{
Paul Bakker40e46942009-01-03 21:51:57 +00004168 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004169
Paul Bakker40e46942009-01-03 21:51:57 +00004170#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004171 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004172 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004173#endif
4174
Paul Bakker40e46942009-01-03 21:51:57 +00004175#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004176 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004177 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004178#endif
4179
Paul Bakker1961b702013-01-25 14:49:24 +01004180 return( ret );
4181}
4182
4183/*
4184 * Perform the SSL handshake
4185 */
4186int ssl_handshake( ssl_context *ssl )
4187{
4188 int ret = 0;
4189
4190 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4191
4192 while( ssl->state != SSL_HANDSHAKE_OVER )
4193 {
4194 ret = ssl_handshake_step( ssl );
4195
4196 if( ret != 0 )
4197 break;
4198 }
4199
Paul Bakker5121ce52009-01-03 21:22:43 +00004200 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4201
4202 return( ret );
4203}
4204
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004205#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004206/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004207 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004208 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004209static int ssl_write_hello_request( ssl_context *ssl )
4210{
4211 int ret;
4212
4213 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4214
4215 ssl->out_msglen = 4;
4216 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4217 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4218
4219 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4220 {
4221 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4222 return( ret );
4223 }
4224
4225 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4226
4227 return( 0 );
4228}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004229#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004230
4231/*
4232 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004233 * - any side: calling ssl_renegotiate(),
4234 * - client: receiving a HelloRequest during ssl_read(),
4235 * - server: receiving any handshake message on server during ssl_read() after
4236 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004237 * If the handshake doesn't complete due to waiting for I/O, it will continue
4238 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004239 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004240static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004241{
4242 int ret;
4243
4244 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4245
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004246 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4247 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004248
4249 ssl->state = SSL_HELLO_REQUEST;
4250 ssl->renegotiation = SSL_RENEGOTIATION;
4251
Paul Bakker48916f92012-09-16 19:57:18 +00004252 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4253 {
4254 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4255 return( ret );
4256 }
4257
4258 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4259
4260 return( 0 );
4261}
4262
4263/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004264 * Renegotiate current connection on client,
4265 * or request renegotiation on server
4266 */
4267int ssl_renegotiate( ssl_context *ssl )
4268{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004269 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004270
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004271#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004272 /* On server, just send the request */
4273 if( ssl->endpoint == SSL_IS_SERVER )
4274 {
4275 if( ssl->state != SSL_HANDSHAKE_OVER )
4276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4277
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004278 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4279
4280 /* Did we already try/start sending HelloRequest? */
4281 if( ssl->out_left != 0 )
4282 return( ssl_flush_output( ssl ) );
4283
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004284 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004285 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004286#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004287
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004288#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004289 /*
4290 * On client, either start the renegotiation process or,
4291 * if already in progress, continue the handshake
4292 */
4293 if( ssl->renegotiation != SSL_RENEGOTIATION )
4294 {
4295 if( ssl->state != SSL_HANDSHAKE_OVER )
4296 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4297
4298 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4299 {
4300 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4301 return( ret );
4302 }
4303 }
4304 else
4305 {
4306 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4307 {
4308 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4309 return( ret );
4310 }
4311 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004312#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004313
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004314 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004315}
4316
4317/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004318 * Receive application data decrypted from the SSL layer
4319 */
Paul Bakker23986e52011-04-24 08:57:21 +00004320int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004321{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004322 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004323 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004324
4325 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4326
4327 if( ssl->state != SSL_HANDSHAKE_OVER )
4328 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004329 ret = ssl_handshake( ssl );
4330 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4331 {
4332 record_read = 1;
4333 }
4334 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004335 {
4336 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4337 return( ret );
4338 }
4339 }
4340
4341 if( ssl->in_offt == NULL )
4342 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004343 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004344 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004345 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4346 {
4347 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4348 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004349
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004350 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4351 return( ret );
4352 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004353 }
4354
4355 if( ssl->in_msglen == 0 &&
4356 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4357 {
4358 /*
4359 * OpenSSL sends empty messages to randomize the IV
4360 */
4361 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4362 {
Paul Bakker831a7552011-05-18 13:32:51 +00004363 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4364 return( 0 );
4365
Paul Bakker5121ce52009-01-03 21:22:43 +00004366 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4367 return( ret );
4368 }
4369 }
4370
Paul Bakker48916f92012-09-16 19:57:18 +00004371 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4372 {
4373 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4374
4375 if( ssl->endpoint == SSL_IS_CLIENT &&
4376 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4377 ssl->in_hslen != 4 ) )
4378 {
4379 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4380 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4381 }
4382
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004383 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4384 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004385 ssl->allow_legacy_renegotiation ==
4386 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004387 {
4388 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4389
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004390#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004391 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004392 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004393 /*
4394 * SSLv3 does not have a "no_renegotiation" alert
4395 */
4396 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4397 return( ret );
4398 }
4399 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004400#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004401#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4402 defined(POLARSSL_SSL_PROTO_TLS1_2)
4403 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004404 {
4405 if( ( ret = ssl_send_alert_message( ssl,
4406 SSL_ALERT_LEVEL_WARNING,
4407 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4408 {
4409 return( ret );
4410 }
Paul Bakker48916f92012-09-16 19:57:18 +00004411 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004412 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004413#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4414 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004415 {
4416 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004417 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004418 }
Paul Bakker48916f92012-09-16 19:57:18 +00004419 }
4420 else
4421 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004422 ret = ssl_start_renegotiation( ssl );
4423 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4424 {
4425 record_read = 1;
4426 }
4427 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004428 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004429 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004430 return( ret );
4431 }
Paul Bakker48916f92012-09-16 19:57:18 +00004432 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004433
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004434 /* If a non-handshake record was read during renego, fallthrough,
4435 * else tell the user they should call ssl_read() again */
4436 if( ! record_read )
4437 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004438 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004439 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4440 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004441 ssl->renego_records_seen++;
4442
4443 if( ssl->renego_max_records >= 0 &&
4444 ssl->renego_records_seen > ssl->renego_max_records )
4445 {
4446 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4447 "but not honored by client" ) );
4448 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4449 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004450 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004451
4452 /* Fatal and closure alerts handled by ssl_read_record() */
4453 if( ssl->in_msgtype == SSL_MSG_ALERT )
4454 {
4455 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4456 return( POLARSSL_ERR_NET_WANT_READ );
4457 }
4458
4459 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004460 {
4461 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004462 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004463 }
4464
4465 ssl->in_offt = ssl->in_msg;
4466 }
4467
4468 n = ( len < ssl->in_msglen )
4469 ? len : ssl->in_msglen;
4470
4471 memcpy( buf, ssl->in_offt, n );
4472 ssl->in_msglen -= n;
4473
4474 if( ssl->in_msglen == 0 )
4475 /* all bytes consumed */
4476 ssl->in_offt = NULL;
4477 else
4478 /* more data available */
4479 ssl->in_offt += n;
4480
4481 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4482
Paul Bakker23986e52011-04-24 08:57:21 +00004483 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004484}
4485
4486/*
4487 * Send application data to be encrypted by the SSL layer
4488 */
Paul Bakker23986e52011-04-24 08:57:21 +00004489int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004490{
Paul Bakker23986e52011-04-24 08:57:21 +00004491 int ret;
4492 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004493 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004494
4495 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4496
4497 if( ssl->state != SSL_HANDSHAKE_OVER )
4498 {
4499 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4500 {
4501 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4502 return( ret );
4503 }
4504 }
4505
Paul Bakker05decb22013-08-15 13:33:48 +02004506#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004507 /*
4508 * Assume mfl_code is correct since it was checked when set
4509 */
4510 max_len = mfl_code_to_length[ssl->mfl_code];
4511
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004512 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004513 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004514 */
4515 if( ssl->session_out != NULL &&
4516 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4517 {
4518 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4519 }
Paul Bakker05decb22013-08-15 13:33:48 +02004520#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004521
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004522 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004523
Paul Bakker5121ce52009-01-03 21:22:43 +00004524 if( ssl->out_left != 0 )
4525 {
4526 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4527 {
4528 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4529 return( ret );
4530 }
4531 }
Paul Bakker887bd502011-06-08 13:10:54 +00004532 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004533 {
Paul Bakker887bd502011-06-08 13:10:54 +00004534 ssl->out_msglen = n;
4535 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4536 memcpy( ssl->out_msg, buf, n );
4537
4538 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4539 {
4540 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4541 return( ret );
4542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004543 }
4544
4545 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4546
Paul Bakker23986e52011-04-24 08:57:21 +00004547 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004548}
4549
4550/*
4551 * Notify the peer that the connection is being closed
4552 */
4553int ssl_close_notify( ssl_context *ssl )
4554{
4555 int ret;
4556
4557 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4558
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004559 if( ssl->out_left != 0 )
4560 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004561
4562 if( ssl->state == SSL_HANDSHAKE_OVER )
4563 {
Paul Bakker48916f92012-09-16 19:57:18 +00004564 if( ( ret = ssl_send_alert_message( ssl,
4565 SSL_ALERT_LEVEL_WARNING,
4566 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004567 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004568 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004569 return( ret );
4570 }
4571 }
4572
4573 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4574
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004575 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004576}
4577
Paul Bakker48916f92012-09-16 19:57:18 +00004578void ssl_transform_free( ssl_transform *transform )
4579{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004580 if( transform == NULL )
4581 return;
4582
Paul Bakker48916f92012-09-16 19:57:18 +00004583#if defined(POLARSSL_ZLIB_SUPPORT)
4584 deflateEnd( &transform->ctx_deflate );
4585 inflateEnd( &transform->ctx_inflate );
4586#endif
4587
Paul Bakker84bbeb52014-07-01 14:53:22 +02004588 cipher_free( &transform->cipher_ctx_enc );
4589 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004590
Paul Bakker84bbeb52014-07-01 14:53:22 +02004591 md_free( &transform->md_ctx_enc );
4592 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004593
Paul Bakker34617722014-06-13 17:20:13 +02004594 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004595}
4596
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004597#if defined(POLARSSL_X509_CRT_PARSE_C)
4598static void ssl_key_cert_free( ssl_key_cert *key_cert )
4599{
4600 ssl_key_cert *cur = key_cert, *next;
4601
4602 while( cur != NULL )
4603 {
4604 next = cur->next;
4605
4606 if( cur->key_own_alloc )
4607 {
4608 pk_free( cur->key );
4609 polarssl_free( cur->key );
4610 }
4611 polarssl_free( cur );
4612
4613 cur = next;
4614 }
4615}
4616#endif /* POLARSSL_X509_CRT_PARSE_C */
4617
Paul Bakker48916f92012-09-16 19:57:18 +00004618void ssl_handshake_free( ssl_handshake_params *handshake )
4619{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004620 if( handshake == NULL )
4621 return;
4622
Paul Bakker48916f92012-09-16 19:57:18 +00004623#if defined(POLARSSL_DHM_C)
4624 dhm_free( &handshake->dhm_ctx );
4625#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004626#if defined(POLARSSL_ECDH_C)
4627 ecdh_free( &handshake->ecdh_ctx );
4628#endif
4629
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004630#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004631 /* explicit void pointer cast for buggy MS compiler */
4632 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004633#endif
4634
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004635#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4636 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4637 /*
4638 * Free only the linked list wrapper, not the keys themselves
4639 * since the belong to the SNI callback
4640 */
4641 if( handshake->sni_key_cert != NULL )
4642 {
4643 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4644
4645 while( cur != NULL )
4646 {
4647 next = cur->next;
4648 polarssl_free( cur );
4649 cur = next;
4650 }
4651 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004652#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004653
Paul Bakker34617722014-06-13 17:20:13 +02004654 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004655}
4656
4657void ssl_session_free( ssl_session *session )
4658{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004659 if( session == NULL )
4660 return;
4661
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004662#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004663 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004664 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004665 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004666 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004667 }
Paul Bakkered27a042013-04-18 22:46:23 +02004668#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004669
Paul Bakkera503a632013-08-14 13:48:06 +02004670#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004671 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004672#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004673
Paul Bakker34617722014-06-13 17:20:13 +02004674 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004675}
4676
Paul Bakker5121ce52009-01-03 21:22:43 +00004677/*
4678 * Free an SSL context
4679 */
4680void ssl_free( ssl_context *ssl )
4681{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004682 if( ssl == NULL )
4683 return;
4684
Paul Bakker5121ce52009-01-03 21:22:43 +00004685 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4686
Paul Bakker5121ce52009-01-03 21:22:43 +00004687 if( ssl->out_ctr != NULL )
4688 {
Paul Bakker34617722014-06-13 17:20:13 +02004689 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004690 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 }
4692
4693 if( ssl->in_ctr != NULL )
4694 {
Paul Bakker34617722014-06-13 17:20:13 +02004695 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004696 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004697 }
4698
Paul Bakker16770332013-10-11 09:59:44 +02004699#if defined(POLARSSL_ZLIB_SUPPORT)
4700 if( ssl->compress_buf != NULL )
4701 {
Paul Bakker34617722014-06-13 17:20:13 +02004702 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004703 polarssl_free( ssl->compress_buf );
4704 }
4705#endif
4706
Paul Bakker40e46942009-01-03 21:51:57 +00004707#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004708 mpi_free( &ssl->dhm_P );
4709 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004710#endif
4711
Paul Bakker48916f92012-09-16 19:57:18 +00004712 if( ssl->transform )
4713 {
4714 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004715 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004716 }
4717
4718 if( ssl->handshake )
4719 {
4720 ssl_handshake_free( ssl->handshake );
4721 ssl_transform_free( ssl->transform_negotiate );
4722 ssl_session_free( ssl->session_negotiate );
4723
Paul Bakker6e339b52013-07-03 13:37:05 +02004724 polarssl_free( ssl->handshake );
4725 polarssl_free( ssl->transform_negotiate );
4726 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004727 }
4728
Paul Bakkerc0463502013-02-14 11:19:38 +01004729 if( ssl->session )
4730 {
4731 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004732 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004733 }
4734
Paul Bakkera503a632013-08-14 13:48:06 +02004735#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004736 if( ssl->ticket_keys )
4737 {
4738 ssl_ticket_keys_free( ssl->ticket_keys );
4739 polarssl_free( ssl->ticket_keys );
4740 }
Paul Bakkera503a632013-08-14 13:48:06 +02004741#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004742
Paul Bakker0be444a2013-08-27 21:55:01 +02004743#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004744 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004745 {
Paul Bakker34617722014-06-13 17:20:13 +02004746 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004747 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004748 ssl->hostname_len = 0;
4749 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004750#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004751
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004752#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004753 if( ssl->psk != NULL )
4754 {
Paul Bakker34617722014-06-13 17:20:13 +02004755 polarssl_zeroize( ssl->psk, ssl->psk_len );
4756 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004757 polarssl_free( ssl->psk );
4758 polarssl_free( ssl->psk_identity );
4759 ssl->psk_len = 0;
4760 ssl->psk_identity_len = 0;
4761 }
4762#endif
4763
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004764#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004765 ssl_key_cert_free( ssl->key_cert );
4766#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004767
Paul Bakker05ef8352012-05-08 09:17:57 +00004768#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4769 if( ssl_hw_record_finish != NULL )
4770 {
4771 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4772 ssl_hw_record_finish( ssl );
4773 }
4774#endif
4775
Paul Bakker5121ce52009-01-03 21:22:43 +00004776 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004777
Paul Bakker86f04f42013-02-14 11:20:09 +01004778 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004779 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004780}
4781
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004782#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004783/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004784 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004785 */
4786unsigned char ssl_sig_from_pk( pk_context *pk )
4787{
4788#if defined(POLARSSL_RSA_C)
4789 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4790 return( SSL_SIG_RSA );
4791#endif
4792#if defined(POLARSSL_ECDSA_C)
4793 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4794 return( SSL_SIG_ECDSA );
4795#endif
4796 return( SSL_SIG_ANON );
4797}
4798
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004799pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4800{
4801 switch( sig )
4802 {
4803#if defined(POLARSSL_RSA_C)
4804 case SSL_SIG_RSA:
4805 return( POLARSSL_PK_RSA );
4806#endif
4807#if defined(POLARSSL_ECDSA_C)
4808 case SSL_SIG_ECDSA:
4809 return( POLARSSL_PK_ECDSA );
4810#endif
4811 default:
4812 return( POLARSSL_PK_NONE );
4813 }
4814}
Paul Bakker9af723c2014-05-01 13:03:14 +02004815#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004816
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004817/*
4818 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4819 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004820md_type_t ssl_md_alg_from_hash( unsigned char hash )
4821{
4822 switch( hash )
4823 {
4824#if defined(POLARSSL_MD5_C)
4825 case SSL_HASH_MD5:
4826 return( POLARSSL_MD_MD5 );
4827#endif
4828#if defined(POLARSSL_SHA1_C)
4829 case SSL_HASH_SHA1:
4830 return( POLARSSL_MD_SHA1 );
4831#endif
4832#if defined(POLARSSL_SHA256_C)
4833 case SSL_HASH_SHA224:
4834 return( POLARSSL_MD_SHA224 );
4835 case SSL_HASH_SHA256:
4836 return( POLARSSL_MD_SHA256 );
4837#endif
4838#if defined(POLARSSL_SHA512_C)
4839 case SSL_HASH_SHA384:
4840 return( POLARSSL_MD_SHA384 );
4841 case SSL_HASH_SHA512:
4842 return( POLARSSL_MD_SHA512 );
4843#endif
4844 default:
4845 return( POLARSSL_MD_NONE );
4846 }
4847}
4848
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004849#if defined(POLARSSL_SSL_SET_CURVES)
4850/*
4851 * Check is a curve proposed by the peer is in our list.
4852 * Return 1 if we're willing to use it, 0 otherwise.
4853 */
4854int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4855{
4856 const ecp_group_id *gid;
4857
4858 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4859 if( *gid == grp_id )
4860 return( 1 );
4861
4862 return( 0 );
4863}
Paul Bakker9af723c2014-05-01 13:03:14 +02004864#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004865
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004866#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004867int ssl_check_cert_usage( const x509_crt *cert,
4868 const ssl_ciphersuite_t *ciphersuite,
4869 int cert_endpoint )
4870{
4871#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4872 int usage = 0;
4873#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004874#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4875 const char *ext_oid;
4876 size_t ext_len;
4877#endif
4878
4879#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4880 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4881 ((void) cert);
4882 ((void) cert_endpoint);
4883#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004884
4885#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4886 if( cert_endpoint == SSL_IS_SERVER )
4887 {
4888 /* Server part of the key exchange */
4889 switch( ciphersuite->key_exchange )
4890 {
4891 case POLARSSL_KEY_EXCHANGE_RSA:
4892 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4893 usage = KU_KEY_ENCIPHERMENT;
4894 break;
4895
4896 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4897 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4898 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4899 usage = KU_DIGITAL_SIGNATURE;
4900 break;
4901
4902 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4903 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4904 usage = KU_KEY_AGREEMENT;
4905 break;
4906
4907 /* Don't use default: we want warnings when adding new values */
4908 case POLARSSL_KEY_EXCHANGE_NONE:
4909 case POLARSSL_KEY_EXCHANGE_PSK:
4910 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4911 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4912 usage = 0;
4913 }
4914 }
4915 else
4916 {
4917 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4918 usage = KU_DIGITAL_SIGNATURE;
4919 }
4920
4921 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4922 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004923#else
4924 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004925#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4926
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004927#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4928 if( cert_endpoint == SSL_IS_SERVER )
4929 {
4930 ext_oid = OID_SERVER_AUTH;
4931 ext_len = OID_SIZE( OID_SERVER_AUTH );
4932 }
4933 else
4934 {
4935 ext_oid = OID_CLIENT_AUTH;
4936 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4937 }
4938
4939 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4940 return( -1 );
4941#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4942
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004943 return( 0 );
4944}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004945#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004946
4947#endif /* POLARSSL_SSL_TLS_C */