blob: 6689894df2efdc18a0c1f5d84aa0360a069e7c87 [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;
1129 add_data[9] = ssl->major_ver;
1130 add_data[10] = ssl->minor_ver;
1131 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 */
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001140#if defined(POLARSSL_SSL_AEAD_RANDOM_IV)
Paul Bakker68884e32013-01-07 18:20:04 +01001141 ret = ssl->f_rng( ssl->p_rng,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001142 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1143 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Paul Bakker68884e32013-01-07 18:20:04 +01001144 if( ret != 0 )
1145 return( ret );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001146
Paul Bakker68884e32013-01-07 18:20:04 +01001147 memcpy( ssl->out_iv,
1148 ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1149 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01001150#else
1151 if( ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen != 8 )
1152 {
1153 /* Reminder if we ever add an AEAD mode with a different size */
1154 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1155 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1156 }
1157
1158 memcpy( ssl->transform_out->iv_enc + ssl->transform_out->fixed_ivlen,
1159 ssl->out_ctr, 8 );
1160 memcpy( ssl->out_iv, ssl->out_ctr, 8 );
1161#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00001162
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001163 SSL_DEBUG_BUF( 4, "IV used", ssl->out_iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001164 ssl->transform_out->ivlen - ssl->transform_out->fixed_ivlen );
Manuel Pégourié-Gonnard226d5da2013-09-05 13:19:22 +02001165
Paul Bakker68884e32013-01-07 18:20:04 +01001166 /*
1167 * Fix pointer positions and message length with added IV
1168 */
1169 enc_msg = ssl->out_msg;
1170 enc_msglen = ssl->out_msglen;
1171 ssl->out_msglen += ssl->transform_out->ivlen -
1172 ssl->transform_out->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001173
Paul Bakker68884e32013-01-07 18:20:04 +01001174 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
1175 "including %d bytes of padding",
1176 ssl->out_msglen, 0 ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001177
Paul Bakker68884e32013-01-07 18:20:04 +01001178 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
1179 ssl->out_msg, ssl->out_msglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001180
Paul Bakker68884e32013-01-07 18:20:04 +01001181 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001182 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001183 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001184 if( ( ret = cipher_auth_encrypt( &ssl->transform_out->cipher_ctx_enc,
1185 ssl->transform_out->iv_enc,
1186 ssl->transform_out->ivlen,
1187 add_data, 13,
1188 enc_msg, enc_msglen,
1189 enc_msg, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001190 enc_msg + enc_msglen, taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001191 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001192 SSL_DEBUG_RET( 1, "cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001193 return( ret );
1194 }
1195
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001196 if( olen != enc_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001197 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001198 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001199 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001200 }
1201
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001202 ssl->out_msglen += taglen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001203
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001204 SSL_DEBUG_BUF( 4, "after encrypt: tag", enc_msg + enc_msglen, taglen );
Paul Bakkerca4ab492012-04-18 14:23:57 +00001205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001207#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001208#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1209 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001210 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001211 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001212 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001213 unsigned char *enc_msg;
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001214 size_t enc_msglen, padlen, olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001215
Paul Bakker48916f92012-09-16 19:57:18 +00001216 padlen = ssl->transform_out->ivlen - ( ssl->out_msglen + 1 ) %
1217 ssl->transform_out->ivlen;
1218 if( padlen == ssl->transform_out->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 padlen = 0;
1220
1221 for( i = 0; i <= padlen; i++ )
1222 ssl->out_msg[ssl->out_msglen + i] = (unsigned char) padlen;
1223
1224 ssl->out_msglen += padlen + 1;
1225
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001226 enc_msglen = ssl->out_msglen;
1227 enc_msg = ssl->out_msg;
1228
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001229#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001230 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001231 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
1232 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001233 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001234 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001235 {
1236 /*
1237 * Generate IV
1238 */
Paul Bakker48916f92012-09-16 19:57:18 +00001239 int ret = ssl->f_rng( ssl->p_rng, ssl->transform_out->iv_enc,
1240 ssl->transform_out->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00001241 if( ret != 0 )
1242 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001243
Paul Bakker92be97b2013-01-02 17:30:03 +01001244 memcpy( ssl->out_iv, ssl->transform_out->iv_enc,
Paul Bakker48916f92012-09-16 19:57:18 +00001245 ssl->transform_out->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001246
1247 /*
1248 * Fix pointer positions and message length with added IV
1249 */
Paul Bakker92be97b2013-01-02 17:30:03 +01001250 enc_msg = ssl->out_msg;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001251 enc_msglen = ssl->out_msglen;
Paul Bakker48916f92012-09-16 19:57:18 +00001252 ssl->out_msglen += ssl->transform_out->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001253 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001254#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001255
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001257 "including %d bytes of IV and %d bytes of padding",
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001258 ssl->out_msglen, ssl->transform_out->ivlen,
1259 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261 SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Paul Bakker92be97b2013-01-02 17:30:03 +01001262 ssl->out_iv, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001264 if( ( ret = cipher_crypt( &ssl->transform_out->cipher_ctx_enc,
Paul Bakker45125bc2013-09-04 16:47:11 +02001265 ssl->transform_out->iv_enc,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001266 ssl->transform_out->ivlen,
1267 enc_msg, enc_msglen,
1268 enc_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001269 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001270 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001271 return( ret );
1272 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001273
Paul Bakkercca5b812013-08-31 17:40:26 +02001274 if( enc_msglen != olen )
1275 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001276 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001277 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001278 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001279
1280#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001281 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1282 {
1283 /*
1284 * Save IV in SSL3 and TLS1
1285 */
1286 memcpy( ssl->transform_out->iv_enc,
1287 ssl->transform_out->cipher_ctx_enc.iv,
1288 ssl->transform_out->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001289 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001290#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001292 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001293#endif /* POLARSSL_CIPHER_MODE_CBC &&
1294 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001295 {
1296 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001297 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Paul Bakkerca4ab492012-04-18 14:23:57 +00001300 for( i = 8; i > 0; i-- )
1301 if( ++ssl->out_ctr[i - 1] != 0 )
1302 break;
1303
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001304 /* The loops goes to its end iff the counter is wrapping */
1305 if( i == 0 )
1306 {
1307 SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
1308 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1309 }
1310
Paul Bakker5121ce52009-01-03 21:22:43 +00001311 SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
1312
1313 return( 0 );
1314}
1315
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001316#define POLARSSL_SSL_MAX_MAC_SIZE 48
Paul Bakkerfab5c822012-02-06 16:45:10 +00001317
Paul Bakker5121ce52009-01-03 21:22:43 +00001318static int ssl_decrypt_buf( ssl_context *ssl )
1319{
Paul Bakker1e5369c2013-12-19 16:40:57 +01001320 size_t i;
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001321 const cipher_mode_t mode = cipher_get_cipher_mode(
1322 &ssl->transform_in->cipher_ctx_dec );
Paul Bakker1e5369c2013-12-19 16:40:57 +01001323#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1324 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1325 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
1326 size_t padlen = 0, correct = 1;
1327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
1329 SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
1330
Paul Bakker48916f92012-09-16 19:57:18 +00001331 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 {
1333 SSL_DEBUG_MSG( 1, ( "in_msglen (%d) < minlen (%d)",
Paul Bakker48916f92012-09-16 19:57:18 +00001334 ssl->in_msglen, ssl->transform_in->minlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001335 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 }
1337
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001338#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001339 if( mode == POLARSSL_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01001340 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001341 int ret;
1342 size_t olen = 0;
1343
Paul Bakker68884e32013-01-07 18:20:04 +01001344 padlen = 0;
1345
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001346 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001347 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001348 ssl->transform_in->ivlen,
1349 ssl->in_msg, ssl->in_msglen,
1350 ssl->in_msg, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001351 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001352 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001353 return( ret );
1354 }
1355
1356 if( ssl->in_msglen != olen )
1357 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001358 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001359 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001360 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
Paul Bakker68884e32013-01-07 18:20:04 +01001362 else
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001363#endif /* POLARSSL_ARC4_C || POLARSSL_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001364#if defined(POLARSSL_GCM_C) || defined(POLARSSL_CCM_C)
1365 if( mode == POLARSSL_MODE_GCM ||
1366 mode == POLARSSL_MODE_CCM )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001367 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001368 int ret;
1369 size_t dec_msglen, olen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001370 unsigned char *dec_msg;
1371 unsigned char *dec_msg_result;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001372 unsigned char add_data[13];
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001373 unsigned char taglen = ssl->transform_in->ciphersuite_info->flags &
1374 POLARSSL_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001375 unsigned char explicit_iv_len = ssl->transform_in->ivlen -
1376 ssl->transform_in->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001377
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02001378 if( ssl->in_msglen < explicit_iv_len + taglen )
1379 {
1380 SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
1381 "+ taglen (%d)", ssl->in_msglen,
1382 explicit_iv_len, taglen ) );
1383 return( POLARSSL_ERR_SSL_INVALID_MAC );
1384 }
1385 dec_msglen = ssl->in_msglen - explicit_iv_len - taglen;
1386
Paul Bakker68884e32013-01-07 18:20:04 +01001387 dec_msg = ssl->in_msg;
1388 dec_msg_result = ssl->in_msg;
1389 ssl->in_msglen = dec_msglen;
1390
1391 memcpy( add_data, ssl->in_ctr, 8 );
1392 add_data[8] = ssl->in_msgtype;
1393 add_data[9] = ssl->major_ver;
1394 add_data[10] = ssl->minor_ver;
1395 add_data[11] = ( ssl->in_msglen >> 8 ) & 0xFF;
1396 add_data[12] = ssl->in_msglen & 0xFF;
1397
1398 SSL_DEBUG_BUF( 4, "additional data used for AEAD",
1399 add_data, 13 );
1400
1401 memcpy( ssl->transform_in->iv_dec + ssl->transform_in->fixed_ivlen,
1402 ssl->in_iv,
1403 ssl->transform_in->ivlen - ssl->transform_in->fixed_ivlen );
1404
1405 SSL_DEBUG_BUF( 4, "IV used", ssl->transform_in->iv_dec,
1406 ssl->transform_in->ivlen );
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001407 SSL_DEBUG_BUF( 4, "TAG used", dec_msg + dec_msglen, taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01001408
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001409 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001410 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001411 */
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001412 if( ( ret = cipher_auth_decrypt( &ssl->transform_in->cipher_ctx_dec,
1413 ssl->transform_in->iv_dec,
1414 ssl->transform_in->ivlen,
1415 add_data, 13,
1416 dec_msg, dec_msglen,
1417 dec_msg_result, &olen,
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001418 dec_msg + dec_msglen, taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001419 {
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001420 SSL_DEBUG_RET( 1, "cipher_auth_decrypt", ret );
1421
1422 if( ret == POLARSSL_ERR_CIPHER_AUTH_FAILED )
1423 return( POLARSSL_ERR_SSL_INVALID_MAC );
1424
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001425 return( ret );
1426 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001427
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02001428 if( olen != dec_msglen )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001429 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001430 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001431 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02001432 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00001433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001434 else
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001435#endif /* POLARSSL_GCM_C || POLARSSL_CCM_C */
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001436#if defined(POLARSSL_CIPHER_MODE_CBC) && \
1437 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) )
Manuel Pégourié-Gonnard5efd7722014-05-14 12:52:22 +02001438 if( mode == POLARSSL_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00001439 {
Paul Bakker45829992013-01-03 14:52:21 +01001440 /*
1441 * Decrypt and check the padding
1442 */
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001443 int ret;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001444 unsigned char *dec_msg;
1445 unsigned char *dec_msg_result;
Paul Bakker23986e52011-04-24 08:57:21 +00001446 size_t dec_msglen;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001447 size_t minlen = 0;
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001448 size_t olen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001449
Paul Bakker5121ce52009-01-03 21:22:43 +00001450 /*
Paul Bakker45829992013-01-03 14:52:21 +01001451 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00001452 */
Paul Bakker48916f92012-09-16 19:57:18 +00001453 if( ssl->in_msglen % ssl->transform_in->ivlen != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001454 {
1455 SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Paul Bakker48916f92012-09-16 19:57:18 +00001456 ssl->in_msglen, ssl->transform_in->ivlen ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001457 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001458 }
1459
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001460#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker45829992013-01-03 14:52:21 +01001461 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
1462 minlen += ssl->transform_in->ivlen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001463#endif
Paul Bakker45829992013-01-03 14:52:21 +01001464
1465 if( ssl->in_msglen < minlen + ssl->transform_in->ivlen ||
1466 ssl->in_msglen < minlen + ssl->transform_in->maclen + 1 )
1467 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001468 SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
1469 "+ 1 ) ( + expl IV )", ssl->in_msglen,
1470 ssl->transform_in->ivlen,
1471 ssl->transform_in->maclen ) );
Paul Bakker45829992013-01-03 14:52:21 +01001472 return( POLARSSL_ERR_SSL_INVALID_MAC );
1473 }
1474
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001475 dec_msglen = ssl->in_msglen;
1476 dec_msg = ssl->in_msg;
1477 dec_msg_result = ssl->in_msg;
1478
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001479#if defined(POLARSSL_SSL_PROTO_TLS1_1) || defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001480 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00001481 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001482 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00001483 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001484 {
Paul Bakker48916f92012-09-16 19:57:18 +00001485 dec_msglen -= ssl->transform_in->ivlen;
1486 ssl->in_msglen -= ssl->transform_in->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001487
Paul Bakker48916f92012-09-16 19:57:18 +00001488 for( i = 0; i < ssl->transform_in->ivlen; i++ )
Paul Bakker92be97b2013-01-02 17:30:03 +01001489 ssl->transform_in->iv_dec[i] = ssl->in_iv[i];
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001490 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001491#endif /* POLARSSL_SSL_PROTO_TLS1_1 || POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001492
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001493 if( ( ret = cipher_crypt( &ssl->transform_in->cipher_ctx_dec,
Paul Bakker45125bc2013-09-04 16:47:11 +02001494 ssl->transform_in->iv_dec,
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001495 ssl->transform_in->ivlen,
1496 dec_msg, dec_msglen,
1497 dec_msg_result, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02001498 {
Manuel Pégourié-Gonnard8764d272014-05-13 11:52:02 +02001499 SSL_DEBUG_RET( 1, "cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02001500 return( ret );
1501 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001502
Paul Bakkercca5b812013-08-31 17:40:26 +02001503 if( dec_msglen != olen )
1504 {
Manuel Pégourié-Gonnard77921982014-05-28 10:23:31 +02001505 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnarda8a25ae2013-10-27 13:48:15 +01001506 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02001507 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001508
1509#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1)
Paul Bakkercca5b812013-08-31 17:40:26 +02001510 if( ssl->minor_ver < SSL_MINOR_VERSION_2 )
1511 {
1512 /*
1513 * Save IV in SSL3 and TLS1
1514 */
1515 memcpy( ssl->transform_in->iv_dec,
1516 ssl->transform_in->cipher_ctx_dec.iv,
1517 ssl->transform_in->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 }
Paul Bakkercca5b812013-08-31 17:40:26 +02001519#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001520
1521 padlen = 1 + ssl->in_msg[ssl->in_msglen - 1];
Paul Bakker45829992013-01-03 14:52:21 +01001522
1523 if( ssl->in_msglen < ssl->transform_in->maclen + padlen )
1524 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001525#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker45829992013-01-03 14:52:21 +01001526 SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
1527 ssl->in_msglen, ssl->transform_in->maclen, padlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001528#endif
Paul Bakker45829992013-01-03 14:52:21 +01001529 padlen = 0;
Paul Bakker45829992013-01-03 14:52:21 +01001530 correct = 0;
1531 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001533#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1535 {
Paul Bakker48916f92012-09-16 19:57:18 +00001536 if( padlen > ssl->transform_in->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001537 {
Paul Bakkerd66f0702013-01-31 16:57:45 +01001538#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker5121ce52009-01-03 21:22:43 +00001539 SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
1540 "should be no more than %d",
Paul Bakker48916f92012-09-16 19:57:18 +00001541 padlen, ssl->transform_in->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001542#endif
Paul Bakker45829992013-01-03 14:52:21 +01001543 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 }
1545 }
1546 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001547#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001548#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1549 defined(POLARSSL_SSL_PROTO_TLS1_2)
1550 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001551 {
1552 /*
Paul Bakker45829992013-01-03 14:52:21 +01001553 * TLSv1+: always check the padding up to the first failure
1554 * and fake check up to 256 bytes of padding
Paul Bakker5121ce52009-01-03 21:22:43 +00001555 */
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001556 size_t pad_count = 0, real_count = 1;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001557 size_t padding_idx = ssl->in_msglen - padlen - 1;
1558
Paul Bakker956c9e02013-12-19 14:42:28 +01001559 /*
1560 * Padding is guaranteed to be incorrect if:
Paul Bakker91c61bc2014-03-26 14:06:55 +01001561 * 1. padlen >= ssl->in_msglen
Paul Bakker956c9e02013-12-19 14:42:28 +01001562 *
Paul Bakker61885c72014-04-25 12:59:03 +02001563 * 2. padding_idx >= SSL_MAX_CONTENT_LEN +
1564 * ssl->transform_in->maclen
Paul Bakker956c9e02013-12-19 14:42:28 +01001565 *
1566 * In both cases we reset padding_idx to a safe value (0) to
1567 * prevent out-of-buffer reads.
1568 */
Paul Bakker91c61bc2014-03-26 14:06:55 +01001569 correct &= ( ssl->in_msglen >= padlen + 1 );
Paul Bakker61885c72014-04-25 12:59:03 +02001570 correct &= ( padding_idx < SSL_MAX_CONTENT_LEN +
1571 ssl->transform_in->maclen );
Paul Bakker956c9e02013-12-19 14:42:28 +01001572
1573 padding_idx *= correct;
1574
Paul Bakkerca9c87e2013-09-25 18:52:37 +02001575 for( i = 1; i <= 256; i++ )
1576 {
1577 real_count &= ( i <= padlen );
1578 pad_count += real_count *
1579 ( ssl->in_msg[padding_idx + i] == padlen - 1 );
1580 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01001581
1582 correct &= ( pad_count == padlen ); /* Only 1 on correct padding */
Paul Bakkere47b34b2013-02-27 14:48:00 +01001583
Paul Bakkerd66f0702013-01-31 16:57:45 +01001584#if defined(POLARSSL_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001585 if( padlen > 0 && correct == 0 )
Paul Bakker45829992013-01-03 14:52:21 +01001586 SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01001587#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01001588 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001590 else
1591#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
1592 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001593 {
1594 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001595 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001596 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001597 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001598 else
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001599#endif /* POLARSSL_CIPHER_MODE_CBC &&
1600 ( POLARSSL_AES_C || POLARSSL_CAMELLIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001601 {
1602 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001603 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02001604 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
1606 SSL_DEBUG_BUF( 4, "raw buffer after decryption",
1607 ssl->in_msg, ssl->in_msglen );
1608
1609 /*
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001610 * Always compute the MAC (RFC4346, CBCTIME), except for AEAD of course
Paul Bakker5121ce52009-01-03 21:22:43 +00001611 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001612#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER) || \
1613 ( defined(POLARSSL_CIPHER_MODE_CBC) && \
1614 ( defined(POLARSSL_AES_C) || defined(POLARSSL_CAMELLIA_C) ) )
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001615 if( mode != POLARSSL_MODE_GCM &&
1616 mode != POLARSSL_MODE_CCM )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001617 {
Paul Bakker1e5369c2013-12-19 16:40:57 +01001618 unsigned char tmp[POLARSSL_SSL_MAX_MAC_SIZE];
1619
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001620 ssl->in_msglen -= ( ssl->transform_in->maclen + padlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001622 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
1623 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001625 memcpy( tmp, ssl->in_msg + ssl->in_msglen, ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001627#if defined(POLARSSL_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001628 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
1629 {
1630 ssl_mac( &ssl->transform_in->md_ctx_dec,
1631 ssl->transform_in->mac_dec,
1632 ssl->in_msg, ssl->in_msglen,
1633 ssl->in_ctr, ssl->in_msgtype );
1634 }
1635 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001636#endif /* POLARSSL_SSL_PROTO_SSL3 */
1637#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001638 defined(POLARSSL_SSL_PROTO_TLS1_2)
1639 if( ssl->minor_ver > SSL_MINOR_VERSION_0 )
1640 {
1641 /*
1642 * Process MAC and always update for padlen afterwards to make
1643 * total time independent of padlen
1644 *
Paul Bakker9af723c2014-05-01 13:03:14 +02001645 * extra_run compensates MAC check for padlen
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001646 *
1647 * Known timing attacks:
1648 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
1649 *
1650 * We use ( ( Lx + 8 ) / 64 ) to handle 'negative Lx' values
1651 * correctly. (We round down instead of up, so -56 is the correct
1652 * value for our calculations instead of -55)
1653 */
1654 size_t j, extra_run = 0;
1655 extra_run = ( 13 + ssl->in_msglen + padlen + 8 ) / 64 -
1656 ( 13 + ssl->in_msglen + 8 ) / 64;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001657
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001658 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01001659
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001660 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_ctr, 13 );
1661 md_hmac_update( &ssl->transform_in->md_ctx_dec, ssl->in_msg,
1662 ssl->in_msglen );
1663 md_hmac_finish( &ssl->transform_in->md_ctx_dec,
1664 ssl->in_msg + ssl->in_msglen );
1665 for( j = 0; j < extra_run; j++ )
1666 md_process( &ssl->transform_in->md_ctx_dec, ssl->in_msg );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001667
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001668 md_hmac_reset( &ssl->transform_in->md_ctx_dec );
1669 }
1670 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001671#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001672 POLARSSL_SSL_PROTO_TLS1_2 */
1673 {
1674 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001675 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001676 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001678 SSL_DEBUG_BUF( 4, "message mac", tmp, ssl->transform_in->maclen );
1679 SSL_DEBUG_BUF( 4, "computed mac", ssl->in_msg + ssl->in_msglen,
1680 ssl->transform_in->maclen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01001682 if( safer_memcmp( tmp, ssl->in_msg + ssl->in_msglen,
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001683 ssl->transform_in->maclen ) != 0 )
1684 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01001685#if defined(POLARSSL_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001686 SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01001687#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001688 correct = 0;
1689 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02001691 /*
1692 * Finally check the correct flag
1693 */
1694 if( correct == 0 )
1695 return( POLARSSL_ERR_SSL_INVALID_MAC );
1696 }
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02001697#endif /* AEAD not the only option */
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
1699 if( ssl->in_msglen == 0 )
1700 {
1701 ssl->nb_zero++;
1702
1703 /*
1704 * Three or more empty messages may be a DoS attack
1705 * (excessive CPU consumption).
1706 */
1707 if( ssl->nb_zero > 3 )
1708 {
1709 SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
1710 "messages, possible DoS attack" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001711 return( POLARSSL_ERR_SSL_INVALID_MAC );
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 }
1713 }
1714 else
1715 ssl->nb_zero = 0;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001716
Paul Bakker23986e52011-04-24 08:57:21 +00001717 for( i = 8; i > 0; i-- )
1718 if( ++ssl->in_ctr[i - 1] != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001719 break;
1720
Manuel Pégourié-Gonnard83cdffc2014-03-10 21:20:29 +01001721 /* The loops goes to its end iff the counter is wrapping */
1722 if( i == 0 )
1723 {
1724 SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
1725 return( POLARSSL_ERR_SSL_COUNTER_WRAPPING );
1726 }
1727
Paul Bakker5121ce52009-01-03 21:22:43 +00001728 SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
1729
1730 return( 0 );
1731}
1732
Paul Bakker2770fbd2012-07-03 13:30:23 +00001733#if defined(POLARSSL_ZLIB_SUPPORT)
1734/*
1735 * Compression/decompression functions
1736 */
1737static int ssl_compress_buf( ssl_context *ssl )
1738{
1739 int ret;
1740 unsigned char *msg_post = ssl->out_msg;
1741 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001742 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001743
1744 SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
1745
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001746 if( len_pre == 0 )
1747 return( 0 );
1748
Paul Bakker2770fbd2012-07-03 13:30:23 +00001749 memcpy( msg_pre, ssl->out_msg, len_pre );
1750
1751 SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
1752 ssl->out_msglen ) );
1753
1754 SSL_DEBUG_BUF( 4, "before compression: output payload",
1755 ssl->out_msg, ssl->out_msglen );
1756
Paul Bakker48916f92012-09-16 19:57:18 +00001757 ssl->transform_out->ctx_deflate.next_in = msg_pre;
1758 ssl->transform_out->ctx_deflate.avail_in = len_pre;
1759 ssl->transform_out->ctx_deflate.next_out = msg_post;
1760 ssl->transform_out->ctx_deflate.avail_out = SSL_BUFFER_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001761
Paul Bakker48916f92012-09-16 19:57:18 +00001762 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001763 if( ret != Z_OK )
1764 {
1765 SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
1766 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1767 }
1768
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001769 ssl->out_msglen = SSL_BUFFER_LEN -
1770 ssl->transform_out->ctx_deflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001771
Paul Bakker2770fbd2012-07-03 13:30:23 +00001772 SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
1773 ssl->out_msglen ) );
1774
1775 SSL_DEBUG_BUF( 4, "after compression: output payload",
1776 ssl->out_msg, ssl->out_msglen );
1777
1778 SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
1779
1780 return( 0 );
1781}
1782
1783static int ssl_decompress_buf( ssl_context *ssl )
1784{
1785 int ret;
1786 unsigned char *msg_post = ssl->in_msg;
1787 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02001788 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001789
1790 SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
1791
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02001792 if( len_pre == 0 )
1793 return( 0 );
1794
Paul Bakker2770fbd2012-07-03 13:30:23 +00001795 memcpy( msg_pre, ssl->in_msg, len_pre );
1796
1797 SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
1798 ssl->in_msglen ) );
1799
1800 SSL_DEBUG_BUF( 4, "before decompression: input payload",
1801 ssl->in_msg, ssl->in_msglen );
1802
Paul Bakker48916f92012-09-16 19:57:18 +00001803 ssl->transform_in->ctx_inflate.next_in = msg_pre;
1804 ssl->transform_in->ctx_inflate.avail_in = len_pre;
1805 ssl->transform_in->ctx_inflate.next_out = msg_post;
1806 ssl->transform_in->ctx_inflate.avail_out = SSL_MAX_CONTENT_LEN;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001807
Paul Bakker48916f92012-09-16 19:57:18 +00001808 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001809 if( ret != Z_OK )
1810 {
1811 SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
1812 return( POLARSSL_ERR_SSL_COMPRESSION_FAILED );
1813 }
1814
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001815 ssl->in_msglen = SSL_MAX_CONTENT_LEN -
1816 ssl->transform_in->ctx_inflate.avail_out;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001817
Paul Bakker2770fbd2012-07-03 13:30:23 +00001818 SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
1819 ssl->in_msglen ) );
1820
1821 SSL_DEBUG_BUF( 4, "after decompression: input payload",
1822 ssl->in_msg, ssl->in_msglen );
1823
1824 SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
1825
1826 return( 0 );
1827}
1828#endif /* POLARSSL_ZLIB_SUPPORT */
1829
Paul Bakker5121ce52009-01-03 21:22:43 +00001830/*
1831 * Fill the input message buffer
1832 */
Paul Bakker23986e52011-04-24 08:57:21 +00001833int ssl_fetch_input( ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00001834{
Paul Bakker23986e52011-04-24 08:57:21 +00001835 int ret;
1836 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
1838 SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
1839
Paul Bakker1a1fbba2014-04-30 14:38:05 +02001840 if( nb_want > SSL_BUFFER_LEN - 8 )
1841 {
1842 SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
1843 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1844 }
1845
Paul Bakker5121ce52009-01-03 21:22:43 +00001846 while( ssl->in_left < nb_want )
1847 {
1848 len = nb_want - ssl->in_left;
1849 ret = ssl->f_recv( ssl->p_recv, ssl->in_hdr + ssl->in_left, len );
1850
1851 SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
1852 ssl->in_left, nb_want ) );
1853 SSL_DEBUG_RET( 2, "ssl->f_recv", ret );
1854
Paul Bakker831a7552011-05-18 13:32:51 +00001855 if( ret == 0 )
1856 return( POLARSSL_ERR_SSL_CONN_EOF );
1857
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 if( ret < 0 )
1859 return( ret );
1860
1861 ssl->in_left += ret;
1862 }
1863
1864 SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
1865
1866 return( 0 );
1867}
1868
1869/*
1870 * Flush any data not yet written
1871 */
1872int ssl_flush_output( ssl_context *ssl )
1873{
1874 int ret;
1875 unsigned char *buf;
1876
1877 SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
1878
1879 while( ssl->out_left > 0 )
1880 {
1881 SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
1882 5 + ssl->out_msglen, ssl->out_left ) );
1883
Paul Bakker5bd42292012-12-19 14:40:42 +01001884 buf = ssl->out_hdr + 5 + ssl->out_msglen - ssl->out_left;
Paul Bakker5121ce52009-01-03 21:22:43 +00001885 ret = ssl->f_send( ssl->p_send, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00001886
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 SSL_DEBUG_RET( 2, "ssl->f_send", ret );
1888
1889 if( ret <= 0 )
1890 return( ret );
1891
1892 ssl->out_left -= ret;
1893 }
1894
1895 SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
1896
1897 return( 0 );
1898}
1899
1900/*
1901 * Record layer functions
1902 */
1903int ssl_write_record( ssl_context *ssl )
1904{
Paul Bakker05ef8352012-05-08 09:17:57 +00001905 int ret, done = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00001906 size_t len = ssl->out_msglen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
1908 SSL_DEBUG_MSG( 2, ( "=> write record" ) );
1909
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 if( ssl->out_msgtype == SSL_MSG_HANDSHAKE )
1911 {
1912 ssl->out_msg[1] = (unsigned char)( ( len - 4 ) >> 16 );
1913 ssl->out_msg[2] = (unsigned char)( ( len - 4 ) >> 8 );
1914 ssl->out_msg[3] = (unsigned char)( ( len - 4 ) );
1915
Manuel Pégourié-Gonnardf3dc2f62013-10-29 18:17:41 +01001916 if( ssl->out_msg[0] != SSL_HS_HELLO_REQUEST )
1917 ssl->handshake->update_checksum( ssl, ssl->out_msg, len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 }
1919
Paul Bakker2770fbd2012-07-03 13:30:23 +00001920#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00001921 if( ssl->transform_out != NULL &&
1922 ssl->session_out->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001923 {
1924 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
1925 {
1926 SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
1927 return( ret );
1928 }
1929
1930 len = ssl->out_msglen;
1931 }
1932#endif /*POLARSSL_ZLIB_SUPPORT */
1933
Paul Bakker05ef8352012-05-08 09:17:57 +00001934#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02001935 if( ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 {
Paul Bakker05ef8352012-05-08 09:17:57 +00001937 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001938
Paul Bakker05ef8352012-05-08 09:17:57 +00001939 ret = ssl_hw_record_write( ssl );
1940 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
1941 {
1942 SSL_DEBUG_RET( 1, "ssl_hw_record_write", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001943 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00001944 }
Paul Bakkerc7878112012-12-19 14:41:14 +01001945
1946 if( ret == 0 )
1947 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00001948 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001949#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001950 if( !done )
1951 {
1952 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
1953 ssl->out_hdr[1] = (unsigned char) ssl->major_ver;
1954 ssl->out_hdr[2] = (unsigned char) ssl->minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00001955 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1956 ssl->out_hdr[4] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00001957
Paul Bakker48916f92012-09-16 19:57:18 +00001958 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001959 {
1960 if( ( ret = ssl_encrypt_buf( ssl ) ) != 0 )
1961 {
1962 SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
1963 return( ret );
1964 }
1965
1966 len = ssl->out_msglen;
1967 ssl->out_hdr[3] = (unsigned char)( len >> 8 );
1968 ssl->out_hdr[4] = (unsigned char)( len );
1969 }
1970
1971 ssl->out_left = 5 + ssl->out_msglen;
1972
1973 SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
1974 "version = [%d:%d], msglen = %d",
1975 ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2],
1976 ( ssl->out_hdr[3] << 8 ) | ssl->out_hdr[4] ) );
1977
1978 SSL_DEBUG_BUF( 4, "output record sent to network",
Paul Bakker5bd42292012-12-19 14:40:42 +01001979 ssl->out_hdr, 5 + ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 }
1981
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
1983 {
1984 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
1985 return( ret );
1986 }
1987
1988 SSL_DEBUG_MSG( 2, ( "<= write record" ) );
1989
1990 return( 0 );
1991}
1992
1993int ssl_read_record( ssl_context *ssl )
1994{
Paul Bakker05ef8352012-05-08 09:17:57 +00001995 int ret, done = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
1997 SSL_DEBUG_MSG( 2, ( "=> read record" ) );
1998
1999 if( ssl->in_hslen != 0 &&
2000 ssl->in_hslen < ssl->in_msglen )
2001 {
2002 /*
2003 * Get next Handshake message in the current record
2004 */
2005 ssl->in_msglen -= ssl->in_hslen;
2006
Paul Bakker8934a982011-08-05 11:11:53 +00002007 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
Paul Bakker48916f92012-09-16 19:57:18 +00002008 ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
2010 ssl->in_hslen = 4;
2011 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2012
2013 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2014 " %d, type = %d, hslen = %d",
2015 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2016
2017 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2018 {
2019 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002020 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 }
2022
2023 if( ssl->in_msglen < ssl->in_hslen )
2024 {
2025 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002026 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002027 }
2028
Paul Bakker4224bc02014-04-08 14:36:50 +02002029 if( ssl->state != SSL_HANDSHAKE_OVER )
2030 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002031
2032 return( 0 );
2033 }
2034
2035 ssl->in_hslen = 0;
2036
2037 /*
2038 * Read the record header and validate it
2039 */
2040 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
2041 {
2042 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2043 return( ret );
2044 }
2045
2046 ssl->in_msgtype = ssl->in_hdr[0];
2047 ssl->in_msglen = ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4];
2048
2049 SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
2050 "version = [%d:%d], msglen = %d",
2051 ssl->in_hdr[0], ssl->in_hdr[1], ssl->in_hdr[2],
2052 ( ssl->in_hdr[3] << 8 ) | ssl->in_hdr[4] ) );
2053
2054 if( ssl->in_hdr[1] != ssl->major_ver )
2055 {
2056 SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002057 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 }
2059
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002060 if( ssl->in_hdr[2] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 {
2062 SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002063 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002064 }
2065
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002066 /* Sanity check (outer boundaries) */
2067 if( ssl->in_msglen < 1 || ssl->in_msglen > SSL_BUFFER_LEN - 13 )
2068 {
2069 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
2070 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2071 }
2072
Paul Bakker5121ce52009-01-03 21:22:43 +00002073 /*
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002074 * Make sure the message length is acceptable for the current transform
2075 * and protocol version.
Paul Bakker5121ce52009-01-03 21:22:43 +00002076 */
Paul Bakker48916f92012-09-16 19:57:18 +00002077 if( ssl->transform_in == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 {
Paul Bakker1a1fbba2014-04-30 14:38:05 +02002079 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 {
2081 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002082 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 }
2084 }
2085 else
2086 {
Paul Bakker48916f92012-09-16 19:57:18 +00002087 if( ssl->in_msglen < ssl->transform_in->minlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002088 {
2089 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002090 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002091 }
2092
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002093#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 if( ssl->minor_ver == SSL_MINOR_VERSION_0 &&
Paul Bakker48916f92012-09-16 19:57:18 +00002095 ssl->in_msglen > ssl->transform_in->minlen + SSL_MAX_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 {
2097 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002098 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002099 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002102#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2103 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 /*
2105 * TLS encrypted messages can have up to 256 bytes of padding
2106 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002107 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002108 ssl->in_msglen > ssl->transform_in->minlen +
2109 SSL_MAX_CONTENT_LEN + 256 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002110 {
2111 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002112 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002113 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002114#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002115 }
2116
2117 /*
2118 * Read and optionally decrypt the message contents
2119 */
2120 if( ( ret = ssl_fetch_input( ssl, 5 + ssl->in_msglen ) ) != 0 )
2121 {
2122 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
2123 return( ret );
2124 }
2125
2126 SSL_DEBUG_BUF( 4, "input record from network",
2127 ssl->in_hdr, 5 + ssl->in_msglen );
2128
Paul Bakker05ef8352012-05-08 09:17:57 +00002129#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002130 if( ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00002131 {
2132 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_read()" ) );
2133
2134 ret = ssl_hw_record_read( ssl );
2135 if( ret != 0 && ret != POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH )
2136 {
2137 SSL_DEBUG_RET( 1, "ssl_hw_record_read", ret );
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002138 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00002139 }
Paul Bakkerc7878112012-12-19 14:41:14 +01002140
2141 if( ret == 0 )
2142 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00002143 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002144#endif /* POLARSSL_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00002145 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002146 {
2147 if( ( ret = ssl_decrypt_buf( ssl ) ) != 0 )
2148 {
Paul Bakker40865c82013-01-31 17:13:13 +01002149#if defined(POLARSSL_SSL_ALERT_MESSAGES)
2150 if( ret == POLARSSL_ERR_SSL_INVALID_MAC )
2151 {
2152 ssl_send_alert_message( ssl,
2153 SSL_ALERT_LEVEL_FATAL,
2154 SSL_ALERT_MSG_BAD_RECORD_MAC );
2155 }
2156#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
2158 return( ret );
2159 }
2160
2161 SSL_DEBUG_BUF( 4, "input payload after decrypt",
2162 ssl->in_msg, ssl->in_msglen );
2163
2164 if( ssl->in_msglen > SSL_MAX_CONTENT_LEN )
2165 {
2166 SSL_DEBUG_MSG( 1, ( "bad message length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002167 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168 }
2169 }
2170
Paul Bakker2770fbd2012-07-03 13:30:23 +00002171#if defined(POLARSSL_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00002172 if( ssl->transform_in != NULL &&
2173 ssl->session_in->compression == SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00002174 {
2175 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
2176 {
2177 SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
2178 return( ret );
2179 }
2180
2181 ssl->in_hdr[3] = (unsigned char)( ssl->in_msglen >> 8 );
2182 ssl->in_hdr[4] = (unsigned char)( ssl->in_msglen );
2183 }
2184#endif /* POLARSSL_ZLIB_SUPPORT */
2185
Paul Bakker0a925182012-04-16 06:46:41 +00002186 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE &&
2187 ssl->in_msgtype != SSL_MSG_ALERT &&
2188 ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC &&
2189 ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
2190 {
2191 SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
2192
Paul Bakker48916f92012-09-16 19:57:18 +00002193 if( ( ret = ssl_send_alert_message( ssl,
2194 SSL_ALERT_LEVEL_FATAL,
2195 SSL_ALERT_MSG_UNEXPECTED_MESSAGE ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00002196 {
2197 return( ret );
2198 }
2199
2200 return( POLARSSL_ERR_SSL_INVALID_RECORD );
2201 }
2202
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
2204 {
2205 ssl->in_hslen = 4;
2206 ssl->in_hslen += ( ssl->in_msg[2] << 8 ) | ssl->in_msg[3];
2207
2208 SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
2209 " %d, type = %d, hslen = %d",
2210 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
2211
2212 /*
2213 * Additional checks to validate the handshake header
2214 */
2215 if( ssl->in_msglen < 4 || ssl->in_msg[1] != 0 )
2216 {
2217 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002218 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 }
2220
2221 if( ssl->in_msglen < ssl->in_hslen )
2222 {
2223 SSL_DEBUG_MSG( 1, ( "bad handshake length" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002224 return( POLARSSL_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 }
2226
Paul Bakker48916f92012-09-16 19:57:18 +00002227 if( ssl->state != SSL_HANDSHAKE_OVER )
2228 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002229 }
2230
2231 if( ssl->in_msgtype == SSL_MSG_ALERT )
2232 {
2233 SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
2234 ssl->in_msg[0], ssl->in_msg[1] ) );
2235
2236 /*
2237 * Ignore non-fatal alerts, except close_notify
2238 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002239 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002240 {
Paul Bakker2770fbd2012-07-03 13:30:23 +00002241 SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
2242 ssl->in_msg[1] ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00002243 return( POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 }
2245
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002246 if( ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2247 ssl->in_msg[1] == SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 {
2249 SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002250 return( POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 }
2252 }
2253
2254 ssl->in_left = 0;
2255
2256 SSL_DEBUG_MSG( 2, ( "<= read record" ) );
2257
2258 return( 0 );
2259}
2260
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002261int ssl_send_fatal_handshake_failure( ssl_context *ssl )
2262{
2263 int ret;
2264
2265 if( ( ret = ssl_send_alert_message( ssl,
2266 SSL_ALERT_LEVEL_FATAL,
2267 SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
2268 {
2269 return( ret );
2270 }
2271
2272 return( 0 );
2273}
2274
Paul Bakker0a925182012-04-16 06:46:41 +00002275int ssl_send_alert_message( ssl_context *ssl,
2276 unsigned char level,
2277 unsigned char message )
2278{
2279 int ret;
2280
2281 SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
2282
2283 ssl->out_msgtype = SSL_MSG_ALERT;
2284 ssl->out_msglen = 2;
2285 ssl->out_msg[0] = level;
2286 ssl->out_msg[1] = message;
2287
2288 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2289 {
2290 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2291 return( ret );
2292 }
2293
2294 SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
2295
2296 return( 0 );
2297}
2298
Paul Bakker5121ce52009-01-03 21:22:43 +00002299/*
2300 * Handshake functions
2301 */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002302#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2303 !defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED) && \
2304 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2305 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2306 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
2307 !defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) && \
2308 !defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002309int ssl_write_certificate( ssl_context *ssl )
2310{
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002311 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002312
2313 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2314
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002316 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002318 {
2319 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2320 ssl->state++;
2321 return( 0 );
2322 }
2323
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002324 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2325 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002326}
2327
2328int ssl_parse_certificate( ssl_context *ssl )
2329{
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002330 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2331
2332 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2333
2334 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002335 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2336 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 {
2338 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2339 ssl->state++;
2340 return( 0 );
2341 }
2342
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002343 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2344 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002345}
2346#else
2347int ssl_write_certificate( ssl_context *ssl )
2348{
2349 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2350 size_t i, n;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002351 const x509_crt *crt;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002352 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2353
2354 SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
2355
2356 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002357 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2358 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359 {
2360 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2361 ssl->state++;
2362 return( 0 );
2363 }
2364
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 if( ssl->endpoint == SSL_IS_CLIENT )
2366 {
2367 if( ssl->client_auth == 0 )
2368 {
2369 SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
2370 ssl->state++;
2371 return( 0 );
2372 }
2373
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002374#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 /*
2376 * If using SSLv3 and got no cert, send an Alert message
2377 * (otherwise an empty Certificate message will be sent).
2378 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002379 if( ssl_own_cert( ssl ) == NULL &&
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2381 {
2382 ssl->out_msglen = 2;
2383 ssl->out_msgtype = SSL_MSG_ALERT;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002384 ssl->out_msg[0] = SSL_ALERT_LEVEL_WARNING;
2385 ssl->out_msg[1] = SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00002386
2387 SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
2388 goto write_msg;
2389 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002390#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 }
2392 else /* SSL_IS_SERVER */
2393 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002394 if( ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 {
2396 SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002397 return( POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 }
2399 }
2400
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002401 SSL_DEBUG_CRT( 3, "own certificate", ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
2403 /*
2404 * 0 . 0 handshake type
2405 * 1 . 3 handshake length
2406 * 4 . 6 length of all certs
2407 * 7 . 9 length of cert. 1
2408 * 10 . n-1 peer certificate
2409 * n . n+2 length of cert. 2
2410 * n+3 . ... upper level cert, etc.
2411 */
2412 i = 7;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002413 crt = ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Paul Bakker29087132010-03-21 21:03:34 +00002415 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 {
2417 n = crt->raw.len;
Paul Bakker6992eb72013-12-31 11:35:16 +01002418 if( n > SSL_MAX_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 {
2420 SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
2421 i + 3 + n, SSL_MAX_CONTENT_LEN ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002422 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 }
2424
2425 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
2426 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
2427 ssl->out_msg[i + 2] = (unsigned char)( n );
2428
2429 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
2430 i += n; crt = crt->next;
2431 }
2432
2433 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
2434 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
2435 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
2436
2437 ssl->out_msglen = i;
2438 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2439 ssl->out_msg[0] = SSL_HS_CERTIFICATE;
2440
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002441#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002442write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002443#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 ssl->state++;
2446
2447 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2448 {
2449 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2450 return( ret );
2451 }
2452
2453 SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
2454
Paul Bakkered27a042013-04-18 22:46:23 +02002455 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002456}
2457
2458int ssl_parse_certificate( ssl_context *ssl )
2459{
Paul Bakkered27a042013-04-18 22:46:23 +02002460 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker23986e52011-04-24 08:57:21 +00002461 size_t i, n;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002462 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
2464 SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
2465
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002466 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002467 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2468 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002469 {
2470 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2471 ssl->state++;
2472 return( 0 );
2473 }
2474
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 if( ssl->endpoint == SSL_IS_SERVER &&
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002476 ( ssl->authmode == SSL_VERIFY_NONE ||
2477 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002479 ssl->session_negotiate->verify_result = BADCERT_SKIP_VERIFY;
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
2481 ssl->state++;
2482 return( 0 );
2483 }
2484
2485 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2486 {
2487 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2488 return( ret );
2489 }
2490
2491 ssl->state++;
2492
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002493#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 /*
2495 * Check if the client sent an empty certificate
2496 */
2497 if( ssl->endpoint == SSL_IS_SERVER &&
2498 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2499 {
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002500 if( ssl->in_msglen == 2 &&
2501 ssl->in_msgtype == SSL_MSG_ALERT &&
2502 ssl->in_msg[0] == SSL_ALERT_LEVEL_WARNING &&
2503 ssl->in_msg[1] == SSL_ALERT_MSG_NO_CERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 {
2505 SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
2506
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002507 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 if( ssl->authmode == SSL_VERIFY_OPTIONAL )
2509 return( 0 );
2510 else
Paul Bakker40e46942009-01-03 21:51:57 +00002511 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002512 }
2513 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002514#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002515
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002516#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2517 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 if( ssl->endpoint == SSL_IS_SERVER &&
2519 ssl->minor_ver != SSL_MINOR_VERSION_0 )
2520 {
2521 if( ssl->in_hslen == 7 &&
2522 ssl->in_msgtype == SSL_MSG_HANDSHAKE &&
2523 ssl->in_msg[0] == SSL_HS_CERTIFICATE &&
2524 memcmp( ssl->in_msg + 4, "\0\0\0", 3 ) == 0 )
2525 {
2526 SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
2527
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02002528 ssl->session_negotiate->verify_result = BADCERT_MISSING;
Paul Bakker5121ce52009-01-03 21:22:43 +00002529 if( ssl->authmode == SSL_VERIFY_REQUIRED )
Paul Bakker40e46942009-01-03 21:51:57 +00002530 return( POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531 else
2532 return( 0 );
2533 }
2534 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002535#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2536 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002537
2538 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2539 {
2540 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002541 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 }
2543
2544 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE || ssl->in_hslen < 10 )
2545 {
2546 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002547 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002548 }
2549
2550 /*
2551 * Same message structure as in ssl_write_certificate()
2552 */
2553 n = ( ssl->in_msg[5] << 8 ) | ssl->in_msg[6];
2554
2555 if( ssl->in_msg[4] != 0 || ssl->in_hslen != 7 + n )
2556 {
2557 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002558 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002559 }
2560
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002561 /* In case we tried to reuse a session but it failed */
2562 if( ssl->session_negotiate->peer_cert != NULL )
2563 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002564 x509_crt_free( ssl->session_negotiate->peer_cert );
Manuel Pégourié-Gonnardbfb355c2013-09-07 17:27:43 +02002565 polarssl_free( ssl->session_negotiate->peer_cert );
2566 }
2567
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002568 if( ( ssl->session_negotiate->peer_cert = (x509_crt *) polarssl_malloc(
2569 sizeof( x509_crt ) ) ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 {
2571 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed",
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002572 sizeof( x509_crt ) ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00002573 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002574 }
2575
Paul Bakkerb6b09562013-09-18 14:17:41 +02002576 x509_crt_init( ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
2578 i = 7;
2579
2580 while( i < ssl->in_hslen )
2581 {
2582 if( ssl->in_msg[i] != 0 )
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
2588 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
2589 | (unsigned int) ssl->in_msg[i + 2];
2590 i += 3;
2591
2592 if( n < 128 || i + n > ssl->in_hslen )
2593 {
2594 SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002595 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 }
2597
Paul Bakkerddf26b42013-09-18 13:46:23 +02002598 ret = x509_crt_parse_der( ssl->session_negotiate->peer_cert,
2599 ssl->in_msg + i, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 if( ret != 0 )
2601 {
Paul Bakkerddf26b42013-09-18 13:46:23 +02002602 SSL_DEBUG_RET( 1, " x509_crt_parse_der", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 return( ret );
2604 }
2605
2606 i += n;
2607 }
2608
Paul Bakker48916f92012-09-16 19:57:18 +00002609 SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
Paul Bakker5121ce52009-01-03 21:22:43 +00002610
Manuel Pégourié-Gonnard796c6f32014-03-10 09:34:49 +01002611 /*
2612 * On client, make sure the server cert doesn't change during renego to
2613 * avoid "triple handshake" attack: https://secure-resumption.com/
2614 */
2615 if( ssl->endpoint == SSL_IS_CLIENT &&
2616 ssl->renegotiation == SSL_RENEGOTIATION )
2617 {
2618 if( ssl->session->peer_cert == NULL )
2619 {
2620 SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
2621 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2622 }
2623
2624 if( ssl->session->peer_cert->raw.len !=
2625 ssl->session_negotiate->peer_cert->raw.len ||
2626 memcmp( ssl->session->peer_cert->raw.p,
2627 ssl->session_negotiate->peer_cert->raw.p,
2628 ssl->session->peer_cert->raw.len ) != 0 )
2629 {
2630 SSL_DEBUG_MSG( 1, ( "server cert changed during renegotiation" ) );
2631 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
2632 }
2633 }
2634
Paul Bakker5121ce52009-01-03 21:22:43 +00002635 if( ssl->authmode != SSL_VERIFY_NONE )
2636 {
2637 if( ssl->ca_chain == NULL )
2638 {
2639 SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002640 return( POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002641 }
2642
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002643 /*
2644 * Main check: verify certificate
2645 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02002646 ret = x509_crt_verify( ssl->session_negotiate->peer_cert,
2647 ssl->ca_chain, ssl->ca_crl, ssl->peer_cn,
2648 &ssl->session_negotiate->verify_result,
2649 ssl->f_vrfy, ssl->p_vrfy );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650
2651 if( ret != 0 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002652 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002653 SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002654 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002655
2656 /*
2657 * Secondary checks: always done, but change 'ret' only if it was 0
2658 */
2659
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002660#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002661 {
Paul Bakker93389cc2014-04-17 14:44:38 +02002662 pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002663
2664 /* If certificate uses an EC key, make sure the curve is OK */
2665 if( pk_can_do( pk, POLARSSL_PK_ECKEY ) &&
2666 ! ssl_curve_is_acceptable( ssl, pk_ec( *pk )->grp.id ) )
2667 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002668 SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002669 if( ret == 0 )
2670 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01002671 }
2672 }
Paul Bakker9af723c2014-05-01 13:03:14 +02002673#endif /* POLARSSL_SSL_SET_CURVES */
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002675 if( ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
2676 ciphersuite_info,
2677 ! ssl->endpoint ) != 0 )
2678 {
Manuel Pégourié-Gonnarda9db85d2014-04-09 14:53:05 +02002679 SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02002680 if( ret == 0 )
2681 ret = POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE;
2682 }
2683
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 if( ssl->authmode != SSL_VERIFY_REQUIRED )
2685 ret = 0;
2686 }
2687
2688 SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
2689
2690 return( ret );
2691}
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002692#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED
2693 !POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED
2694 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED
2695 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED
2696 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
2697 !POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED
2698 !POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
2700int ssl_write_change_cipher_spec( ssl_context *ssl )
2701{
2702 int ret;
2703
2704 SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
2705
2706 ssl->out_msgtype = SSL_MSG_CHANGE_CIPHER_SPEC;
2707 ssl->out_msglen = 1;
2708 ssl->out_msg[0] = 1;
2709
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 ssl->state++;
2711
2712 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2713 {
2714 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2715 return( ret );
2716 }
2717
2718 SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
2719
2720 return( 0 );
2721}
2722
2723int ssl_parse_change_cipher_spec( ssl_context *ssl )
2724{
2725 int ret;
2726
2727 SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
2728
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2730 {
2731 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2732 return( ret );
2733 }
2734
2735 if( ssl->in_msgtype != SSL_MSG_CHANGE_CIPHER_SPEC )
2736 {
2737 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002738 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002739 }
2740
2741 if( ssl->in_msglen != 1 || ssl->in_msg[0] != 1 )
2742 {
2743 SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002744 return( POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC );
Paul Bakker5121ce52009-01-03 21:22:43 +00002745 }
2746
2747 ssl->state++;
2748
2749 SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
2750
2751 return( 0 );
2752}
2753
Paul Bakker41c83d32013-03-20 14:39:14 +01002754void ssl_optimize_checksum( ssl_context *ssl,
2755 const ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00002756{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02002757 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01002758
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2760 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker380da532012-04-18 16:10:25 +00002761 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00002762 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00002763 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002764#endif
2765#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2766#if defined(POLARSSL_SHA512_C)
2767 if( ciphersuite_info->mac == POLARSSL_MD_SHA384 )
2768 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
2769 else
2770#endif
2771#if defined(POLARSSL_SHA256_C)
2772 if( ciphersuite_info->mac != POLARSSL_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00002773 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002774 else
2775#endif
2776#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002777 {
2778 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002779 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002780 }
Paul Bakker380da532012-04-18 16:10:25 +00002781}
Paul Bakkerf7abd422013-04-16 13:15:56 +02002782
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002783static void ssl_update_checksum_start( ssl_context *ssl,
2784 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002785{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002786#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2787 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker48916f92012-09-16 19:57:18 +00002788 md5_update( &ssl->handshake->fin_md5 , buf, len );
2789 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002790#endif
2791#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2792#if defined(POLARSSL_SHA256_C)
Paul Bakker9e36f042013-06-30 14:34:05 +02002793 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#endif
Paul Bakker9e36f042013-06-30 14:34:05 +02002795#if defined(POLARSSL_SHA512_C)
2796 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01002797#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002798#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002799}
2800
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002801#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2802 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002803static void ssl_update_checksum_md5sha1( ssl_context *ssl,
2804 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002805{
Paul Bakker48916f92012-09-16 19:57:18 +00002806 md5_update( &ssl->handshake->fin_md5 , buf, len );
2807 sha1_update( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002808}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002809#endif
Paul Bakker380da532012-04-18 16:10:25 +00002810
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002811#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2812#if defined(POLARSSL_SHA256_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002813static void ssl_update_checksum_sha256( ssl_context *ssl,
2814 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002815{
Paul Bakker9e36f042013-06-30 14:34:05 +02002816 sha256_update( &ssl->handshake->fin_sha256, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002817}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002818#endif
Paul Bakker380da532012-04-18 16:10:25 +00002819
Paul Bakker9e36f042013-06-30 14:34:05 +02002820#if defined(POLARSSL_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002821static void ssl_update_checksum_sha384( ssl_context *ssl,
2822 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00002823{
Paul Bakker9e36f042013-06-30 14:34:05 +02002824 sha512_update( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00002825}
Paul Bakker769075d2012-11-24 11:26:46 +01002826#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002827#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00002828
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002829#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002830static void ssl_calc_finished_ssl(
2831 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002832{
Paul Bakker3c2122f2013-06-24 19:03:14 +02002833 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002834 md5_context md5;
2835 sha1_context sha1;
2836
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 unsigned char padbuf[48];
2838 unsigned char md5sum[16];
2839 unsigned char sha1sum[20];
2840
Paul Bakker48916f92012-09-16 19:57:18 +00002841 ssl_session *session = ssl->session_negotiate;
2842 if( !session )
2843 session = ssl->session;
2844
Paul Bakker1ef83d62012-04-11 12:09:53 +00002845 SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
2846
Paul Bakker48916f92012-09-16 19:57:18 +00002847 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2848 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
2850 /*
2851 * SSLv3:
2852 * hash =
2853 * MD5( master + pad2 +
2854 * MD5( handshake + sender + master + pad1 ) )
2855 * + SHA1( master + pad2 +
2856 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 */
2858
Paul Bakker90995b52013-06-24 19:20:35 +02002859#if !defined(POLARSSL_MD5_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002861 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002862#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002863
Paul Bakker90995b52013-06-24 19:20:35 +02002864#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002866 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002867#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Paul Bakker3c2122f2013-06-24 19:03:14 +02002869 sender = ( from == SSL_IS_CLIENT ) ? "CLNT"
2870 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Paul Bakker1ef83d62012-04-11 12:09:53 +00002872 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Paul Bakker3c2122f2013-06-24 19:03:14 +02002874 md5_update( &md5, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002875 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002876 md5_update( &md5, padbuf, 48 );
2877 md5_finish( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002878
Paul Bakker3c2122f2013-06-24 19:03:14 +02002879 sha1_update( &sha1, (const unsigned char *) sender, 4 );
Paul Bakker48916f92012-09-16 19:57:18 +00002880 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002881 sha1_update( &sha1, padbuf, 40 );
2882 sha1_finish( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00002883
Paul Bakker1ef83d62012-04-11 12:09:53 +00002884 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakker1ef83d62012-04-11 12:09:53 +00002886 md5_starts( &md5 );
Paul Bakker48916f92012-09-16 19:57:18 +00002887 md5_update( &md5, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002888 md5_update( &md5, padbuf, 48 );
2889 md5_update( &md5, md5sum, 16 );
2890 md5_finish( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Paul Bakker1ef83d62012-04-11 12:09:53 +00002892 sha1_starts( &sha1 );
Paul Bakker48916f92012-09-16 19:57:18 +00002893 sha1_update( &sha1, session->master, 48 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002894 sha1_update( &sha1, padbuf , 40 );
2895 sha1_update( &sha1, sha1sum, 20 );
2896 sha1_finish( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002897
Paul Bakker1ef83d62012-04-11 12:09:53 +00002898 SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002899
Paul Bakker5b4af392014-06-26 12:09:34 +02002900 md5_free( &md5 );
2901 sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Paul Bakker34617722014-06-13 17:20:13 +02002903 polarssl_zeroize( padbuf, sizeof( padbuf ) );
2904 polarssl_zeroize( md5sum, sizeof( md5sum ) );
2905 polarssl_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
2907 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2908}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002909#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002910
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002911#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002912static void ssl_calc_finished_tls(
2913 ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00002914{
Paul Bakker1ef83d62012-04-11 12:09:53 +00002915 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002916 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002917 md5_context md5;
Paul Bakker5121ce52009-01-03 21:22:43 +00002918 sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002919 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00002920
Paul Bakker48916f92012-09-16 19:57:18 +00002921 ssl_session *session = ssl->session_negotiate;
2922 if( !session )
2923 session = ssl->session;
2924
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakker48916f92012-09-16 19:57:18 +00002927 memcpy( &md5 , &ssl->handshake->fin_md5 , sizeof(md5_context) );
2928 memcpy( &sha1, &ssl->handshake->fin_sha1, sizeof(sha1_context) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002929
Paul Bakker1ef83d62012-04-11 12:09:53 +00002930 /*
2931 * TLSv1:
2932 * hash = PRF( master, finished_label,
2933 * MD5( handshake ) + SHA1( handshake ) )[0..11]
2934 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002935
Paul Bakker90995b52013-06-24 19:20:35 +02002936#if !defined(POLARSSL_MD5_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002937 SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
2938 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002939#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002940
Paul Bakker90995b52013-06-24 19:20:35 +02002941#if !defined(POLARSSL_SHA1_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002942 SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
2943 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002944#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002945
2946 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002947 ? "client finished"
2948 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002949
2950 md5_finish( &md5, padbuf );
2951 sha1_finish( &sha1, padbuf + 16 );
2952
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002953 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00002954 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002955
2956 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
2957
Paul Bakker5b4af392014-06-26 12:09:34 +02002958 md5_free( &md5 );
2959 sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002960
Paul Bakker34617722014-06-13 17:20:13 +02002961 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002962
2963 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
2964}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002965#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002966
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002967#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2968#if defined(POLARSSL_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00002969static void ssl_calc_finished_tls_sha256(
Paul Bakker1ef83d62012-04-11 12:09:53 +00002970 ssl_context *ssl, unsigned char *buf, int from )
2971{
2972 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02002973 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02002974 sha256_context sha256;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002975 unsigned char padbuf[32];
2976
Paul Bakker48916f92012-09-16 19:57:18 +00002977 ssl_session *session = ssl->session_negotiate;
2978 if( !session )
2979 session = ssl->session;
2980
Paul Bakker380da532012-04-18 16:10:25 +00002981 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002982
Paul Bakker9e36f042013-06-30 14:34:05 +02002983 memcpy( &sha256, &ssl->handshake->fin_sha256, sizeof(sha256_context) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002984
2985 /*
2986 * TLSv1.2:
2987 * hash = PRF( master, finished_label,
2988 * Hash( handshake ) )[0.11]
2989 */
2990
Paul Bakker9e36f042013-06-30 14:34:05 +02002991#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker1ef83d62012-04-11 12:09:53 +00002992 SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Paul Bakker9e36f042013-06-30 14:34:05 +02002993 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02002994#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002995
2996 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02002997 ? "client finished"
2998 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00002999
Paul Bakker9e36f042013-06-30 14:34:05 +02003000 sha256_finish( &sha256, padbuf );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003001
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003002 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003003 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003004
3005 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3006
Paul Bakker5b4af392014-06-26 12:09:34 +02003007 sha256_free( &sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003008
Paul Bakker34617722014-06-13 17:20:13 +02003009 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003010
3011 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3012}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003013#endif /* POLARSSL_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003014
Paul Bakker9e36f042013-06-30 14:34:05 +02003015#if defined(POLARSSL_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00003016static void ssl_calc_finished_tls_sha384(
3017 ssl_context *ssl, unsigned char *buf, int from )
3018{
3019 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02003020 const char *sender;
Paul Bakker9e36f042013-06-30 14:34:05 +02003021 sha512_context sha512;
Paul Bakkerca4ab492012-04-18 14:23:57 +00003022 unsigned char padbuf[48];
3023
Paul Bakker48916f92012-09-16 19:57:18 +00003024 ssl_session *session = ssl->session_negotiate;
3025 if( !session )
3026 session = ssl->session;
3027
Paul Bakker380da532012-04-18 16:10:25 +00003028 SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003029
Paul Bakker9e36f042013-06-30 14:34:05 +02003030 memcpy( &sha512, &ssl->handshake->fin_sha512, sizeof(sha512_context) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003031
3032 /*
3033 * TLSv1.2:
3034 * hash = PRF( master, finished_label,
3035 * Hash( handshake ) )[0.11]
3036 */
3037
Paul Bakker9e36f042013-06-30 14:34:05 +02003038#if !defined(POLARSSL_SHA512_ALT)
3039 SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
3040 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02003041#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00003042
3043 sender = ( from == SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02003044 ? "client finished"
3045 : "server finished";
Paul Bakkerca4ab492012-04-18 14:23:57 +00003046
Paul Bakker9e36f042013-06-30 14:34:05 +02003047 sha512_finish( &sha512, padbuf );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003048
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003049 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00003050 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003051
3052 SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
3053
Paul Bakker5b4af392014-06-26 12:09:34 +02003054 sha512_free( &sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003055
Paul Bakker34617722014-06-13 17:20:13 +02003056 polarssl_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00003057
3058 SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
3059}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003060#endif /* POLARSSL_SHA512_C */
3061#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00003062
Paul Bakker48916f92012-09-16 19:57:18 +00003063void ssl_handshake_wrapup( ssl_context *ssl )
3064{
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003065 int resume = ssl->handshake->resume;
3066
Paul Bakker48916f92012-09-16 19:57:18 +00003067 SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
3068
3069 /*
3070 * Free our handshake params
3071 */
3072 ssl_handshake_free( ssl->handshake );
Paul Bakker6e339b52013-07-03 13:37:05 +02003073 polarssl_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00003074 ssl->handshake = NULL;
3075
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003076 if( ssl->renegotiation == SSL_RENEGOTIATION )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003077 {
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003078 ssl->renegotiation = SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003079 ssl->renego_records_seen = 0;
3080 }
Manuel Pégourié-Gonnardcaed0542013-10-30 12:47:35 +01003081
Paul Bakker48916f92012-09-16 19:57:18 +00003082 /*
3083 * Switch in our now active transform context
3084 */
3085 if( ssl->transform )
3086 {
3087 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003088 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003089 }
3090 ssl->transform = ssl->transform_negotiate;
3091 ssl->transform_negotiate = NULL;
3092
Paul Bakker0a597072012-09-25 21:55:46 +00003093 if( ssl->session )
3094 {
3095 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003096 polarssl_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00003097 }
3098 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00003099 ssl->session_negotiate = NULL;
3100
Paul Bakker0a597072012-09-25 21:55:46 +00003101 /*
3102 * Add cache entry
3103 */
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003104 if( ssl->f_set_cache != NULL &&
3105 ssl->session->length != 0 &&
3106 resume == 0 )
3107 {
Paul Bakker0a597072012-09-25 21:55:46 +00003108 if( ssl->f_set_cache( ssl->p_set_cache, ssl->session ) != 0 )
3109 SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02003110 }
Paul Bakker0a597072012-09-25 21:55:46 +00003111
Paul Bakker48916f92012-09-16 19:57:18 +00003112 ssl->state++;
3113
3114 SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
3115}
3116
Paul Bakker1ef83d62012-04-11 12:09:53 +00003117int ssl_write_finished( ssl_context *ssl )
3118{
3119 int ret, hash_len;
3120
3121 SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
3122
Paul Bakker92be97b2013-01-02 17:30:03 +01003123 /*
3124 * Set the out_msg pointer to the correct location based on IV length
3125 */
3126 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3127 {
3128 ssl->out_msg = ssl->out_iv + ssl->transform_negotiate->ivlen -
3129 ssl->transform_negotiate->fixed_ivlen;
3130 }
3131 else
3132 ssl->out_msg = ssl->out_iv;
3133
Paul Bakker48916f92012-09-16 19:57:18 +00003134 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00003135
3136 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003137 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3138
Paul Bakker48916f92012-09-16 19:57:18 +00003139 ssl->verify_data_len = hash_len;
3140 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
3141
Paul Bakker5121ce52009-01-03 21:22:43 +00003142 ssl->out_msglen = 4 + hash_len;
3143 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3144 ssl->out_msg[0] = SSL_HS_FINISHED;
3145
3146 /*
3147 * In case of session resuming, invert the client and server
3148 * ChangeCipherSpec messages order.
3149 */
Paul Bakker0a597072012-09-25 21:55:46 +00003150 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003151 {
3152 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker48916f92012-09-16 19:57:18 +00003153 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003154 else
3155 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3156 }
3157 else
3158 ssl->state++;
3159
Paul Bakker48916f92012-09-16 19:57:18 +00003160 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003161 * Switch to our negotiated transform and session parameters for outbound
3162 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003163 */
3164 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
3165 ssl->transform_out = ssl->transform_negotiate;
3166 ssl->session_out = ssl->session_negotiate;
3167 memset( ssl->out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003168
Paul Bakker07eb38b2012-12-19 14:42:06 +01003169#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003170 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003171 {
3172 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_OUTBOUND ) ) != 0 )
3173 {
3174 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3175 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3176 }
3177 }
3178#endif
3179
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3181 {
3182 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3183 return( ret );
3184 }
3185
3186 SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
3187
3188 return( 0 );
3189}
3190
3191int ssl_parse_finished( ssl_context *ssl )
3192{
Paul Bakker23986e52011-04-24 08:57:21 +00003193 int ret;
3194 unsigned int hash_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003195 unsigned char buf[36];
3196
3197 SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
3198
Paul Bakker48916f92012-09-16 19:57:18 +00003199 ssl->handshake->calc_finished( ssl, buf, ssl->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003200
Paul Bakker48916f92012-09-16 19:57:18 +00003201 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003202 * Switch to our negotiated transform and session parameters for inbound
3203 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00003204 */
3205 SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
3206 ssl->transform_in = ssl->transform_negotiate;
3207 ssl->session_in = ssl->session_negotiate;
3208 memset( ssl->in_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003209
Paul Bakker92be97b2013-01-02 17:30:03 +01003210 /*
3211 * Set the in_msg pointer to the correct location based on IV length
3212 */
3213 if( ssl->minor_ver >= SSL_MINOR_VERSION_2 )
3214 {
3215 ssl->in_msg = ssl->in_iv + ssl->transform_negotiate->ivlen -
3216 ssl->transform_negotiate->fixed_ivlen;
3217 }
3218 else
3219 ssl->in_msg = ssl->in_iv;
3220
Paul Bakker07eb38b2012-12-19 14:42:06 +01003221#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003222 if( ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01003223 {
3224 if( ( ret = ssl_hw_record_activate( ssl, SSL_CHANNEL_INBOUND ) ) != 0 )
3225 {
3226 SSL_DEBUG_RET( 1, "ssl_hw_record_activate", ret );
3227 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3228 }
3229 }
3230#endif
3231
Paul Bakker5121ce52009-01-03 21:22:43 +00003232 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3233 {
3234 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3235 return( ret );
3236 }
3237
3238 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3239 {
3240 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003241 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003242 }
3243
Paul Bakker1ef83d62012-04-11 12:09:53 +00003244 // TODO TLS/1.2 Hash length is determined by cipher suite (Page 63)
Paul Bakker5121ce52009-01-03 21:22:43 +00003245 hash_len = ( ssl->minor_ver == SSL_MINOR_VERSION_0 ) ? 36 : 12;
3246
3247 if( ssl->in_msg[0] != SSL_HS_FINISHED ||
3248 ssl->in_hslen != 4 + hash_len )
3249 {
3250 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003251 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003252 }
3253
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003254 if( safer_memcmp( ssl->in_msg + 4, buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003255 {
3256 SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003257 return( POLARSSL_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003258 }
3259
Paul Bakker48916f92012-09-16 19:57:18 +00003260 ssl->verify_data_len = hash_len;
3261 memcpy( ssl->peer_verify_data, buf, hash_len );
3262
Paul Bakker0a597072012-09-25 21:55:46 +00003263 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003264 {
3265 if( ssl->endpoint == SSL_IS_CLIENT )
3266 ssl->state = SSL_CLIENT_CHANGE_CIPHER_SPEC;
3267
3268 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker48916f92012-09-16 19:57:18 +00003269 ssl->state = SSL_HANDSHAKE_WRAPUP;
Paul Bakker5121ce52009-01-03 21:22:43 +00003270 }
3271 else
3272 ssl->state++;
3273
3274 SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
3275
3276 return( 0 );
3277}
3278
Paul Bakker968afaa2014-07-09 11:09:24 +02003279static void ssl_handshake_params_init( ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003280{
3281 memset( handshake, 0, sizeof( ssl_handshake_params ) );
3282
3283#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3284 defined(POLARSSL_SSL_PROTO_TLS1_1)
3285 md5_init( &handshake->fin_md5 );
3286 sha1_init( &handshake->fin_sha1 );
3287 md5_starts( &handshake->fin_md5 );
3288 sha1_starts( &handshake->fin_sha1 );
3289#endif
3290#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3291#if defined(POLARSSL_SHA256_C)
3292 sha256_init( &handshake->fin_sha256 );
3293 sha256_starts( &handshake->fin_sha256, 0 );
3294#endif
3295#if defined(POLARSSL_SHA512_C)
3296 sha512_init( &handshake->fin_sha512 );
3297 sha512_starts( &handshake->fin_sha512, 1 );
3298#endif
3299#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3300
3301 handshake->update_checksum = ssl_update_checksum_start;
3302 handshake->sig_alg = SSL_HASH_SHA1;
3303
3304#if defined(POLARSSL_DHM_C)
3305 dhm_init( &handshake->dhm_ctx );
3306#endif
3307#if defined(POLARSSL_ECDH_C)
3308 ecdh_init( &handshake->ecdh_ctx );
3309#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003310}
3311
3312static void ssl_transform_init( ssl_transform *transform )
3313{
3314 memset( transform, 0, sizeof(ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003315
3316 cipher_init( &transform->cipher_ctx_enc );
3317 cipher_init( &transform->cipher_ctx_dec );
3318
3319 md_init( &transform->md_ctx_enc );
3320 md_init( &transform->md_ctx_dec );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003321}
3322
3323void ssl_session_init( ssl_session *session )
3324{
3325 memset( session, 0, sizeof(ssl_session) );
3326}
3327
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02003328static int ssl_handshake_init( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00003329{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003330 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00003331 if( ssl->transform_negotiate )
3332 ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003333 if( ssl->session_negotiate )
3334 ssl_session_free( ssl->session_negotiate );
3335 if( ssl->handshake )
3336 ssl_handshake_free( ssl->handshake );
3337
3338 /*
3339 * Either the pointers are now NULL or cleared properly and can be freed.
3340 * Now allocate missing structures.
3341 */
3342 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003343 {
3344 ssl->transform_negotiate =
3345 (ssl_transform *) polarssl_malloc( sizeof(ssl_transform) );
3346 }
Paul Bakker48916f92012-09-16 19:57:18 +00003347
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003348 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003349 {
3350 ssl->session_negotiate =
3351 (ssl_session *) polarssl_malloc( sizeof(ssl_session) );
3352 }
Paul Bakker48916f92012-09-16 19:57:18 +00003353
Paul Bakker82788fb2014-10-20 13:59:19 +02003354 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003355 {
3356 ssl->handshake = (ssl_handshake_params *)
3357 polarssl_malloc( sizeof(ssl_handshake_params) );
3358 }
Paul Bakker48916f92012-09-16 19:57:18 +00003359
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003360 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00003361 if( ssl->handshake == NULL ||
3362 ssl->transform_negotiate == NULL ||
3363 ssl->session_negotiate == NULL )
3364 {
3365 SSL_DEBUG_MSG( 1, ( "malloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003366
3367 polarssl_free( ssl->handshake );
3368 polarssl_free( ssl->transform_negotiate );
3369 polarssl_free( ssl->session_negotiate );
3370
3371 ssl->handshake = NULL;
3372 ssl->transform_negotiate = NULL;
3373 ssl->session_negotiate = NULL;
3374
Paul Bakker48916f92012-09-16 19:57:18 +00003375 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3376 }
3377
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003378 /* Initialize structures */
3379 ssl_session_init( ssl->session_negotiate );
3380 ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02003381 ssl_handshake_params_init( ssl->handshake );
3382
3383#if defined(POLARSSL_X509_CRT_PARSE_C)
3384 ssl->handshake->key_cert = ssl->key_cert;
3385#endif
Manuel Pégourié-Gonnarde5e1bb92013-10-30 11:25:30 +01003386
Paul Bakker48916f92012-09-16 19:57:18 +00003387 return( 0 );
3388}
3389
Paul Bakker5121ce52009-01-03 21:22:43 +00003390/*
3391 * Initialize an SSL context
3392 */
3393int ssl_init( ssl_context *ssl )
3394{
Paul Bakker48916f92012-09-16 19:57:18 +00003395 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 int len = SSL_BUFFER_LEN;
3397
3398 memset( ssl, 0, sizeof( ssl_context ) );
3399
Paul Bakker62f2dee2012-09-28 07:31:51 +00003400 /*
3401 * Sane defaults
3402 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003403 ssl->min_major_ver = SSL_MIN_MAJOR_VERSION;
3404 ssl->min_minor_ver = SSL_MIN_MINOR_VERSION;
3405 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
3406 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003407
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003408 ssl_set_ciphersuites( ssl, ssl_list_ciphersuites() );
Paul Bakker645ce3a2012-10-31 12:32:41 +00003409
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003410 ssl->renego_max_records = SSL_RENEGO_MAX_RECORDS_DEFAULT;
3411
Paul Bakker62f2dee2012-09-28 07:31:51 +00003412#if defined(POLARSSL_DHM_C)
3413 if( ( ret = mpi_read_string( &ssl->dhm_P, 16,
3414 POLARSSL_DHM_RFC5114_MODP_1024_P) ) != 0 ||
3415 ( ret = mpi_read_string( &ssl->dhm_G, 16,
3416 POLARSSL_DHM_RFC5114_MODP_1024_G) ) != 0 )
3417 {
3418 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3419 return( ret );
3420 }
3421#endif
3422
3423 /*
3424 * Prepare base structures
3425 */
Paul Bakker6e339b52013-07-03 13:37:05 +02003426 ssl->in_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003427 ssl->in_hdr = ssl->in_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003428 ssl->in_iv = ssl->in_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 ssl->in_msg = ssl->in_ctr + 13;
3430
3431 if( ssl->in_ctr == NULL )
3432 {
3433 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker69e095c2011-12-10 21:55:01 +00003434 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003435 }
3436
Paul Bakker6e339b52013-07-03 13:37:05 +02003437 ssl->out_ctr = (unsigned char *) polarssl_malloc( len );
Paul Bakker5121ce52009-01-03 21:22:43 +00003438 ssl->out_hdr = ssl->out_ctr + 8;
Paul Bakker92be97b2013-01-02 17:30:03 +01003439 ssl->out_iv = ssl->out_ctr + 13;
Paul Bakker5bd42292012-12-19 14:40:42 +01003440 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker5121ce52009-01-03 21:22:43 +00003441
3442 if( ssl->out_ctr == NULL )
3443 {
3444 SSL_DEBUG_MSG( 1, ( "malloc(%d bytes) failed", len ) );
Paul Bakker3d6504a2014-03-17 13:41:51 +01003445 polarssl_free( ssl->in_ctr );
3446 ssl->in_ctr = NULL;
Paul Bakker69e095c2011-12-10 21:55:01 +00003447 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 }
3449
3450 memset( ssl-> in_ctr, 0, SSL_BUFFER_LEN );
3451 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3452
Paul Bakker606b4ba2013-08-14 16:52:14 +02003453#if defined(POLARSSL_SSL_SESSION_TICKETS)
3454 ssl->ticket_lifetime = SSL_DEFAULT_TICKET_LIFETIME;
3455#endif
3456
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003457#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +01003458 ssl->curve_list = ecp_grp_id_list( );
Gergely Budai987bfb52014-01-19 21:48:42 +01003459#endif
3460
Paul Bakker48916f92012-09-16 19:57:18 +00003461 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3462 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003463
3464 return( 0 );
3465}
3466
3467/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00003468 * Reset an initialized and used SSL context for re-use while retaining
3469 * all application-set variables, function pointers and data.
3470 */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003471int ssl_session_reset( ssl_context *ssl )
Paul Bakker7eb013f2011-10-06 12:37:39 +00003472{
Paul Bakker48916f92012-09-16 19:57:18 +00003473 int ret;
3474
Paul Bakker7eb013f2011-10-06 12:37:39 +00003475 ssl->state = SSL_HELLO_REQUEST;
Paul Bakker48916f92012-09-16 19:57:18 +00003476 ssl->renegotiation = SSL_INITIAL_HANDSHAKE;
3477 ssl->secure_renegotiation = SSL_LEGACY_RENEGOTIATION;
3478
3479 ssl->verify_data_len = 0;
3480 memset( ssl->own_verify_data, 0, 36 );
3481 memset( ssl->peer_verify_data, 0, 36 );
3482
Paul Bakker7eb013f2011-10-06 12:37:39 +00003483 ssl->in_offt = NULL;
3484
Paul Bakker92be97b2013-01-02 17:30:03 +01003485 ssl->in_msg = ssl->in_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003486 ssl->in_msgtype = 0;
3487 ssl->in_msglen = 0;
3488 ssl->in_left = 0;
3489
3490 ssl->in_hslen = 0;
3491 ssl->nb_zero = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003492 ssl->record_read = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003493
Paul Bakker92be97b2013-01-02 17:30:03 +01003494 ssl->out_msg = ssl->out_ctr + 13;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003495 ssl->out_msgtype = 0;
3496 ssl->out_msglen = 0;
3497 ssl->out_left = 0;
3498
Paul Bakker48916f92012-09-16 19:57:18 +00003499 ssl->transform_in = NULL;
3500 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00003501
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003502 ssl->renego_records_seen = 0;
3503
Paul Bakker7eb013f2011-10-06 12:37:39 +00003504 memset( ssl->out_ctr, 0, SSL_BUFFER_LEN );
3505 memset( ssl->in_ctr, 0, SSL_BUFFER_LEN );
Paul Bakker05ef8352012-05-08 09:17:57 +00003506
3507#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003508 if( ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00003509 {
3510 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_reset()" ) );
Paul Bakker07eb38b2012-12-19 14:42:06 +01003511 if( ( ret = ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003512 {
3513 SSL_DEBUG_RET( 1, "ssl_hw_record_reset", ret );
3514 return( POLARSSL_ERR_SSL_HW_ACCEL_FAILED );
3515 }
Paul Bakker05ef8352012-05-08 09:17:57 +00003516 }
3517#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00003518
Paul Bakker48916f92012-09-16 19:57:18 +00003519 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003520 {
Paul Bakker48916f92012-09-16 19:57:18 +00003521 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02003522 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00003523 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003524 }
Paul Bakker48916f92012-09-16 19:57:18 +00003525
Paul Bakkerc0463502013-02-14 11:19:38 +01003526 if( ssl->session )
3527 {
3528 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02003529 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01003530 ssl->session = NULL;
3531 }
3532
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003533#if defined(POLARSSL_SSL_ALPN)
3534 ssl->alpn_chosen = NULL;
3535#endif
3536
Paul Bakker48916f92012-09-16 19:57:18 +00003537 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
3538 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003539
3540 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00003541}
3542
Paul Bakkera503a632013-08-14 13:48:06 +02003543#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003544static void ssl_ticket_keys_free( ssl_ticket_keys *tkeys )
3545{
3546 aes_free( &tkeys->enc );
3547 aes_free( &tkeys->dec );
3548
3549 polarssl_zeroize( tkeys, sizeof(ssl_ticket_keys) );
3550}
3551
Paul Bakker7eb013f2011-10-06 12:37:39 +00003552/*
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003553 * Allocate and initialize ticket keys
3554 */
3555static int ssl_ticket_keys_init( ssl_context *ssl )
3556{
3557 int ret;
3558 ssl_ticket_keys *tkeys;
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003559 unsigned char buf[16];
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003560
3561 if( ssl->ticket_keys != NULL )
3562 return( 0 );
3563
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003564 tkeys = (ssl_ticket_keys *) polarssl_malloc( sizeof(ssl_ticket_keys) );
3565 if( tkeys == NULL )
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003566 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3567
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003568 aes_init( &tkeys->enc );
3569 aes_init( &tkeys->dec );
3570
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003571 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->key_name, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003572 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003573 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003574 polarssl_free( tkeys );
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003575 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003576 }
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003577
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003578 if( ( ret = ssl->f_rng( ssl->p_rng, buf, 16 ) ) != 0 ||
3579 ( ret = aes_setkey_enc( &tkeys->enc, buf, 128 ) ) != 0 ||
3580 ( ret = aes_setkey_dec( &tkeys->dec, buf, 128 ) ) != 0 )
3581 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003582 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003583 polarssl_free( tkeys );
3584 return( ret );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003585 }
3586
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003587 if( ( ret = ssl->f_rng( ssl->p_rng, tkeys->mac_key, 16 ) ) != 0 )
Paul Bakker6f0636a2013-12-16 15:24:05 +01003588 {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02003589 ssl_ticket_keys_free( tkeys );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003590 polarssl_free( tkeys );
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003591 return( ret );
Paul Bakker6f0636a2013-12-16 15:24:05 +01003592 }
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +02003593
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003594 ssl->ticket_keys = tkeys;
3595
3596 return( 0 );
3597}
Paul Bakkera503a632013-08-14 13:48:06 +02003598#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02003599
3600/*
Paul Bakker5121ce52009-01-03 21:22:43 +00003601 * SSL set accessors
3602 */
3603void ssl_set_endpoint( ssl_context *ssl, int endpoint )
3604{
3605 ssl->endpoint = endpoint;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003606
Paul Bakker606b4ba2013-08-14 16:52:14 +02003607#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003608 if( endpoint == SSL_IS_CLIENT )
3609 ssl->session_tickets = SSL_SESSION_TICKETS_ENABLED;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003611}
3612
3613void ssl_set_authmode( ssl_context *ssl, int authmode )
3614{
3615 ssl->authmode = authmode;
3616}
3617
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003618#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003619void ssl_set_verify( ssl_context *ssl,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003620 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003621 void *p_vrfy )
3622{
3623 ssl->f_vrfy = f_vrfy;
3624 ssl->p_vrfy = p_vrfy;
3625}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003626#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00003627
Paul Bakker5121ce52009-01-03 21:22:43 +00003628void ssl_set_rng( ssl_context *ssl,
Paul Bakkera3d195c2011-11-27 21:07:34 +00003629 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00003630 void *p_rng )
3631{
3632 ssl->f_rng = f_rng;
3633 ssl->p_rng = p_rng;
3634}
3635
3636void ssl_set_dbg( ssl_context *ssl,
Paul Bakkerff60ee62010-03-16 21:09:09 +00003637 void (*f_dbg)(void *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00003638 void *p_dbg )
3639{
3640 ssl->f_dbg = f_dbg;
3641 ssl->p_dbg = p_dbg;
3642}
3643
3644void ssl_set_bio( ssl_context *ssl,
Paul Bakker23986e52011-04-24 08:57:21 +00003645 int (*f_recv)(void *, unsigned char *, size_t), void *p_recv,
Paul Bakker39bb4182011-06-21 07:36:43 +00003646 int (*f_send)(void *, const unsigned char *, size_t), void *p_send )
Paul Bakker5121ce52009-01-03 21:22:43 +00003647{
3648 ssl->f_recv = f_recv;
3649 ssl->f_send = f_send;
3650 ssl->p_recv = p_recv;
3651 ssl->p_send = p_send;
3652}
3653
Paul Bakker0a597072012-09-25 21:55:46 +00003654void ssl_set_session_cache( ssl_context *ssl,
3655 int (*f_get_cache)(void *, ssl_session *), void *p_get_cache,
3656 int (*f_set_cache)(void *, const ssl_session *), void *p_set_cache )
Paul Bakker5121ce52009-01-03 21:22:43 +00003657{
Paul Bakker0a597072012-09-25 21:55:46 +00003658 ssl->f_get_cache = f_get_cache;
3659 ssl->p_get_cache = p_get_cache;
3660 ssl->f_set_cache = f_set_cache;
3661 ssl->p_set_cache = p_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00003662}
3663
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003664int ssl_set_session( ssl_context *ssl, const ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00003665{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003666 int ret;
3667
3668 if( ssl == NULL ||
3669 session == NULL ||
3670 ssl->session_negotiate == NULL ||
3671 ssl->endpoint != SSL_IS_CLIENT )
3672 {
3673 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3674 }
3675
3676 if( ( ret = ssl_session_copy( ssl->session_negotiate, session ) ) != 0 )
3677 return( ret );
3678
Paul Bakker0a597072012-09-25 21:55:46 +00003679 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02003680
3681 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003682}
3683
Paul Bakkerb68cad62012-08-23 08:34:18 +00003684void ssl_set_ciphersuites( ssl_context *ssl, const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00003685{
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003686 ssl->ciphersuite_list[SSL_MINOR_VERSION_0] = ciphersuites;
3687 ssl->ciphersuite_list[SSL_MINOR_VERSION_1] = ciphersuites;
3688 ssl->ciphersuite_list[SSL_MINOR_VERSION_2] = ciphersuites;
3689 ssl->ciphersuite_list[SSL_MINOR_VERSION_3] = ciphersuites;
3690}
3691
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003692void ssl_set_ciphersuites_for_version( ssl_context *ssl,
3693 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02003694 int major, int minor )
3695{
3696 if( major != SSL_MAJOR_VERSION_3 )
3697 return;
3698
3699 if( minor < SSL_MINOR_VERSION_0 || minor > SSL_MINOR_VERSION_3 )
3700 return;
3701
3702 ssl->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00003703}
3704
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003705#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003706/* Add a new (empty) key_cert entry an return a pointer to it */
3707static ssl_key_cert *ssl_add_key_cert( ssl_context *ssl )
3708{
3709 ssl_key_cert *key_cert, *last;
3710
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003711 key_cert = (ssl_key_cert *) polarssl_malloc( sizeof(ssl_key_cert) );
3712 if( key_cert == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003713 return( NULL );
3714
3715 memset( key_cert, 0, sizeof( ssl_key_cert ) );
3716
3717 /* Append the new key_cert to the (possibly empty) current list */
3718 if( ssl->key_cert == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01003719 {
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003720 ssl->key_cert = key_cert;
Paul Bakker08b028f2013-11-19 10:42:37 +01003721 if( ssl->handshake != NULL )
3722 ssl->handshake->key_cert = key_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01003723 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003724 else
3725 {
3726 last = ssl->key_cert;
3727 while( last->next != NULL )
3728 last = last->next;
3729 last->next = key_cert;
3730 }
3731
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003732 return( key_cert );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003733}
3734
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003735void ssl_set_ca_chain( ssl_context *ssl, x509_crt *ca_chain,
Paul Bakker57b79142010-03-24 06:51:15 +00003736 x509_crl *ca_crl, const char *peer_cn )
Paul Bakker5121ce52009-01-03 21:22:43 +00003737{
3738 ssl->ca_chain = ca_chain;
Paul Bakker40ea7de2009-05-03 10:18:48 +00003739 ssl->ca_crl = ca_crl;
Paul Bakker5121ce52009-01-03 21:22:43 +00003740 ssl->peer_cn = peer_cn;
3741}
3742
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003743int ssl_set_own_cert( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003744 pk_context *pk_key )
Paul Bakker5121ce52009-01-03 21:22:43 +00003745{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003746 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
3747
3748 if( key_cert == NULL )
3749 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3750
3751 key_cert->cert = own_cert;
3752 key_cert->key = pk_key;
3753
3754 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755}
3756
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003757#if defined(POLARSSL_RSA_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003758int ssl_set_own_cert_rsa( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003759 rsa_context *rsa_key )
Paul Bakker43b7e352011-01-18 15:27:19 +00003760{
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003761 int ret;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003762 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003763
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003764 if( key_cert == NULL )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003765 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3766
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003767 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3768 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003769 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003770
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003771 pk_init( key_cert->key );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003772
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003773 ret = pk_init_ctx( key_cert->key, pk_info_from_type( POLARSSL_PK_RSA ) );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003774 if( ret != 0 )
3775 return( ret );
3776
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003777 if( ( ret = rsa_copy( pk_rsa( *key_cert->key ), rsa_key ) ) != 0 )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003778 return( ret );
3779
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003780 key_cert->cert = own_cert;
3781 key_cert->key_own_alloc = 1;
3782
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003783 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003784}
Manuel Pégourié-Gonnardac755232013-08-19 14:10:16 +02003785#endif /* POLARSSL_RSA_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003786
Paul Bakkerc559c7a2013-09-18 14:13:26 +02003787int ssl_set_own_cert_alt( ssl_context *ssl, x509_crt *own_cert,
Manuel Pégourié-Gonnard2fb15f62013-08-22 17:54:20 +02003788 void *rsa_key,
3789 rsa_decrypt_func rsa_decrypt,
3790 rsa_sign_func rsa_sign,
3791 rsa_key_len_func rsa_key_len )
Paul Bakker43b7e352011-01-18 15:27:19 +00003792{
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003793 int ret;
3794 ssl_key_cert *key_cert = ssl_add_key_cert( ssl );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003795
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003796 if( key_cert == NULL )
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003797 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3798
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003799 key_cert->key = (pk_context *) polarssl_malloc( sizeof(pk_context) );
3800 if( key_cert->key == NULL )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003801 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003802
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003803 pk_init( key_cert->key );
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003804
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003805 if( ( ret = pk_init_ctx_rsa_alt( key_cert->key, rsa_key,
3806 rsa_decrypt, rsa_sign, rsa_key_len ) ) != 0 )
3807 return( ret );
3808
3809 key_cert->cert = own_cert;
3810 key_cert->key_own_alloc = 1;
3811
3812 return( 0 );
Paul Bakker43b7e352011-01-18 15:27:19 +00003813}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003814#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00003815
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003816#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02003817int ssl_set_psk( ssl_context *ssl, const unsigned char *psk, size_t psk_len,
3818 const unsigned char *psk_identity, size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003819{
Paul Bakker6db455e2013-09-18 17:29:31 +02003820 if( psk == NULL || psk_identity == NULL )
3821 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3822
Manuel Pégourié-Gonnard481fcfd2014-07-03 16:12:50 +02003823 if( psk_len > POLARSSL_PSK_MAX_LEN )
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003824 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3825
Paul Bakker6db455e2013-09-18 17:29:31 +02003826 if( ssl->psk != NULL )
3827 {
3828 polarssl_free( ssl->psk );
3829 polarssl_free( ssl->psk_identity );
3830 }
3831
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003832 ssl->psk_len = psk_len;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003833 ssl->psk_identity_len = psk_identity_len;
Paul Bakker6db455e2013-09-18 17:29:31 +02003834
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02003835 ssl->psk = (unsigned char *) polarssl_malloc( ssl->psk_len );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003836 ssl->psk_identity = (unsigned char *)
3837 polarssl_malloc( ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02003838
3839 if( ssl->psk == NULL || ssl->psk_identity == NULL )
3840 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3841
3842 memcpy( ssl->psk, psk, ssl->psk_len );
3843 memcpy( ssl->psk_identity, psk_identity, ssl->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02003844
3845 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02003846}
3847
3848void ssl_set_psk_cb( ssl_context *ssl,
3849 int (*f_psk)(void *, ssl_context *, const unsigned char *,
3850 size_t),
3851 void *p_psk )
3852{
3853 ssl->f_psk = f_psk;
3854 ssl->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02003855}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003856#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00003857
Paul Bakker48916f92012-09-16 19:57:18 +00003858#if defined(POLARSSL_DHM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003859int ssl_set_dh_param( ssl_context *ssl, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860{
3861 int ret;
3862
Paul Bakker48916f92012-09-16 19:57:18 +00003863 if( ( ret = mpi_read_string( &ssl->dhm_P, 16, dhm_P ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003864 {
3865 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3866 return( ret );
3867 }
3868
Paul Bakker48916f92012-09-16 19:57:18 +00003869 if( ( ret = mpi_read_string( &ssl->dhm_G, 16, dhm_G ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 {
3871 SSL_DEBUG_RET( 1, "mpi_read_string", ret );
3872 return( ret );
3873 }
3874
3875 return( 0 );
3876}
3877
Paul Bakker1b57b062011-01-06 15:48:19 +00003878int ssl_set_dh_param_ctx( ssl_context *ssl, dhm_context *dhm_ctx )
3879{
3880 int ret;
3881
Paul Bakker66d5d072014-06-17 16:39:18 +02003882 if( ( ret = mpi_copy( &ssl->dhm_P, &dhm_ctx->P ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003883 {
3884 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3885 return( ret );
3886 }
3887
Paul Bakker66d5d072014-06-17 16:39:18 +02003888 if( ( ret = mpi_copy( &ssl->dhm_G, &dhm_ctx->G ) ) != 0 )
Paul Bakker1b57b062011-01-06 15:48:19 +00003889 {
3890 SSL_DEBUG_RET( 1, "mpi_copy", ret );
3891 return( ret );
3892 }
3893
3894 return( 0 );
3895}
Paul Bakker48916f92012-09-16 19:57:18 +00003896#endif /* POLARSSL_DHM_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00003897
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01003898#if defined(POLARSSL_SSL_SET_CURVES)
3899/*
3900 * Set the allowed elliptic curves
3901 */
3902void ssl_set_curves( ssl_context *ssl, const ecp_group_id *curve_list )
3903{
3904 ssl->curve_list = curve_list;
3905}
3906#endif
3907
Paul Bakker0be444a2013-08-27 21:55:01 +02003908#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerff60ee62010-03-16 21:09:09 +00003909int ssl_set_hostname( ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00003910{
3911 if( hostname == NULL )
Paul Bakker40e46942009-01-03 21:51:57 +00003912 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003913
3914 ssl->hostname_len = strlen( hostname );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02003915
3916 if( ssl->hostname_len + 1 == 0 )
3917 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3918
Paul Bakker6e339b52013-07-03 13:37:05 +02003919 ssl->hostname = (unsigned char *) polarssl_malloc( ssl->hostname_len + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003920
Paul Bakkerb15b8512012-01-13 13:44:06 +00003921 if( ssl->hostname == NULL )
3922 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
3923
Paul Bakker3c2122f2013-06-24 19:03:14 +02003924 memcpy( ssl->hostname, (const unsigned char *) hostname,
Paul Bakker5121ce52009-01-03 21:22:43 +00003925 ssl->hostname_len );
Paul Bakkerf7abd422013-04-16 13:15:56 +02003926
Paul Bakker40ea7de2009-05-03 10:18:48 +00003927 ssl->hostname[ssl->hostname_len] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +00003928
3929 return( 0 );
3930}
3931
Paul Bakker5701cdc2012-09-27 21:49:42 +00003932void ssl_set_sni( ssl_context *ssl,
3933 int (*f_sni)(void *, ssl_context *,
3934 const unsigned char *, size_t),
3935 void *p_sni )
3936{
3937 ssl->f_sni = f_sni;
3938 ssl->p_sni = p_sni;
3939}
Paul Bakker0be444a2013-08-27 21:55:01 +02003940#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003941
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003942#if defined(POLARSSL_SSL_ALPN)
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003943int ssl_set_alpn_protocols( ssl_context *ssl, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003944{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003945 size_t cur_len, tot_len;
3946 const char **p;
3947
3948 /*
3949 * "Empty strings MUST NOT be included and byte strings MUST NOT be
3950 * truncated". Check lengths now rather than later.
3951 */
3952 tot_len = 0;
3953 for( p = protos; *p != NULL; p++ )
3954 {
3955 cur_len = strlen( *p );
3956 tot_len += cur_len;
3957
3958 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
3959 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3960 }
3961
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003962 ssl->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003963
3964 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003965}
3966
3967const char *ssl_get_alpn_protocol( const ssl_context *ssl )
3968{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02003969 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003970}
3971#endif /* POLARSSL_SSL_ALPN */
3972
Paul Bakker490ecc82011-10-06 13:04:09 +00003973void ssl_set_max_version( ssl_context *ssl, int major, int minor )
3974{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003975 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3976 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3977 {
3978 ssl->max_major_ver = major;
3979 ssl->max_minor_ver = minor;
3980 }
Paul Bakker490ecc82011-10-06 13:04:09 +00003981}
3982
Paul Bakker1d29fb52012-09-28 13:28:45 +00003983void ssl_set_min_version( ssl_context *ssl, int major, int minor )
3984{
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003985 if( major >= SSL_MIN_MAJOR_VERSION && major <= SSL_MAX_MAJOR_VERSION &&
3986 minor >= SSL_MIN_MINOR_VERSION && minor <= SSL_MAX_MINOR_VERSION )
3987 {
3988 ssl->min_major_ver = major;
3989 ssl->min_minor_ver = minor;
3990 }
Paul Bakker1d29fb52012-09-28 13:28:45 +00003991}
3992
Paul Bakker05decb22013-08-15 13:33:48 +02003993#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003994int ssl_set_max_frag_len( ssl_context *ssl, unsigned char mfl_code )
3995{
Paul Bakker77e257e2013-12-16 15:29:52 +01003996 if( mfl_code >= SSL_MAX_FRAG_LEN_INVALID ||
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003997 mfl_code_to_length[mfl_code] > SSL_MAX_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003998 {
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02003999 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004000 }
4001
4002 ssl->mfl_code = mfl_code;
4003
4004 return( 0 );
4005}
Paul Bakker05decb22013-08-15 13:33:48 +02004006#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02004007
Paul Bakker1f2bc622013-08-15 13:45:55 +02004008#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Paul Bakker8c1ede62013-07-19 14:14:37 +02004009int ssl_set_truncated_hmac( ssl_context *ssl, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004010{
4011 if( ssl->endpoint != SSL_IS_CLIENT )
4012 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4013
Paul Bakker8c1ede62013-07-19 14:14:37 +02004014 ssl->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004015
4016 return( 0 );
4017}
Paul Bakker1f2bc622013-08-15 13:45:55 +02004018#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02004019
Paul Bakker48916f92012-09-16 19:57:18 +00004020void ssl_set_renegotiation( ssl_context *ssl, int renegotiation )
4021{
4022 ssl->disable_renegotiation = renegotiation;
4023}
4024
4025void ssl_legacy_renegotiation( ssl_context *ssl, int allow_legacy )
4026{
4027 ssl->allow_legacy_renegotiation = allow_legacy;
4028}
4029
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004030void ssl_set_renegotiation_enforced( ssl_context *ssl, int max_records )
4031{
4032 ssl->renego_max_records = max_records;
4033}
4034
Paul Bakkera503a632013-08-14 13:48:06 +02004035#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004036int ssl_set_session_tickets( ssl_context *ssl, int use_tickets )
4037{
4038 ssl->session_tickets = use_tickets;
4039
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004040 if( ssl->endpoint == SSL_IS_CLIENT )
4041 return( 0 );
4042
4043 if( ssl->f_rng == NULL )
4044 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4045
4046 return( ssl_ticket_keys_init( ssl ) );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004047}
Paul Bakker606b4ba2013-08-14 16:52:14 +02004048
4049void ssl_set_session_ticket_lifetime( ssl_context *ssl, int lifetime )
4050{
4051 ssl->ticket_lifetime = lifetime;
4052}
Paul Bakkera503a632013-08-14 13:48:06 +02004053#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02004054
Paul Bakker5121ce52009-01-03 21:22:43 +00004055/*
4056 * SSL get accessors
4057 */
Paul Bakker23986e52011-04-24 08:57:21 +00004058size_t ssl_get_bytes_avail( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004059{
4060 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
4061}
4062
Paul Bakkerff60ee62010-03-16 21:09:09 +00004063int ssl_get_verify_result( const ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004064{
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +02004065 return( ssl->session->verify_result );
Paul Bakker5121ce52009-01-03 21:22:43 +00004066}
4067
Paul Bakkere3166ce2011-01-27 17:40:50 +00004068const char *ssl_get_ciphersuite( const ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00004069{
Paul Bakker926c8e42013-03-06 10:23:34 +01004070 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004071 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01004072
Paul Bakkere3166ce2011-01-27 17:40:50 +00004073 return ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00004074}
4075
Paul Bakker43ca69c2011-01-15 17:35:19 +00004076const char *ssl_get_version( const ssl_context *ssl )
4077{
4078 switch( ssl->minor_ver )
4079 {
4080 case SSL_MINOR_VERSION_0:
4081 return( "SSLv3.0" );
4082
4083 case SSL_MINOR_VERSION_1:
4084 return( "TLSv1.0" );
4085
4086 case SSL_MINOR_VERSION_2:
4087 return( "TLSv1.1" );
4088
Paul Bakker1ef83d62012-04-11 12:09:53 +00004089 case SSL_MINOR_VERSION_3:
4090 return( "TLSv1.2" );
4091
Paul Bakker43ca69c2011-01-15 17:35:19 +00004092 default:
4093 break;
4094 }
4095 return( "unknown" );
4096}
4097
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004098#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkerc559c7a2013-09-18 14:13:26 +02004099const x509_crt *ssl_get_peer_cert( const ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00004100{
4101 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004102 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004103
Paul Bakkerd8bb8262014-06-17 14:06:49 +02004104 return( ssl->session->peer_cert );
Paul Bakkerb0550d92012-10-30 07:51:03 +00004105}
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004106#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00004107
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004108int ssl_get_session( const ssl_context *ssl, ssl_session *dst )
4109{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004110 if( ssl == NULL ||
4111 dst == NULL ||
4112 ssl->session == NULL ||
4113 ssl->endpoint != SSL_IS_CLIENT )
4114 {
4115 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4116 }
4117
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02004118 return( ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02004119}
4120
Paul Bakker5121ce52009-01-03 21:22:43 +00004121/*
Paul Bakker1961b702013-01-25 14:49:24 +01004122 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00004123 */
Paul Bakker1961b702013-01-25 14:49:24 +01004124int ssl_handshake_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004125{
Paul Bakker40e46942009-01-03 21:51:57 +00004126 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00004127
Paul Bakker40e46942009-01-03 21:51:57 +00004128#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004129 if( ssl->endpoint == SSL_IS_CLIENT )
Paul Bakker1961b702013-01-25 14:49:24 +01004130 ret = ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004131#endif
4132
Paul Bakker40e46942009-01-03 21:51:57 +00004133#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 if( ssl->endpoint == SSL_IS_SERVER )
Paul Bakker1961b702013-01-25 14:49:24 +01004135 ret = ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00004136#endif
4137
Paul Bakker1961b702013-01-25 14:49:24 +01004138 return( ret );
4139}
4140
4141/*
4142 * Perform the SSL handshake
4143 */
4144int ssl_handshake( ssl_context *ssl )
4145{
4146 int ret = 0;
4147
4148 SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
4149
4150 while( ssl->state != SSL_HANDSHAKE_OVER )
4151 {
4152 ret = ssl_handshake_step( ssl );
4153
4154 if( ret != 0 )
4155 break;
4156 }
4157
Paul Bakker5121ce52009-01-03 21:22:43 +00004158 SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
4159
4160 return( ret );
4161}
4162
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004163#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00004164/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004165 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00004166 */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004167static int ssl_write_hello_request( ssl_context *ssl )
4168{
4169 int ret;
4170
4171 SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
4172
4173 ssl->out_msglen = 4;
4174 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
4175 ssl->out_msg[0] = SSL_HS_HELLO_REQUEST;
4176
4177 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4178 {
4179 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4180 return( ret );
4181 }
4182
4183 SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
4184
4185 return( 0 );
4186}
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004187#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004188
4189/*
4190 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02004191 * - any side: calling ssl_renegotiate(),
4192 * - client: receiving a HelloRequest during ssl_read(),
4193 * - server: receiving any handshake message on server during ssl_read() after
4194 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004195 * If the handshake doesn't complete due to waiting for I/O, it will continue
4196 * during the next calls to ssl_renegotiate() or ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004197 */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004198static int ssl_start_renegotiation( ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00004199{
4200 int ret;
4201
4202 SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
4203
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004204 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
4205 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004206
4207 ssl->state = SSL_HELLO_REQUEST;
4208 ssl->renegotiation = SSL_RENEGOTIATION;
4209
Paul Bakker48916f92012-09-16 19:57:18 +00004210 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4211 {
4212 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4213 return( ret );
4214 }
4215
4216 SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
4217
4218 return( 0 );
4219}
4220
4221/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004222 * Renegotiate current connection on client,
4223 * or request renegotiation on server
4224 */
4225int ssl_renegotiate( ssl_context *ssl )
4226{
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004227 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004228
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004229#if defined(POLARSSL_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004230 /* On server, just send the request */
4231 if( ssl->endpoint == SSL_IS_SERVER )
4232 {
4233 if( ssl->state != SSL_HANDSHAKE_OVER )
4234 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4235
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004236 ssl->renegotiation = SSL_RENEGOTIATION_PENDING;
4237
4238 /* Did we already try/start sending HelloRequest? */
4239 if( ssl->out_left != 0 )
4240 return( ssl_flush_output( ssl ) );
4241
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004242 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004243 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004244#endif /* POLARSSL_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004245
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004246#if defined(POLARSSL_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004247 /*
4248 * On client, either start the renegotiation process or,
4249 * if already in progress, continue the handshake
4250 */
4251 if( ssl->renegotiation != SSL_RENEGOTIATION )
4252 {
4253 if( ssl->state != SSL_HANDSHAKE_OVER )
4254 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
4255
4256 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
4257 {
4258 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
4259 return( ret );
4260 }
4261 }
4262 else
4263 {
4264 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4265 {
4266 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4267 return( ret );
4268 }
4269 }
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004270#endif /* POLARSSL_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004271
Paul Bakker37ce0ff2013-10-31 14:32:04 +01004272 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004273}
4274
4275/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004276 * Receive application data decrypted from the SSL layer
4277 */
Paul Bakker23986e52011-04-24 08:57:21 +00004278int ssl_read( ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004279{
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004280 int ret, record_read = 0;
Paul Bakker23986e52011-04-24 08:57:21 +00004281 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00004282
4283 SSL_DEBUG_MSG( 2, ( "=> read" ) );
4284
4285 if( ssl->state != SSL_HANDSHAKE_OVER )
4286 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004287 ret = ssl_handshake( ssl );
4288 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4289 {
4290 record_read = 1;
4291 }
4292 else if( ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004293 {
4294 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4295 return( ret );
4296 }
4297 }
4298
4299 if( ssl->in_offt == NULL )
4300 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004301 if( ! record_read )
Paul Bakker5121ce52009-01-03 21:22:43 +00004302 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004303 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4304 {
4305 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4306 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00004307
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004308 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4309 return( ret );
4310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004311 }
4312
4313 if( ssl->in_msglen == 0 &&
4314 ssl->in_msgtype == SSL_MSG_APPLICATION_DATA )
4315 {
4316 /*
4317 * OpenSSL sends empty messages to randomize the IV
4318 */
4319 if( ( ret = ssl_read_record( ssl ) ) != 0 )
4320 {
Paul Bakker831a7552011-05-18 13:32:51 +00004321 if( ret == POLARSSL_ERR_SSL_CONN_EOF )
4322 return( 0 );
4323
Paul Bakker5121ce52009-01-03 21:22:43 +00004324 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
4325 return( ret );
4326 }
4327 }
4328
Paul Bakker48916f92012-09-16 19:57:18 +00004329 if( ssl->in_msgtype == SSL_MSG_HANDSHAKE )
4330 {
4331 SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
4332
4333 if( ssl->endpoint == SSL_IS_CLIENT &&
4334 ( ssl->in_msg[0] != SSL_HS_HELLO_REQUEST ||
4335 ssl->in_hslen != 4 ) )
4336 {
4337 SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
4338 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4339 }
4340
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004341 if( ssl->disable_renegotiation == SSL_RENEGOTIATION_DISABLED ||
4342 ( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02004343 ssl->allow_legacy_renegotiation ==
4344 SSL_LEGACY_NO_RENEGOTIATION ) )
Paul Bakker48916f92012-09-16 19:57:18 +00004345 {
4346 SSL_DEBUG_MSG( 3, ( "ignoring renegotiation, sending alert" ) );
4347
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004348#if defined(POLARSSL_SSL_PROTO_SSL3)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004349 if( ssl->minor_ver == SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004350 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004351 /*
4352 * SSLv3 does not have a "no_renegotiation" alert
4353 */
4354 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
4355 return( ret );
4356 }
4357 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004358#endif /* POLARSSL_SSL_PROTO_SSL3 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004359#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
4360 defined(POLARSSL_SSL_PROTO_TLS1_2)
4361 if( ssl->minor_ver >= SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00004362 {
4363 if( ( ret = ssl_send_alert_message( ssl,
4364 SSL_ALERT_LEVEL_WARNING,
4365 SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
4366 {
4367 return( ret );
4368 }
Paul Bakker48916f92012-09-16 19:57:18 +00004369 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02004370 else
Paul Bakker9af723c2014-05-01 13:03:14 +02004371#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 ||
4372 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02004373 {
4374 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02004375 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02004376 }
Paul Bakker48916f92012-09-16 19:57:18 +00004377 }
4378 else
4379 {
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004380 ret = ssl_start_renegotiation( ssl );
4381 if( ret == POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO )
4382 {
4383 record_read = 1;
4384 }
4385 else if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00004386 {
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004387 SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00004388 return( ret );
4389 }
Paul Bakker48916f92012-09-16 19:57:18 +00004390 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004391
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02004392 /* If a non-handshake record was read during renego, fallthrough,
4393 * else tell the user they should call ssl_read() again */
4394 if( ! record_read )
4395 return( POLARSSL_ERR_NET_WANT_READ );
Paul Bakker48916f92012-09-16 19:57:18 +00004396 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004397 else if( ssl->renegotiation == SSL_RENEGOTIATION_PENDING )
4398 {
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02004399 ssl->renego_records_seen++;
4400
4401 if( ssl->renego_max_records >= 0 &&
4402 ssl->renego_records_seen > ssl->renego_max_records )
4403 {
4404 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
4405 "but not honored by client" ) );
4406 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
4407 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01004408 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02004409
4410 /* Fatal and closure alerts handled by ssl_read_record() */
4411 if( ssl->in_msgtype == SSL_MSG_ALERT )
4412 {
4413 SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
4414 return( POLARSSL_ERR_NET_WANT_READ );
4415 }
4416
4417 if( ssl->in_msgtype != SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00004418 {
4419 SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00004420 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00004421 }
4422
4423 ssl->in_offt = ssl->in_msg;
4424 }
4425
4426 n = ( len < ssl->in_msglen )
4427 ? len : ssl->in_msglen;
4428
4429 memcpy( buf, ssl->in_offt, n );
4430 ssl->in_msglen -= n;
4431
4432 if( ssl->in_msglen == 0 )
4433 /* all bytes consumed */
4434 ssl->in_offt = NULL;
4435 else
4436 /* more data available */
4437 ssl->in_offt += n;
4438
4439 SSL_DEBUG_MSG( 2, ( "<= read" ) );
4440
Paul Bakker23986e52011-04-24 08:57:21 +00004441 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004442}
4443
4444/*
4445 * Send application data to be encrypted by the SSL layer
4446 */
Paul Bakker23986e52011-04-24 08:57:21 +00004447int ssl_write( ssl_context *ssl, const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00004448{
Paul Bakker23986e52011-04-24 08:57:21 +00004449 int ret;
4450 size_t n;
Paul Bakker05decb22013-08-15 13:33:48 +02004451 unsigned int max_len = SSL_MAX_CONTENT_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00004452
4453 SSL_DEBUG_MSG( 2, ( "=> write" ) );
4454
4455 if( ssl->state != SSL_HANDSHAKE_OVER )
4456 {
4457 if( ( ret = ssl_handshake( ssl ) ) != 0 )
4458 {
4459 SSL_DEBUG_RET( 1, "ssl_handshake", ret );
4460 return( ret );
4461 }
4462 }
4463
Paul Bakker05decb22013-08-15 13:33:48 +02004464#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004465 /*
4466 * Assume mfl_code is correct since it was checked when set
4467 */
4468 max_len = mfl_code_to_length[ssl->mfl_code];
4469
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004470 /*
Paul Bakker05decb22013-08-15 13:33:48 +02004471 * Check if a smaller max length was negotiated
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004472 */
4473 if( ssl->session_out != NULL &&
4474 mfl_code_to_length[ssl->session_out->mfl_code] < max_len )
4475 {
4476 max_len = mfl_code_to_length[ssl->session_out->mfl_code];
4477 }
Paul Bakker05decb22013-08-15 13:33:48 +02004478#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02004479
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +02004480 n = ( len < max_len) ? len : max_len;
Paul Bakker887bd502011-06-08 13:10:54 +00004481
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 if( ssl->out_left != 0 )
4483 {
4484 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
4485 {
4486 SSL_DEBUG_RET( 1, "ssl_flush_output", ret );
4487 return( ret );
4488 }
4489 }
Paul Bakker887bd502011-06-08 13:10:54 +00004490 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00004491 {
Paul Bakker887bd502011-06-08 13:10:54 +00004492 ssl->out_msglen = n;
4493 ssl->out_msgtype = SSL_MSG_APPLICATION_DATA;
4494 memcpy( ssl->out_msg, buf, n );
4495
4496 if( ( ret = ssl_write_record( ssl ) ) != 0 )
4497 {
4498 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4499 return( ret );
4500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004501 }
4502
4503 SSL_DEBUG_MSG( 2, ( "<= write" ) );
4504
Paul Bakker23986e52011-04-24 08:57:21 +00004505 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00004506}
4507
4508/*
4509 * Notify the peer that the connection is being closed
4510 */
4511int ssl_close_notify( ssl_context *ssl )
4512{
4513 int ret;
4514
4515 SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
4516
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004517 if( ssl->out_left != 0 )
4518 return( ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004519
4520 if( ssl->state == SSL_HANDSHAKE_OVER )
4521 {
Paul Bakker48916f92012-09-16 19:57:18 +00004522 if( ( ret = ssl_send_alert_message( ssl,
4523 SSL_ALERT_LEVEL_WARNING,
4524 SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004525 {
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004526 SSL_DEBUG_RET( 1, "ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004527 return( ret );
4528 }
4529 }
4530
4531 SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
4532
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +02004533 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004534}
4535
Paul Bakker48916f92012-09-16 19:57:18 +00004536void ssl_transform_free( ssl_transform *transform )
4537{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004538 if( transform == NULL )
4539 return;
4540
Paul Bakker48916f92012-09-16 19:57:18 +00004541#if defined(POLARSSL_ZLIB_SUPPORT)
4542 deflateEnd( &transform->ctx_deflate );
4543 inflateEnd( &transform->ctx_inflate );
4544#endif
4545
Paul Bakker84bbeb52014-07-01 14:53:22 +02004546 cipher_free( &transform->cipher_ctx_enc );
4547 cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +02004548
Paul Bakker84bbeb52014-07-01 14:53:22 +02004549 md_free( &transform->md_ctx_enc );
4550 md_free( &transform->md_ctx_dec );
Paul Bakker61d113b2013-07-04 11:51:43 +02004551
Paul Bakker34617722014-06-13 17:20:13 +02004552 polarssl_zeroize( transform, sizeof( ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004553}
4554
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004555#if defined(POLARSSL_X509_CRT_PARSE_C)
4556static void ssl_key_cert_free( ssl_key_cert *key_cert )
4557{
4558 ssl_key_cert *cur = key_cert, *next;
4559
4560 while( cur != NULL )
4561 {
4562 next = cur->next;
4563
4564 if( cur->key_own_alloc )
4565 {
4566 pk_free( cur->key );
4567 polarssl_free( cur->key );
4568 }
4569 polarssl_free( cur );
4570
4571 cur = next;
4572 }
4573}
4574#endif /* POLARSSL_X509_CRT_PARSE_C */
4575
Paul Bakker48916f92012-09-16 19:57:18 +00004576void ssl_handshake_free( ssl_handshake_params *handshake )
4577{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004578 if( handshake == NULL )
4579 return;
4580
Paul Bakker48916f92012-09-16 19:57:18 +00004581#if defined(POLARSSL_DHM_C)
4582 dhm_free( &handshake->dhm_ctx );
4583#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004584#if defined(POLARSSL_ECDH_C)
4585 ecdh_free( &handshake->ecdh_ctx );
4586#endif
4587
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004588#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker9af723c2014-05-01 13:03:14 +02004589 /* explicit void pointer cast for buggy MS compiler */
4590 polarssl_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004591#endif
4592
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004593#if defined(POLARSSL_X509_CRT_PARSE_C) && \
4594 defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
4595 /*
4596 * Free only the linked list wrapper, not the keys themselves
4597 * since the belong to the SNI callback
4598 */
4599 if( handshake->sni_key_cert != NULL )
4600 {
4601 ssl_key_cert *cur = handshake->sni_key_cert, *next;
4602
4603 while( cur != NULL )
4604 {
4605 next = cur->next;
4606 polarssl_free( cur );
4607 cur = next;
4608 }
4609 }
Paul Bakker9af723c2014-05-01 13:03:14 +02004610#endif /* POLARSSL_X509_CRT_PARSE_C && POLARSSL_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004611
Paul Bakker34617722014-06-13 17:20:13 +02004612 polarssl_zeroize( handshake, sizeof( ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004613}
4614
4615void ssl_session_free( ssl_session *session )
4616{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004617 if( session == NULL )
4618 return;
4619
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004620#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakker0a597072012-09-25 21:55:46 +00004621 if( session->peer_cert != NULL )
Paul Bakker48916f92012-09-16 19:57:18 +00004622 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02004623 x509_crt_free( session->peer_cert );
Paul Bakker6e339b52013-07-03 13:37:05 +02004624 polarssl_free( session->peer_cert );
Paul Bakker48916f92012-09-16 19:57:18 +00004625 }
Paul Bakkered27a042013-04-18 22:46:23 +02004626#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004627
Paul Bakkera503a632013-08-14 13:48:06 +02004628#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004629 polarssl_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +02004630#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004631
Paul Bakker34617722014-06-13 17:20:13 +02004632 polarssl_zeroize( session, sizeof( ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +00004633}
4634
Paul Bakker5121ce52009-01-03 21:22:43 +00004635/*
4636 * Free an SSL context
4637 */
4638void ssl_free( ssl_context *ssl )
4639{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004640 if( ssl == NULL )
4641 return;
4642
Paul Bakker5121ce52009-01-03 21:22:43 +00004643 SSL_DEBUG_MSG( 2, ( "=> free" ) );
4644
Paul Bakker5121ce52009-01-03 21:22:43 +00004645 if( ssl->out_ctr != NULL )
4646 {
Paul Bakker34617722014-06-13 17:20:13 +02004647 polarssl_zeroize( ssl->out_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004648 polarssl_free( ssl->out_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004649 }
4650
4651 if( ssl->in_ctr != NULL )
4652 {
Paul Bakker34617722014-06-13 17:20:13 +02004653 polarssl_zeroize( ssl->in_ctr, SSL_BUFFER_LEN );
Paul Bakker6e339b52013-07-03 13:37:05 +02004654 polarssl_free( ssl->in_ctr );
Paul Bakker5121ce52009-01-03 21:22:43 +00004655 }
4656
Paul Bakker16770332013-10-11 09:59:44 +02004657#if defined(POLARSSL_ZLIB_SUPPORT)
4658 if( ssl->compress_buf != NULL )
4659 {
Paul Bakker34617722014-06-13 17:20:13 +02004660 polarssl_zeroize( ssl->compress_buf, SSL_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02004661 polarssl_free( ssl->compress_buf );
4662 }
4663#endif
4664
Paul Bakker40e46942009-01-03 21:51:57 +00004665#if defined(POLARSSL_DHM_C)
Paul Bakker48916f92012-09-16 19:57:18 +00004666 mpi_free( &ssl->dhm_P );
4667 mpi_free( &ssl->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00004668#endif
4669
Paul Bakker48916f92012-09-16 19:57:18 +00004670 if( ssl->transform )
4671 {
4672 ssl_transform_free( ssl->transform );
Paul Bakker6e339b52013-07-03 13:37:05 +02004673 polarssl_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00004674 }
4675
4676 if( ssl->handshake )
4677 {
4678 ssl_handshake_free( ssl->handshake );
4679 ssl_transform_free( ssl->transform_negotiate );
4680 ssl_session_free( ssl->session_negotiate );
4681
Paul Bakker6e339b52013-07-03 13:37:05 +02004682 polarssl_free( ssl->handshake );
4683 polarssl_free( ssl->transform_negotiate );
4684 polarssl_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +00004685 }
4686
Paul Bakkerc0463502013-02-14 11:19:38 +01004687 if( ssl->session )
4688 {
4689 ssl_session_free( ssl->session );
Paul Bakker6e339b52013-07-03 13:37:05 +02004690 polarssl_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01004691 }
4692
Paul Bakkera503a632013-08-14 13:48:06 +02004693#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02004694 if( ssl->ticket_keys )
4695 {
4696 ssl_ticket_keys_free( ssl->ticket_keys );
4697 polarssl_free( ssl->ticket_keys );
4698 }
Paul Bakkera503a632013-08-14 13:48:06 +02004699#endif
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02004700
Paul Bakker0be444a2013-08-27 21:55:01 +02004701#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker66d5d072014-06-17 16:39:18 +02004702 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004703 {
Paul Bakker34617722014-06-13 17:20:13 +02004704 polarssl_zeroize( ssl->hostname, ssl->hostname_len );
Paul Bakker6e339b52013-07-03 13:37:05 +02004705 polarssl_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +00004706 ssl->hostname_len = 0;
4707 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004708#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004709
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02004710#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker6db455e2013-09-18 17:29:31 +02004711 if( ssl->psk != NULL )
4712 {
Paul Bakker34617722014-06-13 17:20:13 +02004713 polarssl_zeroize( ssl->psk, ssl->psk_len );
4714 polarssl_zeroize( ssl->psk_identity, ssl->psk_identity_len );
Paul Bakker6db455e2013-09-18 17:29:31 +02004715 polarssl_free( ssl->psk );
4716 polarssl_free( ssl->psk_identity );
4717 ssl->psk_len = 0;
4718 ssl->psk_identity_len = 0;
4719 }
4720#endif
4721
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02004722#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004723 ssl_key_cert_free( ssl->key_cert );
4724#endif
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02004725
Paul Bakker05ef8352012-05-08 09:17:57 +00004726#if defined(POLARSSL_SSL_HW_RECORD_ACCEL)
4727 if( ssl_hw_record_finish != NULL )
4728 {
4729 SSL_DEBUG_MSG( 2, ( "going for ssl_hw_record_finish()" ) );
4730 ssl_hw_record_finish( ssl );
4731 }
4732#endif
4733
Paul Bakker5121ce52009-01-03 21:22:43 +00004734 SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +00004735
Paul Bakker86f04f42013-02-14 11:20:09 +01004736 /* Actually clear after last debug message */
Paul Bakker34617722014-06-13 17:20:13 +02004737 polarssl_zeroize( ssl, sizeof( ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004738}
4739
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004740#if defined(POLARSSL_PK_C)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004741/*
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004742 * Convert between POLARSSL_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02004743 */
4744unsigned char ssl_sig_from_pk( pk_context *pk )
4745{
4746#if defined(POLARSSL_RSA_C)
4747 if( pk_can_do( pk, POLARSSL_PK_RSA ) )
4748 return( SSL_SIG_RSA );
4749#endif
4750#if defined(POLARSSL_ECDSA_C)
4751 if( pk_can_do( pk, POLARSSL_PK_ECDSA ) )
4752 return( SSL_SIG_ECDSA );
4753#endif
4754 return( SSL_SIG_ANON );
4755}
4756
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004757pk_type_t ssl_pk_alg_from_sig( unsigned char sig )
4758{
4759 switch( sig )
4760 {
4761#if defined(POLARSSL_RSA_C)
4762 case SSL_SIG_RSA:
4763 return( POLARSSL_PK_RSA );
4764#endif
4765#if defined(POLARSSL_ECDSA_C)
4766 case SSL_SIG_ECDSA:
4767 return( POLARSSL_PK_ECDSA );
4768#endif
4769 default:
4770 return( POLARSSL_PK_NONE );
4771 }
4772}
Paul Bakker9af723c2014-05-01 13:03:14 +02004773#endif /* POLARSSL_PK_C */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004774
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02004775/*
4776 * Convert between SSL_HASH_XXX and POLARSSL_MD_XXX
4777 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02004778md_type_t ssl_md_alg_from_hash( unsigned char hash )
4779{
4780 switch( hash )
4781 {
4782#if defined(POLARSSL_MD5_C)
4783 case SSL_HASH_MD5:
4784 return( POLARSSL_MD_MD5 );
4785#endif
4786#if defined(POLARSSL_SHA1_C)
4787 case SSL_HASH_SHA1:
4788 return( POLARSSL_MD_SHA1 );
4789#endif
4790#if defined(POLARSSL_SHA256_C)
4791 case SSL_HASH_SHA224:
4792 return( POLARSSL_MD_SHA224 );
4793 case SSL_HASH_SHA256:
4794 return( POLARSSL_MD_SHA256 );
4795#endif
4796#if defined(POLARSSL_SHA512_C)
4797 case SSL_HASH_SHA384:
4798 return( POLARSSL_MD_SHA384 );
4799 case SSL_HASH_SHA512:
4800 return( POLARSSL_MD_SHA512 );
4801#endif
4802 default:
4803 return( POLARSSL_MD_NONE );
4804 }
4805}
4806
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01004807#if defined(POLARSSL_SSL_SET_CURVES)
4808/*
4809 * Check is a curve proposed by the peer is in our list.
4810 * Return 1 if we're willing to use it, 0 otherwise.
4811 */
4812int ssl_curve_is_acceptable( const ssl_context *ssl, ecp_group_id grp_id )
4813{
4814 const ecp_group_id *gid;
4815
4816 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
4817 if( *gid == grp_id )
4818 return( 1 );
4819
4820 return( 0 );
4821}
Paul Bakker9af723c2014-05-01 13:03:14 +02004822#endif /* POLARSSL_SSL_SET_CURVES */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004823
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004824#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004825int ssl_check_cert_usage( const x509_crt *cert,
4826 const ssl_ciphersuite_t *ciphersuite,
4827 int cert_endpoint )
4828{
4829#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4830 int usage = 0;
4831#endif
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004832#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4833 const char *ext_oid;
4834 size_t ext_len;
4835#endif
4836
4837#if !defined(POLARSSL_X509_CHECK_KEY_USAGE) && \
4838 !defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4839 ((void) cert);
4840 ((void) cert_endpoint);
4841#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004842
4843#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
4844 if( cert_endpoint == SSL_IS_SERVER )
4845 {
4846 /* Server part of the key exchange */
4847 switch( ciphersuite->key_exchange )
4848 {
4849 case POLARSSL_KEY_EXCHANGE_RSA:
4850 case POLARSSL_KEY_EXCHANGE_RSA_PSK:
4851 usage = KU_KEY_ENCIPHERMENT;
4852 break;
4853
4854 case POLARSSL_KEY_EXCHANGE_DHE_RSA:
4855 case POLARSSL_KEY_EXCHANGE_ECDHE_RSA:
4856 case POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA:
4857 usage = KU_DIGITAL_SIGNATURE;
4858 break;
4859
4860 case POLARSSL_KEY_EXCHANGE_ECDH_RSA:
4861 case POLARSSL_KEY_EXCHANGE_ECDH_ECDSA:
4862 usage = KU_KEY_AGREEMENT;
4863 break;
4864
4865 /* Don't use default: we want warnings when adding new values */
4866 case POLARSSL_KEY_EXCHANGE_NONE:
4867 case POLARSSL_KEY_EXCHANGE_PSK:
4868 case POLARSSL_KEY_EXCHANGE_DHE_PSK:
4869 case POLARSSL_KEY_EXCHANGE_ECDHE_PSK:
4870 usage = 0;
4871 }
4872 }
4873 else
4874 {
4875 /* Client auth: we only implement rsa_sign and ecdsa_sign for now */
4876 usage = KU_DIGITAL_SIGNATURE;
4877 }
4878
4879 if( x509_crt_check_key_usage( cert, usage ) != 0 )
4880 return( -1 );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004881#else
4882 ((void) ciphersuite);
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004883#endif /* POLARSSL_X509_CHECK_KEY_USAGE */
4884
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02004885#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
4886 if( cert_endpoint == SSL_IS_SERVER )
4887 {
4888 ext_oid = OID_SERVER_AUTH;
4889 ext_len = OID_SIZE( OID_SERVER_AUTH );
4890 }
4891 else
4892 {
4893 ext_oid = OID_CLIENT_AUTH;
4894 ext_len = OID_SIZE( OID_CLIENT_AUTH );
4895 }
4896
4897 if( x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
4898 return( -1 );
4899#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
4900
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02004901 return( 0 );
4902}
Paul Bakkerd6ad8e92014-04-09 17:24:14 +02004903#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02004904
4905#endif /* POLARSSL_SSL_TLS_C */