blob: b853aaee6c31bb984f3e74f917e0d1428143b857 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The SSL 3.0 specification was drafted by Netscape in 1996,
27 * and became an IETF standard in 1999.
28 *
29 * http://wp.netscape.com/eng/ssl3/
30 * http://www.ietf.org/rfc/rfc2246.txt
31 * http://www.ietf.org/rfc/rfc4346.txt
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker40e46942009-01-03 21:51:57 +000040#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakker0be444a2013-08-27 21:55:01 +020042#include "polarssl/debug.h"
43#include "polarssl/ssl.h"
44
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020045#if defined(POLARSSL_X509_CRT_PARSE_C) && \
46 defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
47#include "polarssl/oid.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6edcd412013-10-29 15:22:54 +010059#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
60 !defined(EFI32)
Paul Bakkeraf5c85f2011-04-18 03:47:52 +000061#define strcasecmp _stricmp
62#endif
63
Paul Bakker34617722014-06-13 17:20:13 +020064/* Implementation that should never be optimized out by the compiler */
65static void polarssl_zeroize( void *v, size_t n ) {
66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
67}
68
Paul Bakker05decb22013-08-15 13:33:48 +020069#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020070/*
71 * Convert max_fragment_length codes to length.
72 * RFC 6066 says:
73 * enum{
74 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
75 * } MaxFragmentLength;
76 * and we add 0 -> extension unused
77 */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +020078static unsigned int mfl_code_to_length[SSL_MAX_FRAG_LEN_INVALID] =
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020079{
80 SSL_MAX_CONTENT_LEN, /* SSL_MAX_FRAG_LEN_NONE */
81 512, /* SSL_MAX_FRAG_LEN_512 */
82 1024, /* SSL_MAX_FRAG_LEN_1024 */
83 2048, /* SSL_MAX_FRAG_LEN_2048 */
84 4096, /* SSL_MAX_FRAG_LEN_4096 */
85};
Paul Bakker05decb22013-08-15 13:33:48 +020086#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +020087
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020088static int ssl_session_copy( ssl_session *dst, const ssl_session *src )
89{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020090 ssl_session_free( dst );
91 memcpy( dst, src, sizeof( ssl_session ) );
92
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +020094 if( src->peer_cert != NULL )
95 {
Paul Bakker2292d1f2013-09-15 17:06:49 +020096 int ret;
97
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 dst->peer_cert = (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
99 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200100 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
101
Paul Bakkerb6b09562013-09-18 14:17:41 +0200102 x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200103
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200104 if( ( ret = x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
105 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200106 {
107 polarssl_free( dst->peer_cert );
108 dst->peer_cert = NULL;
109 return( ret );
110 }
111 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200113
Paul Bakkera503a632013-08-14 13:48:06 +0200114#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200115 if( src->ticket != NULL )
116 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200117 dst->ticket = (unsigned char *) polarssl_malloc( src->ticket_len );
118 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200119 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
120
121 memcpy( dst->ticket, src->ticket, src->ticket_len );
122 }
Paul Bakkera503a632013-08-14 13:48:06 +0200123#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200124
125 return( 0 );
126}
127
Paul Bakker05ef8352012-05-08 09:17:57 +0000128#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200129int (*ssl_hw_record_init)( ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200130 const unsigned char *key_enc, const unsigned char *key_dec,
131 size_t keylen,
132 const unsigned char *iv_enc, const unsigned char *iv_dec,
133 size_t ivlen,
134 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200135 size_t maclen ) = NULL;
136int (*ssl_hw_record_activate)( ssl_context *ssl, int direction) = NULL;
137int (*ssl_hw_record_reset)( ssl_context *ssl ) = NULL;
138int (*ssl_hw_record_write)( ssl_context *ssl ) = NULL;
139int (*ssl_hw_record_read)( ssl_context *ssl ) = NULL;
140int (*ssl_hw_record_finish)( ssl_context *ssl ) = NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143/*
144 * Key material generation
145 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200146#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200147static int ssl3_prf( const unsigned char *secret, size_t slen,
148 const char *label,
149 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000150 unsigned char *dstbuf, size_t dlen )
151{
152 size_t i;
153 md5_context md5;
154 sha1_context sha1;
155 unsigned char padding[16];
156 unsigned char sha1sum[20];
157 ((void)label);
158
Paul Bakker5b4af392014-06-26 12:09:34 +0200159 md5_init( &md5 );
160 sha1_init( &sha1 );
161
Paul Bakker5f70b252012-09-13 14:23:06 +0000162 /*
163 * SSLv3:
164 * block =
165 * MD5( secret + SHA1( 'A' + secret + random ) ) +
166 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
167 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
168 * ...
169 */
170 for( i = 0; i < dlen / 16; i++ )
171 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200172 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000173
174 sha1_starts( &sha1 );
175 sha1_update( &sha1, padding, 1 + i );
176 sha1_update( &sha1, secret, slen );
177 sha1_update( &sha1, random, rlen );
178 sha1_finish( &sha1, sha1sum );
179
180 md5_starts( &md5 );
181 md5_update( &md5, secret, slen );
182 md5_update( &md5, sha1sum, 20 );
183 md5_finish( &md5, dstbuf + i * 16 );
184 }
185
Paul Bakker5b4af392014-06-26 12:09:34 +0200186 md5_free( &md5 );
187 sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000188
Paul Bakker34617722014-06-13 17:20:13 +0200189 polarssl_zeroize( padding, sizeof( padding ) );
190 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000191
192 return( 0 );
193}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200194#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000195
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200196#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200197static int tls1_prf( const unsigned char *secret, size_t slen,
198 const char *label,
199 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000200 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
Paul Bakker23986e52011-04-24 08:57:21 +0000202 size_t nb, hs;
203 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200204 const unsigned char *S1, *S2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char tmp[128];
206 unsigned char h_i[20];
207
208 if( sizeof( tmp ) < 20 + strlen( label ) + rlen )
Paul Bakker40e46942009-01-03 21:51:57 +0000209 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
211 hs = ( slen + 1 ) / 2;
212 S1 = secret;
213 S2 = secret + slen - hs;
214
215 nb = strlen( label );
216 memcpy( tmp + 20, label, nb );
217 memcpy( tmp + 20 + nb, random, rlen );
218 nb += rlen;
219
220 /*
221 * First compute P_md5(secret,label+random)[0..dlen]
222 */
223 md5_hmac( S1, hs, tmp + 20, nb, 4 + tmp );
224
225 for( i = 0; i < dlen; i += 16 )
226 {
227 md5_hmac( S1, hs, 4 + tmp, 16 + nb, h_i );
228 md5_hmac( S1, hs, 4 + tmp, 16, 4 + tmp );
229
230 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
231
232 for( j = 0; j < k; j++ )
233 dstbuf[i + j] = h_i[j];
234 }
235
236 /*
237 * XOR out with P_sha1(secret,label+random)[0..dlen]
238 */
239 sha1_hmac( S2, hs, tmp + 20, nb, tmp );
240
241 for( i = 0; i < dlen; i += 20 )
242 {
243 sha1_hmac( S2, hs, tmp, 20 + nb, h_i );
244 sha1_hmac( S2, hs, tmp, 20, tmp );
245
246 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
247
248 for( j = 0; j < k; j++ )
249 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
250 }
251
Paul Bakker34617722014-06-13 17:20:13 +0200252 polarssl_zeroize( tmp, sizeof( tmp ) );
253 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
255 return( 0 );
256}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200257#endif /* POLARSSL_SSL_PROTO_TLS1) || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200259#if defined(POLARSSL_SSL_PROTO_TLS1_2)
260#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200261static int tls_prf_sha256( const unsigned char *secret, size_t slen,
262 const char *label,
263 const unsigned char *random, size_t rlen,
Paul Bakker1ef83d62012-04-11 12:09:53 +0000264 unsigned char *dstbuf, size_t dlen )
265{
266 size_t nb;
267 size_t i, j, k;
268 unsigned char tmp[128];
269 unsigned char h_i[32];
270
271 if( sizeof( tmp ) < 32 + strlen( label ) + rlen )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 nb = strlen( label );
275 memcpy( tmp + 32, label, nb );
276 memcpy( tmp + 32 + nb, random, rlen );
277 nb += rlen;
278
279 /*
280 * Compute P_<hash>(secret, label + random)[0..dlen]
281 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200282 sha256_hmac( secret, slen, tmp + 32, nb, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000283
284 for( i = 0; i < dlen; i += 32 )
285 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200286 sha256_hmac( secret, slen, tmp, 32 + nb, h_i, 0 );
287 sha256_hmac( secret, slen, tmp, 32, tmp, 0 );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000288
289 k = ( i + 32 > dlen ) ? dlen % 32 : 32;
290
291 for( j = 0; j < k; j++ )
292 dstbuf[i + j] = h_i[j];
293 }
294
Paul Bakker34617722014-06-13 17:20:13 +0200295 polarssl_zeroize( tmp, sizeof( tmp ) );
296 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000297
298 return( 0 );
299}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200300#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000301
Paul Bakker9e36f042013-06-30 14:34:05 +0200302#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200303static int tls_prf_sha384( const unsigned char *secret, size_t slen,
304 const char *label,
305 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000306 unsigned char *dstbuf, size_t dlen )
307{
308 size_t nb;
309 size_t i, j, k;
310 unsigned char tmp[128];
311 unsigned char h_i[48];
312
313 if( sizeof( tmp ) < 48 + strlen( label ) + rlen )
314 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
315
316 nb = strlen( label );
317 memcpy( tmp + 48, label, nb );
318 memcpy( tmp + 48 + nb, random, rlen );
319 nb += rlen;
320
321 /*
322 * Compute P_<hash>(secret, label + random)[0..dlen]
323 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200324 sha512_hmac( secret, slen, tmp + 48, nb, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000325
326 for( i = 0; i < dlen; i += 48 )
327 {
Paul Bakker9e36f042013-06-30 14:34:05 +0200328 sha512_hmac( secret, slen, tmp, 48 + nb, h_i, 1 );
329 sha512_hmac( secret, slen, tmp, 48, tmp, 1 );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000330
331 k = ( i + 48 > dlen ) ? dlen % 48 : 48;
332
333 for( j = 0; j < k; j++ )
334 dstbuf[i + j] = h_i[j];
335 }
336
Paul Bakker34617722014-06-13 17:20:13 +0200337 polarssl_zeroize( tmp, sizeof( tmp ) );
338 polarssl_zeroize( h_i, sizeof( h_i ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000339
340 return( 0 );
341}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200342#endif /* POLARSSL_SHA512_C */
343#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000344
Paul Bakker66d5d072014-06-17 16:39:18 +0200345static void ssl_update_checksum_start( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200346
347#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
348 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200349static void ssl_update_checksum_md5sha1( ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200350#endif
Paul Bakker380da532012-04-18 16:10:25 +0000351
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200352#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker66d5d072014-06-17 16:39:18 +0200353static void ssl_calc_verify_ssl( ssl_context *, unsigned char * );
354static void ssl_calc_finished_ssl( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200355#endif
356
357#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +0200358static void ssl_calc_verify_tls( ssl_context *, unsigned char * );
359static void ssl_calc_finished_tls( ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200360#endif
361
362#if defined(POLARSSL_SSL_PROTO_TLS1_2)
363#if defined(POLARSSL_SHA256_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200364static void ssl_update_checksum_sha256( ssl_context *, const unsigned char *, size_t );
365static void ssl_calc_verify_tls_sha256( ssl_context *,unsigned char * );
366static void ssl_calc_finished_tls_sha256( ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200367#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100368
Paul Bakker9e36f042013-06-30 14:34:05 +0200369#if defined(POLARSSL_SHA512_C)
Paul Bakker66d5d072014-06-17 16:39:18 +0200370static void ssl_update_checksum_sha384( ssl_context *, const unsigned char *, size_t );
371static void ssl_calc_verify_tls_sha384( ssl_context *, unsigned char * );
372static void ssl_calc_finished_tls_sha384( ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100373#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200374#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376int ssl_derive_keys( ssl_context *ssl )
377{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200378 int ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 unsigned char keyblk[256];
381 unsigned char *key1;
382 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100383 unsigned char *mac_enc;
384 unsigned char *mac_dec;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200385 size_t iv_copy_len;
Paul Bakker68884e32013-01-07 18:20:04 +0100386 const cipher_info_t *cipher_info;
387 const md_info_t *md_info;
388
Paul Bakker48916f92012-09-16 19:57:18 +0000389 ssl_session *session = ssl->session_negotiate;
390 ssl_transform *transform = ssl->transform_negotiate;
391 ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393 SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
394
Paul Bakker68884e32013-01-07 18:20:04 +0100395 cipher_info = cipher_info_from_type( transform->ciphersuite_info->cipher );
396 if( cipher_info == NULL )
397 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200398 SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100399 transform->ciphersuite_info->cipher ) );
400 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
401 }
402
403 md_info = md_info_from_type( transform->ciphersuite_info->mac );
404 if( md_info == NULL )
405 {
Paul Bakkerf7abd422013-04-16 13:15:56 +0200406 SSL_DEBUG_MSG( 1, ( "md info for %d not found",
Paul Bakker68884e32013-01-07 18:20:04 +0100407 transform->ciphersuite_info->mac ) );
408 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
409 }
410
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000412 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000413 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200414#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +0000415 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000416 {
Paul Bakker48916f92012-09-16 19:57:18 +0000417 handshake->tls_prf = ssl3_prf;
418 handshake->calc_verify = ssl_calc_verify_ssl;
419 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000420 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200421 else
422#endif
423#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
424 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000425 {
Paul Bakker48916f92012-09-16 19:57:18 +0000426 handshake->tls_prf = tls1_prf;
427 handshake->calc_verify = ssl_calc_verify_tls;
428 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200430 else
431#endif
432#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker9e36f042013-06-30 14:34:05 +0200433#if defined(POLARSSL_SHA512_C)
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200434 if( ssl->minor_ver == SSL_MINOR_VERSION_3 &&
435 transform->ciphersuite_info->mac == POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000436 {
Paul Bakker48916f92012-09-16 19:57:18 +0000437 handshake->tls_prf = tls_prf_sha384;
438 handshake->calc_verify = ssl_calc_verify_tls_sha384;
439 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000440 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000441 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200442#endif
443#if defined(POLARSSL_SHA256_C)
444 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000445 {
Paul Bakker48916f92012-09-16 19:57:18 +0000446 handshake->tls_prf = tls_prf_sha256;
447 handshake->calc_verify = ssl_calc_verify_tls_sha256;
448 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000449 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200450 else
451#endif
Paul Bakker9af723c2014-05-01 13:03:14 +0200452#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +0200453 {
454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200456 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000457
458 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * SSLv3:
460 * master =
461 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
462 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
463 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +0200464 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200465 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +0000466 * master = PRF( premaster, "master secret", randbytes )[0..47]
467 */
Paul Bakker0a597072012-09-25 21:55:46 +0000468 if( handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 {
Paul Bakker48916f92012-09-16 19:57:18 +0000470 SSL_DEBUG_BUF( 3, "premaster secret", handshake->premaster,
471 handshake->pmslen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000472
Paul Bakker48916f92012-09-16 19:57:18 +0000473 handshake->tls_prf( handshake->premaster, handshake->pmslen,
474 "master secret",
475 handshake->randbytes, 64, session->master, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
Paul Bakker34617722014-06-13 17:20:13 +0200477 polarssl_zeroize( handshake->premaster, sizeof(handshake->premaster) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478 }
479 else
480 SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
481
482 /*
483 * Swap the client and server random values.
484 */
Paul Bakker48916f92012-09-16 19:57:18 +0000485 memcpy( tmp, handshake->randbytes, 64 );
486 memcpy( handshake->randbytes, tmp + 32, 32 );
487 memcpy( handshake->randbytes + 32, tmp, 32 );
Paul Bakker34617722014-06-13 17:20:13 +0200488 polarssl_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490 /*
491 * SSLv3:
492 * key block =
493 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
494 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
495 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
496 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
497 * ...
498 *
499 * TLSv1:
500 * key block = PRF( master, "key expansion", randbytes )
501 */
Paul Bakker48916f92012-09-16 19:57:18 +0000502 handshake->tls_prf( session->master, 48, "key expansion",
503 handshake->randbytes, 64, keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000504
Paul Bakker48916f92012-09-16 19:57:18 +0000505 SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
506 ssl_get_ciphersuite_name( session->ciphersuite ) ) );
507 SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
508 SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509 SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
510
Paul Bakker34617722014-06-13 17:20:13 +0200511 polarssl_zeroize( handshake->randbytes, sizeof( handshake->randbytes ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 /*
514 * Determine the appropriate key, IV and MAC length.
515 */
Paul Bakker68884e32013-01-07 18:20:04 +0100516
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200517 transform->keylen = cipher_info->key_length / 8;
518
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +0200519 if( cipher_info->mode == POLARSSL_MODE_GCM ||
520 cipher_info->mode == POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200522 transform->maclen = 0;
523
Paul Bakker68884e32013-01-07 18:20:04 +0100524 transform->ivlen = 12;
525 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200526
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200527 /* Minimum length is expicit IV + tag */
528 transform->minlen = transform->ivlen - transform->fixed_ivlen
529 + ( transform->ciphersuite_info->flags &
530 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16 );
Paul Bakker68884e32013-01-07 18:20:04 +0100531 }
532 else
533 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200534 int ret;
535
536 /* Initialize HMAC contexts */
537 if( ( ret = md_init_ctx( &transform->md_ctx_enc, md_info ) ) != 0 ||
538 ( ret = md_init_ctx( &transform->md_ctx_dec, md_info ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +0100539 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200540 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
541 return( ret );
Paul Bakker68884e32013-01-07 18:20:04 +0100542 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +0200544 /* Get MAC length */
545 transform->maclen = md_get_size( md_info );
546
547#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
548 /*
549 * If HMAC is to be truncated, we shall keep the leftmost bytes,
550 * (rfc 6066 page 13 or rfc 2104 section 4),
551 * so we only need to adjust the length here.
552 */
553 if( session->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
554 transform->maclen = SSL_TRUNCATED_HMAC_LEN;
555#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
556
557 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +0100558 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200560 /* Minimum length */
561 if( cipher_info->mode == POLARSSL_MODE_STREAM )
562 transform->minlen = transform->maclen;
563 else
Paul Bakker68884e32013-01-07 18:20:04 +0100564 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200565 /*
566 * GenericBlockCipher:
567 * first multiple of blocklen greater than maclen
568 * + IV except for SSL3 and TLS 1.0
569 */
570 transform->minlen = transform->maclen
571 + cipher_info->block_size
572 - transform->maclen % cipher_info->block_size;
573
574#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
575 if( ssl->minor_ver == SSL_MINOR_VERSION_0 ||
576 ssl->minor_ver == SSL_MINOR_VERSION_1 )
577 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +0100578 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +0200579#endif
580#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
581 if( ssl->minor_ver == SSL_MINOR_VERSION_2 ||
582 ssl->minor_ver == SSL_MINOR_VERSION_3 )
583 {
584 transform->minlen += transform->ivlen;
585 }
586 else
587#endif
588 {
589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
591 }
Paul Bakker68884e32013-01-07 18:20:04 +0100592 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 }
594
595 SSL_DEBUG_MSG( 3, ( "keylen: %d, minlen: %d, ivlen: %d, maclen: %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000596 transform->keylen, transform->minlen, transform->ivlen,
597 transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000598
599 /*
600 * Finally setup the cipher contexts, IVs and MAC secrets.
601 */
602 if( ssl->endpoint == SSL_IS_CLIENT )
603 {
Paul Bakker48916f92012-09-16 19:57:18 +0000604 key1 = keyblk + transform->maclen * 2;
605 key2 = keyblk + transform->maclen * 2 + transform->keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
Paul Bakker68884e32013-01-07 18:20:04 +0100607 mac_enc = keyblk;
608 mac_dec = keyblk + transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000610 /*
611 * This is not used in TLS v1.1.
612 */
Paul Bakker48916f92012-09-16 19:57:18 +0000613 iv_copy_len = ( transform->fixed_ivlen ) ?
614 transform->fixed_ivlen : transform->ivlen;
615 memcpy( transform->iv_enc, key2 + transform->keylen, iv_copy_len );
616 memcpy( transform->iv_dec, key2 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000617 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 }
619 else
620 {
Paul Bakker48916f92012-09-16 19:57:18 +0000621 key1 = keyblk + transform->maclen * 2 + transform->keylen;
622 key2 = keyblk + transform->maclen * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Paul Bakker68884e32013-01-07 18:20:04 +0100624 mac_enc = keyblk + transform->maclen;
625 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Paul Bakker2e11f7d2010-07-25 14:24:53 +0000627 /*
628 * This is not used in TLS v1.1.
629 */
Paul Bakker48916f92012-09-16 19:57:18 +0000630 iv_copy_len = ( transform->fixed_ivlen ) ?
631 transform->fixed_ivlen : transform->ivlen;
632 memcpy( transform->iv_dec, key1 + transform->keylen, iv_copy_len );
633 memcpy( transform->iv_enc, key1 + transform->keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000634 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 }
636
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200637#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker68884e32013-01-07 18:20:04 +0100638 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
639 {
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100640 if( transform->maclen > sizeof transform->mac_enc )
641 {
642 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200643 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +0100644 }
645
Paul Bakker68884e32013-01-07 18:20:04 +0100646 memcpy( transform->mac_enc, mac_enc, transform->maclen );
647 memcpy( transform->mac_dec, mac_dec, transform->maclen );
648 }
649 else
Paul Bakker9af723c2014-05-01 13:03:14 +0200650#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200651#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
652 defined(POLARSSL_SSL_PROTO_TLS1_2)
653 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +0100654 {
655 md_hmac_starts( &transform->md_ctx_enc, mac_enc, transform->maclen );
656 md_hmac_starts( &transform->md_ctx_dec, mac_dec, transform->maclen );
657 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200658 else
659#endif
Paul Bakker577e0062013-08-28 11:57:20 +0200660 {
661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200662 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +0200663 }
Paul Bakker68884e32013-01-07 18:20:04 +0100664
Paul Bakker05ef8352012-05-08 09:17:57 +0000665#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +0200666 if( ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +0000667 {
668 int ret = 0;
669
670 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_init()" ) );
671
Paul Bakker07eb38b2012-12-19 14:42:06 +0100672 if( ( ret = ssl_hw_record_init( ssl, key1, key2, transform->keylen,
673 transform->iv_enc, transform->iv_dec,
674 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +0100675 mac_enc, mac_dec,
Paul Bakker07eb38b2012-12-19 14:42:06 +0100676 transform->maclen ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +0000677 {
678 SSL_DEBUG_RET( 1, "ssl_hw_record_init", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200679 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +0000680 }
681 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200682#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000683
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200684 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_enc,
685 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200687 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
688 return( ret );
689 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200690
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200691 if( ( ret = cipher_init_ctx( &transform->cipher_ctx_dec,
692 cipher_info ) ) != 0 )
693 {
694 SSL_DEBUG_RET( 1, "cipher_init_ctx", ret );
695 return( ret );
696 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200697
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200698 if( ( ret = cipher_setkey( &transform->cipher_ctx_enc, key1,
699 cipher_info->key_length,
700 POLARSSL_ENCRYPT ) ) != 0 )
701 {
702 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
703 return( ret );
704 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +0200705
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200706 if( ( ret = cipher_setkey( &transform->cipher_ctx_dec, key2,
707 cipher_info->key_length,
708 POLARSSL_DECRYPT ) ) != 0 )
709 {
710 SSL_DEBUG_RET( 1, "cipher_setkey", ret );
711 return( ret );
712 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200713
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +0200714#if defined(POLARSSL_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200715 if( cipher_info->mode == POLARSSL_MODE_CBC )
716 {
717 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_enc,
718 POLARSSL_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200719 {
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200720 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
721 return( ret );
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +0200722 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200723
724 if( ( ret = cipher_set_padding_mode( &transform->cipher_ctx_dec,
725 POLARSSL_PADDING_NONE ) ) != 0 )
726 {
727 SSL_DEBUG_RET( 1, "cipher_set_padding_mode", ret );
728 return( ret );
729 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000730 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +0200731#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000732
Paul Bakker34617722014-06-13 17:20:13 +0200733 polarssl_zeroize( keyblk, sizeof( keyblk ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000734
Paul Bakker2770fbd2012-07-03 13:30:23 +0000735#if defined(POLARSSL_ZLIB_SUPPORT)
736 // Initialize compression
737 //
Paul Bakker48916f92012-09-16 19:57:18 +0000738 if( session->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000739 {
Paul Bakker16770332013-10-11 09:59:44 +0200740 if( ssl->compress_buf == NULL )
741 {
742 SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
743 ssl->compress_buf = polarssl_malloc( SSL_BUFFER_LEN );
744 if( ssl->compress_buf == NULL )
745 {
746 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
747 SSL_BUFFER_LEN ) );
748 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
749 }
750 }
751
Paul Bakker2770fbd2012-07-03 13:30:23 +0000752 SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
753
Paul Bakker48916f92012-09-16 19:57:18 +0000754 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
755 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000756
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200757 if( deflateInit( &transform->ctx_deflate,
758 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +0000759 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +0000760 {
761 SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
762 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
763 }
764 }
765#endif /* POLARSSL_ZLIB_SUPPORT */
766
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
768
769 return( 0 );
770}
771
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200772#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker380da532012-04-18 16:10:25 +0000773void ssl_calc_verify_ssl( ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +0000774{
775 md5_context md5;
776 sha1_context sha1;
777 unsigned char pad_1[48];
778 unsigned char pad_2[48];
779
Paul Bakker380da532012-04-18 16:10:25 +0000780 SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakker48916f92012-09-16 19:57:18 +0000782 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
783 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
Paul Bakker380da532012-04-18 16:10:25 +0000785 memset( pad_1, 0x36, 48 );
786 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787
Paul Bakker48916f92012-09-16 19:57:18 +0000788 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000789 md5_update( &md5, pad_1, 48 );
790 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791
Paul Bakker380da532012-04-18 16:10:25 +0000792 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +0000793 md5_update( &md5, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000794 md5_update( &md5, pad_2, 48 );
795 md5_update( &md5, hash, 16 );
796 md5_finish( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
Paul Bakker48916f92012-09-16 19:57:18 +0000798 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000799 sha1_update( &sha1, pad_1, 40 );
800 sha1_finish( &sha1, hash + 16 );
801
802 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +0000803 sha1_update( &sha1, ssl->session_negotiate->master, 48 );
Paul Bakker380da532012-04-18 16:10:25 +0000804 sha1_update( &sha1, pad_2, 40 );
805 sha1_update( &sha1, hash + 16, 20 );
806 sha1_finish( &sha1, hash + 16 );
807
808 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
809 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
810
Paul Bakker5b4af392014-06-26 12:09:34 +0200811 md5_free( &md5 );
812 sha1_free( &sha1 );
813
Paul Bakker380da532012-04-18 16:10:25 +0000814 return;
815}
Paul Bakker9af723c2014-05-01 13:03:14 +0200816#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +0000817
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200818#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +0000819void ssl_calc_verify_tls( ssl_context *ssl, unsigned char hash[36] )
820{
821 md5_context md5;
822 sha1_context sha1;
823
824 SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
825
Paul Bakker48916f92012-09-16 19:57:18 +0000826 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
827 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker380da532012-04-18 16:10:25 +0000828
Paul Bakker48916f92012-09-16 19:57:18 +0000829 md5_finish( &md5, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000830 sha1_finish( &sha1, hash + 16 );
831
832 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
833 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
834
Paul Bakker5b4af392014-06-26 12:09:34 +0200835 md5_free( &md5 );
836 sha1_free( &sha1 );
837
Paul Bakker380da532012-04-18 16:10:25 +0000838 return;
839}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200840#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +0000841
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200842#if defined(POLARSSL_SSL_PROTO_TLS1_2)
843#if defined(POLARSSL_SHA256_C)
Paul Bakker380da532012-04-18 16:10:25 +0000844void ssl_calc_verify_tls_sha256( ssl_context *ssl, unsigned char hash[32] )
845{
Paul Bakker9e36f042013-06-30 14:34:05 +0200846 sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +0000847
848 SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
849
Paul Bakker9e36f042013-06-30 14:34:05 +0200850 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
851 sha256_finish( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +0000852
853 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
854 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
855
Paul Bakker5b4af392014-06-26 12:09:34 +0200856 sha256_free( &sha256 );
857
Paul Bakker380da532012-04-18 16:10:25 +0000858 return;
859}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200860#endif /* POLARSSL_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +0000861
Paul Bakker9e36f042013-06-30 14:34:05 +0200862#if defined(POLARSSL_SHA512_C)
Paul Bakker380da532012-04-18 16:10:25 +0000863void ssl_calc_verify_tls_sha384( ssl_context *ssl, unsigned char hash[48] )
864{
Paul Bakker9e36f042013-06-30 14:34:05 +0200865 sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +0000866
867 SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
868
Paul Bakker9e36f042013-06-30 14:34:05 +0200869 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
870 sha512_finish( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
Paul Bakkerca4ab492012-04-18 14:23:57 +0000872 SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
874
Paul Bakker5b4af392014-06-26 12:09:34 +0200875 sha512_free( &sha512 );
876
Paul Bakker5121ce52009-01-03 21:22:43 +0000877 return;
878}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200879#endif /* POLARSSL_SHA512_C */
880#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200882#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200883int ssl_psk_derive_premaster( ssl_context *ssl, key_exchange_type_t key_ex )
884{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200885 unsigned char *p = ssl->handshake->premaster;
886 unsigned char *end = p + sizeof( ssl->handshake->premaster );
887
888 /*
889 * PMS = struct {
890 * opaque other_secret<0..2^16-1>;
891 * opaque psk<0..2^16-1>;
892 * };
893 * with "other_secret" depending on the particular key exchange
894 */
895#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
896 if( key_ex == POLARSSL_KEY_EXCHANGE_PSK )
897 {
898 if( end - p < 2 + (int) ssl->psk_len )
899 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
900
901 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
902 *(p++) = (unsigned char)( ssl->psk_len );
903 p += ssl->psk_len;
904 }
905 else
906#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +0200907#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
908 if( key_ex == POLARSSL_KEY_EXCHANGE_RSA_PSK )
909 {
910 /*
911 * other_secret already set by the ClientKeyExchange message,
912 * and is 48 bytes long
913 */
914 *p++ = 0;
915 *p++ = 48;
916 p += 48;
917 }
918 else
919#endif /* POLARSSL_KEY_EXCHANGE_RSA_PKS_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200920#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
921 if( key_ex == POLARSSL_KEY_EXCHANGE_DHE_PSK )
922 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200923 int ret;
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +0200924 size_t len = end - ( p + 2 );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200925
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200926 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200927 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200928 p + 2, &len,
929 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200930 {
931 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
932 return( ret );
933 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +0200934 *(p++) = (unsigned char)( len >> 8 );
935 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200936 p += len;
937
938 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
939 }
940 else
941#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
942#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
943 if( key_ex == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
944 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +0200945 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200946 size_t zlen;
947
948 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +0200949 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200950 ssl->f_rng, ssl->p_rng ) ) != 0 )
951 {
952 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
953 return( ret );
954 }
955
956 *(p++) = (unsigned char)( zlen >> 8 );
957 *(p++) = (unsigned char)( zlen );
958 p += zlen;
959
960 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
961 }
962 else
963#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
964 {
965 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +0200966 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200967 }
968
969 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +0100970 if( end - p < 2 + (int) ssl->psk_len )
971 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
972
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200973 *(p++) = (unsigned char)( ssl->psk_len >> 8 );
974 *(p++) = (unsigned char)( ssl->psk_len );
975 memcpy( p, ssl->psk, ssl->psk_len );
976 p += ssl->psk_len;
977
978 ssl->handshake->pmslen = p - ssl->handshake->premaster;
979
980 return( 0 );
981}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +0200982#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +0200983
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200984#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +0000985/*
986 * SSLv3.0 MAC functions
987 */
Paul Bakker68884e32013-01-07 18:20:04 +0100988static void ssl_mac( md_context_t *md_ctx, unsigned char *secret,
989 unsigned char *buf, size_t len,
990 unsigned char *ctr, int type )
Paul Bakker5121ce52009-01-03 21:22:43 +0000991{
992 unsigned char header[11];
993 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +0200994 int padlen;
Paul Bakker68884e32013-01-07 18:20:04 +0100995 int md_size = md_get_size( md_ctx->md_info );
996 int md_type = md_get_type( md_ctx->md_info );
997
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +0200998 /* Only MD5 and SHA-1 supported */
Paul Bakker68884e32013-01-07 18:20:04 +0100999 if( md_type == POLARSSL_MD_MD5 )
1000 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001001 else
Paul Bakker68884e32013-01-07 18:20:04 +01001002 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001003
1004 memcpy( header, ctr, 8 );
1005 header[ 8] = (unsigned char) type;
1006 header[ 9] = (unsigned char)( len >> 8 );
1007 header[10] = (unsigned char)( len );
1008
Paul Bakker68884e32013-01-07 18:20:04 +01001009 memset( padding, 0x36, padlen );
1010 md_starts( md_ctx );
1011 md_update( md_ctx, secret, md_size );
1012 md_update( md_ctx, padding, padlen );
1013 md_update( md_ctx, header, 11 );
1014 md_update( md_ctx, buf, len );
1015 md_finish( md_ctx, buf + len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001016
Paul Bakker68884e32013-01-07 18:20:04 +01001017 memset( padding, 0x5C, padlen );
1018 md_starts( md_ctx );
1019 md_update( md_ctx, secret, md_size );
1020 md_update( md_ctx, padding, padlen );
1021 md_update( md_ctx, buf + len, md_size );
1022 md_finish( md_ctx, buf + len );
Paul Bakker5f70b252012-09-13 14:23:06 +00001023}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001024#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001025
Paul Bakker5121ce52009-01-03 21:22:43 +00001026/*
1027 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001028 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001029static int ssl_encrypt_buf( ssl_context *ssl )
1030{
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001031 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001032 const cipher_mode_t mode = cipher_get_cipher_mode(
1033 &ssl->transform_out->cipher_ctx_enc );
Paul Bakker5121ce52009-01-03 21:22:43 +00001034
1035 SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
1036
1037 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001038 * Add MAC before encrypt, except for AEAD modes
Paul Bakker5121ce52009-01-03 21:22:43 +00001039 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001040#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1041 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1042 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001043 if( mode != POLARSSL_MODE_GCM &&
1044 mode != POLARSSL_MODE_CCM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 {
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001046#if defined(POLARSSL_SSL_PROTO_SSL3)
1047 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1048 {
1049 ssl_mac( &ssl->transform_out->md_ctx_enc,
1050 ssl->transform_out->mac_enc,
1051 ssl->out_msg, ssl->out_msglen,
1052 ssl->out_ctr, ssl->out_msgtype );
1053 }
1054 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001055#endif
1056#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001057 defined(POLARSSL_SSL_PROTO_TLS1_2)
1058 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
1059 {
1060 md_hmac_update( &ssl->transform_out->md_ctx_enc, ssl->out_ctr, 13 );
1061 md_hmac_update( &ssl->transform_out->md_ctx_enc,
1062 ssl->out_msg, ssl->out_msglen );
1063 md_hmac_finish( &ssl->transform_out->md_ctx_enc,
1064 ssl->out_msg + ssl->out_msglen );
1065 md_hmac_reset( &ssl->transform_out->md_ctx_enc );
1066 }
1067 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001068#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001069 {
1070 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001071 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001072 }
1073
1074 SSL_DEBUG_BUF( 4, "computed mac",
1075 ssl->out_msg + ssl->out_msglen,
1076 ssl->transform_out->maclen );
1077
1078 ssl->out_msglen += ssl->transform_out->maclen;
Paul Bakker577e0062013-08-28 11:57:20 +02001079 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001080#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001081
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001082 /*
1083 * Encrypt
1084 */
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001085#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001086 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00001087 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001088 int ret;
1089 size_t olen = 0;
1090
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1092 "including %d bytes of padding",
1093 ssl->out_msglen, 0 ) );
1094
1095 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1096 ssl->out_msg, ssl->out_msglen );
1097
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001098 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001099 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001100 ssl->transform_out->ivlen,
1101 ssl->out_msg, ssl->out_msglen,
1102 ssl->out_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001103 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001104 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001105 return( ret );
1106 }
1107
1108 if( ssl->out_msglen != olen )
1109 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001110 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001111 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001113 }
Paul Bakker68884e32013-01-07 18:20:04 +01001114 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001115#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001116#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1117 if( mode == POLARSSL_MODE_GCM ||
1118 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001119 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001120 int ret;
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001121 size_t enc_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001122 unsigned char *enc_msg;
1123 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001124 unsigned char taglen = ssl->transform_out->ciphersuite_info->flags &
1125 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001126
Paul Bakkerca4ab492012-04-18 14:23:57 +00001127 memcpy( add_data, ssl->out_ctr, 8 );
1128 add_data[8] = ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001129 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1130 ssl->transport, add_data + 9 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001131 add_data[11] = ( ssl->out_msglen >> 8 ) & 0xFF;
1132 add_data[12] = ssl->out_msglen & 0xFF;
1133
1134 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1135 add_data, 13 );
1136
Paul Bakker68884e32013-01-07 18:20:04 +01001137 /*
1138 * Generate IV
1139 */
1140 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001141 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1142 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001143 if( ret != 0 )
1144 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001145
Paul Bakker68884e32013-01-07 18:20:04 +01001146 memcpy( ssl->out_iv,
1147 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1148 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001149
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001150 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001151 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001152
Paul Bakker68884e32013-01-07 18:20:04 +01001153 /*
1154 * Fix pointer positions and message length with added IV
1155 */
1156 enc_msg = ssl->out_msg;
1157 enc_msglen = ssl->out_msglen;
1158 ssl->out_msglen += ssl->transform_out->ivlen -
1159 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001160
Paul Bakker68884e32013-01-07 18:20:04 +01001161 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1162 "including %d bytes of padding",
1163 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001164
Paul Bakker68884e32013-01-07 18:20:04 +01001165 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1166 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001167
Paul Bakker68884e32013-01-07 18:20:04 +01001168 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001169 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001170 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001171 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1172 ssl->transform_out->iv_enc,
1173 ssl->transform_out->ivlen,
1174 add_data, 13,
1175 enc_msg, enc_msglen,
1176 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001177 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001178 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001179 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001180 return( ret );
1181 }
1182
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001183 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001184 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001185 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001186 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001187 }
1188
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001189 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001190
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001191 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001192 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001193 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001194#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001195#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1196 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001197 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001198 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001199 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001200 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001201 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001202
Paul Bakker48916f92012-09-16 19:57:18 +00001203 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1204 ssl->transform_out->ivlen;
1205 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 padlen = 0;
1207
1208 for( i = 0; i <= padlen; i++ )
1209 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1210
1211 ssl->out_msglen += padlen + 1;
1212
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 enc_msglen = ssl->out_msglen;
1214 enc_msg = ssl->out_msg;
1215
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001216#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001217 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001218 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1219 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001220 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001221 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001222 {
1223 /*
1224 * Generate IV
1225 */
Paul Bakker48916f92012-09-16 19:57:18 +00001226 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1227 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001228 if( ret != 0 )
1229 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230
Paul Bakker92be97b2013-01-02 17:30:03 +01001231 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001232 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233
1234 /*
1235 * Fix pointer positions and message length with added IV
1236 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001237 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001238 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001239 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001240 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001241#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001242
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001244 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001245 ssl->out_msglen, ssl->transform_out->ivlen,
1246 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001249 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001251 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001252 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001253 ssl->transform_out->ivlen,
1254 enc_msg, enc_msglen,
1255 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001256 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001257 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001258 return( ret );
1259 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001260
Paul Bakkercca5b812013-08-31 17:40:26 +02001261 if( enc_msglen != olen )
1262 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001263 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001264 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001265 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001266
1267#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001268 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1269 {
1270 /*
1271 * Save IV in SSL3 and TLS1
1272 */
1273 memcpy( ssl->transform_out->iv_enc,
1274 ssl->transform_out->cipher_ctx_enc.iv,
1275 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001277#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001279 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001280#endif /* POLARSSL_CIPHER_MODE_CBC &&
1281 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001282 {
1283 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001284 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Paul Bakkerca4ab492012-04-18 14:23:57 +00001287 for( i = 8; i > 0; i-- )
1288 if( ++ssl->out_ctr[i - 1] != 0 )
1289 break;
1290
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001291 /* The loops goes to its end iff the counter is wrapping */
1292 if( i == 0 )
1293 {
1294 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1295 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1296 }
1297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1299
1300 return( 0 );
1301}
1302
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001303#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001304
Paul Bakker5121ce52009-01-03 21:22:43 +00001305static int ssl_decrypt_buf( ssl_context *ssl )
1306{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001307 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001308 const cipher_mode_t mode = cipher_get_cipher_mode(
1309 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001310#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1311 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1312 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1313 size_t padlen = 0, correct = 1;
1314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
1316 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1317
Paul Bakker48916f92012-09-16 19:57:18 +00001318 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 {
1320 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001321 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001322 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
1324
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001325#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001326 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001327 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001328 int ret;
1329 size_t olen = 0;
1330
Paul Bakker68884e32013-01-07 18:20:04 +01001331 padlen = 0;
1332
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001333 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001334 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001335 ssl->transform_in->ivlen,
1336 ssl->in_msg, ssl->in_msglen,
1337 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001338 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001339 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001340 return( ret );
1341 }
1342
1343 if( ssl->in_msglen != olen )
1344 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001345 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001346 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001347 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 }
Paul Bakker68884e32013-01-07 18:20:04 +01001349 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001350#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001351#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1352 if( mode == POLARSSL_MODE_GCM ||
1353 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001354 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001355 int ret;
1356 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001357 unsigned char *dec_msg;
1358 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001360 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1361 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001362 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1363 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001364
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001365 if( ssl->in_msglen < explicit_iv_len + taglen )
1366 {
1367 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1368 "+ taglen (%d)", ssl->in_msglen,
1369 explicit_iv_len, taglen ) );
1370 return( POLARSSL_ERR_SSL_INVALID_MAC );
1371 }
1372 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1373
Paul Bakker68884e32013-01-07 18:20:04 +01001374 dec_msg = ssl->in_msg;
1375 dec_msg_result = ssl->in_msg;
1376 ssl->in_msglen = dec_msglen;
1377
1378 memcpy( add_data, ssl->in_ctr, 8 );
1379 add_data[8] = ssl->in_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001380 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1381 ssl->transport, add_data + 9 );
Paul Bakker68884e32013-01-07 18:20:04 +01001382 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1383 add_data[12] = ssl->in_msglen & 0xFF;
1384
1385 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1386 add_data, 13 );
1387
1388 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1389 ssl->in_iv,
1390 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1391
1392 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1393 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001394 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001395
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001396 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001397 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001398 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001399 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1400 ssl->transform_in->iv_dec,
1401 ssl->transform_in->ivlen,
1402 add_data, 13,
1403 dec_msg, dec_msglen,
1404 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001405 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001406 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001407 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1408
1409 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1410 return( POLARSSL_ERR_SSL_INVALID_MAC );
1411
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001412 return( ret );
1413 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001414
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001415 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001416 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001417 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001418 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001419 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001420 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001422#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001423#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1424 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001425 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 {
Paul Bakker45829992013-01-03 14:52:21 +01001427 /*
1428 * Decrypt and check the padding
1429 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001430 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001431 unsigned char *dec_msg;
1432 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001433 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001434 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001435 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001436
Paul Bakker5121ce52009-01-03 21:22:43 +00001437 /*
Paul Bakker45829992013-01-03 14:52:21 +01001438 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 */
Paul Bakker48916f92012-09-16 19:57:18 +00001440 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 {
1442 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001443 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001444 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001445 }
1446
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001447#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001448 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1449 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001450#endif
Paul Bakker45829992013-01-03 14:52:21 +01001451
1452 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1453 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1454 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001455 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1456 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1457 ssl->transform_in->ivlen,
1458 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001459 return( POLARSSL_ERR_SSL_INVALID_MAC );
1460 }
1461
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001462 dec_msglen = ssl->in_msglen;
1463 dec_msg = ssl->in_msg;
1464 dec_msg_result = ssl->in_msg;
1465
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001466#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001467 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001468 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001469 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001470 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001471 {
Paul Bakker48916f92012-09-16 19:57:18 +00001472 dec_msglen -= ssl->transform_in->ivlen;
1473 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001474
Paul Bakker48916f92012-09-16 19:57:18 +00001475 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001476 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001477 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001478#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001479
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001480 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001481 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001482 ssl->transform_in->ivlen,
1483 dec_msg, dec_msglen,
1484 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001485 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001486 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001487 return( ret );
1488 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001489
Paul Bakkercca5b812013-08-31 17:40:26 +02001490 if( dec_msglen != olen )
1491 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001492 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001493 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001494 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001495
1496#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001497 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1498 {
1499 /*
1500 * Save IV in SSL3 and TLS1
1501 */
1502 memcpy( ssl->transform_in->iv_dec,
1503 ssl->transform_in->cipher_ctx_dec.iv,
1504 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001505 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
1508 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001509
1510 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1511 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001512#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001513 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1514 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001515#endif
Paul Bakker45829992013-01-03 14:52:21 +01001516 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001517 correct = 0;
1518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001520#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001521 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1522 {
Paul Bakker48916f92012-09-16 19:57:18 +00001523 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001524 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001525#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001526 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1527 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001528 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001529#endif
Paul Bakker45829992013-01-03 14:52:21 +01001530 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 }
1532 }
1533 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001534#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001535#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1536 defined(POLARSSL_SSL_PROTO_TLS1_2)
1537 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001538 {
1539 /*
Paul Bakker45829992013-01-03 14:52:21 +01001540 * TLSv1+: always check the padding up to the first failure
1541 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001542 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001543 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001544 size_t padding_idx = ssl->in_msglen - padlen - 1;
1545
Paul Bakker956c9e02013-12-19 14:42:28 +01001546 /*
1547 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001548 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001549 *
Paul Bakker61885c72014-04-25 12:59:03 +02001550 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1551 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001552 *
1553 * In both cases we reset padding_idx to a safe value (0) to
1554 * prevent out-of-buffer reads.
1555 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001556 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001557 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1558 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001559
1560 padding_idx *= correct;
1561
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001562 for( i = 1; i <= 256; i++ )
1563 {
1564 real_count &= ( i <= padlen );
1565 pad_count += real_count *
1566 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1567 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001568
1569 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001570
Paul Bakkerd66f0702013-01-31 16:57:45 +01001571#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001572 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001573 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001574#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001575 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001576 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001577 else
1578#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1579 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001580 {
1581 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001582 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001583 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001585 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001586#endif /* POLARSSL_CIPHER_MODE_CBC &&
1587 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001588 {
1589 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001590 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001591 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
1593 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1594 ssl->in_msg, ssl->in_msglen );
1595
1596 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001597 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001599#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1600 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1601 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001602 if( mode != POLARSSL_MODE_GCM &&
1603 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001604 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001605 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1606
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001607 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001609 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1610 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001612 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001614#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001615 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1616 {
1617 ssl_mac( &ssl->transform_in->md_ctx_dec,
1618 ssl->transform_in->mac_dec,
1619 ssl->in_msg, ssl->in_msglen,
1620 ssl->in_ctr, ssl->in_msgtype );
1621 }
1622 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001623#endif /* POLARSSL_SSL_PROTO_SSL3 */
1624#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001625 defined(POLARSSL_SSL_PROTO_TLS1_2)
1626 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1627 {
1628 /*
1629 * Process MAC and always update for padlen afterwards to make
1630 * total time independent of padlen
1631 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001632 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001633 *
1634 * Known timing attacks:
1635 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1636 *
1637 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1638 * correctly. (We round down instead of up, so -56 is the correct
1639 * value for our calculations instead of -55)
1640 */
1641 size_t j, extra_run = 0;
1642 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1643 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001644
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001645 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001646
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001647 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1648 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1649 ssl->in_msglen );
1650 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1651 ssl->in_msg + ssl->in_msglen );
1652 for( j = 0; j < extra_run; j++ )
1653 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001654
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001655 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1656 }
1657 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001658#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001659 POLARSSL_SSL_PROTO_TLS1_2 */
1660 {
1661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001662 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001663 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001665 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1666 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1667 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001668
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001669 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001670 ssl->transform_in->maclen ) != 0 )
1671 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001672#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001673 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001674#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001675 correct = 0;
1676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 /*
1679 * Finally check the correct flag
1680 */
1681 if( correct == 0 )
1682 return( POLARSSL_ERR_SSL_INVALID_MAC );
1683 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001684#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
1686 if( ssl->in_msglen == 0 )
1687 {
1688 ssl->nb_zero++;
1689
1690 /*
1691 * Three or more empty messages may be a DoS attack
1692 * (excessive CPU consumption).
1693 */
1694 if( ssl->nb_zero > 3 )
1695 {
1696 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1697 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001698 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 }
1700 }
1701 else
1702 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001703
Paul Bakker23986e52011-04-24 08:57:21 +00001704 for( i = 8; i > 0; i-- )
1705 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 break;
1707
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001708 /* The loops goes to its end iff the counter is wrapping */
1709 if( i == 0 )
1710 {
1711 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1712 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1713 }
1714
Paul Bakker5121ce52009-01-03 21:22:43 +00001715 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1716
1717 return( 0 );
1718}
1719
Paul Bakker2770fbd2012-07-03 13:30:23 +00001720#if defined(POLARSSL_ZLIB_SUPPORT)
1721/*
1722 * Compression/decompression functions
1723 */
1724static int ssl_compress_buf( ssl_context *ssl )
1725{
1726 int ret;
1727 unsigned char *msg_post = ssl->out_msg;
1728 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001729 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001730
1731 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1732
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001733 if( len_pre == 0 )
1734 return( 0 );
1735
Paul Bakker2770fbd2012-07-03 13:30:23 +00001736 memcpy( msg_pre, ssl->out_msg, len_pre );
1737
1738 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1739 ssl->out_msglen ) );
1740
1741 SSL_DEBUG_BUF( 4, "before compression: output payload",
1742 ssl->out_msg, ssl->out_msglen );
1743
Paul Bakker48916f92012-09-16 19:57:18 +00001744 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1745 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1746 ssl->transform_out->ctx_deflate.next_out = msg_post;
1747 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001748
Paul Bakker48916f92012-09-16 19:57:18 +00001749 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001750 if( ret != Z_OK )
1751 {
1752 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1753 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1754 }
1755
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001756 ssl->out_msglen = SSL_BUFFER_LEN -
1757 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001758
Paul Bakker2770fbd2012-07-03 13:30:23 +00001759 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1760 ssl->out_msglen ) );
1761
1762 SSL_DEBUG_BUF( 4, "after compression: output payload",
1763 ssl->out_msg, ssl->out_msglen );
1764
1765 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1766
1767 return( 0 );
1768}
1769
1770static int ssl_decompress_buf( ssl_context *ssl )
1771{
1772 int ret;
1773 unsigned char *msg_post = ssl->in_msg;
1774 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001775 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001776
1777 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1778
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001779 if( len_pre == 0 )
1780 return( 0 );
1781
Paul Bakker2770fbd2012-07-03 13:30:23 +00001782 memcpy( msg_pre, ssl->in_msg, len_pre );
1783
1784 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1785 ssl->in_msglen ) );
1786
1787 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1788 ssl->in_msg, ssl->in_msglen );
1789
Paul Bakker48916f92012-09-16 19:57:18 +00001790 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1791 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1792 ssl->transform_in->ctx_inflate.next_out = msg_post;
1793 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001796 if( ret != Z_OK )
1797 {
1798 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1799 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1800 }
1801
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001802 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1803 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001804
Paul Bakker2770fbd2012-07-03 13:30:23 +00001805 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1806 ssl->in_msglen ) );
1807
1808 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1809 ssl->in_msg, ssl->in_msglen );
1810
1811 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1812
1813 return( 0 );
1814}
1815#endif /* POLARSSL_ZLIB_SUPPORT */
1816
Paul Bakker5121ce52009-01-03 21:22:43 +00001817/*
1818 * Fill the input message buffer
1819 */
Paul Bakker23986e52011-04-24 08:57:21 +00001820int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001821{
Paul Bakker23986e52011-04-24 08:57:21 +00001822 int ret;
1823 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001824
1825 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1826
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001827 if( nb_want > SSL_BUFFER_LEN - 8 )
1828 {
1829 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1830 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1831 }
1832
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 while( ssl->in_left < nb_want )
1834 {
1835 len = nb_want - ssl->in_left;
1836 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1837
1838 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1839 ssl->in_left, nb_want ) );
1840 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1841
Paul Bakker831a7552011-05-18 13:32:51 +00001842 if( ret == 0 )
1843 return( POLARSSL_ERR_SSL_CONN_EOF );
1844
Paul Bakker5121ce52009-01-03 21:22:43 +00001845 if( ret < 0 )
1846 return( ret );
1847
1848 ssl->in_left += ret;
1849 }
1850
1851 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1852
1853 return( 0 );
1854}
1855
1856/*
1857 * Flush any data not yet written
1858 */
1859int ssl_flush_output( ssl_context *ssl )
1860{
1861 int ret;
1862 unsigned char *buf;
1863
1864 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1865
1866 while( ssl->out_left > 0 )
1867 {
1868 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1869 5 + ssl->out_msglen, ssl->out_left ) );
1870
Paul Bakker5bd42292012-12-19 14:40:42 +01001871 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001873
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1875
1876 if( ret <= 0 )
1877 return( ret );
1878
1879 ssl->out_left -= ret;
1880 }
1881
1882 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1883
1884 return( 0 );
1885}
1886
1887/*
1888 * Record layer functions
1889 */
1890int ssl_write_record( ssl_context *ssl )
1891{
Paul Bakker05ef8352012-05-08 09:17:57 +00001892 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001893 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
1895 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1898 {
1899 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1900 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1901 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1902
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001903 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1904 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 }
1906
Paul Bakker2770fbd2012-07-03 13:30:23 +00001907#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001908 if( ssl->transform_out != NULL &&
1909 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001910 {
1911 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1912 {
1913 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1914 return( ret );
1915 }
1916
1917 len = ssl->out_msglen;
1918 }
1919#endif /*POLARSSL_ZLIB_SUPPORT */
1920
Paul Bakker05ef8352012-05-08 09:17:57 +00001921#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001922 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001924 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001925
Paul Bakker05ef8352012-05-08 09:17:57 +00001926 ret = ssl_hw_record_write( ssl );
1927 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1928 {
1929 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001930 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001931 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001932
1933 if( ret == 0 )
1934 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001935 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001936#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001937 if( !done )
1938 {
1939 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001940 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1941 ssl->transport, ssl->out_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1943 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001944
Paul Bakker48916f92012-09-16 19:57:18 +00001945 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001946 {
1947 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1948 {
1949 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1950 return( ret );
1951 }
1952
1953 len = ssl->out_msglen;
1954 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1955 ssl->out_hdr[4] = (unsigned char)( len );
1956 }
1957
1958 ssl->out_left = 5 + ssl->out_msglen;
1959
1960 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1961 "version = [%d:%d], msglen = %d",
1962 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1963 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1964
1965 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001966 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 }
1968
Paul Bakker5121ce52009-01-03 21:22:43 +00001969 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1970 {
1971 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1972 return( ret );
1973 }
1974
1975 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1976
1977 return( 0 );
1978}
1979
1980int ssl_read_record( ssl_context *ssl )
1981{
Paul Bakker05ef8352012-05-08 09:17:57 +00001982 int ret, done = 0;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001983 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001984
1985 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1986
1987 if( ssl->in_hslen != 0 &&
1988 ssl->in_hslen < ssl->in_msglen )
1989 {
1990 /*
1991 * Get next Handshake message in the current record
1992 */
1993 ssl->in_msglen -= ssl->in_hslen;
1994
Paul Bakker8934a982011-08-05 11:11:53 +00001995 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00001996 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
1998 ssl->in_hslen = 4;
1999 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2000
2001 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2002 " %d, type = %d, hslen = %d",
2003 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2004
2005 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2006 {
2007 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002008 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 }
2010
2011 if( ssl->in_msglen < ssl->in_hslen )
2012 {
2013 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002014 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002015 }
2016
Paul Bakker4224bc02014-04-08 14:36:50 +02002017 if( ssl->state != SSL_HANDSHAKE_OVER )
2018 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002019
2020 return( 0 );
2021 }
2022
2023 ssl->in_hslen = 0;
2024
2025 /*
2026 * Read the record header and validate it
2027 */
2028 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2029 {
2030 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2031 return( ret );
2032 }
2033
2034 ssl->in_msgtype = ssl->in_hdr[0];
2035 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2036
2037 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2038 "version = [%d:%d], msglen = %d",
2039 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2040 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2041
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002042 ssl_read_version( &major_ver, &minor_ver, ssl->transport, ssl->in_hdr + 1 );
2043
2044 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002045 {
2046 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002047 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002048 }
2049
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002050 if( minor_ver > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
2052 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002053 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002054 }
2055
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002056 /* Sanity check (outer boundaries) */
2057 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2058 {
2059 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2060 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2061 }
2062
Paul Bakker5121ce52009-01-03 21:22:43 +00002063 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002064 * Make sure the message length is acceptable for the current transform
2065 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002066 */
Paul Bakker48916f92012-09-16 19:57:18 +00002067 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002069 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002070 {
2071 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002072 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 }
2074 }
2075 else
2076 {
Paul Bakker48916f92012-09-16 19:57:18 +00002077 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
2079 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002080 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002081 }
2082
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002083#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002084 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002085 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002086 {
2087 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002088 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002089 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002090#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002091
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002092#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2093 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 /*
2095 * TLS encrypted messages can have up to 256 bytes of padding
2096 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002097 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002098 ssl->in_msglen > ssl->transform_in->minlen +
2099 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 {
2101 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002102 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002103 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002105 }
2106
2107 /*
2108 * Read and optionally decrypt the message contents
2109 */
2110 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2111 {
2112 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2113 return( ret );
2114 }
2115
2116 SSL_DEBUG_BUF( 4, "input record from network",
2117 ssl->in_hdr, 5 + ssl->in_msglen );
2118
Paul Bakker05ef8352012-05-08 09:17:57 +00002119#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002120 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002121 {
2122 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2123
2124 ret = ssl_hw_record_read( ssl );
2125 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2126 {
2127 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002128 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002129 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002130
2131 if( ret == 0 )
2132 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002133 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002134#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002135 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002136 {
2137 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2138 {
Paul Bakker40865c82013-01-31 17:13:13 +01002139#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2140 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2141 {
2142 ssl_send_alert_message( ssl,
2143 SSL_ALERT_LEVEL_FATAL,
2144 SSL_ALERT_MSG_BAD_RECORD_MAC );
2145 }
2146#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002147 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2148 return( ret );
2149 }
2150
2151 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2152 ssl->in_msg, ssl->in_msglen );
2153
2154 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2155 {
2156 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002157 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158 }
2159 }
2160
Paul Bakker2770fbd2012-07-03 13:30:23 +00002161#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002162 if( ssl->transform_in != NULL &&
2163 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002164 {
2165 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2166 {
2167 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2168 return( ret );
2169 }
2170
2171 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2172 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2173 }
2174#endif /* POLARSSL_ZLIB_SUPPORT */
2175
Paul Bakker0a925182012-04-16 06:46:41 +00002176 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2177 ssl->in_msgtype != SSL_MSG_ALERT &&
2178 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2179 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2180 {
2181 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2182
Paul Bakker48916f92012-09-16 19:57:18 +00002183 if( ( ret = ssl_send_alert_message( ssl,
2184 SSL_ALERT_LEVEL_FATAL,
2185 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002186 {
2187 return( ret );
2188 }
2189
2190 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2191 }
2192
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2194 {
2195 ssl->in_hslen = 4;
2196 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2197
2198 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2199 " %d, type = %d, hslen = %d",
2200 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2201
2202 /*
2203 * Additional checks to validate the handshake header
2204 */
2205 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2206 {
2207 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002208 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 }
2210
2211 if( ssl->in_msglen < ssl->in_hslen )
2212 {
2213 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002214 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 }
2216
Paul Bakker48916f92012-09-16 19:57:18 +00002217 if( ssl->state != SSL_HANDSHAKE_OVER )
2218 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220
2221 if( ssl->in_msgtype == SSL_MSG_ALERT )
2222 {
2223 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2224 ssl->in_msg[0], ssl->in_msg[1] ) );
2225
2226 /*
2227 * Ignore non-fatal alerts, except close_notify
2228 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002229 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002230 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002231 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2232 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002233 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002234 }
2235
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002236 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2237 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002238 {
2239 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002240 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002241 }
2242 }
2243
2244 ssl->in_left = 0;
2245
2246 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2247
2248 return( 0 );
2249}
2250
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002251int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2252{
2253 int ret;
2254
2255 if( ( ret = ssl_send_alert_message( ssl,
2256 SSL_ALERT_LEVEL_FATAL,
2257 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2258 {
2259 return( ret );
2260 }
2261
2262 return( 0 );
2263}
2264
Paul Bakker0a925182012-04-16 06:46:41 +00002265int ssl_send_alert_message( ssl_context *ssl,
2266 unsigned char level,
2267 unsigned char message )
2268{
2269 int ret;
2270
2271 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2272
2273 ssl->out_msgtype = SSL_MSG_ALERT;
2274 ssl->out_msglen = 2;
2275 ssl->out_msg[0] = level;
2276 ssl->out_msg[1] = message;
2277
2278 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2279 {
2280 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2281 return( ret );
2282 }
2283
2284 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2285
2286 return( 0 );
2287}
2288
Paul Bakker5121ce52009-01-03 21:22:43 +00002289/*
2290 * Handshake functions
2291 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002292#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2293 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2294 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2295 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2296 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2297 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2298 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002299int ssl_write_certificate( ssl_context *ssl )
2300{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002301 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002302
2303 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2304
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002306 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2307 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002308 {
2309 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2310 ssl->state++;
2311 return( 0 );
2312 }
2313
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002314 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2315 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002316}
2317
2318int ssl_parse_certificate( ssl_context *ssl )
2319{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002320 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2321
2322 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2323
2324 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002325 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2326 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327 {
2328 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2329 ssl->state++;
2330 return( 0 );
2331 }
2332
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002333 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2334 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002335}
2336#else
2337int ssl_write_certificate( ssl_context *ssl )
2338{
2339 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2340 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002341 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2343
2344 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2345
2346 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002347 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2348 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349 {
2350 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2351 ssl->state++;
2352 return( 0 );
2353 }
2354
Paul Bakker5121ce52009-01-03 21:22:43 +00002355 if( ssl->endpoint == SSL_IS_CLIENT )
2356 {
2357 if( ssl->client_auth == 0 )
2358 {
2359 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2360 ssl->state++;
2361 return( 0 );
2362 }
2363
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002364#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 /*
2366 * If using SSLv3 and got no cert, send an Alert message
2367 * (otherwise an empty Certificate message will be sent).
2368 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002369 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2371 {
2372 ssl->out_msglen = 2;
2373 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002374 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2375 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
2377 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2378 goto write_msg;
2379 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002380#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 }
2382 else /* SSL_IS_SERVER */
2383 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002384 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 {
2386 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002387 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 }
2389 }
2390
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002391 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392
2393 /*
2394 * 0 . 0 handshake type
2395 * 1 . 3 handshake length
2396 * 4 . 6 length of all certs
2397 * 7 . 9 length of cert. 1
2398 * 10 . n-1 peer certificate
2399 * n . n+2 length of cert. 2
2400 * n+3 . ... upper level cert, etc.
2401 */
2402 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002403 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002404
Paul Bakker29087132010-03-21 21:03:34 +00002405 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 {
2407 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002408 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002409 {
2410 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2411 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002412 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002413 }
2414
2415 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2416 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2417 ssl->out_msg[i + 2] = (unsigned char)( n );
2418
2419 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2420 i += n; crt = crt->next;
2421 }
2422
2423 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2424 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2425 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2426
2427 ssl->out_msglen = i;
2428 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2429 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2430
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002431#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002432write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
2435 ssl->state++;
2436
2437 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2438 {
2439 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2440 return( ret );
2441 }
2442
2443 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2444
Paul Bakkered27a042013-04-18 22:46:23 +02002445 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002446}
2447
2448int ssl_parse_certificate( ssl_context *ssl )
2449{
Paul Bakkered27a042013-04-18 22:46:23 +02002450 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002451 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002452 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002453
2454 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2455
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002456 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002457 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2458 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002459 {
2460 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2461 ssl->state++;
2462 return( 0 );
2463 }
2464
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002466 ( ssl->authmode == SSL_VERIFY_NONE ||
2467 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002468 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002469 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2471 ssl->state++;
2472 return( 0 );
2473 }
2474
2475 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2476 {
2477 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2478 return( ret );
2479 }
2480
2481 ssl->state++;
2482
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002483#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 /*
2485 * Check if the client sent an empty certificate
2486 */
2487 if( ssl->endpoint == SSL_IS_SERVER &&
2488 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2489 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002490 if( ssl->in_msglen == 2 &&
2491 ssl->in_msgtype == SSL_MSG_ALERT &&
2492 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2493 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 {
2495 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2496
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002497 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2499 return( 0 );
2500 else
Paul Bakker40e46942009-01-03 21:51:57 +00002501 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 }
2503 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002504#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002505
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002506#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2507 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 if( ssl->endpoint == SSL_IS_SERVER &&
2509 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2510 {
2511 if( ssl->in_hslen == 7 &&
2512 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2513 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2514 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2515 {
2516 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2517
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002518 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002520 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 else
2522 return( 0 );
2523 }
2524 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002525#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2526 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2529 {
2530 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002531 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532 }
2533
2534 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2535 {
2536 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002537 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
2539
2540 /*
2541 * Same message structure as in ssl_write_certificate()
2542 */
2543 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2544
2545 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2546 {
2547 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002548 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 }
2550
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002551 /* In case we tried to reuse a session but it failed */
2552 if( ssl->session_negotiate->peer_cert != NULL )
2553 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002554 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002555 polarssl_free( ssl->session_negotiate->peer_cert );
2556 }
2557
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002558 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2559 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 {
2561 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002562 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002563 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564 }
2565
Paul Bakkerb6b09562013-09-18 14:17:41 +02002566 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567
2568 i = 7;
2569
2570 while( i < ssl->in_hslen )
2571 {
2572 if( ssl->in_msg[i] != 0 )
2573 {
2574 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577
2578 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2579 | (unsigned int) ssl->in_msg[i + 2];
2580 i += 3;
2581
2582 if( n < 128 || i + n > ssl->in_hslen )
2583 {
2584 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002585 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 }
2587
Paul Bakkerddf26b42013-09-18 13:46:23 +02002588 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2589 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 if( ret != 0 )
2591 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002592 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 return( ret );
2594 }
2595
2596 i += n;
2597 }
2598
Paul Bakker48916f92012-09-16 19:57:18 +00002599 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002601 /*
2602 * On client, make sure the server cert doesn't change during renego to
2603 * avoid "triple handshake" attack: https://secure-resumption.com/
2604 */
2605 if( ssl->endpoint == SSL_IS_CLIENT &&
2606 ssl->renegotiation == SSL_RENEGOTIATION )
2607 {
2608 if( ssl->session->peer_cert == NULL )
2609 {
2610 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2611 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2612 }
2613
2614 if( ssl->session->peer_cert->raw.len !=
2615 ssl->session_negotiate->peer_cert->raw.len ||
2616 memcmp( ssl->session->peer_cert->raw.p,
2617 ssl->session_negotiate->peer_cert->raw.p,
2618 ssl->session->peer_cert->raw.len ) != 0 )
2619 {
2620 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2621 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2622 }
2623 }
2624
Paul Bakker5121ce52009-01-03 21:22:43 +00002625 if( ssl->authmode != SSL_VERIFY_NONE )
2626 {
2627 if( ssl->ca_chain == NULL )
2628 {
2629 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002630 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002631 }
2632
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002633 /*
2634 * Main check: verify certificate
2635 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002636 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2637 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2638 &ssl->session_negotiate->verify_result,
2639 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002640
2641 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002642 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002644 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002645
2646 /*
2647 * Secondary checks: always done, but change 'ret' only if it was 0
2648 */
2649
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002650#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002651 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002652 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002653
2654 /* If certificate uses an EC key, make sure the curve is OK */
2655 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2656 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2657 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002658 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002659 if( ret == 0 )
2660 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002661 }
2662 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002663#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002665 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2666 ciphersuite_info,
2667 ! ssl->endpoint ) != 0 )
2668 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002669 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002670 if( ret == 0 )
2671 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2672 }
2673
Paul Bakker5121ce52009-01-03 21:22:43 +00002674 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2675 ret = 0;
2676 }
2677
2678 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2679
2680 return( ret );
2681}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002682#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2683 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2684 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2685 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2686 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2687 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2688 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002689
2690int ssl_write_change_cipher_spec( ssl_context *ssl )
2691{
2692 int ret;
2693
2694 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2695
2696 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2697 ssl->out_msglen = 1;
2698 ssl->out_msg[0] = 1;
2699
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 ssl->state++;
2701
2702 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2703 {
2704 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2705 return( ret );
2706 }
2707
2708 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2709
2710 return( 0 );
2711}
2712
2713int ssl_parse_change_cipher_spec( ssl_context *ssl )
2714{
2715 int ret;
2716
2717 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2718
Paul Bakker5121ce52009-01-03 21:22:43 +00002719 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2720 {
2721 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2722 return( ret );
2723 }
2724
2725 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2726 {
2727 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002728 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 }
2730
2731 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2732 {
2733 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002734 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
2737 ssl->state++;
2738
2739 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2740
2741 return( 0 );
2742}
2743
Paul Bakker41c83d32013-03-20 14:39:14 +01002744void ssl_optimize_checksum( ssl_context *ssl,
2745 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002746{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002747 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002748
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002749#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2750 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002751 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002752 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002753 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002754#endif
2755#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2756#if defined(POLARSSL_SHA512_C)
2757 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2758 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2759 else
2760#endif
2761#if defined(POLARSSL_SHA256_C)
2762 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002763 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002764 else
2765#endif
2766#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002767 {
2768 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002770 }
Paul Bakker380da532012-04-18 16:10:25 +00002771}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002772
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002773static void ssl_update_checksum_start( ssl_context *ssl,
2774 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002775{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002776#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2777 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002778 md5_update( &ssl->handshake->fin_md5 , buf, len );
2779 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002780#endif
2781#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2782#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002783 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002784#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002785#if defined(POLARSSL_SHA512_C)
2786 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002787#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002789}
2790
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002791#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2792 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002793static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2794 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002795{
Paul Bakker48916f92012-09-16 19:57:18 +00002796 md5_update( &ssl->handshake->fin_md5 , buf, len );
2797 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002798}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002799#endif
Paul Bakker380da532012-04-18 16:10:25 +00002800
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002801#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2802#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002803static void ssl_update_checksum_sha256( ssl_context *ssl,
2804 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002805{
Paul Bakker9e36f042013-06-30 14:34:05 +02002806 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002807}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002808#endif
Paul Bakker380da532012-04-18 16:10:25 +00002809
Paul Bakker9e36f042013-06-30 14:34:05 +02002810#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002811static void ssl_update_checksum_sha384( ssl_context *ssl,
2812 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002813{
Paul Bakker9e36f042013-06-30 14:34:05 +02002814 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002815}
Paul Bakker769075d2012-11-24 11:26:46 +01002816#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002817#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002818
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002819#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002820static void ssl_calc_finished_ssl(
2821 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002822{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002823 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002824 md5_context md5;
2825 sha1_context sha1;
2826
Paul Bakker5121ce52009-01-03 21:22:43 +00002827 unsigned char padbuf[48];
2828 unsigned char md5sum[16];
2829 unsigned char sha1sum[20];
2830
Paul Bakker48916f92012-09-16 19:57:18 +00002831 ssl_session *session = ssl->session_negotiate;
2832 if( !session )
2833 session = ssl->session;
2834
Paul Bakker1ef83d62012-04-11 12:09:53 +00002835 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2836
Paul Bakker48916f92012-09-16 19:57:18 +00002837 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2838 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
2840 /*
2841 * SSLv3:
2842 * hash =
2843 * MD5( master + pad2 +
2844 * MD5( handshake + sender + master + pad1 ) )
2845 * + SHA1( master + pad2 +
2846 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002847 */
2848
Paul Bakker90995b52013-06-24 19:20:35 +02002849#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002851 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Paul Bakker90995b52013-06-24 19:20:35 +02002854#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002856 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002857#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002858
Paul Bakker3c2122f2013-06-24 19:03:14 +02002859 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2860 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002861
Paul Bakker1ef83d62012-04-11 12:09:53 +00002862 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakker3c2122f2013-06-24 19:03:14 +02002864 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002865 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 md5_update( &md5, padbuf, 48 );
2867 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker3c2122f2013-06-24 19:03:14 +02002869 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002870 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002871 sha1_update( &sha1, padbuf, 40 );
2872 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002875
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002877 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002878 md5_update( &md5, padbuf, 48 );
2879 md5_update( &md5, md5sum, 16 );
2880 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Paul Bakker1ef83d62012-04-11 12:09:53 +00002882 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002883 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 sha1_update( &sha1, padbuf , 40 );
2885 sha1_update( &sha1, sha1sum, 20 );
2886 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002887
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002889
Paul Bakker5b4af392014-06-26 12:09:34 +02002890 md5_free( &md5 );
2891 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002892
Paul Bakker34617722014-06-13 17:20:13 +02002893 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2894 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2895 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002896
2897 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2898}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002899#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002901#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002902static void ssl_calc_finished_tls(
2903 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002904{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002905 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002906 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002907 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002909 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakker48916f92012-09-16 19:57:18 +00002911 ssl_session *session = ssl->session_negotiate;
2912 if( !session )
2913 session = ssl->session;
2914
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakker48916f92012-09-16 19:57:18 +00002917 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2918 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002919
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920 /*
2921 * TLSv1:
2922 * hash = PRF( master, finished_label,
2923 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2924 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002925
Paul Bakker90995b52013-06-24 19:20:35 +02002926#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002927 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2928 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002929#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930
Paul Bakker90995b52013-06-24 19:20:35 +02002931#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002932 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2933 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002934#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002935
2936 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002937 ? "client finished"
2938 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002939
2940 md5_finish( &md5, padbuf );
2941 sha1_finish( &sha1, padbuf + 16 );
2942
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002943 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002944 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
2946 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2947
Paul Bakker5b4af392014-06-26 12:09:34 +02002948 md5_free( &md5 );
2949 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002950
Paul Bakker34617722014-06-13 17:20:13 +02002951 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002952
2953 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2954}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002955#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002956
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002957#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2958#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002959static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960 ssl_context *ssl, unsigned char *buf, int from )
2961{
2962 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002963 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002964 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002965 unsigned char padbuf[32];
2966
Paul Bakker48916f92012-09-16 19:57:18 +00002967 ssl_session *session = ssl->session_negotiate;
2968 if( !session )
2969 session = ssl->session;
2970
Paul Bakker380da532012-04-18 16:10:25 +00002971 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972
Paul Bakker9e36f042013-06-30 14:34:05 +02002973 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002974
2975 /*
2976 * TLSv1.2:
2977 * hash = PRF( master, finished_label,
2978 * Hash( handshake ) )[0.11]
2979 */
2980
Paul Bakker9e36f042013-06-30 14:34:05 +02002981#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002983 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002984#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002985
2986 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002987 ? "client finished"
2988 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002989
Paul Bakker9e36f042013-06-30 14:34:05 +02002990 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002991
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002992 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002993 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002994
2995 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2996
Paul Bakker5b4af392014-06-26 12:09:34 +02002997 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002998
Paul Bakker34617722014-06-13 17:20:13 +02002999 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003000
3001 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3002}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003003#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004
Paul Bakker9e36f042013-06-30 14:34:05 +02003005#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003006static void ssl_calc_finished_tls_sha384(
3007 ssl_context *ssl, unsigned char *buf, int from )
3008{
3009 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003010 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003011 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003012 unsigned char padbuf[48];
3013
Paul Bakker48916f92012-09-16 19:57:18 +00003014 ssl_session *session = ssl->session_negotiate;
3015 if( !session )
3016 session = ssl->session;
3017
Paul Bakker380da532012-04-18 16:10:25 +00003018 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003019
Paul Bakker9e36f042013-06-30 14:34:05 +02003020 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003021
3022 /*
3023 * TLSv1.2:
3024 * hash = PRF( master, finished_label,
3025 * Hash( handshake ) )[0.11]
3026 */
3027
Paul Bakker9e36f042013-06-30 14:34:05 +02003028#if !defined(POLARSSL_SHA512_ALT)
3029 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3030 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003031#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003032
3033 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003034 ? "client finished"
3035 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003036
Paul Bakker9e36f042013-06-30 14:34:05 +02003037 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003038
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003039 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003040 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003041
3042 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3043
Paul Bakker5b4af392014-06-26 12:09:34 +02003044 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003045
Paul Bakker34617722014-06-13 17:20:13 +02003046 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003047
3048 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3049}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050#endif /* POLARSSL_SHA512_C */
3051#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003052
Paul Bakker48916f92012-09-16 19:57:18 +00003053void ssl_handshake_wrapup( ssl_context *ssl )
3054{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003055 int resume = ssl->handshake->resume;
3056
Paul Bakker48916f92012-09-16 19:57:18 +00003057 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3058
3059 /*
3060 * Free our handshake params
3061 */
3062 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003063 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003064 ssl->handshake = NULL;
3065
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003066 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003067 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003068 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003069 ssl->renego_records_seen = 0;
3070 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003071
Paul Bakker48916f92012-09-16 19:57:18 +00003072 /*
3073 * Switch in our now active transform context
3074 */
3075 if( ssl->transform )
3076 {
3077 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003078 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003079 }
3080 ssl->transform = ssl->transform_negotiate;
3081 ssl->transform_negotiate = NULL;
3082
Paul Bakker0a597072012-09-25 21:55:46 +00003083 if( ssl->session )
3084 {
3085 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003086 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003087 }
3088 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003089 ssl->session_negotiate = NULL;
3090
Paul Bakker0a597072012-09-25 21:55:46 +00003091 /*
3092 * Add cache entry
3093 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003094 if( ssl->f_set_cache != NULL &&
3095 ssl->session->length != 0 &&
3096 resume == 0 )
3097 {
Paul Bakker0a597072012-09-25 21:55:46 +00003098 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3099 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003100 }
Paul Bakker0a597072012-09-25 21:55:46 +00003101
Paul Bakker48916f92012-09-16 19:57:18 +00003102 ssl->state++;
3103
3104 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3105}
3106
Paul Bakker1ef83d62012-04-11 12:09:53 +00003107int ssl_write_finished( ssl_context *ssl )
3108{
3109 int ret, hash_len;
3110
3111 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3112
Paul Bakker92be97b2013-01-02 17:30:03 +01003113 /*
3114 * Set the out_msg pointer to the correct location based on IV length
3115 */
3116 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3117 {
3118 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3119 ssl->transform_negotiate->fixed_ivlen;
3120 }
3121 else
3122 ssl->out_msg = ssl->out_iv;
3123
Paul Bakker48916f92012-09-16 19:57:18 +00003124 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125
3126 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003127 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3128
Paul Bakker48916f92012-09-16 19:57:18 +00003129 ssl->verify_data_len = hash_len;
3130 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3131
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 ssl->out_msglen = 4 + hash_len;
3133 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3134 ssl->out_msg[0] = SSL_HS_FINISHED;
3135
3136 /*
3137 * In case of session resuming, invert the client and server
3138 * ChangeCipherSpec messages order.
3139 */
Paul Bakker0a597072012-09-25 21:55:46 +00003140 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003141 {
3142 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003143 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 else
3145 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3146 }
3147 else
3148 ssl->state++;
3149
Paul Bakker48916f92012-09-16 19:57:18 +00003150 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003151 * Switch to our negotiated transform and session parameters for outbound
3152 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003153 */
3154 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3155 ssl->transform_out = ssl->transform_negotiate;
3156 ssl->session_out = ssl->session_negotiate;
3157 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003158
Paul Bakker07eb38b2012-12-19 14:42:06 +01003159#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003160 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003161 {
3162 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3163 {
3164 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3165 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3166 }
3167 }
3168#endif
3169
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3171 {
3172 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3173 return( ret );
3174 }
3175
3176 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3177
3178 return( 0 );
3179}
3180
3181int ssl_parse_finished( ssl_context *ssl )
3182{
Paul Bakker23986e52011-04-24 08:57:21 +00003183 int ret;
3184 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003185 unsigned char buf[36];
3186
3187 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3188
Paul Bakker48916f92012-09-16 19:57:18 +00003189 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003190
Paul Bakker48916f92012-09-16 19:57:18 +00003191 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003192 * Switch to our negotiated transform and session parameters for inbound
3193 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003194 */
3195 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3196 ssl->transform_in = ssl->transform_negotiate;
3197 ssl->session_in = ssl->session_negotiate;
3198 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003199
Paul Bakker92be97b2013-01-02 17:30:03 +01003200 /*
3201 * Set the in_msg pointer to the correct location based on IV length
3202 */
3203 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3204 {
3205 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3206 ssl->transform_negotiate->fixed_ivlen;
3207 }
3208 else
3209 ssl->in_msg = ssl->in_iv;
3210
Paul Bakker07eb38b2012-12-19 14:42:06 +01003211#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003212 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003213 {
3214 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3215 {
3216 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3217 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3218 }
3219 }
3220#endif
3221
Paul Bakker5121ce52009-01-03 21:22:43 +00003222 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3223 {
3224 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3225 return( ret );
3226 }
3227
3228 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3229 {
3230 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003231 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 }
3233
Paul Bakker1ef83d62012-04-11 12:09:53 +00003234 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003235 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3236
3237 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3238 ssl->in_hslen != 4 + hash_len )
3239 {
3240 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003241 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 }
3243
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003244 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 {
3246 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003247 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003248 }
3249
Paul Bakker48916f92012-09-16 19:57:18 +00003250 ssl->verify_data_len = hash_len;
3251 memcpy( ssl->peer_verify_data, buf, hash_len );
3252
Paul Bakker0a597072012-09-25 21:55:46 +00003253 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003254 {
3255 if( ssl->endpoint == SSL_IS_CLIENT )
3256 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3257
3258 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003259 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003260 }
3261 else
3262 ssl->state++;
3263
3264 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3265
3266 return( 0 );
3267}
3268
Paul Bakker968afaa2014-07-09 11:09:24 +02003269static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003270{
3271 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3272
3273#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3274 defined(POLARSSL_SSL_PROTO_TLS1_1)
3275 md5_init( &handshake->fin_md5 );
3276 sha1_init( &handshake->fin_sha1 );
3277 md5_starts( &handshake->fin_md5 );
3278 sha1_starts( &handshake->fin_sha1 );
3279#endif
3280#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3281#if defined(POLARSSL_SHA256_C)
3282 sha256_init( &handshake->fin_sha256 );
3283 sha256_starts( &handshake->fin_sha256, 0 );
3284#endif
3285#if defined(POLARSSL_SHA512_C)
3286 sha512_init( &handshake->fin_sha512 );
3287 sha512_starts( &handshake->fin_sha512, 1 );
3288#endif
3289#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3290
3291 handshake->update_checksum = ssl_update_checksum_start;
3292 handshake->sig_alg = SSL_HASH_SHA1;
3293
3294#if defined(POLARSSL_DHM_C)
3295 dhm_init( &handshake->dhm_ctx );
3296#endif
3297#if defined(POLARSSL_ECDH_C)
3298 ecdh_init( &handshake->ecdh_ctx );
3299#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003300}
3301
3302static void ssl_transform_init( ssl_transform *transform )
3303{
3304 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003305
3306 cipher_init( &transform->cipher_ctx_enc );
3307 cipher_init( &transform->cipher_ctx_dec );
3308
3309 md_init( &transform->md_ctx_enc );
3310 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003311}
3312
3313void ssl_session_init( ssl_session *session )
3314{
3315 memset( session, 0, sizeof(ssl_session) );
3316}
3317
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003318static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003319{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003320 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003321 if( ssl->transform_negotiate )
3322 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003323 if( ssl->session_negotiate )
3324 ssl_session_free( ssl->session_negotiate );
3325 if( ssl->handshake )
3326 ssl_handshake_free( ssl->handshake );
3327
3328 /*
3329 * Either the pointers are now NULL or cleared properly and can be freed.
3330 * Now allocate missing structures.
3331 */
3332 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003333 {
3334 ssl->transform_negotiate =
3335 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3336 }
Paul Bakker48916f92012-09-16 19:57:18 +00003337
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003338 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003339 {
3340 ssl->session_negotiate =
3341 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3342 }
Paul Bakker48916f92012-09-16 19:57:18 +00003343
Paul Bakker82788fb2014-10-20 13:59:19 +02003344 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003345 {
3346 ssl->handshake = (ssl_handshake_params *)
3347 polarssl_malloc( sizeof(ssl_handshake_params) );
3348 }
Paul Bakker48916f92012-09-16 19:57:18 +00003349
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003350 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003351 if( ssl->handshake == NULL ||
3352 ssl->transform_negotiate == NULL ||
3353 ssl->session_negotiate == NULL )
3354 {
3355 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003356
3357 polarssl_free( ssl->handshake );
3358 polarssl_free( ssl->transform_negotiate );
3359 polarssl_free( ssl->session_negotiate );
3360
3361 ssl->handshake = NULL;
3362 ssl->transform_negotiate = NULL;
3363 ssl->session_negotiate = NULL;
3364
Paul Bakker48916f92012-09-16 19:57:18 +00003365 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3366 }
3367
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003368 /* Initialize structures */
3369 ssl_session_init( ssl->session_negotiate );
3370 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003371 ssl_handshake_params_init( ssl->handshake );
3372
3373#if defined(POLARSSL_X509_CRT_PARSE_C)
3374 ssl->handshake->key_cert = ssl->key_cert;
3375#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003376
Paul Bakker48916f92012-09-16 19:57:18 +00003377 return( 0 );
3378}
3379
Paul Bakker5121ce52009-01-03 21:22:43 +00003380/*
3381 * Initialize an SSL context
3382 */
3383int ssl_init( ssl_context *ssl )
3384{
Paul Bakker48916f92012-09-16 19:57:18 +00003385 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 int len = SSL_BUFFER_LEN;
3387
3388 memset( ssl, 0, sizeof( ssl_context ) );
3389
Paul Bakker62f2dee2012-09-28 07:31:51 +00003390 /*
3391 * Sane defaults
3392 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003393 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3394 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3395 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3396 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003397
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003398 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003399
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003400 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3401
Paul Bakker62f2dee2012-09-28 07:31:51 +00003402#if defined(POLARSSL_DHM_C)
3403 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3404 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3405 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3406 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3407 {
3408 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3409 return( ret );
3410 }
3411#endif
3412
3413 /*
3414 * Prepare base structures
3415 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003416 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003417 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003418 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 ssl->in_msg = ssl->in_ctr + 13;
3420
3421 if( ssl->in_ctr == NULL )
3422 {
3423 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003424 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003425 }
3426
Paul Bakker6e339b52013-07-03 13:37:05 +02003427 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003428 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003429 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003430 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003431
3432 if( ssl->out_ctr == NULL )
3433 {
3434 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003435 polarssl_free( ssl->in_ctr );
3436 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003437 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 }
3439
3440 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3441 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3442
Paul Bakker606b4ba2013-08-14 16:52:14 +02003443#if defined(POLARSSL_SSL_SESSION_TICKETS)
3444 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3445#endif
3446
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003447#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003448 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003449#endif
3450
Paul Bakker48916f92012-09-16 19:57:18 +00003451 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3452 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003453
3454 return( 0 );
3455}
3456
3457/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003458 * Reset an initialized and used SSL context for re-use while retaining
3459 * all application-set variables, function pointers and data.
3460 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003462{
Paul Bakker48916f92012-09-16 19:57:18 +00003463 int ret;
3464
Paul Bakker7eb013f2011-10-06 12:37:39 +00003465 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003466 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3467 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3468
3469 ssl->verify_data_len = 0;
3470 memset( ssl->own_verify_data, 0, 36 );
3471 memset( ssl->peer_verify_data, 0, 36 );
3472
Paul Bakker7eb013f2011-10-06 12:37:39 +00003473 ssl->in_offt = NULL;
3474
Paul Bakker92be97b2013-01-02 17:30:03 +01003475 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003476 ssl->in_msgtype = 0;
3477 ssl->in_msglen = 0;
3478 ssl->in_left = 0;
3479
3480 ssl->in_hslen = 0;
3481 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003482 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003483
Paul Bakker92be97b2013-01-02 17:30:03 +01003484 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003485 ssl->out_msgtype = 0;
3486 ssl->out_msglen = 0;
3487 ssl->out_left = 0;
3488
Paul Bakker48916f92012-09-16 19:57:18 +00003489 ssl->transform_in = NULL;
3490 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003491
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003492 ssl->renego_records_seen = 0;
3493
Paul Bakker7eb013f2011-10-06 12:37:39 +00003494 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3495 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003496
3497#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003498 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003499 {
3500 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003501 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003502 {
3503 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3504 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3505 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003506 }
3507#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003508
Paul Bakker48916f92012-09-16 19:57:18 +00003509 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003510 {
Paul Bakker48916f92012-09-16 19:57:18 +00003511 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003512 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003513 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003514 }
Paul Bakker48916f92012-09-16 19:57:18 +00003515
Paul Bakkerc0463502013-02-14 11:19:38 +01003516 if( ssl->session )
3517 {
3518 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003519 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003520 ssl->session = NULL;
3521 }
3522
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003523#if defined(POLARSSL_SSL_ALPN)
3524 ssl->alpn_chosen = NULL;
3525#endif
3526
Paul Bakker48916f92012-09-16 19:57:18 +00003527 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3528 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003529
3530 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003531}
3532
Paul Bakkera503a632013-08-14 13:48:06 +02003533#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003534static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3535{
3536 aes_free( &tkeys->enc );
3537 aes_free( &tkeys->dec );
3538
3539 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3540}
3541
Paul Bakker7eb013f2011-10-06 12:37:39 +00003542/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003543 * Allocate and initialize ticket keys
3544 */
3545static int ssl_ticket_keys_init( ssl_context *ssl )
3546{
3547 int ret;
3548 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003549 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003550
3551 if( ssl->ticket_keys != NULL )
3552 return( 0 );
3553
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003554 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3555 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003556 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3557
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003558 aes_init( &tkeys->enc );
3559 aes_init( &tkeys->dec );
3560
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003561 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003562 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003563 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003564 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003565 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003566 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003567
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003568 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3569 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3570 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3571 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003572 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003573 polarssl_free( tkeys );
3574 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003575 }
3576
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003577 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003578 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003579 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003580 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003581 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003582 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003583
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003584 ssl->ticket_keys = tkeys;
3585
3586 return( 0 );
3587}
Paul Bakkera503a632013-08-14 13:48:06 +02003588#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003589
3590/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003591 * SSL set accessors
3592 */
3593void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3594{
3595 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003596
Paul Bakker606b4ba2013-08-14 16:52:14 +02003597#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003598 if( endpoint == SSL_IS_CLIENT )
3599 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003601}
3602
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003603int ssl_set_transport( ssl_context *ssl, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003604{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003605#if defined(POLARSSL_SSL_PROTO_DTLS)
3606 if( transport == SSL_TRANSPORT_DATAGRAM )
3607 {
3608 ssl->transport = transport;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003609
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003610 /* DTLS starts with TLS1.1 */
3611 if( ssl->min_minor_ver < SSL_MINOR_VERSION_2 )
3612 ssl->min_minor_ver = SSL_MINOR_VERSION_2;
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003613
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003614 if( ssl->max_minor_ver < SSL_MINOR_VERSION_2 )
3615 ssl->max_minor_ver = SSL_MINOR_VERSION_2;
3616
3617 return( 0 );
3618 }
3619#endif
3620
3621 if( transport == SSL_TRANSPORT_STREAM )
3622 {
3623 ssl->transport = transport;
3624 return( 0 );
3625 }
3626
3627 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01003628}
3629
Paul Bakker5121ce52009-01-03 21:22:43 +00003630void ssl_set_authmode( ssl_context *ssl, int authmode )
3631{
3632 ssl->authmode = authmode;
3633}
3634
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003635#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003636void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003637 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003638 void *p_vrfy )
3639{
3640 ssl->f_vrfy = f_vrfy;
3641 ssl->p_vrfy = p_vrfy;
3642}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003643#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003644
Paul Bakker5121ce52009-01-03 21:22:43 +00003645void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003646 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 void *p_rng )
3648{
3649 ssl->f_rng = f_rng;
3650 ssl->p_rng = p_rng;
3651}
3652
3653void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003654 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003655 void *p_dbg )
3656{
3657 ssl->f_dbg = f_dbg;
3658 ssl->p_dbg = p_dbg;
3659}
3660
3661void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003662 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003663 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003664{
3665 ssl->f_recv = f_recv;
3666 ssl->f_send = f_send;
3667 ssl->p_recv = p_recv;
3668 ssl->p_send = p_send;
3669}
3670
Paul Bakker0a597072012-09-25 21:55:46 +00003671void ssl_set_session_cache( ssl_context *ssl,
3672 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3673 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003674{
Paul Bakker0a597072012-09-25 21:55:46 +00003675 ssl->f_get_cache = f_get_cache;
3676 ssl->p_get_cache = p_get_cache;
3677 ssl->f_set_cache = f_set_cache;
3678 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003679}
3680
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003681int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003682{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003683 int ret;
3684
3685 if( ssl == NULL ||
3686 session == NULL ||
3687 ssl->session_negotiate == NULL ||
3688 ssl->endpoint != SSL_IS_CLIENT )
3689 {
3690 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3691 }
3692
3693 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3694 return( ret );
3695
Paul Bakker0a597072012-09-25 21:55:46 +00003696 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003697
3698 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003699}
3700
Paul Bakkerb68cad62012-08-23 08:34:18 +00003701void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003702{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003703 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3704 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3705 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3706 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3707}
3708
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003709void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3710 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003711 int major, int minor )
3712{
3713 if( major != SSL_MAJOR_VERSION_3 )
3714 return;
3715
3716 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3717 return;
3718
3719 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003720}
3721
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003722#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003723/* Add a new (empty) key_cert entry an return a pointer to it */
3724static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3725{
3726 ssl_key_cert *key_cert, *last;
3727
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003728 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3729 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003730 return( NULL );
3731
3732 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3733
3734 /* Append the new key_cert to the (possibly empty) current list */
3735 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003736 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003737 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003738 if( ssl->handshake != NULL )
3739 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003740 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003741 else
3742 {
3743 last = ssl->key_cert;
3744 while( last->next != NULL )
3745 last = last->next;
3746 last->next = key_cert;
3747 }
3748
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003749 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003750}
3751
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003752void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003753 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003754{
3755 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003756 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003757 ssl->peer_cn = peer_cn;
3758}
3759
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003760int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003761 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003762{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003763 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3764
3765 if( key_cert == NULL )
3766 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3767
3768 key_cert->cert = own_cert;
3769 key_cert->key = pk_key;
3770
3771 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772}
3773
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003774#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003775int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003776 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003777{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003778 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003779 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003780
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003781 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003782 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3783
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003784 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3785 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003786 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003787
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003788 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003789
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003790 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003791 if( ret != 0 )
3792 return( ret );
3793
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003794 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003795 return( ret );
3796
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003797 key_cert->cert = own_cert;
3798 key_cert->key_own_alloc = 1;
3799
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003800 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003801}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003802#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003803
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003804int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003805 void *rsa_key,
3806 rsa_decrypt_func rsa_decrypt,
3807 rsa_sign_func rsa_sign,
3808 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003809{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003810 int ret;
3811 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003812
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003813 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003814 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3815
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003816 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3817 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003818 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003819
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003820 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003821
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003822 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3823 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3824 return( ret );
3825
3826 key_cert->cert = own_cert;
3827 key_cert->key_own_alloc = 1;
3828
3829 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003830}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003831#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003832
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003833#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003834int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3835 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003836{
Paul Bakker6db455e2013-09-18 17:29:31 +02003837 if( psk == NULL || psk_identity == NULL )
3838 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3839
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003840 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003841 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3842
Paul Bakker6db455e2013-09-18 17:29:31 +02003843 if( ssl->psk != NULL )
3844 {
3845 polarssl_free( ssl->psk );
3846 polarssl_free( ssl->psk_identity );
3847 }
3848
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003849 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003850 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003851
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003852 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003853 ssl->psk_identity = (unsigned char *)
3854 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003855
3856 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3857 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3858
3859 memcpy( ssl->psk, psk, ssl->psk_len );
3860 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003861
3862 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003863}
3864
3865void ssl_set_psk_cb( ssl_context *ssl,
3866 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3867 size_t),
3868 void *p_psk )
3869{
3870 ssl->f_psk = f_psk;
3871 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003872}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003873#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003874
Paul Bakker48916f92012-09-16 19:57:18 +00003875#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003876int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003877{
3878 int ret;
3879
Paul Bakker48916f92012-09-16 19:57:18 +00003880 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003881 {
3882 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3883 return( ret );
3884 }
3885
Paul Bakker48916f92012-09-16 19:57:18 +00003886 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003887 {
3888 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3889 return( ret );
3890 }
3891
3892 return( 0 );
3893}
3894
Paul Bakker1b57b062011-01-06 15:48:19 +00003895int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3896{
3897 int ret;
3898
Paul Bakker66d5d072014-06-17 16:39:18 +02003899 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003900 {
3901 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3902 return( ret );
3903 }
3904
Paul Bakker66d5d072014-06-17 16:39:18 +02003905 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003906 {
3907 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3908 return( ret );
3909 }
3910
3911 return( 0 );
3912}
Paul Bakker48916f92012-09-16 19:57:18 +00003913#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003914
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003915#if defined(POLARSSL_SSL_SET_CURVES)
3916/*
3917 * Set the allowed elliptic curves
3918 */
3919void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3920{
3921 ssl->curve_list = curve_list;
3922}
3923#endif
3924
Paul Bakker0be444a2013-08-27 21:55:01 +02003925#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003926int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003927{
3928 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003929 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003930
3931 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003932
3933 if( ssl->hostname_len + 1 == 0 )
3934 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3935
Paul Bakker6e339b52013-07-03 13:37:05 +02003936 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Paul Bakkerb15b8512012-01-13 13:44:06 +00003938 if( ssl->hostname == NULL )
3939 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3940
Paul Bakker3c2122f2013-06-24 19:03:14 +02003941 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003942 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003943
Paul Bakker40ea7de2009-05-03 10:18:48 +00003944 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003945
3946 return( 0 );
3947}
3948
Paul Bakker5701cdc2012-09-27 21:49:42 +00003949void ssl_set_sni( ssl_context *ssl,
3950 int (*f_sni)(void *, ssl_context *,
3951 const unsigned char *, size_t),
3952 void *p_sni )
3953{
3954 ssl->f_sni = f_sni;
3955 ssl->p_sni = p_sni;
3956}
Paul Bakker0be444a2013-08-27 21:55:01 +02003957#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003958
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003959#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003960int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003961{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003962 size_t cur_len, tot_len;
3963 const char **p;
3964
3965 /*
3966 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3967 * truncated". Check lengths now rather than later.
3968 */
3969 tot_len = 0;
3970 for( p = protos; *p != NULL; p++ )
3971 {
3972 cur_len = strlen( *p );
3973 tot_len += cur_len;
3974
3975 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3976 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3977 }
3978
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003979 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003980
3981 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003982}
3983
3984const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3985{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003986 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003987}
3988#endif /* POLARSSL_SSL_ALPN */
3989
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003990static int ssl_check_version( const ssl_context *ssl, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00003991{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003992 if( major < SSL_MIN_MAJOR_VERSION || major > SSL_MAX_MAJOR_VERSION ||
3993 minor < SSL_MIN_MINOR_VERSION || minor > SSL_MAX_MINOR_VERSION ||
3994 ( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3995 minor < SSL_MINOR_VERSION_2 ) )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003996 {
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01003997 return( -1 );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003998 }
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003999
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004000 return( 0 );
4001}
4002
4003int ssl_set_max_version( ssl_context *ssl, int major, int minor )
4004{
4005 if( ssl_check_version( ssl, major, minor ) != 0 )
4006 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4007
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004008 ssl->max_major_ver = major;
4009 ssl->max_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004010
4011 return( 0 );
Paul Bakker490ecc82011-10-06 13:04:09 +00004012}
4013
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004014int ssl_set_min_version( ssl_context *ssl, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00004015{
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004016 if( ssl_check_version( ssl, major, minor ) != 0 )
4017 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004018
4019 ssl->min_major_ver = major;
4020 ssl->min_minor_ver = minor;
Manuel Pégourié-Gonnard864a81f2014-02-10 14:25:10 +01004021
4022 return( 0 );
Paul Bakker1d29fb52012-09-28 13:28:45 +00004023}
4024
Paul Bakker05decb22013-08-15 13:33:48 +02004025#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004026int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
4027{
Paul Bakker77e257e2013-12-16 15:29:52 +01004028 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004029 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004030 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004031 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004032 }
4033
4034 ssl->mfl_code = mfl_code;
4035
4036 return( 0 );
4037}
Paul Bakker05decb22013-08-15 13:33:48 +02004038#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004039
Paul Bakker1f2bc622013-08-15 13:45:55 +02004040#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004041int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004042{
4043 if( ssl->endpoint != SSL_IS_CLIENT )
4044 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4045
Paul Bakker8c1ede62013-07-19 14:14:37 +02004046 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004047
4048 return( 0 );
4049}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004050#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004051
Paul Bakker48916f92012-09-16 19:57:18 +00004052void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4053{
4054 ssl->disable_renegotiation = renegotiation;
4055}
4056
4057void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4058{
4059 ssl->allow_legacy_renegotiation = allow_legacy;
4060}
4061
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004062void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4063{
4064 ssl->renego_max_records = max_records;
4065}
4066
Paul Bakkera503a632013-08-14 13:48:06 +02004067#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004068int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4069{
4070 ssl->session_tickets = use_tickets;
4071
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004072 if( ssl->endpoint == SSL_IS_CLIENT )
4073 return( 0 );
4074
4075 if( ssl->f_rng == NULL )
4076 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4077
4078 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004079}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004080
4081void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4082{
4083 ssl->ticket_lifetime = lifetime;
4084}
Paul Bakkera503a632013-08-14 13:48:06 +02004085#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004086
Paul Bakker5121ce52009-01-03 21:22:43 +00004087/*
4088 * SSL get accessors
4089 */
Paul Bakker23986e52011-04-24 08:57:21 +00004090size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004091{
4092 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4093}
4094
Paul Bakkerff60ee62010-03-16 21:09:09 +00004095int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004096{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004097 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004098}
4099
Paul Bakkere3166ce2011-01-27 17:40:50 +00004100const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004101{
Paul Bakker926c8e42013-03-06 10:23:34 +01004102 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004103 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004104
Paul Bakkere3166ce2011-01-27 17:40:50 +00004105 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004106}
4107
Paul Bakker43ca69c2011-01-15 17:35:19 +00004108const char *ssl_get_version( const ssl_context *ssl )
4109{
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004110#if defined(POLARSSL_SSL_PROTO_DTLS)
4111 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
4112 {
4113 switch( ssl->minor_ver )
4114 {
4115 case SSL_MINOR_VERSION_2:
4116 return( "DTLSv1.0" );
4117
4118 case SSL_MINOR_VERSION_3:
4119 return( "DTLSv1.2" );
4120
4121 default:
4122 return( "unknown (DTLS)" );
4123 }
4124 }
4125#endif
4126
Paul Bakker43ca69c2011-01-15 17:35:19 +00004127 switch( ssl->minor_ver )
4128 {
4129 case SSL_MINOR_VERSION_0:
4130 return( "SSLv3.0" );
4131
4132 case SSL_MINOR_VERSION_1:
4133 return( "TLSv1.0" );
4134
4135 case SSL_MINOR_VERSION_2:
4136 return( "TLSv1.1" );
4137
Paul Bakker1ef83d62012-04-11 12:09:53 +00004138 case SSL_MINOR_VERSION_3:
4139 return( "TLSv1.2" );
4140
Paul Bakker43ca69c2011-01-15 17:35:19 +00004141 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01004142 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00004143 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00004144}
4145
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004146#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004147const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004148{
4149 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004150 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004151
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004152 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004153}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004154#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004155
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004156int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4157{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004158 if( ssl == NULL ||
4159 dst == NULL ||
4160 ssl->session == NULL ||
4161 ssl->endpoint != SSL_IS_CLIENT )
4162 {
4163 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4164 }
4165
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004166 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004167}
4168
Paul Bakker5121ce52009-01-03 21:22:43 +00004169/*
Paul Bakker1961b702013-01-25 14:49:24 +01004170 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004171 */
Paul Bakker1961b702013-01-25 14:49:24 +01004172int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004173{
Paul Bakker40e46942009-01-03 21:51:57 +00004174 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004175
Paul Bakker40e46942009-01-03 21:51:57 +00004176#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004177 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004178 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004179#endif
4180
Paul Bakker40e46942009-01-03 21:51:57 +00004181#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004182 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004183 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004184#endif
4185
Paul Bakker1961b702013-01-25 14:49:24 +01004186 return( ret );
4187}
4188
4189/*
4190 * Perform the SSL handshake
4191 */
4192int ssl_handshake( ssl_context *ssl )
4193{
4194 int ret = 0;
4195
4196 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4197
4198 while( ssl->state != SSL_HANDSHAKE_OVER )
4199 {
4200 ret = ssl_handshake_step( ssl );
4201
4202 if( ret != 0 )
4203 break;
4204 }
4205
Paul Bakker5121ce52009-01-03 21:22:43 +00004206 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4207
4208 return( ret );
4209}
4210
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004211#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004212/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004213 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004214 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004215static int ssl_write_hello_request( ssl_context *ssl )
4216{
4217 int ret;
4218
4219 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4220
4221 ssl->out_msglen = 4;
4222 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4223 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4224
4225 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4226 {
4227 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4228 return( ret );
4229 }
4230
4231 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4232
4233 return( 0 );
4234}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004235#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004236
4237/*
4238 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004239 * - any side: calling ssl_renegotiate(),
4240 * - client: receiving a HelloRequest during ssl_read(),
4241 * - server: receiving any handshake message on server during ssl_read() after
4242 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004243 * If the handshake doesn't complete due to waiting for I/O, it will continue
4244 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004245 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004246static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004247{
4248 int ret;
4249
4250 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4251
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004252 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4253 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004254
4255 ssl->state = SSL_HELLO_REQUEST;
4256 ssl->renegotiation = SSL_RENEGOTIATION;
4257
Paul Bakker48916f92012-09-16 19:57:18 +00004258 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4259 {
4260 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4261 return( ret );
4262 }
4263
4264 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4265
4266 return( 0 );
4267}
4268
4269/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004270 * Renegotiate current connection on client,
4271 * or request renegotiation on server
4272 */
4273int ssl_renegotiate( ssl_context *ssl )
4274{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004275 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004276
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004277#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004278 /* On server, just send the request */
4279 if( ssl->endpoint == SSL_IS_SERVER )
4280 {
4281 if( ssl->state != SSL_HANDSHAKE_OVER )
4282 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4283
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004284 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4285
4286 /* Did we already try/start sending HelloRequest? */
4287 if( ssl->out_left != 0 )
4288 return( ssl_flush_output( ssl ) );
4289
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004290 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004291 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004292#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004293
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004294#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004295 /*
4296 * On client, either start the renegotiation process or,
4297 * if already in progress, continue the handshake
4298 */
4299 if( ssl->renegotiation != SSL_RENEGOTIATION )
4300 {
4301 if( ssl->state != SSL_HANDSHAKE_OVER )
4302 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4303
4304 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4305 {
4306 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4307 return( ret );
4308 }
4309 }
4310 else
4311 {
4312 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4313 {
4314 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4315 return( ret );
4316 }
4317 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004318#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004319
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004320 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004321}
4322
4323/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 * Receive application data decrypted from the SSL layer
4325 */
Paul Bakker23986e52011-04-24 08:57:21 +00004326int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004327{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004328 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004329 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004330
4331 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4332
4333 if( ssl->state != SSL_HANDSHAKE_OVER )
4334 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004335 ret = ssl_handshake( ssl );
4336 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4337 {
4338 record_read = 1;
4339 }
4340 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004341 {
4342 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4343 return( ret );
4344 }
4345 }
4346
4347 if( ssl->in_offt == NULL )
4348 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004349 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004350 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004351 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4352 {
4353 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4354 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004355
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004356 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4357 return( ret );
4358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 }
4360
4361 if( ssl->in_msglen == 0 &&
4362 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4363 {
4364 /*
4365 * OpenSSL sends empty messages to randomize the IV
4366 */
4367 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4368 {
Paul Bakker831a7552011-05-18 13:32:51 +00004369 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4370 return( 0 );
4371
Paul Bakker5121ce52009-01-03 21:22:43 +00004372 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4373 return( ret );
4374 }
4375 }
4376
Paul Bakker48916f92012-09-16 19:57:18 +00004377 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4378 {
4379 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4380
4381 if( ssl->endpoint == SSL_IS_CLIENT &&
4382 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4383 ssl->in_hslen != 4 ) )
4384 {
4385 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4386 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4387 }
4388
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004389 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4390 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004391 ssl->allow_legacy_renegotiation ==
4392 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004393 {
4394 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4395
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004396#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004397 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004398 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004399 /*
4400 * SSLv3 does not have a "no_renegotiation" alert
4401 */
4402 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4403 return( ret );
4404 }
4405 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004406#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004407#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4408 defined(POLARSSL_SSL_PROTO_TLS1_2)
4409 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004410 {
4411 if( ( ret = ssl_send_alert_message( ssl,
4412 SSL_ALERT_LEVEL_WARNING,
4413 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4414 {
4415 return( ret );
4416 }
Paul Bakker48916f92012-09-16 19:57:18 +00004417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004418 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004419#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4420 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004421 {
4422 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004423 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004424 }
Paul Bakker48916f92012-09-16 19:57:18 +00004425 }
4426 else
4427 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004428 ret = ssl_start_renegotiation( ssl );
4429 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4430 {
4431 record_read = 1;
4432 }
4433 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004434 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004435 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004436 return( ret );
4437 }
Paul Bakker48916f92012-09-16 19:57:18 +00004438 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004439
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004440 /* If a non-handshake record was read during renego, fallthrough,
4441 * else tell the user they should call ssl_read() again */
4442 if( ! record_read )
4443 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004444 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004445 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4446 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004447 ssl->renego_records_seen++;
4448
4449 if( ssl->renego_max_records >= 0 &&
4450 ssl->renego_records_seen > ssl->renego_max_records )
4451 {
4452 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4453 "but not honored by client" ) );
4454 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4455 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004456 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004457
4458 /* Fatal and closure alerts handled by ssl_read_record() */
4459 if( ssl->in_msgtype == SSL_MSG_ALERT )
4460 {
4461 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4462 return( POLARSSL_ERR_NET_WANT_READ );
4463 }
4464
4465 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004466 {
4467 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004468 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004469 }
4470
4471 ssl->in_offt = ssl->in_msg;
4472 }
4473
4474 n = ( len < ssl->in_msglen )
4475 ? len : ssl->in_msglen;
4476
4477 memcpy( buf, ssl->in_offt, n );
4478 ssl->in_msglen -= n;
4479
4480 if( ssl->in_msglen == 0 )
4481 /* all bytes consumed */
4482 ssl->in_offt = NULL;
4483 else
4484 /* more data available */
4485 ssl->in_offt += n;
4486
4487 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4488
Paul Bakker23986e52011-04-24 08:57:21 +00004489 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004490}
4491
4492/*
4493 * Send application data to be encrypted by the SSL layer
4494 */
Paul Bakker23986e52011-04-24 08:57:21 +00004495int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004496{
Paul Bakker23986e52011-04-24 08:57:21 +00004497 int ret;
4498 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004499 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004500
4501 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4502
4503 if( ssl->state != SSL_HANDSHAKE_OVER )
4504 {
4505 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4506 {
4507 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4508 return( ret );
4509 }
4510 }
4511
Paul Bakker05decb22013-08-15 13:33:48 +02004512#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004513 /*
4514 * Assume mfl_code is correct since it was checked when set
4515 */
4516 max_len = mfl_code_to_length[ssl->mfl_code];
4517
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004518 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004519 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004520 */
4521 if( ssl->session_out != NULL &&
4522 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4523 {
4524 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4525 }
Paul Bakker05decb22013-08-15 13:33:48 +02004526#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004527
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004528 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004529
Paul Bakker5121ce52009-01-03 21:22:43 +00004530 if( ssl->out_left != 0 )
4531 {
4532 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4533 {
4534 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4535 return( ret );
4536 }
4537 }
Paul Bakker887bd502011-06-08 13:10:54 +00004538 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004539 {
Paul Bakker887bd502011-06-08 13:10:54 +00004540 ssl->out_msglen = n;
4541 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4542 memcpy( ssl->out_msg, buf, n );
4543
4544 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4545 {
4546 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4547 return( ret );
4548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004549 }
4550
4551 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4552
Paul Bakker23986e52011-04-24 08:57:21 +00004553 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004554}
4555
4556/*
4557 * Notify the peer that the connection is being closed
4558 */
4559int ssl_close_notify( ssl_context *ssl )
4560{
4561 int ret;
4562
4563 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4564
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004565 if( ssl->out_left != 0 )
4566 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004567
4568 if( ssl->state == SSL_HANDSHAKE_OVER )
4569 {
Paul Bakker48916f92012-09-16 19:57:18 +00004570 if( ( ret = ssl_send_alert_message( ssl,
4571 SSL_ALERT_LEVEL_WARNING,
4572 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004573 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004574 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004575 return( ret );
4576 }
4577 }
4578
4579 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4580
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004581 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004582}
4583
Paul Bakker48916f92012-09-16 19:57:18 +00004584void ssl_transform_free( ssl_transform *transform )
4585{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004586 if( transform == NULL )
4587 return;
4588
Paul Bakker48916f92012-09-16 19:57:18 +00004589#if defined(POLARSSL_ZLIB_SUPPORT)
4590 deflateEnd( &transform->ctx_deflate );
4591 inflateEnd( &transform->ctx_inflate );
4592#endif
4593
Paul Bakker84bbeb52014-07-01 14:53:22 +02004594 cipher_free( &transform->cipher_ctx_enc );
4595 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004596
Paul Bakker84bbeb52014-07-01 14:53:22 +02004597 md_free( &transform->md_ctx_enc );
4598 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004599
Paul Bakker34617722014-06-13 17:20:13 +02004600 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004601}
4602
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004603#if defined(POLARSSL_X509_CRT_PARSE_C)
4604static void ssl_key_cert_free( ssl_key_cert *key_cert )
4605{
4606 ssl_key_cert *cur = key_cert, *next;
4607
4608 while( cur != NULL )
4609 {
4610 next = cur->next;
4611
4612 if( cur->key_own_alloc )
4613 {
4614 pk_free( cur->key );
4615 polarssl_free( cur->key );
4616 }
4617 polarssl_free( cur );
4618
4619 cur = next;
4620 }
4621}
4622#endif /* POLARSSL_X509_CRT_PARSE_C */
4623
Paul Bakker48916f92012-09-16 19:57:18 +00004624void ssl_handshake_free( ssl_handshake_params *handshake )
4625{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004626 if( handshake == NULL )
4627 return;
4628
Paul Bakker48916f92012-09-16 19:57:18 +00004629#if defined(POLARSSL_DHM_C)
4630 dhm_free( &handshake->dhm_ctx );
4631#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004632#if defined(POLARSSL_ECDH_C)
4633 ecdh_free( &handshake->ecdh_ctx );
4634#endif
4635
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004636#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004637 /* explicit void pointer cast for buggy MS compiler */
4638 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004639#endif
4640
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004641#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4642 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4643 /*
4644 * Free only the linked list wrapper, not the keys themselves
4645 * since the belong to the SNI callback
4646 */
4647 if( handshake->sni_key_cert != NULL )
4648 {
4649 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4650
4651 while( cur != NULL )
4652 {
4653 next = cur->next;
4654 polarssl_free( cur );
4655 cur = next;
4656 }
4657 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004658#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004659
Paul Bakker34617722014-06-13 17:20:13 +02004660 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004661}
4662
4663void ssl_session_free( ssl_session *session )
4664{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004665 if( session == NULL )
4666 return;
4667
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004668#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004669 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004670 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004671 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004672 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004673 }
Paul Bakkered27a042013-04-18 22:46:23 +02004674#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004675
Paul Bakkera503a632013-08-14 13:48:06 +02004676#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004677 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004678#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004679
Paul Bakker34617722014-06-13 17:20:13 +02004680 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004681}
4682
Paul Bakker5121ce52009-01-03 21:22:43 +00004683/*
4684 * Free an SSL context
4685 */
4686void ssl_free( ssl_context *ssl )
4687{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004688 if( ssl == NULL )
4689 return;
4690
Paul Bakker5121ce52009-01-03 21:22:43 +00004691 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4692
Paul Bakker5121ce52009-01-03 21:22:43 +00004693 if( ssl->out_ctr != NULL )
4694 {
Paul Bakker34617722014-06-13 17:20:13 +02004695 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004696 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004697 }
4698
4699 if( ssl->in_ctr != NULL )
4700 {
Paul Bakker34617722014-06-13 17:20:13 +02004701 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004702 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 }
4704
Paul Bakker16770332013-10-11 09:59:44 +02004705#if defined(POLARSSL_ZLIB_SUPPORT)
4706 if( ssl->compress_buf != NULL )
4707 {
Paul Bakker34617722014-06-13 17:20:13 +02004708 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004709 polarssl_free( ssl->compress_buf );
4710 }
4711#endif
4712
Paul Bakker40e46942009-01-03 21:51:57 +00004713#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004714 mpi_free( &ssl->dhm_P );
4715 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004716#endif
4717
Paul Bakker48916f92012-09-16 19:57:18 +00004718 if( ssl->transform )
4719 {
4720 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004721 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004722 }
4723
4724 if( ssl->handshake )
4725 {
4726 ssl_handshake_free( ssl->handshake );
4727 ssl_transform_free( ssl->transform_negotiate );
4728 ssl_session_free( ssl->session_negotiate );
4729
Paul Bakker6e339b52013-07-03 13:37:05 +02004730 polarssl_free( ssl->handshake );
4731 polarssl_free( ssl->transform_negotiate );
4732 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004733 }
4734
Paul Bakkerc0463502013-02-14 11:19:38 +01004735 if( ssl->session )
4736 {
4737 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004738 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004739 }
4740
Paul Bakkera503a632013-08-14 13:48:06 +02004741#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004742 if( ssl->ticket_keys )
4743 {
4744 ssl_ticket_keys_free( ssl->ticket_keys );
4745 polarssl_free( ssl->ticket_keys );
4746 }
Paul Bakkera503a632013-08-14 13:48:06 +02004747#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004748
Paul Bakker0be444a2013-08-27 21:55:01 +02004749#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004750 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 {
Paul Bakker34617722014-06-13 17:20:13 +02004752 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004753 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004754 ssl->hostname_len = 0;
4755 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004756#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004757
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004758#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004759 if( ssl->psk != NULL )
4760 {
Paul Bakker34617722014-06-13 17:20:13 +02004761 polarssl_zeroize( ssl->psk, ssl->psk_len );
4762 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004763 polarssl_free( ssl->psk );
4764 polarssl_free( ssl->psk_identity );
4765 ssl->psk_len = 0;
4766 ssl->psk_identity_len = 0;
4767 }
4768#endif
4769
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004770#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004771 ssl_key_cert_free( ssl->key_cert );
4772#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004773
Paul Bakker05ef8352012-05-08 09:17:57 +00004774#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4775 if( ssl_hw_record_finish != NULL )
4776 {
4777 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4778 ssl_hw_record_finish( ssl );
4779 }
4780#endif
4781
Paul Bakker5121ce52009-01-03 21:22:43 +00004782 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004783
Paul Bakker86f04f42013-02-14 11:20:09 +01004784 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004785 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004786}
4787
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004788#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004789/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004790 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004791 */
4792unsigned char ssl_sig_from_pk( pk_context *pk )
4793{
4794#if defined(POLARSSL_RSA_C)
4795 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4796 return( SSL_SIG_RSA );
4797#endif
4798#if defined(POLARSSL_ECDSA_C)
4799 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4800 return( SSL_SIG_ECDSA );
4801#endif
4802 return( SSL_SIG_ANON );
4803}
4804
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004805pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4806{
4807 switch( sig )
4808 {
4809#if defined(POLARSSL_RSA_C)
4810 case SSL_SIG_RSA:
4811 return( POLARSSL_PK_RSA );
4812#endif
4813#if defined(POLARSSL_ECDSA_C)
4814 case SSL_SIG_ECDSA:
4815 return( POLARSSL_PK_ECDSA );
4816#endif
4817 default:
4818 return( POLARSSL_PK_NONE );
4819 }
4820}
Paul Bakker9af723c2014-05-01 13:03:14 +02004821#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004822
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004823/*
4824 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4825 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004826md_type_t ssl_md_alg_from_hash( unsigned char hash )
4827{
4828 switch( hash )
4829 {
4830#if defined(POLARSSL_MD5_C)
4831 case SSL_HASH_MD5:
4832 return( POLARSSL_MD_MD5 );
4833#endif
4834#if defined(POLARSSL_SHA1_C)
4835 case SSL_HASH_SHA1:
4836 return( POLARSSL_MD_SHA1 );
4837#endif
4838#if defined(POLARSSL_SHA256_C)
4839 case SSL_HASH_SHA224:
4840 return( POLARSSL_MD_SHA224 );
4841 case SSL_HASH_SHA256:
4842 return( POLARSSL_MD_SHA256 );
4843#endif
4844#if defined(POLARSSL_SHA512_C)
4845 case SSL_HASH_SHA384:
4846 return( POLARSSL_MD_SHA384 );
4847 case SSL_HASH_SHA512:
4848 return( POLARSSL_MD_SHA512 );
4849#endif
4850 default:
4851 return( POLARSSL_MD_NONE );
4852 }
4853}
4854
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004855#if defined(POLARSSL_SSL_SET_CURVES)
4856/*
4857 * Check is a curve proposed by the peer is in our list.
4858 * Return 1 if we're willing to use it, 0 otherwise.
4859 */
4860int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4861{
4862 const ecp_group_id *gid;
4863
4864 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4865 if( *gid == grp_id )
4866 return( 1 );
4867
4868 return( 0 );
4869}
Paul Bakker9af723c2014-05-01 13:03:14 +02004870#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004871
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004872#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004873int ssl_check_cert_usage( const x509_crt *cert,
4874 const ssl_ciphersuite_t *ciphersuite,
4875 int cert_endpoint )
4876{
4877#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4878 int usage = 0;
4879#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004880#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4881 const char *ext_oid;
4882 size_t ext_len;
4883#endif
4884
4885#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4886 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4887 ((void) cert);
4888 ((void) cert_endpoint);
4889#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004890
4891#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4892 if( cert_endpoint == SSL_IS_SERVER )
4893 {
4894 /* Server part of the key exchange */
4895 switch( ciphersuite->key_exchange )
4896 {
4897 case POLARSSL_KEY_EXCHANGE_RSA:
4898 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4899 usage = KU_KEY_ENCIPHERMENT;
4900 break;
4901
4902 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4903 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4904 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4905 usage = KU_DIGITAL_SIGNATURE;
4906 break;
4907
4908 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4909 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4910 usage = KU_KEY_AGREEMENT;
4911 break;
4912
4913 /* Don't use default: we want warnings when adding new values */
4914 case POLARSSL_KEY_EXCHANGE_NONE:
4915 case POLARSSL_KEY_EXCHANGE_PSK:
4916 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4917 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4918 usage = 0;
4919 }
4920 }
4921 else
4922 {
4923 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4924 usage = KU_DIGITAL_SIGNATURE;
4925 }
4926
4927 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4928 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004929#else
4930 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004931#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4932
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004933#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4934 if( cert_endpoint == SSL_IS_SERVER )
4935 {
4936 ext_oid = OID_SERVER_AUTH;
4937 ext_len = OID_SIZE( OID_SERVER_AUTH );
4938 }
4939 else
4940 {
4941 ext_oid = OID_CLIENT_AUTH;
4942 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4943 }
4944
4945 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4946 return( -1 );
4947#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4948
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004949 return( 0 );
4950}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004951#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004952
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004953/*
4954 * Convert version numbers to/from wire format
4955 * and, for DTLS, to/from TLS equivalent.
4956 *
4957 * For TLS this is the identity.
4958 * For DTLS, use one complement (v -> 255 - v, and then map as follows:
4959 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
4960 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
4961 */
4962void ssl_write_version( int major, int minor, int transport,
4963 unsigned char ver[2] )
4964{
4965 if( transport == SSL_TRANSPORT_STREAM )
4966 {
4967 ver[0] = (unsigned char) major;
4968 ver[1] = (unsigned char) minor;
4969 }
4970#if defined(POLARSSL_SSL_PROTO_DTLS)
4971 else
4972 {
4973 if( minor == SSL_MINOR_VERSION_2 )
4974 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
4975
4976 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
4977 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
4978 }
4979#endif
4980}
4981
4982void ssl_read_version( int *major, int *minor, int transport,
4983 const unsigned char ver[2] )
4984{
4985 if( transport == SSL_TRANSPORT_STREAM )
4986 {
4987 *major = ver[0];
4988 *minor = ver[1];
4989 }
4990#if defined(POLARSSL_SSL_PROTO_DTLS)
4991 else
4992 {
4993 *major = 255 - ver[0] + 2;
4994 *minor = 255 - ver[1] + 1;
4995
4996 if( *minor == SSL_MINOR_VERSION_1 )
4997 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
4998 }
4999#endif
5000}
5001
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005002#endif /* POLARSSL_SSL_TLS_C */